ModuleBuilder.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. assert(theFunction && "found detached local variable");
  54. const uint32_t varId = theContext.takeNextId();
  55. theFunction->addVariable(type, varId);
  56. theModule.addDebugName(varId, name);
  57. return varId;
  58. }
  59. bool ModuleBuilder::endFunction() {
  60. if (theFunction == nullptr) {
  61. assert(false && "no active function");
  62. return false;
  63. }
  64. // Move all basic blocks into the current function.
  65. // TODO: we should adjust the order the basic blocks according to
  66. // SPIR-V validation rules.
  67. for (auto &bb : basicBlocks) {
  68. theFunction->addBasicBlock(std::move(bb.second));
  69. }
  70. basicBlocks.clear();
  71. theModule.addFunction(std::move(theFunction));
  72. theFunction.reset(nullptr);
  73. insertPoint = nullptr;
  74. return true;
  75. }
  76. uint32_t ModuleBuilder::createBasicBlock(llvm::StringRef name) {
  77. if (theFunction == nullptr) {
  78. assert(false && "found detached basic block");
  79. return 0;
  80. }
  81. const uint32_t labelId = theContext.takeNextId();
  82. basicBlocks[labelId] = llvm::make_unique<BasicBlock>(labelId);
  83. theModule.addDebugName(labelId, name);
  84. return labelId;
  85. }
  86. bool ModuleBuilder::setInsertPoint(uint32_t labelId) {
  87. auto it = basicBlocks.find(labelId);
  88. if (it == basicBlocks.end()) {
  89. assert(false && "invalid <label-id>");
  90. return false;
  91. }
  92. insertPoint = it->second.get();
  93. return true;
  94. }
  95. uint32_t ModuleBuilder::createLoad(uint32_t resultType, uint32_t pointer) {
  96. assert(insertPoint && "null insert point");
  97. const uint32_t resultId = theContext.takeNextId();
  98. instBuilder.opLoad(resultType, resultId, pointer, llvm::None).x();
  99. insertPoint->appendInstruction(std::move(constructSite));
  100. return resultId;
  101. }
  102. void ModuleBuilder::createStore(uint32_t address, uint32_t value) {
  103. assert(insertPoint && "null insert point");
  104. instBuilder.opStore(address, value, llvm::None).x();
  105. insertPoint->appendInstruction(std::move(constructSite));
  106. }
  107. uint32_t ModuleBuilder::createAccessChain(uint32_t resultType, uint32_t base,
  108. llvm::ArrayRef<uint32_t> indexes) {
  109. assert(insertPoint && "null insert point");
  110. const uint32_t id = theContext.takeNextId();
  111. instBuilder.opAccessChain(resultType, id, base, indexes).x();
  112. insertPoint->appendInstruction(std::move(constructSite));
  113. return id;
  114. }
  115. void ModuleBuilder::createReturn() {
  116. assert(insertPoint && "null insert point");
  117. instBuilder.opReturn().x();
  118. insertPoint->appendInstruction(std::move(constructSite));
  119. }
  120. void ModuleBuilder::createReturnValue(uint32_t value) {
  121. assert(insertPoint && "null insert point");
  122. instBuilder.opReturnValue(value).x();
  123. insertPoint->appendInstruction(std::move(constructSite));
  124. }
  125. void ModuleBuilder::addExecutionMode(uint32_t entryPointId,
  126. spv::ExecutionMode em,
  127. const std::vector<uint32_t> &params) {
  128. instBuilder.opExecutionMode(entryPointId, em);
  129. for (const auto &param : params) {
  130. instBuilder.literalInteger(param);
  131. }
  132. instBuilder.x();
  133. theModule.addExecutionMode(std::move(constructSite));
  134. }
  135. uint32_t ModuleBuilder::addStageIOVariable(uint32_t type,
  136. spv::StorageClass storageClass) {
  137. const uint32_t pointerType = getPointerType(type, storageClass);
  138. const uint32_t varId = theContext.takeNextId();
  139. instBuilder.opVariable(pointerType, varId, storageClass, llvm::None).x();
  140. theModule.addVariable(std::move(constructSite));
  141. return varId;
  142. }
  143. uint32_t ModuleBuilder::addStageBuiltinVariable(uint32_t type,
  144. spv::BuiltIn builtin) {
  145. spv::StorageClass sc = spv::StorageClass::Input;
  146. switch (builtin) {
  147. case spv::BuiltIn::Position:
  148. case spv::BuiltIn::PointSize:
  149. // TODO: add the rest output builtins
  150. sc = spv::StorageClass::Output;
  151. break;
  152. default:
  153. break;
  154. }
  155. const uint32_t pointerType = getPointerType(type, sc);
  156. const uint32_t varId = theContext.takeNextId();
  157. instBuilder.opVariable(pointerType, varId, sc, llvm::None).x();
  158. theModule.addVariable(std::move(constructSite));
  159. // Decorate with the specified Builtin
  160. const Decoration *d = Decoration::getBuiltIn(theContext, builtin);
  161. theModule.addDecoration(*d, varId);
  162. return varId;
  163. }
  164. void ModuleBuilder::decorateLocation(uint32_t targetId, uint32_t location) {
  165. const Decoration *d =
  166. Decoration::getLocation(theContext, location, llvm::None);
  167. theModule.addDecoration(*d, targetId);
  168. }
  169. #define IMPL_GET_PRIMITIVE_TYPE(ty) \
  170. \
  171. uint32_t ModuleBuilder::get##ty##Type() { \
  172. const Type *type = Type::get##ty(theContext); \
  173. const uint32_t typeId = theContext.getResultIdForType(type); \
  174. theModule.addType(type, typeId); \
  175. return typeId; \
  176. \
  177. }
  178. IMPL_GET_PRIMITIVE_TYPE(Void)
  179. IMPL_GET_PRIMITIVE_TYPE(Int32)
  180. IMPL_GET_PRIMITIVE_TYPE(Uint32)
  181. IMPL_GET_PRIMITIVE_TYPE(Float32)
  182. #undef IMPL_GET_PRIMITIVE_TYPE
  183. uint32_t ModuleBuilder::getVecType(uint32_t elemType, uint32_t elemCount) {
  184. const Type *type = nullptr;
  185. switch (elemCount) {
  186. case 2:
  187. type = Type::getVec2(theContext, elemType);
  188. break;
  189. case 3:
  190. type = Type::getVec3(theContext, elemType);
  191. break;
  192. case 4:
  193. type = Type::getVec4(theContext, elemType);
  194. break;
  195. default:
  196. assert(false && "unhandled vector size");
  197. // Error found. Return 0 as the <result-id> directly.
  198. return 0;
  199. }
  200. const uint32_t typeId = theContext.getResultIdForType(type);
  201. theModule.addType(type, typeId);
  202. return typeId;
  203. }
  204. uint32_t ModuleBuilder::getPointerType(uint32_t pointeeType,
  205. spv::StorageClass storageClass) {
  206. const Type *type = Type::getPointer(theContext, storageClass, pointeeType);
  207. const uint32_t typeId = theContext.getResultIdForType(type);
  208. theModule.addType(type, typeId);
  209. return typeId;
  210. }
  211. uint32_t ModuleBuilder::getStructType(llvm::ArrayRef<uint32_t> fieldTypes) {
  212. const Type *type = Type::getStruct(theContext, fieldTypes);
  213. const uint32_t typeId = theContext.getResultIdForType(type);
  214. theModule.addType(type, typeId);
  215. return typeId;
  216. }
  217. uint32_t
  218. ModuleBuilder::getFunctionType(uint32_t returnType,
  219. const std::vector<uint32_t> &paramTypes) {
  220. const Type *type = Type::getFunction(theContext, returnType, paramTypes);
  221. const uint32_t typeId = theContext.getResultIdForType(type);
  222. theModule.addType(type, typeId);
  223. return typeId;
  224. }
  225. #define IMPL_GET_PRIMITIVE_VALUE(builderTy, cppTy) \
  226. \
  227. uint32_t ModuleBuilder::getConstant##builderTy(cppTy value) { \
  228. const uint32_t typeId = get##builderTy##Type(); \
  229. const Constant *constant = \
  230. Constant::get##builderTy(theContext, typeId, value); \
  231. const uint32_t constId = theContext.getResultIdForConstant(constant); \
  232. theModule.addConstant(constant, constId); \
  233. return constId; \
  234. \
  235. }
  236. IMPL_GET_PRIMITIVE_VALUE(Int32, int32_t)
  237. IMPL_GET_PRIMITIVE_VALUE(Uint32, uint32_t)
  238. IMPL_GET_PRIMITIVE_VALUE(Float32, float)
  239. #undef IMPL_GET_PRIMITIVE_VALUE
  240. uint32_t
  241. ModuleBuilder::getConstantComposite(uint32_t typeId,
  242. llvm::ArrayRef<uint32_t> constituents) {
  243. const Constant *constant =
  244. Constant::getComposite(theContext, typeId, constituents);
  245. const uint32_t constId = theContext.getResultIdForConstant(constant);
  246. theModule.addConstant(constant, constId);
  247. return constId;
  248. }
  249. } // end namespace spirv
  250. } // end namespace clang