1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
// 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.
// Validates correctness of barrier SPIR-V instructions.
#include "source/opcode.h"
#include "source/spirv_constant.h"
#include "source/spirv_target_env.h"
#include "source/val/instruction.h"
#include "source/val/validate.h"
#include "source/val/validate_scopes.h"
#include "source/val/validation_state.h"
namespace spvtools {
namespace val {
namespace {
spv_result_t ValidateGroupNonUniformElect(ValidationState_t& _,
const Instruction* inst) {
if (!_.IsBoolScalarType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a boolean scalar type";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformAnyAll(ValidationState_t& _,
const Instruction* inst) {
if (!_.IsBoolScalarType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a boolean scalar type";
}
if (!_.IsBoolScalarType(_.GetOperandTypeId(inst, 3))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Predicate must be a boolean scalar type";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformAllEqual(ValidationState_t& _,
const Instruction* inst) {
if (!_.IsBoolScalarType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a boolean scalar type";
}
const auto value_type = _.GetOperandTypeId(inst, 3);
if (!_.IsFloatScalarOrVectorType(value_type) &&
!_.IsIntScalarOrVectorType(value_type) &&
!_.IsBoolScalarOrVectorType(value_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Value must be a scalar or vector of integer, floating-point, or "
"boolean type";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformBroadcastShuffle(ValidationState_t& _,
const Instruction* inst) {
const auto type_id = inst->type_id();
if (!_.IsFloatScalarOrVectorType(type_id) &&
!_.IsIntScalarOrVectorType(type_id) &&
!_.IsBoolScalarOrVectorType(type_id)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a scalar or vector of integer, floating-point, "
"or boolean type";
}
const auto value_type_id = _.GetOperandTypeId(inst, 3);
if (value_type_id != type_id) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "The type of Value must match the Result type";
}
const auto GetOperandName = [](const spv::Op opcode) {
std::string operand;
switch (opcode) {
case spv::Op::OpGroupNonUniformBroadcast:
case spv::Op::OpGroupNonUniformShuffle:
operand = "Id";
break;
case spv::Op::OpGroupNonUniformShuffleXor:
operand = "Mask";
break;
case spv::Op::OpGroupNonUniformQuadBroadcast:
operand = "Index";
break;
case spv::Op::OpGroupNonUniformQuadSwap:
operand = "Direction";
break;
case spv::Op::OpGroupNonUniformShuffleUp:
case spv::Op::OpGroupNonUniformShuffleDown:
default:
operand = "Delta";
break;
}
return operand;
};
const auto id_type_id = _.GetOperandTypeId(inst, 4);
if (!_.IsUnsignedIntScalarType(id_type_id)) {
std::string operand = GetOperandName(inst->opcode());
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< operand << " must be an unsigned integer scalar";
}
const bool should_be_constant =
inst->opcode() == spv::Op::OpGroupNonUniformQuadSwap ||
((inst->opcode() == spv::Op::OpGroupNonUniformBroadcast ||
inst->opcode() == spv::Op::OpGroupNonUniformQuadBroadcast) &&
_.version() < SPV_SPIRV_VERSION_WORD(1, 5));
if (should_be_constant) {
const auto id_id = inst->GetOperandAs<uint32_t>(4);
const auto id_op = _.GetIdOpcode(id_id);
if (!spvOpcodeIsConstant(id_op)) {
std::string operand = GetOperandName(inst->opcode());
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "In SPIR-V 1.4 or earlier, " << operand
<< " must be a constant instruction";
}
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformBroadcastFirst(ValidationState_t& _,
const Instruction* inst) {
const auto type_id = inst->type_id();
if (!_.IsFloatScalarOrVectorType(type_id) &&
!_.IsIntScalarOrVectorType(type_id) &&
!_.IsBoolScalarOrVectorType(type_id)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a scalar or vector of integer, floating-point, "
"or boolean type";
}
const auto value_type_id = _.GetOperandTypeId(inst, 3);
if (value_type_id != type_id) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "The type of Value must match the Result type";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformBallot(ValidationState_t& _,
const Instruction* inst) {
if (!_.IsUnsignedIntVectorType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a 4-component unsigned integer vector";
}
if (_.GetDimension(inst->type_id()) != 4) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a 4-component unsigned integer vector";
}
const auto pred_type_id = _.GetOperandTypeId(inst, 3);
if (!_.IsBoolScalarType(pred_type_id)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Predicate must be a boolean scalar";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformInverseBallot(ValidationState_t& _,
const Instruction* inst) {
if (!_.IsBoolScalarType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a boolean scalar";
}
const auto value_type_id = _.GetOperandTypeId(inst, 3);
if (!_.IsUnsignedIntVectorType(value_type_id)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Value must be a 4-component unsigned integer vector";
}
if (_.GetDimension(value_type_id) != 4) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Value must be a 4-component unsigned integer vector";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformBallotBitExtract(ValidationState_t& _,
const Instruction* inst) {
if (!_.IsBoolScalarType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a boolean scalar";
}
const auto value_type_id = _.GetOperandTypeId(inst, 3);
if (!_.IsUnsignedIntVectorType(value_type_id)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Value must be a 4-component unsigned integer vector";
}
if (_.GetDimension(value_type_id) != 4) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Value must be a 4-component unsigned integer vector";
}
const auto id_type_id = _.GetOperandTypeId(inst, 4);
if (!_.IsUnsignedIntScalarType(id_type_id)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Id must be an unsigned integer scalar";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformBallotBitCount(ValidationState_t& _,
const Instruction* inst) {
// Scope is already checked by ValidateExecutionScope() above.
const uint32_t result_type = inst->type_id();
if (!_.IsUnsignedIntScalarType(result_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Result Type to be an unsigned integer type scalar.";
}
const auto value = inst->GetOperandAs<uint32_t>(4);
const auto value_type = _.FindDef(value)->type_id();
if (!_.IsUnsignedIntVectorType(value_type) ||
_.GetDimension(value_type) != 4) {
return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
"vector of four components "
"of integer type scalar";
}
const auto group = inst->GetOperandAs<spv::GroupOperation>(3);
if (spvIsVulkanEnv(_.context()->target_env)) {
if ((group != spv::GroupOperation::Reduce) &&
(group != spv::GroupOperation::InclusiveScan) &&
(group != spv::GroupOperation::ExclusiveScan)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(4685)
<< "In Vulkan: The OpGroupNonUniformBallotBitCount group "
"operation must be only: Reduce, InclusiveScan, or "
"ExclusiveScan.";
}
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformBallotFind(ValidationState_t& _,
const Instruction* inst) {
if (!_.IsUnsignedIntScalarType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be an unsigned integer scalar";
}
const auto value_type_id = _.GetOperandTypeId(inst, 3);
if (!_.IsUnsignedIntVectorType(value_type_id)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Value must be a 4-component unsigned integer vector";
}
if (_.GetDimension(value_type_id) != 4) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Value must be a 4-component unsigned integer vector";
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformArithmetic(ValidationState_t& _,
const Instruction* inst) {
const bool is_unsigned = inst->opcode() == spv::Op::OpGroupNonUniformUMin ||
inst->opcode() == spv::Op::OpGroupNonUniformUMax;
const bool is_float = inst->opcode() == spv::Op::OpGroupNonUniformFAdd ||
inst->opcode() == spv::Op::OpGroupNonUniformFMul ||
inst->opcode() == spv::Op::OpGroupNonUniformFMin ||
inst->opcode() == spv::Op::OpGroupNonUniformFMax;
const bool is_bool = inst->opcode() == spv::Op::OpGroupNonUniformLogicalAnd ||
inst->opcode() == spv::Op::OpGroupNonUniformLogicalOr ||
inst->opcode() == spv::Op::OpGroupNonUniformLogicalXor;
if (is_float) {
if (!_.IsFloatScalarOrVectorType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a floating-point scalar or vector";
}
} else if (is_bool) {
if (!_.IsBoolScalarOrVectorType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be a boolean scalar or vector";
}
} else if (is_unsigned) {
if (!_.IsUnsignedIntScalarOrVectorType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be an unsigned integer scalar or vector";
}
} else if (!_.IsIntScalarOrVectorType(inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result must be an integer scalar or vector";
}
const auto value_type_id = _.GetOperandTypeId(inst, 4);
if (value_type_id != inst->type_id()) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "The type of Value must match the Result type";
}
const auto group_op = inst->GetOperandAs<spv::GroupOperation>(3);
bool is_clustered_reduce = group_op == spv::GroupOperation::ClusteredReduce;
bool is_partitioned_nv =
group_op == spv::GroupOperation::PartitionedReduceNV ||
group_op == spv::GroupOperation::PartitionedInclusiveScanNV ||
group_op == spv::GroupOperation::PartitionedExclusiveScanNV;
if (inst->operands().size() <= 5) {
if (is_clustered_reduce) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "ClusterSize must be present when Operation is ClusteredReduce";
} else if (is_partitioned_nv) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ballot must be present when Operation is PartitionedReduceNV, "
"PartitionedInclusiveScanNV, or PartitionedExclusiveScanNV";
}
} else {
const auto operand_id = inst->GetOperandAs<uint32_t>(5);
const auto* operand = _.FindDef(operand_id);
if (is_partitioned_nv) {
if (!operand || !_.IsIntScalarOrVectorType(operand->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ballot must be a 4-component integer vector";
}
if (_.GetDimension(operand->type_id()) != 4) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ballot must be a 4-component integer vector";
}
} else {
if (!operand || !_.IsUnsignedIntScalarType(operand->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "ClusterSize must be an unsigned integer scalar";
}
if (!spvOpcodeIsConstant(operand->opcode())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "ClusterSize must be a constant instruction";
}
}
}
return SPV_SUCCESS;
}
spv_result_t ValidateGroupNonUniformRotateKHR(ValidationState_t& _,
const Instruction* inst) {
// Scope is already checked by ValidateExecutionScope() above.
const uint32_t result_type = inst->type_id();
if (!_.IsIntScalarOrVectorType(result_type) &&
!_.IsFloatScalarOrVectorType(result_type) &&
!_.IsBoolScalarOrVectorType(result_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Result Type to be a scalar or vector of "
"floating-point, integer or boolean type.";
}
const uint32_t value_type = _.GetTypeId(inst->GetOperandAs<uint32_t>(3));
if (value_type != result_type) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Result Type must be the same as the type of Value.";
}
const uint32_t delta_type = _.GetTypeId(inst->GetOperandAs<uint32_t>(4));
if (!_.IsUnsignedIntScalarType(delta_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Delta must be a scalar of integer type, whose Signedness "
"operand is 0.";
}
if (inst->words().size() > 6) {
const uint32_t cluster_size_op_id = inst->GetOperandAs<uint32_t>(5);
const Instruction* cluster_size_inst = _.FindDef(cluster_size_op_id);
if (!cluster_size_inst ||
!_.IsUnsignedIntScalarType(cluster_size_inst->type_id())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "ClusterSize must be a scalar of integer type, whose "
"Signedness operand is 0.";
}
if (!spvOpcodeIsConstant(cluster_size_inst->opcode())) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "ClusterSize must come from a constant instruction.";
}
uint64_t cluster_size;
const bool valid_const =
_.EvalConstantValUint64(cluster_size_op_id, &cluster_size);
if (valid_const &&
((cluster_size == 0) || ((cluster_size & (cluster_size - 1)) != 0))) {
return _.diag(SPV_WARNING, inst)
<< "Behavior is undefined unless ClusterSize is at least 1 and a "
"power of 2.";
}
// TODO(kpet) Warn about undefined behavior when ClusterSize is greater than
// the declared SubGroupSize
}
return SPV_SUCCESS;
}
} // namespace
// Validates correctness of non-uniform group instructions.
spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst) {
const spv::Op opcode = inst->opcode();
if (spvOpcodeIsNonUniformGroupOperation(opcode)) {
// OpGroupNonUniformQuadAllKHR and OpGroupNonUniformQuadAnyKHR don't have
// scope paramter
if ((opcode != spv::Op::OpGroupNonUniformQuadAllKHR) &&
(opcode != spv::Op::OpGroupNonUniformQuadAnyKHR)) {
const uint32_t execution_scope = inst->GetOperandAs<uint32_t>(2);
if (auto error = ValidateExecutionScope(_, inst, execution_scope)) {
return error;
}
}
}
switch (opcode) {
case spv::Op::OpGroupNonUniformElect:
return ValidateGroupNonUniformElect(_, inst);
case spv::Op::OpGroupNonUniformAny:
case spv::Op::OpGroupNonUniformAll:
return ValidateGroupNonUniformAnyAll(_, inst);
case spv::Op::OpGroupNonUniformAllEqual:
return ValidateGroupNonUniformAllEqual(_, inst);
case spv::Op::OpGroupNonUniformBroadcast:
case spv::Op::OpGroupNonUniformShuffle:
case spv::Op::OpGroupNonUniformShuffleXor:
case spv::Op::OpGroupNonUniformShuffleUp:
case spv::Op::OpGroupNonUniformShuffleDown:
case spv::Op::OpGroupNonUniformQuadBroadcast:
case spv::Op::OpGroupNonUniformQuadSwap:
return ValidateGroupNonUniformBroadcastShuffle(_, inst);
case spv::Op::OpGroupNonUniformBroadcastFirst:
return ValidateGroupNonUniformBroadcastFirst(_, inst);
case spv::Op::OpGroupNonUniformBallot:
return ValidateGroupNonUniformBallot(_, inst);
case spv::Op::OpGroupNonUniformInverseBallot:
return ValidateGroupNonUniformInverseBallot(_, inst);
case spv::Op::OpGroupNonUniformBallotBitExtract:
return ValidateGroupNonUniformBallotBitExtract(_, inst);
case spv::Op::OpGroupNonUniformBallotBitCount:
return ValidateGroupNonUniformBallotBitCount(_, inst);
case spv::Op::OpGroupNonUniformBallotFindLSB:
case spv::Op::OpGroupNonUniformBallotFindMSB:
return ValidateGroupNonUniformBallotFind(_, inst);
case spv::Op::OpGroupNonUniformIAdd:
case spv::Op::OpGroupNonUniformFAdd:
case spv::Op::OpGroupNonUniformIMul:
case spv::Op::OpGroupNonUniformFMul:
case spv::Op::OpGroupNonUniformSMin:
case spv::Op::OpGroupNonUniformUMin:
case spv::Op::OpGroupNonUniformFMin:
case spv::Op::OpGroupNonUniformSMax:
case spv::Op::OpGroupNonUniformUMax:
case spv::Op::OpGroupNonUniformFMax:
case spv::Op::OpGroupNonUniformBitwiseAnd:
case spv::Op::OpGroupNonUniformBitwiseOr:
case spv::Op::OpGroupNonUniformBitwiseXor:
case spv::Op::OpGroupNonUniformLogicalAnd:
case spv::Op::OpGroupNonUniformLogicalOr:
case spv::Op::OpGroupNonUniformLogicalXor:
return ValidateGroupNonUniformArithmetic(_, inst);
case spv::Op::OpGroupNonUniformRotateKHR:
return ValidateGroupNonUniformRotateKHR(_, inst);
default:
break;
}
return SPV_SUCCESS;
}
} // namespace val
} // namespace spvtools
|