2019-06-03 14:55:07 +00:00
|
|
|
// Copyright (c) 2018 Google LLC.
|
|
|
|
// Copyright (c) 2019 NVIDIA Corporation
|
|
|
|
//
|
|
|
|
// 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/val/validate.h"
|
|
|
|
|
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/spirv_target_env.h"
|
|
|
|
#include "source/val/instruction.h"
|
2019-10-03 17:35:35 +00:00
|
|
|
#include "source/val/validate_scopes.h"
|
2019-06-03 14:55:07 +00:00
|
|
|
#include "source/val/validation_state.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace val {
|
2019-07-11 17:05:14 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
spv_result_t ValidateUndef(ValidationState_t& _, const Instruction* inst) {
|
2020-11-27 16:31:04 +00:00
|
|
|
if (_.IsVoidType(inst->type_id())) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cannot create undefined values with void type";
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (_.HasCapability(spv::Capability::Shader) &&
|
2019-07-11 17:05:14 +00:00
|
|
|
_.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
|
|
|
|
!_.IsPointerType(inst->type_id())) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Cannot create undefined values with 8- or 16-bit types";
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-10-03 17:35:35 +00:00
|
|
|
spv_result_t ValidateShaderClock(ValidationState_t& _,
|
|
|
|
const Instruction* inst) {
|
2019-11-07 14:51:38 +00:00
|
|
|
const uint32_t scope = inst->GetOperandAs<uint32_t>(2);
|
|
|
|
if (auto error = ValidateScope(_, inst, scope)) {
|
2019-10-03 17:35:35 +00:00
|
|
|
return error;
|
|
|
|
}
|
2019-11-07 14:51:38 +00:00
|
|
|
|
|
|
|
bool is_int32 = false, is_const_int32 = false;
|
|
|
|
uint32_t value = 0;
|
|
|
|
std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
|
2024-05-21 17:02:17 +00:00
|
|
|
if (is_const_int32) {
|
|
|
|
spv::Scope scope_val{value};
|
|
|
|
if (spvIsVulkanEnv(_.context()->target_env)) {
|
|
|
|
if (scope_val != spv::Scope::Subgroup &&
|
|
|
|
scope_val != spv::Scope::Device) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< _.VkErrorID(4652) << "Scope must be Subgroup or Device";
|
|
|
|
}
|
|
|
|
} else if (spvIsOpenCLEnv(_.context()->target_env)) {
|
|
|
|
if (scope_val != spv::Scope::Workgroup &&
|
|
|
|
scope_val != spv::Scope::Subgroup &&
|
|
|
|
scope_val != spv::Scope::Device) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Scope must be Subgroup, Workgroup, or Device";
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 14:51:38 +00:00
|
|
|
}
|
2019-10-03 17:35:35 +00:00
|
|
|
|
|
|
|
// Result Type must be a 64 - bit unsigned integer type or
|
|
|
|
// a vector of two - components of 32 -
|
|
|
|
// bit unsigned integer type
|
|
|
|
const uint32_t result_type = inst->type_id();
|
2022-07-15 13:55:33 +00:00
|
|
|
if (!_.IsUnsigned64BitHandle(result_type)) {
|
2019-10-03 17:35:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
|
|
|
|
"vector of two components"
|
|
|
|
" of unsigned integer"
|
|
|
|
" or 64bit unsigned integer";
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:51:59 +00:00
|
|
|
spv_result_t ValidateAssumeTrue(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const auto operand_type_id = _.GetOperandTypeId(inst, 0);
|
|
|
|
if (!operand_type_id || !_.IsBoolScalarType(operand_type_id)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Value operand of OpAssumeTrueKHR must be a boolean scalar";
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t ValidateExpect(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const auto result_type = inst->type_id();
|
|
|
|
if (!_.IsBoolScalarOrVectorType(result_type) &&
|
|
|
|
!_.IsIntScalarOrVectorType(result_type)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Result of OpExpectKHR must be a scalar or vector of integer "
|
|
|
|
"type or boolean type";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.GetOperandTypeId(inst, 2) != result_type) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Type of Value operand of OpExpectKHR does not match the result "
|
|
|
|
"type ";
|
|
|
|
}
|
|
|
|
if (_.GetOperandTypeId(inst, 3) != result_type) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Type of ExpectedValue operand of OpExpectKHR does not match the "
|
|
|
|
"result type ";
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:05:14 +00:00
|
|
|
} // namespace
|
2019-06-03 14:55:07 +00:00
|
|
|
|
|
|
|
spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
|
2019-07-11 17:05:14 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpUndef:
|
2019-07-11 17:05:14 +00:00
|
|
|
if (auto error = ValidateUndef(_, inst)) return error;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-06-03 14:55:07 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBeginInvocationInterlockEXT:
|
|
|
|
case spv::Op::OpEndInvocationInterlockEXT:
|
2019-06-03 14:55:07 +00:00
|
|
|
_.function(inst->function()->id())
|
|
|
|
->RegisterExecutionModelLimitation(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::ExecutionModel::Fragment,
|
2019-06-03 14:55:07 +00:00
|
|
|
"OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
|
|
|
|
"require Fragment execution model");
|
|
|
|
|
|
|
|
_.function(inst->function()->id())
|
|
|
|
->RegisterLimitation([](const ValidationState_t& state,
|
|
|
|
const Function* entry_point,
|
|
|
|
std::string* message) {
|
|
|
|
const auto* execution_modes =
|
|
|
|
state.GetExecutionModes(entry_point->id());
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
auto find_interlock = [](const spv::ExecutionMode& mode) {
|
2019-06-03 14:55:07 +00:00
|
|
|
switch (mode) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::ExecutionMode::PixelInterlockOrderedEXT:
|
|
|
|
case spv::ExecutionMode::PixelInterlockUnorderedEXT:
|
|
|
|
case spv::ExecutionMode::SampleInterlockOrderedEXT:
|
|
|
|
case spv::ExecutionMode::SampleInterlockUnorderedEXT:
|
|
|
|
case spv::ExecutionMode::ShadingRateInterlockOrderedEXT:
|
|
|
|
case spv::ExecutionMode::ShadingRateInterlockUnorderedEXT:
|
2019-06-03 14:55:07 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
if (execution_modes) {
|
|
|
|
auto i = std::find_if(execution_modes->begin(),
|
|
|
|
execution_modes->end(), find_interlock);
|
|
|
|
found = (i != execution_modes->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
*message =
|
|
|
|
"OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
|
|
|
|
"require a fragment shader interlock execution mode.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDemoteToHelperInvocationEXT:
|
2019-07-11 14:33:22 +00:00
|
|
|
_.function(inst->function()->id())
|
|
|
|
->RegisterExecutionModelLimitation(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::ExecutionModel::Fragment,
|
2019-07-11 14:33:22 +00:00
|
|
|
"OpDemoteToHelperInvocationEXT requires Fragment execution "
|
|
|
|
"model");
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpIsHelperInvocationEXT: {
|
2019-07-11 14:33:22 +00:00
|
|
|
const uint32_t result_type = inst->type_id();
|
|
|
|
_.function(inst->function()->id())
|
|
|
|
->RegisterExecutionModelLimitation(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::ExecutionModel::Fragment,
|
2019-07-11 14:33:22 +00:00
|
|
|
"OpIsHelperInvocationEXT requires Fragment execution model");
|
|
|
|
if (!_.IsBoolScalarType(result_type))
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected bool scalar type as Result Type: "
|
|
|
|
<< spvOpcodeString(inst->opcode());
|
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpReadClockKHR:
|
2019-10-03 17:35:35 +00:00
|
|
|
if (auto error = ValidateShaderClock(_, inst)) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpAssumeTrueKHR:
|
2021-03-16 13:51:59 +00:00
|
|
|
if (auto error = ValidateAssumeTrue(_, inst)) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpExpectKHR:
|
2021-03-16 13:51:59 +00:00
|
|
|
if (auto error = ValidateExpect(_, inst)) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
break;
|
2019-06-03 14:55:07 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace val
|
|
|
|
} // namespace spvtools
|