2018-08-01 18:44:56 +00:00
|
|
|
// Copyright (c) 2018 Google LLC.
|
2020-03-17 19:30:19 +00:00
|
|
|
// Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
|
|
|
|
// reserved.
|
2018-08-01 18:44:56 +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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-08-01 18:44:56 +00:00
|
|
|
|
|
|
|
#include "source/opcode.h"
|
2018-11-14 20:00:03 +00:00
|
|
|
#include "source/spirv_target_env.h"
|
2018-08-01 18:44:56 +00:00
|
|
|
#include "source/val/instruction.h"
|
2019-06-17 17:13:07 +00:00
|
|
|
#include "source/val/validate.h"
|
2018-12-03 22:15:47 +00:00
|
|
|
#include "source/val/validate_scopes.h"
|
2018-08-01 18:44:56 +00:00
|
|
|
#include "source/val/validation_state.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace val {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool AreLayoutCompatibleStructs(ValidationState_t&, const Instruction*,
|
|
|
|
const Instruction*);
|
|
|
|
bool HaveLayoutCompatibleMembers(ValidationState_t&, const Instruction*,
|
|
|
|
const Instruction*);
|
|
|
|
bool HaveSameLayoutDecorations(ValidationState_t&, const Instruction*,
|
|
|
|
const Instruction*);
|
2022-06-02 15:32:38 +00:00
|
|
|
bool HasConflictingMemberOffsets(const std::set<Decoration>&,
|
|
|
|
const std::set<Decoration>&);
|
2018-08-01 18:44:56 +00:00
|
|
|
|
2018-11-15 18:42:17 +00:00
|
|
|
bool IsAllowedTypeOrArrayOfSame(ValidationState_t& _, const Instruction* type,
|
2022-11-04 21:27:10 +00:00
|
|
|
std::initializer_list<spv::Op> allowed) {
|
2018-11-15 18:42:17 +00:00
|
|
|
if (std::find(allowed.begin(), allowed.end(), type->opcode()) !=
|
|
|
|
allowed.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (type->opcode() == spv::Op::OpTypeArray ||
|
|
|
|
type->opcode() == spv::Op::OpTypeRuntimeArray) {
|
2018-11-15 18:42:17 +00:00
|
|
|
auto elem_type = _.FindDef(type->word(2));
|
|
|
|
return std::find(allowed.begin(), allowed.end(), elem_type->opcode()) !=
|
|
|
|
allowed.end();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:44:56 +00:00
|
|
|
// Returns true if the two instructions represent structs that, as far as the
|
|
|
|
// validator can tell, have the exact same data layout.
|
|
|
|
bool AreLayoutCompatibleStructs(ValidationState_t& _, const Instruction* type1,
|
|
|
|
const Instruction* type2) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (type1->opcode() != spv::Op::OpTypeStruct) {
|
2018-08-01 18:44:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (type2->opcode() != spv::Op::OpTypeStruct) {
|
2018-08-01 18:44:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HaveLayoutCompatibleMembers(_, type1, type2)) return false;
|
|
|
|
|
|
|
|
return HaveSameLayoutDecorations(_, type1, type2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the operands to the OpTypeStruct instruction defining the
|
|
|
|
// types are the same or are layout compatible types. |type1| and |type2| must
|
|
|
|
// be OpTypeStruct instructions.
|
|
|
|
bool HaveLayoutCompatibleMembers(ValidationState_t& _, const Instruction* type1,
|
|
|
|
const Instruction* type2) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(type1->opcode() == spv::Op::OpTypeStruct &&
|
2018-11-30 18:12:05 +00:00
|
|
|
"type1 must be an OpTypeStruct instruction.");
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(type2->opcode() == spv::Op::OpTypeStruct &&
|
2018-11-30 18:12:05 +00:00
|
|
|
"type2 must be an OpTypeStruct instruction.");
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto& type1_operands = type1->operands();
|
|
|
|
const auto& type2_operands = type2->operands();
|
|
|
|
if (type1_operands.size() != type2_operands.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t operand = 2; operand < type1_operands.size(); ++operand) {
|
|
|
|
if (type1->word(operand) != type2->word(operand)) {
|
|
|
|
auto def1 = _.FindDef(type1->word(operand));
|
|
|
|
auto def2 = _.FindDef(type2->word(operand));
|
|
|
|
if (!AreLayoutCompatibleStructs(_, def1, def2)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if all decorations that affect the data layout of the struct
|
|
|
|
// (like Offset), are the same for the two types. |type1| and |type2| must be
|
|
|
|
// OpTypeStruct instructions.
|
|
|
|
bool HaveSameLayoutDecorations(ValidationState_t& _, const Instruction* type1,
|
|
|
|
const Instruction* type2) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(type1->opcode() == spv::Op::OpTypeStruct &&
|
2018-11-30 18:12:05 +00:00
|
|
|
"type1 must be an OpTypeStruct instruction.");
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(type2->opcode() == spv::Op::OpTypeStruct &&
|
2018-11-30 18:12:05 +00:00
|
|
|
"type2 must be an OpTypeStruct instruction.");
|
2022-06-02 15:32:38 +00:00
|
|
|
const std::set<Decoration>& type1_decorations = _.id_decorations(type1->id());
|
|
|
|
const std::set<Decoration>& type2_decorations = _.id_decorations(type2->id());
|
2018-08-01 18:44:56 +00:00
|
|
|
|
|
|
|
// TODO: Will have to add other check for arrays an matricies if we want to
|
|
|
|
// handle them.
|
|
|
|
if (HasConflictingMemberOffsets(type1_decorations, type2_decorations)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasConflictingMemberOffsets(
|
2022-06-02 15:32:38 +00:00
|
|
|
const std::set<Decoration>& type1_decorations,
|
|
|
|
const std::set<Decoration>& type2_decorations) {
|
2018-08-01 18:44:56 +00:00
|
|
|
{
|
|
|
|
// We are interested in conflicting decoration. If a decoration is in one
|
|
|
|
// list but not the other, then we will assume the code is correct. We are
|
|
|
|
// looking for things we know to be wrong.
|
|
|
|
//
|
|
|
|
// We do not have to traverse type2_decoration because, after traversing
|
|
|
|
// type1_decorations, anything new will not be found in
|
|
|
|
// type1_decoration. Therefore, it cannot lead to a conflict.
|
|
|
|
for (const Decoration& decoration : type1_decorations) {
|
|
|
|
switch (decoration.dec_type()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::Offset: {
|
2018-08-01 18:44:56 +00:00
|
|
|
// Since these affect the layout of the struct, they must be present
|
|
|
|
// in both structs.
|
|
|
|
auto compare = [&decoration](const Decoration& rhs) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (rhs.dec_type() != spv::Decoration::Offset) return false;
|
2018-08-01 18:44:56 +00:00
|
|
|
return decoration.struct_member_index() ==
|
|
|
|
rhs.struct_member_index();
|
|
|
|
};
|
|
|
|
auto i = std::find_if(type2_decorations.begin(),
|
|
|
|
type2_decorations.end(), compare);
|
|
|
|
if (i != type2_decorations.end() &&
|
|
|
|
decoration.params().front() != i->params().front()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
// This decoration does not affect the layout of the structure, so
|
|
|
|
// just moving on.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-30 13:01:37 +00:00
|
|
|
// If |skip_builtin| is true, returns true if |storage| contains bool within
|
|
|
|
// it and no storage that contains the bool is builtin.
|
|
|
|
// If |skip_builtin| is false, returns true if |storage| contains bool within
|
|
|
|
// it.
|
|
|
|
bool ContainsInvalidBool(ValidationState_t& _, const Instruction* storage,
|
|
|
|
bool skip_builtin) {
|
|
|
|
if (skip_builtin) {
|
|
|
|
for (const Decoration& decoration : _.id_decorations(storage->id())) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (decoration.dec_type() == spv::Decoration::BuiltIn) return false;
|
2018-08-30 13:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t elem_type_index = 1;
|
|
|
|
uint32_t elem_type_id;
|
|
|
|
Instruction* elem_type;
|
|
|
|
|
|
|
|
switch (storage->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeBool:
|
2018-08-30 13:01:37 +00:00
|
|
|
return true;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeVector:
|
|
|
|
case spv::Op::OpTypeMatrix:
|
|
|
|
case spv::Op::OpTypeArray:
|
|
|
|
case spv::Op::OpTypeRuntimeArray:
|
2018-08-30 13:01:37 +00:00
|
|
|
elem_type_id = storage->GetOperandAs<uint32_t>(elem_type_index);
|
|
|
|
elem_type = _.FindDef(elem_type_id);
|
|
|
|
return ContainsInvalidBool(_, elem_type, skip_builtin);
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeStruct:
|
2018-08-30 13:01:37 +00:00
|
|
|
for (size_t member_type_index = 1;
|
|
|
|
member_type_index < storage->operands().size();
|
|
|
|
++member_type_index) {
|
|
|
|
auto member_type_id =
|
|
|
|
storage->GetOperandAs<uint32_t>(member_type_index);
|
|
|
|
auto member_type = _.FindDef(member_type_id);
|
|
|
|
if (ContainsInvalidBool(_, member_type, skip_builtin)) return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:43:11 +00:00
|
|
|
bool ContainsCooperativeMatrix(ValidationState_t& _,
|
|
|
|
const Instruction* storage) {
|
|
|
|
const size_t elem_type_index = 1;
|
|
|
|
uint32_t elem_type_id;
|
|
|
|
Instruction* elem_type;
|
|
|
|
|
|
|
|
switch (storage->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeCooperativeMatrixNV:
|
2019-02-25 22:43:11 +00:00
|
|
|
return true;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeArray:
|
|
|
|
case spv::Op::OpTypeRuntimeArray:
|
2019-02-25 22:43:11 +00:00
|
|
|
elem_type_id = storage->GetOperandAs<uint32_t>(elem_type_index);
|
|
|
|
elem_type = _.FindDef(elem_type_id);
|
|
|
|
return ContainsCooperativeMatrix(_, elem_type);
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeStruct:
|
2019-02-25 22:43:11 +00:00
|
|
|
for (size_t member_type_index = 1;
|
|
|
|
member_type_index < storage->operands().size();
|
|
|
|
++member_type_index) {
|
|
|
|
auto member_type_id =
|
|
|
|
storage->GetOperandAs<uint32_t>(member_type_index);
|
|
|
|
auto member_type = _.FindDef(member_type_id);
|
|
|
|
if (ContainsCooperativeMatrix(_, member_type)) return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
std::pair<spv::StorageClass, spv::StorageClass> GetStorageClass(
|
2018-10-15 13:30:47 +00:00
|
|
|
ValidationState_t& _, const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass dst_sc = spv::StorageClass::Max;
|
|
|
|
spv::StorageClass src_sc = spv::StorageClass::Max;
|
2018-10-15 13:30:47 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCooperativeMatrixLoadNV:
|
|
|
|
case spv::Op::OpLoad: {
|
2018-10-15 13:30:47 +00:00
|
|
|
auto load_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(2));
|
|
|
|
auto load_pointer_type = _.FindDef(load_pointer->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
dst_sc = load_pointer_type->GetOperandAs<spv::StorageClass>(1);
|
2018-10-15 13:30:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCooperativeMatrixStoreNV:
|
|
|
|
case spv::Op::OpStore: {
|
2018-10-15 13:30:47 +00:00
|
|
|
auto store_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(0));
|
|
|
|
auto store_pointer_type = _.FindDef(store_pointer->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
dst_sc = store_pointer_type->GetOperandAs<spv::StorageClass>(1);
|
2018-10-15 13:30:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCopyMemory:
|
|
|
|
case spv::Op::OpCopyMemorySized: {
|
2018-10-15 13:30:47 +00:00
|
|
|
auto dst = _.FindDef(inst->GetOperandAs<uint32_t>(0));
|
|
|
|
auto dst_type = _.FindDef(dst->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
dst_sc = dst_type->GetOperandAs<spv::StorageClass>(1);
|
2018-10-15 13:30:47 +00:00
|
|
|
auto src = _.FindDef(inst->GetOperandAs<uint32_t>(1));
|
|
|
|
auto src_type = _.FindDef(src->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
src_sc = src_type->GetOperandAs<spv::StorageClass>(1);
|
2018-10-15 13:30:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(dst_sc, src_sc);
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
// Returns the number of instruction words taken up by a memory access
|
|
|
|
// argument and its implied operands.
|
|
|
|
int MemoryAccessNumWords(uint32_t mask) {
|
|
|
|
int result = 1; // Count the mask
|
2022-11-04 21:27:10 +00:00
|
|
|
if (mask & uint32_t(spv::MemoryAccessMask::Aligned)) ++result;
|
|
|
|
if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) ++result;
|
|
|
|
if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) ++result;
|
2019-05-07 16:27:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the scope ID operand for MakeAvailable memory access with mask
|
|
|
|
// at the given operand index.
|
2018-12-03 22:15:47 +00:00
|
|
|
// This function is only called for OpLoad, OpStore, OpCopyMemory and
|
2019-02-25 22:43:11 +00:00
|
|
|
// OpCopyMemorySized, OpCooperativeMatrixLoadNV, and
|
|
|
|
// OpCooperativeMatrixStoreNV.
|
2019-05-07 16:27:18 +00:00
|
|
|
uint32_t GetMakeAvailableScope(const Instruction* inst, uint32_t mask,
|
|
|
|
uint32_t mask_index) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR));
|
|
|
|
uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR);
|
2019-05-07 16:27:18 +00:00
|
|
|
uint32_t index =
|
|
|
|
mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1)));
|
|
|
|
return inst->GetOperandAs<uint32_t>(index);
|
2018-12-03 22:15:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:43:11 +00:00
|
|
|
// This function is only called for OpLoad, OpStore, OpCopyMemory,
|
|
|
|
// OpCopyMemorySized, OpCooperativeMatrixLoadNV, and
|
|
|
|
// OpCooperativeMatrixStoreNV.
|
2019-05-07 16:27:18 +00:00
|
|
|
uint32_t GetMakeVisibleScope(const Instruction* inst, uint32_t mask,
|
|
|
|
uint32_t mask_index) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR));
|
|
|
|
uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR);
|
2019-05-07 16:27:18 +00:00
|
|
|
uint32_t index =
|
|
|
|
mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1)));
|
|
|
|
return inst->GetOperandAs<uint32_t>(index);
|
2018-12-03 22:15:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 15:39:12 +00:00
|
|
|
bool DoesStructContainRTA(const ValidationState_t& _, const Instruction* inst) {
|
|
|
|
for (size_t member_index = 1; member_index < inst->operands().size();
|
|
|
|
++member_index) {
|
|
|
|
const auto member_id = inst->GetOperandAs<uint32_t>(member_index);
|
|
|
|
const auto member_type = _.FindDef(member_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (member_type->opcode() == spv::Op::OpTypeRuntimeArray) return true;
|
2019-01-17 15:39:12 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-15 13:30:47 +00:00
|
|
|
spv_result_t CheckMemoryAccess(ValidationState_t& _, const Instruction* inst,
|
2019-01-07 18:19:24 +00:00
|
|
|
uint32_t index) {
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass dst_sc, src_sc;
|
2019-01-07 18:19:24 +00:00
|
|
|
std::tie(dst_sc, src_sc) = GetStorageClass(_, inst);
|
|
|
|
if (inst->operands().size() <= index) {
|
2022-03-23 15:08:21 +00:00
|
|
|
// Cases where lack of some operand is invalid
|
2022-11-04 21:27:10 +00:00
|
|
|
if (src_sc == spv::StorageClass::PhysicalStorageBuffer ||
|
|
|
|
dst_sc == spv::StorageClass::PhysicalStorageBuffer) {
|
2019-01-17 15:39:12 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-23 15:08:21 +00:00
|
|
|
<< _.VkErrorID(4708)
|
|
|
|
<< "Memory accesses with PhysicalStorageBuffer must use Aligned.";
|
2019-01-07 18:19:24 +00:00
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
const uint32_t mask = inst->GetOperandAs<uint32_t>(index);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) {
|
|
|
|
if (inst->opcode() == spv::Op::OpLoad ||
|
|
|
|
inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) {
|
2018-10-15 13:30:47 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "MakePointerAvailableKHR cannot be used with OpLoad.";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) {
|
2018-10-15 13:30:47 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "NonPrivatePointerKHR must be specified if "
|
|
|
|
"MakePointerAvailableKHR is specified.";
|
|
|
|
}
|
2018-12-03 22:15:47 +00:00
|
|
|
|
|
|
|
// Check the associated scope for MakeAvailableKHR.
|
2019-05-07 16:27:18 +00:00
|
|
|
const auto available_scope = GetMakeAvailableScope(inst, mask, index);
|
2018-12-03 22:15:47 +00:00
|
|
|
if (auto error = ValidateMemoryScope(_, inst, available_scope))
|
|
|
|
return error;
|
2018-10-15 13:30:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) {
|
|
|
|
if (inst->opcode() == spv::Op::OpStore ||
|
|
|
|
inst->opcode() == spv::Op::OpCooperativeMatrixStoreNV) {
|
2018-10-15 13:30:47 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "MakePointerVisibleKHR cannot be used with OpStore.";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) {
|
2018-10-15 13:30:47 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "NonPrivatePointerKHR must be specified if "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "MakePointerVisibleKHR is specified.";
|
2018-10-15 13:30:47 +00:00
|
|
|
}
|
2018-12-03 22:15:47 +00:00
|
|
|
|
|
|
|
// Check the associated scope for MakeVisibleKHR.
|
2019-05-07 16:27:18 +00:00
|
|
|
const auto visible_scope = GetMakeVisibleScope(inst, mask, index);
|
2018-12-03 22:15:47 +00:00
|
|
|
if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error;
|
2018-10-15 13:30:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR)) {
|
|
|
|
if (dst_sc != spv::StorageClass::Uniform &&
|
|
|
|
dst_sc != spv::StorageClass::Workgroup &&
|
|
|
|
dst_sc != spv::StorageClass::CrossWorkgroup &&
|
|
|
|
dst_sc != spv::StorageClass::Generic &&
|
|
|
|
dst_sc != spv::StorageClass::Image &&
|
|
|
|
dst_sc != spv::StorageClass::StorageBuffer &&
|
|
|
|
dst_sc != spv::StorageClass::PhysicalStorageBuffer) {
|
2018-10-15 13:30:47 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "NonPrivatePointerKHR requires a pointer in Uniform, "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
|
|
|
|
<< "storage classes.";
|
2018-10-15 13:30:47 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (src_sc != spv::StorageClass::Max &&
|
|
|
|
src_sc != spv::StorageClass::Uniform &&
|
|
|
|
src_sc != spv::StorageClass::Workgroup &&
|
|
|
|
src_sc != spv::StorageClass::CrossWorkgroup &&
|
|
|
|
src_sc != spv::StorageClass::Generic &&
|
|
|
|
src_sc != spv::StorageClass::Image &&
|
|
|
|
src_sc != spv::StorageClass::StorageBuffer &&
|
|
|
|
src_sc != spv::StorageClass::PhysicalStorageBuffer) {
|
2018-10-15 13:30:47 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "NonPrivatePointerKHR requires a pointer in Uniform, "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
|
|
|
|
<< "storage classes.";
|
2018-10-15 13:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!(mask & uint32_t(spv::MemoryAccessMask::Aligned))) {
|
|
|
|
if (src_sc == spv::StorageClass::PhysicalStorageBuffer ||
|
|
|
|
dst_sc == spv::StorageClass::PhysicalStorageBuffer) {
|
2019-01-17 15:39:12 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-23 15:08:21 +00:00
|
|
|
<< _.VkErrorID(4708)
|
|
|
|
<< "Memory accesses with PhysicalStorageBuffer must use Aligned.";
|
2019-01-07 18:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 13:30:47 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:34:14 +00:00
|
|
|
spv_result_t ValidateVariable(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
auto result_type = _.FindDef(inst->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!result_type || result_type->opcode() != spv::Op::OpTypePointer) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpVariable Result Type <id> " << _.getIdName(inst->type_id())
|
|
|
|
<< " is not a pointer type.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 14:49:14 +00:00
|
|
|
const auto type_index = 2;
|
|
|
|
const auto value_id = result_type->GetOperandAs<uint32_t>(type_index);
|
|
|
|
auto value_type = _.FindDef(value_id);
|
|
|
|
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto initializer_index = 3;
|
2018-08-30 13:01:37 +00:00
|
|
|
const auto storage_class_index = 2;
|
2018-09-27 16:34:14 +00:00
|
|
|
if (initializer_index < inst->operands().size()) {
|
|
|
|
const auto initializer_id = inst->GetOperandAs<uint32_t>(initializer_index);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto initializer = _.FindDef(initializer_id);
|
|
|
|
const auto is_module_scope_var =
|
2022-11-04 21:27:10 +00:00
|
|
|
initializer && (initializer->opcode() == spv::Op::OpVariable) &&
|
|
|
|
(initializer->GetOperandAs<spv::StorageClass>(storage_class_index) !=
|
|
|
|
spv::StorageClass::Function);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto is_constant =
|
|
|
|
initializer && spvOpcodeIsConstant(initializer->opcode());
|
|
|
|
if (!initializer || !(is_constant || is_module_scope_var)) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpVariable Initializer <id> " << _.getIdName(initializer_id)
|
|
|
|
<< " is not a constant or module-scope variable.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
2021-02-05 14:49:14 +00:00
|
|
|
if (initializer->type_id() != value_id) {
|
2019-06-15 04:34:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Initializer type must match the type pointed to by the Result "
|
|
|
|
"Type";
|
|
|
|
}
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
auto storage_class =
|
|
|
|
inst->GetOperandAs<spv::StorageClass>(storage_class_index);
|
|
|
|
if (storage_class != spv::StorageClass::Workgroup &&
|
|
|
|
storage_class != spv::StorageClass::CrossWorkgroup &&
|
|
|
|
storage_class != spv::StorageClass::Private &&
|
|
|
|
storage_class != spv::StorageClass::Function &&
|
|
|
|
storage_class != spv::StorageClass::RayPayloadKHR &&
|
|
|
|
storage_class != spv::StorageClass::IncomingRayPayloadKHR &&
|
|
|
|
storage_class != spv::StorageClass::HitAttributeKHR &&
|
|
|
|
storage_class != spv::StorageClass::CallableDataKHR &&
|
|
|
|
storage_class != spv::StorageClass::IncomingCallableDataKHR &&
|
2022-11-24 14:50:45 +00:00
|
|
|
storage_class != spv::StorageClass::TaskPayloadWorkgroupEXT &&
|
|
|
|
storage_class != spv::StorageClass::HitObjectAttributeNV) {
|
2022-11-04 21:27:10 +00:00
|
|
|
bool storage_input_or_output = storage_class == spv::StorageClass::Input ||
|
|
|
|
storage_class == spv::StorageClass::Output;
|
2018-08-30 13:01:37 +00:00
|
|
|
bool builtin = false;
|
|
|
|
if (storage_input_or_output) {
|
2018-09-27 16:34:14 +00:00
|
|
|
for (const Decoration& decoration : _.id_decorations(inst->id())) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (decoration.dec_type() == spv::Decoration::BuiltIn) {
|
2018-08-30 13:01:37 +00:00
|
|
|
builtin = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 16:47:16 +00:00
|
|
|
if (!builtin &&
|
2021-02-05 14:49:14 +00:00
|
|
|
ContainsInvalidBool(_, value_type, storage_input_or_output)) {
|
2022-08-29 16:47:16 +00:00
|
|
|
if (storage_input_or_output) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-16 18:05:40 +00:00
|
|
|
<< _.VkErrorID(7290)
|
2022-08-29 16:47:16 +00:00
|
|
|
<< "If OpTypeBool is stored in conjunction with OpVariable "
|
|
|
|
"using Input or Output Storage Classes it requires a BuiltIn "
|
|
|
|
"decoration";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "If OpTypeBool is stored in conjunction with OpVariable, it "
|
|
|
|
"can only be used with non-externally visible shader Storage "
|
|
|
|
"Classes: Workgroup, CrossWorkgroup, Private, Function, "
|
|
|
|
"Input, Output, RayPayloadKHR, IncomingRayPayloadKHR, "
|
|
|
|
"HitAttributeKHR, CallableDataKHR, or "
|
|
|
|
"IncomingCallableDataKHR";
|
|
|
|
}
|
2018-08-30 13:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-14 20:00:03 +00:00
|
|
|
|
2019-09-27 04:06:36 +00:00
|
|
|
if (!_.IsValidStorageClass(storage_class)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY, inst)
|
2021-03-19 13:02:48 +00:00
|
|
|
<< _.VkErrorID(4643)
|
2019-09-27 04:06:36 +00:00
|
|
|
<< "Invalid storage class for target environment";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::Generic) {
|
2019-09-27 04:06:36 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY, inst)
|
|
|
|
<< "OpVariable storage class cannot be Generic";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->function() && storage_class != spv::StorageClass::Function) {
|
2019-09-27 04:06:36 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
|
|
|
<< "Variables must have a function[7] storage class inside"
|
|
|
|
" of a function";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!inst->function() && storage_class == spv::StorageClass::Function) {
|
2019-09-27 04:06:36 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
|
|
|
<< "Variables can not have a function[7] storage class "
|
|
|
|
"outside of a function";
|
|
|
|
}
|
|
|
|
|
2018-11-16 16:22:11 +00:00
|
|
|
// SPIR-V 3.32.8: Check that pointer type and variable type have the same
|
|
|
|
// storage class.
|
|
|
|
const auto result_storage_class_index = 1;
|
|
|
|
const auto result_storage_class =
|
2022-11-04 21:27:10 +00:00
|
|
|
result_type->GetOperandAs<spv::StorageClass>(result_storage_class_index);
|
2018-11-16 16:22:11 +00:00
|
|
|
if (storage_class != result_storage_class) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "From SPIR-V spec, section 3.32.8 on OpVariable:\n"
|
|
|
|
<< "Its Storage Class operand must be the same as the Storage Class "
|
|
|
|
<< "operand of the result type.";
|
|
|
|
}
|
|
|
|
|
2018-11-27 21:47:10 +00:00
|
|
|
// Variable pointer related restrictions.
|
2019-01-07 18:19:24 +00:00
|
|
|
const auto pointee = _.FindDef(result_type->word(3));
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.addressing_model() == spv::AddressingModel::Logical &&
|
2018-11-27 21:47:10 +00:00
|
|
|
!_.options()->relax_logical_pointer) {
|
|
|
|
// VariablePointersStorageBuffer is implied by VariablePointers.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (pointee->opcode() == spv::Op::OpTypePointer) {
|
|
|
|
if (!_.HasCapability(spv::Capability::VariablePointersStorageBuffer)) {
|
2019-01-17 15:39:12 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "In Logical addressing, variables may not allocate a pointer "
|
|
|
|
<< "type";
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (storage_class != spv::StorageClass::Function &&
|
|
|
|
storage_class != spv::StorageClass::Private) {
|
2018-11-27 21:47:10 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "In Logical addressing with variable pointers, variables "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "that allocate pointers must be in Function or Private "
|
|
|
|
<< "storage classes";
|
2018-11-27 21:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:42:17 +00:00
|
|
|
if (spvIsVulkanEnv(_.context()->target_env)) {
|
2022-03-10 17:57:20 +00:00
|
|
|
// Vulkan Push Constant Interface section: Check type of PushConstant
|
|
|
|
// variables.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::PushConstant) {
|
|
|
|
if (pointee->opcode() != spv::Op::OpTypeStruct) {
|
2018-11-30 18:12:05 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.VkErrorID(6808) << "PushConstant OpVariable <id> "
|
|
|
|
<< _.getIdName(inst->id()) << " has illegal type.\n"
|
2022-03-10 17:57:20 +00:00
|
|
|
<< "From Vulkan spec, Push Constant Interface section:\n"
|
|
|
|
<< "Such variables must be typed as OpTypeStruct";
|
2018-11-30 18:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 17:57:20 +00:00
|
|
|
// Vulkan Descriptor Set Interface: Check type of UniformConstant and
|
|
|
|
// Uniform variables.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::UniformConstant) {
|
2018-11-15 18:42:17 +00:00
|
|
|
if (!IsAllowedTypeOrArrayOfSame(
|
|
|
|
_, pointee,
|
2022-11-04 21:27:10 +00:00
|
|
|
{spv::Op::OpTypeImage, spv::Op::OpTypeSampler,
|
|
|
|
spv::Op::OpTypeSampledImage,
|
|
|
|
spv::Op::OpTypeAccelerationStructureKHR})) {
|
2018-11-14 20:00:03 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.VkErrorID(4655) << "UniformConstant OpVariable <id> "
|
|
|
|
<< _.getIdName(inst->id()) << " has illegal type.\n"
|
2018-11-14 20:00:03 +00:00
|
|
|
<< "Variables identified with the UniformConstant storage class "
|
|
|
|
<< "are used only as handles to refer to opaque resources. Such "
|
|
|
|
<< "variables must be typed as OpTypeImage, OpTypeSampler, "
|
2021-02-05 14:57:00 +00:00
|
|
|
<< "OpTypeSampledImage, OpTypeAccelerationStructureKHR, "
|
2018-11-15 02:50:09 +00:00
|
|
|
<< "or an array of one of these types.";
|
2018-11-15 18:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::Uniform) {
|
|
|
|
if (!IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) {
|
2018-11-15 18:42:17 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.VkErrorID(6807) << "Uniform OpVariable <id> "
|
|
|
|
<< _.getIdName(inst->id()) << " has illegal type.\n"
|
2022-06-15 15:25:16 +00:00
|
|
|
<< "From Vulkan spec:\n"
|
2018-11-15 18:42:17 +00:00
|
|
|
<< "Variables identified with the Uniform storage class are "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "used to access transparent buffer backed resources. Such "
|
|
|
|
<< "variables must be typed as OpTypeStruct, or an array of "
|
|
|
|
<< "this type";
|
2018-11-15 18:42:17 +00:00
|
|
|
}
|
2018-11-14 20:00:03 +00:00
|
|
|
}
|
2019-09-16 14:46:31 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::StorageBuffer) {
|
|
|
|
if (!IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) {
|
2019-09-16 14:46:31 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.VkErrorID(6807) << "StorageBuffer OpVariable <id> "
|
|
|
|
<< _.getIdName(inst->id()) << " has illegal type.\n"
|
2022-06-15 15:25:16 +00:00
|
|
|
<< "From Vulkan spec:\n"
|
2019-09-16 14:46:31 +00:00
|
|
|
<< "Variables identified with the StorageBuffer storage class "
|
|
|
|
"are used to access transparent buffer backed resources. "
|
|
|
|
"Such variables must be typed as OpTypeStruct, or an array "
|
|
|
|
"of this type";
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 14:49:14 +00:00
|
|
|
|
|
|
|
// Check for invalid use of Invariant
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class != spv::StorageClass::Input &&
|
|
|
|
storage_class != spv::StorageClass::Output) {
|
|
|
|
if (_.HasDecoration(inst->id(), spv::Decoration::Invariant)) {
|
2021-02-05 14:49:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< _.VkErrorID(4677)
|
|
|
|
<< "Variable decorated with Invariant must only be identified "
|
|
|
|
"with the Input or Output storage class in Vulkan "
|
|
|
|
"environment.";
|
|
|
|
}
|
|
|
|
// Need to check if only the members in a struct are decorated
|
2022-11-04 21:27:10 +00:00
|
|
|
if (value_type && value_type->opcode() == spv::Op::OpTypeStruct) {
|
|
|
|
if (_.HasDecoration(value_id, spv::Decoration::Invariant)) {
|
2021-02-05 14:49:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< _.VkErrorID(4677)
|
|
|
|
<< "Variable struct member decorated with Invariant must only "
|
|
|
|
"be identified with the Input or Output storage class in "
|
|
|
|
"Vulkan environment.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 20:00:03 +00:00
|
|
|
|
2022-03-04 15:07:24 +00:00
|
|
|
// Initializers in Vulkan are only allowed in some storage clases
|
|
|
|
if (inst->operands().size() > 3) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::Workgroup) {
|
2021-01-28 00:38:09 +00:00
|
|
|
auto init_id = inst->GetOperandAs<uint32_t>(3);
|
|
|
|
auto init = _.FindDef(init_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (init->opcode() != spv::Op::OpConstantNull) {
|
2021-01-28 00:38:09 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.VkErrorID(4734) << "OpVariable, <id> "
|
2022-03-04 15:07:24 +00:00
|
|
|
<< _.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< ", initializers are limited to OpConstantNull in "
|
2022-03-04 15:07:24 +00:00
|
|
|
"Workgroup "
|
|
|
|
"storage class";
|
2021-01-28 00:38:09 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (storage_class != spv::StorageClass::Output &&
|
|
|
|
storage_class != spv::StorageClass::Private &&
|
|
|
|
storage_class != spv::StorageClass::Function) {
|
2021-01-28 00:38:09 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.VkErrorID(4651) << "OpVariable, <id> "
|
2021-01-28 00:38:09 +00:00
|
|
|
<< _.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< ", has a disallowed initializer & storage class "
|
2021-01-28 00:38:09 +00:00
|
|
|
<< "combination.\n"
|
|
|
|
<< "From " << spvLogStringForEnv(_.context()->target_env)
|
|
|
|
<< " spec:\n"
|
|
|
|
<< "Variable declarations that include initializers must have "
|
|
|
|
<< "one of the following storage classes: Output, Private, "
|
|
|
|
<< "Function or Workgroup";
|
|
|
|
}
|
2018-11-22 17:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 15:06:46 +00:00
|
|
|
if (inst->operands().size() > 3) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::TaskPayloadWorkgroupEXT) {
|
2022-09-23 15:06:46 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpVariable, <id> " << _.getIdName(inst->id())
|
|
|
|
<< ", initializer are not allowed for TaskPayloadWorkgroupEXT";
|
2022-09-23 15:06:46 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::Input) {
|
2022-09-23 15:06:46 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpVariable, <id> " << _.getIdName(inst->id())
|
|
|
|
<< ", initializer are not allowed for Input";
|
2022-09-23 15:06:46 +00:00
|
|
|
}
|
2022-11-24 14:50:45 +00:00
|
|
|
if (storage_class == spv::StorageClass::HitObjectAttributeNV) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "OpVariable, <id> " << _.getIdName(inst->id())
|
|
|
|
<< ", initializer are not allowed for HitObjectAttributeNV";
|
|
|
|
}
|
2022-09-23 15:06:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::PhysicalStorageBuffer) {
|
2019-01-07 18:19:24 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-23 15:08:21 +00:00
|
|
|
<< "PhysicalStorageBuffer must not be used with OpVariable.";
|
2019-01-07 18:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pointee_base = pointee;
|
2022-11-04 21:27:10 +00:00
|
|
|
while (pointee_base->opcode() == spv::Op::OpTypeArray) {
|
2019-01-07 18:19:24 +00:00
|
|
|
pointee_base = _.FindDef(pointee_base->GetOperandAs<uint32_t>(1u));
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (pointee_base->opcode() == spv::Op::OpTypePointer) {
|
|
|
|
if (pointee_base->GetOperandAs<spv::StorageClass>(1u) ==
|
|
|
|
spv::StorageClass::PhysicalStorageBuffer) {
|
2022-03-23 15:08:21 +00:00
|
|
|
// check for AliasedPointer/RestrictPointer
|
2019-01-14 21:44:44 +00:00
|
|
|
bool foundAliased =
|
2022-11-04 21:27:10 +00:00
|
|
|
_.HasDecoration(inst->id(), spv::Decoration::AliasedPointer);
|
2019-01-14 21:44:44 +00:00
|
|
|
bool foundRestrict =
|
2022-11-04 21:27:10 +00:00
|
|
|
_.HasDecoration(inst->id(), spv::Decoration::RestrictPointer);
|
2019-01-07 18:19:24 +00:00
|
|
|
if (!foundAliased && !foundRestrict) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "OpVariable " << inst->id()
|
2022-03-23 15:08:21 +00:00
|
|
|
<< ": expected AliasedPointer or RestrictPointer for "
|
|
|
|
<< "PhysicalStorageBuffer pointer.";
|
2019-01-07 18:19:24 +00:00
|
|
|
}
|
|
|
|
if (foundAliased && foundRestrict) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "OpVariable " << inst->id()
|
2022-03-23 15:08:21 +00:00
|
|
|
<< ": can't specify both AliasedPointer and "
|
|
|
|
<< "RestrictPointer for PhysicalStorageBuffer pointer.";
|
2019-01-07 18:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:44:44 +00:00
|
|
|
// Vulkan specific validation rules for OpTypeRuntimeArray
|
|
|
|
if (spvIsVulkanEnv(_.context()->target_env)) {
|
|
|
|
// OpTypeRuntimeArray should only ever be in a container like OpTypeStruct,
|
|
|
|
// so should never appear as a bare variable.
|
|
|
|
// Unless the module has the RuntimeDescriptorArrayEXT capability.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (value_type && value_type->opcode() == spv::Op::OpTypeRuntimeArray) {
|
|
|
|
if (!_.HasCapability(spv::Capability::RuntimeDescriptorArrayEXT)) {
|
2019-01-14 21:44:44 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.VkErrorID(4680) << "OpVariable, <id> "
|
2022-03-10 18:05:26 +00:00
|
|
|
<< _.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< ", is attempting to create memory for an illegal type, "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "OpTypeRuntimeArray.\nFor Vulkan OpTypeRuntimeArray can only "
|
|
|
|
<< "appear as the final member of an OpTypeStruct, thus cannot "
|
|
|
|
<< "be instantiated via OpVariable";
|
2019-01-14 21:44:44 +00:00
|
|
|
} else {
|
|
|
|
// A bare variable OpTypeRuntimeArray is allowed in this context, but
|
|
|
|
// still need to check the storage class.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class != spv::StorageClass::StorageBuffer &&
|
|
|
|
storage_class != spv::StorageClass::Uniform &&
|
|
|
|
storage_class != spv::StorageClass::UniformConstant) {
|
2019-01-14 21:44:44 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-10 18:05:26 +00:00
|
|
|
<< _.VkErrorID(4680)
|
2019-01-14 21:44:44 +00:00
|
|
|
<< "For Vulkan with RuntimeDescriptorArrayEXT, a variable "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "containing OpTypeRuntimeArray must have storage class of "
|
|
|
|
<< "StorageBuffer, Uniform, or UniformConstant.";
|
2019-01-14 21:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an OpStruct has an OpTypeRuntimeArray somewhere within it, then it
|
|
|
|
// must either have the storage class StorageBuffer and be decorated
|
|
|
|
// with Block, or it must be in the Uniform storage class and be decorated
|
|
|
|
// as BufferBlock.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (value_type && value_type->opcode() == spv::Op::OpTypeStruct) {
|
2019-01-17 15:39:12 +00:00
|
|
|
if (DoesStructContainRTA(_, value_type)) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::StorageBuffer ||
|
|
|
|
storage_class == spv::StorageClass::PhysicalStorageBuffer) {
|
|
|
|
if (!_.HasDecoration(value_id, spv::Decoration::Block)) {
|
2019-01-14 21:44:44 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-10 18:05:26 +00:00
|
|
|
<< _.VkErrorID(4680)
|
2019-01-14 21:44:44 +00:00
|
|
|
<< "For Vulkan, an OpTypeStruct variable containing an "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "OpTypeRuntimeArray must be decorated with Block if it "
|
2022-03-10 18:05:26 +00:00
|
|
|
<< "has storage class StorageBuffer or "
|
|
|
|
"PhysicalStorageBuffer.";
|
2019-01-14 21:44:44 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (storage_class == spv::StorageClass::Uniform) {
|
|
|
|
if (!_.HasDecoration(value_id, spv::Decoration::BufferBlock)) {
|
2019-01-14 21:44:44 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-10 18:05:26 +00:00
|
|
|
<< _.VkErrorID(4680)
|
2019-01-14 21:44:44 +00:00
|
|
|
<< "For Vulkan, an OpTypeStruct variable containing an "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "OpTypeRuntimeArray must be decorated with BufferBlock "
|
|
|
|
<< "if it has storage class Uniform.";
|
2019-01-14 21:44:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-10 18:05:26 +00:00
|
|
|
<< _.VkErrorID(4680)
|
2019-01-14 21:44:44 +00:00
|
|
|
<< "For Vulkan, OpTypeStruct variables containing "
|
2019-01-17 15:39:12 +00:00
|
|
|
<< "OpTypeRuntimeArray must have storage class of "
|
2022-03-10 18:05:26 +00:00
|
|
|
<< "StorageBuffer, PhysicalStorageBuffer, or Uniform.";
|
2019-01-17 15:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:43:11 +00:00
|
|
|
// Cooperative matrix types can only be allocated in Function or Private
|
2022-11-04 21:27:10 +00:00
|
|
|
if ((storage_class != spv::StorageClass::Function &&
|
|
|
|
storage_class != spv::StorageClass::Private) &&
|
2019-02-25 22:43:11 +00:00
|
|
|
ContainsCooperativeMatrix(_, pointee)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cooperative matrix types (or types containing them) can only be "
|
|
|
|
"allocated "
|
|
|
|
<< "in Function or Private storage classes or as function "
|
|
|
|
"parameters";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.HasCapability(spv::Capability::Shader)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
// Don't allow variables containing 16-bit elements without the appropriate
|
|
|
|
// capabilities.
|
2022-11-04 21:27:10 +00:00
|
|
|
if ((!_.HasCapability(spv::Capability::Int16) &&
|
|
|
|
_.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 16)) ||
|
|
|
|
(!_.HasCapability(spv::Capability::Float16) &&
|
|
|
|
_.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeFloat, 16))) {
|
2019-07-08 18:10:13 +00:00
|
|
|
auto underlying_type = value_type;
|
2022-11-04 21:27:10 +00:00
|
|
|
while (underlying_type->opcode() == spv::Op::OpTypePointer) {
|
|
|
|
storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u);
|
2019-07-08 18:10:13 +00:00
|
|
|
underlying_type =
|
|
|
|
_.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
|
|
|
|
}
|
|
|
|
bool storage_class_ok = true;
|
|
|
|
std::string sc_name = _.grammar().lookupOperandName(
|
2022-11-04 21:27:10 +00:00
|
|
|
SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class));
|
2019-07-08 18:10:13 +00:00
|
|
|
switch (storage_class) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::StorageBuffer:
|
|
|
|
case spv::StorageClass::PhysicalStorageBuffer:
|
|
|
|
if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::Uniform:
|
2019-07-08 18:10:13 +00:00
|
|
|
if (!_.HasCapability(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Capability::UniformAndStorageBuffer16BitAccess)) {
|
|
|
|
if (underlying_type->opcode() == spv::Op::OpTypeArray ||
|
|
|
|
underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) {
|
2019-07-08 18:10:13 +00:00
|
|
|
underlying_type =
|
|
|
|
_.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess) ||
|
2019-07-08 18:10:13 +00:00
|
|
|
!_.HasDecoration(underlying_type->id(),
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Decoration::BufferBlock)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::PushConstant:
|
|
|
|
if (!_.HasCapability(spv::Capability::StoragePushConstant16)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::Input:
|
|
|
|
case spv::StorageClass::Output:
|
|
|
|
if (!_.HasCapability(spv::Capability::StorageInputOutput16)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::Workgroup:
|
|
|
|
if (!_.HasCapability(
|
|
|
|
spv::Capability::
|
|
|
|
WorkgroupMemoryExplicitLayout16BitAccessKHR)) {
|
2021-01-28 00:38:38 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
break;
|
2019-07-08 18:10:13 +00:00
|
|
|
default:
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cannot allocate a variable containing a 16-bit type in "
|
|
|
|
<< sc_name << " storage class";
|
|
|
|
}
|
|
|
|
if (!storage_class_ok) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Allocating a variable containing a 16-bit element in "
|
|
|
|
<< sc_name << " storage class requires an additional capability";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Don't allow variables containing 8-bit elements without the appropriate
|
|
|
|
// capabilities.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!_.HasCapability(spv::Capability::Int8) &&
|
|
|
|
_.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 8)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
auto underlying_type = value_type;
|
2022-11-04 21:27:10 +00:00
|
|
|
while (underlying_type->opcode() == spv::Op::OpTypePointer) {
|
|
|
|
storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u);
|
2019-07-08 18:10:13 +00:00
|
|
|
underlying_type =
|
|
|
|
_.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
|
|
|
|
}
|
|
|
|
bool storage_class_ok = true;
|
|
|
|
std::string sc_name = _.grammar().lookupOperandName(
|
2022-11-04 21:27:10 +00:00
|
|
|
SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class));
|
2019-07-08 18:10:13 +00:00
|
|
|
switch (storage_class) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::StorageBuffer:
|
|
|
|
case spv::StorageClass::PhysicalStorageBuffer:
|
|
|
|
if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::Uniform:
|
2019-07-08 18:10:13 +00:00
|
|
|
if (!_.HasCapability(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Capability::UniformAndStorageBuffer8BitAccess)) {
|
|
|
|
if (underlying_type->opcode() == spv::Op::OpTypeArray ||
|
|
|
|
underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) {
|
2019-07-08 18:10:13 +00:00
|
|
|
underlying_type =
|
|
|
|
_.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess) ||
|
2019-07-08 18:10:13 +00:00
|
|
|
!_.HasDecoration(underlying_type->id(),
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Decoration::BufferBlock)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::PushConstant:
|
|
|
|
if (!_.HasCapability(spv::Capability::StoragePushConstant8)) {
|
2019-07-08 18:10:13 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::Workgroup:
|
|
|
|
if (!_.HasCapability(
|
|
|
|
spv::Capability::
|
|
|
|
WorkgroupMemoryExplicitLayout8BitAccessKHR)) {
|
2021-01-28 00:38:38 +00:00
|
|
|
storage_class_ok = false;
|
|
|
|
}
|
|
|
|
break;
|
2019-07-08 18:10:13 +00:00
|
|
|
default:
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cannot allocate a variable containing a 8-bit type in "
|
|
|
|
<< sc_name << " storage class";
|
|
|
|
}
|
|
|
|
if (!storage_class_ok) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Allocating a variable containing a 8-bit element in "
|
|
|
|
<< sc_name << " storage class requires an additional capability";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:44:56 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:34:14 +00:00
|
|
|
spv_result_t ValidateLoad(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const auto result_type = _.FindDef(inst->type_id());
|
2018-08-01 18:44:56 +00:00
|
|
|
if (!result_type) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
|
|
|
|
<< " is not defined.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto pointer_index = 2;
|
2018-09-27 16:34:14 +00:00
|
|
|
const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto pointer = _.FindDef(pointer_id);
|
|
|
|
if (!pointer ||
|
2022-11-04 21:27:10 +00:00
|
|
|
((_.addressing_model() == spv::AddressingModel::Logical) &&
|
2022-03-24 16:02:29 +00:00
|
|
|
((!_.features().variable_pointers &&
|
2018-08-01 18:44:56 +00:00
|
|
|
!spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
|
2022-03-24 16:02:29 +00:00
|
|
|
(_.features().variable_pointers &&
|
2018-08-01 18:44:56 +00:00
|
|
|
!spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpLoad Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " is not a logical pointer.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto pointer_type = _.FindDef(pointer->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpLoad type for pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " is not a pointer type.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:09:06 +00:00
|
|
|
uint32_t pointee_data_type;
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass storage_class;
|
2022-08-29 15:09:06 +00:00
|
|
|
if (!_.GetPointerTypeInfo(pointer_type->id(), &pointee_data_type,
|
|
|
|
&storage_class) ||
|
|
|
|
result_type->id() != pointee_data_type) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
|
|
|
|
<< " does not match Pointer <id> " << _.getIdName(pointer->id())
|
|
|
|
<< "s type.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 22:23:10 +00:00
|
|
|
if (!_.options()->before_hlsl_legalization &&
|
|
|
|
_.ContainsRuntimeArray(inst->type_id())) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cannot load a runtime-sized array";
|
|
|
|
}
|
|
|
|
|
2019-01-07 18:19:24 +00:00
|
|
|
if (auto error = CheckMemoryAccess(_, inst, 3)) return error;
|
2018-10-15 13:30:47 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.HasCapability(spv::Capability::Shader) &&
|
2019-07-08 18:10:13 +00:00
|
|
|
_.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
|
2022-11-04 21:27:10 +00:00
|
|
|
result_type->opcode() != spv::Op::OpTypePointer) {
|
|
|
|
if (result_type->opcode() != spv::Op::OpTypeInt &&
|
|
|
|
result_type->opcode() != spv::Op::OpTypeFloat &&
|
|
|
|
result_type->opcode() != spv::Op::OpTypeVector &&
|
|
|
|
result_type->opcode() != spv::Op::OpTypeMatrix) {
|
2019-07-08 18:10:13 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "8- or 16-bit loads must be a scalar, vector or matrix type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:44:56 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:34:14 +00:00
|
|
|
spv_result_t ValidateStore(ValidationState_t& _, const Instruction* inst) {
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto pointer_index = 0;
|
2018-09-27 16:34:14 +00:00
|
|
|
const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto pointer = _.FindDef(pointer_id);
|
|
|
|
if (!pointer ||
|
2022-11-04 21:27:10 +00:00
|
|
|
(_.addressing_model() == spv::AddressingModel::Logical &&
|
2022-03-24 16:02:29 +00:00
|
|
|
((!_.features().variable_pointers &&
|
2018-08-01 18:44:56 +00:00
|
|
|
!spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
|
2022-03-24 16:02:29 +00:00
|
|
|
(_.features().variable_pointers &&
|
2018-08-01 18:44:56 +00:00
|
|
|
!spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " is not a logical pointer.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
const auto pointer_type = _.FindDef(pointer->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore type for pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " is not a pointer type.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
const auto type_id = pointer_type->GetOperandAs<uint32_t>(2);
|
|
|
|
const auto type = _.FindDef(type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!type || spv::Op::OpTypeVoid == type->opcode()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< "s type is void.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate storage class
|
|
|
|
{
|
|
|
|
uint32_t data_type;
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass storage_class;
|
2018-08-01 18:44:56 +00:00
|
|
|
if (!_.GetPointerTypeInfo(pointer_type->id(), &data_type, &storage_class)) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " is not pointer type";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class == spv::StorageClass::UniformConstant ||
|
|
|
|
storage_class == spv::StorageClass::Input ||
|
|
|
|
storage_class == spv::StorageClass::PushConstant) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " storage class is read-only";
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (storage_class == spv::StorageClass::ShaderRecordBufferKHR) {
|
2022-08-29 15:09:06 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "ShaderRecordBufferKHR Storage Class variables are read only";
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (storage_class == spv::StorageClass::HitAttributeKHR) {
|
2022-08-29 15:09:06 +00:00
|
|
|
std::string errorVUID = _.VkErrorID(4703);
|
|
|
|
_.function(inst->function()->id())
|
|
|
|
->RegisterExecutionModelLimitation(
|
2022-11-04 21:27:10 +00:00
|
|
|
[errorVUID](spv::ExecutionModel model, std::string* message) {
|
|
|
|
if (model == spv::ExecutionModel::AnyHitKHR ||
|
|
|
|
model == spv::ExecutionModel::ClosestHitKHR) {
|
2022-08-29 15:09:06 +00:00
|
|
|
if (message) {
|
|
|
|
*message =
|
|
|
|
errorVUID +
|
|
|
|
"HitAttributeKHR Storage Class variables are read only "
|
|
|
|
"with AnyHitKHR and ClosestHitKHR";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
2019-06-17 17:13:07 +00:00
|
|
|
|
|
|
|
if (spvIsVulkanEnv(_.context()->target_env) &&
|
2022-11-04 21:27:10 +00:00
|
|
|
storage_class == spv::StorageClass::Uniform) {
|
2019-06-17 17:13:07 +00:00
|
|
|
auto base_ptr = _.TracePointer(pointer);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (base_ptr->opcode() == spv::Op::OpVariable) {
|
2019-06-17 17:13:07 +00:00
|
|
|
// If it's not a variable a different check should catch the problem.
|
|
|
|
auto base_type = _.FindDef(base_ptr->GetOperandAs<uint32_t>(0));
|
2019-06-20 18:22:41 +00:00
|
|
|
// Get the pointed-to type.
|
|
|
|
base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(2u));
|
2022-11-04 21:27:10 +00:00
|
|
|
if (base_type->opcode() == spv::Op::OpTypeArray ||
|
|
|
|
base_type->opcode() == spv::Op::OpTypeRuntimeArray) {
|
2019-06-20 18:22:41 +00:00
|
|
|
base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(1u));
|
2019-06-17 17:13:07 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.HasDecoration(base_type->id(), spv::Decoration::Block)) {
|
2019-06-17 17:13:07 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-07-13 14:05:52 +00:00
|
|
|
<< _.VkErrorID(6925)
|
2019-06-17 17:13:07 +00:00
|
|
|
<< "In the Vulkan environment, cannot store to Uniform Blocks";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto object_index = 1;
|
2018-09-27 16:34:14 +00:00
|
|
|
const auto object_id = inst->GetOperandAs<uint32_t>(object_index);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto object = _.FindDef(object_id);
|
|
|
|
if (!object || !object->type_id()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Object <id> " << _.getIdName(object_id)
|
|
|
|
<< " is not an object.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
const auto object_type = _.FindDef(object->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!object_type || spv::Op::OpTypeVoid == object_type->opcode()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Object <id> " << _.getIdName(object_id)
|
|
|
|
<< "s type is void.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type->id() != object_type->id()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!_.options()->relax_struct_store ||
|
|
|
|
type->opcode() != spv::Op::OpTypeStruct ||
|
|
|
|
object_type->opcode() != spv::Op::OpTypeStruct) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< "s type does not match Object <id> "
|
|
|
|
<< _.getIdName(object->id()) << "s type.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check for layout compatible matricies and arrays as well.
|
|
|
|
if (!AreLayoutCompatibleStructs(_, type, object_type)) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpStore Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< "s layout does not match Object <id> "
|
|
|
|
<< _.getIdName(object->id()) << "s layout.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-15 13:30:47 +00:00
|
|
|
|
2019-01-07 18:19:24 +00:00
|
|
|
if (auto error = CheckMemoryAccess(_, inst, 2)) return error;
|
2018-10-15 13:30:47 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.HasCapability(spv::Capability::Shader) &&
|
2019-07-08 18:10:13 +00:00
|
|
|
_.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
|
2022-11-04 21:27:10 +00:00
|
|
|
object_type->opcode() != spv::Op::OpTypePointer) {
|
|
|
|
if (object_type->opcode() != spv::Op::OpTypeInt &&
|
|
|
|
object_type->opcode() != spv::Op::OpTypeFloat &&
|
|
|
|
object_type->opcode() != spv::Op::OpTypeVector &&
|
|
|
|
object_type->opcode() != spv::Op::OpTypeMatrix) {
|
2019-07-08 18:10:13 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "8- or 16-bit stores must be a scalar, vector or matrix type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:44:56 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
spv_result_t ValidateCopyMemoryMemoryAccess(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(inst->opcode() == spv::Op::OpCopyMemory ||
|
|
|
|
inst->opcode() == spv::Op::OpCopyMemorySized);
|
|
|
|
const uint32_t first_access_index =
|
|
|
|
inst->opcode() == spv::Op::OpCopyMemory ? 2 : 3;
|
2019-05-07 16:27:18 +00:00
|
|
|
if (inst->operands().size() > first_access_index) {
|
|
|
|
if (auto error = CheckMemoryAccess(_, inst, first_access_index))
|
|
|
|
return error;
|
|
|
|
|
|
|
|
const auto first_access = inst->GetOperandAs<uint32_t>(first_access_index);
|
|
|
|
const uint32_t second_access_index =
|
|
|
|
first_access_index + MemoryAccessNumWords(first_access);
|
|
|
|
if (inst->operands().size() > second_access_index) {
|
|
|
|
if (_.features().copy_memory_permits_two_memory_accesses) {
|
|
|
|
if (auto error = CheckMemoryAccess(_, inst, second_access_index))
|
|
|
|
return error;
|
|
|
|
|
|
|
|
// In the two-access form in SPIR-V 1.4 and later:
|
|
|
|
// - the first is the target (write) access and it can't have
|
|
|
|
// make-visible.
|
|
|
|
// - the second is the source (read) access and it can't have
|
|
|
|
// make-available.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (first_access &
|
|
|
|
uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Target memory access must not include "
|
|
|
|
"MakePointerVisibleKHR";
|
|
|
|
}
|
|
|
|
const auto second_access =
|
|
|
|
inst->GetOperandAs<uint32_t>(second_access_index);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (second_access &
|
|
|
|
uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Source memory access must not include "
|
|
|
|
"MakePointerAvailableKHR";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2022-11-04 21:27:10 +00:00
|
|
|
<< spvOpcodeString(static_cast<spv::Op>(inst->opcode()))
|
2019-05-07 16:27:18 +00:00
|
|
|
<< " with two memory access operands requires SPIR-V 1.4 or "
|
|
|
|
"later";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:34:14 +00:00
|
|
|
spv_result_t ValidateCopyMemory(ValidationState_t& _, const Instruction* inst) {
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto target_index = 0;
|
2018-09-27 16:34:14 +00:00
|
|
|
const auto target_id = inst->GetOperandAs<uint32_t>(target_index);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto target = _.FindDef(target_id);
|
|
|
|
if (!target) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Target operand <id> " << _.getIdName(target_id)
|
|
|
|
<< " is not defined.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto source_index = 1;
|
2018-09-27 16:34:14 +00:00
|
|
|
const auto source_id = inst->GetOperandAs<uint32_t>(source_index);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto source = _.FindDef(source_id);
|
|
|
|
if (!source) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Source operand <id> " << _.getIdName(source_id)
|
|
|
|
<< " is not defined.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto target_pointer_type = _.FindDef(target->type_id());
|
|
|
|
if (!target_pointer_type ||
|
2022-11-04 21:27:10 +00:00
|
|
|
target_pointer_type->opcode() != spv::Op::OpTypePointer) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Target operand <id> " << _.getIdName(target_id)
|
|
|
|
<< " is not a pointer.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
2018-08-03 18:50:14 +00:00
|
|
|
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto source_pointer_type = _.FindDef(source->type_id());
|
|
|
|
if (!source_pointer_type ||
|
2022-11-04 21:27:10 +00:00
|
|
|
source_pointer_type->opcode() != spv::Op::OpTypePointer) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Source operand <id> " << _.getIdName(source_id)
|
|
|
|
<< " is not a pointer.";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
2018-08-03 18:50:14 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpCopyMemory) {
|
2018-08-03 18:50:14 +00:00
|
|
|
const auto target_type =
|
|
|
|
_.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!target_type || target_type->opcode() == spv::Op::OpTypeVoid) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Target operand <id> " << _.getIdName(target_id)
|
|
|
|
<< " cannot be a void pointer.";
|
2018-08-03 18:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto source_type =
|
|
|
|
_.FindDef(source_pointer_type->GetOperandAs<uint32_t>(2));
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!source_type || source_type->opcode() == spv::Op::OpTypeVoid) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Source operand <id> " << _.getIdName(source_id)
|
|
|
|
<< " cannot be a void pointer.";
|
2018-08-03 18:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target_type->id() != source_type->id()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Target <id> " << _.getIdName(source_id)
|
|
|
|
<< "s type does not match Source <id> "
|
|
|
|
<< _.getIdName(source_type->id()) << "s type.";
|
2018-08-03 18:50:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-27 16:34:14 +00:00
|
|
|
const auto size_id = inst->GetOperandAs<uint32_t>(2);
|
2018-08-03 18:50:14 +00:00
|
|
|
const auto size = _.FindDef(size_id);
|
|
|
|
if (!size) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Size operand <id> " << _.getIdName(size_id)
|
|
|
|
<< " is not defined.";
|
2018-08-03 18:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto size_type = _.FindDef(size->type_id());
|
|
|
|
if (!_.IsIntScalarType(size_type->id())) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Size operand <id> " << _.getIdName(size_id)
|
|
|
|
<< " must be a scalar integer type.";
|
2018-08-03 18:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_zero = true;
|
|
|
|
switch (size->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpConstantNull:
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Size operand <id> " << _.getIdName(size_id)
|
|
|
|
<< " cannot be a constant zero.";
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpConstant:
|
2018-08-03 18:50:14 +00:00
|
|
|
if (size_type->word(3) == 1 &&
|
|
|
|
size->word(size->words().size() - 1) & 0x80000000) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Size operand <id> " << _.getIdName(size_id)
|
|
|
|
<< " cannot have the sign bit set to 1.";
|
2018-08-03 18:50:14 +00:00
|
|
|
}
|
|
|
|
for (size_t i = 3; is_zero && i < size->words().size(); ++i) {
|
|
|
|
is_zero &= (size->word(i) == 0);
|
|
|
|
}
|
|
|
|
if (is_zero) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Size operand <id> " << _.getIdName(size_id)
|
|
|
|
<< " cannot be a constant zero.";
|
2018-08-03 18:50:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Cannot infer any other opcodes.
|
|
|
|
break;
|
|
|
|
}
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
2019-07-08 18:10:13 +00:00
|
|
|
if (auto error = ValidateCopyMemoryMemoryAccess(_, inst)) return error;
|
|
|
|
|
|
|
|
// Get past the pointers to avoid checking a pointer copy.
|
|
|
|
auto sub_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
|
2022-11-04 21:27:10 +00:00
|
|
|
while (sub_type->opcode() == spv::Op::OpTypePointer) {
|
2019-07-08 18:10:13 +00:00
|
|
|
sub_type = _.FindDef(sub_type->GetOperandAs<uint32_t>(2));
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.HasCapability(spv::Capability::Shader) &&
|
2019-07-08 18:10:13 +00:00
|
|
|
_.ContainsLimitedUseIntOrFloatType(sub_type->id())) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cannot copy memory of objects containing 8- or 16-bit types";
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidateAccessChain(ValidationState_t& _,
|
2018-09-27 16:34:14 +00:00
|
|
|
const Instruction* inst) {
|
2018-08-01 18:44:56 +00:00
|
|
|
std::string instr_name =
|
2022-11-04 21:27:10 +00:00
|
|
|
"Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
|
2018-08-01 18:44:56 +00:00
|
|
|
|
|
|
|
// The result type must be OpTypePointer.
|
2018-09-27 16:34:14 +00:00
|
|
|
auto result_type = _.FindDef(inst->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (spv::Op::OpTypePointer != result_type->opcode()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The Result Type of " << instr_name << " <id> "
|
|
|
|
<< _.getIdName(inst->id()) << " must be OpTypePointer. Found Op"
|
2022-11-04 21:27:10 +00:00
|
|
|
<< spvOpcodeString(static_cast<spv::Op>(result_type->opcode()))
|
|
|
|
<< ".";
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result type is a pointer. Find out what it's pointing to.
|
|
|
|
// This will be used to make sure the indexing results in the same type.
|
|
|
|
// OpTypePointer word 3 is the type being pointed to.
|
|
|
|
const auto result_type_pointee = _.FindDef(result_type->word(3));
|
|
|
|
|
|
|
|
// Base must be a pointer, pointing to the base of a composite object.
|
|
|
|
const auto base_index = 2;
|
2018-09-27 16:34:14 +00:00
|
|
|
const auto base_id = inst->GetOperandAs<uint32_t>(base_index);
|
2018-08-01 18:44:56 +00:00
|
|
|
const auto base = _.FindDef(base_id);
|
|
|
|
const auto base_type = _.FindDef(base->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!base_type || spv::Op::OpTypePointer != base_type->opcode()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The Base <id> " << _.getIdName(base_id) << " in " << instr_name
|
2018-08-01 18:44:56 +00:00
|
|
|
<< " instruction must be a pointer.";
|
|
|
|
}
|
|
|
|
|
|
|
|
// The result pointer storage class and base pointer storage class must match.
|
|
|
|
// Word 2 of OpTypePointer is the Storage Class.
|
|
|
|
auto result_type_storage_class = result_type->word(2);
|
|
|
|
auto base_type_storage_class = base_type->word(2);
|
|
|
|
if (result_type_storage_class != base_type_storage_class) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2018-08-01 18:44:56 +00:00
|
|
|
<< "The result pointer storage class and base "
|
|
|
|
"pointer storage class in "
|
|
|
|
<< instr_name << " do not match.";
|
|
|
|
}
|
|
|
|
|
|
|
|
// The type pointed to by OpTypePointer (word 3) must be a composite type.
|
|
|
|
auto type_pointee = _.FindDef(base_type->word(3));
|
|
|
|
|
|
|
|
// Check Universal Limit (SPIR-V Spec. Section 2.17).
|
|
|
|
// The number of indexes passed to OpAccessChain may not exceed 255
|
|
|
|
// The instruction includes 4 words + N words (for N indexes)
|
2018-09-27 16:34:14 +00:00
|
|
|
size_t num_indexes = inst->words().size() - 4;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpPtrAccessChain ||
|
|
|
|
inst->opcode() == spv::Op::OpInBoundsPtrAccessChain) {
|
2018-08-01 18:44:56 +00:00
|
|
|
// In pointer access chains, the element operand is required, but not
|
|
|
|
// counted as an index.
|
|
|
|
--num_indexes;
|
|
|
|
}
|
|
|
|
const size_t num_indexes_limit =
|
|
|
|
_.options()->universal_limits_.max_access_chain_indexes;
|
|
|
|
if (num_indexes > num_indexes_limit) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2018-08-01 18:44:56 +00:00
|
|
|
<< "The number of indexes in " << instr_name << " may not exceed "
|
|
|
|
<< num_indexes_limit << ". Found " << num_indexes << " indexes.";
|
|
|
|
}
|
|
|
|
// Indexes walk the type hierarchy to the desired depth, potentially down to
|
|
|
|
// scalar granularity. The first index in Indexes will select the top-level
|
|
|
|
// member/element/component/element of the base composite. All composite
|
|
|
|
// constituents use zero-based numbering, as described by their OpType...
|
|
|
|
// instruction. The second index will apply similarly to that result, and so
|
|
|
|
// on. Once any non-composite type is reached, there must be no remaining
|
|
|
|
// (unused) indexes.
|
|
|
|
auto starting_index = 4;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpPtrAccessChain ||
|
|
|
|
inst->opcode() == spv::Op::OpInBoundsPtrAccessChain) {
|
2018-08-01 18:44:56 +00:00
|
|
|
++starting_index;
|
|
|
|
}
|
2018-09-27 16:34:14 +00:00
|
|
|
for (size_t i = starting_index; i < inst->words().size(); ++i) {
|
|
|
|
const uint32_t cur_word = inst->words()[i];
|
2018-08-01 18:44:56 +00:00
|
|
|
// Earlier ID checks ensure that cur_word definition exists.
|
|
|
|
auto cur_word_instr = _.FindDef(cur_word);
|
|
|
|
// The index must be a scalar integer type (See OpAccessChain in the Spec.)
|
|
|
|
auto index_type = _.FindDef(cur_word_instr->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!index_type || spv::Op::OpTypeInt != index_type->opcode()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2018-08-01 18:44:56 +00:00
|
|
|
<< "Indexes passed to " << instr_name
|
|
|
|
<< " must be of type integer.";
|
|
|
|
}
|
|
|
|
switch (type_pointee->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeMatrix:
|
|
|
|
case spv::Op::OpTypeVector:
|
|
|
|
case spv::Op::OpTypeCooperativeMatrixNV:
|
|
|
|
case spv::Op::OpTypeArray:
|
|
|
|
case spv::Op::OpTypeRuntimeArray: {
|
|
|
|
// In OpTypeMatrix, OpTypeVector, spv::Op::OpTypeCooperativeMatrixNV,
|
2019-02-25 22:43:11 +00:00
|
|
|
// OpTypeArray, and OpTypeRuntimeArray, word 2 is the Element Type.
|
2018-08-01 18:44:56 +00:00
|
|
|
type_pointee = _.FindDef(type_pointee->word(2));
|
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeStruct: {
|
2018-08-01 18:44:56 +00:00
|
|
|
// In case of structures, there is an additional constraint on the
|
|
|
|
// index: the index must be an OpConstant.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (spv::Op::OpConstant != cur_word_instr->opcode()) {
|
2018-08-01 18:44:56 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
|
|
|
|
<< "The <id> passed to " << instr_name
|
|
|
|
<< " to index into a "
|
|
|
|
"structure must be an OpConstant.";
|
|
|
|
}
|
|
|
|
// Get the index value from the OpConstant (word 3 of OpConstant).
|
|
|
|
// OpConstant could be a signed integer. But it's okay to treat it as
|
|
|
|
// unsigned because a negative constant int would never be seen as
|
|
|
|
// correct as a struct offset, since structs can't have more than 2
|
|
|
|
// billion members.
|
|
|
|
const uint32_t cur_index = cur_word_instr->word(3);
|
|
|
|
// The index points to the struct member we want, therefore, the index
|
|
|
|
// should be less than the number of struct members.
|
|
|
|
const uint32_t num_struct_members =
|
|
|
|
static_cast<uint32_t>(type_pointee->words().size() - 2);
|
|
|
|
if (cur_index >= num_struct_members) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
|
|
|
|
<< "Index is out of bounds: " << instr_name
|
|
|
|
<< " can not find index " << cur_index
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " into the structure <id> "
|
|
|
|
<< _.getIdName(type_pointee->id()) << ". This structure has "
|
2018-08-01 18:44:56 +00:00
|
|
|
<< num_struct_members << " members. Largest valid index is "
|
|
|
|
<< num_struct_members - 1 << ".";
|
|
|
|
}
|
|
|
|
// Struct members IDs start at word 2 of OpTypeStruct.
|
|
|
|
auto structMemberId = type_pointee->word(cur_index + 2);
|
|
|
|
type_pointee = _.FindDef(structMemberId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Give an error. reached non-composite type while indexes still remain.
|
2023-01-11 21:58:53 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2018-08-01 18:44:56 +00:00
|
|
|
<< instr_name
|
|
|
|
<< " reached non-composite type while indexes "
|
|
|
|
"still remain to be traversed.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// At this point, we have fully walked down from the base using the indeces.
|
|
|
|
// The type being pointed to should be the same as the result type.
|
|
|
|
if (type_pointee->id() != result_type_pointee->id()) {
|
2018-09-27 16:34:14 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2018-08-01 18:44:56 +00:00
|
|
|
<< instr_name << " result type (Op"
|
2022-11-04 21:27:10 +00:00
|
|
|
<< spvOpcodeString(
|
|
|
|
static_cast<spv::Op>(result_type_pointee->opcode()))
|
2018-08-01 18:44:56 +00:00
|
|
|
<< ") does not match the type that results from indexing into the "
|
|
|
|
"base "
|
|
|
|
"<id> (Op"
|
2022-11-04 21:27:10 +00:00
|
|
|
<< spvOpcodeString(static_cast<spv::Op>(type_pointee->opcode()))
|
2018-08-01 18:44:56 +00:00
|
|
|
<< ").";
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:57:55 +00:00
|
|
|
spv_result_t ValidatePtrAccessChain(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.addressing_model() == spv::AddressingModel::Logical) {
|
2022-03-24 16:02:29 +00:00
|
|
|
if (!_.features().variable_pointers) {
|
2018-10-16 18:57:55 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Generating variable pointers requires capability "
|
|
|
|
<< "VariablePointers or VariablePointersStorageBuffer";
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 16:45:08 +00:00
|
|
|
|
|
|
|
// Need to call first, will make sure Base is a valid ID
|
|
|
|
if (auto error = ValidateAccessChain(_, inst)) return error;
|
|
|
|
|
|
|
|
const auto base_id = inst->GetOperandAs<uint32_t>(2);
|
|
|
|
const auto base = _.FindDef(base_id);
|
|
|
|
const auto base_type = _.FindDef(base->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
const auto base_type_storage_class =
|
|
|
|
base_type->GetOperandAs<spv::StorageClass>(1);
|
|
|
|
|
|
|
|
if (_.HasCapability(spv::Capability::Shader) &&
|
|
|
|
(base_type_storage_class == spv::StorageClass::Uniform ||
|
|
|
|
base_type_storage_class == spv::StorageClass::StorageBuffer ||
|
|
|
|
base_type_storage_class == spv::StorageClass::PhysicalStorageBuffer ||
|
|
|
|
base_type_storage_class == spv::StorageClass::PushConstant ||
|
|
|
|
(_.HasCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR) &&
|
|
|
|
base_type_storage_class == spv::StorageClass::Workgroup)) &&
|
|
|
|
!_.HasDecoration(base_type->id(), spv::Decoration::ArrayStride)) {
|
2022-10-24 16:45:08 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "OpPtrAccessChain must have a Base whose type is decorated "
|
|
|
|
"with ArrayStride";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spvIsVulkanEnv(_.context()->target_env)) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (base_type_storage_class == spv::StorageClass::Workgroup) {
|
|
|
|
if (!_.HasCapability(spv::Capability::VariablePointers)) {
|
2022-10-24 16:45:08 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2022-10-27 14:10:27 +00:00
|
|
|
<< _.VkErrorID(7651)
|
2022-10-24 16:45:08 +00:00
|
|
|
<< "OpPtrAccessChain Base operand pointing to Workgroup "
|
|
|
|
"storage class must use VariablePointers capability";
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (base_type_storage_class == spv::StorageClass::StorageBuffer) {
|
2022-10-24 16:45:08 +00:00
|
|
|
if (!_.features().variable_pointers) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2022-10-27 14:10:27 +00:00
|
|
|
<< _.VkErrorID(7652)
|
2022-10-24 16:45:08 +00:00
|
|
|
<< "OpPtrAccessChain Base operand pointing to StorageBuffer "
|
|
|
|
"storage class must use VariablePointers or "
|
|
|
|
"VariablePointersStorageBuffer capability";
|
|
|
|
}
|
|
|
|
} else if (base_type_storage_class !=
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass::PhysicalStorageBuffer) {
|
2022-10-24 16:45:08 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2022-10-27 14:10:27 +00:00
|
|
|
<< _.VkErrorID(7650)
|
2022-10-24 16:45:08 +00:00
|
|
|
<< "OpPtrAccessChain Base operand must point to Workgroup, "
|
|
|
|
"StorageBuffer, or PhysicalStorageBuffer storage class";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
2018-10-16 18:57:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 00:46:08 +00:00
|
|
|
spv_result_t ValidateArrayLength(ValidationState_t& state,
|
|
|
|
const Instruction* inst) {
|
|
|
|
std::string instr_name =
|
2022-11-04 21:27:10 +00:00
|
|
|
"Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
|
2018-11-27 00:46:08 +00:00
|
|
|
|
|
|
|
// Result type must be a 32-bit unsigned int.
|
|
|
|
auto result_type = state.FindDef(inst->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (result_type->opcode() != spv::Op::OpTypeInt ||
|
2018-11-27 00:46:08 +00:00
|
|
|
result_type->GetOperandAs<uint32_t>(1) != 32 ||
|
|
|
|
result_type->GetOperandAs<uint32_t>(2) != 0) {
|
|
|
|
return state.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The Result Type of " << instr_name << " <id> "
|
2018-11-27 00:46:08 +00:00
|
|
|
<< state.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " must be OpTypeInt with width 32 and signedness 0.";
|
2018-11-27 00:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The structure that is passed in must be an pointer to a structure, whose
|
|
|
|
// last element is a runtime array.
|
|
|
|
auto pointer = state.FindDef(inst->GetOperandAs<uint32_t>(2));
|
|
|
|
auto pointer_type = state.FindDef(pointer->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (pointer_type->opcode() != spv::Op::OpTypePointer) {
|
2018-11-27 00:46:08 +00:00
|
|
|
return state.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The Structure's type in " << instr_name << " <id> "
|
2018-11-27 00:46:08 +00:00
|
|
|
<< state.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " must be a pointer to an OpTypeStruct.";
|
2018-11-27 00:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto structure_type = state.FindDef(pointer_type->GetOperandAs<uint32_t>(2));
|
2022-11-04 21:27:10 +00:00
|
|
|
if (structure_type->opcode() != spv::Op::OpTypeStruct) {
|
2018-11-27 00:46:08 +00:00
|
|
|
return state.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The Structure's type in " << instr_name << " <id> "
|
2018-11-27 00:46:08 +00:00
|
|
|
<< state.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " must be a pointer to an OpTypeStruct.";
|
2018-11-27 00:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto num_of_members = structure_type->operands().size() - 1;
|
|
|
|
auto last_member =
|
|
|
|
state.FindDef(structure_type->GetOperandAs<uint32_t>(num_of_members));
|
2022-11-04 21:27:10 +00:00
|
|
|
if (last_member->opcode() != spv::Op::OpTypeRuntimeArray) {
|
2018-11-27 00:46:08 +00:00
|
|
|
return state.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The Structure's last member in " << instr_name << " <id> "
|
|
|
|
<< state.getIdName(inst->id()) << " must be an OpTypeRuntimeArray.";
|
2018-11-27 00:46:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 00:04:13 +00:00
|
|
|
// The array member must the index of the last element (the run time
|
2018-11-27 00:46:08 +00:00
|
|
|
// array).
|
|
|
|
if (inst->GetOperandAs<uint32_t>(3) != num_of_members - 1) {
|
|
|
|
return state.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The array member in " << instr_name << " <id> "
|
2018-11-27 00:46:08 +00:00
|
|
|
<< state.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " must be an the last member of the struct.";
|
2018-11-27 00:46:08 +00:00
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:43:11 +00:00
|
|
|
spv_result_t ValidateCooperativeMatrixLengthNV(ValidationState_t& state,
|
|
|
|
const Instruction* inst) {
|
|
|
|
std::string instr_name =
|
2022-11-04 21:27:10 +00:00
|
|
|
"Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
|
2019-02-25 22:43:11 +00:00
|
|
|
|
|
|
|
// Result type must be a 32-bit unsigned int.
|
|
|
|
auto result_type = state.FindDef(inst->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (result_type->opcode() != spv::Op::OpTypeInt ||
|
2019-02-25 22:43:11 +00:00
|
|
|
result_type->GetOperandAs<uint32_t>(1) != 32 ||
|
|
|
|
result_type->GetOperandAs<uint32_t>(2) != 0) {
|
|
|
|
return state.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The Result Type of " << instr_name << " <id> "
|
2019-02-25 22:43:11 +00:00
|
|
|
<< state.getIdName(inst->id())
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " must be OpTypeInt with width 32 and signedness 0.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto type_id = inst->GetOperandAs<uint32_t>(2);
|
|
|
|
auto type = state.FindDef(type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) {
|
2019-02-25 22:43:11 +00:00
|
|
|
return state.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "The type in " << instr_name << " <id> "
|
|
|
|
<< state.getIdName(type_id) << " must be OpTypeCooperativeMatrixNV.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidateCooperativeMatrixLoadStoreNV(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
|
|
|
uint32_t type_id;
|
|
|
|
const char* opname;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) {
|
2019-02-25 22:43:11 +00:00
|
|
|
type_id = inst->type_id();
|
2022-11-04 21:27:10 +00:00
|
|
|
opname = "spv::Op::OpCooperativeMatrixLoadNV";
|
2019-02-25 22:43:11 +00:00
|
|
|
} else {
|
|
|
|
// get Object operand's type
|
|
|
|
type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
|
2022-11-04 21:27:10 +00:00
|
|
|
opname = "spv::Op::OpCooperativeMatrixStoreNV";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto matrix_type = _.FindDef(type_id);
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) {
|
|
|
|
if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) {
|
2019-02-25 22:43:11 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-11-04 21:27:10 +00:00
|
|
|
<< "spv::Op::OpCooperativeMatrixLoadNV Result Type <id> "
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.getIdName(type_id) << " is not a cooperative matrix type.";
|
2019-02-25 22:43:11 +00:00
|
|
|
} else {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-11-04 21:27:10 +00:00
|
|
|
<< "spv::Op::OpCooperativeMatrixStoreNV Object type <id> "
|
2022-09-29 08:03:49 +00:00
|
|
|
<< _.getIdName(type_id) << " is not a cooperative matrix type.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto pointer_index =
|
2022-11-04 21:27:10 +00:00
|
|
|
(inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 2u : 0u;
|
2019-02-25 22:43:11 +00:00
|
|
|
const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
|
|
|
|
const auto pointer = _.FindDef(pointer_id);
|
|
|
|
if (!pointer ||
|
2022-11-04 21:27:10 +00:00
|
|
|
((_.addressing_model() == spv::AddressingModel::Logical) &&
|
2022-03-24 16:02:29 +00:00
|
|
|
((!_.features().variable_pointers &&
|
2019-02-25 22:43:11 +00:00
|
|
|
!spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
|
2022-03-24 16:02:29 +00:00
|
|
|
(_.features().variable_pointers &&
|
2019-02-25 22:43:11 +00:00
|
|
|
!spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< opname << " Pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " is not a logical pointer.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto pointer_type_id = pointer->type_id();
|
|
|
|
const auto pointer_type = _.FindDef(pointer_type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
|
2019-02-25 22:43:11 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< opname << " type for pointer <id> " << _.getIdName(pointer_id)
|
|
|
|
<< " is not a pointer type.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto storage_class_index = 1u;
|
|
|
|
const auto storage_class =
|
2022-11-04 21:27:10 +00:00
|
|
|
pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index);
|
2019-02-25 22:43:11 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (storage_class != spv::StorageClass::Workgroup &&
|
|
|
|
storage_class != spv::StorageClass::StorageBuffer &&
|
|
|
|
storage_class != spv::StorageClass::PhysicalStorageBuffer) {
|
2019-02-25 22:43:11 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< opname << " storage class for pointer type <id> "
|
2019-02-25 22:43:11 +00:00
|
|
|
<< _.getIdName(pointer_type_id)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " is not Workgroup or StorageBuffer.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
|
|
|
|
const auto pointee_type = _.FindDef(pointee_id);
|
|
|
|
if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) ||
|
|
|
|
_.IsFloatScalarOrVectorType(pointee_id))) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< opname << " Pointer <id> " << _.getIdName(pointer->id())
|
|
|
|
<< "s Type must be a scalar or vector type.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto stride_index =
|
2022-11-04 21:27:10 +00:00
|
|
|
(inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 3u : 2u;
|
2019-02-25 22:43:11 +00:00
|
|
|
const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index);
|
|
|
|
const auto stride = _.FindDef(stride_id);
|
|
|
|
if (!stride || !_.IsIntScalarType(stride->type_id())) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Stride operand <id> " << _.getIdName(stride_id)
|
|
|
|
<< " must be a scalar integer type.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto colmajor_index =
|
2022-11-04 21:27:10 +00:00
|
|
|
(inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 4u : 3u;
|
2019-02-25 22:43:11 +00:00
|
|
|
const auto colmajor_id = inst->GetOperandAs<uint32_t>(colmajor_index);
|
|
|
|
const auto colmajor = _.FindDef(colmajor_id);
|
|
|
|
if (!colmajor || !_.IsBoolScalarType(colmajor->type_id()) ||
|
|
|
|
!(spvOpcodeIsConstant(colmajor->opcode()) ||
|
|
|
|
spvOpcodeIsSpecConstant(colmajor->opcode()))) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "Column Major operand <id> " << _.getIdName(colmajor_id)
|
|
|
|
<< " must be a boolean constant instruction.";
|
2019-02-25 22:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto memory_access_index =
|
2022-11-04 21:27:10 +00:00
|
|
|
(inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 5u : 4u;
|
2019-02-25 22:43:11 +00:00
|
|
|
if (inst->operands().size() > memory_access_index) {
|
|
|
|
if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
spv_result_t ValidatePtrComparison(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.addressing_model() == spv::AddressingModel::Logical &&
|
2022-03-24 16:02:29 +00:00
|
|
|
!_.features().variable_pointers) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-03-24 16:02:29 +00:00
|
|
|
<< "Instruction cannot for logical addressing model be used without "
|
|
|
|
"a variable pointers capability";
|
2019-05-07 16:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto result_type = _.FindDef(inst->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpPtrDiff) {
|
|
|
|
if (!result_type || result_type->opcode() != spv::Op::OpTypeInt) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Result Type must be an integer scalar";
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!result_type || result_type->opcode() != spv::Op::OpTypeBool) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Result Type must be OpTypeBool";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto op1 = _.FindDef(inst->GetOperandAs<uint32_t>(2u));
|
|
|
|
const auto op2 = _.FindDef(inst->GetOperandAs<uint32_t>(3u));
|
|
|
|
if (!op1 || !op2 || op1->type_id() != op2->type_id()) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "The types of Operand 1 and Operand 2 must match";
|
|
|
|
}
|
|
|
|
const auto op1_type = _.FindDef(op1->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!op1_type || op1_type->opcode() != spv::Op::OpTypePointer) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Operand type must be a pointer";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass sc = op1_type->GetOperandAs<spv::StorageClass>(1u);
|
|
|
|
if (_.addressing_model() == spv::AddressingModel::Logical) {
|
|
|
|
if (sc != spv::StorageClass::Workgroup &&
|
|
|
|
sc != spv::StorageClass::StorageBuffer) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Invalid pointer storage class";
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (sc == spv::StorageClass::Workgroup &&
|
|
|
|
!_.HasCapability(spv::Capability::VariablePointers)) {
|
2019-05-07 16:27:18 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Workgroup storage class pointer requires VariablePointers "
|
|
|
|
"capability to be specified";
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (sc == spv::StorageClass::PhysicalStorageBuffer) {
|
2019-10-03 01:12:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cannot use a pointer in the PhysicalStorageBuffer storage class";
|
2019-05-07 16:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:44:56 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-09-27 16:34:14 +00:00
|
|
|
spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst) {
|
2018-08-02 14:00:52 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpVariable:
|
2018-09-27 16:34:14 +00:00
|
|
|
if (auto error = ValidateVariable(_, inst)) return error;
|
2018-08-02 14:00:52 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLoad:
|
2018-09-27 16:34:14 +00:00
|
|
|
if (auto error = ValidateLoad(_, inst)) return error;
|
2018-08-02 14:00:52 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpStore:
|
2018-09-27 16:34:14 +00:00
|
|
|
if (auto error = ValidateStore(_, inst)) return error;
|
2018-08-02 14:00:52 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCopyMemory:
|
|
|
|
case spv::Op::OpCopyMemorySized:
|
2018-09-27 16:34:14 +00:00
|
|
|
if (auto error = ValidateCopyMemory(_, inst)) return error;
|
2018-08-02 14:00:52 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpPtrAccessChain:
|
2018-10-16 18:57:55 +00:00
|
|
|
if (auto error = ValidatePtrAccessChain(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpAccessChain:
|
|
|
|
case spv::Op::OpInBoundsAccessChain:
|
|
|
|
case spv::Op::OpInBoundsPtrAccessChain:
|
2018-09-27 16:34:14 +00:00
|
|
|
if (auto error = ValidateAccessChain(_, inst)) return error;
|
2018-08-02 14:00:52 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpArrayLength:
|
2018-11-27 00:46:08 +00:00
|
|
|
if (auto error = ValidateArrayLength(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCooperativeMatrixLoadNV:
|
|
|
|
case spv::Op::OpCooperativeMatrixStoreNV:
|
2019-02-25 22:43:11 +00:00
|
|
|
if (auto error = ValidateCooperativeMatrixLoadStoreNV(_, inst))
|
|
|
|
return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCooperativeMatrixLengthNV:
|
2019-02-25 22:43:11 +00:00
|
|
|
if (auto error = ValidateCooperativeMatrixLengthNV(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpPtrEqual:
|
|
|
|
case spv::Op::OpPtrNotEqual:
|
|
|
|
case spv::Op::OpPtrDiff:
|
2019-05-07 16:27:18 +00:00
|
|
|
if (auto error = ValidatePtrComparison(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpImageTexelPointer:
|
|
|
|
case spv::Op::OpGenericPtrMemSemantics:
|
2018-08-02 14:00:52 +00:00
|
|
|
default:
|
|
|
|
break;
|
2018-08-01 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
} // namespace val
|
|
|
|
} // namespace spvtools
|