SpirvModule.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //===--- SpirvModule.cpp - SPIR-V Module 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/SpirvModule.h"
  10. #include "clang/SPIRV/SpirvFunction.h"
  11. #include "clang/SPIRV/SpirvVisitor.h"
  12. namespace clang {
  13. namespace spirv {
  14. SpirvModule::SpirvModule()
  15. : capabilities({}), extensions({}), extInstSets({}), memoryModel(nullptr),
  16. entryPoints({}), executionModes({}), moduleProcesses({}), decorations({}),
  17. constants({}), variables({}), functions({}) {}
  18. bool SpirvModule::invokeVisitor(Visitor *visitor, bool reverseOrder) {
  19. // Note: It is debatable whether reverse order of visiting the module should
  20. // reverse everything in this method. For the time being, we just reverse the
  21. // order of the function visitors, and keeping everything else the same.
  22. // For example, it is not clear what the value would be of vising the last
  23. // function first. We can update this methodology if needed.
  24. if (!visitor->visit(this, Visitor::Phase::Init))
  25. return false;
  26. if (reverseOrder) {
  27. // Reverse order of a SPIR-V module.
  28. // Our transformations do not cross function bounaries, therefore the order
  29. // of visiting functions is not important.
  30. for (auto iter = functions.rbegin(); iter != functions.rend(); ++iter) {
  31. auto *fn = *iter;
  32. if (!fn->invokeVisitor(visitor, reverseOrder))
  33. return false;
  34. }
  35. for (auto iter = variables.rbegin(); iter != variables.rend(); ++iter) {
  36. auto *var = *iter;
  37. if (!var->invokeVisitor(visitor))
  38. return false;
  39. }
  40. for (auto iter = constants.rbegin(); iter != constants.rend(); ++iter) {
  41. auto *constant = *iter;
  42. if (!constant->invokeVisitor(visitor))
  43. return false;
  44. }
  45. // Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
  46. // manual indexing.
  47. for (auto decorIndex = decorations.size(); decorIndex > 0; --decorIndex) {
  48. auto *decoration = decorations[decorIndex - 1];
  49. if (!decoration->invokeVisitor(visitor))
  50. return false;
  51. }
  52. for (auto iter = moduleProcesses.rbegin(); iter != moduleProcesses.rend();
  53. ++iter) {
  54. auto *moduleProcess = *iter;
  55. if (!moduleProcess->invokeVisitor(visitor))
  56. return false;
  57. }
  58. if (!debugSources.empty())
  59. for (auto iter = debugSources.rbegin(); iter != debugSources.rend();
  60. ++iter) {
  61. auto *source = *iter;
  62. if (!source->invokeVisitor(visitor))
  63. return false;
  64. }
  65. for (auto iter = constStrings.rbegin(); iter != constStrings.rend();
  66. ++iter) {
  67. if (!(*iter)->invokeVisitor(visitor))
  68. return false;
  69. }
  70. for (auto iter = executionModes.rbegin(); iter != executionModes.rend();
  71. ++iter) {
  72. auto *execMode = *iter;
  73. if (!execMode->invokeVisitor(visitor))
  74. return false;
  75. }
  76. for (auto iter = entryPoints.rbegin(); iter != entryPoints.rend(); ++iter) {
  77. auto *entryPoint = *iter;
  78. if (!entryPoint->invokeVisitor(visitor))
  79. return false;
  80. }
  81. if (!memoryModel->invokeVisitor(visitor))
  82. return false;
  83. for (auto iter = extInstSets.rbegin(); iter != extInstSets.rend(); ++iter) {
  84. auto *extInstSet = *iter;
  85. if (!extInstSet->invokeVisitor(visitor))
  86. return false;
  87. }
  88. // Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
  89. // manual indexing.
  90. for (auto extIndex = extensions.size(); extIndex > 0; --extIndex) {
  91. auto *extension = extensions[extIndex - 1];
  92. if (!extension->invokeVisitor(visitor))
  93. return false;
  94. }
  95. // Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
  96. // manual indexing.
  97. for (auto capIndex = capabilities.size(); capIndex > 0; --capIndex) {
  98. auto *capability = capabilities[capIndex - 1];
  99. if (!capability->invokeVisitor(visitor))
  100. return false;
  101. }
  102. }
  103. // Traverse the regular order of a SPIR-V module.
  104. else {
  105. for (auto *cap : capabilities)
  106. if (!cap->invokeVisitor(visitor))
  107. return false;
  108. for (auto ext : extensions)
  109. if (!ext->invokeVisitor(visitor))
  110. return false;
  111. for (auto extInstSet : extInstSets)
  112. if (!extInstSet->invokeVisitor(visitor))
  113. return false;
  114. if (!memoryModel->invokeVisitor(visitor))
  115. return false;
  116. for (auto entryPoint : entryPoints)
  117. if (!entryPoint->invokeVisitor(visitor))
  118. return false;
  119. for (auto execMode : executionModes)
  120. if (!execMode->invokeVisitor(visitor))
  121. return false;
  122. for (auto *str : constStrings)
  123. if (!str->invokeVisitor(visitor))
  124. return false;
  125. if (!debugSources.empty())
  126. for (auto *source : debugSources)
  127. if (!source->invokeVisitor(visitor))
  128. return false;
  129. for (auto moduleProcess : moduleProcesses)
  130. if (!moduleProcess->invokeVisitor(visitor))
  131. return false;
  132. for (auto decoration : decorations)
  133. if (!decoration->invokeVisitor(visitor))
  134. return false;
  135. for (auto constant : constants)
  136. if (!constant->invokeVisitor(visitor))
  137. return false;
  138. for (auto var : variables)
  139. if (!var->invokeVisitor(visitor))
  140. return false;
  141. for (auto fn : functions)
  142. if (!fn->invokeVisitor(visitor, reverseOrder))
  143. return false;
  144. }
  145. if (!visitor->visit(this, Visitor::Phase::Done))
  146. return false;
  147. return true;
  148. }
  149. void SpirvModule::addFunction(SpirvFunction *fn) {
  150. assert(fn && "cannot add null function to the module");
  151. functions.push_back(fn);
  152. }
  153. void SpirvModule::addCapability(SpirvCapability *cap) {
  154. assert(cap && "cannot add null capability to the module");
  155. capabilities.insert(cap);
  156. }
  157. void SpirvModule::setMemoryModel(SpirvMemoryModel *model) {
  158. assert(model && "cannot set a null memory model");
  159. memoryModel = model;
  160. }
  161. void SpirvModule::addEntryPoint(SpirvEntryPoint *ep) {
  162. assert(ep && "cannot add null as an entry point");
  163. entryPoints.push_back(ep);
  164. }
  165. void SpirvModule::addExecutionMode(SpirvExecutionMode *em) {
  166. assert(em && "cannot add null execution mode");
  167. executionModes.push_back(em);
  168. }
  169. void SpirvModule::addExtension(SpirvExtension *ext) {
  170. assert(ext && "cannot add null extension");
  171. extensions.insert(ext);
  172. }
  173. void SpirvModule::addExtInstSet(SpirvExtInstImport *set) {
  174. assert(set && "cannot add null extended instruction set");
  175. extInstSets.push_back(set);
  176. }
  177. SpirvExtInstImport *SpirvModule::getExtInstSet(llvm::StringRef name) {
  178. // We expect very few (usually 1) extended instruction sets to exist in the
  179. // module, so this is not expensive.
  180. auto found = std::find_if(extInstSets.begin(), extInstSets.end(),
  181. [name](const SpirvExtInstImport *set) {
  182. return set->getExtendedInstSetName() == name;
  183. });
  184. if (found != extInstSets.end())
  185. return *found;
  186. return nullptr;
  187. }
  188. void SpirvModule::addVariable(SpirvVariable *var) {
  189. assert(var && "cannot add null variable to the module");
  190. variables.push_back(var);
  191. }
  192. void SpirvModule::addDecoration(SpirvDecoration *decor) {
  193. assert(decor && "cannot add null decoration to the module");
  194. decorations.insert(decor);
  195. }
  196. void SpirvModule::addConstant(SpirvConstant *constant) {
  197. assert(constant);
  198. constants.push_back(constant);
  199. }
  200. void SpirvModule::addString(SpirvString *str) {
  201. assert(str);
  202. constStrings.push_back(str);
  203. }
  204. void SpirvModule::addDebugSource(SpirvSource *src) {
  205. assert(src);
  206. debugSources.push_back(src);
  207. }
  208. void SpirvModule::addModuleProcessed(SpirvModuleProcessed *p) {
  209. assert(p);
  210. moduleProcesses.push_back(p);
  211. }
  212. } // end namespace spirv
  213. } // end namespace clang