123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- // 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 conversion instructions.
- #include "source/val/validate.h"
- #include "source/diagnostic.h"
- #include "source/opcode.h"
- #include "source/val/instruction.h"
- #include "source/val/validation_state.h"
- namespace spvtools {
- namespace val {
- // Validates correctness of conversion instructions.
- spv_result_t ConversionPass(ValidationState_t& _, const Instruction* inst) {
- const SpvOp opcode = inst->opcode();
- const uint32_t result_type = inst->type_id();
- switch (opcode) {
- case SpvOpConvertFToU: {
- if (!_.IsUnsignedIntScalarType(result_type) &&
- !_.IsUnsignedIntVectorType(result_type) &&
- !_.IsUnsignedIntCooperativeMatrixType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected unsigned int scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type || (!_.IsFloatScalarType(input_type) &&
- !_.IsFloatVectorType(input_type) &&
- !_.IsFloatCooperativeMatrixType(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be float scalar or vector: "
- << spvOpcodeString(opcode);
- if (_.IsCooperativeMatrixType(result_type) ||
- _.IsCooperativeMatrixType(input_type)) {
- spv_result_t ret =
- _.CooperativeMatrixShapesMatch(inst, result_type, input_type);
- if (ret != SPV_SUCCESS) return ret;
- } else {
- if (_.GetDimension(result_type) != _.GetDimension(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same dimension as Result Type: "
- << spvOpcodeString(opcode);
- }
- if (!_.features().use_int8_type && (8 == _.GetBitWidth(result_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Invalid cast to 8-bit integer from a floating-point: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpConvertFToS: {
- if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
- !_.IsIntCooperativeMatrixType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected int scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type || (!_.IsFloatScalarType(input_type) &&
- !_.IsFloatVectorType(input_type) &&
- !_.IsFloatCooperativeMatrixType(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be float scalar or vector: "
- << spvOpcodeString(opcode);
- if (_.IsCooperativeMatrixType(result_type) ||
- _.IsCooperativeMatrixType(input_type)) {
- spv_result_t ret =
- _.CooperativeMatrixShapesMatch(inst, result_type, input_type);
- if (ret != SPV_SUCCESS) return ret;
- } else {
- if (_.GetDimension(result_type) != _.GetDimension(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same dimension as Result Type: "
- << spvOpcodeString(opcode);
- }
- if (!_.features().use_int8_type && (8 == _.GetBitWidth(result_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Invalid cast to 8-bit integer from a floating-point: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpConvertSToF:
- case SpvOpConvertUToF: {
- if (!_.IsFloatScalarType(result_type) &&
- !_.IsFloatVectorType(result_type) &&
- !_.IsFloatCooperativeMatrixType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected float scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type ||
- (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type) &&
- !_.IsIntCooperativeMatrixType(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be int scalar or vector: "
- << spvOpcodeString(opcode);
- if (_.IsCooperativeMatrixType(result_type) ||
- _.IsCooperativeMatrixType(input_type)) {
- spv_result_t ret =
- _.CooperativeMatrixShapesMatch(inst, result_type, input_type);
- if (ret != SPV_SUCCESS) return ret;
- } else {
- if (_.GetDimension(result_type) != _.GetDimension(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same dimension as Result Type: "
- << spvOpcodeString(opcode);
- }
- if (!_.features().use_int8_type && (8 == _.GetBitWidth(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Invalid cast to floating-point from an 8-bit integer: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpUConvert: {
- if (!_.IsUnsignedIntScalarType(result_type) &&
- !_.IsUnsignedIntVectorType(result_type) &&
- !_.IsUnsignedIntCooperativeMatrixType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected unsigned int scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type ||
- (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type) &&
- !_.IsIntCooperativeMatrixType(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be int scalar or vector: "
- << spvOpcodeString(opcode);
- if (_.IsCooperativeMatrixType(result_type) ||
- _.IsCooperativeMatrixType(input_type)) {
- spv_result_t ret =
- _.CooperativeMatrixShapesMatch(inst, result_type, input_type);
- if (ret != SPV_SUCCESS) return ret;
- } else {
- if (_.GetDimension(result_type) != _.GetDimension(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same dimension as Result Type: "
- << spvOpcodeString(opcode);
- }
- if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have different bit width from Result "
- "Type: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpSConvert: {
- if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
- !_.IsIntCooperativeMatrixType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected int scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type ||
- (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type) &&
- !_.IsIntCooperativeMatrixType(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be int scalar or vector: "
- << spvOpcodeString(opcode);
- if (_.IsCooperativeMatrixType(result_type) ||
- _.IsCooperativeMatrixType(input_type)) {
- spv_result_t ret =
- _.CooperativeMatrixShapesMatch(inst, result_type, input_type);
- if (ret != SPV_SUCCESS) return ret;
- } else {
- if (_.GetDimension(result_type) != _.GetDimension(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same dimension as Result Type: "
- << spvOpcodeString(opcode);
- }
- if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have different bit width from Result "
- "Type: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpFConvert: {
- if (!_.IsFloatScalarType(result_type) &&
- !_.IsFloatVectorType(result_type) &&
- !_.IsFloatCooperativeMatrixType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected float scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type || (!_.IsFloatScalarType(input_type) &&
- !_.IsFloatVectorType(input_type) &&
- !_.IsFloatCooperativeMatrixType(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be float scalar or vector: "
- << spvOpcodeString(opcode);
- if (_.IsCooperativeMatrixType(result_type) ||
- _.IsCooperativeMatrixType(input_type)) {
- spv_result_t ret =
- _.CooperativeMatrixShapesMatch(inst, result_type, input_type);
- if (ret != SPV_SUCCESS) return ret;
- } else {
- if (_.GetDimension(result_type) != _.GetDimension(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same dimension as Result Type: "
- << spvOpcodeString(opcode);
- }
- if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have different bit width from Result "
- "Type: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpQuantizeToF16: {
- if ((!_.IsFloatScalarType(result_type) &&
- !_.IsFloatVectorType(result_type)) ||
- _.GetBitWidth(result_type) != 32)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected 32-bit float scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (input_type != result_type)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input type to be equal to Result Type: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpConvertPtrToU: {
- if (!_.IsUnsignedIntScalarType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected unsigned int scalar type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!_.IsPointerType(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be a pointer: " << spvOpcodeString(opcode);
- if (_.addressing_model() == SpvAddressingModelLogical)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Logical addressing not supported: "
- << spvOpcodeString(opcode);
- if (_.addressing_model() ==
- SpvAddressingModelPhysicalStorageBuffer64EXT) {
- uint32_t input_storage_class = 0;
- uint32_t input_data_type = 0;
- _.GetPointerTypeInfo(input_type, &input_data_type,
- &input_storage_class);
- if (input_storage_class != SpvStorageClassPhysicalStorageBufferEXT)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Pointer storage class must be PhysicalStorageBufferEXT: "
- << spvOpcodeString(opcode);
- }
- break;
- }
- case SpvOpSatConvertSToU:
- case SpvOpSatConvertUToS: {
- if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected int scalar or vector type as Result Type: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type ||
- (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type)))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected int scalar or vector as input: "
- << spvOpcodeString(opcode);
- if (_.GetDimension(result_type) != _.GetDimension(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same dimension as Result Type: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpConvertUToPtr: {
- if (!_.IsPointerType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to be a pointer: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type || !_.IsIntScalarType(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected int scalar as input: " << spvOpcodeString(opcode);
- if (_.addressing_model() == SpvAddressingModelLogical)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Logical addressing not supported: "
- << spvOpcodeString(opcode);
- if (_.addressing_model() ==
- SpvAddressingModelPhysicalStorageBuffer64EXT) {
- uint32_t result_storage_class = 0;
- uint32_t result_data_type = 0;
- _.GetPointerTypeInfo(result_type, &result_data_type,
- &result_storage_class);
- if (result_storage_class != SpvStorageClassPhysicalStorageBufferEXT)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Pointer storage class must be PhysicalStorageBufferEXT: "
- << spvOpcodeString(opcode);
- }
- break;
- }
- case SpvOpPtrCastToGeneric: {
- uint32_t result_storage_class = 0;
- uint32_t result_data_type = 0;
- if (!_.GetPointerTypeInfo(result_type, &result_data_type,
- &result_storage_class))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to be a pointer: "
- << spvOpcodeString(opcode);
- if (result_storage_class != SpvStorageClassGeneric)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to have storage class Generic: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- uint32_t input_storage_class = 0;
- uint32_t input_data_type = 0;
- if (!_.GetPointerTypeInfo(input_type, &input_data_type,
- &input_storage_class))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be a pointer: " << spvOpcodeString(opcode);
- if (input_storage_class != SpvStorageClassWorkgroup &&
- input_storage_class != SpvStorageClassCrossWorkgroup &&
- input_storage_class != SpvStorageClassFunction)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have storage class Workgroup, "
- << "CrossWorkgroup or Function: " << spvOpcodeString(opcode);
- if (result_data_type != input_data_type)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input and Result Type to point to the same type: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpGenericCastToPtr: {
- uint32_t result_storage_class = 0;
- uint32_t result_data_type = 0;
- if (!_.GetPointerTypeInfo(result_type, &result_data_type,
- &result_storage_class))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to be a pointer: "
- << spvOpcodeString(opcode);
- if (result_storage_class != SpvStorageClassWorkgroup &&
- result_storage_class != SpvStorageClassCrossWorkgroup &&
- result_storage_class != SpvStorageClassFunction)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to have storage class Workgroup, "
- << "CrossWorkgroup or Function: " << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- uint32_t input_storage_class = 0;
- uint32_t input_data_type = 0;
- if (!_.GetPointerTypeInfo(input_type, &input_data_type,
- &input_storage_class))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be a pointer: " << spvOpcodeString(opcode);
- if (input_storage_class != SpvStorageClassGeneric)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have storage class Generic: "
- << spvOpcodeString(opcode);
- if (result_data_type != input_data_type)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input and Result Type to point to the same type: "
- << spvOpcodeString(opcode);
- break;
- }
- case SpvOpGenericCastToPtrExplicit: {
- uint32_t result_storage_class = 0;
- uint32_t result_data_type = 0;
- if (!_.GetPointerTypeInfo(result_type, &result_data_type,
- &result_storage_class))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to be a pointer: "
- << spvOpcodeString(opcode);
- const uint32_t target_storage_class = inst->word(4);
- if (result_storage_class != target_storage_class)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to be of target storage class: "
- << spvOpcodeString(opcode);
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- uint32_t input_storage_class = 0;
- uint32_t input_data_type = 0;
- if (!_.GetPointerTypeInfo(input_type, &input_data_type,
- &input_storage_class))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be a pointer: " << spvOpcodeString(opcode);
- if (input_storage_class != SpvStorageClassGeneric)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have storage class Generic: "
- << spvOpcodeString(opcode);
- if (result_data_type != input_data_type)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input and Result Type to point to the same type: "
- << spvOpcodeString(opcode);
- if (target_storage_class != SpvStorageClassWorkgroup &&
- target_storage_class != SpvStorageClassCrossWorkgroup &&
- target_storage_class != SpvStorageClassFunction)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected target storage class to be Workgroup, "
- << "CrossWorkgroup or Function: " << spvOpcodeString(opcode);
- break;
- }
- case SpvOpBitcast: {
- const uint32_t input_type = _.GetOperandTypeId(inst, 2);
- if (!input_type)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have a type: " << spvOpcodeString(opcode);
- const bool result_is_pointer = _.IsPointerType(result_type);
- const bool result_is_int_scalar = _.IsIntScalarType(result_type);
- const bool input_is_pointer = _.IsPointerType(input_type);
- const bool input_is_int_scalar = _.IsIntScalarType(input_type);
- if (!result_is_pointer && !result_is_int_scalar &&
- !_.IsIntVectorType(result_type) &&
- !_.IsFloatScalarType(result_type) &&
- !_.IsFloatVectorType(result_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected Result Type to be a pointer or int or float vector "
- << "or scalar type: " << spvOpcodeString(opcode);
- if (!input_is_pointer && !input_is_int_scalar &&
- !_.IsIntVectorType(input_type) && !_.IsFloatScalarType(input_type) &&
- !_.IsFloatVectorType(input_type))
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be a pointer or int or float vector "
- << "or scalar: " << spvOpcodeString(opcode);
- if (result_is_pointer && !input_is_pointer && !input_is_int_scalar)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to be a pointer or int scalar if Result Type "
- << "is pointer: " << spvOpcodeString(opcode);
- if (input_is_pointer && !result_is_pointer && !result_is_int_scalar)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Pointer can only be converted to another pointer or int "
- << "scalar: " << spvOpcodeString(opcode);
- if (!result_is_pointer && !input_is_pointer) {
- const uint32_t result_size =
- _.GetBitWidth(result_type) * _.GetDimension(result_type);
- const uint32_t input_size =
- _.GetBitWidth(input_type) * _.GetDimension(input_type);
- if (result_size != input_size)
- return _.diag(SPV_ERROR_INVALID_DATA, inst)
- << "Expected input to have the same total bit width as "
- << "Result Type: " << spvOpcodeString(opcode);
- }
- break;
- }
- default:
- break;
- }
- return SPV_SUCCESS;
- }
- } // namespace val
- } // namespace spvtools
|