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
|
|
|
|
2016-06-02 22:51:05 +00:00
|
|
|
#include "val/ValidationState.h"
|
|
|
|
|
2015-12-16 02:44:21 +00:00
|
|
|
#include <cassert>
|
2016-06-02 22:51:05 +00:00
|
|
|
|
|
|
|
#include "val/BasicBlock.h"
|
|
|
|
#include "val/Construct.h"
|
|
|
|
#include "val/Function.h"
|
|
|
|
|
2016-08-09 18:05:03 +00:00
|
|
|
using std::deque;
|
2016-07-08 13:44:10 +00:00
|
|
|
using std::make_pair;
|
|
|
|
using std::pair;
|
2015-12-14 13:21:08 +00:00
|
|
|
using std::string;
|
2016-07-08 13:44:10 +00:00
|
|
|
using std::unordered_map;
|
2015-12-15 19:50:05 +00:00
|
|
|
using std::vector;
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2016-06-02 22:51:05 +00:00
|
|
|
namespace libspirv {
|
2015-12-15 19:50:05 +00:00
|
|
|
|
|
|
|
namespace {
|
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;
|
|
|
|
case kLayoutExecutionMode: out = op == SpvOpExecutionMode; break;
|
|
|
|
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;
|
|
|
|
case kLayoutAnnotations:
|
|
|
|
switch (op) {
|
|
|
|
case SpvOpDecorate:
|
|
|
|
case SpvOpMemberDecorate:
|
|
|
|
case SpvOpGroupDecorate:
|
|
|
|
case SpvOpGroupMemberDecorate:
|
|
|
|
case SpvOpDecorationGroup:
|
|
|
|
out = true;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kLayoutTypes:
|
|
|
|
switch (op) {
|
|
|
|
case SpvOpTypeVoid:
|
|
|
|
case SpvOpTypeBool:
|
|
|
|
case SpvOpTypeInt:
|
|
|
|
case SpvOpTypeFloat:
|
|
|
|
case SpvOpTypeVector:
|
|
|
|
case SpvOpTypeMatrix:
|
|
|
|
case SpvOpTypeImage:
|
|
|
|
case SpvOpTypeSampler:
|
|
|
|
case SpvOpTypeSampledImage:
|
|
|
|
case SpvOpTypeArray:
|
|
|
|
case SpvOpTypeRuntimeArray:
|
|
|
|
case SpvOpTypeStruct:
|
|
|
|
case SpvOpTypeOpaque:
|
|
|
|
case SpvOpTypePointer:
|
|
|
|
case SpvOpTypeFunction:
|
|
|
|
case SpvOpTypeEvent:
|
|
|
|
case SpvOpTypeDeviceEvent:
|
|
|
|
case SpvOpTypeReserveId:
|
|
|
|
case SpvOpTypeQueue:
|
|
|
|
case SpvOpTypePipe:
|
|
|
|
case SpvOpTypeForwardPointer:
|
|
|
|
case SpvOpConstantTrue:
|
|
|
|
case SpvOpConstantFalse:
|
|
|
|
case SpvOpConstant:
|
|
|
|
case SpvOpConstantComposite:
|
|
|
|
case SpvOpConstantSampler:
|
|
|
|
case SpvOpConstantNull:
|
|
|
|
case SpvOpSpecConstantTrue:
|
|
|
|
case SpvOpSpecConstantFalse:
|
|
|
|
case SpvOpSpecConstant:
|
|
|
|
case SpvOpSpecConstantComposite:
|
|
|
|
case SpvOpSpecConstantOp:
|
|
|
|
case SpvOpVariable:
|
|
|
|
case SpvOpLine:
|
2016-01-18 18:41:52 +00:00
|
|
|
case SpvOpNoLine:
|
2016-08-12 18:28:17 +00:00
|
|
|
case SpvOpUndef:
|
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
|
|
|
|
switch (op) {
|
|
|
|
case SpvOpCapability:
|
|
|
|
case SpvOpExtension:
|
|
|
|
case SpvOpExtInstImport:
|
|
|
|
case SpvOpMemoryModel:
|
|
|
|
case SpvOpEntryPoint:
|
|
|
|
case SpvOpExecutionMode:
|
|
|
|
case SpvOpSourceContinued:
|
|
|
|
case SpvOpSource:
|
|
|
|
case SpvOpSourceExtension:
|
|
|
|
case SpvOpString:
|
|
|
|
case SpvOpName:
|
|
|
|
case SpvOpMemberName:
|
|
|
|
case SpvOpDecorate:
|
|
|
|
case SpvOpMemberDecorate:
|
|
|
|
case SpvOpGroupDecorate:
|
|
|
|
case SpvOpGroupMemberDecorate:
|
|
|
|
case SpvOpDecorationGroup:
|
|
|
|
case SpvOpTypeVoid:
|
|
|
|
case SpvOpTypeBool:
|
|
|
|
case SpvOpTypeInt:
|
|
|
|
case SpvOpTypeFloat:
|
|
|
|
case SpvOpTypeVector:
|
|
|
|
case SpvOpTypeMatrix:
|
|
|
|
case SpvOpTypeImage:
|
|
|
|
case SpvOpTypeSampler:
|
|
|
|
case SpvOpTypeSampledImage:
|
|
|
|
case SpvOpTypeArray:
|
|
|
|
case SpvOpTypeRuntimeArray:
|
|
|
|
case SpvOpTypeStruct:
|
|
|
|
case SpvOpTypeOpaque:
|
|
|
|
case SpvOpTypePointer:
|
|
|
|
case SpvOpTypeFunction:
|
|
|
|
case SpvOpTypeEvent:
|
|
|
|
case SpvOpTypeDeviceEvent:
|
|
|
|
case SpvOpTypeReserveId:
|
|
|
|
case SpvOpTypeQueue:
|
|
|
|
case SpvOpTypePipe:
|
|
|
|
case SpvOpTypeForwardPointer:
|
|
|
|
case SpvOpConstantTrue:
|
|
|
|
case SpvOpConstantFalse:
|
|
|
|
case SpvOpConstant:
|
|
|
|
case SpvOpConstantComposite:
|
|
|
|
case SpvOpConstantSampler:
|
|
|
|
case SpvOpConstantNull:
|
|
|
|
case SpvOpSpecConstantTrue:
|
|
|
|
case SpvOpSpecConstantFalse:
|
|
|
|
case SpvOpSpecConstant:
|
|
|
|
case SpvOpSpecConstantComposite:
|
|
|
|
case SpvOpSpecConstantOp:
|
|
|
|
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
|
|
|
|
|
|
|
} // anonymous namespace
|
2015-12-14 13:21:08 +00:00
|
|
|
|
2016-01-09 22:25:36 +00:00
|
|
|
ValidationState_t::ValidationState_t(spv_diagnostic* diagnostic,
|
2016-01-31 04:32:09 +00:00
|
|
|
const spv_const_context context)
|
2016-01-13 14:52:23 +00:00
|
|
|
: diagnostic_(diagnostic),
|
2015-12-15 19:50:05 +00:00
|
|
|
instruction_counter_(0),
|
|
|
|
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_(),
|
2016-08-06 17:29:33 +00:00
|
|
|
ordered_instructions_(),
|
|
|
|
all_definitions_(),
|
2016-03-02 21:17:54 +00:00
|
|
|
grammar_(context),
|
|
|
|
addressing_model_(SpvAddressingModelLogical),
|
2016-03-16 21:20:02 +00:00
|
|
|
memory_model_(SpvMemoryModelSimple),
|
|
|
|
in_function_(false) {}
|
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-06-25 03:45:25 +00:00
|
|
|
void ValidationState_t::AssignNameToId(uint32_t id, string name) {
|
2015-12-14 13:21:08 +00:00
|
|
|
operand_names_[id] = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
string ValidationState_t::getIdName(uint32_t id) const {
|
|
|
|
std::stringstream out;
|
|
|
|
out << id;
|
|
|
|
if (operand_names_.find(id) != end(operand_names_)) {
|
|
|
|
out << "[" << operand_names_.at(id) << "]";
|
|
|
|
}
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2016-03-16 21:20:02 +00:00
|
|
|
string ValidationState_t::getIdOrName(uint32_t id) const {
|
|
|
|
std::stringstream out;
|
|
|
|
if (operand_names_.find(id) != end(operand_names_)) {
|
|
|
|
out << operand_names_.at(id);
|
|
|
|
} else {
|
|
|
|
out << id;
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
vector<uint32_t> ValidationState_t::UnresolvedForwardIds() const {
|
2015-12-14 13:21:08 +00:00
|
|
|
vector<uint32_t> out(begin(unresolved_forward_ids_),
|
|
|
|
end(unresolved_forward_ids_));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
bool ValidationState_t::IsDefinedId(uint32_t id) const {
|
2016-08-06 17:29:33 +00:00
|
|
|
return all_definitions_.find(id) != 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 {
|
|
|
|
if (all_definitions_.count(id) == 0) {
|
2016-07-08 13:44:10 +00:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
/// We are in a const function, so we cannot use defs.operator[]().
|
|
|
|
/// Luckily we know the key exists, so defs_.at() won't throw an
|
|
|
|
/// exception.
|
2016-08-06 17:29:33 +00:00
|
|
|
return all_definitions_.at(id);
|
2016-07-08 13:44:10 +00:00
|
|
|
}
|
2015-12-14 13:21:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 17:29:33 +00:00
|
|
|
Instruction* ValidationState_t::FindDef(uint32_t id) {
|
2016-08-09 18:05:03 +00:00
|
|
|
if (all_definitions_.count(id) == 0) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
/// We are in a const function, so we cannot use defs.operator[]().
|
|
|
|
/// Luckily we know the key exists, so defs_.at() won't throw an
|
|
|
|
/// exception.
|
|
|
|
return all_definitions_.at(id);
|
|
|
|
}
|
2016-08-06 17:29:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 13:21:08 +00:00
|
|
|
// Increments the instruction count. Used for diagnostic
|
2016-06-25 03:45:25 +00:00
|
|
|
int ValidationState_t::increment_instruction_count() {
|
2015-12-14 13:21:08 +00:00
|
|
|
return instruction_counter_++;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-16 02:44:21 +00:00
|
|
|
DiagnosticStream ValidationState_t::diag(spv_result_t error_code) const {
|
2015-12-14 13:21:08 +00:00
|
|
|
return libspirv::DiagnosticStream(
|
|
|
|
{0, 0, static_cast<size_t>(instruction_counter_)}, diagnostic_,
|
|
|
|
error_code);
|
|
|
|
}
|
2015-12-16 02:44:21 +00:00
|
|
|
|
2016-08-09 18:05:03 +00:00
|
|
|
deque<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
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
desc->capabilities.ForEach(
|
|
|
|
[this](SpvCapability c) { RegisterCapability(c); });
|
|
|
|
}
|
2016-01-23 19:14:32 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 18:49:00 +00:00
|
|
|
bool ValidationState_t::HasAnyOf(const CapabilitySet& capabilities) const {
|
2016-01-31 04:32:09 +00:00
|
|
|
bool found = false;
|
2016-08-29 18:49:00 +00:00
|
|
|
bool any_queried = false;
|
|
|
|
capabilities.ForEach([&found, &any_queried, this](SpvCapability c) {
|
|
|
|
any_queried = true;
|
|
|
|
found = found || this->module_capabilities_.Contains(c);
|
2016-01-31 04:32:09 +00:00
|
|
|
});
|
2016-08-29 18:49:00 +00:00
|
|
|
return !any_queried || found;
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-06 17:29:33 +00:00
|
|
|
void ValidationState_t::RegisterInstruction(
|
|
|
|
const spv_parsed_instruction_t& inst) {
|
2016-07-08 13:44:10 +00:00
|
|
|
if (in_function_body()) {
|
2016-08-06 17:29:33 +00:00
|
|
|
ordered_instructions_.emplace_back(
|
|
|
|
&inst, ¤t_function(), current_function().current_block());
|
2016-07-08 13:44:10 +00:00
|
|
|
} else {
|
2016-08-06 17:29:33 +00:00
|
|
|
ordered_instructions_.emplace_back(&inst, nullptr, nullptr);
|
2016-07-08 13:44:10 +00:00
|
|
|
}
|
2016-08-06 17:29:33 +00:00
|
|
|
uint32_t id = ordered_instructions_.back().id();
|
|
|
|
if (id) {
|
|
|
|
all_definitions_.insert(make_pair(id, &ordered_instructions_.back()));
|
2016-07-08 13:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 22:51:05 +00:00
|
|
|
} /// namespace libspirv
|