SpirvInstruction.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. //===- SpirvInstruction.cpp - SPIR-V Instruction Representation -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the in-memory representation of SPIR-V instructions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/SPIRV/SpirvInstruction.h"
  13. #include "clang/SPIRV/BitwiseCast.h"
  14. #include "clang/SPIRV/SpirvBasicBlock.h"
  15. #include "clang/SPIRV/SpirvFunction.h"
  16. #include "clang/SPIRV/SpirvType.h"
  17. #include "clang/SPIRV/SpirvVisitor.h"
  18. #include "clang/SPIRV/String.h"
  19. namespace clang {
  20. namespace spirv {
  21. #define DEFINE_INVOKE_VISITOR_FOR_CLASS(cls) \
  22. bool cls::invokeVisitor(Visitor *v) { return v->visit(this); }
  23. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvCapability)
  24. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvExtension)
  25. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvExtInstImport)
  26. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvMemoryModel)
  27. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvEntryPoint)
  28. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvExecutionMode)
  29. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvString)
  30. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvSource)
  31. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvModuleProcessed)
  32. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDecoration)
  33. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvVariable)
  34. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvFunctionParameter)
  35. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvLoopMerge)
  36. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvSelectionMerge)
  37. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvBranch)
  38. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvBranchConditional)
  39. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvKill)
  40. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvReturn)
  41. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvSwitch)
  42. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvUnreachable)
  43. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvAccessChain)
  44. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvAtomic)
  45. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvBarrier)
  46. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvBinaryOp)
  47. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvBitFieldExtract)
  48. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvBitFieldInsert)
  49. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvConstantBoolean)
  50. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvConstantInteger)
  51. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvConstantFloat)
  52. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvConstantComposite)
  53. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvConstantNull)
  54. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvCompositeConstruct)
  55. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvCompositeExtract)
  56. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvCompositeInsert)
  57. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvEmitVertex)
  58. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvEndPrimitive)
  59. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvExtInst)
  60. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvFunctionCall)
  61. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvNonUniformBinaryOp)
  62. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvNonUniformElect)
  63. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvNonUniformUnaryOp)
  64. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvImageOp)
  65. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvImageQuery)
  66. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvImageSparseTexelsResident)
  67. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvImageTexelPointer)
  68. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvLoad)
  69. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvCopyObject)
  70. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvSampledImage)
  71. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvSelect)
  72. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvSpecConstantBinaryOp)
  73. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvSpecConstantUnaryOp)
  74. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvStore)
  75. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvUnaryOp)
  76. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvVectorShuffle)
  77. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvArrayLength)
  78. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvRayTracingOpNV)
  79. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDemoteToHelperInvocationEXT)
  80. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugInfoNone)
  81. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugSource)
  82. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugCompilationUnit)
  83. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugFunctionDeclaration)
  84. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugFunction)
  85. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugLocalVariable)
  86. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugGlobalVariable)
  87. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugOperation)
  88. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugExpression)
  89. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugDeclare)
  90. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugLexicalBlock)
  91. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugScope)
  92. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeBasic)
  93. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeArray)
  94. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeVector)
  95. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeFunction)
  96. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeComposite)
  97. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeMember)
  98. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeTemplate)
  99. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeTemplateParameter)
  100. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvRayQueryOpKHR)
  101. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvReadClock)
  102. DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvRayTracingTerminateOpKHR)
  103. #undef DEFINE_INVOKE_VISITOR_FOR_CLASS
  104. SpirvInstruction::SpirvInstruction(Kind k, spv::Op op, QualType astType,
  105. SourceLocation loc)
  106. : kind(k), opcode(op), astResultType(astType), resultId(0), srcLoc(loc),
  107. debugName(), resultType(nullptr), resultTypeId(0),
  108. layoutRule(SpirvLayoutRule::Void), containsAlias(false),
  109. storageClass(spv::StorageClass::Function), isRValue_(false),
  110. isRelaxedPrecision_(false), isNonUniform_(false), isPrecise_(false) {}
  111. bool SpirvInstruction::isArithmeticInstruction() const {
  112. switch (opcode) {
  113. case spv::Op::OpSNegate:
  114. case spv::Op::OpFNegate:
  115. case spv::Op::OpIAdd:
  116. case spv::Op::OpFAdd:
  117. case spv::Op::OpISub:
  118. case spv::Op::OpFSub:
  119. case spv::Op::OpIMul:
  120. case spv::Op::OpFMul:
  121. case spv::Op::OpUDiv:
  122. case spv::Op::OpSDiv:
  123. case spv::Op::OpFDiv:
  124. case spv::Op::OpUMod:
  125. case spv::Op::OpSRem:
  126. case spv::Op::OpSMod:
  127. case spv::Op::OpFRem:
  128. case spv::Op::OpFMod:
  129. case spv::Op::OpVectorTimesScalar:
  130. case spv::Op::OpMatrixTimesScalar:
  131. case spv::Op::OpVectorTimesMatrix:
  132. case spv::Op::OpMatrixTimesVector:
  133. case spv::Op::OpMatrixTimesMatrix:
  134. case spv::Op::OpOuterProduct:
  135. case spv::Op::OpDot:
  136. case spv::Op::OpIAddCarry:
  137. case spv::Op::OpISubBorrow:
  138. case spv::Op::OpUMulExtended:
  139. case spv::Op::OpSMulExtended:
  140. return true;
  141. default:
  142. return false;
  143. }
  144. }
  145. SpirvCapability::SpirvCapability(SourceLocation loc, spv::Capability cap)
  146. : SpirvInstruction(IK_Capability, spv::Op::OpCapability, QualType(), loc),
  147. capability(cap) {}
  148. bool SpirvCapability::operator==(const SpirvCapability &that) const {
  149. return capability == that.capability;
  150. }
  151. SpirvExtension::SpirvExtension(SourceLocation loc,
  152. llvm::StringRef extensionName)
  153. : SpirvInstruction(IK_Extension, spv::Op::OpExtension, QualType(), loc),
  154. extName(extensionName) {}
  155. bool SpirvExtension::operator==(const SpirvExtension &that) const {
  156. return extName == that.extName;
  157. }
  158. SpirvExtInstImport::SpirvExtInstImport(SourceLocation loc,
  159. llvm::StringRef extensionName)
  160. : SpirvInstruction(IK_ExtInstImport, spv::Op::OpExtInstImport, QualType(),
  161. loc),
  162. extName(extensionName) {}
  163. SpirvMemoryModel::SpirvMemoryModel(spv::AddressingModel addrModel,
  164. spv::MemoryModel memModel)
  165. : SpirvInstruction(IK_MemoryModel, spv::Op::OpMemoryModel, QualType(),
  166. /*SrcLoc*/ {}),
  167. addressModel(addrModel), memoryModel(memModel) {}
  168. SpirvEntryPoint::SpirvEntryPoint(SourceLocation loc,
  169. spv::ExecutionModel executionModel,
  170. SpirvFunction *entryPointFn,
  171. llvm::StringRef nameStr,
  172. llvm::ArrayRef<SpirvVariable *> iface)
  173. : SpirvInstruction(IK_EntryPoint, spv::Op::OpEntryPoint, QualType(), loc),
  174. execModel(executionModel), entryPoint(entryPointFn), name(nameStr),
  175. interfaceVec(iface.begin(), iface.end()) {}
  176. // OpExecutionMode and OpExecutionModeId instructions
  177. SpirvExecutionMode::SpirvExecutionMode(SourceLocation loc, SpirvFunction *entry,
  178. spv::ExecutionMode em,
  179. llvm::ArrayRef<uint32_t> paramsVec,
  180. bool usesIdParams)
  181. : SpirvInstruction(IK_ExecutionMode,
  182. usesIdParams ? spv::Op::OpExecutionModeId
  183. : spv::Op::OpExecutionMode,
  184. QualType(), loc),
  185. entryPoint(entry), execMode(em),
  186. params(paramsVec.begin(), paramsVec.end()) {}
  187. SpirvString::SpirvString(SourceLocation loc, llvm::StringRef stringLiteral)
  188. : SpirvInstruction(IK_String, spv::Op::OpString, QualType(), loc),
  189. str(stringLiteral) {}
  190. SpirvSource::SpirvSource(SourceLocation loc, spv::SourceLanguage language,
  191. uint32_t ver, SpirvString *fileString,
  192. llvm::StringRef src)
  193. : SpirvInstruction(IK_Source, spv::Op::OpSource, QualType(), loc),
  194. lang(language), version(ver), file(fileString), source(src) {}
  195. SpirvModuleProcessed::SpirvModuleProcessed(SourceLocation loc,
  196. llvm::StringRef processStr)
  197. : SpirvInstruction(IK_ModuleProcessed, spv::Op::OpModuleProcessed,
  198. QualType(), loc),
  199. process(processStr) {}
  200. SpirvDecoration::SpirvDecoration(SourceLocation loc,
  201. SpirvInstruction *targetInst,
  202. spv::Decoration decor,
  203. llvm::ArrayRef<uint32_t> p,
  204. llvm::Optional<uint32_t> idx)
  205. : SpirvInstruction(IK_Decoration, getDecorateOpcode(decor, idx),
  206. /*type*/ {}, loc),
  207. target(targetInst), decoration(decor), index(idx),
  208. params(p.begin(), p.end()), idParams() {}
  209. SpirvDecoration::SpirvDecoration(SourceLocation loc,
  210. SpirvInstruction *targetInst,
  211. spv::Decoration decor,
  212. llvm::StringRef strParam,
  213. llvm::Optional<uint32_t> idx)
  214. : SpirvInstruction(IK_Decoration, getDecorateOpcode(decor, idx),
  215. /*type*/ {}, loc),
  216. target(targetInst), decoration(decor), index(idx), params(), idParams() {
  217. const auto &stringWords = string::encodeSPIRVString(strParam);
  218. params.insert(params.end(), stringWords.begin(), stringWords.end());
  219. }
  220. SpirvDecoration::SpirvDecoration(SourceLocation loc,
  221. SpirvInstruction *targetInst,
  222. spv::Decoration decor,
  223. llvm::ArrayRef<SpirvInstruction *> ids)
  224. : SpirvInstruction(IK_Decoration, spv::Op::OpDecorateId,
  225. /*type*/ {}, loc),
  226. target(targetInst), decoration(decor), index(llvm::None), params(),
  227. idParams(ids.begin(), ids.end()) {}
  228. spv::Op SpirvDecoration::getDecorateOpcode(
  229. spv::Decoration decoration, const llvm::Optional<uint32_t> &memberIndex) {
  230. if (decoration == spv::Decoration::HlslSemanticGOOGLE ||
  231. decoration == spv::Decoration::UserTypeGOOGLE)
  232. return memberIndex.hasValue() ? spv::Op::OpMemberDecorateStringGOOGLE
  233. : spv::Op::OpDecorateStringGOOGLE;
  234. return memberIndex.hasValue() ? spv::Op::OpMemberDecorate
  235. : spv::Op::OpDecorate;
  236. }
  237. bool SpirvDecoration::operator==(const SpirvDecoration &that) const {
  238. return target == that.target && decoration == that.decoration &&
  239. params == that.params && idParams == that.idParams &&
  240. index.hasValue() == that.index.hasValue() &&
  241. (!index.hasValue() || index.getValue() == that.index.getValue());
  242. }
  243. SpirvVariable::SpirvVariable(QualType resultType, SourceLocation loc,
  244. spv::StorageClass sc, bool precise,
  245. SpirvInstruction *initializerInst)
  246. : SpirvInstruction(IK_Variable, spv::Op::OpVariable, resultType, loc),
  247. initializer(initializerInst), descriptorSet(-1), binding(-1),
  248. hlslUserType("") {
  249. setStorageClass(sc);
  250. setPrecise(precise);
  251. }
  252. SpirvVariable::SpirvVariable(const SpirvType *spvType, SourceLocation loc,
  253. spv::StorageClass sc, bool precise,
  254. SpirvInstruction *initializerInst)
  255. : SpirvInstruction(IK_Variable, spv::Op::OpVariable, QualType(), loc),
  256. initializer(initializerInst), descriptorSet(-1), binding(-1),
  257. hlslUserType("") {
  258. setResultType(spvType);
  259. setStorageClass(sc);
  260. setPrecise(precise);
  261. }
  262. SpirvFunctionParameter::SpirvFunctionParameter(QualType resultType,
  263. bool isPrecise,
  264. SourceLocation loc)
  265. : SpirvInstruction(IK_FunctionParameter, spv::Op::OpFunctionParameter,
  266. resultType, loc) {
  267. setPrecise(isPrecise);
  268. }
  269. SpirvFunctionParameter::SpirvFunctionParameter(const SpirvType *spvType,
  270. bool isPrecise,
  271. SourceLocation loc)
  272. : SpirvInstruction(IK_FunctionParameter, spv::Op::OpFunctionParameter,
  273. QualType(), loc) {
  274. setResultType(spvType);
  275. setPrecise(isPrecise);
  276. }
  277. SpirvMerge::SpirvMerge(Kind kind, spv::Op op, SourceLocation loc,
  278. SpirvBasicBlock *mergeLabel)
  279. : SpirvInstruction(kind, op, QualType(), loc), mergeBlock(mergeLabel) {}
  280. SpirvLoopMerge::SpirvLoopMerge(SourceLocation loc, SpirvBasicBlock *mergeBlock,
  281. SpirvBasicBlock *contTarget,
  282. spv::LoopControlMask mask)
  283. : SpirvMerge(IK_LoopMerge, spv::Op::OpLoopMerge, loc, mergeBlock),
  284. continueTarget(contTarget), loopControlMask(mask) {}
  285. SpirvSelectionMerge::SpirvSelectionMerge(SourceLocation loc,
  286. SpirvBasicBlock *mergeBlock,
  287. spv::SelectionControlMask mask)
  288. : SpirvMerge(IK_SelectionMerge, spv::Op::OpSelectionMerge, loc, mergeBlock),
  289. selControlMask(mask) {}
  290. SpirvTerminator::SpirvTerminator(Kind kind, spv::Op op, SourceLocation loc)
  291. : SpirvInstruction(kind, op, QualType(), loc) {}
  292. SpirvBranching::SpirvBranching(Kind kind, spv::Op op, SourceLocation loc)
  293. : SpirvTerminator(kind, op, loc) {}
  294. SpirvBranch::SpirvBranch(SourceLocation loc, SpirvBasicBlock *target)
  295. : SpirvBranching(IK_Branch, spv::Op::OpBranch, loc), targetLabel(target) {}
  296. SpirvBranchConditional::SpirvBranchConditional(SourceLocation loc,
  297. SpirvInstruction *cond,
  298. SpirvBasicBlock *trueInst,
  299. SpirvBasicBlock *falseInst)
  300. : SpirvBranching(IK_BranchConditional, spv::Op::OpBranchConditional, loc),
  301. condition(cond), trueLabel(trueInst), falseLabel(falseInst) {}
  302. SpirvKill::SpirvKill(SourceLocation loc)
  303. : SpirvTerminator(IK_Kill, spv::Op::OpKill, loc) {}
  304. SpirvReturn::SpirvReturn(SourceLocation loc, SpirvInstruction *retVal)
  305. : SpirvTerminator(IK_Return,
  306. retVal ? spv::Op::OpReturnValue : spv::Op::OpReturn, loc),
  307. returnValue(retVal) {}
  308. SpirvSwitch::SpirvSwitch(
  309. SourceLocation loc, SpirvInstruction *selectorInst,
  310. SpirvBasicBlock *defaultLbl,
  311. llvm::ArrayRef<std::pair<uint32_t, SpirvBasicBlock *>> &targetsVec)
  312. : SpirvBranching(IK_Switch, spv::Op::OpSwitch, loc), selector(selectorInst),
  313. defaultLabel(defaultLbl), targets(targetsVec.begin(), targetsVec.end()) {}
  314. // Switch instruction methods.
  315. SpirvBasicBlock *SpirvSwitch::getTargetLabelForLiteral(uint32_t lit) const {
  316. for (auto pair : targets)
  317. if (pair.first == lit)
  318. return pair.second;
  319. return defaultLabel;
  320. }
  321. llvm::ArrayRef<SpirvBasicBlock *> SpirvSwitch::getTargetBranches() const {
  322. llvm::SmallVector<SpirvBasicBlock *, 4> branches;
  323. for (auto pair : targets)
  324. branches.push_back(pair.second);
  325. branches.push_back(defaultLabel);
  326. return branches;
  327. }
  328. SpirvUnreachable::SpirvUnreachable(SourceLocation loc)
  329. : SpirvTerminator(IK_Unreachable, spv::Op::OpUnreachable, loc) {}
  330. SpirvAccessChain::SpirvAccessChain(QualType resultType, SourceLocation loc,
  331. SpirvInstruction *baseInst,
  332. llvm::ArrayRef<SpirvInstruction *> indexVec)
  333. : SpirvInstruction(IK_AccessChain, spv::Op::OpAccessChain, resultType, loc),
  334. base(baseInst), indices(indexVec.begin(), indexVec.end()) {}
  335. SpirvAtomic::SpirvAtomic(spv::Op op, QualType resultType, SourceLocation loc,
  336. SpirvInstruction *pointerInst, spv::Scope s,
  337. spv::MemorySemanticsMask mask,
  338. SpirvInstruction *valueInst)
  339. : SpirvInstruction(IK_Atomic, op, resultType, loc), pointer(pointerInst),
  340. scope(s), memorySemantic(mask),
  341. memorySemanticUnequal(spv::MemorySemanticsMask::MaskNone),
  342. value(valueInst), comparator(nullptr) {
  343. assert(
  344. op == spv::Op::OpAtomicLoad || op == spv::Op::OpAtomicIIncrement ||
  345. op == spv::Op::OpAtomicIDecrement || op == spv::Op::OpAtomicFlagClear ||
  346. op == spv::Op::OpAtomicFlagTestAndSet || op == spv::Op::OpAtomicStore ||
  347. op == spv::Op::OpAtomicAnd || op == spv::Op::OpAtomicOr ||
  348. op == spv::Op::OpAtomicXor || op == spv::Op::OpAtomicIAdd ||
  349. op == spv::Op::OpAtomicISub || op == spv::Op::OpAtomicSMin ||
  350. op == spv::Op::OpAtomicUMin || op == spv::Op::OpAtomicSMax ||
  351. op == spv::Op::OpAtomicUMax || op == spv::Op::OpAtomicExchange);
  352. }
  353. SpirvAtomic::SpirvAtomic(spv::Op op, QualType resultType, SourceLocation loc,
  354. SpirvInstruction *pointerInst, spv::Scope s,
  355. spv::MemorySemanticsMask semanticsEqual,
  356. spv::MemorySemanticsMask semanticsUnequal,
  357. SpirvInstruction *valueInst,
  358. SpirvInstruction *comparatorInst)
  359. : SpirvInstruction(IK_Atomic, op, resultType, loc), pointer(pointerInst),
  360. scope(s), memorySemantic(semanticsEqual),
  361. memorySemanticUnequal(semanticsUnequal), value(valueInst),
  362. comparator(comparatorInst) {
  363. assert(op == spv::Op::OpAtomicCompareExchange);
  364. }
  365. SpirvBarrier::SpirvBarrier(SourceLocation loc, spv::Scope memScope,
  366. spv::MemorySemanticsMask memSemantics,
  367. llvm::Optional<spv::Scope> execScope)
  368. : SpirvInstruction(IK_Barrier,
  369. execScope.hasValue() ? spv::Op::OpControlBarrier
  370. : spv::Op::OpMemoryBarrier,
  371. QualType(), loc),
  372. memoryScope(memScope), memorySemantics(memSemantics),
  373. executionScope(execScope) {}
  374. SpirvBinaryOp::SpirvBinaryOp(spv::Op opcode, QualType resultType,
  375. SourceLocation loc, SpirvInstruction *op1,
  376. SpirvInstruction *op2)
  377. : SpirvInstruction(IK_BinaryOp, opcode, resultType, loc), operand1(op1),
  378. operand2(op2) {}
  379. SpirvBitField::SpirvBitField(Kind kind, spv::Op op, QualType resultType,
  380. SourceLocation loc, SpirvInstruction *baseInst,
  381. SpirvInstruction *offsetInst,
  382. SpirvInstruction *countInst)
  383. : SpirvInstruction(kind, op, resultType, loc), base(baseInst),
  384. offset(offsetInst), count(countInst) {}
  385. SpirvBitFieldExtract::SpirvBitFieldExtract(
  386. QualType resultType, SourceLocation loc, SpirvInstruction *baseInst,
  387. SpirvInstruction *offsetInst, SpirvInstruction *countInst, bool isSigned)
  388. : SpirvBitField(IK_BitFieldExtract,
  389. isSigned ? spv::Op::OpBitFieldSExtract
  390. : spv::Op::OpBitFieldUExtract,
  391. resultType, loc, baseInst, offsetInst, countInst) {}
  392. SpirvBitFieldInsert::SpirvBitFieldInsert(QualType resultType,
  393. SourceLocation loc,
  394. SpirvInstruction *baseInst,
  395. SpirvInstruction *insertInst,
  396. SpirvInstruction *offsetInst,
  397. SpirvInstruction *countInst)
  398. : SpirvBitField(IK_BitFieldInsert, spv::Op::OpBitFieldInsert, resultType,
  399. loc, baseInst, offsetInst, countInst),
  400. insert(insertInst) {}
  401. SpirvCompositeConstruct::SpirvCompositeConstruct(
  402. QualType resultType, SourceLocation loc,
  403. llvm::ArrayRef<SpirvInstruction *> constituentsVec)
  404. : SpirvInstruction(IK_CompositeConstruct, spv::Op::OpCompositeConstruct,
  405. resultType, loc),
  406. consituents(constituentsVec.begin(), constituentsVec.end()) {}
  407. SpirvConstant::SpirvConstant(Kind kind, spv::Op op, const SpirvType *spvType)
  408. : SpirvInstruction(kind, op, QualType(),
  409. /*SourceLocation*/ {}) {
  410. setResultType(spvType);
  411. }
  412. SpirvConstant::SpirvConstant(Kind kind, spv::Op op, QualType resultType)
  413. : SpirvInstruction(kind, op, resultType,
  414. /*SourceLocation*/ {}) {}
  415. bool SpirvConstant::isSpecConstant() const {
  416. return opcode == spv::Op::OpSpecConstant ||
  417. opcode == spv::Op::OpSpecConstantTrue ||
  418. opcode == spv::Op::OpSpecConstantFalse ||
  419. opcode == spv::Op::OpSpecConstantComposite;
  420. }
  421. SpirvConstantBoolean::SpirvConstantBoolean(QualType type, bool val,
  422. bool isSpecConst)
  423. : SpirvConstant(IK_ConstantBoolean,
  424. val ? (isSpecConst ? spv::Op::OpSpecConstantTrue
  425. : spv::Op::OpConstantTrue)
  426. : (isSpecConst ? spv::Op::OpSpecConstantFalse
  427. : spv::Op::OpConstantFalse),
  428. type),
  429. value(val) {}
  430. bool SpirvConstantBoolean::operator==(const SpirvConstantBoolean &that) const {
  431. return resultType == that.resultType && astResultType == that.astResultType &&
  432. value == that.value && opcode == that.opcode;
  433. }
  434. SpirvConstantInteger::SpirvConstantInteger(QualType type, llvm::APInt val,
  435. bool isSpecConst)
  436. : SpirvConstant(IK_ConstantInteger,
  437. isSpecConst ? spv::Op::OpSpecConstant : spv::Op::OpConstant,
  438. type),
  439. value(val) {
  440. assert(type->isIntegerType());
  441. }
  442. bool SpirvConstantInteger::operator==(const SpirvConstantInteger &that) const {
  443. return resultType == that.resultType && astResultType == that.astResultType &&
  444. value == that.value && opcode == that.opcode;
  445. }
  446. SpirvConstantFloat::SpirvConstantFloat(QualType type, llvm::APFloat val,
  447. bool isSpecConst)
  448. : SpirvConstant(IK_ConstantFloat,
  449. isSpecConst ? spv::Op::OpSpecConstant : spv::Op::OpConstant,
  450. type),
  451. value(val) {
  452. assert(type->isFloatingType());
  453. }
  454. bool SpirvConstantFloat::operator==(const SpirvConstantFloat &that) const {
  455. return resultType == that.resultType && astResultType == that.astResultType &&
  456. value.bitwiseIsEqual(that.value) && opcode == that.opcode;
  457. }
  458. SpirvConstantComposite::SpirvConstantComposite(
  459. QualType type, llvm::ArrayRef<SpirvConstant *> constituentsVec,
  460. bool isSpecConst)
  461. : SpirvConstant(IK_ConstantComposite,
  462. isSpecConst ? spv::Op::OpSpecConstantComposite
  463. : spv::Op::OpConstantComposite,
  464. type),
  465. constituents(constituentsVec.begin(), constituentsVec.end()) {}
  466. SpirvConstantNull::SpirvConstantNull(QualType type)
  467. : SpirvConstant(IK_ConstantNull, spv::Op::OpConstantNull, type) {}
  468. bool SpirvConstantNull::operator==(const SpirvConstantNull &that) const {
  469. return opcode == that.opcode && resultType == that.resultType &&
  470. astResultType == that.astResultType;
  471. }
  472. SpirvCompositeExtract::SpirvCompositeExtract(QualType resultType,
  473. SourceLocation loc,
  474. SpirvInstruction *compositeInst,
  475. llvm::ArrayRef<uint32_t> indexVec)
  476. : SpirvInstruction(IK_CompositeExtract, spv::Op::OpCompositeExtract,
  477. resultType, loc),
  478. composite(compositeInst), indices(indexVec.begin(), indexVec.end()) {}
  479. SpirvCompositeInsert::SpirvCompositeInsert(QualType resultType,
  480. SourceLocation loc,
  481. SpirvInstruction *compositeInst,
  482. SpirvInstruction *objectInst,
  483. llvm::ArrayRef<uint32_t> indexVec)
  484. : SpirvInstruction(IK_CompositeInsert, spv::Op::OpCompositeInsert,
  485. resultType, loc),
  486. composite(compositeInst), object(objectInst),
  487. indices(indexVec.begin(), indexVec.end()) {}
  488. SpirvEmitVertex::SpirvEmitVertex(SourceLocation loc)
  489. : SpirvInstruction(IK_EmitVertex, spv::Op::OpEmitVertex, QualType(), loc) {}
  490. SpirvEndPrimitive::SpirvEndPrimitive(SourceLocation loc)
  491. : SpirvInstruction(IK_EndPrimitive, spv::Op::OpEndPrimitive, QualType(),
  492. loc) {}
  493. SpirvExtInst::SpirvExtInst(QualType resultType, SourceLocation loc,
  494. SpirvExtInstImport *set, uint32_t inst,
  495. llvm::ArrayRef<SpirvInstruction *> operandsVec)
  496. : SpirvInstruction(IK_ExtInst, spv::Op::OpExtInst, resultType, loc),
  497. instructionSet(set), instruction(inst),
  498. operands(operandsVec.begin(), operandsVec.end()) {}
  499. SpirvFunctionCall::SpirvFunctionCall(QualType resultType, SourceLocation loc,
  500. SpirvFunction *fn,
  501. llvm::ArrayRef<SpirvInstruction *> argsVec)
  502. : SpirvInstruction(IK_FunctionCall, spv::Op::OpFunctionCall, resultType,
  503. loc),
  504. function(fn), args(argsVec.begin(), argsVec.end()) {}
  505. SpirvGroupNonUniformOp::SpirvGroupNonUniformOp(Kind kind, spv::Op op,
  506. QualType resultType,
  507. SourceLocation loc,
  508. spv::Scope scope)
  509. : SpirvInstruction(kind, op, resultType, loc), execScope(scope) {}
  510. SpirvNonUniformBinaryOp::SpirvNonUniformBinaryOp(
  511. spv::Op op, QualType resultType, SourceLocation loc, spv::Scope scope,
  512. SpirvInstruction *arg1Inst, SpirvInstruction *arg2Inst)
  513. : SpirvGroupNonUniformOp(IK_GroupNonUniformBinaryOp, op, resultType, loc,
  514. scope),
  515. arg1(arg1Inst), arg2(arg2Inst) {
  516. assert(op == spv::Op::OpGroupNonUniformBroadcast ||
  517. op == spv::Op::OpGroupNonUniformBallotBitExtract ||
  518. op == spv::Op::OpGroupNonUniformShuffle ||
  519. op == spv::Op::OpGroupNonUniformShuffleXor ||
  520. op == spv::Op::OpGroupNonUniformShuffleUp ||
  521. op == spv::Op::OpGroupNonUniformShuffleDown ||
  522. op == spv::Op::OpGroupNonUniformQuadBroadcast ||
  523. op == spv::Op::OpGroupNonUniformQuadSwap);
  524. }
  525. SpirvNonUniformElect::SpirvNonUniformElect(QualType resultType,
  526. SourceLocation loc, spv::Scope scope)
  527. : SpirvGroupNonUniformOp(IK_GroupNonUniformElect,
  528. spv::Op::OpGroupNonUniformElect, resultType, loc,
  529. scope) {}
  530. SpirvNonUniformUnaryOp::SpirvNonUniformUnaryOp(
  531. spv::Op op, QualType resultType, SourceLocation loc, spv::Scope scope,
  532. llvm::Optional<spv::GroupOperation> group, SpirvInstruction *argInst)
  533. : SpirvGroupNonUniformOp(IK_GroupNonUniformUnaryOp, op, resultType, loc,
  534. scope),
  535. arg(argInst), groupOp(group) {
  536. assert(op == spv::Op::OpGroupNonUniformAll ||
  537. op == spv::Op::OpGroupNonUniformAny ||
  538. op == spv::Op::OpGroupNonUniformAllEqual ||
  539. op == spv::Op::OpGroupNonUniformBroadcastFirst ||
  540. op == spv::Op::OpGroupNonUniformBallot ||
  541. op == spv::Op::OpGroupNonUniformInverseBallot ||
  542. op == spv::Op::OpGroupNonUniformBallotBitCount ||
  543. op == spv::Op::OpGroupNonUniformBallotFindLSB ||
  544. op == spv::Op::OpGroupNonUniformBallotFindMSB ||
  545. op == spv::Op::OpGroupNonUniformIAdd ||
  546. op == spv::Op::OpGroupNonUniformFAdd ||
  547. op == spv::Op::OpGroupNonUniformIMul ||
  548. op == spv::Op::OpGroupNonUniformFMul ||
  549. op == spv::Op::OpGroupNonUniformSMin ||
  550. op == spv::Op::OpGroupNonUniformUMin ||
  551. op == spv::Op::OpGroupNonUniformFMin ||
  552. op == spv::Op::OpGroupNonUniformSMax ||
  553. op == spv::Op::OpGroupNonUniformUMax ||
  554. op == spv::Op::OpGroupNonUniformFMax ||
  555. op == spv::Op::OpGroupNonUniformBitwiseAnd ||
  556. op == spv::Op::OpGroupNonUniformBitwiseOr ||
  557. op == spv::Op::OpGroupNonUniformBitwiseXor ||
  558. op == spv::Op::OpGroupNonUniformLogicalAnd ||
  559. op == spv::Op::OpGroupNonUniformLogicalOr ||
  560. op == spv::Op::OpGroupNonUniformLogicalXor);
  561. }
  562. SpirvImageOp::SpirvImageOp(
  563. spv::Op op, QualType resultType, SourceLocation loc,
  564. SpirvInstruction *imageInst, SpirvInstruction *coordinateInst,
  565. spv::ImageOperandsMask mask, SpirvInstruction *drefInst,
  566. SpirvInstruction *biasInst, SpirvInstruction *lodInst,
  567. SpirvInstruction *gradDxInst, SpirvInstruction *gradDyInst,
  568. SpirvInstruction *constOffsetInst, SpirvInstruction *offsetInst,
  569. SpirvInstruction *constOffsetsInst, SpirvInstruction *sampleInst,
  570. SpirvInstruction *minLodInst, SpirvInstruction *componentInst,
  571. SpirvInstruction *texelToWriteInst)
  572. : SpirvInstruction(IK_ImageOp, op, resultType, loc), image(imageInst),
  573. coordinate(coordinateInst), dref(drefInst), bias(biasInst), lod(lodInst),
  574. gradDx(gradDxInst), gradDy(gradDyInst), constOffset(constOffsetInst),
  575. offset(offsetInst), constOffsets(constOffsetsInst), sample(sampleInst),
  576. minLod(minLodInst), component(componentInst),
  577. texelToWrite(texelToWriteInst), operandsMask(mask) {
  578. assert(op == spv::Op::OpImageSampleImplicitLod ||
  579. op == spv::Op::OpImageSampleExplicitLod ||
  580. op == spv::Op::OpImageSampleDrefImplicitLod ||
  581. op == spv::Op::OpImageSampleDrefExplicitLod ||
  582. op == spv::Op::OpImageSparseSampleImplicitLod ||
  583. op == spv::Op::OpImageSparseSampleExplicitLod ||
  584. op == spv::Op::OpImageSparseSampleDrefImplicitLod ||
  585. op == spv::Op::OpImageSparseSampleDrefExplicitLod ||
  586. op == spv::Op::OpImageFetch || op == spv::Op::OpImageSparseFetch ||
  587. op == spv::Op::OpImageGather || op == spv::Op::OpImageSparseGather ||
  588. op == spv::Op::OpImageDrefGather ||
  589. op == spv::Op::OpImageSparseDrefGather || op == spv::Op::OpImageRead ||
  590. op == spv::Op::OpImageSparseRead || op == spv::Op::OpImageWrite);
  591. if (op == spv::Op::OpImageSampleExplicitLod ||
  592. op == spv::Op::OpImageSampleDrefExplicitLod ||
  593. op == spv::Op::OpImageSparseSampleExplicitLod ||
  594. op == spv::Op::OpImageSparseSampleDrefExplicitLod) {
  595. assert(lod || (gradDx && gradDy));
  596. }
  597. if (op == spv::Op::OpImageSampleDrefImplicitLod ||
  598. op == spv::Op::OpImageSampleDrefExplicitLod ||
  599. op == spv::Op::OpImageSparseSampleDrefImplicitLod ||
  600. op == spv::Op::OpImageSparseSampleDrefExplicitLod ||
  601. op == spv::Op::OpImageDrefGather ||
  602. op == spv::Op::OpImageSparseDrefGather) {
  603. assert(dref);
  604. }
  605. if (op == spv::Op::OpImageWrite) {
  606. assert(texelToWrite);
  607. }
  608. if (op == spv::Op::OpImageGather || op == spv::Op::OpImageSparseGather) {
  609. assert(component);
  610. }
  611. }
  612. bool SpirvImageOp::isSparse() const {
  613. return opcode == spv::Op::OpImageSparseSampleImplicitLod ||
  614. opcode == spv::Op::OpImageSparseSampleExplicitLod ||
  615. opcode == spv::Op::OpImageSparseSampleDrefImplicitLod ||
  616. opcode == spv::Op::OpImageSparseSampleDrefExplicitLod ||
  617. opcode == spv::Op::OpImageSparseFetch ||
  618. opcode == spv::Op::OpImageSparseGather ||
  619. opcode == spv::Op::OpImageSparseDrefGather ||
  620. opcode == spv::Op::OpImageSparseRead;
  621. }
  622. SpirvImageQuery::SpirvImageQuery(spv::Op op, QualType resultType,
  623. SourceLocation loc, SpirvInstruction *img,
  624. SpirvInstruction *lodInst,
  625. SpirvInstruction *coordInst)
  626. : SpirvInstruction(IK_ImageQuery, op, resultType, loc), image(img),
  627. lod(lodInst), coordinate(coordInst) {
  628. assert(op == spv::Op::OpImageQueryFormat ||
  629. op == spv::Op::OpImageQueryOrder || op == spv::Op::OpImageQuerySize ||
  630. op == spv::Op::OpImageQueryLevels ||
  631. op == spv::Op::OpImageQuerySamples || op == spv::Op::OpImageQueryLod ||
  632. op == spv::Op::OpImageQuerySizeLod);
  633. if (lodInst)
  634. assert(op == spv::Op::OpImageQuerySizeLod);
  635. if (coordInst)
  636. assert(op == spv::Op::OpImageQueryLod);
  637. }
  638. SpirvImageSparseTexelsResident::SpirvImageSparseTexelsResident(
  639. QualType resultType, SourceLocation loc, SpirvInstruction *resCode)
  640. : SpirvInstruction(IK_ImageSparseTexelsResident,
  641. spv::Op::OpImageSparseTexelsResident, resultType, loc),
  642. residentCode(resCode) {}
  643. SpirvImageTexelPointer::SpirvImageTexelPointer(QualType resultType,
  644. SourceLocation loc,
  645. SpirvInstruction *imageInst,
  646. SpirvInstruction *coordinateInst,
  647. SpirvInstruction *sampleInst)
  648. : SpirvInstruction(IK_ImageTexelPointer, spv::Op::OpImageTexelPointer,
  649. resultType, loc),
  650. image(imageInst), coordinate(coordinateInst), sample(sampleInst) {}
  651. SpirvLoad::SpirvLoad(QualType resultType, SourceLocation loc,
  652. SpirvInstruction *pointerInst,
  653. llvm::Optional<spv::MemoryAccessMask> mask)
  654. : SpirvInstruction(IK_Load, spv::Op::OpLoad, resultType, loc),
  655. pointer(pointerInst), memoryAccess(mask) {}
  656. SpirvCopyObject::SpirvCopyObject(QualType resultType, SourceLocation loc,
  657. SpirvInstruction *pointerInst)
  658. : SpirvInstruction(IK_CopyObject, spv::Op::OpCopyObject, resultType, loc),
  659. pointer(pointerInst) {}
  660. SpirvSampledImage::SpirvSampledImage(QualType resultType, SourceLocation loc,
  661. SpirvInstruction *imageInst,
  662. SpirvInstruction *samplerInst)
  663. : SpirvInstruction(IK_SampledImage, spv::Op::OpSampledImage, resultType,
  664. loc),
  665. image(imageInst), sampler(samplerInst) {}
  666. SpirvSelect::SpirvSelect(QualType resultType, SourceLocation loc,
  667. SpirvInstruction *cond, SpirvInstruction *trueInst,
  668. SpirvInstruction *falseInst)
  669. : SpirvInstruction(IK_Select, spv::Op::OpSelect, resultType, loc),
  670. condition(cond), trueObject(trueInst), falseObject(falseInst) {}
  671. SpirvSpecConstantBinaryOp::SpirvSpecConstantBinaryOp(spv::Op specConstantOp,
  672. QualType resultType,
  673. SourceLocation loc,
  674. SpirvInstruction *op1,
  675. SpirvInstruction *op2)
  676. : SpirvInstruction(IK_SpecConstantBinaryOp, spv::Op::OpSpecConstantOp,
  677. resultType, loc),
  678. specOp(specConstantOp), operand1(op1), operand2(op2) {}
  679. SpirvSpecConstantUnaryOp::SpirvSpecConstantUnaryOp(spv::Op specConstantOp,
  680. QualType resultType,
  681. SourceLocation loc,
  682. SpirvInstruction *op)
  683. : SpirvInstruction(IK_SpecConstantUnaryOp, spv::Op::OpSpecConstantOp,
  684. resultType, loc),
  685. specOp(specConstantOp), operand(op) {}
  686. SpirvStore::SpirvStore(SourceLocation loc, SpirvInstruction *pointerInst,
  687. SpirvInstruction *objectInst,
  688. llvm::Optional<spv::MemoryAccessMask> mask)
  689. : SpirvInstruction(IK_Store, spv::Op::OpStore, QualType(), loc),
  690. pointer(pointerInst), object(objectInst), memoryAccess(mask) {}
  691. SpirvUnaryOp::SpirvUnaryOp(spv::Op opcode, QualType resultType,
  692. SourceLocation loc, SpirvInstruction *op)
  693. : SpirvInstruction(IK_UnaryOp, opcode, resultType, loc), operand(op) {}
  694. bool SpirvUnaryOp::isConversionOp() const {
  695. return opcode == spv::Op::OpConvertFToU || opcode == spv::Op::OpConvertFToS ||
  696. opcode == spv::Op::OpConvertSToF || opcode == spv::Op::OpConvertUToF ||
  697. opcode == spv::Op::OpUConvert || opcode == spv::Op::OpSConvert ||
  698. opcode == spv::Op::OpFConvert || opcode == spv::Op::OpQuantizeToF16 ||
  699. opcode == spv::Op::OpBitcast;
  700. }
  701. SpirvVectorShuffle::SpirvVectorShuffle(QualType resultType, SourceLocation loc,
  702. SpirvInstruction *vec1Inst,
  703. SpirvInstruction *vec2Inst,
  704. llvm::ArrayRef<uint32_t> componentsVec)
  705. : SpirvInstruction(IK_VectorShuffle, spv::Op::OpVectorShuffle, resultType,
  706. loc),
  707. vec1(vec1Inst), vec2(vec2Inst),
  708. components(componentsVec.begin(), componentsVec.end()) {}
  709. SpirvArrayLength::SpirvArrayLength(QualType resultType, SourceLocation loc,
  710. SpirvInstruction *structure_,
  711. uint32_t memberLiteral)
  712. : SpirvInstruction(IK_ArrayLength, spv::Op::OpArrayLength, resultType, loc),
  713. structure(structure_), arrayMember(memberLiteral) {}
  714. SpirvRayTracingOpNV::SpirvRayTracingOpNV(
  715. QualType resultType, spv::Op opcode,
  716. llvm::ArrayRef<SpirvInstruction *> vecOperands, SourceLocation loc)
  717. : SpirvInstruction(IK_RayTracingOpNV, opcode, resultType, loc),
  718. operands(vecOperands.begin(), vecOperands.end()) {}
  719. SpirvDemoteToHelperInvocationEXT::SpirvDemoteToHelperInvocationEXT(
  720. SourceLocation loc)
  721. : SpirvInstruction(IK_DemoteToHelperInvocationEXT,
  722. spv::Op::OpDemoteToHelperInvocationEXT, /*QualType*/ {},
  723. loc) {}
  724. // Note: we are using a null result type in the constructor. All debug
  725. // instructions should later get OpTypeVoid as their result type.
  726. SpirvDebugInstruction::SpirvDebugInstruction(Kind kind, uint32_t opcode)
  727. : SpirvInstruction(kind, spv::Op::OpExtInst,
  728. /*result type */ {},
  729. /*SourceLocation*/ {}),
  730. debugOpcode(opcode), debugSpirvType(nullptr), debugType(nullptr),
  731. instructionSet(nullptr) {}
  732. SpirvDebugInfoNone::SpirvDebugInfoNone()
  733. : SpirvDebugInstruction(IK_DebugInfoNone, /*opcode*/ 0u) {}
  734. SpirvDebugSource::SpirvDebugSource(llvm::StringRef f, llvm::StringRef t)
  735. : SpirvDebugInstruction(IK_DebugSource, /*opcode*/ 35u), file(f), text(t) {}
  736. SpirvDebugCompilationUnit::SpirvDebugCompilationUnit(uint32_t spvVer,
  737. uint32_t dwarfVer,
  738. SpirvDebugSource *src)
  739. : SpirvDebugInstruction(IK_DebugCompilationUnit, /*opcode*/ 1u),
  740. spirvVersion(spvVer), dwarfVersion(dwarfVer), source(src),
  741. lang(spv::SourceLanguage::HLSL) {}
  742. SpirvDebugFunction::SpirvDebugFunction(
  743. llvm::StringRef name, SpirvDebugSource *src, uint32_t fline, uint32_t fcol,
  744. SpirvDebugInstruction *parent, llvm::StringRef linkName, uint32_t flags_,
  745. uint32_t bodyLine, SpirvFunction *func)
  746. : SpirvDebugInstruction(IK_DebugFunction, /*opcode*/ 20u), source(src),
  747. fnLine(fline), fnColumn(fcol), parentScope(parent), linkageName(linkName),
  748. flags(flags_), scopeLine(bodyLine), fn(func), debugNone(nullptr),
  749. fnType(nullptr) {
  750. debugName = name;
  751. }
  752. SpirvDebugFunctionDeclaration::SpirvDebugFunctionDeclaration(
  753. llvm::StringRef name, SpirvDebugSource *src, uint32_t fline, uint32_t fcol,
  754. SpirvDebugInstruction *parent, llvm::StringRef linkName, uint32_t flags_)
  755. : SpirvDebugInstruction(IK_DebugFunctionDecl, /*opcode*/ 19u), source(src),
  756. fnLine(fline), fnColumn(fcol), parentScope(parent), linkageName(linkName),
  757. flags(flags_) {
  758. debugName = name;
  759. }
  760. SpirvDebugLocalVariable::SpirvDebugLocalVariable(
  761. QualType debugQualType_, llvm::StringRef varName, SpirvDebugSource *src,
  762. uint32_t lineNumber, uint32_t colNumber, SpirvDebugInstruction *parent,
  763. uint32_t flags_, llvm::Optional<uint32_t> argNumber_)
  764. : SpirvDebugInstruction(IK_DebugLocalVariable, /*opcode*/ 26u), source(src),
  765. line(lineNumber), column(colNumber), parentScope(parent), flags(flags_),
  766. argNumber(argNumber_) {
  767. debugName = varName;
  768. setDebugQualType(debugQualType_);
  769. }
  770. SpirvDebugGlobalVariable::SpirvDebugGlobalVariable(
  771. QualType debugQualType, llvm::StringRef varName, SpirvDebugSource *src,
  772. uint32_t line_, uint32_t column_, SpirvDebugInstruction *parent,
  773. llvm::StringRef linkageName_, SpirvVariable *var_, uint32_t flags_,
  774. llvm::Optional<SpirvInstruction *> staticMemberDebugDecl_)
  775. : SpirvDebugInstruction(IK_DebugGlobalVariable, /*opcode*/ 18u),
  776. source(src), line(line_), column(column_), parentScope(parent),
  777. linkageName(linkageName_), var(var_), flags(flags_),
  778. staticMemberDebugDecl(staticMemberDebugDecl_) {
  779. debugName = varName;
  780. setDebugQualType(debugQualType);
  781. setDebugType(nullptr);
  782. }
  783. SpirvDebugOperation::SpirvDebugOperation(uint32_t operationOpCode_,
  784. llvm::ArrayRef<int32_t> operands_)
  785. : SpirvDebugInstruction(IK_DebugOperation, /*opcode*/ 30u),
  786. operationOpcode(operationOpCode_),
  787. operands(operands_.begin(), operands_.end()) {}
  788. SpirvDebugExpression::SpirvDebugExpression(
  789. llvm::ArrayRef<SpirvDebugOperation *> operations_)
  790. : SpirvDebugInstruction(IK_DebugExpression, /*opcode*/ 31u),
  791. operations(operations_.begin(), operations_.end()) {}
  792. SpirvDebugDeclare::SpirvDebugDeclare(SpirvDebugLocalVariable *debugVar_,
  793. SpirvInstruction *var_,
  794. SpirvDebugExpression *expr)
  795. : SpirvDebugInstruction(IK_DebugDeclare, /*opcode*/ 28u),
  796. debugVar(debugVar_), var(var_), expression(expr) {}
  797. SpirvDebugLexicalBlock::SpirvDebugLexicalBlock(SpirvDebugSource *source_,
  798. uint32_t line_, uint32_t column_,
  799. SpirvDebugInstruction *parent_)
  800. : SpirvDebugInstruction(IK_DebugLexicalBlock, /*opcode*/ 21u),
  801. source(source_), line(line_), column(column_), parent(parent_) {}
  802. SpirvDebugScope::SpirvDebugScope(SpirvDebugInstruction *scope_)
  803. : SpirvDebugInstruction(IK_DebugScope, /*opcode*/ 23u), scope(scope_) {}
  804. SpirvDebugTypeBasic::SpirvDebugTypeBasic(llvm::StringRef name,
  805. SpirvConstant *size_,
  806. uint32_t encoding_)
  807. : SpirvDebugType(IK_DebugTypeBasic, /*opcode*/ 2u), size(size_),
  808. encoding(encoding_) {
  809. debugName = name;
  810. }
  811. uint32_t SpirvDebugTypeBasic::getSizeInBits() const {
  812. auto *size_ = dyn_cast<SpirvConstantInteger>(size);
  813. assert(size_ && "Size of DebugTypeBasic must be int type const.");
  814. return size_->getValue().getLimitedValue();
  815. }
  816. SpirvDebugTypeArray::SpirvDebugTypeArray(SpirvDebugType *elemType,
  817. llvm::ArrayRef<uint32_t> elemCount)
  818. : SpirvDebugType(IK_DebugTypeArray, /*opcode*/ 5u), elementType(elemType),
  819. elementCount(elemCount.begin(), elemCount.end()) {}
  820. SpirvDebugTypeVector::SpirvDebugTypeVector(SpirvDebugType *elemType,
  821. uint32_t elemCount)
  822. : SpirvDebugType(IK_DebugTypeVector, /*opcode*/ 6u), elementType(elemType),
  823. elementCount(elemCount) {}
  824. SpirvDebugTypeFunction::SpirvDebugTypeFunction(
  825. uint32_t flags, SpirvDebugType *ret,
  826. llvm::ArrayRef<SpirvDebugType *> params)
  827. : SpirvDebugType(IK_DebugTypeFunction, /*opcode*/ 8u), debugFlags(flags),
  828. returnType(ret), paramTypes(params.begin(), params.end()) {}
  829. SpirvDebugTypeMember::SpirvDebugTypeMember(
  830. llvm::StringRef name, SpirvDebugType *type, SpirvDebugSource *source_,
  831. uint32_t line_, uint32_t column_, SpirvDebugInstruction *parent_,
  832. uint32_t flags_, uint32_t offsetInBits_, uint32_t sizeInBits_,
  833. const APValue *value_)
  834. : SpirvDebugType(IK_DebugTypeMember, /*opcode*/ 11u), source(source_),
  835. line(line_), column(column_), parent(parent_),
  836. offsetInBits(offsetInBits_), sizeInBits(sizeInBits_), debugFlags(flags_),
  837. value(value_) {
  838. debugName = name;
  839. setDebugType(type);
  840. }
  841. SpirvDebugTypeComposite::SpirvDebugTypeComposite(
  842. llvm::StringRef name, SpirvDebugSource *source_, uint32_t line_,
  843. uint32_t column_, SpirvDebugInstruction *parent_,
  844. llvm::StringRef linkageName_, uint32_t flags_, uint32_t tag_)
  845. : SpirvDebugType(IK_DebugTypeComposite, /*opcode*/ 10u), source(source_),
  846. line(line_), column(column_), parent(parent_), linkageName(linkageName_),
  847. debugFlags(flags_), tag(tag_), debugNone(nullptr) {
  848. debugName = name;
  849. }
  850. SpirvDebugTypeTemplate::SpirvDebugTypeTemplate(
  851. SpirvDebugInstruction *target_,
  852. const llvm::SmallVector<SpirvDebugTypeTemplateParameter *, 2> &params_)
  853. : SpirvDebugType(IK_DebugTypeTemplate, /*opcode*/ 14u), target(target_),
  854. params(params_) {}
  855. SpirvDebugTypeTemplateParameter::SpirvDebugTypeTemplateParameter(
  856. llvm::StringRef name, SpirvDebugType *type, SpirvInstruction *value_,
  857. SpirvDebugSource *source_, uint32_t line_, uint32_t column_)
  858. : SpirvDebugType(IK_DebugTypeTemplateParameter, /*opcode*/ 15u),
  859. actualType(type), value(value_), source(source_), line(line_),
  860. column(column_) {
  861. debugName = name;
  862. }
  863. SpirvRayQueryOpKHR::SpirvRayQueryOpKHR(
  864. QualType resultType, spv::Op opcode,
  865. llvm::ArrayRef<SpirvInstruction *> vecOperands, bool flags,
  866. SourceLocation loc)
  867. : SpirvInstruction(IK_RayQueryOpKHR, opcode, resultType, loc),
  868. operands(vecOperands.begin(), vecOperands.end()), cullFlags(flags) {}
  869. SpirvReadClock::SpirvReadClock(QualType resultType, SpirvInstruction *s,
  870. SourceLocation loc)
  871. : SpirvInstruction(IK_ReadClock, spv::Op::OpReadClockKHR, resultType, loc),
  872. scope(s) {}
  873. SpirvRayTracingTerminateOpKHR::SpirvRayTracingTerminateOpKHR(spv::Op opcode,
  874. SourceLocation loc)
  875. : SpirvTerminator(IK_RayTracingTerminate, opcode, loc) {
  876. assert(opcode == spv::Op::OpTerminateRayKHR ||
  877. opcode == spv::Op::OpIgnoreIntersectionKHR);
  878. }
  879. } // namespace spirv
  880. } // namespace clang