ModuleBuilder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 std::move(binary);
  30. }
  31. uint32_t ModuleBuilder::beginFunction(uint32_t funcType, uint32_t returnType,
  32. llvm::StringRef funcName) {
  33. if (theFunction) {
  34. assert(false && "found nested function");
  35. return 0;
  36. }
  37. const uint32_t fId = theContext.takeNextId();
  38. theFunction = llvm::make_unique<Function>(
  39. returnType, fId, spv::FunctionControlMask::MaskNone, funcType);
  40. theModule.addDebugName(fId, funcName);
  41. return fId;
  42. }
  43. uint32_t ModuleBuilder::addFnParameter(uint32_t type, llvm::StringRef name) {
  44. assert(theFunction && "found detached parameter");
  45. const uint32_t pointerType =
  46. getPointerType(type, spv::StorageClass::Function);
  47. const uint32_t paramId = theContext.takeNextId();
  48. theFunction->addParameter(pointerType, paramId);
  49. theModule.addDebugName(paramId, name);
  50. return paramId;
  51. }
  52. uint32_t ModuleBuilder::addFnVariable(uint32_t type, 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(type, 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. bool ModuleBuilder::setInsertPoint(uint32_t labelId) {
  88. auto it = basicBlocks.find(labelId);
  89. if (it == basicBlocks.end()) {
  90. assert(false && "invalid <label-id>");
  91. return false;
  92. }
  93. insertPoint = it->second.get();
  94. return true;
  95. }
  96. uint32_t ModuleBuilder::createLoad(uint32_t resultType, uint32_t pointer) {
  97. assert(insertPoint && "null insert point");
  98. const uint32_t resultId = theContext.takeNextId();
  99. instBuilder.opLoad(resultType, resultId, pointer, llvm::None).x();
  100. insertPoint->appendInstruction(std::move(constructSite));
  101. return resultId;
  102. }
  103. void ModuleBuilder::createStore(uint32_t address, uint32_t value) {
  104. assert(insertPoint && "null insert point");
  105. instBuilder.opStore(address, value, llvm::None).x();
  106. insertPoint->appendInstruction(std::move(constructSite));
  107. }
  108. uint32_t ModuleBuilder::createAccessChain(uint32_t resultType, uint32_t base,
  109. llvm::ArrayRef<uint32_t> indexes) {
  110. assert(insertPoint && "null insert point");
  111. const uint32_t id = theContext.takeNextId();
  112. instBuilder.opAccessChain(resultType, id, base, indexes).x();
  113. insertPoint->appendInstruction(std::move(constructSite));
  114. return id;
  115. }
  116. uint32_t ModuleBuilder::createBinaryOp(spv::Op op, uint32_t resultType,
  117. uint32_t lhs, uint32_t rhs) {
  118. assert(insertPoint && "null insert point");
  119. const uint32_t id = theContext.takeNextId();
  120. instBuilder.binaryOp(op, resultType, id, lhs, rhs).x();
  121. insertPoint->appendInstruction(std::move(constructSite));
  122. return id;
  123. }
  124. void ModuleBuilder::createBranch(uint32_t targetLabel) {
  125. assert(insertPoint && "null insert point");
  126. instBuilder.opBranch(targetLabel).x();
  127. insertPoint->appendInstruction(std::move(constructSite));
  128. }
  129. void ModuleBuilder::createConditionalBranch(uint32_t condition,
  130. uint32_t trueLabel,
  131. uint32_t falseLabel,
  132. uint32_t mergeLabel) {
  133. assert(insertPoint && "null insert point");
  134. instBuilder.opSelectionMerge(mergeLabel, spv::SelectionControlMask::MaskNone)
  135. .x();
  136. insertPoint->appendInstruction(std::move(constructSite));
  137. instBuilder.opBranchConditional(condition, trueLabel, falseLabel, {}).x();
  138. insertPoint->appendInstruction(std::move(constructSite));
  139. }
  140. void ModuleBuilder::createReturn() {
  141. assert(insertPoint && "null insert point");
  142. instBuilder.opReturn().x();
  143. insertPoint->appendInstruction(std::move(constructSite));
  144. }
  145. void ModuleBuilder::createReturnValue(uint32_t value) {
  146. assert(insertPoint && "null insert point");
  147. instBuilder.opReturnValue(value).x();
  148. insertPoint->appendInstruction(std::move(constructSite));
  149. }
  150. void ModuleBuilder::addExecutionMode(uint32_t entryPointId,
  151. spv::ExecutionMode em,
  152. const std::vector<uint32_t> &params) {
  153. instBuilder.opExecutionMode(entryPointId, em);
  154. for (const auto &param : params) {
  155. instBuilder.literalInteger(param);
  156. }
  157. instBuilder.x();
  158. theModule.addExecutionMode(std::move(constructSite));
  159. }
  160. uint32_t ModuleBuilder::addStageIOVariable(uint32_t type,
  161. spv::StorageClass storageClass) {
  162. const uint32_t pointerType = getPointerType(type, storageClass);
  163. const uint32_t varId = theContext.takeNextId();
  164. instBuilder.opVariable(pointerType, varId, storageClass, llvm::None).x();
  165. theModule.addVariable(std::move(constructSite));
  166. return varId;
  167. }
  168. uint32_t ModuleBuilder::addStageBuiltinVariable(uint32_t type,
  169. spv::BuiltIn builtin) {
  170. spv::StorageClass sc = spv::StorageClass::Input;
  171. switch (builtin) {
  172. case spv::BuiltIn::Position:
  173. case spv::BuiltIn::PointSize:
  174. // TODO: add the rest output builtins
  175. sc = spv::StorageClass::Output;
  176. break;
  177. default:
  178. break;
  179. }
  180. const uint32_t pointerType = getPointerType(type, sc);
  181. const uint32_t varId = theContext.takeNextId();
  182. instBuilder.opVariable(pointerType, varId, sc, llvm::None).x();
  183. theModule.addVariable(std::move(constructSite));
  184. // Decorate with the specified Builtin
  185. const Decoration *d = Decoration::getBuiltIn(theContext, builtin);
  186. theModule.addDecoration(*d, varId);
  187. return varId;
  188. }
  189. void ModuleBuilder::decorateLocation(uint32_t targetId, uint32_t location) {
  190. const Decoration *d =
  191. Decoration::getLocation(theContext, location, llvm::None);
  192. theModule.addDecoration(*d, targetId);
  193. }
  194. #define IMPL_GET_PRIMITIVE_TYPE(ty) \
  195. \
  196. uint32_t ModuleBuilder::get##ty##Type() { \
  197. const Type *type = Type::get##ty(theContext); \
  198. const uint32_t typeId = theContext.getResultIdForType(type); \
  199. theModule.addType(type, typeId); \
  200. return typeId; \
  201. \
  202. }
  203. IMPL_GET_PRIMITIVE_TYPE(Void)
  204. IMPL_GET_PRIMITIVE_TYPE(Bool)
  205. IMPL_GET_PRIMITIVE_TYPE(Int32)
  206. IMPL_GET_PRIMITIVE_TYPE(Uint32)
  207. IMPL_GET_PRIMITIVE_TYPE(Float32)
  208. #undef IMPL_GET_PRIMITIVE_TYPE
  209. uint32_t ModuleBuilder::getVecType(uint32_t elemType, uint32_t elemCount) {
  210. const Type *type = nullptr;
  211. switch (elemCount) {
  212. case 2:
  213. type = Type::getVec2(theContext, elemType);
  214. break;
  215. case 3:
  216. type = Type::getVec3(theContext, elemType);
  217. break;
  218. case 4:
  219. type = Type::getVec4(theContext, elemType);
  220. break;
  221. default:
  222. assert(false && "unhandled vector size");
  223. // Error found. Return 0 as the <result-id> directly.
  224. return 0;
  225. }
  226. const uint32_t typeId = theContext.getResultIdForType(type);
  227. theModule.addType(type, typeId);
  228. return typeId;
  229. }
  230. uint32_t ModuleBuilder::getPointerType(uint32_t pointeeType,
  231. spv::StorageClass storageClass) {
  232. const Type *type = Type::getPointer(theContext, storageClass, pointeeType);
  233. const uint32_t typeId = theContext.getResultIdForType(type);
  234. theModule.addType(type, typeId);
  235. return typeId;
  236. }
  237. uint32_t ModuleBuilder::getStructType(llvm::ArrayRef<uint32_t> fieldTypes) {
  238. const Type *type = Type::getStruct(theContext, fieldTypes);
  239. const uint32_t typeId = theContext.getResultIdForType(type);
  240. theModule.addType(type, typeId);
  241. return typeId;
  242. }
  243. uint32_t
  244. ModuleBuilder::getFunctionType(uint32_t returnType,
  245. const std::vector<uint32_t> &paramTypes) {
  246. const Type *type = Type::getFunction(theContext, returnType, paramTypes);
  247. const uint32_t typeId = theContext.getResultIdForType(type);
  248. theModule.addType(type, typeId);
  249. return typeId;
  250. }
  251. uint32_t ModuleBuilder::getConstantBool(bool value) {
  252. const uint32_t typeId = getBoolType();
  253. const Constant *constant = value ? Constant::getTrue(theContext, typeId)
  254. : Constant::getFalse(theContext, typeId);
  255. const uint32_t constId = theContext.getResultIdForConstant(constant);
  256. theModule.addConstant(constant, constId);
  257. return constId;
  258. }
  259. #define IMPL_GET_PRIMITIVE_CONST(builderTy, cppTy) \
  260. \
  261. uint32_t ModuleBuilder::getConstant##builderTy(cppTy value) { \
  262. const uint32_t typeId = get##builderTy##Type(); \
  263. const Constant *constant = \
  264. Constant::get##builderTy(theContext, typeId, value); \
  265. const uint32_t constId = theContext.getResultIdForConstant(constant); \
  266. theModule.addConstant(constant, constId); \
  267. return constId; \
  268. \
  269. }
  270. IMPL_GET_PRIMITIVE_CONST(Int32, int32_t)
  271. IMPL_GET_PRIMITIVE_CONST(Uint32, uint32_t)
  272. IMPL_GET_PRIMITIVE_CONST(Float32, float)
  273. #undef IMPL_GET_PRIMITIVE_VALUE
  274. uint32_t
  275. ModuleBuilder::getConstantComposite(uint32_t typeId,
  276. llvm::ArrayRef<uint32_t> constituents) {
  277. const Constant *constant =
  278. Constant::getComposite(theContext, typeId, constituents);
  279. const uint32_t constId = theContext.getResultIdForConstant(constant);
  280. theModule.addConstant(constant, constId);
  281. return constId;
  282. }
  283. } // end namespace spirv
  284. } // end namespace clang