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 #include "source/opt/feature_manager.h"
16
17 #include <string>
18
19 #include "source/enum_string_mapping.h"
20
21 namespace spvtools {
22 namespace opt {
23
Analyze(Module * module)24 void FeatureManager::Analyze(Module* module) {
25 AddExtensions(module);
26 AddCapabilities(module);
27 AddExtInstImportIds(module);
28 }
29
AddExtensions(Module * module)30 void FeatureManager::AddExtensions(Module* module) {
31 for (auto ext : module->extensions()) {
32 AddExtension(&ext);
33 }
34 }
35
AddExtension(Instruction * ext)36 void FeatureManager::AddExtension(Instruction* ext) {
37 assert(ext->opcode() == spv::Op::OpExtension &&
38 "Expecting an extension instruction.");
39
40 const std::string name = ext->GetInOperand(0u).AsString();
41 Extension extension;
42 if (GetExtensionFromString(name.c_str(), &extension)) {
43 extensions_.insert(extension);
44 }
45 }
46
RemoveExtension(Extension ext)47 void FeatureManager::RemoveExtension(Extension ext) {
48 if (!extensions_.contains(ext)) return;
49 extensions_.erase(ext);
50 }
51
AddCapability(spv::Capability cap)52 void FeatureManager::AddCapability(spv::Capability cap) {
53 if (capabilities_.contains(cap)) return;
54
55 capabilities_.insert(cap);
56
57 spv_operand_desc desc = {};
58 if (SPV_SUCCESS == grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
59 uint32_t(cap), &desc)) {
60 for (auto capability :
61 CapabilitySet(desc->numCapabilities, desc->capabilities)) {
62 AddCapability(capability);
63 }
64 }
65 }
66
RemoveCapability(spv::Capability cap)67 void FeatureManager::RemoveCapability(spv::Capability cap) {
68 if (!capabilities_.contains(cap)) return;
69 capabilities_.erase(cap);
70 }
71
AddCapabilities(Module * module)72 void FeatureManager::AddCapabilities(Module* module) {
73 for (Instruction& inst : module->capabilities()) {
74 AddCapability(static_cast<spv::Capability>(inst.GetSingleWordInOperand(0)));
75 }
76 }
77
AddExtInstImportIds(Module * module)78 void FeatureManager::AddExtInstImportIds(Module* module) {
79 extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
80 extinst_importid_OpenCL100DebugInfo_ =
81 module->GetExtInstImportId("OpenCL.DebugInfo.100");
82 extinst_importid_Shader100DebugInfo_ =
83 module->GetExtInstImportId("NonSemantic.Shader.DebugInfo.100");
84 }
85
operator ==(const FeatureManager & a,const FeatureManager & b)86 bool operator==(const FeatureManager& a, const FeatureManager& b) {
87 // We check that the addresses of the grammars are the same because they
88 // are large objects, and this is faster. It can be changed if needed as a
89 // later time.
90 if (&a.grammar_ != &b.grammar_) {
91 return false;
92 }
93
94 if (a.capabilities_ != b.capabilities_) {
95 return false;
96 }
97
98 if (a.extensions_ != b.extensions_) {
99 return false;
100 }
101
102 if (a.extinst_importid_GLSLstd450_ != b.extinst_importid_GLSLstd450_) {
103 return false;
104 }
105
106 if (a.extinst_importid_OpenCL100DebugInfo_ !=
107 b.extinst_importid_OpenCL100DebugInfo_) {
108 return false;
109 }
110
111 if (a.extinst_importid_Shader100DebugInfo_ !=
112 b.extinst_importid_Shader100DebugInfo_) {
113 return false;
114 }
115
116 return true;
117 }
118 } // namespace opt
119 } // namespace spvtools
120