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 <string>
  16. #include "source/enum_string_mapping.h"
  17. namespace spvtools {
  18. namespace opt {
  19. void FeatureManager::Analyze(Module* module) {
  20. AddExtensions(module);
  21. AddCapabilities(module);
  22. AddExtInstImportIds(module);
  23. }
  24. void FeatureManager::AddExtensions(Module* module) {
  25. for (auto ext : module->extensions()) {
  26. AddExtension(&ext);
  27. }
  28. }
  29. void FeatureManager::AddExtension(Instruction* ext) {
  30. assert(ext->opcode() == spv::Op::OpExtension &&
  31. "Expecting an extension instruction.");
  32. const std::string name = ext->GetInOperand(0u).AsString();
  33. Extension extension;
  34. if (GetExtensionFromString(name.c_str(), &extension)) {
  35. extensions_.insert(extension);
  36. }
  37. }
  38. void FeatureManager::RemoveExtension(Extension ext) {
  39. if (!extensions_.contains(ext)) return;
  40. extensions_.erase(ext);
  41. }
  42. void FeatureManager::AddCapability(spv::Capability cap) {
  43. if (capabilities_.contains(cap)) return;
  44. capabilities_.insert(cap);
  45. spv_operand_desc desc = {};
  46. if (SPV_SUCCESS == grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
  47. uint32_t(cap), &desc)) {
  48. for (auto capability :
  49. CapabilitySet(desc->numCapabilities, desc->capabilities)) {
  50. AddCapability(capability);
  51. }
  52. }
  53. }
  54. void FeatureManager::RemoveCapability(spv::Capability cap) {
  55. if (!capabilities_.contains(cap)) return;
  56. capabilities_.erase(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