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
22namespace spvtools {
23namespace opt {
24
25// Tracks features enabled by a module. The IRContext has a FeatureManager.
26class 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 // 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.
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
47 CapabilitySet* GetCapabilities() { return &capabilities_; }
48 const CapabilitySet* GetCapabilities() const { return &capabilities_; }
49
50 uint32_t GetExtInstImportId_GLSLstd450() const {
51 return extinst_importid_GLSLstd450_;
52 }
53
54 friend bool operator==(const FeatureManager& a, const FeatureManager& b);
55 friend bool operator!=(const FeatureManager& a, const FeatureManager& b) {
56 return !(a == b);
57 }
58
59 // Adds the given |capability| and all implied capabilities into the current
60 // FeatureManager.
61 void AddCapability(SpvCapability capability);
62
63 // Add the extension |ext| to the feature manager.
64 void AddExtension(Instruction* ext);
65
66 // Analyzes |module| and records imported external instruction sets.
67 void AddExtInstImportIds(Module* module);
68
69 private:
70 // Analyzes |module| and records enabled extensions.
71 void AddExtensions(Module* module);
72
73 // Analyzes |module| and records enabled capabilities.
74 void AddCapabilities(Module* module);
75
76 // Auxiliary object for querying SPIR-V grammar facts.
77 const AssemblyGrammar& grammar_;
78
79 // The enabled extensions.
80 ExtensionSet extensions_;
81
82 // The enabled capabilities.
83 CapabilitySet capabilities_;
84
85 // Common external instruction import ids, cached for performance.
86 uint32_t extinst_importid_GLSLstd450_ = 0;
87};
88
89} // namespace opt
90} // namespace spvtools
91
92#endif // SOURCE_OPT_FEATURE_MANAGER_H_
93