SpvBuilder.h 48 KB

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