ModuleBuilder.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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);
  85. theModule.addDebugName(labelId, name);
  86. return labelId;
  87. }
  88. void ModuleBuilder::addSuccessor(uint32_t successorLabel) {
  89. assert(insertPoint && "null insert point");
  90. insertPoint->addSuccessor(getBasicBlock(successorLabel));
  91. }
  92. void ModuleBuilder::setMergeTarget(uint32_t mergeLabel) {
  93. assert(insertPoint && "null insert point");
  94. insertPoint->setMergeTarget(getBasicBlock(mergeLabel));
  95. }
  96. void ModuleBuilder::setContinueTarget(uint32_t continueLabel) {
  97. assert(insertPoint && "null insert point");
  98. insertPoint->setContinueTarget(getBasicBlock(continueLabel));
  99. }
  100. void ModuleBuilder::setInsertPoint(uint32_t labelId) {
  101. insertPoint = getBasicBlock(labelId);
  102. }
  103. uint32_t
  104. ModuleBuilder::createCompositeConstruct(uint32_t resultType,
  105. llvm::ArrayRef<uint32_t> constituents) {
  106. assert(insertPoint && "null insert point");
  107. const uint32_t resultId = theContext.takeNextId();
  108. instBuilder.opCompositeConstruct(resultType, resultId, constituents).x();
  109. insertPoint->appendInstruction(std::move(constructSite));
  110. return resultId;
  111. }
  112. uint32_t
  113. ModuleBuilder::createCompositeExtract(uint32_t resultType, uint32_t composite,
  114. llvm::ArrayRef<uint32_t> indexes) {
  115. assert(insertPoint && "null insert point");
  116. const uint32_t resultId = theContext.takeNextId();
  117. instBuilder.opCompositeExtract(resultType, resultId, composite, indexes).x();
  118. insertPoint->appendInstruction(std::move(constructSite));
  119. return resultId;
  120. }
  121. uint32_t
  122. ModuleBuilder::createVectorShuffle(uint32_t resultType, uint32_t vector1,
  123. uint32_t vector2,
  124. llvm::ArrayRef<uint32_t> selectors) {
  125. assert(insertPoint && "null insert point");
  126. const uint32_t resultId = theContext.takeNextId();
  127. instBuilder.opVectorShuffle(resultType, resultId, vector1, vector2, selectors)
  128. .x();
  129. insertPoint->appendInstruction(std::move(constructSite));
  130. return resultId;
  131. }
  132. uint32_t ModuleBuilder::createLoad(uint32_t resultType, uint32_t pointer) {
  133. assert(insertPoint && "null insert point");
  134. const uint32_t resultId = theContext.takeNextId();
  135. instBuilder.opLoad(resultType, resultId, pointer, llvm::None).x();
  136. insertPoint->appendInstruction(std::move(constructSite));
  137. return resultId;
  138. }
  139. void ModuleBuilder::createStore(uint32_t address, uint32_t value) {
  140. assert(insertPoint && "null insert point");
  141. instBuilder.opStore(address, value, llvm::None).x();
  142. insertPoint->appendInstruction(std::move(constructSite));
  143. }
  144. uint32_t ModuleBuilder::createFunctionCall(uint32_t returnType,
  145. uint32_t functionId,
  146. llvm::ArrayRef<uint32_t> params) {
  147. assert(insertPoint && "null insert point");
  148. const uint32_t id = theContext.takeNextId();
  149. instBuilder.opFunctionCall(returnType, id, functionId, params).x();
  150. insertPoint->appendInstruction(std::move(constructSite));
  151. return id;
  152. }
  153. uint32_t ModuleBuilder::createAccessChain(uint32_t resultType, uint32_t base,
  154. llvm::ArrayRef<uint32_t> indexes) {
  155. assert(insertPoint && "null insert point");
  156. const uint32_t id = theContext.takeNextId();
  157. instBuilder.opAccessChain(resultType, id, base, indexes).x();
  158. insertPoint->appendInstruction(std::move(constructSite));
  159. return id;
  160. }
  161. uint32_t ModuleBuilder::createUnaryOp(spv::Op op, uint32_t resultType,
  162. uint32_t operand) {
  163. assert(insertPoint && "null insert point");
  164. const uint32_t id = theContext.takeNextId();
  165. instBuilder.unaryOp(op, resultType, id, operand).x();
  166. insertPoint->appendInstruction(std::move(constructSite));
  167. return id;
  168. }
  169. uint32_t ModuleBuilder::createBinaryOp(spv::Op op, uint32_t resultType,
  170. uint32_t lhs, uint32_t rhs) {
  171. assert(insertPoint && "null insert point");
  172. const uint32_t id = theContext.takeNextId();
  173. instBuilder.binaryOp(op, resultType, id, lhs, rhs).x();
  174. insertPoint->appendInstruction(std::move(constructSite));
  175. return id;
  176. }
  177. uint32_t ModuleBuilder::createSelect(uint32_t resultType, uint32_t condition,
  178. uint32_t trueValue, uint32_t falseValue) {
  179. assert(insertPoint && "null insert point");
  180. const uint32_t id = theContext.takeNextId();
  181. instBuilder.opSelect(resultType, id, condition, trueValue, falseValue).x();
  182. insertPoint->appendInstruction(std::move(constructSite));
  183. return id;
  184. }
  185. void ModuleBuilder::createSwitch(
  186. uint32_t mergeLabel, uint32_t selector, uint32_t defaultLabel,
  187. llvm::ArrayRef<std::pair<uint32_t, uint32_t>> target) {
  188. assert(insertPoint && "null insert point");
  189. // Create the OpSelectioMerege.
  190. instBuilder.opSelectionMerge(mergeLabel, spv::SelectionControlMask::MaskNone)
  191. .x();
  192. insertPoint->appendInstruction(std::move(constructSite));
  193. // Create the OpSwitch.
  194. instBuilder.opSwitch(selector, defaultLabel, target).x();
  195. insertPoint->appendInstruction(std::move(constructSite));
  196. }
  197. void ModuleBuilder::createBranch(uint32_t targetLabel, uint32_t mergeBB,
  198. uint32_t continueBB,
  199. spv::LoopControlMask loopControl) {
  200. assert(insertPoint && "null insert point");
  201. if (mergeBB && continueBB) {
  202. instBuilder.opLoopMerge(mergeBB, continueBB, loopControl).x();
  203. insertPoint->appendInstruction(std::move(constructSite));
  204. }
  205. instBuilder.opBranch(targetLabel).x();
  206. insertPoint->appendInstruction(std::move(constructSite));
  207. }
  208. void ModuleBuilder::createConditionalBranch(
  209. uint32_t condition, uint32_t trueLabel, uint32_t falseLabel,
  210. uint32_t mergeLabel, uint32_t continueLabel,
  211. spv::SelectionControlMask selectionControl,
  212. spv::LoopControlMask loopControl) {
  213. assert(insertPoint && "null insert point");
  214. if (mergeLabel) {
  215. if (continueLabel) {
  216. instBuilder.opLoopMerge(mergeLabel, continueLabel, loopControl).x();
  217. insertPoint->appendInstruction(std::move(constructSite));
  218. } else {
  219. instBuilder.opSelectionMerge(mergeLabel, selectionControl).x();
  220. insertPoint->appendInstruction(std::move(constructSite));
  221. }
  222. }
  223. instBuilder.opBranchConditional(condition, trueLabel, falseLabel, {}).x();
  224. insertPoint->appendInstruction(std::move(constructSite));
  225. }
  226. void ModuleBuilder::createReturn() {
  227. assert(insertPoint && "null insert point");
  228. instBuilder.opReturn().x();
  229. insertPoint->appendInstruction(std::move(constructSite));
  230. }
  231. void ModuleBuilder::createReturnValue(uint32_t value) {
  232. assert(insertPoint && "null insert point");
  233. instBuilder.opReturnValue(value).x();
  234. insertPoint->appendInstruction(std::move(constructSite));
  235. }
  236. uint32_t ModuleBuilder::createExtInst(uint32_t resultType, uint32_t setId,
  237. uint32_t instId,
  238. llvm::ArrayRef<uint32_t> operands) {
  239. assert(insertPoint && "null insert point");
  240. uint32_t resultId = theContext.takeNextId();
  241. instBuilder.opExtInst(resultType, resultId, setId, instId, operands).x();
  242. insertPoint->appendInstruction(std::move(constructSite));
  243. return resultId;
  244. }
  245. void ModuleBuilder::addExecutionMode(uint32_t entryPointId,
  246. spv::ExecutionMode em,
  247. const std::vector<uint32_t> &params) {
  248. instBuilder.opExecutionMode(entryPointId, em);
  249. for (const auto &param : params) {
  250. instBuilder.literalInteger(param);
  251. }
  252. instBuilder.x();
  253. theModule.addExecutionMode(std::move(constructSite));
  254. }
  255. uint32_t ModuleBuilder::getGLSLExtInstSet() {
  256. if (glslExtSetId == 0) {
  257. glslExtSetId = theContext.takeNextId();
  258. theModule.addExtInstSet(glslExtSetId, "GLSL.std.450");
  259. }
  260. return glslExtSetId;
  261. }
  262. uint32_t ModuleBuilder::addStageIOVar(uint32_t type,
  263. spv::StorageClass storageClass) {
  264. const uint32_t pointerType = getPointerType(type, storageClass);
  265. const uint32_t varId = theContext.takeNextId();
  266. instBuilder.opVariable(pointerType, varId, storageClass, llvm::None).x();
  267. theModule.addVariable(std::move(constructSite));
  268. return varId;
  269. }
  270. uint32_t ModuleBuilder::addStageBuiltinVar(uint32_t type, spv::StorageClass sc,
  271. spv::BuiltIn builtin) {
  272. const uint32_t pointerType = getPointerType(type, sc);
  273. const uint32_t varId = theContext.takeNextId();
  274. instBuilder.opVariable(pointerType, varId, sc, llvm::None).x();
  275. theModule.addVariable(std::move(constructSite));
  276. // Decorate with the specified Builtin
  277. const Decoration *d = Decoration::getBuiltIn(theContext, builtin);
  278. theModule.addDecoration(*d, varId);
  279. return varId;
  280. }
  281. uint32_t ModuleBuilder::addFileVar(uint32_t type, llvm::StringRef name,
  282. llvm::Optional<uint32_t> init) {
  283. const uint32_t pointerType = getPointerType(type, spv::StorageClass::Private);
  284. const uint32_t varId = theContext.takeNextId();
  285. instBuilder.opVariable(pointerType, varId, spv::StorageClass::Private, init)
  286. .x();
  287. theModule.addVariable(std::move(constructSite));
  288. theModule.addDebugName(varId, name);
  289. return varId;
  290. }
  291. void ModuleBuilder::decorateLocation(uint32_t targetId, uint32_t location) {
  292. const Decoration *d =
  293. Decoration::getLocation(theContext, location, llvm::None);
  294. theModule.addDecoration(*d, targetId);
  295. }
  296. #define IMPL_GET_PRIMITIVE_TYPE(ty) \
  297. \
  298. uint32_t ModuleBuilder::get##ty##Type() { \
  299. const Type *type = Type::get##ty(theContext); \
  300. const uint32_t typeId = theContext.getResultIdForType(type); \
  301. theModule.addType(type, typeId); \
  302. return typeId; \
  303. \
  304. }
  305. IMPL_GET_PRIMITIVE_TYPE(Void)
  306. IMPL_GET_PRIMITIVE_TYPE(Bool)
  307. IMPL_GET_PRIMITIVE_TYPE(Int32)
  308. IMPL_GET_PRIMITIVE_TYPE(Uint32)
  309. IMPL_GET_PRIMITIVE_TYPE(Float32)
  310. #undef IMPL_GET_PRIMITIVE_TYPE
  311. uint32_t ModuleBuilder::getVecType(uint32_t elemType, uint32_t elemCount) {
  312. const Type *type = nullptr;
  313. switch (elemCount) {
  314. case 2:
  315. type = Type::getVec2(theContext, elemType);
  316. break;
  317. case 3:
  318. type = Type::getVec3(theContext, elemType);
  319. break;
  320. case 4:
  321. type = Type::getVec4(theContext, elemType);
  322. break;
  323. default:
  324. assert(false && "unhandled vector size");
  325. // Error found. Return 0 as the <result-id> directly.
  326. return 0;
  327. }
  328. const uint32_t typeId = theContext.getResultIdForType(type);
  329. theModule.addType(type, typeId);
  330. return typeId;
  331. }
  332. uint32_t ModuleBuilder::getMatType(uint32_t colType, uint32_t colCount) {
  333. const Type *type = Type::getMatrix(theContext, colType, colCount);
  334. const uint32_t typeId = theContext.getResultIdForType(type);
  335. theModule.addType(type, typeId);
  336. return typeId;
  337. }
  338. uint32_t ModuleBuilder::getPointerType(uint32_t pointeeType,
  339. spv::StorageClass storageClass) {
  340. const Type *type = Type::getPointer(theContext, storageClass, pointeeType);
  341. const uint32_t typeId = theContext.getResultIdForType(type);
  342. theModule.addType(type, typeId);
  343. return typeId;
  344. }
  345. uint32_t ModuleBuilder::getStructType(llvm::ArrayRef<uint32_t> fieldTypes) {
  346. const Type *type = Type::getStruct(theContext, fieldTypes);
  347. const uint32_t typeId = theContext.getResultIdForType(type);
  348. theModule.addType(type, typeId);
  349. return typeId;
  350. }
  351. uint32_t ModuleBuilder::getFunctionType(uint32_t returnType,
  352. llvm::ArrayRef<uint32_t> paramTypes) {
  353. const Type *type = Type::getFunction(theContext, returnType, paramTypes);
  354. const uint32_t typeId = theContext.getResultIdForType(type);
  355. theModule.addType(type, typeId);
  356. return typeId;
  357. }
  358. uint32_t ModuleBuilder::getConstantBool(bool value) {
  359. const uint32_t typeId = getBoolType();
  360. const Constant *constant = value ? Constant::getTrue(theContext, typeId)
  361. : Constant::getFalse(theContext, typeId);
  362. const uint32_t constId = theContext.getResultIdForConstant(constant);
  363. theModule.addConstant(constant, constId);
  364. return constId;
  365. }
  366. #define IMPL_GET_PRIMITIVE_CONST(builderTy, cppTy) \
  367. \
  368. uint32_t ModuleBuilder::getConstant##builderTy(cppTy value) { \
  369. const uint32_t typeId = get##builderTy##Type(); \
  370. const Constant *constant = \
  371. Constant::get##builderTy(theContext, typeId, value); \
  372. const uint32_t constId = theContext.getResultIdForConstant(constant); \
  373. theModule.addConstant(constant, constId); \
  374. return constId; \
  375. \
  376. }
  377. IMPL_GET_PRIMITIVE_CONST(Int32, int32_t)
  378. IMPL_GET_PRIMITIVE_CONST(Uint32, uint32_t)
  379. IMPL_GET_PRIMITIVE_CONST(Float32, float)
  380. #undef IMPL_GET_PRIMITIVE_VALUE
  381. uint32_t
  382. ModuleBuilder::getConstantComposite(uint32_t typeId,
  383. llvm::ArrayRef<uint32_t> constituents) {
  384. const Constant *constant =
  385. Constant::getComposite(theContext, typeId, constituents);
  386. const uint32_t constId = theContext.getResultIdForConstant(constant);
  387. theModule.addConstant(constant, constId);
  388. return constId;
  389. }
  390. uint32_t ModuleBuilder::getConstantNull(uint32_t typeId) {
  391. const Constant *constant = Constant::getNull(theContext, typeId);
  392. const uint32_t constId = theContext.getResultIdForConstant(constant);
  393. theModule.addConstant(constant, constId);
  394. return constId;
  395. }
  396. BasicBlock *ModuleBuilder::getBasicBlock(uint32_t labelId) {
  397. auto it = basicBlocks.find(labelId);
  398. if (it == basicBlocks.end()) {
  399. assert(false && "invalid <label-id>");
  400. return nullptr;
  401. }
  402. return it->second.get();
  403. }
  404. } // end namespace spirv
  405. } // end namespace clang