Home | History | Annotate | Download | only in opt
      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:
     28   explicit FeatureManager(const AssemblyGrammar& grammar) : grammar_(grammar) {}
     29 
     30   // Returns true if |ext| is an enabled extension in the module.
     31   bool HasExtension(Extension ext) const { return extensions_.Contains(ext); }
     32 
     33   // Returns true if |cap| is an enabled capability in the module.
     34   bool HasCapability(SpvCapability cap) const {
     35     return capabilities_.Contains(cap);
     36   }
     37 
     38   // Analyzes |module| and records enabled extensions and capabilities.
     39   void Analyze(Module* module);
     40 
     41   CapabilitySet* GetCapabilities() { return &capabilities_; }
     42   const CapabilitySet* GetCapabilities() const { return &capabilities_; }
     43 
     44   uint32_t GetExtInstImportId_GLSLstd450() const {
     45     return extinst_importid_GLSLstd450_;
     46   }
     47 
     48  private:
     49   // Analyzes |module| and records enabled extensions.
     50   void AddExtensions(Module* module);
     51 
     52   // Adds the given |capability| and all implied capabilities into the current
     53   // FeatureManager.
     54   void AddCapability(SpvCapability capability);
     55 
     56   // Analyzes |module| and records enabled capabilities.
     57   void AddCapabilities(Module* module);
     58 
     59   // Analyzes |module| and records imported external instruction sets.
     60   void AddExtInstImportIds(Module* module);
     61 
     62   // Auxiliary object for querying SPIR-V grammar facts.
     63   const AssemblyGrammar& grammar_;
     64 
     65   // The enabled extensions.
     66   ExtensionSet extensions_;
     67 
     68   // The enabled capabilities.
     69   CapabilitySet capabilities_;
     70 
     71   // Common external instruction import ids, cached for performance.
     72   uint32_t extinst_importid_GLSLstd450_ = 0;
     73 };
     74 
     75 }  // namespace opt
     76 }  // namespace spvtools
     77 
     78 #endif  // SOURCE_OPT_FEATURE_MANAGER_H_
     79