2017-09-07 21:27:57 +00:00
|
|
|
// Copyright (c) 2017 Google Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Validates correctness of logical SPIR-V instructions.
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/val/instruction.h"
|
2023-03-28 18:18:19 +00:00
|
|
|
#include "source/val/validate.h"
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validation_state.h"
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
// Validates correctness of logical instructions.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t LogicalsPass(ValidationState_t& _, const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
const spv::Op opcode = inst->opcode();
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t result_type = inst->type_id();
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
switch (opcode) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpAny:
|
|
|
|
case spv::Op::OpAll: {
|
2017-09-07 21:27:57 +00:00
|
|
|
if (!_.IsBoolScalarType(result_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected bool scalar type as Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t vector_type = _.GetOperandTypeId(inst, 2);
|
2017-09-07 21:27:57 +00:00
|
|
|
if (!vector_type || !_.IsBoolVectorType(vector_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected operand to be vector bool: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpIsNan:
|
|
|
|
case spv::Op::OpIsInf:
|
|
|
|
case spv::Op::OpIsFinite:
|
|
|
|
case spv::Op::OpIsNormal:
|
|
|
|
case spv::Op::OpSignBitSet: {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected bool scalar or vector type as Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t operand_type = _.GetOperandTypeId(inst, 2);
|
2017-09-07 21:27:57 +00:00
|
|
|
if (!operand_type || (!_.IsFloatScalarType(operand_type) &&
|
2017-11-08 17:40:02 +00:00
|
|
|
!_.IsFloatVectorType(operand_type)))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected operand to be scalar or vector float: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
if (_.GetDimension(result_type) != _.GetDimension(operand_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected vector sizes of Result Type and the operand to be "
|
|
|
|
"equal: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpFOrdEqual:
|
|
|
|
case spv::Op::OpFUnordEqual:
|
|
|
|
case spv::Op::OpFOrdNotEqual:
|
|
|
|
case spv::Op::OpFUnordNotEqual:
|
|
|
|
case spv::Op::OpFOrdLessThan:
|
|
|
|
case spv::Op::OpFUnordLessThan:
|
|
|
|
case spv::Op::OpFOrdGreaterThan:
|
|
|
|
case spv::Op::OpFUnordGreaterThan:
|
|
|
|
case spv::Op::OpFOrdLessThanEqual:
|
|
|
|
case spv::Op::OpFUnordLessThanEqual:
|
|
|
|
case spv::Op::OpFOrdGreaterThanEqual:
|
|
|
|
case spv::Op::OpFUnordGreaterThanEqual:
|
|
|
|
case spv::Op::OpLessOrGreater:
|
|
|
|
case spv::Op::OpOrdered:
|
|
|
|
case spv::Op::OpUnordered: {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected bool scalar or vector type as Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t left_operand_type = _.GetOperandTypeId(inst, 2);
|
2017-09-07 21:27:57 +00:00
|
|
|
if (!left_operand_type || (!_.IsFloatScalarType(left_operand_type) &&
|
|
|
|
!_.IsFloatVectorType(left_operand_type)))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected operands to be scalar or vector float: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
if (_.GetDimension(result_type) != _.GetDimension(left_operand_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected vector sizes of Result Type and the operands to be "
|
|
|
|
"equal: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
if (left_operand_type != _.GetOperandTypeId(inst, 3))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected left and right operands to have the same type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLogicalEqual:
|
|
|
|
case spv::Op::OpLogicalNotEqual:
|
|
|
|
case spv::Op::OpLogicalOr:
|
|
|
|
case spv::Op::OpLogicalAnd: {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected bool scalar or vector type as Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
if (result_type != _.GetOperandTypeId(inst, 2) ||
|
|
|
|
result_type != _.GetOperandTypeId(inst, 3))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected both operands to be of Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLogicalNot: {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected bool scalar or vector type as Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
if (result_type != _.GetOperandTypeId(inst, 2))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected operand to be of Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSelect: {
|
2017-09-07 21:27:57 +00:00
|
|
|
uint32_t dimension = 1;
|
|
|
|
{
|
|
|
|
const Instruction* type_inst = _.FindDef(result_type);
|
|
|
|
assert(type_inst);
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
const auto composites = _.features().select_between_composites;
|
|
|
|
auto fail = [&_, composites, inst, opcode]() -> spv_result_t {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected scalar or "
|
|
|
|
<< (composites ? "composite" : "vector")
|
|
|
|
<< " type as Result Type: " << spvOpcodeString(opcode);
|
|
|
|
};
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
const spv::Op type_opcode = type_inst->opcode();
|
2017-09-07 21:27:57 +00:00
|
|
|
switch (type_opcode) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypePointer: {
|
|
|
|
if (_.addressing_model() == spv::AddressingModel::Logical &&
|
2022-03-24 16:02:29 +00:00
|
|
|
!_.features().variable_pointers)
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Using pointers with OpSelect requires capability "
|
|
|
|
<< "VariablePointers or VariablePointersStorageBuffer";
|
2017-09-07 21:27:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeSampledImage:
|
|
|
|
case spv::Op::OpTypeImage:
|
|
|
|
case spv::Op::OpTypeSampler: {
|
|
|
|
if (!_.HasCapability(spv::Capability::BindlessTextureNV))
|
2022-07-19 18:41:19 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Using image/sampler with OpSelect requires capability "
|
|
|
|
<< "BindlessTextureNV";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeVector: {
|
2017-09-07 21:27:57 +00:00
|
|
|
dimension = type_inst->word(3);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeBool:
|
|
|
|
case spv::Op::OpTypeInt:
|
|
|
|
case spv::Op::OpTypeFloat: {
|
2017-09-07 21:27:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
// Not RuntimeArray because of other rules.
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeArray:
|
|
|
|
case spv::Op::OpTypeMatrix:
|
|
|
|
case spv::Op::OpTypeStruct: {
|
2019-05-07 16:27:18 +00:00
|
|
|
if (!composites) return fail();
|
|
|
|
break;
|
2021-03-09 13:16:43 +00:00
|
|
|
}
|
2019-05-07 16:27:18 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t condition_type = _.GetOperandTypeId(inst, 2);
|
|
|
|
const uint32_t left_type = _.GetOperandTypeId(inst, 3);
|
|
|
|
const uint32_t right_type = _.GetOperandTypeId(inst, 4);
|
|
|
|
|
|
|
|
if (!condition_type || (!_.IsBoolScalarType(condition_type) &&
|
|
|
|
!_.IsBoolVectorType(condition_type)))
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected bool scalar or vector type as condition: "
|
|
|
|
<< spvOpcodeString(opcode);
|
|
|
|
|
|
|
|
if (_.GetDimension(condition_type) != dimension) {
|
|
|
|
// If the condition is a vector type, then the result must also be a
|
|
|
|
// vector with matching dimensions. In SPIR-V 1.4, a scalar condition
|
|
|
|
// can be used to select between vector types. |composites| is a
|
|
|
|
// proxy for SPIR-V 1.4 functionality.
|
|
|
|
if (!composites || _.IsBoolVectorType(condition_type)) {
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2019-05-07 16:27:18 +00:00
|
|
|
<< "Expected vector sizes of Result Type and the condition "
|
|
|
|
"to be equal: "
|
2017-11-08 17:40:02 +00:00
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
if (result_type != left_type || result_type != right_type)
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< "Expected both objects to be of Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2019-05-07 16:27:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-07 21:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpIEqual:
|
|
|
|
case spv::Op::OpINotEqual:
|
|
|
|
case spv::Op::OpUGreaterThan:
|
|
|
|
case spv::Op::OpUGreaterThanEqual:
|
|
|
|
case spv::Op::OpULessThan:
|
|
|
|
case spv::Op::OpULessThanEqual:
|
|
|
|
case spv::Op::OpSGreaterThan:
|
|
|
|
case spv::Op::OpSGreaterThanEqual:
|
|
|
|
case spv::Op::OpSLessThan:
|
|
|
|
case spv::Op::OpSLessThanEqual: {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected bool scalar or vector type as Result Type: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t left_type = _.GetOperandTypeId(inst, 2);
|
|
|
|
const uint32_t right_type = _.GetOperandTypeId(inst, 3);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!left_type ||
|
|
|
|
(!_.IsIntScalarType(left_type) && !_.IsIntVectorType(left_type)))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected operands to be scalar or vector int: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
if (_.GetDimension(result_type) != _.GetDimension(left_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected vector sizes of Result Type and the operands to be"
|
|
|
|
<< " equal: " << spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!right_type ||
|
|
|
|
(!_.IsIntScalarType(right_type) && !_.IsIntVectorType(right_type)))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected operands to be scalar or vector int: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
if (_.GetDimension(result_type) != _.GetDimension(right_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected vector sizes of Result Type and the operands to be"
|
|
|
|
<< " equal: " << spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
if (_.GetBitWidth(left_type) != _.GetBitWidth(right_type))
|
2018-08-01 16:41:57 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "Expected both operands to have the same component bit "
|
|
|
|
"width: "
|
|
|
|
<< spvOpcodeString(opcode);
|
2017-09-07 21:27:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
} // namespace val
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace spvtools
|