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 58 friend bool operator==(const FeatureManager& a, const FeatureManager& b); 59 friend bool operator!=(const FeatureManager& a, const FeatureManager& b) { 60 return !(a == b); 61 } 62 63 // Adds the given |capability| and all implied capabilities into the current 64 // FeatureManager. 65 void AddCapability(SpvCapability capability); 66 67 // Add the extension |ext| to the feature manager. 68 void AddExtension(Instruction* ext); 69 70 // Analyzes |module| and records imported external instruction sets. 71 void AddExtInstImportIds(Module* module); 72 73 private: 74 // Analyzes |module| and records enabled extensions. 75 void AddExtensions(Module* module); 76 77 // Analyzes |module| and records enabled capabilities. 78 void AddCapabilities(Module* module); 79 80 // Auxiliary object for querying SPIR-V grammar facts. 81 const AssemblyGrammar& grammar_; 82 83 // The enabled extensions. 84 ExtensionSet extensions_; 85 86 // The enabled capabilities. 87 CapabilitySet capabilities_; 88 89 // Common external instruction import ids, cached for performance. 90 uint32_t extinst_importid_GLSLstd450_ = 0; 91 92 // Common OpenCL100DebugInfo external instruction import ids, cached 93 // for performance. 94 uint32_t extinst_importid_OpenCL100DebugInfo_ = 0; 95 }; 96 97 } // namespace opt 98 } // namespace spvtools 99 100 #endif // SOURCE_OPT_FEATURE_MANAGER_H_ 101