ModuleBuilder.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. uint32_t ModuleBuilder::createSelect(uint32_t resultType, uint32_t condition,
  177. uint32_t trueValue, uint32_t falseValue) {
  178. assert(insertPoint && "null insert point");
  179. const uint32_t id = theContext.takeNextId();
  180. instBuilder.opSelect(resultType, id, condition, trueValue, falseValue).x();
  181. insertPoint->appendInstruction(std::move(constructSite));
  182. return id;
  183. }
  184. void ModuleBuilder::createSwitch(
  185. uint32_t mergeLabel, uint32_t selector, uint32_t defaultLabel,
  186. llvm::ArrayRef<std::pair<uint32_t, uint32_t>> target) {
  187. assert(insertPoint && "null insert point");
  188. // Create the OpSelectioMerege.
  189. instBuilder.opSelectionMerge(mergeLabel, spv::SelectionControlMask::MaskNone)
  190. .x();
  191. insertPoint->appendInstruction(std::move(constructSite));
  192. // Create the OpSwitch.
  193. instBuilder.opSwitch(selector, defaultLabel, target).x();
  194. insertPoint->appendInstruction(std::move(constructSite));
  195. }
  196. void ModuleBuilder::createKill() {
  197. assert(insertPoint && "null insert point");
  198. assert(!isCurrentBasicBlockTerminated());
  199. instBuilder.opKill().x();
  200. insertPoint->appendInstruction(std::move(constructSite));
  201. }
  202. void ModuleBuilder::createBranch(uint32_t targetLabel, uint32_t mergeBB,
  203. uint32_t continueBB,
  204. spv::LoopControlMask loopControl) {
  205. assert(insertPoint && "null insert point");
  206. if (mergeBB && continueBB) {
  207. instBuilder.opLoopMerge(mergeBB, continueBB, loopControl).x();
  208. insertPoint->appendInstruction(std::move(constructSite));
  209. }
  210. instBuilder.opBranch(targetLabel).x();
  211. insertPoint->appendInstruction(std::move(constructSite));
  212. }
  213. void ModuleBuilder::createConditionalBranch(
  214. uint32_t condition, uint32_t trueLabel, uint32_t falseLabel,
  215. uint32_t mergeLabel, uint32_t continueLabel,
  216. spv::SelectionControlMask selectionControl,
  217. spv::LoopControlMask loopControl) {
  218. assert(insertPoint && "null insert point");
  219. if (mergeLabel) {
  220. if (continueLabel) {
  221. instBuilder.opLoopMerge(mergeLabel, continueLabel, loopControl).x();
  222. insertPoint->appendInstruction(std::move(constructSite));
  223. } else {
  224. instBuilder.opSelectionMerge(mergeLabel, selectionControl).x();
  225. insertPoint->appendInstruction(std::move(constructSite));
  226. }
  227. }
  228. instBuilder.opBranchConditional(condition, trueLabel, falseLabel, {}).x();
  229. insertPoint->appendInstruction(std::move(constructSite));
  230. }
  231. void ModuleBuilder::createReturn() {
  232. assert(insertPoint && "null insert point");
  233. instBuilder.opReturn().x();
  234. insertPoint->appendInstruction(std::move(constructSite));
  235. }
  236. void ModuleBuilder::createReturnValue(uint32_t value) {
  237. assert(insertPoint && "null insert point");
  238. instBuilder.opReturnValue(value).x();
  239. insertPoint->appendInstruction(std::move(constructSite));
  240. }
  241. uint32_t ModuleBuilder::createExtInst(uint32_t resultType, uint32_t setId,
  242. uint32_t instId,
  243. llvm::ArrayRef<uint32_t> operands) {
  244. assert(insertPoint && "null insert point");
  245. uint32_t resultId = theContext.takeNextId();
  246. instBuilder.opExtInst(resultType, resultId, setId, instId, operands).x();
  247. insertPoint->appendInstruction(std::move(constructSite));
  248. return resultId;
  249. }
  250. void ModuleBuilder::addExecutionMode(uint32_t entryPointId,
  251. spv::ExecutionMode em,
  252. llvm::ArrayRef<uint32_t> params) {
  253. instBuilder.opExecutionMode(entryPointId, em);
  254. for (const auto &param : params) {
  255. instBuilder.literalInteger(param);
  256. }
  257. instBuilder.x();
  258. theModule.addExecutionMode(std::move(constructSite));
  259. }
  260. uint32_t ModuleBuilder::getGLSLExtInstSet() {
  261. if (glslExtSetId == 0) {
  262. glslExtSetId = theContext.takeNextId();
  263. theModule.addExtInstSet(glslExtSetId, "GLSL.std.450");
  264. }
  265. return glslExtSetId;
  266. }
  267. uint32_t ModuleBuilder::addStageIOVar(uint32_t type,
  268. spv::StorageClass storageClass,
  269. std::string name) {
  270. const uint32_t pointerType = getPointerType(type, storageClass);
  271. const uint32_t varId = theContext.takeNextId();
  272. instBuilder.opVariable(pointerType, varId, storageClass, llvm::None).x();
  273. theModule.addVariable(std::move(constructSite));
  274. theModule.addDebugName(varId, name);
  275. return varId;
  276. }
  277. uint32_t ModuleBuilder::addStageBuiltinVar(uint32_t type, spv::StorageClass sc,
  278. spv::BuiltIn builtin) {
  279. const uint32_t pointerType = getPointerType(type, sc);
  280. const uint32_t varId = theContext.takeNextId();
  281. instBuilder.opVariable(pointerType, varId, sc, llvm::None).x();
  282. theModule.addVariable(std::move(constructSite));
  283. // Decorate with the specified Builtin
  284. const Decoration *d = Decoration::getBuiltIn(theContext, builtin);
  285. theModule.addDecoration(*d, varId);
  286. return varId;
  287. }
  288. uint32_t ModuleBuilder::addFileVar(uint32_t type, llvm::StringRef name,
  289. llvm::Optional<uint32_t> init) {
  290. const uint32_t pointerType = getPointerType(type, spv::StorageClass::Private);
  291. const uint32_t varId = theContext.takeNextId();
  292. instBuilder.opVariable(pointerType, varId, spv::StorageClass::Private, init)
  293. .x();
  294. theModule.addVariable(std::move(constructSite));
  295. theModule.addDebugName(varId, name);
  296. return varId;
  297. }
  298. void ModuleBuilder::decorateLocation(uint32_t targetId, uint32_t location) {
  299. const Decoration *d =
  300. Decoration::getLocation(theContext, location, llvm::None);
  301. theModule.addDecoration(*d, targetId);
  302. }
  303. #define IMPL_GET_PRIMITIVE_TYPE(ty) \
  304. \
  305. uint32_t ModuleBuilder::get##ty##Type() { \
  306. const Type *type = Type::get##ty(theContext); \
  307. const uint32_t typeId = theContext.getResultIdForType(type); \
  308. theModule.addType(type, typeId); \
  309. return typeId; \
  310. \
  311. }
  312. IMPL_GET_PRIMITIVE_TYPE(Void)
  313. IMPL_GET_PRIMITIVE_TYPE(Bool)
  314. IMPL_GET_PRIMITIVE_TYPE(Int32)
  315. IMPL_GET_PRIMITIVE_TYPE(Uint32)
  316. IMPL_GET_PRIMITIVE_TYPE(Float32)
  317. #undef IMPL_GET_PRIMITIVE_TYPE
  318. uint32_t ModuleBuilder::getVecType(uint32_t elemType, uint32_t elemCount) {
  319. const Type *type = nullptr;
  320. switch (elemCount) {
  321. case 2:
  322. type = Type::getVec2(theContext, elemType);
  323. break;
  324. case 3:
  325. type = Type::getVec3(theContext, elemType);
  326. break;
  327. case 4:
  328. type = Type::getVec4(theContext, elemType);
  329. break;
  330. default:
  331. assert(false && "unhandled vector size");
  332. // Error found. Return 0 as the <result-id> directly.
  333. return 0;
  334. }
  335. const uint32_t typeId = theContext.getResultIdForType(type);
  336. theModule.addType(type, typeId);
  337. return typeId;
  338. }
  339. uint32_t ModuleBuilder::getMatType(uint32_t colType, uint32_t colCount) {
  340. const Type *type = Type::getMatrix(theContext, colType, colCount);
  341. const uint32_t typeId = theContext.getResultIdForType(type);
  342. theModule.addType(type, typeId);
  343. return typeId;
  344. }
  345. uint32_t ModuleBuilder::getPointerType(uint32_t pointeeType,
  346. spv::StorageClass storageClass) {
  347. const Type *type = Type::getPointer(theContext, storageClass, pointeeType);
  348. const uint32_t typeId = theContext.getResultIdForType(type);
  349. theModule.addType(type, typeId);
  350. return typeId;
  351. }
  352. uint32_t
  353. ModuleBuilder::getStructType(llvm::ArrayRef<uint32_t> fieldTypes,
  354. llvm::StringRef structName,
  355. llvm::ArrayRef<llvm::StringRef> fieldNames) {
  356. const Type *type = Type::getStruct(theContext, fieldTypes);
  357. bool isRegistered = false;
  358. const uint32_t typeId = theContext.getResultIdForType(type, &isRegistered);
  359. theModule.addType(type, typeId);
  360. // TODO: Probably we should check duplication and do nothing if trying to add
  361. // the same debug name for the same entity in addDebugName().
  362. if (!isRegistered) {
  363. theModule.addDebugName(typeId, structName);
  364. if (!fieldNames.empty()) {
  365. assert(fieldNames.size() == fieldTypes.size());
  366. for (uint32_t i = 0; i < fieldNames.size(); ++i)
  367. theModule.addDebugName(typeId, fieldNames[i],
  368. llvm::Optional<uint32_t>(i));
  369. }
  370. }
  371. return typeId;
  372. }
  373. uint32_t ModuleBuilder::getFunctionType(uint32_t returnType,
  374. llvm::ArrayRef<uint32_t> paramTypes) {
  375. const Type *type = Type::getFunction(theContext, returnType, paramTypes);
  376. const uint32_t typeId = theContext.getResultIdForType(type);
  377. theModule.addType(type, typeId);
  378. return typeId;
  379. }
  380. uint32_t ModuleBuilder::getConstantBool(bool value) {
  381. const uint32_t typeId = getBoolType();
  382. const Constant *constant = value ? Constant::getTrue(theContext, typeId)
  383. : Constant::getFalse(theContext, typeId);
  384. const uint32_t constId = theContext.getResultIdForConstant(constant);
  385. theModule.addConstant(constant, constId);
  386. return constId;
  387. }
  388. #define IMPL_GET_PRIMITIVE_CONST(builderTy, cppTy) \
  389. \
  390. uint32_t ModuleBuilder::getConstant##builderTy(cppTy value) { \
  391. const uint32_t typeId = get##builderTy##Type(); \
  392. const Constant *constant = \
  393. Constant::get##builderTy(theContext, typeId, value); \
  394. const uint32_t constId = theContext.getResultIdForConstant(constant); \
  395. theModule.addConstant(constant, constId); \
  396. return constId; \
  397. \
  398. }
  399. IMPL_GET_PRIMITIVE_CONST(Int32, int32_t)
  400. IMPL_GET_PRIMITIVE_CONST(Uint32, uint32_t)
  401. IMPL_GET_PRIMITIVE_CONST(Float32, float)
  402. #undef IMPL_GET_PRIMITIVE_VALUE
  403. uint32_t
  404. ModuleBuilder::getConstantComposite(uint32_t typeId,
  405. llvm::ArrayRef<uint32_t> constituents) {
  406. const Constant *constant =
  407. Constant::getComposite(theContext, typeId, constituents);
  408. const uint32_t constId = theContext.getResultIdForConstant(constant);
  409. theModule.addConstant(constant, constId);
  410. return constId;
  411. }
  412. uint32_t ModuleBuilder::getConstantNull(uint32_t typeId) {
  413. const Constant *constant = Constant::getNull(theContext, typeId);
  414. const uint32_t constId = theContext.getResultIdForConstant(constant);
  415. theModule.addConstant(constant, constId);
  416. return constId;
  417. }
  418. BasicBlock *ModuleBuilder::getBasicBlock(uint32_t labelId) {
  419. auto it = basicBlocks.find(labelId);
  420. if (it == basicBlocks.end()) {
  421. assert(false && "invalid <label-id>");
  422. return nullptr;
  423. }
  424. return it->second.get();
  425. }
  426. } // end namespace spirv
  427. } // end namespace clang