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 15 #ifndef SOURCE_OPT_FEATURE_MANAGER_H_ 16 #define SOURCE_OPT_FEATURE_MANAGER_H_ 17 18 #include "source/assembly_grammar.h" 19 #include "source/extensions.h" 20 #include "source/opt/module.h" 21 22 namespace spvtools { 23 namespace opt { 24 25 // Tracks features enabled by a module. The IRContext has a FeatureManager. 26 class FeatureManager { 27 public: FeatureManager(const AssemblyGrammar & grammar)28 explicit FeatureManager(const AssemblyGrammar& grammar) : grammar_(grammar) {} 29 30 // Returns true if |ext| is an enabled extension in the module. HasExtension(Extension ext)31 bool HasExtension(Extension ext) const { return extensions_.Contains(ext); } 32 33 // Removes the given |extension| from the current FeatureManager. 34 void RemoveExtension(Extension extension); 35 36 // Returns true if |cap| is an enabled capability in the module. HasCapability(SpvCapability cap)37 bool HasCapability(SpvCapability cap) const { 38 return capabilities_.Contains(cap); 39 } 40 41 // Removes the given |capability| from the current FeatureManager. 42 void RemoveCapability(SpvCapability capability); 43 44 // Analyzes |module| and records enabled extensions and capabilities. 45 void Analyze(Module* module); 46 GetCapabilities()47 CapabilitySet* GetCapabilities() { return &capabilities_; } GetCapabilities()48 const CapabilitySet* GetCapabilities() const { return &capabilities_; } 49 GetExtInstImportId_GLSLstd450()50 uint32_t GetExtInstImportId_GLSLstd450() const { 51 return extinst_importid_GLSLstd450_; 52 } 53 GetExtInstImportId_OpenCL100DebugInfo()54 uint32_t GetExtInstImportId_OpenCL100DebugInfo() const { 55 return extinst_importid_OpenCL100DebugInfo_; 56 } 57 GetExtInstImportId_Shader100DebugInfo()58 uint32_t GetExtInstImportId_Shader100DebugInfo() const { 59 return extinst_importid_Shader100DebugInfo_; 60 } 61 62 friend bool operator==(const FeatureManager& a, const FeatureManager& b); 63 friend bool operator!=(const FeatureManager& a, const FeatureManager& b) { 64 return !(a == b); 65 } 66 67 // Adds the given |capability| and all implied capabilities into the current 68 // FeatureManager. 69 void AddCapability(SpvCapability capability); 70 71 // Add the extension |ext| to the feature manager. 72 void AddExtension(Instruction* ext); 73 74 // Analyzes |module| and records imported external instruction sets. 75 void AddExtInstImportIds(Module* module); 76 77 private: 78 // Analyzes |module| and records enabled extensions. 79 void AddExtensions(Module* module); 80 81 // Analyzes |module| and records enabled capabilities. 82 void AddCapabilities(Module* module); 83 84 // Auxiliary object for querying SPIR-V grammar facts. 85 const AssemblyGrammar& grammar_; 86 87 // The enabled extensions. 88 ExtensionSet extensions_; 89 90 // The enabled capabilities. 91 CapabilitySet capabilities_; 92 93 // Common external instruction import ids, cached for performance. 94 uint32_t extinst_importid_GLSLstd450_ = 0; 95 96 // Common OpenCL100DebugInfo external instruction import ids, cached 97 // for performance. 98 uint32_t extinst_importid_OpenCL100DebugInfo_ = 0; 99 100 // Common NonSemanticShader100DebugInfo external instruction import ids, 101 // cached for performance. 102 uint32_t extinst_importid_Shader100DebugInfo_ = 0; 103 }; 104 105 } // namespace opt 106 } // namespace spvtools 107 108 #endif // SOURCE_OPT_FEATURE_MANAGER_H_ 109