2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-12-14 13:21:08 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2015-12-14 13:21:08 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-14 13:21:08 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2015-12-14 13:21:08 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/val/validation_state.h"
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2015-12-16 02:44:21 +00:00
|
|
|
#include <cassert>
|
2018-04-16 17:58:11 +00:00
|
|
|
#include <stack>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <utility>
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opcode.h"
|
2019-05-07 16:27:18 +00:00
|
|
|
#include "source/spirv_constant.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/spirv_target_env.h"
|
|
|
|
#include "source/val/basic_block.h"
|
|
|
|
#include "source/val/construct.h"
|
|
|
|
#include "source/val/function.h"
|
2018-08-01 14:37:36 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2015-12-15 19:50:05 +00:00
|
|
|
namespace {
|
2018-07-10 03:18:44 +00:00
|
|
|
|
2016-01-09 22:25:36 +00:00
|
|
|
bool IsInstructionInLayoutSection(ModuleLayoutSection layout, SpvOp op) {
|
2015-12-15 19:50:05 +00:00
|
|
|
// See Section 2.4
|
2016-01-09 22:25:36 +00:00
|
|
|
bool out = false;
|
2015-12-15 19:50:05 +00:00
|
|
|
// clang-format off
|
2016-01-09 22:25:36 +00:00
|
|
|
switch (layout) {
|
|
|
|
case kLayoutCapabilities: out = op == SpvOpCapability; break;
|
|
|
|
case kLayoutExtensions: out = op == SpvOpExtension; break;
|
|
|
|
case kLayoutExtInstImport: out = op == SpvOpExtInstImport; break;
|
|
|
|
case kLayoutMemoryModel: out = op == SpvOpMemoryModel; break;
|
|
|
|
case kLayoutEntryPoint: out = op == SpvOpEntryPoint; break;
|
2018-05-16 12:43:50 +00:00
|
|
|
case kLayoutExecutionMode:
|
|
|
|
out = op == SpvOpExecutionMode || op == SpvOpExecutionModeId;
|
|
|
|
break;
|
2016-01-09 22:25:36 +00:00
|
|
|
case kLayoutDebug1:
|
|
|
|
switch (op) {
|
|
|
|
case SpvOpSourceContinued:
|
|
|
|
case SpvOpSource:
|
|
|
|
case SpvOpSourceExtension:
|
|
|
|
case SpvOpString:
|
|
|
|
out = true;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kLayoutDebug2:
|
|
|
|
switch (op) {
|
|
|
|
case SpvOpName:
|
|
|
|
case SpvOpMemberName:
|
|
|
|
out = true;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2017-09-07 21:38:31 +00:00
|
|
|
case kLayoutDebug3:
|
|
|
|
// Only OpModuleProcessed is allowed here.
|
|
|
|
out = (op == SpvOpModuleProcessed);
|
|
|
|
break;
|
2016-01-09 22:25:36 +00:00
|
|
|
case kLayoutAnnotations:
|
|
|
|
switch (op) {
|
|
|
|
case SpvOpDecorate:
|
|
|
|
case SpvOpMemberDecorate:
|
|
|
|
case SpvOpGroupDecorate:
|
|
|
|
case SpvOpGroupMemberDecorate:
|
|
|
|
case SpvOpDecorationGroup:
|
2018-03-05 18:34:13 +00:00
|
|
|
case SpvOpDecorateId:
|
|
|
|
case SpvOpDecorateStringGOOGLE:
|
|
|
|
case SpvOpMemberDecorateStringGOOGLE:
|
2016-01-09 22:25:36 +00:00
|
|
|
out = true;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kLayoutTypes:
|
2017-02-28 16:53:47 +00:00
|
|
|
if (spvOpcodeGeneratesType(op) || spvOpcodeIsConstant(op)) {
|
|
|
|
out = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-01-09 22:25:36 +00:00
|
|
|
switch (op) {
|
|
|
|
case SpvOpTypeForwardPointer:
|
|
|
|
case SpvOpVariable:
|
|
|
|
case SpvOpLine:
|
2016-01-18 18:41:52 +00:00
|
|
|
case SpvOpNoLine:
|
2016-08-12 18:28:17 +00:00
|
|
|
case SpvOpUndef:
|
2019-12-18 23:10:29 +00:00
|
|
|
// SpvOpExtInst is only allowed here for certain extended instruction
|
|
|
|
// sets. This will be checked separately
|
|
|
|
case SpvOpExtInst:
|
2016-01-09 22:25:36 +00:00
|
|
|
out = true;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kLayoutFunctionDeclarations:
|
|
|
|
case kLayoutFunctionDefinitions:
|
|
|
|
// NOTE: These instructions should NOT be in these layout sections
|
2017-02-28 16:53:47 +00:00
|
|
|
if (spvOpcodeGeneratesType(op) || spvOpcodeIsConstant(op)) {
|
|
|
|
out = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-01-09 22:25:36 +00:00
|
|
|
switch (op) {
|
|
|
|
case SpvOpCapability:
|
|
|
|
case SpvOpExtension:
|
|
|
|
case SpvOpExtInstImport:
|
|
|
|
case SpvOpMemoryModel:
|
|
|
|
case SpvOpEntryPoint:
|
|
|
|
case SpvOpExecutionMode:
|
2018-05-16 12:43:50 +00:00
|
|
|
case SpvOpExecutionModeId:
|
2016-01-09 22:25:36 +00:00
|
|
|
case SpvOpSourceContinued:
|
|
|
|
case SpvOpSource:
|
|
|
|
case SpvOpSourceExtension:
|
|
|
|
case SpvOpString:
|
|
|
|
case SpvOpName:
|
|
|
|
case SpvOpMemberName:
|
2017-09-07 21:38:31 +00:00
|
|
|
case SpvOpModuleProcessed:
|
2016-01-09 22:25:36 +00:00
|
|
|
case SpvOpDecorate:
|
|
|
|
case SpvOpMemberDecorate:
|
|
|
|
case SpvOpGroupDecorate:
|
|
|
|
case SpvOpGroupMemberDecorate:
|
|
|
|
case SpvOpDecorationGroup:
|
|
|
|
case SpvOpTypeForwardPointer:
|
|
|
|
out = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
out = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 19:50:05 +00:00
|
|
|
// clang-format on
|
2016-01-09 22:25:36 +00:00
|
|
|
return out;
|
2015-12-15 19:50:05 +00:00
|
|
|
}
|
2016-01-23 19:14:32 +00:00
|
|
|
|
2018-08-01 14:37:36 +00:00
|
|
|
// Counts the number of instructions and functions in the file.
|
|
|
|
spv_result_t CountInstructions(void* user_data,
|
|
|
|
const spv_parsed_instruction_t* inst) {
|
|
|
|
ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
|
|
|
|
if (inst->opcode == SpvOpFunction) _.increment_total_functions();
|
|
|
|
_.increment_total_instructions();
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
spv_result_t setHeader(void* user_data, spv_endianness_t, uint32_t,
|
|
|
|
uint32_t version, uint32_t generator, uint32_t id_bound,
|
|
|
|
uint32_t) {
|
|
|
|
ValidationState_t& vstate =
|
|
|
|
*(reinterpret_cast<ValidationState_t*>(user_data));
|
|
|
|
vstate.setIdBound(id_bound);
|
|
|
|
vstate.setGenerator(generator);
|
|
|
|
vstate.setVersion(version);
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add features based on SPIR-V core version number.
|
|
|
|
void UpdateFeaturesBasedOnSpirvVersion(ValidationState_t::Feature* features,
|
|
|
|
uint32_t version) {
|
|
|
|
assert(features);
|
|
|
|
if (version >= SPV_SPIRV_VERSION_WORD(1, 4)) {
|
|
|
|
features->select_between_composites = true;
|
|
|
|
features->copy_memory_permits_two_memory_accesses = true;
|
|
|
|
features->uconvert_spec_constant_op = true;
|
|
|
|
features->nonwritable_var_in_function_or_private = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
} // namespace
|
2015-12-14 13:21:08 +00:00
|
|
|
|
2017-02-15 18:29:33 +00:00
|
|
|
ValidationState_t::ValidationState_t(const spv_const_context ctx,
|
2018-06-19 20:02:44 +00:00
|
|
|
const spv_const_validator_options opt,
|
|
|
|
const uint32_t* words,
|
2018-10-03 19:59:40 +00:00
|
|
|
const size_t num_words,
|
|
|
|
const uint32_t max_warnings)
|
2016-09-02 22:06:18 +00:00
|
|
|
: context_(ctx),
|
2017-02-15 18:29:33 +00:00
|
|
|
options_(opt),
|
2018-06-19 20:02:44 +00:00
|
|
|
words_(words),
|
|
|
|
num_words_(num_words),
|
2015-12-15 19:50:05 +00:00
|
|
|
unresolved_forward_ids_{},
|
|
|
|
operand_names_{},
|
2016-01-15 16:25:11 +00:00
|
|
|
current_layout_section_(kLayoutCapabilities),
|
2016-03-16 21:20:02 +00:00
|
|
|
module_functions_(),
|
2016-08-27 18:49:53 +00:00
|
|
|
module_capabilities_(),
|
2017-03-08 18:59:01 +00:00
|
|
|
module_extensions_(),
|
2016-08-06 17:29:33 +00:00
|
|
|
ordered_instructions_(),
|
|
|
|
all_definitions_(),
|
2017-01-13 16:46:21 +00:00
|
|
|
global_vars_(),
|
|
|
|
local_vars_(),
|
2016-12-04 15:48:26 +00:00
|
|
|
struct_nesting_depth_(),
|
2019-02-25 15:37:43 +00:00
|
|
|
struct_has_nested_blockorbufferblock_struct_(),
|
2016-09-02 22:06:18 +00:00
|
|
|
grammar_(ctx),
|
2018-05-22 17:09:50 +00:00
|
|
|
addressing_model_(SpvAddressingModelMax),
|
|
|
|
memory_model_(SpvMemoryModelMax),
|
2019-01-07 18:19:24 +00:00
|
|
|
pointer_size_and_alignment_(0),
|
2018-10-03 19:59:40 +00:00
|
|
|
in_function_(false),
|
|
|
|
num_of_warnings_(0),
|
|
|
|
max_num_of_warnings_(max_warnings) {
|
2017-02-15 18:29:33 +00:00
|
|
|
assert(opt && "Validator options may not be Null.");
|
2018-06-20 17:29:38 +00:00
|
|
|
|
2018-07-10 21:01:06 +00:00
|
|
|
const auto env = context_->target_env;
|
|
|
|
|
|
|
|
if (spvIsVulkanEnv(env)) {
|
|
|
|
// Vulkan 1.1 includes VK_KHR_relaxed_block_layout in core.
|
|
|
|
if (env != SPV_ENV_VULKAN_1_0) {
|
|
|
|
features_.env_relaxed_block_layout = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:37:36 +00:00
|
|
|
// Only attempt to count if we have words, otherwise let the other validation
|
|
|
|
// fail and generate an error.
|
|
|
|
if (num_words > 0) {
|
|
|
|
// Count the number of instructions in the binary.
|
2018-11-26 21:58:09 +00:00
|
|
|
// This parse should not produce any error messages. Hijack the context and
|
|
|
|
// replace the message consumer so that we do not pollute any state in input
|
|
|
|
// consumer.
|
|
|
|
spv_context_t hijacked_context = *ctx;
|
|
|
|
hijacked_context.consumer = [](spv_message_level_t, const char*,
|
|
|
|
const spv_position_t&, const char*) {};
|
2019-05-07 16:27:18 +00:00
|
|
|
spvBinaryParse(&hijacked_context, this, words, num_words, setHeader,
|
|
|
|
CountInstructions,
|
2018-08-01 14:37:36 +00:00
|
|
|
/* diagnostic = */ nullptr);
|
|
|
|
preallocateStorage();
|
|
|
|
}
|
2019-05-07 16:27:18 +00:00
|
|
|
UpdateFeaturesBasedOnSpirvVersion(&features_, version_);
|
2018-12-03 22:01:30 +00:00
|
|
|
|
|
|
|
friendly_mapper_ = spvtools::MakeUnique<spvtools::FriendlyNameMapper>(
|
|
|
|
context_, words_, num_words_);
|
|
|
|
name_mapper_ = friendly_mapper_->GetNameMapper();
|
2018-08-01 14:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ValidationState_t::preallocateStorage() {
|
|
|
|
ordered_instructions_.reserve(total_instructions_);
|
|
|
|
module_functions_.reserve(total_functions_);
|
2017-02-15 18:29:33 +00:00
|
|
|
}
|
2015-12-14 13:21:08 +00:00
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
spv_result_t ValidationState_t::ForwardDeclareId(uint32_t id) {
|
2015-12-14 13:21:08 +00:00
|
|
|
unresolved_forward_ids_.insert(id);
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
spv_result_t ValidationState_t::RemoveIfForwardDeclared(uint32_t id) {
|
2015-12-14 13:21:08 +00:00
|
|
|
unresolved_forward_ids_.erase(id);
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-11-10 20:12:26 +00:00
|
|
|
spv_result_t ValidationState_t::RegisterForwardPointer(uint32_t id) {
|
|
|
|
forward_pointer_ids_.insert(id);
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsForwardPointer(uint32_t id) const {
|
|
|
|
return (forward_pointer_ids_.find(id) != forward_pointer_ids_.end());
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:37:36 +00:00
|
|
|
void ValidationState_t::AssignNameToId(uint32_t id, std::string name) {
|
2015-12-14 13:21:08 +00:00
|
|
|
operand_names_[id] = name;
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:37:36 +00:00
|
|
|
std::string ValidationState_t::getIdName(uint32_t id) const {
|
2018-12-03 22:01:30 +00:00
|
|
|
const std::string id_name = name_mapper_(id);
|
2015-12-14 13:21:08 +00:00
|
|
|
|
2016-03-16 21:20:02 +00:00
|
|
|
std::stringstream out;
|
2018-12-03 22:01:30 +00:00
|
|
|
out << id << "[%" << id_name << "]";
|
2016-03-16 21:20:02 +00:00
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
size_t ValidationState_t::unresolved_forward_id_count() const {
|
2015-12-14 13:21:08 +00:00
|
|
|
return unresolved_forward_ids_.size();
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:37:36 +00:00
|
|
|
std::vector<uint32_t> ValidationState_t::UnresolvedForwardIds() const {
|
|
|
|
std::vector<uint32_t> out(std::begin(unresolved_forward_ids_),
|
|
|
|
std::end(unresolved_forward_ids_));
|
2015-12-14 13:21:08 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
bool ValidationState_t::IsDefinedId(uint32_t id) const {
|
2018-08-01 18:58:12 +00:00
|
|
|
return all_definitions_.find(id) != std::end(all_definitions_);
|
2016-07-08 13:44:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 17:29:33 +00:00
|
|
|
const Instruction* ValidationState_t::FindDef(uint32_t id) const {
|
2017-06-27 21:21:33 +00:00
|
|
|
auto it = all_definitions_.find(id);
|
2017-10-25 16:15:51 +00:00
|
|
|
if (it == all_definitions_.end()) return nullptr;
|
2017-06-27 21:21:33 +00:00
|
|
|
return it->second;
|
2015-12-14 13:21:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 17:29:33 +00:00
|
|
|
Instruction* ValidationState_t::FindDef(uint32_t id) {
|
2017-06-27 21:21:33 +00:00
|
|
|
auto it = all_definitions_.find(id);
|
2017-10-25 16:15:51 +00:00
|
|
|
if (it == all_definitions_.end()) return nullptr;
|
2017-06-27 21:21:33 +00:00
|
|
|
return it->second;
|
2016-08-06 17:29:33 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
ModuleLayoutSection ValidationState_t::current_layout_section() const {
|
2016-01-15 16:25:11 +00:00
|
|
|
return current_layout_section_;
|
2015-12-15 19:50:05 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
void ValidationState_t::ProgressToNextLayoutSectionOrder() {
|
2016-01-09 22:25:36 +00:00
|
|
|
// Guard against going past the last element(kLayoutFunctionDefinitions)
|
2016-01-15 16:25:11 +00:00
|
|
|
if (current_layout_section_ <= kLayoutFunctionDefinitions) {
|
|
|
|
current_layout_section_ =
|
|
|
|
static_cast<ModuleLayoutSection>(current_layout_section_ + 1);
|
2015-12-15 19:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
bool ValidationState_t::IsOpcodeInCurrentLayoutSection(SpvOp op) {
|
2016-01-15 16:25:11 +00:00
|
|
|
return IsInstructionInLayoutSection(current_layout_section_, op);
|
2015-12-15 19:50:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 18:19:34 +00:00
|
|
|
DiagnosticStream ValidationState_t::diag(spv_result_t error_code,
|
2018-10-03 19:59:40 +00:00
|
|
|
const Instruction* inst) {
|
|
|
|
if (error_code == SPV_WARNING) {
|
|
|
|
if (num_of_warnings_ == max_num_of_warnings_) {
|
|
|
|
DiagnosticStream({0, 0, 0}, context_->consumer, "", error_code)
|
|
|
|
<< "Other warnings have been suppressed.\n";
|
|
|
|
}
|
|
|
|
if (num_of_warnings_ >= max_num_of_warnings_) {
|
|
|
|
return DiagnosticStream({0, 0, 0}, nullptr, "", error_code);
|
|
|
|
}
|
|
|
|
++num_of_warnings_;
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:02:44 +00:00
|
|
|
std::string disassembly;
|
2018-08-01 20:12:07 +00:00
|
|
|
if (inst) disassembly = Disassemble(*inst);
|
|
|
|
|
|
|
|
return DiagnosticStream({0, 0, inst ? inst->LineNum() : 0},
|
|
|
|
context_->consumer, disassembly, error_code);
|
2015-12-14 13:21:08 +00:00
|
|
|
}
|
2015-12-16 02:44:21 +00:00
|
|
|
|
2018-08-01 14:37:36 +00:00
|
|
|
std::vector<Function>& ValidationState_t::functions() {
|
|
|
|
return module_functions_;
|
|
|
|
}
|
2015-12-16 02:44:21 +00:00
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
Function& ValidationState_t::current_function() {
|
2016-03-16 21:20:02 +00:00
|
|
|
assert(in_function_body());
|
|
|
|
return module_functions_.back();
|
2015-12-16 02:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 22:13:21 +00:00
|
|
|
const Function& ValidationState_t::current_function() const {
|
|
|
|
assert(in_function_body());
|
|
|
|
return module_functions_.back();
|
|
|
|
}
|
|
|
|
|
2017-11-23 17:11:15 +00:00
|
|
|
const Function* ValidationState_t::function(uint32_t id) const {
|
|
|
|
const auto it = id_to_function_.find(id);
|
2017-11-27 15:16:41 +00:00
|
|
|
if (it == id_to_function_.end()) return nullptr;
|
2017-11-23 17:11:15 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-08-03 16:57:11 +00:00
|
|
|
Function* ValidationState_t::function(uint32_t id) {
|
|
|
|
auto it = id_to_function_.find(id);
|
|
|
|
if (it == id_to_function_.end()) return nullptr;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2016-03-16 21:20:02 +00:00
|
|
|
bool ValidationState_t::in_function_body() const { return in_function_; }
|
|
|
|
|
2015-12-16 02:44:21 +00:00
|
|
|
bool ValidationState_t::in_block() const {
|
2016-06-09 19:51:39 +00:00
|
|
|
return module_functions_.empty() == false &&
|
2016-06-25 03:45:25 +00:00
|
|
|
module_functions_.back().current_block() != nullptr;
|
2015-12-16 02:44:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 21:20:02 +00:00
|
|
|
void ValidationState_t::RegisterCapability(SpvCapability cap) {
|
2016-08-29 18:49:00 +00:00
|
|
|
// Avoid redundant work. Otherwise the recursion could induce work
|
|
|
|
// quadrdatic in the capability dependency depth. (Ok, not much, but
|
|
|
|
// it's something.)
|
|
|
|
if (module_capabilities_.Contains(cap)) return;
|
|
|
|
|
|
|
|
module_capabilities_.Add(cap);
|
2016-01-31 04:32:09 +00:00
|
|
|
spv_operand_desc desc;
|
|
|
|
if (SPV_SUCCESS ==
|
2016-08-29 18:49:00 +00:00
|
|
|
grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc)) {
|
2017-10-25 16:15:51 +00:00
|
|
|
CapabilitySet(desc->numCapabilities, desc->capabilities)
|
|
|
|
.ForEach([this](SpvCapability c) { RegisterCapability(c); });
|
2016-08-29 18:49:00 +00:00
|
|
|
}
|
2017-02-22 22:53:08 +00:00
|
|
|
|
|
|
|
switch (cap) {
|
2017-11-24 19:18:17 +00:00
|
|
|
case SpvCapabilityKernel:
|
|
|
|
features_.group_ops_reduce_and_scans = true;
|
|
|
|
break;
|
2018-06-01 19:19:11 +00:00
|
|
|
case SpvCapabilityInt8:
|
2018-10-19 17:45:26 +00:00
|
|
|
features_.use_int8_type = true;
|
|
|
|
features_.declare_int8_type = true;
|
|
|
|
break;
|
2018-06-01 19:19:11 +00:00
|
|
|
case SpvCapabilityStorageBuffer8BitAccess:
|
|
|
|
case SpvCapabilityUniformAndStorageBuffer8BitAccess:
|
|
|
|
case SpvCapabilityStoragePushConstant8:
|
|
|
|
features_.declare_int8_type = true;
|
|
|
|
break;
|
2017-02-22 22:53:08 +00:00
|
|
|
case SpvCapabilityInt16:
|
|
|
|
features_.declare_int16_type = true;
|
|
|
|
break;
|
|
|
|
case SpvCapabilityFloat16:
|
|
|
|
case SpvCapabilityFloat16Buffer:
|
|
|
|
features_.declare_float16_type = true;
|
|
|
|
break;
|
2017-02-22 23:10:05 +00:00
|
|
|
case SpvCapabilityStorageUniformBufferBlock16:
|
|
|
|
case SpvCapabilityStorageUniform16:
|
|
|
|
case SpvCapabilityStoragePushConstant16:
|
|
|
|
case SpvCapabilityStorageInputOutput16:
|
|
|
|
features_.declare_int16_type = true;
|
|
|
|
features_.declare_float16_type = true;
|
2017-03-01 20:51:44 +00:00
|
|
|
features_.free_fp_rounding_mode = true;
|
2017-02-01 20:37:39 +00:00
|
|
|
break;
|
|
|
|
case SpvCapabilityVariablePointers:
|
|
|
|
features_.variable_pointers = true;
|
2017-03-26 01:12:22 +00:00
|
|
|
features_.variable_pointers_storage_buffer = true;
|
2017-02-01 20:37:39 +00:00
|
|
|
break;
|
2017-03-26 01:12:22 +00:00
|
|
|
case SpvCapabilityVariablePointersStorageBuffer:
|
|
|
|
features_.variable_pointers_storage_buffer = true;
|
2017-02-01 20:37:39 +00:00
|
|
|
break;
|
2017-02-22 22:53:08 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-01-23 19:14:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:59:01 +00:00
|
|
|
void ValidationState_t::RegisterExtension(Extension ext) {
|
|
|
|
if (module_extensions_.Contains(ext)) return;
|
|
|
|
|
|
|
|
module_extensions_.Add(ext);
|
2017-11-24 19:18:17 +00:00
|
|
|
|
|
|
|
switch (ext) {
|
2018-03-06 20:44:05 +00:00
|
|
|
case kSPV_AMD_gpu_shader_half_float:
|
2018-09-20 17:50:34 +00:00
|
|
|
case kSPV_AMD_gpu_shader_half_float_fetch:
|
2018-03-06 20:44:05 +00:00
|
|
|
// SPV_AMD_gpu_shader_half_float enables float16 type.
|
|
|
|
// https://github.com/KhronosGroup/SPIRV-Tools/issues/1375
|
|
|
|
features_.declare_float16_type = true;
|
|
|
|
break;
|
2019-01-09 00:00:18 +00:00
|
|
|
case kSPV_AMD_gpu_shader_int16:
|
|
|
|
// This is not yet in the extension, but it's recommended for it.
|
|
|
|
// See https://github.com/KhronosGroup/glslang/issues/848
|
|
|
|
features_.uconvert_spec_constant_op = true;
|
|
|
|
break;
|
2017-11-24 19:18:17 +00:00
|
|
|
case kSPV_AMD_shader_ballot:
|
|
|
|
// The grammar doesn't encode the fact that SPV_AMD_shader_ballot
|
|
|
|
// enables the use of group operations Reduce, InclusiveScan,
|
|
|
|
// and ExclusiveScan. Enable it manually.
|
|
|
|
// https://github.com/KhronosGroup/SPIRV-Tools/issues/991
|
|
|
|
features_.group_ops_reduce_and_scans = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-03-08 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 20:58:15 +00:00
|
|
|
bool ValidationState_t::HasAnyOfCapabilities(
|
|
|
|
const CapabilitySet& capabilities) const {
|
|
|
|
return module_capabilities_.HasAnyOf(capabilities);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::HasAnyOfExtensions(
|
|
|
|
const ExtensionSet& extensions) const {
|
|
|
|
return module_extensions_.HasAnyOf(extensions);
|
2016-01-31 04:32:09 +00:00
|
|
|
}
|
2016-03-16 21:20:02 +00:00
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
void ValidationState_t::set_addressing_model(SpvAddressingModel am) {
|
2016-03-02 21:17:54 +00:00
|
|
|
addressing_model_ = am;
|
2019-01-07 18:19:24 +00:00
|
|
|
switch (am) {
|
|
|
|
case SpvAddressingModelPhysical32:
|
|
|
|
pointer_size_and_alignment_ = 4;
|
|
|
|
break;
|
|
|
|
default:
|
2019-05-07 16:27:18 +00:00
|
|
|
// fall through
|
2019-01-07 18:19:24 +00:00
|
|
|
case SpvAddressingModelPhysical64:
|
|
|
|
case SpvAddressingModelPhysicalStorageBuffer64EXT:
|
|
|
|
pointer_size_and_alignment_ = 8;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-02 21:17:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
SpvAddressingModel ValidationState_t::addressing_model() const {
|
2016-03-02 21:17:54 +00:00
|
|
|
return addressing_model_;
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
void ValidationState_t::set_memory_model(SpvMemoryModel mm) {
|
2016-03-02 21:17:54 +00:00
|
|
|
memory_model_ = mm;
|
|
|
|
}
|
|
|
|
|
2016-07-08 13:44:10 +00:00
|
|
|
SpvMemoryModel ValidationState_t::memory_model() const { return memory_model_; }
|
2016-01-31 04:32:09 +00:00
|
|
|
|
2016-03-16 21:20:02 +00:00
|
|
|
spv_result_t ValidationState_t::RegisterFunction(
|
|
|
|
uint32_t id, uint32_t ret_type_id, SpvFunctionControlMask function_control,
|
|
|
|
uint32_t function_type_id) {
|
|
|
|
assert(in_function_body() == false &&
|
|
|
|
"RegisterFunction can only be called when parsing the binary outside "
|
|
|
|
"of another function");
|
2015-12-16 02:44:21 +00:00
|
|
|
in_function_ = true;
|
2016-03-16 21:20:02 +00:00
|
|
|
module_functions_.emplace_back(id, ret_type_id, function_control,
|
2016-07-08 13:44:10 +00:00
|
|
|
function_type_id);
|
2017-11-23 17:11:15 +00:00
|
|
|
id_to_function_.emplace(id, ¤t_function());
|
2015-12-16 02:44:21 +00:00
|
|
|
|
|
|
|
// TODO(umar): validate function type and type_id
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-03-16 21:20:02 +00:00
|
|
|
spv_result_t ValidationState_t::RegisterFunctionEnd() {
|
|
|
|
assert(in_function_body() == true &&
|
|
|
|
"RegisterFunctionEnd can only be called when parsing the binary "
|
|
|
|
"inside of another function");
|
|
|
|
assert(in_block() == false &&
|
|
|
|
"RegisterFunctionParameter can only be called when parsing the binary "
|
|
|
|
"ouside of a block");
|
2016-06-25 03:45:25 +00:00
|
|
|
current_function().RegisterFunctionEnd();
|
2016-03-16 21:20:02 +00:00
|
|
|
in_function_ = false;
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-31 13:54:25 +00:00
|
|
|
Instruction* ValidationState_t::AddOrderedInstruction(
|
|
|
|
const spv_parsed_instruction_t* inst) {
|
2018-08-02 19:12:06 +00:00
|
|
|
ordered_instructions_.emplace_back(inst);
|
2018-08-01 20:12:07 +00:00
|
|
|
ordered_instructions_.back().SetLineNum(ordered_instructions_.size());
|
2018-07-31 13:54:25 +00:00
|
|
|
return &ordered_instructions_.back();
|
|
|
|
}
|
2018-06-19 20:02:44 +00:00
|
|
|
|
2018-07-31 13:54:25 +00:00
|
|
|
// Improves diagnostic messages by collecting names of IDs
|
|
|
|
void ValidationState_t::RegisterDebugInstruction(const Instruction* inst) {
|
|
|
|
switch (inst->opcode()) {
|
|
|
|
case SpvOpName: {
|
|
|
|
const auto target = inst->GetOperandAs<uint32_t>(0);
|
|
|
|
const auto* str = reinterpret_cast<const char*>(inst->words().data() +
|
|
|
|
inst->operand(1).offset);
|
|
|
|
AssignNameToId(target, str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SpvOpMemberName: {
|
|
|
|
const auto target = inst->GetOperandAs<uint32_t>(0);
|
|
|
|
const auto* str = reinterpret_cast<const char*>(inst->words().data() +
|
|
|
|
inst->operand(2).offset);
|
|
|
|
AssignNameToId(target, str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SpvOpSourceContinued:
|
|
|
|
case SpvOpSource:
|
|
|
|
case SpvOpSourceExtension:
|
|
|
|
case SpvOpString:
|
|
|
|
case SpvOpLine:
|
|
|
|
case SpvOpNoLine:
|
|
|
|
default:
|
|
|
|
break;
|
2016-07-08 13:44:10 +00:00
|
|
|
}
|
2018-07-31 13:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ValidationState_t::RegisterInstruction(Instruction* inst) {
|
2018-08-01 14:37:36 +00:00
|
|
|
if (inst->id()) all_definitions_.insert(std::make_pair(inst->id(), inst));
|
2016-11-22 23:06:55 +00:00
|
|
|
|
|
|
|
// If the instruction is using an OpTypeSampledImage as an operand, it should
|
|
|
|
// be recorded. The validator will ensure that all usages of an
|
|
|
|
// OpTypeSampledImage and its definition are in the same basic block.
|
2018-07-31 13:54:25 +00:00
|
|
|
for (uint16_t i = 0; i < inst->operands().size(); ++i) {
|
|
|
|
const spv_parsed_operand_t& operand = inst->operand(i);
|
2016-11-22 23:06:55 +00:00
|
|
|
if (SPV_OPERAND_TYPE_ID == operand.type) {
|
2018-07-31 13:54:25 +00:00
|
|
|
const uint32_t operand_word = inst->word(operand.offset);
|
2016-11-22 23:06:55 +00:00
|
|
|
Instruction* operand_inst = FindDef(operand_word);
|
|
|
|
if (operand_inst && SpvOpSampledImage == operand_inst->opcode()) {
|
2019-03-19 16:39:37 +00:00
|
|
|
RegisterSampledImageConsumer(operand_word, inst);
|
2016-11-22 23:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-08 13:44:10 +00:00
|
|
|
}
|
2016-11-22 23:06:55 +00:00
|
|
|
|
2019-03-19 16:39:37 +00:00
|
|
|
std::vector<Instruction*> ValidationState_t::getSampledImageConsumers(
|
2016-11-22 23:06:55 +00:00
|
|
|
uint32_t sampled_image_id) const {
|
2019-03-19 16:39:37 +00:00
|
|
|
std::vector<Instruction*> result;
|
2016-11-22 23:06:55 +00:00
|
|
|
auto iter = sampled_image_consumers_.find(sampled_image_id);
|
|
|
|
if (iter != sampled_image_consumers_.end()) {
|
|
|
|
result = iter->second;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValidationState_t::RegisterSampledImageConsumer(uint32_t sampled_image_id,
|
2019-03-19 16:39:37 +00:00
|
|
|
Instruction* consumer) {
|
|
|
|
sampled_image_consumers_[sampled_image_id].push_back(consumer);
|
2016-11-22 23:06:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 20:37:22 +00:00
|
|
|
uint32_t ValidationState_t::getIdBound() const { return id_bound_; }
|
|
|
|
|
|
|
|
void ValidationState_t::setIdBound(const uint32_t bound) { id_bound_ = bound; }
|
2017-02-23 21:07:52 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
bool ValidationState_t::RegisterUniqueTypeDeclaration(const Instruction* inst) {
|
2017-02-23 21:07:52 +00:00
|
|
|
std::vector<uint32_t> key;
|
2018-07-10 14:57:52 +00:00
|
|
|
key.push_back(static_cast<uint32_t>(inst->opcode()));
|
|
|
|
for (size_t index = 0; index < inst->operands().size(); ++index) {
|
|
|
|
const spv_parsed_operand_t& operand = inst->operand(index);
|
2017-02-23 21:07:52 +00:00
|
|
|
|
|
|
|
if (operand.type == SPV_OPERAND_TYPE_RESULT_ID) continue;
|
|
|
|
|
|
|
|
const int words_begin = operand.offset;
|
|
|
|
const int words_end = words_begin + operand.num_words;
|
2018-07-10 14:57:52 +00:00
|
|
|
assert(words_end <= static_cast<int>(inst->words().size()));
|
2017-02-23 21:07:52 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
key.insert(key.end(), inst->words().begin() + words_begin,
|
|
|
|
inst->words().begin() + words_end);
|
2017-02-23 21:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return unique_type_declarations_.insert(std::move(key)).second;
|
|
|
|
}
|
2017-08-30 14:13:10 +00:00
|
|
|
|
|
|
|
uint32_t ValidationState_t::GetTypeId(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
2017-10-26 19:30:23 +00:00
|
|
|
return inst ? inst->type_id() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpvOp ValidationState_t::GetIdOpcode(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
return inst ? inst->opcode() : SpvOpNop;
|
2017-08-30 14:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ValidationState_t::GetComponentType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
switch (inst->opcode()) {
|
|
|
|
case SpvOpTypeFloat:
|
|
|
|
case SpvOpTypeInt:
|
|
|
|
case SpvOpTypeBool:
|
|
|
|
return id;
|
|
|
|
|
|
|
|
case SpvOpTypeVector:
|
|
|
|
return inst->word(2);
|
|
|
|
|
|
|
|
case SpvOpTypeMatrix:
|
|
|
|
return GetComponentType(inst->word(2));
|
|
|
|
|
2019-02-25 22:43:11 +00:00
|
|
|
case SpvOpTypeCooperativeMatrixNV:
|
|
|
|
return inst->word(2);
|
|
|
|
|
2017-08-30 14:13:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-25 16:15:51 +00:00
|
|
|
if (inst->type_id()) return GetComponentType(inst->type_id());
|
2017-08-30 14:13:10 +00:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ValidationState_t::GetDimension(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
switch (inst->opcode()) {
|
|
|
|
case SpvOpTypeFloat:
|
|
|
|
case SpvOpTypeInt:
|
|
|
|
case SpvOpTypeBool:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case SpvOpTypeVector:
|
|
|
|
case SpvOpTypeMatrix:
|
|
|
|
return inst->word(3);
|
|
|
|
|
2019-02-25 22:43:11 +00:00
|
|
|
case SpvOpTypeCooperativeMatrixNV:
|
|
|
|
// Actual dimension isn't known, return 0
|
|
|
|
return 0;
|
|
|
|
|
2017-08-30 14:13:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-25 16:15:51 +00:00
|
|
|
if (inst->type_id()) return GetDimension(inst->type_id());
|
2017-08-30 14:13:10 +00:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ValidationState_t::GetBitWidth(uint32_t id) const {
|
|
|
|
const uint32_t component_type_id = GetComponentType(id);
|
|
|
|
const Instruction* inst = FindDef(component_type_id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeFloat || inst->opcode() == SpvOpTypeInt)
|
|
|
|
return inst->word(2);
|
|
|
|
|
2017-10-25 16:15:51 +00:00
|
|
|
if (inst->opcode() == SpvOpTypeBool) return 1;
|
2017-08-30 14:13:10 +00:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-21 13:17:50 +00:00
|
|
|
bool ValidationState_t::IsVoidType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypeVoid;
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:13:10 +00:00
|
|
|
bool ValidationState_t::IsFloatScalarType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypeFloat;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsFloatVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsFloatScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:30:23 +00:00
|
|
|
bool ValidationState_t::IsFloatScalarOrVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeFloat) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsFloatScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:13:10 +00:00
|
|
|
bool ValidationState_t::IsIntScalarType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypeInt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsIntVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsIntScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:30:23 +00:00
|
|
|
bool ValidationState_t::IsIntScalarOrVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeInt) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsIntScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:13:10 +00:00
|
|
|
bool ValidationState_t::IsUnsignedIntScalarType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypeInt && inst->word(3) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsUnsignedIntVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsUnsignedIntScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsSignedIntScalarType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypeInt && inst->word(3) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsSignedIntVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsSignedIntScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsBoolScalarType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypeBool;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsBoolVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsBoolScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:30:23 +00:00
|
|
|
bool ValidationState_t::IsBoolScalarOrVectorType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeBool) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeVector) {
|
|
|
|
return IsBoolScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:30:27 +00:00
|
|
|
bool ValidationState_t::IsFloatMatrixType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpTypeMatrix) {
|
|
|
|
return IsFloatScalarType(GetComponentType(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-25 16:15:51 +00:00
|
|
|
bool ValidationState_t::GetMatrixTypeInfo(uint32_t id, uint32_t* num_rows,
|
|
|
|
uint32_t* num_cols,
|
|
|
|
uint32_t* column_type,
|
|
|
|
uint32_t* component_type) const {
|
|
|
|
if (!id) return false;
|
2017-09-06 18:30:27 +00:00
|
|
|
|
|
|
|
const Instruction* mat_inst = FindDef(id);
|
|
|
|
assert(mat_inst);
|
2017-10-25 16:15:51 +00:00
|
|
|
if (mat_inst->opcode() != SpvOpTypeMatrix) return false;
|
2017-09-06 18:30:27 +00:00
|
|
|
|
|
|
|
const uint32_t vec_type = mat_inst->word(2);
|
|
|
|
const Instruction* vec_inst = FindDef(vec_type);
|
|
|
|
assert(vec_inst);
|
|
|
|
|
|
|
|
if (vec_inst->opcode() != SpvOpTypeVector) {
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*num_cols = mat_inst->word(3);
|
|
|
|
*num_rows = vec_inst->word(3);
|
|
|
|
*column_type = mat_inst->word(2);
|
|
|
|
*component_type = vec_inst->word(2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:06:40 +00:00
|
|
|
bool ValidationState_t::GetStructMemberTypes(
|
|
|
|
uint32_t struct_type_id, std::vector<uint32_t>* member_types) const {
|
|
|
|
member_types->clear();
|
2017-10-25 16:15:51 +00:00
|
|
|
if (!struct_type_id) return false;
|
2017-09-18 17:06:40 +00:00
|
|
|
|
|
|
|
const Instruction* inst = FindDef(struct_type_id);
|
|
|
|
assert(inst);
|
2017-10-25 16:15:51 +00:00
|
|
|
if (inst->opcode() != SpvOpTypeStruct) return false;
|
2017-09-18 17:06:40 +00:00
|
|
|
|
2017-10-25 16:15:51 +00:00
|
|
|
*member_types =
|
|
|
|
std::vector<uint32_t>(inst->words().cbegin() + 2, inst->words().cend());
|
2017-09-18 17:06:40 +00:00
|
|
|
|
2017-10-25 16:15:51 +00:00
|
|
|
if (member_types->empty()) return false;
|
2017-09-18 17:06:40 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-28 18:53:24 +00:00
|
|
|
bool ValidationState_t::IsPointerType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypePointer;
|
|
|
|
}
|
|
|
|
|
2017-10-25 16:15:51 +00:00
|
|
|
bool ValidationState_t::GetPointerTypeInfo(uint32_t id, uint32_t* data_type,
|
|
|
|
uint32_t* storage_class) const {
|
|
|
|
if (!id) return false;
|
2017-09-28 18:53:24 +00:00
|
|
|
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
2017-10-25 16:15:51 +00:00
|
|
|
if (inst->opcode() != SpvOpTypePointer) return false;
|
2017-09-28 18:53:24 +00:00
|
|
|
|
|
|
|
*storage_class = inst->word(2);
|
|
|
|
*data_type = inst->word(3);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:43:11 +00:00
|
|
|
bool ValidationState_t::IsCooperativeMatrixType(uint32_t id) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
return inst->opcode() == SpvOpTypeCooperativeMatrixNV;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsFloatCooperativeMatrixType(uint32_t id) const {
|
|
|
|
if (!IsCooperativeMatrixType(id)) return false;
|
|
|
|
return IsFloatScalarType(FindDef(id)->word(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsIntCooperativeMatrixType(uint32_t id) const {
|
|
|
|
if (!IsCooperativeMatrixType(id)) return false;
|
|
|
|
return IsIntScalarType(FindDef(id)->word(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::IsUnsignedIntCooperativeMatrixType(uint32_t id) const {
|
|
|
|
if (!IsCooperativeMatrixType(id)) return false;
|
|
|
|
return IsUnsignedIntScalarType(FindDef(id)->word(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidationState_t::CooperativeMatrixShapesMatch(
|
|
|
|
const Instruction* inst, uint32_t m1, uint32_t m2) {
|
|
|
|
const auto m1_type = FindDef(m1);
|
|
|
|
const auto m2_type = FindDef(m2);
|
|
|
|
|
|
|
|
if (m1_type->opcode() != SpvOpTypeCooperativeMatrixNV ||
|
|
|
|
m2_type->opcode() != SpvOpTypeCooperativeMatrixNV) {
|
|
|
|
return diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected cooperative matrix types";
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t m1_scope_id = m1_type->GetOperandAs<uint32_t>(2);
|
|
|
|
uint32_t m1_rows_id = m1_type->GetOperandAs<uint32_t>(3);
|
|
|
|
uint32_t m1_cols_id = m1_type->GetOperandAs<uint32_t>(4);
|
|
|
|
|
|
|
|
uint32_t m2_scope_id = m2_type->GetOperandAs<uint32_t>(2);
|
|
|
|
uint32_t m2_rows_id = m2_type->GetOperandAs<uint32_t>(3);
|
|
|
|
uint32_t m2_cols_id = m2_type->GetOperandAs<uint32_t>(4);
|
|
|
|
|
|
|
|
bool m1_is_int32 = false, m1_is_const_int32 = false, m2_is_int32 = false,
|
|
|
|
m2_is_const_int32 = false;
|
|
|
|
uint32_t m1_value = 0, m2_value = 0;
|
|
|
|
|
|
|
|
std::tie(m1_is_int32, m1_is_const_int32, m1_value) =
|
|
|
|
EvalInt32IfConst(m1_scope_id);
|
|
|
|
std::tie(m2_is_int32, m2_is_const_int32, m2_value) =
|
|
|
|
EvalInt32IfConst(m2_scope_id);
|
|
|
|
|
|
|
|
if (m1_is_const_int32 && m2_is_const_int32 && m1_value != m2_value) {
|
|
|
|
return diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected scopes of Matrix and Result Type to be "
|
|
|
|
<< "identical";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tie(m1_is_int32, m1_is_const_int32, m1_value) =
|
|
|
|
EvalInt32IfConst(m1_rows_id);
|
|
|
|
std::tie(m2_is_int32, m2_is_const_int32, m2_value) =
|
|
|
|
EvalInt32IfConst(m2_rows_id);
|
|
|
|
|
|
|
|
if (m1_is_const_int32 && m2_is_const_int32 && m1_value != m2_value) {
|
|
|
|
return diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected rows of Matrix type and Result Type to be "
|
|
|
|
<< "identical";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tie(m1_is_int32, m1_is_const_int32, m1_value) =
|
|
|
|
EvalInt32IfConst(m1_cols_id);
|
|
|
|
std::tie(m2_is_int32, m2_is_const_int32, m2_value) =
|
|
|
|
EvalInt32IfConst(m2_cols_id);
|
|
|
|
|
|
|
|
if (m1_is_const_int32 && m2_is_const_int32 && m1_value != m2_value) {
|
|
|
|
return diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected columns of Matrix type and Result Type to be "
|
|
|
|
<< "identical";
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
uint32_t ValidationState_t::GetOperandTypeId(const Instruction* inst,
|
|
|
|
size_t operand_index) const {
|
|
|
|
return GetTypeId(inst->GetOperandAs<uint32_t>(operand_index));
|
2017-09-28 18:53:24 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 19:30:23 +00:00
|
|
|
bool ValidationState_t::GetConstantValUint64(uint32_t id, uint64_t* val) const {
|
|
|
|
const Instruction* inst = FindDef(id);
|
|
|
|
if (!inst) {
|
|
|
|
assert(0 && "Instruction not found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inst->opcode() != SpvOpConstant && inst->opcode() != SpvOpSpecConstant)
|
|
|
|
return false;
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
if (!IsIntScalarType(inst->type_id())) return false;
|
2017-10-26 19:30:23 +00:00
|
|
|
|
|
|
|
if (inst->words().size() == 4) {
|
|
|
|
*val = inst->word(3);
|
|
|
|
} else {
|
|
|
|
assert(inst->words().size() == 5);
|
|
|
|
*val = inst->word(3);
|
|
|
|
*val |= uint64_t(inst->word(4)) << 32;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-06 17:10:11 +00:00
|
|
|
std::tuple<bool, bool, uint32_t> ValidationState_t::EvalInt32IfConst(
|
2019-02-25 22:43:11 +00:00
|
|
|
uint32_t id) const {
|
2018-02-06 17:10:11 +00:00
|
|
|
const Instruction* const inst = FindDef(id);
|
|
|
|
assert(inst);
|
|
|
|
const uint32_t type = inst->type_id();
|
|
|
|
|
2018-08-20 14:41:01 +00:00
|
|
|
if (type == 0 || !IsIntScalarType(type) || GetBitWidth(type) != 32) {
|
2018-02-06 17:10:11 +00:00
|
|
|
return std::make_tuple(false, false, 0);
|
|
|
|
}
|
|
|
|
|
2018-11-30 19:00:56 +00:00
|
|
|
// Spec constant values cannot be evaluated so don't consider constant for
|
|
|
|
// the purpose of this method.
|
|
|
|
if (!spvOpcodeIsConstant(inst->opcode()) ||
|
|
|
|
spvOpcodeIsSpecConstant(inst->opcode())) {
|
2018-02-06 17:10:11 +00:00
|
|
|
return std::make_tuple(true, false, 0);
|
|
|
|
}
|
|
|
|
|
2018-07-27 17:11:49 +00:00
|
|
|
if (inst->opcode() == SpvOpConstantNull) {
|
|
|
|
return std::make_tuple(true, true, 0);
|
|
|
|
}
|
|
|
|
|
2018-02-06 17:10:11 +00:00
|
|
|
assert(inst->words().size() == 4);
|
|
|
|
return std::make_tuple(true, true, inst->word(3));
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:58:11 +00:00
|
|
|
void ValidationState_t::ComputeFunctionToEntryPointMapping() {
|
|
|
|
for (const uint32_t entry_point : entry_points()) {
|
|
|
|
std::stack<uint32_t> call_stack;
|
|
|
|
std::set<uint32_t> visited;
|
|
|
|
call_stack.push(entry_point);
|
|
|
|
while (!call_stack.empty()) {
|
|
|
|
const uint32_t called_func_id = call_stack.top();
|
|
|
|
call_stack.pop();
|
|
|
|
if (!visited.insert(called_func_id).second) continue;
|
|
|
|
|
|
|
|
function_to_entry_points_[called_func_id].push_back(entry_point);
|
|
|
|
|
|
|
|
const Function* called_func = function(called_func_id);
|
|
|
|
if (called_func) {
|
|
|
|
// Other checks should error out on this invalid SPIR-V.
|
2018-12-05 18:58:43 +00:00
|
|
|
for (const uint32_t new_call : called_func->function_call_targets()) {
|
|
|
|
call_stack.push(new_call);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValidationState_t::ComputeRecursiveEntryPoints() {
|
2020-02-23 23:50:59 +00:00
|
|
|
for (const Function& func : functions()) {
|
2018-12-05 18:58:43 +00:00
|
|
|
std::stack<uint32_t> call_stack;
|
|
|
|
std::set<uint32_t> visited;
|
|
|
|
|
|
|
|
for (const uint32_t new_call : func.function_call_targets()) {
|
|
|
|
call_stack.push(new_call);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!call_stack.empty()) {
|
|
|
|
const uint32_t called_func_id = call_stack.top();
|
|
|
|
call_stack.pop();
|
|
|
|
|
|
|
|
if (!visited.insert(called_func_id).second) continue;
|
|
|
|
|
|
|
|
if (called_func_id == func.id()) {
|
|
|
|
for (const uint32_t entry_point :
|
|
|
|
function_to_entry_points_[called_func_id])
|
|
|
|
recursive_entry_points_.insert(entry_point);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Function* called_func = function(called_func_id);
|
|
|
|
if (called_func) {
|
|
|
|
// Other checks should error out on this invalid SPIR-V.
|
2018-04-16 17:58:11 +00:00
|
|
|
for (const uint32_t new_call : called_func->function_call_targets()) {
|
|
|
|
call_stack.push(new_call);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<uint32_t>& ValidationState_t::FunctionEntryPoints(
|
|
|
|
uint32_t func) const {
|
|
|
|
auto iter = function_to_entry_points_.find(func);
|
|
|
|
if (iter == function_to_entry_points_.end()) {
|
|
|
|
return empty_ids_;
|
|
|
|
} else {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 13:10:02 +00:00
|
|
|
std::set<uint32_t> ValidationState_t::EntryPointReferences(uint32_t id) const {
|
|
|
|
std::set<uint32_t> referenced_entry_points;
|
|
|
|
const auto inst = FindDef(id);
|
|
|
|
if (!inst) return referenced_entry_points;
|
|
|
|
|
|
|
|
std::vector<const Instruction*> stack;
|
|
|
|
stack.push_back(inst);
|
|
|
|
while (!stack.empty()) {
|
|
|
|
const auto current_inst = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
|
|
|
|
if (const auto func = current_inst->function()) {
|
|
|
|
// Instruction lives in a function, we can stop searching.
|
|
|
|
const auto function_entry_points = FunctionEntryPoints(func->id());
|
|
|
|
referenced_entry_points.insert(function_entry_points.begin(),
|
|
|
|
function_entry_points.end());
|
|
|
|
} else {
|
|
|
|
// Instruction is in the global scope, keep searching its uses.
|
|
|
|
for (auto pair : current_inst->uses()) {
|
|
|
|
const auto next_inst = pair.first;
|
|
|
|
stack.push_back(next_inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return referenced_entry_points;
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:02:44 +00:00
|
|
|
std::string ValidationState_t::Disassemble(const Instruction& inst) const {
|
|
|
|
const spv_parsed_instruction_t& c_inst(inst.c_inst());
|
|
|
|
return Disassemble(c_inst.words, c_inst.num_words);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ValidationState_t::Disassemble(const uint32_t* words,
|
|
|
|
uint16_t num_words) const {
|
|
|
|
uint32_t disassembly_options = SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
return spvInstructionBinaryToText(context()->target_env, words, num_words,
|
|
|
|
words_, num_words_, disassembly_options);
|
2018-06-19 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 17:48:17 +00:00
|
|
|
bool ValidationState_t::LogicallyMatch(const Instruction* lhs,
|
|
|
|
const Instruction* rhs,
|
|
|
|
bool check_decorations) {
|
|
|
|
if (lhs->opcode() != rhs->opcode()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_decorations) {
|
|
|
|
const auto& dec_a = id_decorations(lhs->id());
|
|
|
|
const auto& dec_b = id_decorations(rhs->id());
|
|
|
|
|
|
|
|
for (const auto& dec : dec_b) {
|
|
|
|
if (std::find(dec_a.begin(), dec_a.end(), dec) == dec_a.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs->opcode() == SpvOpTypeArray) {
|
|
|
|
// Size operands must match.
|
|
|
|
if (lhs->GetOperandAs<uint32_t>(2u) != rhs->GetOperandAs<uint32_t>(2u)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Elements must match or logically match.
|
|
|
|
const auto lhs_ele_id = lhs->GetOperandAs<uint32_t>(1u);
|
|
|
|
const auto rhs_ele_id = rhs->GetOperandAs<uint32_t>(1u);
|
|
|
|
if (lhs_ele_id == rhs_ele_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto lhs_ele = FindDef(lhs_ele_id);
|
|
|
|
const auto rhs_ele = FindDef(rhs_ele_id);
|
|
|
|
if (!lhs_ele || !rhs_ele) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return LogicallyMatch(lhs_ele, rhs_ele, check_decorations);
|
|
|
|
} else if (lhs->opcode() == SpvOpTypeStruct) {
|
|
|
|
// Number of elements must match.
|
|
|
|
if (lhs->operands().size() != rhs->operands().size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 1u; i < lhs->operands().size(); ++i) {
|
|
|
|
const auto lhs_ele_id = lhs->GetOperandAs<uint32_t>(i);
|
|
|
|
const auto rhs_ele_id = rhs->GetOperandAs<uint32_t>(i);
|
|
|
|
// Elements must match or logically match.
|
|
|
|
if (lhs_ele_id == rhs_ele_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto lhs_ele = FindDef(lhs_ele_id);
|
|
|
|
const auto rhs_ele = FindDef(rhs_ele_id);
|
|
|
|
if (!lhs_ele || !rhs_ele) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!LogicallyMatch(lhs_ele, rhs_ele, check_decorations)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All checks passed.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No other opcodes are acceptable at this point. Arrays and structs are
|
|
|
|
// caught above and if they're elements are not arrays or structs they are
|
|
|
|
// required to match exactly.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:13:07 +00:00
|
|
|
const Instruction* ValidationState_t::TracePointer(
|
|
|
|
const Instruction* inst) const {
|
|
|
|
auto base_ptr = inst;
|
|
|
|
while (base_ptr->opcode() == SpvOpAccessChain ||
|
|
|
|
base_ptr->opcode() == SpvOpInBoundsAccessChain ||
|
|
|
|
base_ptr->opcode() == SpvOpPtrAccessChain ||
|
|
|
|
base_ptr->opcode() == SpvOpInBoundsPtrAccessChain ||
|
|
|
|
base_ptr->opcode() == SpvOpCopyObject) {
|
|
|
|
base_ptr = FindDef(base_ptr->GetOperandAs<uint32_t>(2u));
|
|
|
|
}
|
|
|
|
return base_ptr;
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:10:13 +00:00
|
|
|
bool ValidationState_t::ContainsSizedIntOrFloatType(uint32_t id, SpvOp type,
|
|
|
|
uint32_t width) const {
|
|
|
|
if (type != SpvOpTypeInt && type != SpvOpTypeFloat) return false;
|
|
|
|
|
|
|
|
const auto inst = FindDef(id);
|
|
|
|
if (!inst) return false;
|
|
|
|
|
|
|
|
if (inst->opcode() == type) {
|
|
|
|
return inst->GetOperandAs<uint32_t>(1u) == width;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (inst->opcode()) {
|
|
|
|
case SpvOpTypeArray:
|
|
|
|
case SpvOpTypeRuntimeArray:
|
|
|
|
case SpvOpTypeVector:
|
|
|
|
case SpvOpTypeMatrix:
|
|
|
|
case SpvOpTypeImage:
|
|
|
|
case SpvOpTypeSampledImage:
|
|
|
|
case SpvOpTypeCooperativeMatrixNV:
|
|
|
|
return ContainsSizedIntOrFloatType(inst->GetOperandAs<uint32_t>(1u), type,
|
|
|
|
width);
|
|
|
|
case SpvOpTypePointer:
|
|
|
|
if (IsForwardPointer(id)) return false;
|
|
|
|
return ContainsSizedIntOrFloatType(inst->GetOperandAs<uint32_t>(2u), type,
|
|
|
|
width);
|
|
|
|
case SpvOpTypeFunction:
|
|
|
|
case SpvOpTypeStruct: {
|
|
|
|
for (uint32_t i = 1; i < inst->operands().size(); ++i) {
|
|
|
|
if (ContainsSizedIntOrFloatType(inst->GetOperandAs<uint32_t>(i), type,
|
|
|
|
width))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidationState_t::ContainsLimitedUseIntOrFloatType(uint32_t id) const {
|
|
|
|
if ((!HasCapability(SpvCapabilityInt16) &&
|
|
|
|
ContainsSizedIntOrFloatType(id, SpvOpTypeInt, 16)) ||
|
|
|
|
(!HasCapability(SpvCapabilityInt8) &&
|
|
|
|
ContainsSizedIntOrFloatType(id, SpvOpTypeInt, 8)) ||
|
|
|
|
(!HasCapability(SpvCapabilityFloat16) &&
|
|
|
|
ContainsSizedIntOrFloatType(id, SpvOpTypeFloat, 16))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-27 04:06:36 +00:00
|
|
|
bool ValidationState_t::IsValidStorageClass(
|
|
|
|
SpvStorageClass storage_class) const {
|
|
|
|
if (spvIsWebGPUEnv(context()->target_env)) {
|
|
|
|
switch (storage_class) {
|
|
|
|
case SpvStorageClassUniformConstant:
|
|
|
|
case SpvStorageClassUniform:
|
|
|
|
case SpvStorageClassStorageBuffer:
|
|
|
|
case SpvStorageClassInput:
|
|
|
|
case SpvStorageClassOutput:
|
|
|
|
case SpvStorageClassImage:
|
|
|
|
case SpvStorageClassWorkgroup:
|
|
|
|
case SpvStorageClassPrivate:
|
|
|
|
case SpvStorageClassFunction:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spvIsVulkanEnv(context()->target_env)) {
|
|
|
|
switch (storage_class) {
|
|
|
|
case SpvStorageClassUniformConstant:
|
|
|
|
case SpvStorageClassUniform:
|
|
|
|
case SpvStorageClassStorageBuffer:
|
|
|
|
case SpvStorageClassInput:
|
|
|
|
case SpvStorageClassOutput:
|
|
|
|
case SpvStorageClassImage:
|
|
|
|
case SpvStorageClassWorkgroup:
|
|
|
|
case SpvStorageClassPrivate:
|
|
|
|
case SpvStorageClassFunction:
|
|
|
|
case SpvStorageClassPushConstant:
|
|
|
|
case SpvStorageClassPhysicalStorageBuffer:
|
|
|
|
case SpvStorageClassRayPayloadNV:
|
|
|
|
case SpvStorageClassIncomingRayPayloadNV:
|
|
|
|
case SpvStorageClassHitAttributeNV:
|
|
|
|
case SpvStorageClassCallableDataNV:
|
|
|
|
case SpvStorageClassIncomingCallableDataNV:
|
|
|
|
case SpvStorageClassShaderRecordBufferNV:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
} // namespace val
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace spvtools
|