SpirvModule.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 = executionModes.rbegin(); iter != executionModes.rend();
  66. ++iter) {
  67. auto *execMode = *iter;
  68. if (!execMode->invokeVisitor(visitor))
  69. return false;
  70. }
  71. for (auto iter = entryPoints.rbegin(); iter != entryPoints.rend(); ++iter) {
  72. auto *entryPoint = *iter;
  73. if (!entryPoint->invokeVisitor(visitor))
  74. return false;
  75. }
  76. if (!memoryModel->invokeVisitor(visitor))
  77. return false;
  78. for (auto iter = extInstSets.rbegin(); iter != extInstSets.rend(); ++iter) {
  79. auto *extInstSet = *iter;
  80. if (!extInstSet->invokeVisitor(visitor))
  81. return false;
  82. }
  83. // Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
  84. // manual indexing.
  85. for (auto extIndex = extensions.size(); extIndex > 0; --extIndex) {
  86. auto *extension = extensions[extIndex - 1];
  87. if (!extension->invokeVisitor(visitor))
  88. return false;
  89. }
  90. // Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
  91. // manual indexing.
  92. for (auto capIndex = capabilities.size(); capIndex > 0; --capIndex) {
  93. auto *capability = capabilities[capIndex - 1];
  94. if (!capability->invokeVisitor(visitor))
  95. return false;
  96. }
  97. }
  98. // Traverse the regular order of a SPIR-V module.
  99. else {
  100. for (auto *cap : capabilities)
  101. if (!cap->invokeVisitor(visitor))
  102. return false;
  103. for (auto ext : extensions)
  104. if (!ext->invokeVisitor(visitor))
  105. return false;
  106. for (auto extInstSet : extInstSets)
  107. if (!extInstSet->invokeVisitor(visitor))
  108. return false;
  109. if (!memoryModel->invokeVisitor(visitor))
  110. return false;
  111. for (auto entryPoint : entryPoints)
  112. if (!entryPoint->invokeVisitor(visitor))
  113. return false;
  114. for (auto execMode : executionModes)
  115. if (!execMode->invokeVisitor(visitor))
  116. return false;
  117. if (!debugSources.empty())
  118. for (auto *source : debugSources)
  119. if (!source->invokeVisitor(visitor))
  120. return false;
  121. for (auto moduleProcess : moduleProcesses)
  122. if (!moduleProcess->invokeVisitor(visitor))
  123. return false;
  124. for (auto decoration : decorations)
  125. if (!decoration->invokeVisitor(visitor))
  126. return false;
  127. for (auto constant : constants)
  128. if (!constant->invokeVisitor(visitor))
  129. return false;
  130. for (auto var : variables)
  131. if (!var->invokeVisitor(visitor))
  132. return false;
  133. for (auto fn : functions)
  134. if (!fn->invokeVisitor(visitor, reverseOrder))
  135. return false;
  136. }
  137. if (!visitor->visit(this, Visitor::Phase::Done))
  138. return false;
  139. return true;
  140. }
  141. void SpirvModule::addFunction(SpirvFunction *fn) {
  142. assert(fn && "cannot add null function to the module");
  143. functions.push_back(fn);
  144. }
  145. void SpirvModule::addCapability(SpirvCapability *cap) {
  146. assert(cap && "cannot add null capability to the module");
  147. capabilities.insert(cap);
  148. }
  149. void SpirvModule::setMemoryModel(SpirvMemoryModel *model) {
  150. assert(model && "cannot set a null memory model");
  151. memoryModel = model;
  152. }
  153. void SpirvModule::addEntryPoint(SpirvEntryPoint *ep) {
  154. assert(ep && "cannot add null as an entry point");
  155. entryPoints.push_back(ep);
  156. }
  157. void SpirvModule::addExecutionMode(SpirvExecutionMode *em) {
  158. assert(em && "cannot add null execution mode");
  159. executionModes.push_back(em);
  160. }
  161. void SpirvModule::addExtension(SpirvExtension *ext) {
  162. assert(ext && "cannot add null extension");
  163. extensions.insert(ext);
  164. }
  165. void SpirvModule::addExtInstSet(SpirvExtInstImport *set) {
  166. assert(set && "cannot add null extended instruction set");
  167. extInstSets.push_back(set);
  168. }
  169. SpirvExtInstImport *SpirvModule::getGLSLExtInstSet() {
  170. // We expect very few (usually 1) extended instruction sets to exist in the
  171. // module, so this is not expensive.
  172. auto found =
  173. std::find_if(extInstSets.begin(), extInstSets.end(),
  174. [](const SpirvExtInstImport *set) {
  175. return set->getExtendedInstSetName() == "GLSL.std.450";
  176. });
  177. if (found != extInstSets.end())
  178. return *found;
  179. return nullptr;
  180. }
  181. void SpirvModule::addVariable(SpirvVariable *var) {
  182. assert(var && "cannot add null variable to the module");
  183. variables.push_back(var);
  184. }
  185. void SpirvModule::addDecoration(SpirvDecoration *decor) {
  186. assert(decor && "cannot add null decoration to the module");
  187. decorations.insert(decor);
  188. }
  189. void SpirvModule::addConstant(SpirvConstant *constant) {
  190. assert(constant);
  191. constants.push_back(constant);
  192. }
  193. void SpirvModule::addDebugSource(SpirvSource *src) {
  194. assert(src);
  195. debugSources.push_back(src);
  196. }
  197. void SpirvModule::addModuleProcessed(SpirvModuleProcessed *p) {
  198. assert(p);
  199. moduleProcesses.push_back(p);
  200. }
  201. } // end namespace spirv
  202. } // end namespace clang