SpirvModule.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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({}), debugSource(nullptr),
  17. moduleProcesses({}), decorations({}), constants({}), variables({}),
  18. functions({}) {}
  19. bool SpirvModule::invokeVisitor(Visitor *visitor, bool reverseOrder) {
  20. // Note: It is debatable whether reverse order of visiting the module should
  21. // reverse everything in this method. For the time being, we just reverse the
  22. // order of the function visitors, and keeping everything else the same.
  23. // For example, it is not clear what the value would be of vising the last
  24. // function first. We can update this methodology if needed.
  25. if (!visitor->visit(this, Visitor::Phase::Init))
  26. return false;
  27. for (auto *cap : capabilities)
  28. if (!cap->invokeVisitor(visitor))
  29. return false;
  30. for (auto ext : extensions)
  31. if (!ext->invokeVisitor(visitor))
  32. return false;
  33. for (auto extInstSet : extInstSets)
  34. if (!extInstSet->invokeVisitor(visitor))
  35. return false;
  36. if (!memoryModel->invokeVisitor(visitor))
  37. return false;
  38. for (auto entryPoint : entryPoints)
  39. if (!entryPoint->invokeVisitor(visitor))
  40. return false;
  41. for (auto execMode : executionModes)
  42. if (!execMode->invokeVisitor(visitor))
  43. return false;
  44. if (debugSource)
  45. if (!debugSource->invokeVisitor(visitor))
  46. return false;
  47. for (auto moduleProcess : moduleProcesses)
  48. if (!moduleProcess->invokeVisitor(visitor))
  49. return false;
  50. for (auto decoration : decorations)
  51. if (!decoration->invokeVisitor(visitor))
  52. return false;
  53. for (auto constant : constants)
  54. constant->invokeVisitor(visitor);
  55. for (auto var : variables)
  56. if (!var->invokeVisitor(visitor))
  57. return false;
  58. for (auto fn : functions)
  59. if (!fn->invokeVisitor(visitor, reverseOrder))
  60. return false;
  61. if (!visitor->visit(this, Visitor::Phase::Done))
  62. return false;
  63. return true;
  64. }
  65. void SpirvModule::addFunction(SpirvFunction *fn) {
  66. assert(fn && "cannot add null function to the module");
  67. functions.insert(fn);
  68. }
  69. void SpirvModule::addCapability(SpirvCapability *cap) {
  70. assert(cap && "cannot add null capability to the module");
  71. capabilities.push_back(cap);
  72. }
  73. void SpirvModule::setMemoryModel(SpirvMemoryModel *model) {
  74. assert(model && "cannot set a null memory model");
  75. memoryModel = model;
  76. }
  77. void SpirvModule::addEntryPoint(SpirvEntryPoint *ep) {
  78. assert(ep && "cannot add null as an entry point");
  79. entryPoints.push_back(ep);
  80. }
  81. void SpirvModule::addExecutionMode(SpirvExecutionMode *em) {
  82. assert(em && "cannot add null execution mode");
  83. executionModes.push_back(em);
  84. }
  85. void SpirvModule::addExtension(SpirvExtension *ext) {
  86. assert(ext && "cannot add null extension");
  87. extensions.push_back(ext);
  88. }
  89. void SpirvModule::addExtInstSet(SpirvExtInstImport *set) {
  90. assert(set && "cannot add null extended instruction set");
  91. extInstSets.push_back(set);
  92. }
  93. SpirvExtInstImport *SpirvModule::getGLSLExtInstSet() {
  94. // We expect very few (usually 1) extended instruction sets to exist in the
  95. // module, so this is not expensive.
  96. auto found =
  97. std::find_if(extInstSets.begin(), extInstSets.end(),
  98. [](const SpirvExtInstImport *set) {
  99. return set->getExtendedInstSetName() == "GLSL.std.450";
  100. });
  101. if (found != extInstSets.end())
  102. return *found;
  103. return nullptr;
  104. }
  105. void SpirvModule::addVariable(SpirvVariable *var) {
  106. assert(var && "cannot add null variable to the module");
  107. variables.push_back(var);
  108. }
  109. void SpirvModule::addDecoration(SpirvDecoration *decor) {
  110. assert(decor && "cannot add null decoration to the module");
  111. decorations.push_back(decor);
  112. }
  113. void SpirvModule::addConstant(SpirvConstant *constant) {
  114. assert(constant);
  115. constants.push_back(constant);
  116. }
  117. void SpirvModule::addDebugSource(SpirvSource *src) {
  118. assert(src);
  119. debugSource = src;
  120. }
  121. void SpirvModule::addModuleProcessed(SpirvModuleProcessed *p) {
  122. assert(p);
  123. moduleProcesses.push_back(p);
  124. }
  125. } // end namespace spirv
  126. } // end namespace clang