1fd4e5da5Sopenharmony_ci// Copyright (c) 2017 Google Inc.
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#ifndef SOURCE_OPT_FEATURE_MANAGER_H_
16fd4e5da5Sopenharmony_ci#define SOURCE_OPT_FEATURE_MANAGER_H_
17fd4e5da5Sopenharmony_ci
18fd4e5da5Sopenharmony_ci#include "source/assembly_grammar.h"
19fd4e5da5Sopenharmony_ci#include "source/extensions.h"
20fd4e5da5Sopenharmony_ci#include "source/opt/module.h"
21fd4e5da5Sopenharmony_ci
22fd4e5da5Sopenharmony_cinamespace spvtools {
23fd4e5da5Sopenharmony_cinamespace opt {
24fd4e5da5Sopenharmony_ci
25fd4e5da5Sopenharmony_ci// Tracks features enabled by a module. The IRContext has a FeatureManager.
26fd4e5da5Sopenharmony_ciclass FeatureManager {
27fd4e5da5Sopenharmony_ci public:
28fd4e5da5Sopenharmony_ci  // Returns true if |ext| is an enabled extension in the module.
29fd4e5da5Sopenharmony_ci  bool HasExtension(Extension ext) const { return extensions_.contains(ext); }
30fd4e5da5Sopenharmony_ci
31fd4e5da5Sopenharmony_ci  // Returns true if |cap| is an enabled capability in the module.
32fd4e5da5Sopenharmony_ci  bool HasCapability(spv::Capability cap) const {
33fd4e5da5Sopenharmony_ci    return capabilities_.contains(cap);
34fd4e5da5Sopenharmony_ci  }
35fd4e5da5Sopenharmony_ci
36fd4e5da5Sopenharmony_ci  // Returns the capabilities the module declares.
37fd4e5da5Sopenharmony_ci  inline const CapabilitySet& GetCapabilities() const { return capabilities_; }
38fd4e5da5Sopenharmony_ci
39fd4e5da5Sopenharmony_ci  // Returns the extensions the module imports.
40fd4e5da5Sopenharmony_ci  inline const ExtensionSet& GetExtensions() const { return extensions_; }
41fd4e5da5Sopenharmony_ci
42fd4e5da5Sopenharmony_ci  uint32_t GetExtInstImportId_GLSLstd450() const {
43fd4e5da5Sopenharmony_ci    return extinst_importid_GLSLstd450_;
44fd4e5da5Sopenharmony_ci  }
45fd4e5da5Sopenharmony_ci
46fd4e5da5Sopenharmony_ci  uint32_t GetExtInstImportId_OpenCL100DebugInfo() const {
47fd4e5da5Sopenharmony_ci    return extinst_importid_OpenCL100DebugInfo_;
48fd4e5da5Sopenharmony_ci  }
49fd4e5da5Sopenharmony_ci
50fd4e5da5Sopenharmony_ci  uint32_t GetExtInstImportId_Shader100DebugInfo() const {
51fd4e5da5Sopenharmony_ci    return extinst_importid_Shader100DebugInfo_;
52fd4e5da5Sopenharmony_ci  }
53fd4e5da5Sopenharmony_ci
54fd4e5da5Sopenharmony_ci  friend bool operator==(const FeatureManager& a, const FeatureManager& b);
55fd4e5da5Sopenharmony_ci  friend bool operator!=(const FeatureManager& a, const FeatureManager& b) {
56fd4e5da5Sopenharmony_ci    return !(a == b);
57fd4e5da5Sopenharmony_ci  }
58fd4e5da5Sopenharmony_ci
59fd4e5da5Sopenharmony_ci private:
60fd4e5da5Sopenharmony_ci  explicit FeatureManager(const AssemblyGrammar& grammar) : grammar_(grammar) {}
61fd4e5da5Sopenharmony_ci
62fd4e5da5Sopenharmony_ci  // Analyzes |module| and records enabled extensions and capabilities.
63fd4e5da5Sopenharmony_ci  void Analyze(Module* module);
64fd4e5da5Sopenharmony_ci
65fd4e5da5Sopenharmony_ci  // Add the extension |ext| to the feature manager.
66fd4e5da5Sopenharmony_ci  void AddExtension(Instruction* ext);
67fd4e5da5Sopenharmony_ci
68fd4e5da5Sopenharmony_ci  // Analyzes |module| and records enabled extensions.
69fd4e5da5Sopenharmony_ci  void AddExtensions(Module* module);
70fd4e5da5Sopenharmony_ci
71fd4e5da5Sopenharmony_ci  // Removes the given |extension| from the current FeatureManager.
72fd4e5da5Sopenharmony_ci  void RemoveExtension(Extension extension);
73fd4e5da5Sopenharmony_ci
74fd4e5da5Sopenharmony_ci  // Adds the given |capability| and all implied capabilities into the current
75fd4e5da5Sopenharmony_ci  // FeatureManager.
76fd4e5da5Sopenharmony_ci  void AddCapability(spv::Capability capability);
77fd4e5da5Sopenharmony_ci
78fd4e5da5Sopenharmony_ci  // Analyzes |module| and records enabled capabilities.
79fd4e5da5Sopenharmony_ci  void AddCapabilities(Module* module);
80fd4e5da5Sopenharmony_ci
81fd4e5da5Sopenharmony_ci  // Removes the given |capability| from the current FeatureManager.
82fd4e5da5Sopenharmony_ci  void RemoveCapability(spv::Capability capability);
83fd4e5da5Sopenharmony_ci
84fd4e5da5Sopenharmony_ci  // Analyzes |module| and records imported external instruction sets.
85fd4e5da5Sopenharmony_ci  void AddExtInstImportIds(Module* module);
86fd4e5da5Sopenharmony_ci
87fd4e5da5Sopenharmony_ci  // Auxiliary object for querying SPIR-V grammar facts.
88fd4e5da5Sopenharmony_ci  const AssemblyGrammar& grammar_;
89fd4e5da5Sopenharmony_ci
90fd4e5da5Sopenharmony_ci  // The enabled extensions.
91fd4e5da5Sopenharmony_ci  ExtensionSet extensions_;
92fd4e5da5Sopenharmony_ci
93fd4e5da5Sopenharmony_ci  // The enabled capabilities.
94fd4e5da5Sopenharmony_ci  CapabilitySet capabilities_;
95fd4e5da5Sopenharmony_ci
96fd4e5da5Sopenharmony_ci  // Common external instruction import ids, cached for performance.
97fd4e5da5Sopenharmony_ci  uint32_t extinst_importid_GLSLstd450_ = 0;
98fd4e5da5Sopenharmony_ci
99fd4e5da5Sopenharmony_ci  // Common OpenCL100DebugInfo external instruction import ids, cached
100fd4e5da5Sopenharmony_ci  // for performance.
101fd4e5da5Sopenharmony_ci  uint32_t extinst_importid_OpenCL100DebugInfo_ = 0;
102fd4e5da5Sopenharmony_ci
103fd4e5da5Sopenharmony_ci  // Common NonSemanticShader100DebugInfo external instruction import ids,
104fd4e5da5Sopenharmony_ci  // cached for performance.
105fd4e5da5Sopenharmony_ci  uint32_t extinst_importid_Shader100DebugInfo_ = 0;
106fd4e5da5Sopenharmony_ci
107fd4e5da5Sopenharmony_ci  friend class IRContext;
108fd4e5da5Sopenharmony_ci};
109fd4e5da5Sopenharmony_ci
110fd4e5da5Sopenharmony_ci}  // namespace opt
111fd4e5da5Sopenharmony_ci}  // namespace spvtools
112fd4e5da5Sopenharmony_ci
113fd4e5da5Sopenharmony_ci#endif  // SOURCE_OPT_FEATURE_MANAGER_H_
114