ModuleBuilder.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. //===--- ModuleBuilder.cpp - SPIR-V builder implementation ----*- 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. #include "clang/SPIRV/ModuleBuilder.h"
  10. #include "spirv/1.0//spirv.hpp11"
  11. #include "clang/SPIRV/InstBuilder.h"
  12. #include "llvm/llvm_assert/assert.h"
  13. namespace clang {
  14. namespace spirv {
  15. ModuleBuilder::ModuleBuilder(SPIRVContext *C)
  16. : theContext(*C), theModule(), theFunction(nullptr), insertPoint(nullptr),
  17. instBuilder(nullptr), glslExtSetId(0) {
  18. instBuilder.setConsumer([this](std::vector<uint32_t> &&words) {
  19. this->constructSite = std::move(words);
  20. });
  21. }
  22. std::vector<uint32_t> ModuleBuilder::takeModule() {
  23. theModule.setBound(theContext.getNextId());
  24. std::vector<uint32_t> binary;
  25. auto ib = InstBuilder([&binary](std::vector<uint32_t> &&words) {
  26. binary.insert(binary.end(), words.begin(), words.end());
  27. });
  28. theModule.take(&ib);
  29. return binary;
  30. }
  31. uint32_t ModuleBuilder::beginFunction(uint32_t funcType, uint32_t returnType,
  32. llvm::StringRef funcName, uint32_t fId) {
  33. if (theFunction) {
  34. assert(false && "found nested function");
  35. return 0;
  36. }
  37. // If the caller doesn't supply a function <result-id>, we need to get one.
  38. if (!fId)
  39. fId = theContext.takeNextId();
  40. theFunction = llvm::make_unique<Function>(
  41. returnType, fId, spv::FunctionControlMask::MaskNone, funcType);
  42. theModule.addDebugName(fId, funcName);
  43. return fId;
  44. }
  45. uint32_t ModuleBuilder::addFnParam(uint32_t ptrType, llvm::StringRef name) {
  46. assert(theFunction && "found detached parameter");
  47. const uint32_t paramId = theContext.takeNextId();
  48. theFunction->addParameter(ptrType, paramId);
  49. theModule.addDebugName(paramId, name);
  50. return paramId;
  51. }
  52. uint32_t ModuleBuilder::addFnVar(uint32_t varType, llvm::StringRef name,
  53. llvm::Optional<uint32_t> init) {
  54. assert(theFunction && "found detached local variable");
  55. const uint32_t ptrType = getPointerType(varType, spv::StorageClass::Function);
  56. const uint32_t varId = theContext.takeNextId();
  57. theFunction->addVariable(ptrType, varId, init);
  58. theModule.addDebugName(varId, name);
  59. return varId;
  60. }
  61. bool ModuleBuilder::endFunction() {
  62. if (theFunction == nullptr) {
  63. assert(false && "no active function");
  64. return false;
  65. }
  66. // Move all basic blocks into the current function.
  67. // TODO: we should adjust the order the basic blocks according to
  68. // SPIR-V validation rules.
  69. for (auto &bb : basicBlocks) {
  70. theFunction->addBasicBlock(std::move(bb.second));
  71. }
  72. basicBlocks.clear();
  73. theModule.addFunction(std::move(theFunction));
  74. theFunction.reset(nullptr);
  75. insertPoint = nullptr;
  76. return true;
  77. }
  78. uint32_t ModuleBuilder::createBasicBlock(llvm::StringRef name) {
  79. if (theFunction == nullptr) {
  80. assert(false && "found detached basic block");
  81. return 0;
  82. }
  83. const uint32_t labelId = theContext.takeNextId();
  84. basicBlocks[labelId] = llvm::make_unique<BasicBlock>(labelId, name);
  85. return labelId;
  86. }
  87. void ModuleBuilder::addSuccessor(uint32_t successorLabel) {
  88. assert(insertPoint && "null insert point");
  89. insertPoint->addSuccessor(getBasicBlock(successorLabel));
  90. }
  91. void ModuleBuilder::setMergeTarget(uint32_t mergeLabel) {
  92. assert(insertPoint && "null insert point");
  93. insertPoint->setMergeTarget(getBasicBlock(mergeLabel));
  94. }
  95. void ModuleBuilder::setContinueTarget(uint32_t continueLabel) {
  96. assert(insertPoint && "null insert point");
  97. insertPoint->setContinueTarget(getBasicBlock(continueLabel));
  98. }
  99. void ModuleBuilder::setInsertPoint(uint32_t labelId) {
  100. insertPoint = getBasicBlock(labelId);
  101. }
  102. uint32_t
  103. ModuleBuilder::createCompositeConstruct(uint32_t resultType,
  104. llvm::ArrayRef<uint32_t> constituents) {
  105. assert(insertPoint && "null insert point");
  106. const uint32_t resultId = theContext.takeNextId();
  107. instBuilder.opCompositeConstruct(resultType, resultId, constituents).x();
  108. insertPoint->appendInstruction(std::move(constructSite));
  109. return resultId;
  110. }
  111. uint32_t
  112. ModuleBuilder::createCompositeExtract(uint32_t resultType, uint32_t composite,
  113. llvm::ArrayRef<uint32_t> indexes) {
  114. assert(insertPoint && "null insert point");
  115. const uint32_t resultId = theContext.takeNextId();
  116. instBuilder.opCompositeExtract(resultType, resultId, composite, indexes).x();
  117. insertPoint->appendInstruction(std::move(constructSite));
  118. return resultId;
  119. }
  120. uint32_t
  121. ModuleBuilder::createVectorShuffle(uint32_t resultType, uint32_t vector1,
  122. uint32_t vector2,
  123. llvm::ArrayRef<uint32_t> selectors) {
  124. assert(insertPoint && "null insert point");
  125. const uint32_t resultId = theContext.takeNextId();
  126. instBuilder.opVectorShuffle(resultType, resultId, vector1, vector2, selectors)
  127. .x();
  128. insertPoint->appendInstruction(std::move(constructSite));
  129. return resultId;
  130. }
  131. uint32_t ModuleBuilder::createLoad(uint32_t resultType, uint32_t pointer) {
  132. assert(insertPoint && "null insert point");
  133. const uint32_t resultId = theContext.takeNextId();
  134. instBuilder.opLoad(resultType, resultId, pointer, llvm::None).x();
  135. insertPoint->appendInstruction(std::move(constructSite));
  136. return resultId;
  137. }
  138. void ModuleBuilder::createStore(uint32_t address, uint32_t value) {
  139. assert(insertPoint && "null insert point");
  140. instBuilder.opStore(address, value, llvm::None).x();
  141. insertPoint->appendInstruction(std::move(constructSite));
  142. }
  143. uint32_t ModuleBuilder::createFunctionCall(uint32_t returnType,
  144. uint32_t functionId,
  145. llvm::ArrayRef<uint32_t> params) {
  146. assert(insertPoint && "null insert point");
  147. const uint32_t id = theContext.takeNextId();
  148. instBuilder.opFunctionCall(returnType, id, functionId, params).x();
  149. insertPoint->appendInstruction(std::move(constructSite));
  150. return id;
  151. }
  152. uint32_t ModuleBuilder::createAccessChain(uint32_t resultType, uint32_t base,
  153. llvm::ArrayRef<uint32_t> indexes) {
  154. assert(insertPoint && "null insert point");
  155. const uint32_t id = theContext.takeNextId();
  156. instBuilder.opAccessChain(resultType, id, base, indexes).x();
  157. insertPoint->appendInstruction(std::move(constructSite));
  158. return id;
  159. }
  160. uint32_t ModuleBuilder::createUnaryOp(spv::Op op, uint32_t resultType,
  161. uint32_t operand) {
  162. assert(insertPoint && "null insert point");
  163. const uint32_t id = theContext.takeNextId();
  164. instBuilder.unaryOp(op, resultType, id, operand).x();
  165. insertPoint->appendInstruction(std::move(constructSite));
  166. return id;
  167. }
  168. uint32_t ModuleBuilder::createBinaryOp(spv::Op op, uint32_t resultType,
  169. uint32_t lhs, uint32_t rhs) {
  170. assert(insertPoint && "null insert point");
  171. const uint32_t id = theContext.takeNextId();
  172. instBuilder.binaryOp(op, resultType, id, lhs, rhs).x();
  173. insertPoint->appendInstruction(std::move(constructSite));
  174. return id;
  175. }
  176. spv::ImageOperandsMask ModuleBuilder::composeImageOperandsMask(
  177. uint32_t bias, uint32_t lod, const std::pair<uint32_t, uint32_t> &grad,
  178. uint32_t constOffset, uint32_t varOffset,
  179. llvm::SmallVectorImpl<uint32_t> *orderedParams) {
  180. using spv::ImageOperandsMask;
  181. // SPIR-V Image Operands from least significant bit to most significant bit
  182. // Bias, Lod, Grad, ConstOffset, Offset, ConstOffsets, Sample, MinLod
  183. auto mask = ImageOperandsMask::MaskNone;
  184. orderedParams->clear();
  185. if (bias) {
  186. mask = mask | ImageOperandsMask::Bias;
  187. orderedParams->push_back(bias);
  188. }
  189. if (lod) {
  190. mask = mask | ImageOperandsMask::Lod;
  191. orderedParams->push_back(lod);
  192. }
  193. if (grad.first && grad.second) {
  194. mask = mask | ImageOperandsMask::Grad;
  195. orderedParams->push_back(grad.first);
  196. orderedParams->push_back(grad.second);
  197. }
  198. if (constOffset) {
  199. mask = mask | ImageOperandsMask::ConstOffset;
  200. orderedParams->push_back(constOffset);
  201. }
  202. if (varOffset) {
  203. mask = mask | ImageOperandsMask::Offset;
  204. requireCapability(spv::Capability::ImageGatherExtended);
  205. orderedParams->push_back(varOffset);
  206. }
  207. return mask;
  208. }
  209. uint32_t ModuleBuilder::createImageSample(uint32_t texelType,
  210. uint32_t imageType, uint32_t image,
  211. uint32_t sampler, uint32_t coordinate,
  212. uint32_t bias, uint32_t lod,
  213. std::pair<uint32_t, uint32_t> grad,
  214. uint32_t constOffset,
  215. uint32_t varOffset) {
  216. assert(insertPoint && "null insert point");
  217. // An OpSampledImage is required to do the image sampling.
  218. const uint32_t sampledImgId = theContext.takeNextId();
  219. const uint32_t sampledImgTy = getSampledImageType(imageType);
  220. instBuilder.opSampledImage(sampledImgTy, sampledImgId, image, sampler).x();
  221. insertPoint->appendInstruction(std::move(constructSite));
  222. const uint32_t texelId = theContext.takeNextId();
  223. llvm::SmallVector<uint32_t, 4> params;
  224. const auto mask = composeImageOperandsMask(bias, lod, grad, constOffset,
  225. varOffset, &params);
  226. // The Lod and Grad image operands requires explicit-lod instructions.
  227. if (lod || (grad.first && grad.second)) {
  228. instBuilder.opImageSampleExplicitLod(texelType, texelId, sampledImgId,
  229. coordinate, mask);
  230. } else {
  231. instBuilder.opImageSampleImplicitLod(
  232. texelType, texelId, sampledImgId, coordinate,
  233. llvm::Optional<spv::ImageOperandsMask>(mask));
  234. }
  235. for (const auto param : params)
  236. instBuilder.idRef(param);
  237. instBuilder.x();
  238. insertPoint->appendInstruction(std::move(constructSite));
  239. return texelId;
  240. }
  241. uint32_t ModuleBuilder::createImageFetch(uint32_t texelType, uint32_t image,
  242. uint32_t coordinate, uint32_t lod,
  243. uint32_t constOffset,
  244. uint32_t varOffset) {
  245. assert(insertPoint && "null insert point");
  246. llvm::SmallVector<uint32_t, 2> params;
  247. const auto mask =
  248. llvm::Optional<spv::ImageOperandsMask>(composeImageOperandsMask(
  249. /*bias*/ 0, lod, std::make_pair(0, 0), constOffset, varOffset,
  250. &params));
  251. const uint32_t texelId = theContext.takeNextId();
  252. instBuilder.opImageFetch(texelType, texelId, image, coordinate, mask);
  253. for (const auto param : params)
  254. instBuilder.idRef(param);
  255. instBuilder.x();
  256. insertPoint->appendInstruction(std::move(constructSite));
  257. return texelId;
  258. }
  259. uint32_t ModuleBuilder::createImageGather(uint32_t texelType,
  260. uint32_t imageType, uint32_t image,
  261. uint32_t sampler, uint32_t coordinate,
  262. uint32_t component,
  263. uint32_t constOffset,
  264. uint32_t varOffset) {
  265. assert(insertPoint && "null insert point");
  266. // An OpSampledImage is required to do the image sampling.
  267. const uint32_t sampledImgId = theContext.takeNextId();
  268. const uint32_t sampledImgTy = getSampledImageType(imageType);
  269. instBuilder.opSampledImage(sampledImgTy, sampledImgId, image, sampler).x();
  270. insertPoint->appendInstruction(std::move(constructSite));
  271. llvm::SmallVector<uint32_t, 2> params;
  272. const auto mask =
  273. llvm::Optional<spv::ImageOperandsMask>(composeImageOperandsMask(
  274. /*bias*/ 0, /*lod*/ 0, std::make_pair(0, 0), constOffset, varOffset,
  275. &params));
  276. const uint32_t texelId = theContext.takeNextId();
  277. instBuilder.opImageGather(texelType, texelId, sampledImgId, coordinate,
  278. component, mask);
  279. for (const auto param : params)
  280. instBuilder.idRef(param);
  281. instBuilder.x();
  282. insertPoint->appendInstruction(std::move(constructSite));
  283. return texelId;
  284. }
  285. uint32_t ModuleBuilder::createSelect(uint32_t resultType, uint32_t condition,
  286. uint32_t trueValue, uint32_t falseValue) {
  287. assert(insertPoint && "null insert point");
  288. const uint32_t id = theContext.takeNextId();
  289. instBuilder.opSelect(resultType, id, condition, trueValue, falseValue).x();
  290. insertPoint->appendInstruction(std::move(constructSite));
  291. return id;
  292. }
  293. void ModuleBuilder::createSwitch(
  294. uint32_t mergeLabel, uint32_t selector, uint32_t defaultLabel,
  295. llvm::ArrayRef<std::pair<uint32_t, uint32_t>> target) {
  296. assert(insertPoint && "null insert point");
  297. // Create the OpSelectioMerege.
  298. instBuilder.opSelectionMerge(mergeLabel, spv::SelectionControlMask::MaskNone)
  299. .x();
  300. insertPoint->appendInstruction(std::move(constructSite));
  301. // Create the OpSwitch.
  302. instBuilder.opSwitch(selector, defaultLabel, target).x();
  303. insertPoint->appendInstruction(std::move(constructSite));
  304. }
  305. void ModuleBuilder::createKill() {
  306. assert(insertPoint && "null insert point");
  307. assert(!isCurrentBasicBlockTerminated());
  308. instBuilder.opKill().x();
  309. insertPoint->appendInstruction(std::move(constructSite));
  310. }
  311. void ModuleBuilder::createBranch(uint32_t targetLabel, uint32_t mergeBB,
  312. uint32_t continueBB,
  313. spv::LoopControlMask loopControl) {
  314. assert(insertPoint && "null insert point");
  315. if (mergeBB && continueBB) {
  316. instBuilder.opLoopMerge(mergeBB, continueBB, loopControl).x();
  317. insertPoint->appendInstruction(std::move(constructSite));
  318. }
  319. instBuilder.opBranch(targetLabel).x();
  320. insertPoint->appendInstruction(std::move(constructSite));
  321. }
  322. void ModuleBuilder::createConditionalBranch(
  323. uint32_t condition, uint32_t trueLabel, uint32_t falseLabel,
  324. uint32_t mergeLabel, uint32_t continueLabel,
  325. spv::SelectionControlMask selectionControl,
  326. spv::LoopControlMask loopControl) {
  327. assert(insertPoint && "null insert point");
  328. if (mergeLabel) {
  329. if (continueLabel) {
  330. instBuilder.opLoopMerge(mergeLabel, continueLabel, loopControl).x();
  331. insertPoint->appendInstruction(std::move(constructSite));
  332. } else {
  333. instBuilder.opSelectionMerge(mergeLabel, selectionControl).x();
  334. insertPoint->appendInstruction(std::move(constructSite));
  335. }
  336. }
  337. instBuilder.opBranchConditional(condition, trueLabel, falseLabel, {}).x();
  338. insertPoint->appendInstruction(std::move(constructSite));
  339. }
  340. void ModuleBuilder::createReturn() {
  341. assert(insertPoint && "null insert point");
  342. instBuilder.opReturn().x();
  343. insertPoint->appendInstruction(std::move(constructSite));
  344. }
  345. void ModuleBuilder::createReturnValue(uint32_t value) {
  346. assert(insertPoint && "null insert point");
  347. instBuilder.opReturnValue(value).x();
  348. insertPoint->appendInstruction(std::move(constructSite));
  349. }
  350. uint32_t ModuleBuilder::createExtInst(uint32_t resultType, uint32_t setId,
  351. uint32_t instId,
  352. llvm::ArrayRef<uint32_t> operands) {
  353. assert(insertPoint && "null insert point");
  354. uint32_t resultId = theContext.takeNextId();
  355. instBuilder.opExtInst(resultType, resultId, setId, instId, operands).x();
  356. insertPoint->appendInstruction(std::move(constructSite));
  357. return resultId;
  358. }
  359. void ModuleBuilder::addExecutionMode(uint32_t entryPointId,
  360. spv::ExecutionMode em,
  361. llvm::ArrayRef<uint32_t> params) {
  362. instBuilder.opExecutionMode(entryPointId, em);
  363. for (const auto &param : params) {
  364. instBuilder.literalInteger(param);
  365. }
  366. instBuilder.x();
  367. theModule.addExecutionMode(std::move(constructSite));
  368. }
  369. uint32_t ModuleBuilder::getGLSLExtInstSet() {
  370. if (glslExtSetId == 0) {
  371. glslExtSetId = theContext.takeNextId();
  372. theModule.addExtInstSet(glslExtSetId, "GLSL.std.450");
  373. }
  374. return glslExtSetId;
  375. }
  376. uint32_t ModuleBuilder::addStageIOVar(uint32_t type,
  377. spv::StorageClass storageClass,
  378. std::string name) {
  379. const uint32_t pointerType = getPointerType(type, storageClass);
  380. const uint32_t varId = theContext.takeNextId();
  381. instBuilder.opVariable(pointerType, varId, storageClass, llvm::None).x();
  382. theModule.addVariable(std::move(constructSite));
  383. theModule.addDebugName(varId, name);
  384. return varId;
  385. }
  386. uint32_t ModuleBuilder::addStageBuiltinVar(uint32_t type, spv::StorageClass sc,
  387. spv::BuiltIn builtin) {
  388. const uint32_t pointerType = getPointerType(type, sc);
  389. const uint32_t varId = theContext.takeNextId();
  390. instBuilder.opVariable(pointerType, varId, sc, llvm::None).x();
  391. theModule.addVariable(std::move(constructSite));
  392. // Decorate with the specified Builtin
  393. const Decoration *d = Decoration::getBuiltIn(theContext, builtin);
  394. theModule.addDecoration(d, varId);
  395. return varId;
  396. }
  397. uint32_t ModuleBuilder::addModuleVar(uint32_t type, spv::StorageClass sc,
  398. llvm::StringRef name,
  399. llvm::Optional<uint32_t> init) {
  400. assert(sc != spv::StorageClass::Function);
  401. // TODO: basically duplicated code of addFileVar()
  402. const uint32_t pointerType = getPointerType(type, sc);
  403. const uint32_t varId = theContext.takeNextId();
  404. instBuilder.opVariable(pointerType, varId, sc, init).x();
  405. theModule.addVariable(std::move(constructSite));
  406. theModule.addDebugName(varId, name);
  407. return varId;
  408. }
  409. void ModuleBuilder::decorateDSetBinding(uint32_t targetId, uint32_t setNumber,
  410. uint32_t bindingNumber) {
  411. const auto *d = Decoration::getDescriptorSet(theContext, setNumber);
  412. theModule.addDecoration(d, targetId);
  413. d = Decoration::getBinding(theContext, bindingNumber);
  414. theModule.addDecoration(d, targetId);
  415. }
  416. void ModuleBuilder::decorateLocation(uint32_t targetId, uint32_t location) {
  417. const Decoration *d =
  418. Decoration::getLocation(theContext, location, llvm::None);
  419. theModule.addDecoration(d, targetId);
  420. }
  421. void ModuleBuilder::decorate(uint32_t targetId, spv::Decoration decoration) {
  422. const Decoration *d = nullptr;
  423. switch (decoration) {
  424. case spv::Decoration::Centroid:
  425. d = Decoration::getCentroid(theContext);
  426. break;
  427. case spv::Decoration::Flat:
  428. d = Decoration::getFlat(theContext);
  429. break;
  430. case spv::Decoration::NoPerspective:
  431. d = Decoration::getNoPerspective(theContext);
  432. break;
  433. case spv::Decoration::Sample:
  434. d = Decoration::getSample(theContext);
  435. break;
  436. case spv::Decoration::Block:
  437. d = Decoration::getBlock(theContext);
  438. break;
  439. }
  440. assert(d && "unimplemented decoration");
  441. theModule.addDecoration(d, targetId);
  442. }
  443. #define IMPL_GET_PRIMITIVE_TYPE(ty) \
  444. \
  445. uint32_t ModuleBuilder::get##ty##Type() { \
  446. const Type *type = Type::get##ty(theContext); \
  447. const uint32_t typeId = theContext.getResultIdForType(type); \
  448. theModule.addType(type, typeId); \
  449. return typeId; \
  450. \
  451. }
  452. IMPL_GET_PRIMITIVE_TYPE(Void)
  453. IMPL_GET_PRIMITIVE_TYPE(Bool)
  454. IMPL_GET_PRIMITIVE_TYPE(Int32)
  455. IMPL_GET_PRIMITIVE_TYPE(Uint32)
  456. IMPL_GET_PRIMITIVE_TYPE(Float32)
  457. #undef IMPL_GET_PRIMITIVE_TYPE
  458. uint32_t ModuleBuilder::getVecType(uint32_t elemType, uint32_t elemCount) {
  459. const Type *type = nullptr;
  460. switch (elemCount) {
  461. case 2:
  462. type = Type::getVec2(theContext, elemType);
  463. break;
  464. case 3:
  465. type = Type::getVec3(theContext, elemType);
  466. break;
  467. case 4:
  468. type = Type::getVec4(theContext, elemType);
  469. break;
  470. default:
  471. assert(false && "unhandled vector size");
  472. // Error found. Return 0 as the <result-id> directly.
  473. return 0;
  474. }
  475. const uint32_t typeId = theContext.getResultIdForType(type);
  476. theModule.addType(type, typeId);
  477. return typeId;
  478. }
  479. uint32_t ModuleBuilder::getMatType(uint32_t colType, uint32_t colCount) {
  480. const Type *type = Type::getMatrix(theContext, colType, colCount);
  481. const uint32_t typeId = theContext.getResultIdForType(type);
  482. theModule.addType(type, typeId);
  483. return typeId;
  484. }
  485. uint32_t ModuleBuilder::getPointerType(uint32_t pointeeType,
  486. spv::StorageClass storageClass) {
  487. const Type *type = Type::getPointer(theContext, storageClass, pointeeType);
  488. const uint32_t typeId = theContext.getResultIdForType(type);
  489. theModule.addType(type, typeId);
  490. return typeId;
  491. }
  492. uint32_t
  493. ModuleBuilder::getStructType(llvm::ArrayRef<uint32_t> fieldTypes,
  494. llvm::StringRef structName,
  495. llvm::ArrayRef<llvm::StringRef> fieldNames,
  496. Type::DecorationSet decorations) {
  497. const Type *type = Type::getStruct(theContext, fieldTypes, decorations);
  498. bool isRegistered = false;
  499. const uint32_t typeId = theContext.getResultIdForType(type, &isRegistered);
  500. theModule.addType(type, typeId);
  501. // TODO: Probably we should check duplication and do nothing if trying to add
  502. // the same debug name for the same entity in addDebugName().
  503. if (!isRegistered) {
  504. theModule.addDebugName(typeId, structName);
  505. if (!fieldNames.empty()) {
  506. assert(fieldNames.size() == fieldTypes.size());
  507. for (uint32_t i = 0; i < fieldNames.size(); ++i)
  508. theModule.addDebugName(typeId, fieldNames[i],
  509. llvm::Optional<uint32_t>(i));
  510. }
  511. }
  512. return typeId;
  513. }
  514. uint32_t ModuleBuilder::getArrayType(uint32_t elemType, uint32_t count,
  515. Type::DecorationSet decorations) {
  516. const Type *type = Type::getArray(theContext, elemType, count, decorations);
  517. const uint32_t typeId = theContext.getResultIdForType(type);
  518. theModule.addType(type, typeId);
  519. return typeId;
  520. }
  521. uint32_t ModuleBuilder::getFunctionType(uint32_t returnType,
  522. llvm::ArrayRef<uint32_t> paramTypes) {
  523. const Type *type = Type::getFunction(theContext, returnType, paramTypes);
  524. const uint32_t typeId = theContext.getResultIdForType(type);
  525. theModule.addType(type, typeId);
  526. return typeId;
  527. }
  528. uint32_t ModuleBuilder::getImageType(uint32_t sampledType, spv::Dim dim,
  529. bool isArray) {
  530. const Type *type = Type::getImage(theContext, sampledType, dim,
  531. /*depth*/ 0, isArray, /*ms*/ 0,
  532. /*sampled*/ 1, spv::ImageFormat::Unknown);
  533. const uint32_t typeId = theContext.getResultIdForType(type);
  534. theModule.addType(type, typeId);
  535. const char *dimStr = "";
  536. switch (dim) {
  537. case spv::Dim::Dim1D:
  538. dimStr = "1d.";
  539. break;
  540. case spv::Dim::Dim2D:
  541. dimStr = "2d.";
  542. break;
  543. case spv::Dim::Dim3D:
  544. dimStr = "3d.";
  545. break;
  546. case spv::Dim::Cube:
  547. dimStr = "cube.";
  548. break;
  549. case spv::Dim::Rect:
  550. dimStr = "rect.";
  551. break;
  552. case spv::Dim::Buffer:
  553. dimStr = "buffer.";
  554. break;
  555. case spv::Dim::SubpassData:
  556. dimStr = "subpass.";
  557. break;
  558. default:
  559. break;
  560. }
  561. std::string name =
  562. std::string("type.") + dimStr + "image" + (isArray ? ".array" : "");
  563. theModule.addDebugName(typeId, name);
  564. return typeId;
  565. }
  566. uint32_t ModuleBuilder::getSamplerType() {
  567. const Type *type = Type::getSampler(theContext);
  568. const uint32_t typeId = theContext.getResultIdForType(type);
  569. theModule.addType(type, typeId);
  570. theModule.addDebugName(typeId, "type.sampler");
  571. return typeId;
  572. }
  573. uint32_t ModuleBuilder::getSampledImageType(uint32_t imageType) {
  574. const Type *type = Type::getSampledImage(theContext, imageType);
  575. const uint32_t typeId = theContext.getResultIdForType(type);
  576. theModule.addType(type, typeId);
  577. theModule.addDebugName(typeId, "type.sampled.image");
  578. return typeId;
  579. }
  580. uint32_t ModuleBuilder::getByteAddressBufferType(bool isRW) {
  581. // Create a uint RuntimeArray with Array Stride of 4.
  582. const uint32_t uintType = getUint32Type();
  583. const auto *arrStride4 = Decoration::getArrayStride(theContext, 4u);
  584. const Type *raType =
  585. Type::getRuntimeArray(theContext, uintType, {arrStride4});
  586. const uint32_t raTypeId = theContext.getResultIdForType(raType);
  587. theModule.addType(raType, raTypeId);
  588. // Create a struct containing the runtime array as its only member.
  589. // The struct must also be decorated as BufferBlock. The offset decoration
  590. // should also be applied to the first (only) member. NonWritable decoration
  591. // should also be applied to the first member if isRW is true.
  592. llvm::SmallVector<const Decoration *, 3> typeDecs;
  593. typeDecs.push_back(Decoration::getBufferBlock(theContext));
  594. typeDecs.push_back(Decoration::getOffset(theContext, 0, 0));
  595. if (!isRW)
  596. typeDecs.push_back(Decoration::getNonWritable(theContext, 0));
  597. const Type *type = Type::getStruct(theContext, {raTypeId}, typeDecs);
  598. const uint32_t typeId = theContext.getResultIdForType(type);
  599. theModule.addType(type, typeId);
  600. theModule.addDebugName(
  601. typeId, isRW ? "type.RWByteAddressBuffer" : "type.ByteAddressBuffer");
  602. return typeId;
  603. }
  604. uint32_t ModuleBuilder::getConstantBool(bool value) {
  605. const uint32_t typeId = getBoolType();
  606. const Constant *constant = value ? Constant::getTrue(theContext, typeId)
  607. : Constant::getFalse(theContext, typeId);
  608. const uint32_t constId = theContext.getResultIdForConstant(constant);
  609. theModule.addConstant(constant, constId);
  610. return constId;
  611. }
  612. #define IMPL_GET_PRIMITIVE_CONST(builderTy, cppTy) \
  613. \
  614. uint32_t ModuleBuilder::getConstant##builderTy(cppTy value) { \
  615. const uint32_t typeId = get##builderTy##Type(); \
  616. const Constant *constant = \
  617. Constant::get##builderTy(theContext, typeId, value); \
  618. const uint32_t constId = theContext.getResultIdForConstant(constant); \
  619. theModule.addConstant(constant, constId); \
  620. return constId; \
  621. \
  622. }
  623. IMPL_GET_PRIMITIVE_CONST(Int32, int32_t)
  624. IMPL_GET_PRIMITIVE_CONST(Uint32, uint32_t)
  625. IMPL_GET_PRIMITIVE_CONST(Float32, float)
  626. #undef IMPL_GET_PRIMITIVE_VALUE
  627. uint32_t
  628. ModuleBuilder::getConstantComposite(uint32_t typeId,
  629. llvm::ArrayRef<uint32_t> constituents) {
  630. const Constant *constant =
  631. Constant::getComposite(theContext, typeId, constituents);
  632. const uint32_t constId = theContext.getResultIdForConstant(constant);
  633. theModule.addConstant(constant, constId);
  634. return constId;
  635. }
  636. uint32_t ModuleBuilder::getConstantNull(uint32_t typeId) {
  637. const Constant *constant = Constant::getNull(theContext, typeId);
  638. const uint32_t constId = theContext.getResultIdForConstant(constant);
  639. theModule.addConstant(constant, constId);
  640. return constId;
  641. }
  642. BasicBlock *ModuleBuilder::getBasicBlock(uint32_t labelId) {
  643. auto it = basicBlocks.find(labelId);
  644. if (it == basicBlocks.end()) {
  645. assert(false && "invalid <label-id>");
  646. return nullptr;
  647. }
  648. return it->second.get();
  649. }
  650. } // end namespace spirv
  651. } // end namespace clang