ModuleBuilder.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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) {
  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::addFnParameter(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::addFnVariable(uint32_t ptrType, llvm::StringRef name,
  53. llvm::Optional<uint32_t> init) {
  54. assert(theFunction && "found detached local variable");
  55. const uint32_t varId = theContext.takeNextId();
  56. theFunction->addVariable(ptrType, varId, init);
  57. theModule.addDebugName(varId, name);
  58. return varId;
  59. }
  60. bool ModuleBuilder::endFunction() {
  61. if (theFunction == nullptr) {
  62. assert(false && "no active function");
  63. return false;
  64. }
  65. // Move all basic blocks into the current function.
  66. // TODO: we should adjust the order the basic blocks according to
  67. // SPIR-V validation rules.
  68. for (auto &bb : basicBlocks) {
  69. theFunction->addBasicBlock(std::move(bb.second));
  70. }
  71. basicBlocks.clear();
  72. theModule.addFunction(std::move(theFunction));
  73. theFunction.reset(nullptr);
  74. insertPoint = nullptr;
  75. return true;
  76. }
  77. uint32_t ModuleBuilder::createBasicBlock(llvm::StringRef name) {
  78. if (theFunction == nullptr) {
  79. assert(false && "found detached basic block");
  80. return 0;
  81. }
  82. const uint32_t labelId = theContext.takeNextId();
  83. basicBlocks[labelId] = llvm::make_unique<BasicBlock>(labelId);
  84. theModule.addDebugName(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::createBranch(uint32_t targetLabel) {
  197. assert(insertPoint && "null insert point");
  198. instBuilder.opBranch(targetLabel).x();
  199. insertPoint->appendInstruction(std::move(constructSite));
  200. }
  201. void ModuleBuilder::createConditionalBranch(
  202. uint32_t condition, uint32_t trueLabel, uint32_t falseLabel,
  203. uint32_t mergeLabel, uint32_t continueLabel,
  204. spv::SelectionControlMask selectionControl,
  205. spv::LoopControlMask loopControl) {
  206. assert(insertPoint && "null insert point");
  207. if (mergeLabel) {
  208. if (continueLabel) {
  209. instBuilder.opLoopMerge(mergeLabel, continueLabel, loopControl).x();
  210. insertPoint->appendInstruction(std::move(constructSite));
  211. } else {
  212. instBuilder.opSelectionMerge(mergeLabel, selectionControl).x();
  213. insertPoint->appendInstruction(std::move(constructSite));
  214. }
  215. }
  216. instBuilder.opBranchConditional(condition, trueLabel, falseLabel, {}).x();
  217. insertPoint->appendInstruction(std::move(constructSite));
  218. }
  219. void ModuleBuilder::createReturn() {
  220. assert(insertPoint && "null insert point");
  221. instBuilder.opReturn().x();
  222. insertPoint->appendInstruction(std::move(constructSite));
  223. }
  224. void ModuleBuilder::createReturnValue(uint32_t value) {
  225. assert(insertPoint && "null insert point");
  226. instBuilder.opReturnValue(value).x();
  227. insertPoint->appendInstruction(std::move(constructSite));
  228. }
  229. void ModuleBuilder::addExecutionMode(uint32_t entryPointId,
  230. spv::ExecutionMode em,
  231. const std::vector<uint32_t> &params) {
  232. instBuilder.opExecutionMode(entryPointId, em);
  233. for (const auto &param : params) {
  234. instBuilder.literalInteger(param);
  235. }
  236. instBuilder.x();
  237. theModule.addExecutionMode(std::move(constructSite));
  238. }
  239. uint32_t ModuleBuilder::addStageIOVariable(uint32_t type,
  240. spv::StorageClass storageClass) {
  241. const uint32_t pointerType = getPointerType(type, storageClass);
  242. const uint32_t varId = theContext.takeNextId();
  243. instBuilder.opVariable(pointerType, varId, storageClass, llvm::None).x();
  244. theModule.addVariable(std::move(constructSite));
  245. return varId;
  246. }
  247. uint32_t ModuleBuilder::addStageBuiltinVariable(uint32_t type,
  248. spv::BuiltIn builtin) {
  249. spv::StorageClass sc = spv::StorageClass::Input;
  250. switch (builtin) {
  251. case spv::BuiltIn::Position:
  252. case spv::BuiltIn::PointSize:
  253. // TODO: add the rest output builtins
  254. sc = spv::StorageClass::Output;
  255. break;
  256. default:
  257. break;
  258. }
  259. const uint32_t pointerType = getPointerType(type, sc);
  260. const uint32_t varId = theContext.takeNextId();
  261. instBuilder.opVariable(pointerType, varId, sc, llvm::None).x();
  262. theModule.addVariable(std::move(constructSite));
  263. // Decorate with the specified Builtin
  264. const Decoration *d = Decoration::getBuiltIn(theContext, builtin);
  265. theModule.addDecoration(*d, varId);
  266. return varId;
  267. }
  268. void ModuleBuilder::decorateLocation(uint32_t targetId, uint32_t location) {
  269. const Decoration *d =
  270. Decoration::getLocation(theContext, location, llvm::None);
  271. theModule.addDecoration(*d, targetId);
  272. }
  273. #define IMPL_GET_PRIMITIVE_TYPE(ty) \
  274. \
  275. uint32_t ModuleBuilder::get##ty##Type() { \
  276. const Type *type = Type::get##ty(theContext); \
  277. const uint32_t typeId = theContext.getResultIdForType(type); \
  278. theModule.addType(type, typeId); \
  279. return typeId; \
  280. \
  281. }
  282. IMPL_GET_PRIMITIVE_TYPE(Void)
  283. IMPL_GET_PRIMITIVE_TYPE(Bool)
  284. IMPL_GET_PRIMITIVE_TYPE(Int32)
  285. IMPL_GET_PRIMITIVE_TYPE(Uint32)
  286. IMPL_GET_PRIMITIVE_TYPE(Float32)
  287. #undef IMPL_GET_PRIMITIVE_TYPE
  288. uint32_t ModuleBuilder::getVecType(uint32_t elemType, uint32_t elemCount) {
  289. const Type *type = nullptr;
  290. switch (elemCount) {
  291. case 2:
  292. type = Type::getVec2(theContext, elemType);
  293. break;
  294. case 3:
  295. type = Type::getVec3(theContext, elemType);
  296. break;
  297. case 4:
  298. type = Type::getVec4(theContext, elemType);
  299. break;
  300. default:
  301. assert(false && "unhandled vector size");
  302. // Error found. Return 0 as the <result-id> directly.
  303. return 0;
  304. }
  305. const uint32_t typeId = theContext.getResultIdForType(type);
  306. theModule.addType(type, typeId);
  307. return typeId;
  308. }
  309. uint32_t ModuleBuilder::getMatType(uint32_t colType, uint32_t colCount) {
  310. const Type *type = Type::getMatrix(theContext, colType, colCount);
  311. const uint32_t typeId = theContext.getResultIdForType(type);
  312. theModule.addType(type, typeId);
  313. return typeId;
  314. }
  315. uint32_t ModuleBuilder::getPointerType(uint32_t pointeeType,
  316. spv::StorageClass storageClass) {
  317. const Type *type = Type::getPointer(theContext, storageClass, pointeeType);
  318. const uint32_t typeId = theContext.getResultIdForType(type);
  319. theModule.addType(type, typeId);
  320. return typeId;
  321. }
  322. uint32_t ModuleBuilder::getStructType(llvm::ArrayRef<uint32_t> fieldTypes) {
  323. const Type *type = Type::getStruct(theContext, fieldTypes);
  324. const uint32_t typeId = theContext.getResultIdForType(type);
  325. theModule.addType(type, typeId);
  326. return typeId;
  327. }
  328. uint32_t ModuleBuilder::getFunctionType(uint32_t returnType,
  329. llvm::ArrayRef<uint32_t> paramTypes) {
  330. const Type *type = Type::getFunction(theContext, returnType, paramTypes);
  331. const uint32_t typeId = theContext.getResultIdForType(type);
  332. theModule.addType(type, typeId);
  333. return typeId;
  334. }
  335. uint32_t ModuleBuilder::getConstantBool(bool value) {
  336. const uint32_t typeId = getBoolType();
  337. const Constant *constant = value ? Constant::getTrue(theContext, typeId)
  338. : Constant::getFalse(theContext, typeId);
  339. const uint32_t constId = theContext.getResultIdForConstant(constant);
  340. theModule.addConstant(constant, constId);
  341. return constId;
  342. }
  343. #define IMPL_GET_PRIMITIVE_CONST(builderTy, cppTy) \
  344. \
  345. uint32_t ModuleBuilder::getConstant##builderTy(cppTy value) { \
  346. const uint32_t typeId = get##builderTy##Type(); \
  347. const Constant *constant = \
  348. Constant::get##builderTy(theContext, typeId, value); \
  349. const uint32_t constId = theContext.getResultIdForConstant(constant); \
  350. theModule.addConstant(constant, constId); \
  351. return constId; \
  352. \
  353. }
  354. IMPL_GET_PRIMITIVE_CONST(Int32, int32_t)
  355. IMPL_GET_PRIMITIVE_CONST(Uint32, uint32_t)
  356. IMPL_GET_PRIMITIVE_CONST(Float32, float)
  357. #undef IMPL_GET_PRIMITIVE_VALUE
  358. uint32_t
  359. ModuleBuilder::getConstantComposite(uint32_t typeId,
  360. llvm::ArrayRef<uint32_t> constituents) {
  361. const Constant *constant =
  362. Constant::getComposite(theContext, typeId, constituents);
  363. const uint32_t constId = theContext.getResultIdForConstant(constant);
  364. theModule.addConstant(constant, constId);
  365. return constId;
  366. }
  367. BasicBlock *ModuleBuilder::getBasicBlock(uint32_t labelId) {
  368. auto it = basicBlocks.find(labelId);
  369. if (it == basicBlocks.end()) {
  370. assert(false && "invalid <label-id>");
  371. return nullptr;
  372. }
  373. return it->second.get();
  374. }
  375. } // end namespace spirv
  376. } // end namespace clang