feature_manager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/feature_manager.h"
  15. #include <queue>
  16. #include <stack>
  17. #include <string>
  18. #include "source/enum_string_mapping.h"
  19. namespace spvtools {
  20. namespace opt {
  21. void FeatureManager::Analyze(Module* module) {
  22. AddExtensions(module);
  23. AddCapabilities(module);
  24. AddExtInstImportIds(module);
  25. }
  26. void FeatureManager::AddExtensions(Module* module) {
  27. for (auto ext : module->extensions()) {
  28. AddExtension(&ext);
  29. }
  30. }
  31. void FeatureManager::AddExtension(Instruction* ext) {
  32. assert(ext->opcode() == SpvOpExtension &&
  33. "Expecting an extension instruction.");
  34. const std::string name =
  35. reinterpret_cast<const char*>(ext->GetInOperand(0u).words.data());
  36. Extension extension;
  37. if (GetExtensionFromString(name.c_str(), &extension)) {
  38. extensions_.Add(extension);
  39. }
  40. }
  41. void FeatureManager::RemoveExtension(Extension ext) {
  42. if (!extensions_.Contains(ext)) return;
  43. extensions_.Remove(ext);
  44. }
  45. void FeatureManager::AddCapability(SpvCapability cap) {
  46. if (capabilities_.Contains(cap)) return;
  47. capabilities_.Add(cap);
  48. spv_operand_desc desc = {};
  49. if (SPV_SUCCESS ==
  50. grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc)) {
  51. CapabilitySet(desc->numCapabilities, desc->capabilities)
  52. .ForEach([this](SpvCapability c) { AddCapability(c); });
  53. }
  54. }
  55. void FeatureManager::RemoveCapability(SpvCapability cap) {
  56. if (!capabilities_.Contains(cap)) return;
  57. capabilities_.Remove(cap);
  58. }
  59. void FeatureManager::AddCapabilities(Module* module) {
  60. for (Instruction& inst : module->capabilities()) {
  61. AddCapability(static_cast<SpvCapability>(inst.GetSingleWordInOperand(0)));
  62. }
  63. }
  64. void FeatureManager::AddExtInstImportIds(Module* module) {
  65. extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
  66. extinst_importid_OpenCL100DebugInfo_ =
  67. module->GetExtInstImportId("OpenCL.DebugInfo.100");
  68. extinst_importid_Vulkan100DebugInfo_ =
  69. module->GetExtInstImportId("NonSemantic.Vulkan.DebugInfo.100");
  70. }
  71. bool operator==(const FeatureManager& a, const FeatureManager& b) {
  72. // We check that the addresses of the grammars are the same because they
  73. // are large objects, and this is faster. It can be changed if needed as a
  74. // later time.
  75. if (&a.grammar_ != &b.grammar_) {
  76. return false;
  77. }
  78. if (a.capabilities_ != b.capabilities_) {
  79. return false;
  80. }
  81. if (a.extensions_ != b.extensions_) {
  82. return false;
  83. }
  84. if (a.extinst_importid_GLSLstd450_ != b.extinst_importid_GLSLstd450_) {
  85. return false;
  86. }
  87. if (a.extinst_importid_OpenCL100DebugInfo_ !=
  88. b.extinst_importid_OpenCL100DebugInfo_) {
  89. return false;
  90. }
  91. if (a.extinst_importid_Vulkan100DebugInfo_ !=
  92. b.extinst_importid_Vulkan100DebugInfo_) {
  93. return false;
  94. }
  95. return true;
  96. }
  97. } // namespace opt
  98. } // namespace spvtools