2017-11-30 16:29:05 +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 atomic SPIR-V instructions.
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validate.h"
|
|
|
|
|
|
|
|
#include "source/diagnostic.h"
|
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/spirv_target_env.h"
|
|
|
|
#include "source/util/bitutils.h"
|
|
|
|
#include "source/val/instruction.h"
|
2018-12-06 19:38:15 +00:00
|
|
|
#include "source/val/validate_memory_semantics.h"
|
2018-11-29 19:51:17 +00:00
|
|
|
#include "source/val/validate_scopes.h"
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validation_state.h"
|
2017-11-30 16:29:05 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2017-11-30 16:29:05 +00:00
|
|
|
|
|
|
|
// Validates correctness of atomic instructions.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t AtomicsPass(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const SpvOp opcode = inst->opcode();
|
|
|
|
const uint32_t result_type = inst->type_id();
|
2017-11-30 16:29:05 +00:00
|
|
|
|
|
|
|
switch (opcode) {
|
|
|
|
case SpvOpAtomicLoad:
|
|
|
|
case SpvOpAtomicStore:
|
|
|
|
case SpvOpAtomicExchange:
|
|
|
|
case SpvOpAtomicCompareExchange:
|
|
|
|
case SpvOpAtomicCompareExchangeWeak:
|
|
|
|
case SpvOpAtomicIIncrement:
|
|
|
|
case SpvOpAtomicIDecrement:
|
|
|
|
case SpvOpAtomicIAdd:
|
|
|
|
case SpvOpAtomicISub:
|
|
|
|
case SpvOpAtomicSMin:
|
|
|
|
case SpvOpAtomicUMin:
|
|
|
|
case SpvOpAtomicSMax:
|
|
|
|
case SpvOpAtomicUMax:
|
|
|
|
case SpvOpAtomicAnd:
|
|
|
|
case SpvOpAtomicOr:
|
|
|
|
case SpvOpAtomicXor:
|
|
|
|
case SpvOpAtomicFlagTestAndSet:
|
|
|
|
case SpvOpAtomicFlagClear: {
|
2018-01-24 17:48:55 +00:00
|
|
|
if (_.HasCapability(SpvCapabilityKernel) &&
|
|
|
|
(opcode == SpvOpAtomicLoad || opcode == SpvOpAtomicExchange ||
|
|
|
|
opcode == SpvOpAtomicCompareExchange)) {
|
2017-11-30 16:29:05 +00:00
|
|
|
if (!_.IsFloatScalarType(result_type) &&
|
|
|
|
!_.IsIntScalarType(result_type)) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Result Type to be int or float scalar type";
|
|
|
|
}
|
|
|
|
} else if (opcode == SpvOpAtomicFlagTestAndSet) {
|
|
|
|
if (!_.IsBoolScalarType(result_type)) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Result Type to be bool scalar type";
|
|
|
|
}
|
|
|
|
} else if (opcode == SpvOpAtomicFlagClear || opcode == SpvOpAtomicStore) {
|
|
|
|
assert(result_type == 0);
|
|
|
|
} else {
|
|
|
|
if (!_.IsIntScalarType(result_type)) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Result Type to be int scalar type";
|
2018-03-23 20:15:03 +00:00
|
|
|
}
|
|
|
|
if (spvIsVulkanEnv(_.context()->target_env) &&
|
|
|
|
_.GetBitWidth(result_type) != 32) {
|
2018-09-14 18:07:25 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case SpvOpAtomicSMin:
|
|
|
|
case SpvOpAtomicUMin:
|
|
|
|
case SpvOpAtomicSMax:
|
|
|
|
case SpvOpAtomicUMax:
|
|
|
|
case SpvOpAtomicAnd:
|
|
|
|
case SpvOpAtomicOr:
|
|
|
|
case SpvOpAtomicXor:
|
|
|
|
case SpvOpAtomicIAdd:
|
2018-09-26 20:45:37 +00:00
|
|
|
case SpvOpAtomicLoad:
|
|
|
|
case SpvOpAtomicStore:
|
2018-09-14 18:07:25 +00:00
|
|
|
case SpvOpAtomicExchange:
|
|
|
|
case SpvOpAtomicCompareExchange: {
|
|
|
|
if (_.GetBitWidth(result_type) == 64 &&
|
|
|
|
!_.HasCapability(SpvCapabilityInt64Atomics))
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": 64-bit atomics require the Int64Atomics "
|
|
|
|
"capability";
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": according to the Vulkan spec atomic Result Type "
|
|
|
|
"needs "
|
|
|
|
"to be a 32-bit int scalar type";
|
|
|
|
}
|
2017-11-30 16:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t operand_index =
|
|
|
|
opcode == SpvOpAtomicFlagClear || opcode == SpvOpAtomicStore ? 0 : 2;
|
|
|
|
const uint32_t pointer_type = _.GetOperandTypeId(inst, operand_index++);
|
|
|
|
|
|
|
|
uint32_t data_type = 0;
|
|
|
|
uint32_t storage_class = 0;
|
|
|
|
if (!_.GetPointerTypeInfo(pointer_type, &data_type, &storage_class)) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Pointer to be of type OpTypePointer";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (storage_class) {
|
|
|
|
case SpvStorageClassUniform:
|
|
|
|
case SpvStorageClassWorkgroup:
|
|
|
|
case SpvStorageClassCrossWorkgroup:
|
|
|
|
case SpvStorageClassGeneric:
|
|
|
|
case SpvStorageClassAtomicCounter:
|
|
|
|
case SpvStorageClassImage:
|
|
|
|
case SpvStorageClassStorageBuffer:
|
2019-01-07 18:19:24 +00:00
|
|
|
case SpvStorageClassPhysicalStorageBufferEXT:
|
2017-11-30 16:29:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-10-09 15:33:01 +00:00
|
|
|
if (spvIsOpenCLEnv(_.context()->target_env)) {
|
|
|
|
if (storage_class != SpvStorageClassFunction) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Pointer Storage Class to be Uniform, "
|
|
|
|
"Workgroup, CrossWorkgroup, Generic, AtomicCounter, "
|
|
|
|
"Image, StorageBuffer or Function";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Pointer Storage Class to be Uniform, "
|
|
|
|
"Workgroup, CrossWorkgroup, Generic, AtomicCounter, "
|
|
|
|
"Image or StorageBuffer";
|
|
|
|
}
|
2017-11-30 16:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode == SpvOpAtomicFlagTestAndSet ||
|
|
|
|
opcode == SpvOpAtomicFlagClear) {
|
|
|
|
if (!_.IsIntScalarType(data_type) || _.GetBitWidth(data_type) != 32) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Pointer to point to a value of 32-bit int type";
|
|
|
|
}
|
|
|
|
} else if (opcode == SpvOpAtomicStore) {
|
|
|
|
if (!_.IsFloatScalarType(data_type) && !_.IsIntScalarType(data_type)) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Pointer to be a pointer to int or float "
|
|
|
|
<< "scalar type";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (data_type != result_type) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Pointer to point to a value of type Result "
|
|
|
|
"Type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
auto memory_scope = inst->GetOperandAs<const uint32_t>(operand_index++);
|
2018-02-06 17:10:11 +00:00
|
|
|
if (auto error = ValidateMemoryScope(_, inst, memory_scope)) {
|
|
|
|
return error;
|
2017-11-30 16:29:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 18:39:43 +00:00
|
|
|
if (auto error = ValidateMemorySemantics(_, inst, operand_index++))
|
|
|
|
return error;
|
2017-11-30 16:29:05 +00:00
|
|
|
|
|
|
|
if (opcode == SpvOpAtomicCompareExchange ||
|
|
|
|
opcode == SpvOpAtomicCompareExchangeWeak) {
|
2018-01-25 18:39:43 +00:00
|
|
|
if (auto error = ValidateMemorySemantics(_, inst, operand_index++))
|
|
|
|
return error;
|
2017-11-30 16:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode == SpvOpAtomicStore) {
|
|
|
|
const uint32_t value_type = _.GetOperandTypeId(inst, 3);
|
|
|
|
if (value_type != data_type) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
2018-09-14 18:07:25 +00:00
|
|
|
<< ": expected Value type and the type pointed to by "
|
|
|
|
"Pointer to be the same";
|
2017-11-30 16:29:05 +00:00
|
|
|
}
|
|
|
|
} else if (opcode != SpvOpAtomicLoad && opcode != SpvOpAtomicIIncrement &&
|
|
|
|
opcode != SpvOpAtomicIDecrement &&
|
|
|
|
opcode != SpvOpAtomicFlagTestAndSet &&
|
|
|
|
opcode != SpvOpAtomicFlagClear) {
|
|
|
|
const uint32_t value_type = _.GetOperandTypeId(inst, operand_index++);
|
|
|
|
if (value_type != result_type) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Value to be of type Result Type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode == SpvOpAtomicCompareExchange ||
|
|
|
|
opcode == SpvOpAtomicCompareExchangeWeak) {
|
|
|
|
const uint32_t comparator_type =
|
|
|
|
_.GetOperandTypeId(inst, operand_index++);
|
|
|
|
if (comparator_type != result_type) {
|
2018-07-31 21:20:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, inst)
|
2017-11-30 16:29:05 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< ": expected Comparator to be of type Result Type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|