• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     AddExtension(&ext);
35   }
36 }
37 
AddExtension(Instruction * ext)38 void FeatureManager::AddExtension(Instruction* ext) {
39   assert(ext->opcode() == SpvOpExtension &&
40          "Expecting an extension instruction.");
41 
42   const std::string name =
43       reinterpret_cast<const char*>(ext->GetInOperand(0u).words.data());
44   Extension extension;
45   if (GetExtensionFromString(name.c_str(), &extension)) {
46     extensions_.Add(extension);
47   }
48 }
49 
AddCapability(SpvCapability cap)50 void FeatureManager::AddCapability(SpvCapability cap) {
51   if (capabilities_.Contains(cap)) return;
52 
53   capabilities_.Add(cap);
54 
55   spv_operand_desc desc = {};
56   if (SPV_SUCCESS ==
57       grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc)) {
58     CapabilitySet(desc->numCapabilities, desc->capabilities)
59         .ForEach([this](SpvCapability c) { AddCapability(c); });
60   }
61 }
62 
AddCapabilities(Module * module)63 void FeatureManager::AddCapabilities(Module* module) {
64   for (Instruction& inst : module->capabilities()) {
65     AddCapability(static_cast<SpvCapability>(inst.GetSingleWordInOperand(0)));
66   }
67 }
68 
AddExtInstImportIds(Module * module)69 void FeatureManager::AddExtInstImportIds(Module* module) {
70   extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
71 }
72 
operator ==(const FeatureManager & a,const FeatureManager & b)73 bool operator==(const FeatureManager& a, const FeatureManager& b) {
74   // We check that the addresses of the grammars are the same because they
75   // are large objects, and this is faster.  It can be changed if needed as a
76   // later time.
77   if (&a.grammar_ != &b.grammar_) {
78     return false;
79   }
80 
81   if (a.capabilities_ != b.capabilities_) {
82     return false;
83   }
84 
85   if (a.extensions_ != b.extensions_) {
86     return false;
87   }
88 
89   if (a.extinst_importid_GLSLstd450_ != b.extinst_importid_GLSLstd450_) {
90     return false;
91   }
92 
93   return true;
94 }
95 }  // namespace opt
96 }  // namespace spvtools
97