1 // Copyright (c) 2018 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Validates correctness of barrier SPIR-V instructions.
16
17 #include "source/val/validate.h"
18
19 #include "source/diagnostic.h"
20 #include "source/opcode.h"
21 #include "source/spirv_constant.h"
22 #include "source/spirv_target_env.h"
23 #include "source/util/bitutils.h"
24 #include "source/val/instruction.h"
25 #include "source/val/validate_scopes.h"
26 #include "source/val/validation_state.h"
27
28 namespace spvtools {
29 namespace val {
30 namespace {
31
ValidateGroupNonUniformBallotBitCount(ValidationState_t & _,const Instruction * inst)32 spv_result_t ValidateGroupNonUniformBallotBitCount(ValidationState_t& _,
33 const Instruction* inst) {
34 // Scope is already checked by ValidateExecutionScope() above.
35
36 const uint32_t result_type = inst->type_id();
37 if (!_.IsUnsignedIntScalarType(result_type)) {
38 return _.diag(SPV_ERROR_INVALID_DATA, inst)
39 << "Expected Result Type to be an unsigned integer type scalar.";
40 }
41
42 const auto value = inst->GetOperandAs<uint32_t>(4);
43 const auto value_type = _.FindDef(value)->type_id();
44 if (!_.IsUnsignedIntVectorType(value_type) ||
45 _.GetDimension(value_type) != 4) {
46 return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
47 "vector of four components "
48 "of integer type scalar";
49 }
50
51 const auto group = inst->GetOperandAs<uint32_t>(3);
52 if (spvIsVulkanEnv(_.context()->target_env)) {
53 if ((group != SpvGroupOperationReduce) &&
54 (group != SpvGroupOperationInclusiveScan) &&
55 (group != SpvGroupOperationExclusiveScan)) {
56 return _.diag(SPV_ERROR_INVALID_DATA, inst)
57 << _.VkErrorID(4685)
58 << "In Vulkan: The OpGroupNonUniformBallotBitCount group "
59 "operation must be only: Reduce, InclusiveScan, or "
60 "ExclusiveScan.";
61 }
62 }
63 return SPV_SUCCESS;
64 }
65
66 } // namespace
67
68 // Validates correctness of non-uniform group instructions.
NonUniformPass(ValidationState_t & _,const Instruction * inst)69 spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst) {
70 const SpvOp opcode = inst->opcode();
71
72 if (spvOpcodeIsNonUniformGroupOperation(opcode)) {
73 const uint32_t execution_scope = inst->word(3);
74 if (auto error = ValidateExecutionScope(_, inst, execution_scope)) {
75 return error;
76 }
77 }
78
79 switch (opcode) {
80 case SpvOpGroupNonUniformBallotBitCount:
81 return ValidateGroupNonUniformBallotBitCount(_, inst);
82 default:
83 break;
84 }
85
86 return SPV_SUCCESS;
87 }
88
89 } // namespace val
90 } // namespace spvtools
91