feature_manager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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() == spv::Op::OpExtension &&
  33. "Expecting an extension instruction.");
  34. const std::string name = ext->GetInOperand(0u).AsString();
  35. Extension extension;
  36. if (GetExtensionFromString(name.c_str(), &extension)) {
  37. extensions_.Add(extension);
  38. }
  39. }
  40. void FeatureManager::RemoveExtension(Extension ext) {
  41. if (!extensions_.Contains(ext)) return;
  42. extensions_.Remove(ext);
  43. }
  44. void FeatureManager::AddCapability(spv::Capability cap) {
  45. if (capabilities_.Contains(cap)) return;
  46. capabilities_.Add(cap);
  47. spv_operand_desc desc = {};
  48. if (SPV_SUCCESS == grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
  49. uint32_t(cap), &desc)) {
  50. CapabilitySet(desc->numCapabilities, desc->capabilities)
  51. .ForEach([this](spv::Capability c) { AddCapability(c); });
  52. }
  53. }
  54. void FeatureManager::RemoveCapability(spv::Capability cap) {
  55. if (!capabilities_.Contains(cap)) return;
  56. capabilities_.Remove(cap);
  57. }
  58. void FeatureManager::AddCapabilities(Module* module) {
  59. for (Instruction& inst : module->capabilities()) {
  60. AddCapability(static_cast<spv::Capability>(inst.GetSingleWordInOperand(0)));
  61. }
  62. }
  63. void FeatureManager::AddExtInstImportIds(Module* module) {
  64. extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
  65. extinst_importid_OpenCL100DebugInfo_ =
  66. module->GetExtInstImportId("OpenCL.DebugInfo.100");
  67. extinst_importid_Shader100DebugInfo_ =
  68. module->GetExtInstImportId("NonSemantic.Shader.DebugInfo.100");
  69. }
  70. bool operator==(const FeatureManager& a, const FeatureManager& b) {
  71. // We check that the addresses of the grammars are the same because they
  72. // are large objects, and this is faster. It can be changed if needed as a
  73. // later time.
  74. if (&a.grammar_ != &b.grammar_) {
  75. return false;
  76. }
  77. if (a.capabilities_ != b.capabilities_) {
  78. return false;
  79. }
  80. if (a.extensions_ != b.extensions_) {
  81. return false;
  82. }
  83. if (a.extinst_importid_GLSLstd450_ != b.extinst_importid_GLSLstd450_) {
  84. return false;
  85. }
  86. if (a.extinst_importid_OpenCL100DebugInfo_ !=
  87. b.extinst_importid_OpenCL100DebugInfo_) {
  88. return false;
  89. }
  90. if (a.extinst_importid_Shader100DebugInfo_ !=
  91. b.extinst_importid_Shader100DebugInfo_) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. } // namespace opt
  97. } // namespace spvtools