SpvBuilder.h 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. //
  2. // Copyright (C) 2014-2015 LunarG, Inc.
  3. // Copyright (C) 2015-2020 Google, Inc.
  4. // Copyright (C) 2017 ARM Limited.
  5. // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
  6. //
  7. // All rights reserved.
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions
  11. // are met:
  12. //
  13. // Redistributions of source code must retain the above copyright
  14. // notice, this list of conditions and the following disclaimer.
  15. //
  16. // Redistributions in binary form must reproduce the above
  17. // copyright notice, this list of conditions and the following
  18. // disclaimer in the documentation and/or other materials provided
  19. // with the distribution.
  20. //
  21. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  22. // contributors may be used to endorse or promote products derived
  23. // from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. // POSSIBILITY OF SUCH DAMAGE.
  37. //
  38. // "Builder" is an interface to fully build SPIR-V IR. Allocate one of
  39. // these to build (a thread safe) internal SPIR-V representation (IR),
  40. // and then dump it as a binary stream according to the SPIR-V specification.
  41. //
  42. // A Builder has a 1:1 relationship with a SPIR-V module.
  43. //
  44. #pragma once
  45. #ifndef SpvBuilder_H
  46. #define SpvBuilder_H
  47. #include "Logger.h"
  48. #define SPV_ENABLE_UTILITY_CODE
  49. #include "spirv.hpp11"
  50. #include "spvIR.h"
  51. #include "spvUtil.h"
  52. namespace spv {
  53. #include "GLSL.ext.KHR.h"
  54. #include "GLSL.ext.EXT.h"
  55. #include "NonSemanticShaderDebugInfo100.h"
  56. }
  57. #include <algorithm>
  58. #include <cstdint>
  59. #include <map>
  60. #include <memory>
  61. #include <set>
  62. #include <sstream>
  63. #include <stack>
  64. #include <unordered_map>
  65. #include <unordered_set>
  66. #include <map>
  67. namespace spv {
  68. typedef enum {
  69. Spv_1_0 = (1 << 16),
  70. Spv_1_1 = (1 << 16) | (1 << 8),
  71. Spv_1_2 = (1 << 16) | (2 << 8),
  72. Spv_1_3 = (1 << 16) | (3 << 8),
  73. Spv_1_4 = (1 << 16) | (4 << 8),
  74. Spv_1_5 = (1 << 16) | (5 << 8),
  75. Spv_1_6 = (1 << 16) | (6 << 8),
  76. } SpvVersion;
  77. struct StructMemberDebugInfo {
  78. std::string name {};
  79. int line {0};
  80. int column {0};
  81. // Set if the caller knows a better debug type than what is associated with the functional SPIR-V type.
  82. spv::Id debugTypeOverride {0};
  83. };
  84. class Builder {
  85. public:
  86. Builder(unsigned int spvVersion, unsigned int userNumber, SpvBuildLogger* logger);
  87. virtual ~Builder();
  88. static const int maxMatrixSize = 4;
  89. unsigned int getSpvVersion() const { return spvVersion; }
  90. void setSource(spv::SourceLanguage lang, int version)
  91. {
  92. sourceLang = lang;
  93. sourceVersion = version;
  94. }
  95. spv::Id getStringId(const std::string& str)
  96. {
  97. auto sItr = stringIds.find(str);
  98. if (sItr != stringIds.end())
  99. return sItr->second;
  100. spv::Id strId = getUniqueId();
  101. Instruction* fileString = new Instruction(strId, NoType, Op::OpString);
  102. const char* file_c_str = str.c_str();
  103. fileString->addStringOperand(file_c_str);
  104. strings.push_back(std::unique_ptr<Instruction>(fileString));
  105. module.mapInstruction(fileString);
  106. stringIds[file_c_str] = strId;
  107. return strId;
  108. }
  109. spv::Id getMainFileId() const { return mainFileId; }
  110. // Initialize the main source file name
  111. void setDebugMainSourceFile(const std::string& file)
  112. {
  113. if (trackDebugInfo) {
  114. dirtyLineTracker = true;
  115. mainFileId = getStringId(file);
  116. currentFileId = mainFileId;
  117. }
  118. }
  119. // Set the debug source location tracker in the builder.
  120. // The upcoming instructions in basic blocks will be associated to this location.
  121. void setDebugSourceLocation(int line, const char* filename)
  122. {
  123. if (trackDebugInfo) {
  124. dirtyLineTracker = true;
  125. if (line != 0) {
  126. // TODO: This is special handling of some AST nodes having (untracked) line 0.
  127. // But they should have a valid line number.
  128. currentLine = line;
  129. if (filename) {
  130. currentFileId = getStringId(filename);
  131. }
  132. }
  133. }
  134. }
  135. void setSourceText(const std::string& text) { sourceText = text; }
  136. void addSourceExtension(const char* ext) { sourceExtensions.push_back(ext); }
  137. void addModuleProcessed(const std::string& p) { moduleProcesses.push_back(p.c_str()); }
  138. void setEmitSpirvDebugInfo()
  139. {
  140. trackDebugInfo = true;
  141. emitSpirvDebugInfo = true;
  142. }
  143. void setEmitNonSemanticShaderDebugInfo(bool emitSourceText)
  144. {
  145. trackDebugInfo = true;
  146. emitNonSemanticShaderDebugInfo = true;
  147. importNonSemanticShaderDebugInfoInstructions();
  148. if (emitSourceText) {
  149. emitNonSemanticShaderDebugSource = emitSourceText;
  150. }
  151. }
  152. void addExtension(const char* ext) { extensions.insert(ext); }
  153. void removeExtension(const char* ext)
  154. {
  155. extensions.erase(ext);
  156. }
  157. void addIncorporatedExtension(const char* ext, SpvVersion incorporatedVersion)
  158. {
  159. if (getSpvVersion() < static_cast<unsigned>(incorporatedVersion))
  160. addExtension(ext);
  161. }
  162. void promoteIncorporatedExtension(const char* baseExt, const char* promoExt, SpvVersion incorporatedVersion)
  163. {
  164. removeExtension(baseExt);
  165. addIncorporatedExtension(promoExt, incorporatedVersion);
  166. }
  167. void addInclude(const std::string& name, const std::string& text)
  168. {
  169. spv::Id incId = getStringId(name);
  170. includeFiles[incId] = &text;
  171. }
  172. Id import(const char*);
  173. void setMemoryModel(spv::AddressingModel addr, spv::MemoryModel mem)
  174. {
  175. addressModel = addr;
  176. memoryModel = mem;
  177. }
  178. void addCapability(spv::Capability cap) { capabilities.insert(cap); }
  179. // To get a new <id> for anything needing a new one.
  180. Id getUniqueId() { return ++uniqueId; }
  181. // To get a set of new <id>s, e.g., for a set of function parameters
  182. Id getUniqueIds(int numIds)
  183. {
  184. Id id = uniqueId + 1;
  185. uniqueId += numIds;
  186. return id;
  187. }
  188. // Maps the given OpType Id to a Non-Semantic DebugType Id.
  189. Id getDebugType(Id type) {
  190. if (auto it = debugTypeIdLookup.find(type); it != debugTypeIdLookup.end()) {
  191. return it->second;
  192. }
  193. return NoType;
  194. }
  195. // Maps the given OpFunction Id to a Non-Semantic DebugFunction Id.
  196. Id getDebugFunction(Id func) {
  197. if (auto it = debugFuncIdLookup.find(func); it != debugFuncIdLookup.end()) {
  198. return it->second;
  199. }
  200. return NoResult;
  201. }
  202. // For creating new types (will return old type if the requested one was already made).
  203. Id makeVoidType();
  204. Id makeBoolType();
  205. Id makePointer(StorageClass, Id pointee);
  206. Id makeForwardPointer(StorageClass);
  207. Id makePointerFromForwardPointer(StorageClass, Id forwardPointerType, Id pointee);
  208. Id makeUntypedPointer(StorageClass storageClass, bool setBufferPointer = false);
  209. Id makeIntegerType(int width, bool hasSign); // generic
  210. Id makeIntType(int width) { return makeIntegerType(width, true); }
  211. Id makeUintType(int width) { return makeIntegerType(width, false); }
  212. Id makeFloatType(int width);
  213. Id makeBFloat16Type();
  214. Id makeFloatE5M2Type();
  215. Id makeFloatE4M3Type();
  216. Id makeStructType(const std::vector<Id>& members, const std::vector<spv::StructMemberDebugInfo>& memberDebugInfo,
  217. const char* name, bool const compilerGenerated = true);
  218. Id makeStructResultType(Id type0, Id type1);
  219. Id makeVectorType(Id component, int size);
  220. Id makeMatrixType(Id component, int cols, int rows);
  221. Id makeArrayType(Id element, Id sizeId, int stride); // 0 stride means no stride decoration
  222. Id makeRuntimeArray(Id element);
  223. Id makeFunctionType(Id returnType, const std::vector<Id>& paramTypes);
  224. Id makeImageType(Id sampledType, Dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format, const char* debugNames);
  225. Id makeSamplerType(const char* debugName);
  226. Id makeSampledImageType(Id imageType, const char* debugName);
  227. Id makeCooperativeMatrixTypeKHR(Id component, Id scope, Id rows, Id cols, Id use);
  228. Id makeCooperativeMatrixTypeNV(Id component, Id scope, Id rows, Id cols);
  229. Id makeCooperativeMatrixTypeWithSameShape(Id component, Id otherType);
  230. Id makeCooperativeVectorTypeNV(Id componentType, Id components);
  231. Id makeTensorTypeARM(Id elementType, Id rank);
  232. Id makeGenericType(spv::Op opcode, std::vector<spv::IdImmediate>& operands);
  233. // SPIR-V NonSemantic Shader DebugInfo Instructions
  234. Id makeDebugInfoNone();
  235. Id makeBoolDebugType(int const size);
  236. Id makeIntegerDebugType(int const width, bool const hasSign);
  237. Id makeFloatDebugType(int const width);
  238. Id makeSequentialDebugType(Id const baseType, Id const componentCount, NonSemanticShaderDebugInfo100Instructions const sequenceType);
  239. Id makeArrayDebugType(Id const baseType, Id const componentCount);
  240. Id makeVectorDebugType(Id const baseType, int const componentCount);
  241. Id makeMatrixDebugType(Id const vectorType, int const vectorCount, bool columnMajor = true);
  242. Id makeMemberDebugType(Id const memberType, StructMemberDebugInfo const& debugTypeLoc);
  243. Id makeCompositeDebugType(std::vector<Id> const& memberTypes, std::vector<StructMemberDebugInfo> const& memberDebugInfo,
  244. char const* const name, NonSemanticShaderDebugInfo100DebugCompositeType const tag);
  245. Id makeOpaqueDebugType(char const* const name);
  246. Id makePointerDebugType(StorageClass storageClass, Id const baseType);
  247. Id makeForwardPointerDebugType(StorageClass storageClass);
  248. Id makeDebugSource(const Id fileName);
  249. Id makeDebugCompilationUnit();
  250. Id createDebugGlobalVariable(Id const type, char const*const name, Id const variable);
  251. Id createDebugLocalVariable(Id type, char const*const name, size_t const argNumber = 0);
  252. Id makeDebugExpression();
  253. Id makeDebugDeclare(Id const debugLocalVariable, Id const pointer);
  254. Id makeDebugValue(Id const debugLocalVariable, Id const value);
  255. Id makeDebugFunctionType(Id returnType, const std::vector<Id>& paramTypes);
  256. Id makeDebugFunction(Function* function, Id nameId, Id funcTypeId);
  257. Id makeDebugLexicalBlock(uint32_t line, uint32_t column);
  258. std::string unmangleFunctionName(std::string const& name) const;
  259. // Initialize non-semantic debug information for a function, including those of:
  260. // - The function definition
  261. // - The function parameters
  262. void setupFunctionDebugInfo(Function* function, const char* name, const std::vector<Id>& paramTypes,
  263. const std::vector<char const*>& paramNames);
  264. // accelerationStructureNV type
  265. Id makeAccelerationStructureType();
  266. // rayQueryEXT type
  267. Id makeRayQueryType();
  268. // hitObjectNV type
  269. Id makeHitObjectNVType();
  270. // hitObjectEXT type
  271. Id makeHitObjectEXTType();
  272. // For querying about types.
  273. Id getTypeId(Id resultId) const { return module.getTypeId(resultId); }
  274. Id getDerefTypeId(Id resultId) const;
  275. Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); }
  276. Op getTypeClass(Id typeId) const { return getOpCode(typeId); }
  277. Op getMostBasicTypeClass(Id typeId) const;
  278. unsigned int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
  279. unsigned int getNumTypeConstituents(Id typeId) const;
  280. unsigned int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
  281. Id getScalarTypeId(Id typeId) const;
  282. Id getContainedTypeId(Id typeId) const;
  283. Id getContainedTypeId(Id typeId, int) const;
  284. StorageClass getTypeStorageClass(Id typeId) const { return module.getStorageClass(typeId); }
  285. ImageFormat getImageTypeFormat(Id typeId) const
  286. { return (ImageFormat)module.getInstruction(typeId)->getImmediateOperand(6); }
  287. Id getResultingAccessChainType() const;
  288. Id getIdOperand(Id resultId, int idx) { return module.getInstruction(resultId)->getIdOperand(idx); }
  289. Id getCooperativeVectorNumComponents(Id typeId) const { return module.getInstruction(typeId)->getIdOperand(1); }
  290. bool isPointer(Id resultId) const { return isPointerType(getTypeId(resultId)); }
  291. bool isUntypedPointer(Id resultId) const
  292. {
  293. const Id tid = getTypeId(resultId);
  294. // Expect that OpString have no type
  295. if (tid == 0)
  296. return false;
  297. return isUntypedPointerType(tid);
  298. }
  299. bool isScalar(Id resultId) const { return isScalarType(getTypeId(resultId)); }
  300. bool isVector(Id resultId) const { return isVectorType(getTypeId(resultId)); }
  301. bool isMatrix(Id resultId) const { return isMatrixType(getTypeId(resultId)); }
  302. bool isCooperativeMatrix(Id resultId)const { return isCooperativeMatrixType(getTypeId(resultId)); }
  303. bool isCooperativeVector(Id resultId)const { return isCooperativeVectorType(getTypeId(resultId)); }
  304. bool isAggregate(Id resultId) const { return isAggregateType(getTypeId(resultId)); }
  305. bool isSampledImage(Id resultId) const { return isSampledImageType(getTypeId(resultId)); }
  306. bool isTensorView(Id resultId)const { return isTensorViewType(getTypeId(resultId)); }
  307. bool isBoolType(Id typeId)
  308. { return groupedTypes[enumCast(Op::OpTypeBool)].size() > 0 && typeId == groupedTypes[enumCast(Op::OpTypeBool)].back()->getResultId(); }
  309. bool isIntType(Id typeId) const
  310. { return getTypeClass(typeId) == Op::OpTypeInt && module.getInstruction(typeId)->getImmediateOperand(1) != 0; }
  311. bool isUintType(Id typeId) const
  312. { return getTypeClass(typeId) == Op::OpTypeInt && module.getInstruction(typeId)->getImmediateOperand(1) == 0; }
  313. bool isFloatType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeFloat; }
  314. bool isPointerType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypePointer; }
  315. bool isUntypedPointerType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeUntypedPointerKHR; }
  316. bool isScalarType(Id typeId) const
  317. { return getTypeClass(typeId) == Op::OpTypeFloat || getTypeClass(typeId) == Op::OpTypeInt ||
  318. getTypeClass(typeId) == Op::OpTypeBool; }
  319. bool isVectorType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeVector; }
  320. bool isMatrixType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeMatrix; }
  321. bool isStructType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeStruct; }
  322. bool isArrayType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeArray; }
  323. bool isCooperativeMatrixType(Id typeId)const
  324. {
  325. return getTypeClass(typeId) == Op::OpTypeCooperativeMatrixKHR || getTypeClass(typeId) == Op::OpTypeCooperativeMatrixNV;
  326. }
  327. bool isTensorViewType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeTensorViewNV; }
  328. bool isCooperativeVectorType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeCooperativeVectorNV; }
  329. bool isTensorTypeARM(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeTensorARM; }
  330. bool isAggregateType(Id typeId) const
  331. { return isArrayType(typeId) || isStructType(typeId) || isCooperativeMatrixType(typeId); }
  332. bool isImageType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeImage; }
  333. bool isSamplerType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeSampler; }
  334. bool isSampledImageType(Id typeId) const { return getTypeClass(typeId) == Op::OpTypeSampledImage; }
  335. bool containsType(Id typeId, Op typeOp, unsigned int width) const;
  336. bool containsPhysicalStorageBufferOrArray(Id typeId) const;
  337. bool isConstantOpCode(Op opcode) const;
  338. bool isSpecConstantOpCode(Op opcode) const;
  339. bool isConstant(Id resultId) const { return isConstantOpCode(getOpCode(resultId)); }
  340. bool isConstantScalar(Id resultId) const { return getOpCode(resultId) == Op::OpConstant; }
  341. bool isSpecConstant(Id resultId) const { return isSpecConstantOpCode(getOpCode(resultId)); }
  342. unsigned int getConstantScalar(Id resultId) const
  343. { return module.getInstruction(resultId)->getImmediateOperand(0); }
  344. StorageClass getStorageClass(Id resultId) const { return getTypeStorageClass(getTypeId(resultId)); }
  345. bool isVariableOpCode(Op opcode) const { return opcode == Op::OpVariable; }
  346. bool isVariable(Id resultId) const { return isVariableOpCode(getOpCode(resultId)); }
  347. bool isGlobalStorage(Id resultId) const { return getStorageClass(resultId) != StorageClass::Function; }
  348. bool isGlobalVariable(Id resultId) const { return isVariable(resultId) && isGlobalStorage(resultId); }
  349. // See if a resultId is valid for use as an initializer.
  350. bool isValidInitializer(Id resultId) const { return isConstant(resultId) || isGlobalVariable(resultId); }
  351. int getScalarTypeWidth(Id typeId) const
  352. {
  353. Id scalarTypeId = getScalarTypeId(typeId);
  354. assert(getTypeClass(scalarTypeId) == Op::OpTypeInt || getTypeClass(scalarTypeId) == Op::OpTypeFloat);
  355. return module.getInstruction(scalarTypeId)->getImmediateOperand(0);
  356. }
  357. unsigned int getTypeNumColumns(Id typeId) const
  358. {
  359. assert(isMatrixType(typeId));
  360. return getNumTypeConstituents(typeId);
  361. }
  362. unsigned int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
  363. unsigned int getTypeNumRows(Id typeId) const
  364. {
  365. assert(isMatrixType(typeId));
  366. return getNumTypeComponents(getContainedTypeId(typeId));
  367. }
  368. unsigned int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
  369. Dim getTypeDimensionality(Id typeId) const
  370. {
  371. assert(isImageType(typeId));
  372. return (Dim)module.getInstruction(typeId)->getImmediateOperand(1);
  373. }
  374. Id getImageType(Id resultId) const
  375. {
  376. Id typeId = getTypeId(resultId);
  377. assert(isImageType(typeId) || isSampledImageType(typeId));
  378. return isSampledImageType(typeId) ? module.getInstruction(typeId)->getIdOperand(0) : typeId;
  379. }
  380. bool isArrayedImageType(Id typeId) const
  381. {
  382. assert(isImageType(typeId));
  383. return module.getInstruction(typeId)->getImmediateOperand(3) != 0;
  384. }
  385. // For making new constants (will return old constant if the requested one was already made).
  386. Id makeNullConstant(Id typeId);
  387. Id makeBoolConstant(bool b, bool specConstant = false);
  388. Id makeIntConstant(Id typeId, unsigned value, bool specConstant);
  389. Id makeInt64Constant(Id typeId, unsigned long long value, bool specConstant);
  390. Id makeInt8Constant(int i, bool specConstant = false)
  391. { return makeIntConstant(makeIntType(8), (unsigned)i, specConstant); }
  392. Id makeUint8Constant(unsigned u, bool specConstant = false)
  393. { return makeIntConstant(makeUintType(8), u, specConstant); }
  394. Id makeInt16Constant(int i, bool specConstant = false)
  395. { return makeIntConstant(makeIntType(16), (unsigned)i, specConstant); }
  396. Id makeUint16Constant(unsigned u, bool specConstant = false)
  397. { return makeIntConstant(makeUintType(16), u, specConstant); }
  398. Id makeIntConstant(int i, bool specConstant = false)
  399. { return makeIntConstant(makeIntType(32), (unsigned)i, specConstant); }
  400. Id makeUintConstant(unsigned u, bool specConstant = false)
  401. { return makeIntConstant(makeUintType(32), u, specConstant); }
  402. Id makeUintConstant(Scope u, bool specConstant = false)
  403. { return makeUintConstant((unsigned)u, specConstant); }
  404. Id makeUintConstant(StorageClass u, bool specConstant = false)
  405. { return makeUintConstant((unsigned)u, specConstant); }
  406. Id makeUintConstant(MemorySemanticsMask u, bool specConstant = false)
  407. { return makeUintConstant((unsigned)u, specConstant); }
  408. Id makeUintConstant(SourceLanguage u, bool specConstant = false)
  409. { return makeUintConstant((unsigned)u, specConstant); }
  410. Id makeInt64Constant(long long i, bool specConstant = false)
  411. { return makeInt64Constant(makeIntType(64), (unsigned long long)i, specConstant); }
  412. Id makeUint64Constant(unsigned long long u, bool specConstant = false)
  413. { return makeInt64Constant(makeUintType(64), u, specConstant); }
  414. Id makeFloatConstant(float f, bool specConstant = false);
  415. Id makeDoubleConstant(double d, bool specConstant = false);
  416. Id makeFloat16Constant(float f16, bool specConstant = false);
  417. Id makeBFloat16Constant(float bf16, bool specConstant = false);
  418. Id makeFloatE5M2Constant(float fe5m2, bool specConstant = false);
  419. Id makeFloatE4M3Constant(float fe4m3, bool specConstant = false);
  420. Id makeFpConstant(Id type, double d, bool specConstant = false);
  421. Id importNonSemanticShaderDebugInfoInstructions();
  422. // Turn the array of constants into a proper spv constant of the requested type.
  423. Id makeCompositeConstant(Id type, const std::vector<Id>& comps, bool specConst = false);
  424. // Methods for adding information outside the CFG.
  425. Instruction* addEntryPoint(ExecutionModel, Function*, const char* name);
  426. void addExecutionMode(Function*, ExecutionMode mode, int value1 = -1, int value2 = -1, int value3 = -1);
  427. void addExecutionMode(Function*, ExecutionMode mode, const std::vector<unsigned>& literals);
  428. void addExecutionModeId(Function*, ExecutionMode mode, const std::vector<Id>& operandIds);
  429. void addName(Id, const char* name);
  430. void addMemberName(Id, int member, const char* name);
  431. void addDecoration(Id, Decoration, int num = -1);
  432. void addDecoration(Id, Decoration, const char*);
  433. void addDecoration(Id, Decoration, const std::vector<unsigned>& literals);
  434. void addDecoration(Id, Decoration, const std::vector<const char*>& strings);
  435. void addLinkageDecoration(Id id, const char* name, spv::LinkageType linkType);
  436. void addDecorationId(Id id, Decoration, Id idDecoration);
  437. void addDecorationId(Id id, Decoration, const std::vector<Id>& operandIds);
  438. void addMemberDecoration(Id, unsigned int member, Decoration, int num = -1);
  439. void addMemberDecoration(Id, unsigned int member, Decoration, const char*);
  440. void addMemberDecoration(Id, unsigned int member, Decoration, const std::vector<unsigned>& literals);
  441. void addMemberDecoration(Id, unsigned int member, Decoration, const std::vector<const char*>& strings);
  442. void addMemberDecorationIdEXT(Id, unsigned int member, Decoration, const std::vector<unsigned>& operands);
  443. // At the end of what block do the next create*() instructions go?
  444. // Also reset current last DebugScope and current source line to unknown
  445. void setBuildPoint(Block* bp) {
  446. buildPoint = bp;
  447. dirtyLineTracker = true;
  448. dirtyScopeTracker = true;
  449. }
  450. Block* getBuildPoint() const { return buildPoint; }
  451. // Append an instruction to the end of the current build point.
  452. // Optionally, additional debug info instructions may also be prepended.
  453. void addInstruction(std::unique_ptr<Instruction> inst);
  454. // Append an instruction to the end of the current build point without prepending any debug instructions.
  455. // This is useful for insertion of some debug info instructions themselves or some control flow instructions
  456. // that are attached to its predecessor instruction.
  457. void addInstructionNoDebugInfo(std::unique_ptr<Instruction> inst);
  458. // Make the entry-point function. The returned pointer is only valid
  459. // for the lifetime of this builder.
  460. Function* makeEntryPoint(const char*);
  461. // Make a shader-style function, and create its entry block if entry is non-zero.
  462. // Return the function, pass back the entry.
  463. // The returned pointer is only valid for the lifetime of this builder.
  464. Function* makeFunctionEntry(Decoration precision, Id returnType, const char* name, LinkageType linkType,
  465. const std::vector<Id>& paramTypes,
  466. const std::vector<std::vector<Decoration>>& precisions, Block** entry = nullptr);
  467. // Create a return. An 'implicit' return is one not appearing in the source
  468. // code. In the case of an implicit return, no post-return block is inserted.
  469. void makeReturn(bool implicit, Id retVal = 0);
  470. // Initialize state and generate instructions for new lexical scope
  471. void enterLexicalBlock(uint32_t line, uint32_t column);
  472. // Set state and generate instructions to exit current lexical scope
  473. void leaveLexicalBlock();
  474. // Prepare builder for generation of instructions for a function.
  475. void enterFunction(Function const* function);
  476. // Generate all the code needed to finish up a function.
  477. void leaveFunction();
  478. // Create block terminator instruction for certain statements like
  479. // discard, terminate-invocation, terminateRayEXT, or ignoreIntersectionEXT
  480. void makeStatementTerminator(spv::Op opcode, const char *name);
  481. // Create block terminator instruction for statements that have input operands
  482. // such as OpEmitMeshTasksEXT
  483. void makeStatementTerminator(spv::Op opcode, const std::vector<Id>& operands, const char* name);
  484. // Create a global/local constant. Because OpConstant is automatically emitted by getting the constant
  485. // ids, this function only handles debug info.
  486. void createConstVariable(Id type, const char* name, Id constant, bool isGlobal);
  487. // Create a global or function local or IO variable.
  488. Id createVariable(Decoration precision, StorageClass storageClass, Id type, const char* name = nullptr,
  489. Id initializer = NoResult, bool const compilerGenerated = true);
  490. // Create an untyped global or function local or IO variable.
  491. Id createUntypedVariable(Decoration precision, StorageClass storageClass, const char* name = nullptr,
  492. Id dataType = NoResult, Id initializer = NoResult);
  493. // Create an intermediate with an undefined value.
  494. Id createUndefined(Id type);
  495. // Create load/store instruction with a remapped descriptor heap base.
  496. Instruction* createDescHeapLoadStoreBaseRemap(Id base, Op op);
  497. // Store into an Id and return the l-value
  498. void createStore(Id rValue, Id lValue, spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMask::MaskNone,
  499. spv::Scope scope = spv::Scope::Max, unsigned int alignment = 0);
  500. // Load from an Id and return it
  501. Id createLoad(Id lValue, spv::Decoration precision,
  502. spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMask::MaskNone,
  503. spv::Scope scope = spv::Scope::Max, unsigned int alignment = 0);
  504. // Create an OpAccessChain instruction
  505. Id createAccessChain(StorageClass, Id base, const std::vector<Id>& offsets);
  506. // Create an OpArrayLength instruction
  507. Id createArrayLength(Id base, unsigned int member, unsigned int bits);
  508. // Create an OpCooperativeMatrixLengthKHR instruction
  509. Id createCooperativeMatrixLengthKHR(Id type);
  510. // Create an OpCooperativeMatrixLengthNV instruction
  511. Id createCooperativeMatrixLengthNV(Id type);
  512. // Create an OpCompositeExtract instruction
  513. Id createCompositeExtract(Id composite, Id typeId, unsigned index);
  514. Id createCompositeExtract(Id composite, Id typeId, const std::vector<unsigned>& indexes);
  515. Id createCompositeInsert(Id object, Id composite, Id typeId, unsigned index);
  516. Id createCompositeInsert(Id object, Id composite, Id typeId, const std::vector<unsigned>& indexes);
  517. Id createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex);
  518. Id createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex);
  519. void createNoResultOp(Op);
  520. void createNoResultOp(Op, Id operand);
  521. void createNoResultOp(Op, const std::vector<Id>& operands);
  522. void createNoResultOp(Op, const std::vector<IdImmediate>& operands);
  523. void createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask);
  524. void createMemoryBarrier(Scope executionScope, MemorySemanticsMask memorySemantics);
  525. Id createUnaryOp(Op, Id typeId, Id operand);
  526. Id createBinOp(Op, Id typeId, Id operand1, Id operand2);
  527. Id createTriOp(Op, Id typeId, Id operand1, Id operand2, Id operand3);
  528. Id createOp(Op, Id typeId, const std::vector<Id>& operands);
  529. Id createOp(Op, Id typeId, const std::vector<IdImmediate>& operands);
  530. Id createFunctionCall(spv::Function*, const std::vector<spv::Id>&);
  531. Id createSpecConstantOp(Op, Id typeId, const std::vector<spv::Id>& operands, const std::vector<unsigned>& literals);
  532. // Take an rvalue (source) and a set of channels to extract from it to
  533. // make a new rvalue, which is returned.
  534. Id createRvalueSwizzle(Decoration precision, Id typeId, Id source, const std::vector<unsigned>& channels);
  535. // Take a copy of an lvalue (target) and a source of components, and set the
  536. // source components into the lvalue where the 'channels' say to put them.
  537. // An updated version of the target is returned.
  538. // (No true lvalue or stores are used.)
  539. Id createLvalueSwizzle(Id typeId, Id target, Id source, const std::vector<unsigned>& channels);
  540. // If both the id and precision are valid, the id
  541. // gets tagged with the requested precision.
  542. // The passed in id is always the returned id, to simplify use patterns.
  543. Id setPrecision(Id id, Decoration precision)
  544. {
  545. if (precision != NoPrecision && id != NoResult)
  546. addDecoration(id, precision);
  547. return id;
  548. }
  549. // Can smear a scalar to a vector for the following forms:
  550. // - promoteScalar(scalar, vector) // smear scalar to width of vector
  551. // - promoteScalar(vector, scalar) // smear scalar to width of vector
  552. // - promoteScalar(pointer, scalar) // smear scalar to width of what pointer points to
  553. // - promoteScalar(scalar, scalar) // do nothing
  554. // Other forms are not allowed.
  555. //
  556. // Generally, the type of 'scalar' does not need to be the same type as the components in 'vector'.
  557. // The type of the created vector is a vector of components of the same type as the scalar.
  558. //
  559. // Note: One of the arguments will change, with the result coming back that way rather than
  560. // through the return value.
  561. void promoteScalar(Decoration precision, Id& left, Id& right);
  562. // Make a value by smearing the scalar to fill the type.
  563. // vectorType should be the correct type for making a vector of scalarVal.
  564. // (No conversions are done.)
  565. Id smearScalar(Decoration precision, Id scalarVal, Id vectorType);
  566. // Create a call to a built-in function.
  567. Id createBuiltinCall(Id resultType, Id builtins, int entryPoint, const std::vector<Id>& args);
  568. // List of parameters used to create a texture operation
  569. struct TextureParameters {
  570. Id sampler;
  571. Id coords;
  572. Id bias;
  573. Id lod;
  574. Id Dref;
  575. Id offset;
  576. Id offsets;
  577. Id gradX;
  578. Id gradY;
  579. Id sample;
  580. Id component;
  581. Id texelOut;
  582. Id lodClamp;
  583. Id granularity;
  584. Id coarse;
  585. bool nonprivate;
  586. bool volatil;
  587. bool nontemporal;
  588. };
  589. // Select the correct texture operation based on all inputs, and emit the correct instruction
  590. Id createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather,
  591. bool noImplicit, const TextureParameters&, ImageOperandsMask);
  592. // Emit the OpTextureQuery* instruction that was passed in.
  593. // Figure out the right return value and type, and return it.
  594. Id createTextureQueryCall(Op, const TextureParameters&, bool isUnsignedResult);
  595. Id createSamplePositionCall(Decoration precision, Id, Id);
  596. Id createBitFieldExtractCall(Decoration precision, Id, Id, Id, bool isSigned);
  597. Id createBitFieldInsertCall(Decoration precision, Id, Id, Id, Id);
  598. // Reduction comparison for composites: For equal and not-equal resulting in a scalar.
  599. Id createCompositeCompare(Decoration precision, Id, Id, bool /* true if for equal, false if for not-equal */);
  600. // OpCompositeConstruct
  601. Id createCompositeConstruct(Id typeId, const std::vector<Id>& constituents);
  602. // vector or scalar constructor
  603. Id createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId);
  604. // matrix constructor
  605. Id createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id constructee);
  606. // coopmat conversion
  607. Id createCooperativeMatrixConversion(Id typeId, Id source);
  608. Id createCooperativeMatrixReduce(Op opcode, Id typeId, Id source, unsigned int mask, Id func);
  609. Id createCooperativeMatrixPerElementOp(Id typeId, const std::vector<Id>& operands);
  610. // Helper to use for building nested control flow with if-then-else.
  611. class If {
  612. public:
  613. If(Id condition, SelectionControlMask ctrl, Builder& builder);
  614. ~If() {}
  615. void makeBeginElse();
  616. void makeEndIf();
  617. private:
  618. If(const If&);
  619. If& operator=(If&);
  620. Builder& builder;
  621. Id condition;
  622. SelectionControlMask control;
  623. Function* function;
  624. Block* headerBlock;
  625. Block* thenBlock;
  626. Block* elseBlock;
  627. Block* mergeBlock;
  628. };
  629. // Make a switch statement. A switch has 'numSegments' of pieces of code, not containing
  630. // any case/default labels, all separated by one or more case/default labels. Each possible
  631. // case value v is a jump to the caseValues[v] segment. The defaultSegment is also in this
  632. // number space. How to compute the value is given by 'condition', as in switch(condition).
  633. //
  634. // The SPIR-V Builder will maintain the stack of post-switch merge blocks for nested switches.
  635. //
  636. // Use a defaultSegment < 0 if there is no default segment (to branch to post switch).
  637. //
  638. // Returns the right set of basic blocks to start each code segment with, so that the caller's
  639. // recursion stack can hold the memory for it.
  640. //
  641. void makeSwitch(Id condition, SelectionControlMask control, int numSegments, const std::vector<int>& caseValues,
  642. const std::vector<int>& valueToSegment, int defaultSegment, std::vector<Block*>& segmentBB);
  643. // Add a branch to the innermost switch's merge block.
  644. void addSwitchBreak(bool implicit);
  645. // Move to the next code segment, passing in the return argument in makeSwitch()
  646. void nextSwitchSegment(std::vector<Block*>& segmentBB, int segment);
  647. // Finish off the innermost switch.
  648. void endSwitch(std::vector<Block*>& segmentBB);
  649. struct LoopBlocks {
  650. LoopBlocks(Block& head, Block& body, Block& merge, Block& continue_target) :
  651. head(head), body(body), merge(merge), continue_target(continue_target) { }
  652. Block &head, &body, &merge, &continue_target;
  653. private:
  654. LoopBlocks();
  655. LoopBlocks& operator=(const LoopBlocks&) = delete;
  656. };
  657. // Start a new loop and prepare the builder to generate code for it. Until
  658. // closeLoop() is called for this loop, createLoopContinue() and
  659. // createLoopExit() will target its corresponding blocks.
  660. LoopBlocks& makeNewLoop();
  661. // Create a new block in the function containing the build point. Memory is
  662. // owned by the function object.
  663. Block& makeNewBlock();
  664. // Add a branch to the continue_target of the current (innermost) loop.
  665. void createLoopContinue();
  666. // Add an exit (e.g. "break") from the innermost loop that we're currently
  667. // in.
  668. void createLoopExit();
  669. // Close the innermost loop that you're in
  670. void closeLoop();
  671. //
  672. // Access chain design for an R-Value vs. L-Value:
  673. //
  674. // There is a single access chain the builder is building at
  675. // any particular time. Such a chain can be used to either to a load or
  676. // a store, when desired.
  677. //
  678. // Expressions can be r-values, l-values, or both, or only r-values:
  679. // a[b.c].d = .... // l-value
  680. // ... = a[b.c].d; // r-value, that also looks like an l-value
  681. // ++a[b.c].d; // r-value and l-value
  682. // (x + y)[2]; // r-value only, can't possibly be l-value
  683. //
  684. // Computing an r-value means generating code. Hence,
  685. // r-values should only be computed when they are needed, not speculatively.
  686. //
  687. // Computing an l-value means saving away information for later use in the compiler,
  688. // no code is generated until the l-value is later dereferenced. It is okay
  689. // to speculatively generate an l-value, just not okay to speculatively dereference it.
  690. //
  691. // The base of the access chain (the left-most variable or expression
  692. // from which everything is based) can be set either as an l-value
  693. // or as an r-value. Most efficient would be to set an l-value if one
  694. // is available. If an expression was evaluated, the resulting r-value
  695. // can be set as the chain base.
  696. //
  697. // The users of this single access chain can save and restore if they
  698. // want to nest or manage multiple chains.
  699. //
  700. struct AccessChain {
  701. Id base; // for l-values, pointer to the base object, for r-values, the base object
  702. std::vector<Id> indexChain;
  703. Id instr; // cache the instruction that generates this access chain
  704. std::vector<unsigned> swizzle; // each std::vector element selects the next GLSL component number
  705. Id component; // a dynamic component index, can coexist with a swizzle,
  706. // done after the swizzle, NoResult if not present
  707. Id preSwizzleBaseType; // dereferenced type, before swizzle or component is applied;
  708. // NoType unless a swizzle or component is present
  709. bool isRValue; // true if 'base' is an r-value, otherwise, base is an l-value
  710. unsigned int alignment; // bitwise OR of alignment values passed in. Accumulates worst alignment.
  711. // Only tracks base and (optional) component selection alignment.
  712. struct DescHeapInfo {
  713. Id descHeapBaseTy; // for descriptor heap, record its base data type.
  714. StorageClass descHeapStorageClass; // for descriptor heap, record its basic storage class.
  715. uint32_t descHeapBaseArrayStride; // for descriptor heap, record its explicit array stride.
  716. std::vector<Instruction*> descHeapInstId;
  717. // for descriptor heap, record its data type for loading/store results.
  718. uint32_t structRsrcTyOffsetCount;
  719. uint32_t structRsrcTyFirstArrIndex;
  720. Id structRemappedBase;
  721. };
  722. DescHeapInfo descHeapInfo;
  723. // Accumulate whether anything in the chain of structures has coherent decorations.
  724. struct CoherentFlags {
  725. CoherentFlags() { clear(); }
  726. bool isVolatile() const { return volatil; }
  727. bool isNonUniform() const { return nonUniform; }
  728. bool anyCoherent() const {
  729. return coherent || devicecoherent || queuefamilycoherent || workgroupcoherent ||
  730. subgroupcoherent || shadercallcoherent;
  731. }
  732. unsigned coherent : 1;
  733. unsigned devicecoherent : 1;
  734. unsigned queuefamilycoherent : 1;
  735. unsigned workgroupcoherent : 1;
  736. unsigned subgroupcoherent : 1;
  737. unsigned shadercallcoherent : 1;
  738. unsigned nonprivate : 1;
  739. unsigned volatil : 1;
  740. unsigned nontemporal : 1;
  741. unsigned isImage : 1;
  742. unsigned nonUniform : 1;
  743. void clear() {
  744. coherent = 0;
  745. devicecoherent = 0;
  746. queuefamilycoherent = 0;
  747. workgroupcoherent = 0;
  748. subgroupcoherent = 0;
  749. shadercallcoherent = 0;
  750. nonprivate = 0;
  751. volatil = 0;
  752. nontemporal = 0;
  753. isImage = 0;
  754. nonUniform = 0;
  755. }
  756. CoherentFlags operator |=(const CoherentFlags &other) {
  757. coherent |= other.coherent;
  758. devicecoherent |= other.devicecoherent;
  759. queuefamilycoherent |= other.queuefamilycoherent;
  760. workgroupcoherent |= other.workgroupcoherent;
  761. subgroupcoherent |= other.subgroupcoherent;
  762. shadercallcoherent |= other.shadercallcoherent;
  763. nonprivate |= other.nonprivate;
  764. volatil |= other.volatil;
  765. nontemporal = other.nontemporal;
  766. isImage |= other.isImage;
  767. nonUniform |= other.nonUniform;
  768. return *this;
  769. }
  770. };
  771. CoherentFlags coherentFlags;
  772. };
  773. //
  774. // the SPIR-V builder maintains a single active chain that
  775. // the following methods operate on
  776. //
  777. // for external save and restore
  778. AccessChain getAccessChain() { return accessChain; }
  779. void setAccessChain(AccessChain newChain) { accessChain = newChain; }
  780. // clear accessChain
  781. void clearAccessChain();
  782. Id createDescHeapAccessChain();
  783. Id createConstantSizeOfEXT(Id typeId);
  784. uint32_t isStructureHeapMember(Id id, std::vector<Id> indexChain, unsigned int idx, spv::BuiltIn* bt = nullptr,
  785. uint32_t* firstArrIndex = nullptr);
  786. // set new base as an l-value base
  787. void setAccessChainLValue(Id lValue)
  788. {
  789. assert(isPointer(lValue) || isUntypedPointer(lValue));
  790. accessChain.base = lValue;
  791. }
  792. // set new base value as an r-value
  793. void setAccessChainRValue(Id rValue)
  794. {
  795. accessChain.isRValue = true;
  796. accessChain.base = rValue;
  797. }
  798. // set access chain info for untyped descriptor heap variable
  799. void setAccessChainDescHeapInfo(StorageClass storageClass = StorageClass::Max, Id baseTy = NoResult,
  800. uint32_t explicitArrayStride = NoResult, uint32_t structRsrcTyOffsetCount = 0,
  801. spv::Id structRemappedBase = NoResult, uint32_t firstArrIndex = NoResult)
  802. {
  803. if (accessChain.descHeapInfo.descHeapStorageClass == StorageClass::Max)
  804. accessChain.descHeapInfo.descHeapStorageClass = storageClass;
  805. if (accessChain.descHeapInfo.descHeapBaseTy == NoResult)
  806. accessChain.descHeapInfo.descHeapBaseTy = baseTy;
  807. if (accessChain.descHeapInfo.descHeapBaseArrayStride == NoResult)
  808. accessChain.descHeapInfo.descHeapBaseArrayStride = explicitArrayStride;
  809. if (accessChain.descHeapInfo.structRemappedBase == NoResult)
  810. accessChain.descHeapInfo.structRemappedBase = structRemappedBase;
  811. if (accessChain.descHeapInfo.structRsrcTyOffsetCount == 0)
  812. accessChain.descHeapInfo.structRsrcTyOffsetCount = structRsrcTyOffsetCount;
  813. if (accessChain.descHeapInfo.structRsrcTyFirstArrIndex == 0)
  814. accessChain.descHeapInfo.structRsrcTyFirstArrIndex = firstArrIndex;
  815. }
  816. // push offset onto the end of the chain
  817. void accessChainPush(Id offset, AccessChain::CoherentFlags coherentFlags, unsigned int alignment)
  818. {
  819. accessChain.indexChain.push_back(offset);
  820. accessChain.coherentFlags |= coherentFlags;
  821. accessChain.alignment |= alignment;
  822. }
  823. // push new swizzle onto the end of any existing swizzle, merging into a single swizzle
  824. void accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType,
  825. AccessChain::CoherentFlags coherentFlags, unsigned int alignment);
  826. // push a dynamic component selection onto the access chain, only applicable with a
  827. // non-trivial swizzle or no swizzle
  828. void accessChainPushComponent(Id component, Id preSwizzleBaseType, AccessChain::CoherentFlags coherentFlags,
  829. unsigned int alignment)
  830. {
  831. if (accessChain.swizzle.size() != 1) {
  832. accessChain.component = component;
  833. if (accessChain.preSwizzleBaseType == NoType)
  834. accessChain.preSwizzleBaseType = preSwizzleBaseType;
  835. }
  836. accessChain.coherentFlags |= coherentFlags;
  837. accessChain.alignment |= alignment;
  838. }
  839. // use accessChain and swizzle to store value
  840. void accessChainStore(Id rvalue, Decoration nonUniform,
  841. spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMask::MaskNone,
  842. spv::Scope scope = spv::Scope::Max, unsigned int alignment = 0);
  843. // use accessChain and swizzle to load an r-value
  844. Id accessChainLoad(Decoration precision, Decoration l_nonUniform, Decoration r_nonUniform, Id ResultType,
  845. spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMask::MaskNone, spv::Scope scope = spv::Scope::Max,
  846. unsigned int alignment = 0);
  847. // Return whether or not the access chain can be represented in SPIR-V
  848. // as an l-value.
  849. // E.g., a[3].yx cannot be, while a[3].y and a[3].y[x] can be.
  850. bool isSpvLvalue() const { return accessChain.swizzle.size() <= 1; }
  851. // get the direct pointer for an l-value
  852. Id accessChainGetLValue();
  853. // Get the inferred SPIR-V type of the result of the current access chain,
  854. // based on the type of the base and the chain of dereferences.
  855. Id accessChainGetInferredType();
  856. // Add capabilities, extensions, remove unneeded decorations, etc.,
  857. // based on the resulting SPIR-V.
  858. void postProcess(bool compileOnly);
  859. // Prune unreachable blocks in the CFG and remove unneeded decorations.
  860. void postProcessCFG();
  861. // Add capabilities, extensions based on instructions in the module.
  862. void postProcessFeatures();
  863. // Hook to visit each instruction in a block in a function
  864. void postProcess(Instruction&);
  865. // Hook to visit each non-32-bit sized float/int operation in a block.
  866. void postProcessType(const Instruction&, spv::Id typeId);
  867. // move OpSampledImage instructions to be next to their users.
  868. void postProcessSamplers();
  869. void dump(std::vector<unsigned int>&) const;
  870. // Add a branch to the target block.
  871. // If set implicit, the branch instruction shouldn't have debug source location.
  872. void createBranch(bool implicit, Block* block);
  873. void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock);
  874. void createLoopMerge(Block* mergeBlock, Block* continueBlock, LoopControlMask control,
  875. const std::vector<unsigned int>& operands);
  876. // Sets to generate opcode for specialization constants.
  877. void setToSpecConstCodeGenMode() { generatingOpCodeForSpecConst = true; }
  878. // Sets to generate opcode for non-specialization constants (normal mode).
  879. void setToNormalCodeGenMode() { generatingOpCodeForSpecConst = false; }
  880. // Check if the builder is generating code for spec constants.
  881. bool isInSpecConstCodeGenMode() { return generatingOpCodeForSpecConst; }
  882. void setUseReplicatedComposites(bool use) { useReplicatedComposites = use; }
  883. private:
  884. // Helper to get size of a scalar (in bytes)
  885. unsigned int postProcessGetLargestScalarSize(const Instruction& type);
  886. protected:
  887. Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value);
  888. Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2);
  889. Id findCompositeConstant(Op typeClass, Op opcode, Id typeId, const std::vector<Id>& comps, size_t numMembers);
  890. Id findStructConstant(Id typeId, const std::vector<Id>& comps);
  891. Id collapseAccessChain();
  892. void remapDynamicSwizzle();
  893. void transferAccessChainSwizzle(bool dynamic);
  894. void simplifyAccessChainSwizzle();
  895. void createAndSetNoPredecessorBlock(const char*);
  896. void createSelectionMerge(Block* mergeBlock, SelectionControlMask control);
  897. void dumpSourceInstructions(std::vector<unsigned int>&) const;
  898. void dumpSourceInstructions(const spv::Id fileId, const std::string& text, std::vector<unsigned int>&) const;
  899. template <class Range> void dumpInstructions(std::vector<unsigned int>& out, const Range& instructions) const;
  900. void dumpModuleProcesses(std::vector<unsigned int>&) const;
  901. spv::MemoryAccessMask sanitizeMemoryAccessForStorageClass(spv::MemoryAccessMask memoryAccess, StorageClass sc)
  902. const;
  903. struct DecorationInstructionLessThan {
  904. bool operator()(const std::unique_ptr<Instruction>& lhs, const std::unique_ptr<Instruction>& rhs) const;
  905. };
  906. unsigned int spvVersion; // the version of SPIR-V to emit in the header
  907. SourceLanguage sourceLang;
  908. int sourceVersion;
  909. spv::Id nonSemanticShaderCompilationUnitId {0};
  910. spv::Id nonSemanticShaderDebugInfo {0};
  911. spv::Id debugInfoNone {0};
  912. spv::Id debugExpression {0}; // Debug expression with zero operations.
  913. std::string sourceText;
  914. // True if an new OpLine/OpDebugLine may need to be inserted. Either:
  915. // 1. The current debug location changed
  916. // 2. The current build point changed
  917. bool dirtyLineTracker;
  918. int currentLine = 0;
  919. // OpString id of the current file name. Always 0 if debug info is off.
  920. spv::Id currentFileId = 0;
  921. // OpString id of the main file name. Always 0 if debug info is off.
  922. spv::Id mainFileId = 0;
  923. // True if an new OpDebugScope may need to be inserted. Either:
  924. // 1. A new lexical block is pushed
  925. // 2. The current build point changed
  926. bool dirtyScopeTracker;
  927. std::stack<spv::Id> currentDebugScopeId;
  928. // This flag toggles tracking of debug info while building the SPIR-V.
  929. bool trackDebugInfo = false;
  930. // This flag toggles emission of SPIR-V debug instructions, like OpLine and OpSource.
  931. bool emitSpirvDebugInfo = false;
  932. // This flag toggles emission of Non-Semantic Debug extension debug instructions.
  933. bool emitNonSemanticShaderDebugInfo = false;
  934. bool restoreNonSemanticShaderDebugInfo = false;
  935. bool emitNonSemanticShaderDebugSource = false;
  936. std::set<std::string> extensions;
  937. std::vector<const char*> sourceExtensions;
  938. std::vector<const char*> moduleProcesses;
  939. AddressingModel addressModel;
  940. MemoryModel memoryModel;
  941. std::set<spv::Capability> capabilities;
  942. int builderNumber;
  943. Module module;
  944. Block* buildPoint;
  945. Id uniqueId;
  946. Function* entryPointFunction;
  947. // This tracks the current function being built, or nullptr if not in a function.
  948. Function const* currentFunction { nullptr };
  949. bool generatingOpCodeForSpecConst;
  950. bool useReplicatedComposites { false };
  951. AccessChain accessChain;
  952. // special blocks of instructions for output
  953. std::vector<std::unique_ptr<Instruction> > strings;
  954. std::vector<std::unique_ptr<Instruction> > imports;
  955. std::vector<std::unique_ptr<Instruction> > entryPoints;
  956. std::vector<std::unique_ptr<Instruction> > executionModes;
  957. std::vector<std::unique_ptr<Instruction> > names;
  958. std::set<std::unique_ptr<Instruction>, DecorationInstructionLessThan> decorations;
  959. std::vector<std::unique_ptr<Instruction> > constantsTypesGlobals;
  960. std::vector<std::unique_ptr<Instruction> > externals;
  961. std::vector<std::unique_ptr<Function> > functions;
  962. // not output, internally used for quick & dirty canonical (unique) creation
  963. // Key for scalar constants (handles both 32-bit and 64-bit)
  964. struct ScalarConstantKey {
  965. unsigned int typeClass; // OpTypeInt, OpTypeFloat, OpTypeBool
  966. unsigned int opcode; // OpConstant, OpSpecConstant, OpConstantTrue, etc.
  967. Id typeId; // The specific type
  968. unsigned value1; // First operand (or only operand)
  969. unsigned value2; // Second operand (0 for single-operand constants)
  970. bool operator==(const ScalarConstantKey& other) const {
  971. return typeClass == other.typeClass &&
  972. opcode == other.opcode &&
  973. typeId == other.typeId &&
  974. value1 == other.value1 &&
  975. value2 == other.value2;
  976. }
  977. };
  978. struct ScalarConstantKeyHash {
  979. // 64/32 bit mix function from MurmurHash3
  980. inline std::size_t hash_mix(std::size_t h) const {
  981. if constexpr (sizeof(std::size_t) == 8) {
  982. h ^= h >> 33;
  983. h *= UINT64_C(0xff51afd7ed558ccd);
  984. h ^= h >> 33;
  985. h *= UINT64_C(0xc4ceb9fe1a85ec53);
  986. h ^= h >> 33;
  987. return h;
  988. } else {
  989. h ^= h >> 16;
  990. h *= UINT32_C(0x85ebca6b);
  991. h ^= h >> 13;
  992. h *= UINT32_C(0xc2b2ae35);
  993. h ^= h >> 16;
  994. return h;
  995. }
  996. }
  997. // Hash combine from boost
  998. inline std::size_t hash_combine(std::size_t seed, std::size_t v) const {
  999. return hash_mix(seed + 0x9e3779b9 + v);
  1000. }
  1001. std::size_t operator()(const ScalarConstantKey& k) const {
  1002. size_t hash1 = hash_combine(std::hash<unsigned>{}(k.typeClass), std::hash<unsigned>{}(k.opcode));
  1003. size_t hash2 = hash_combine(std::hash<Id>{}(k.value1), std::hash<unsigned>{}(k.value2));
  1004. size_t hash3 = hash_combine(hash1, hash2);
  1005. return hash_combine(hash3, std::hash<unsigned>{}(k.typeId));
  1006. }
  1007. };
  1008. // map type opcodes to constant inst.
  1009. std::unordered_map<unsigned int, std::vector<Instruction*>> groupedCompositeConstants;
  1010. // map struct-id to constant instructions
  1011. std::unordered_map<unsigned int, std::vector<Instruction*>> groupedStructConstants;
  1012. // map type opcodes to type instructions
  1013. std::unordered_map<unsigned int, std::vector<Instruction*>> groupedTypes;
  1014. // map type opcodes to debug type instructions
  1015. std::unordered_map<unsigned int, std::vector<Instruction*>> groupedDebugTypes;
  1016. // list of OpConstantNull instructions
  1017. std::vector<Instruction*> nullConstants;
  1018. // map scalar constants to result IDs
  1019. std::unordered_map<ScalarConstantKey, Id, ScalarConstantKeyHash> groupedScalarConstantResultIDs;
  1020. // Track which types have explicit layouts, to avoid reusing in storage classes without layout.
  1021. // Currently only tracks array types.
  1022. std::unordered_set<unsigned int> explicitlyLaidOut;
  1023. // stack of switches
  1024. std::stack<Block*> switchMerges;
  1025. // Our loop stack.
  1026. std::stack<LoopBlocks> loops;
  1027. // map from strings to their string ids
  1028. std::unordered_map<std::string, spv::Id> stringIds;
  1029. // map from include file name ids to their contents
  1030. std::map<spv::Id, const std::string*> includeFiles;
  1031. // maps from OpTypeXXX id to DebugTypeXXX id
  1032. std::unordered_map<spv::Id, spv::Id> debugTypeIdLookup;
  1033. // maps from OpFunction id to DebugFunction id
  1034. std::unordered_map<spv::Id, spv::Id> debugFuncIdLookup;
  1035. // map from file name string id to DebugSource id
  1036. std::unordered_map<spv::Id, spv::Id> debugSourceId;
  1037. // The stream for outputting warnings and errors.
  1038. SpvBuildLogger* logger;
  1039. }; // end Builder class
  1040. } // end spv namespace
  1041. #endif // SpvBuilder_H