2018-08-08 18:21:27 +00:00
|
|
|
// Copyright (c) 2018 Google LLC.
|
|
|
|
//
|
|
|
|
// 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 "source/opcode.h"
|
2018-11-20 21:40:38 +00:00
|
|
|
#include "source/spirv_target_env.h"
|
2018-08-08 18:21:27 +00:00
|
|
|
#include "source/val/instruction.h"
|
2019-07-24 18:43:49 +00:00
|
|
|
#include "source/val/validate.h"
|
2018-08-08 18:21:27 +00:00
|
|
|
#include "source/val/validation_state.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace val {
|
|
|
|
namespace {
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
// Returns true if the decoration takes ID parameters.
|
|
|
|
// TODO(dneto): This can be generated from the grammar.
|
2022-11-04 21:27:10 +00:00
|
|
|
bool DecorationTakesIdParameters(spv::Decoration type) {
|
2021-11-05 17:18:19 +00:00
|
|
|
switch (type) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::UniformId:
|
|
|
|
case spv::Decoration::AlignmentId:
|
|
|
|
case spv::Decoration::MaxByteOffsetId:
|
|
|
|
case spv::Decoration::HlslCounterBufferGOOGLE:
|
2019-05-07 16:27:18 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
bool IsMemberDecorationOnly(spv::Decoration dec) {
|
2021-11-05 17:18:19 +00:00
|
|
|
switch (dec) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::RowMajor:
|
|
|
|
case spv::Decoration::ColMajor:
|
|
|
|
case spv::Decoration::MatrixStride:
|
2021-11-05 17:18:19 +00:00
|
|
|
// SPIR-V spec bug? Offset is generated on variables when dealing with
|
|
|
|
// transform feedback.
|
2022-11-04 21:27:10 +00:00
|
|
|
// case spv::Decoration::Offset:
|
2021-11-05 17:18:19 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
bool IsNotMemberDecoration(spv::Decoration dec) {
|
2021-11-05 17:18:19 +00:00
|
|
|
switch (dec) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::SpecId:
|
|
|
|
case spv::Decoration::Block:
|
|
|
|
case spv::Decoration::BufferBlock:
|
|
|
|
case spv::Decoration::ArrayStride:
|
|
|
|
case spv::Decoration::GLSLShared:
|
|
|
|
case spv::Decoration::GLSLPacked:
|
|
|
|
case spv::Decoration::CPacked:
|
2021-11-05 17:18:19 +00:00
|
|
|
// TODO: https://github.com/KhronosGroup/glslang/issues/703:
|
|
|
|
// glslang applies Restrict to structure members.
|
2022-11-04 21:27:10 +00:00
|
|
|
// case spv::Decoration::Restrict:
|
|
|
|
case spv::Decoration::Aliased:
|
|
|
|
case spv::Decoration::Constant:
|
|
|
|
case spv::Decoration::Uniform:
|
|
|
|
case spv::Decoration::UniformId:
|
|
|
|
case spv::Decoration::SaturatedConversion:
|
|
|
|
case spv::Decoration::Index:
|
|
|
|
case spv::Decoration::Binding:
|
|
|
|
case spv::Decoration::DescriptorSet:
|
|
|
|
case spv::Decoration::FuncParamAttr:
|
|
|
|
case spv::Decoration::FPRoundingMode:
|
|
|
|
case spv::Decoration::FPFastMathMode:
|
|
|
|
case spv::Decoration::LinkageAttributes:
|
|
|
|
case spv::Decoration::NoContraction:
|
|
|
|
case spv::Decoration::InputAttachmentIndex:
|
|
|
|
case spv::Decoration::Alignment:
|
|
|
|
case spv::Decoration::MaxByteOffset:
|
|
|
|
case spv::Decoration::AlignmentId:
|
|
|
|
case spv::Decoration::MaxByteOffsetId:
|
|
|
|
case spv::Decoration::NoSignedWrap:
|
|
|
|
case spv::Decoration::NoUnsignedWrap:
|
|
|
|
case spv::Decoration::NonUniform:
|
|
|
|
case spv::Decoration::RestrictPointer:
|
|
|
|
case spv::Decoration::AliasedPointer:
|
|
|
|
case spv::Decoration::CounterBuffer:
|
2021-11-05 17:18:19 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
spv_result_t ValidateDecorationTarget(ValidationState_t& _, spv::Decoration dec,
|
2021-11-05 17:18:19 +00:00
|
|
|
const Instruction* inst,
|
|
|
|
const Instruction* target) {
|
2021-11-26 17:58:43 +00:00
|
|
|
auto fail = [&_, dec, inst, target](uint32_t vuid) -> DiagnosticStream {
|
2021-11-05 17:18:19 +00:00
|
|
|
DiagnosticStream ds = std::move(
|
|
|
|
_.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-08-29 15:09:20 +00:00
|
|
|
<< _.VkErrorID(vuid) << _.SpvDecorationString(dec)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< " decoration on target <id> " << _.getIdName(target->id()) << " ");
|
2021-11-05 17:18:19 +00:00
|
|
|
return ds;
|
|
|
|
};
|
|
|
|
switch (dec) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::SpecId:
|
2021-11-05 17:18:19 +00:00
|
|
|
if (!spvOpcodeIsScalarSpecConstant(target->opcode())) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be a scalar specialization constant";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::Block:
|
|
|
|
case spv::Decoration::BufferBlock:
|
|
|
|
case spv::Decoration::GLSLShared:
|
|
|
|
case spv::Decoration::GLSLPacked:
|
|
|
|
case spv::Decoration::CPacked:
|
|
|
|
if (target->opcode() != spv::Op::OpTypeStruct) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be a structure type";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::ArrayStride:
|
|
|
|
if (target->opcode() != spv::Op::OpTypeArray &&
|
|
|
|
target->opcode() != spv::Op::OpTypeRuntimeArray &&
|
|
|
|
target->opcode() != spv::Op::OpTypePointer) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be an array or pointer type";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::BuiltIn:
|
|
|
|
if (target->opcode() != spv::Op::OpVariable &&
|
2021-11-05 17:18:19 +00:00
|
|
|
!spvOpcodeIsConstant(target->opcode())) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "BuiltIns can only target variables, structure members or "
|
|
|
|
"constants";
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.HasCapability(spv::Capability::Shader) &&
|
|
|
|
inst->GetOperandAs<spv::BuiltIn>(2) == spv::BuiltIn::WorkgroupSize) {
|
2021-11-05 17:18:19 +00:00
|
|
|
if (!spvOpcodeIsConstant(target->opcode())) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be a constant for WorkgroupSize";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (target->opcode() != spv::Op::OpVariable) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be a variable";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::NoPerspective:
|
|
|
|
case spv::Decoration::Flat:
|
|
|
|
case spv::Decoration::Patch:
|
|
|
|
case spv::Decoration::Centroid:
|
|
|
|
case spv::Decoration::Sample:
|
|
|
|
case spv::Decoration::Restrict:
|
|
|
|
case spv::Decoration::Aliased:
|
|
|
|
case spv::Decoration::Volatile:
|
|
|
|
case spv::Decoration::Coherent:
|
|
|
|
case spv::Decoration::NonWritable:
|
|
|
|
case spv::Decoration::NonReadable:
|
|
|
|
case spv::Decoration::XfbBuffer:
|
|
|
|
case spv::Decoration::XfbStride:
|
|
|
|
case spv::Decoration::Component:
|
|
|
|
case spv::Decoration::Stream:
|
|
|
|
case spv::Decoration::RestrictPointer:
|
|
|
|
case spv::Decoration::AliasedPointer:
|
|
|
|
if (target->opcode() != spv::Op::OpVariable &&
|
|
|
|
target->opcode() != spv::Op::OpFunctionParameter) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be a memory object declaration";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.GetIdOpcode(target->type_id()) != spv::Op::OpTypePointer) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be a pointer type";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::Invariant:
|
|
|
|
case spv::Decoration::Constant:
|
|
|
|
case spv::Decoration::Location:
|
|
|
|
case spv::Decoration::Index:
|
|
|
|
case spv::Decoration::Binding:
|
|
|
|
case spv::Decoration::DescriptorSet:
|
|
|
|
case spv::Decoration::InputAttachmentIndex:
|
|
|
|
if (target->opcode() != spv::Op::OpVariable) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be a variable";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spvIsVulkanEnv(_.context()->target_env)) {
|
|
|
|
// The following were all checked as pointer types above.
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass sc = spv::StorageClass::Uniform;
|
2021-11-05 17:18:19 +00:00
|
|
|
const auto type = _.FindDef(target->type_id());
|
|
|
|
if (type && type->operands().size() > 2) {
|
2022-11-04 21:27:10 +00:00
|
|
|
sc = type->GetOperandAs<spv::StorageClass>(1);
|
2018-08-08 18:21:27 +00:00
|
|
|
}
|
2021-11-05 17:18:19 +00:00
|
|
|
switch (dec) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::Location:
|
|
|
|
case spv::Decoration::Component:
|
2021-11-05 17:18:19 +00:00
|
|
|
// Location is used for input, output and ray tracing stages.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (sc != spv::StorageClass::Input && sc != spv::StorageClass::Output &&
|
|
|
|
sc != spv::StorageClass::RayPayloadKHR &&
|
|
|
|
sc != spv::StorageClass::IncomingRayPayloadKHR &&
|
|
|
|
sc != spv::StorageClass::HitAttributeKHR &&
|
|
|
|
sc != spv::StorageClass::CallableDataKHR &&
|
|
|
|
sc != spv::StorageClass::IncomingCallableDataKHR &&
|
2022-11-24 14:50:45 +00:00
|
|
|
sc != spv::StorageClass::ShaderRecordBufferKHR &&
|
|
|
|
sc != spv::StorageClass::HitObjectAttributeNV) {
|
2021-11-05 17:18:19 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, target)
|
2022-08-29 15:09:20 +00:00
|
|
|
<< _.VkErrorID(6672) << _.SpvDecorationString(dec)
|
2021-11-05 17:18:19 +00:00
|
|
|
<< " decoration must not be applied to this storage class";
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::Index:
|
2022-03-07 17:30:05 +00:00
|
|
|
// Langauge from SPIR-V definition of Index
|
2022-11-04 21:27:10 +00:00
|
|
|
if (sc != spv::StorageClass::Output) {
|
2021-11-26 17:58:43 +00:00
|
|
|
return fail(0) << "must be in the Output storage class";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::Binding:
|
|
|
|
case spv::Decoration::DescriptorSet:
|
|
|
|
if (sc != spv::StorageClass::StorageBuffer &&
|
|
|
|
sc != spv::StorageClass::Uniform &&
|
|
|
|
sc != spv::StorageClass::UniformConstant) {
|
2022-03-07 17:30:05 +00:00
|
|
|
return fail(6491) << "must be in the StorageBuffer, Uniform, or "
|
|
|
|
"UniformConstant storage class";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::InputAttachmentIndex:
|
|
|
|
if (sc != spv::StorageClass::UniformConstant) {
|
2022-03-25 13:29:19 +00:00
|
|
|
return fail(6678) << "must be in the UniformConstant storage class";
|
2021-11-05 17:18:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::Flat:
|
|
|
|
case spv::Decoration::NoPerspective:
|
|
|
|
case spv::Decoration::Centroid:
|
|
|
|
case spv::Decoration::Sample:
|
|
|
|
if (sc != spv::StorageClass::Input && sc != spv::StorageClass::Output) {
|
2021-11-05 17:18:19 +00:00
|
|
|
return fail(4670) << "storage class must be Input or Output";
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Decoration::PerVertexKHR:
|
|
|
|
if (sc != spv::StorageClass::Input) {
|
2022-05-26 17:11:05 +00:00
|
|
|
return fail(6777) << "storage class must be Input";
|
|
|
|
}
|
|
|
|
break;
|
2021-11-05 17:18:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidateDecorate(ValidationState_t& _, const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
const auto decoration = inst->GetOperandAs<spv::Decoration>(1);
|
2021-11-05 17:18:19 +00:00
|
|
|
const auto target_id = inst->GetOperandAs<uint32_t>(0);
|
|
|
|
const auto target = _.FindDef(target_id);
|
|
|
|
if (!target) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst) << "target is not defined";
|
2018-08-08 18:21:27 +00:00
|
|
|
}
|
2019-01-31 21:25:46 +00:00
|
|
|
|
2021-01-06 19:30:56 +00:00
|
|
|
if (spvIsVulkanEnv(_.context()->target_env)) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if ((decoration == spv::Decoration::GLSLShared) ||
|
|
|
|
(decoration == spv::Decoration::GLSLPacked)) {
|
2021-01-06 19:30:56 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< _.VkErrorID(4669) << "OpDecorate decoration '"
|
2022-08-29 15:09:20 +00:00
|
|
|
<< _.SpvDecorationString(decoration)
|
2021-01-06 19:30:56 +00:00
|
|
|
<< "' is not valid for the Vulkan execution environment.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
if (DecorationTakesIdParameters(decoration)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Decorations taking ID parameters may not be used with "
|
|
|
|
"OpDecorateId";
|
|
|
|
}
|
2021-11-05 17:18:19 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (target->opcode() != spv::Op::OpDecorationGroup) {
|
2021-11-05 17:18:19 +00:00
|
|
|
if (IsMemberDecorationOnly(decoration)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-08-29 15:09:20 +00:00
|
|
|
<< _.SpvDecorationString(decoration)
|
2021-11-05 17:18:19 +00:00
|
|
|
<< " can only be applied to structure members";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto error = ValidateDecorationTarget(_, decoration, inst, target)) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:21:27 +00:00
|
|
|
// TODO: Add validations for all decorations.
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
spv_result_t ValidateDecorateId(ValidationState_t& _, const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
const auto decoration = inst->GetOperandAs<spv::Decoration>(1);
|
2019-05-07 16:27:18 +00:00
|
|
|
if (!DecorationTakesIdParameters(decoration)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Decorations that don't take ID parameters may not be used with "
|
|
|
|
"OpDecorateId";
|
|
|
|
}
|
2021-11-05 17:18:19 +00:00
|
|
|
|
|
|
|
// No member decorations take id parameters, so we don't bother checking if
|
|
|
|
// we are using a member only decoration here.
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
// TODO: Add validations for these decorations.
|
|
|
|
// UniformId is covered elsewhere.
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:21:27 +00:00
|
|
|
spv_result_t ValidateMemberDecorate(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
|
|
|
const auto struct_type_id = inst->GetOperandAs<uint32_t>(0);
|
|
|
|
const auto struct_type = _.FindDef(struct_type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!struct_type || spv::Op::OpTypeStruct != struct_type->opcode()) {
|
2018-08-08 18:21:27 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpMemberDecorate Structure type <id> "
|
|
|
|
<< _.getIdName(struct_type_id) << " is not a struct type.";
|
2018-08-08 18:21:27 +00:00
|
|
|
}
|
|
|
|
const auto member = inst->GetOperandAs<uint32_t>(1);
|
|
|
|
const auto member_count =
|
|
|
|
static_cast<uint32_t>(struct_type->words().size() - 2);
|
2018-09-17 21:22:31 +00:00
|
|
|
if (member_count <= member) {
|
2018-08-08 18:21:27 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Index " << member
|
|
|
|
<< " provided in OpMemberDecorate for struct <id> "
|
|
|
|
<< _.getIdName(struct_type_id)
|
|
|
|
<< " is out of bounds. The structure has " << member_count
|
|
|
|
<< " members. Largest valid index is " << member_count - 1 << ".";
|
|
|
|
}
|
2019-01-31 21:25:46 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
const auto decoration = inst->GetOperandAs<spv::Decoration>(2);
|
2021-11-05 17:18:19 +00:00
|
|
|
if (IsNotMemberDecoration(decoration)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-08-29 15:09:20 +00:00
|
|
|
<< _.SpvDecorationString(decoration)
|
2021-11-05 17:18:19 +00:00
|
|
|
<< " cannot be applied to structure members";
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:21:27 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidateDecorationGroup(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
|
|
|
const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
|
|
|
|
const auto decoration_group = _.FindDef(decoration_group_id);
|
|
|
|
for (auto pair : decoration_group->uses()) {
|
|
|
|
auto use = pair.first;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (use->opcode() != spv::Op::OpDecorate &&
|
|
|
|
use->opcode() != spv::Op::OpGroupDecorate &&
|
|
|
|
use->opcode() != spv::Op::OpGroupMemberDecorate &&
|
|
|
|
use->opcode() != spv::Op::OpName &&
|
|
|
|
use->opcode() != spv::Op::OpDecorateId && !use->IsNonSemantic()) {
|
2018-08-08 18:21:27 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Result id of OpDecorationGroup can only "
|
|
|
|
<< "be targeted by OpName, OpGroupDecorate, "
|
2019-07-24 18:43:49 +00:00
|
|
|
<< "OpDecorate, OpDecorateId, and OpGroupMemberDecorate";
|
2018-08-08 18:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidateGroupDecorate(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
|
|
|
const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
|
|
|
|
auto decoration_group = _.FindDef(decoration_group_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!decoration_group ||
|
|
|
|
spv::Op::OpDecorationGroup != decoration_group->opcode()) {
|
2018-08-08 18:21:27 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpGroupDecorate Decoration group <id> "
|
|
|
|
<< _.getIdName(decoration_group_id) << " is not a decoration group.";
|
2018-08-08 18:21:27 +00:00
|
|
|
}
|
2018-10-17 17:45:05 +00:00
|
|
|
for (unsigned i = 1; i < inst->operands().size(); ++i) {
|
|
|
|
auto target_id = inst->GetOperandAs<uint32_t>(i);
|
|
|
|
auto target = _.FindDef(target_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!target || target->opcode() == spv::Op::OpDecorationGroup) {
|
2018-10-17 17:45:05 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpGroupDecorate may not target OpDecorationGroup <id> "
|
|
|
|
<< _.getIdName(target_id);
|
2018-10-17 17:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-08 18:21:27 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidateGroupMemberDecorate(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
|
|
|
const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
|
|
|
|
const auto decoration_group = _.FindDef(decoration_group_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!decoration_group ||
|
|
|
|
spv::Op::OpDecorationGroup != decoration_group->opcode()) {
|
2018-08-08 18:21:27 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpGroupMemberDecorate Decoration group <id> "
|
|
|
|
<< _.getIdName(decoration_group_id) << " is not a decoration group.";
|
2018-08-08 18:21:27 +00:00
|
|
|
}
|
|
|
|
// Grammar checks ensures that the number of arguments to this instruction
|
|
|
|
// is an odd number: 1 decoration group + (id,literal) pairs.
|
|
|
|
for (size_t i = 1; i + 1 < inst->operands().size(); i += 2) {
|
|
|
|
const uint32_t struct_id = inst->GetOperandAs<uint32_t>(i);
|
|
|
|
const uint32_t index = inst->GetOperandAs<uint32_t>(i + 1);
|
|
|
|
auto struct_instr = _.FindDef(struct_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!struct_instr || spv::Op::OpTypeStruct != struct_instr->opcode()) {
|
2018-08-08 18:21:27 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
2022-09-29 08:03:49 +00:00
|
|
|
<< "OpGroupMemberDecorate Structure type <id> "
|
|
|
|
<< _.getIdName(struct_id) << " is not a struct type.";
|
2018-08-08 18:21:27 +00:00
|
|
|
}
|
|
|
|
const uint32_t num_struct_members =
|
|
|
|
static_cast<uint32_t>(struct_instr->words().size() - 2);
|
|
|
|
if (index >= num_struct_members) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Index " << index
|
|
|
|
<< " provided in OpGroupMemberDecorate for struct <id> "
|
|
|
|
<< _.getIdName(struct_id)
|
|
|
|
<< " is out of bounds. The structure has " << num_struct_members
|
|
|
|
<< " members. Largest valid index is " << num_struct_members - 1
|
|
|
|
<< ".";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-09-18 12:53:09 +00:00
|
|
|
// Registers necessary decoration(s) for the appropriate IDs based on the
|
|
|
|
// instruction.
|
|
|
|
spv_result_t RegisterDecorations(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
|
|
|
case spv::Op::OpDecorateId: {
|
2018-09-18 12:53:09 +00:00
|
|
|
const uint32_t target_id = inst->word(1);
|
2022-11-04 21:27:10 +00:00
|
|
|
const spv::Decoration dec_type =
|
|
|
|
static_cast<spv::Decoration>(inst->word(2));
|
2018-09-18 12:53:09 +00:00
|
|
|
std::vector<uint32_t> dec_params;
|
|
|
|
if (inst->words().size() > 3) {
|
|
|
|
dec_params.insert(dec_params.end(), inst->words().begin() + 3,
|
|
|
|
inst->words().end());
|
|
|
|
}
|
|
|
|
_.RegisterDecorationForId(target_id, Decoration(dec_type, dec_params));
|
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpMemberDecorate: {
|
2018-09-18 12:53:09 +00:00
|
|
|
const uint32_t struct_id = inst->word(1);
|
|
|
|
const uint32_t index = inst->word(2);
|
2022-11-04 21:27:10 +00:00
|
|
|
const spv::Decoration dec_type =
|
|
|
|
static_cast<spv::Decoration>(inst->word(3));
|
2018-09-18 12:53:09 +00:00
|
|
|
std::vector<uint32_t> dec_params;
|
|
|
|
if (inst->words().size() > 4) {
|
|
|
|
dec_params.insert(dec_params.end(), inst->words().begin() + 4,
|
|
|
|
inst->words().end());
|
|
|
|
}
|
|
|
|
_.RegisterDecorationForId(struct_id,
|
|
|
|
Decoration(dec_type, dec_params, index));
|
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorationGroup: {
|
2018-09-18 12:53:09 +00:00
|
|
|
// We don't need to do anything right now. Assigning decorations to groups
|
|
|
|
// will be taken care of via OpGroupDecorate.
|
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupDecorate: {
|
2018-09-18 12:53:09 +00:00
|
|
|
// Word 1 is the group <id>. All subsequent words are target <id>s that
|
|
|
|
// are going to be decorated with the decorations.
|
|
|
|
const uint32_t decoration_group_id = inst->word(1);
|
2022-06-02 15:32:38 +00:00
|
|
|
std::set<Decoration>& group_decorations =
|
2018-09-18 12:53:09 +00:00
|
|
|
_.id_decorations(decoration_group_id);
|
|
|
|
for (size_t i = 2; i < inst->words().size(); ++i) {
|
|
|
|
const uint32_t target_id = inst->word(i);
|
|
|
|
_.RegisterDecorationsForId(target_id, group_decorations.begin(),
|
|
|
|
group_decorations.end());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupMemberDecorate: {
|
2018-09-18 12:53:09 +00:00
|
|
|
// Word 1 is the Decoration Group <id> followed by (struct<id>,literal)
|
|
|
|
// pairs. All decorations of the group should be applied to all the struct
|
|
|
|
// members that are specified in the instructions.
|
|
|
|
const uint32_t decoration_group_id = inst->word(1);
|
2022-06-02 15:32:38 +00:00
|
|
|
std::set<Decoration>& group_decorations =
|
2018-09-18 12:53:09 +00:00
|
|
|
_.id_decorations(decoration_group_id);
|
|
|
|
// Grammar checks ensures that the number of arguments to this instruction
|
|
|
|
// is an odd number: 1 decoration group + (id,literal) pairs.
|
|
|
|
for (size_t i = 2; i + 1 < inst->words().size(); i = i + 2) {
|
|
|
|
const uint32_t struct_id = inst->word(i);
|
|
|
|
const uint32_t index = inst->word(i + 1);
|
|
|
|
// ID validation phase ensures this is in fact a struct instruction and
|
|
|
|
// that the index is not out of bound.
|
|
|
|
_.RegisterDecorationsForStructMember(struct_id, index,
|
|
|
|
group_decorations.begin(),
|
|
|
|
group_decorations.end());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:21:27 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
spv_result_t AnnotationPass(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
2018-08-08 18:21:27 +00:00
|
|
|
if (auto error = ValidateDecorate(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorateId:
|
2019-05-07 16:27:18 +00:00
|
|
|
if (auto error = ValidateDecorateId(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
// TODO(dneto): spv::Op::OpDecorateStringGOOGLE
|
2019-05-07 16:27:18 +00:00
|
|
|
// See https://github.com/KhronosGroup/SPIRV-Tools/issues/2253
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpMemberDecorate:
|
2018-08-08 18:21:27 +00:00
|
|
|
if (auto error = ValidateMemberDecorate(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorationGroup:
|
2018-08-08 18:21:27 +00:00
|
|
|
if (auto error = ValidateDecorationGroup(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupDecorate:
|
2018-08-08 18:21:27 +00:00
|
|
|
if (auto error = ValidateGroupDecorate(_, inst)) return error;
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupMemberDecorate:
|
2018-08-08 18:21:27 +00:00
|
|
|
if (auto error = ValidateGroupMemberDecorate(_, inst)) return error;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-09-18 12:53:09 +00:00
|
|
|
// In order to validate decoration rules, we need to know all the decorations
|
|
|
|
// that are applied to any given <id>.
|
|
|
|
RegisterDecorations(_, inst);
|
|
|
|
|
2018-08-08 18:21:27 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace val
|
|
|
|
} // namespace spvtools
|