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 <queue>
18 #include <stack>
19 #include <string>
20 
21 #include "source/enum_string_mapping.h"
22 
23 namespace spvtools {
24 namespace opt {
25 
Analyze(Module * module)26 void FeatureManager::Analyze(Module* module) {
27   AddExtensions(module);
28   AddCapabilities(module);
29   AddExtInstImportIds(module);
30 }
31 
AddExtensions(Module * module)32 void FeatureManager::AddExtensions(Module* module) {
33   for (auto ext : module->extensions()) {
34     const std::string name =
35         reinterpret_cast<const char*>(ext.GetInOperand(0u).words.data());
36     Extension extension;
37     if (GetExtensionFromString(name.c_str(), &extension)) {
38       extensions_.Add(extension);
39     }
40   }
41 }
42 
AddCapability(SpvCapability cap)43 void FeatureManager::AddCapability(SpvCapability cap) {
44   if (capabilities_.Contains(cap)) return;
45 
46   capabilities_.Add(cap);
47 
48   spv_operand_desc desc = {};
49   if (SPV_SUCCESS ==
50       grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc)) {
51     CapabilitySet(desc->numCapabilities, desc->capabilities)
52         .ForEach([this](SpvCapability c) { AddCapability(c); });
53   }
54 }
55 
AddCapabilities(Module * module)56 void FeatureManager::AddCapabilities(Module* module) {
57   for (Instruction& inst : module->capabilities()) {
58     AddCapability(static_cast<SpvCapability>(inst.GetSingleWordInOperand(0)));
59   }
60 }
61 
AddExtInstImportIds(Module * module)62 void FeatureManager::AddExtInstImportIds(Module* module) {
63   extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
64 }
65 
66 }  // namespace opt
67 }  // namespace spvtools
68