2017-11-30 22:03:06 +00:00
|
|
|
// Copyright (c) 2017 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/feature_manager.h"
|
|
|
|
|
2017-12-19 19:18:13 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <stack>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/enum_string_mapping.h"
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void FeatureManager::Analyze(Module* module) {
|
2017-12-19 19:18:13 +00:00
|
|
|
AddExtensions(module);
|
|
|
|
AddCapabilities(module);
|
2018-02-17 19:55:54 +00:00
|
|
|
AddExtInstImportIds(module);
|
2017-12-19 19:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void FeatureManager::AddExtensions(Module* module) {
|
2017-11-30 22:03:06 +00:00
|
|
|
for (auto ext : module->extensions()) {
|
2019-08-28 15:49:16 +00:00
|
|
|
AddExtension(&ext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureManager::AddExtension(Instruction* ext) {
|
|
|
|
assert(ext->opcode() == SpvOpExtension &&
|
|
|
|
"Expecting an extension instruction.");
|
|
|
|
|
2021-12-08 17:01:26 +00:00
|
|
|
const std::string name = ext->GetInOperand(0u).AsString();
|
2019-08-28 15:49:16 +00:00
|
|
|
Extension extension;
|
|
|
|
if (GetExtensionFromString(name.c_str(), &extension)) {
|
|
|
|
extensions_.Add(extension);
|
2017-11-30 22:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:19:52 +00:00
|
|
|
void FeatureManager::RemoveExtension(Extension ext) {
|
|
|
|
if (!extensions_.Contains(ext)) return;
|
|
|
|
extensions_.Remove(ext);
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:18:13 +00:00
|
|
|
void FeatureManager::AddCapability(SpvCapability cap) {
|
|
|
|
if (capabilities_.Contains(cap)) return;
|
|
|
|
|
|
|
|
capabilities_.Add(cap);
|
|
|
|
|
|
|
|
spv_operand_desc desc = {};
|
|
|
|
if (SPV_SUCCESS ==
|
|
|
|
grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc)) {
|
2018-07-07 13:38:00 +00:00
|
|
|
CapabilitySet(desc->numCapabilities, desc->capabilities)
|
2017-12-19 19:18:13 +00:00
|
|
|
.ForEach([this](SpvCapability c) { AddCapability(c); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:19:52 +00:00
|
|
|
void FeatureManager::RemoveCapability(SpvCapability cap) {
|
|
|
|
if (!capabilities_.Contains(cap)) return;
|
|
|
|
capabilities_.Remove(cap);
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void FeatureManager::AddCapabilities(Module* module) {
|
|
|
|
for (Instruction& inst : module->capabilities()) {
|
2017-12-19 19:18:13 +00:00
|
|
|
AddCapability(static_cast<SpvCapability>(inst.GetSingleWordInOperand(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void FeatureManager::AddExtInstImportIds(Module* module) {
|
2018-02-17 19:55:54 +00:00
|
|
|
extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
|
2020-04-13 13:29:36 +00:00
|
|
|
extinst_importid_OpenCL100DebugInfo_ =
|
|
|
|
module->GetExtInstImportId("OpenCL.DebugInfo.100");
|
2021-09-24 14:56:08 +00:00
|
|
|
extinst_importid_Shader100DebugInfo_ =
|
2021-09-15 18:38:53 +00:00
|
|
|
module->GetExtInstImportId("NonSemantic.Shader.DebugInfo.100");
|
2018-02-17 19:55:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 15:49:16 +00:00
|
|
|
bool operator==(const FeatureManager& a, const FeatureManager& b) {
|
|
|
|
// We check that the addresses of the grammars are the same because they
|
|
|
|
// are large objects, and this is faster. It can be changed if needed as a
|
|
|
|
// later time.
|
|
|
|
if (&a.grammar_ != &b.grammar_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.capabilities_ != b.capabilities_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.extensions_ != b.extensions_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.extinst_importid_GLSLstd450_ != b.extinst_importid_GLSLstd450_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-13 13:29:36 +00:00
|
|
|
if (a.extinst_importid_OpenCL100DebugInfo_ !=
|
|
|
|
b.extinst_importid_OpenCL100DebugInfo_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-24 14:56:08 +00:00
|
|
|
if (a.extinst_importid_Shader100DebugInfo_ !=
|
|
|
|
b.extinst_importid_Shader100DebugInfo_) {
|
2021-07-16 20:28:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:49:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-30 22:03:06 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|