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
ValidateGroupNonUniformRotateKHR(ValidationState_t & _,const Instruction * inst)66 spv_result_t ValidateGroupNonUniformRotateKHR(ValidationState_t& _,
67 const Instruction* inst) {
68 // Scope is already checked by ValidateExecutionScope() above.
69 const uint32_t result_type = inst->type_id();
70 if (!_.IsIntScalarOrVectorType(result_type) &&
71 !_.IsFloatScalarOrVectorType(result_type) &&
72 !_.IsBoolScalarOrVectorType(result_type)) {
73 return _.diag(SPV_ERROR_INVALID_DATA, inst)
74 << "Expected Result Type to be a scalar or vector of "
75 "floating-point, integer or boolean type.";
76 }
77
78 const uint32_t value_type = _.GetTypeId(inst->GetOperandAs<uint32_t>(3));
79 if (value_type != result_type) {
80 return _.diag(SPV_ERROR_INVALID_DATA, inst)
81 << "Result Type must be the same as the type of Value.";
82 }
83
84 const uint32_t delta_type = _.GetTypeId(inst->GetOperandAs<uint32_t>(4));
85 if (!_.IsUnsignedIntScalarType(delta_type)) {
86 return _.diag(SPV_ERROR_INVALID_DATA, inst)
87 << "Delta must be a scalar of integer type, whose Signedness "
88 "operand is 0.";
89 }
90
91 if (inst->words().size() > 6) {
92 const uint32_t cluster_size_op_id = inst->GetOperandAs<uint32_t>(5);
93 const uint32_t cluster_size_type = _.GetTypeId(cluster_size_op_id);
94 if (!_.IsUnsignedIntScalarType(cluster_size_type)) {
95 return _.diag(SPV_ERROR_INVALID_DATA, inst)
96 << "ClusterSize must be a scalar of integer type, whose "
97 "Signedness operand is 0.";
98 }
99
100 uint64_t cluster_size;
101 if (!_.GetConstantValUint64(cluster_size_op_id, &cluster_size)) {
102 return _.diag(SPV_ERROR_INVALID_DATA, inst)
103 << "ClusterSize must come from a constant instruction.";
104 }
105
106 if ((cluster_size == 0) || ((cluster_size & (cluster_size - 1)) != 0)) {
107 return _.diag(SPV_WARNING, inst)
108 << "Behavior is undefined unless ClusterSize is at least 1 and a "
109 "power of 2.";
110 }
111
112 // TODO(kpet) Warn about undefined behavior when ClusterSize is greater than
113 // the declared SubGroupSize
114 }
115
116 return SPV_SUCCESS;
117 }
118
119 } // namespace
120
121 // Validates correctness of non-uniform group instructions.
NonUniformPass(ValidationState_t & _,const Instruction * inst)122 spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst) {
123 const SpvOp opcode = inst->opcode();
124
125 if (spvOpcodeIsNonUniformGroupOperation(opcode)) {
126 const uint32_t execution_scope = inst->word(3);
127 if (auto error = ValidateExecutionScope(_, inst, execution_scope)) {
128 return error;
129 }
130 }
131
132 switch (opcode) {
133 case SpvOpGroupNonUniformBallotBitCount:
134 return ValidateGroupNonUniformBallotBitCount(_, inst);
135 case SpvOpGroupNonUniformRotateKHR:
136 return ValidateGroupNonUniformRotateKHR(_, inst);
137 default:
138 break;
139 }
140
141 return SPV_SUCCESS;
142 }
143
144 } // namespace val
145 } // namespace spvtools
146