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 return SPV_SUCCESS;
51 }
52
53 } // namespace
54
55 // Validates correctness of non-uniform group instructions.
NonUniformPass(ValidationState_t & _,const Instruction * inst)56 spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst) {
57 const SpvOp opcode = inst->opcode();
58
59 if (spvOpcodeIsNonUniformGroupOperation(opcode)) {
60 const uint32_t execution_scope = inst->word(3);
61 if (auto error = ValidateExecutionScope(_, inst, execution_scope)) {
62 return error;
63 }
64 }
65
66 switch (opcode) {
67 case SpvOpGroupNonUniformBallotBitCount:
68 return ValidateGroupNonUniformBallotBitCount(_, inst);
69 default:
70 break;
71 }
72
73 return SPV_SUCCESS;
74 }
75
76 } // namespace val
77 } // namespace spvtools
78