1 // Copyright (c) 2018 Google LLC.
2 // Copyright (c) 2019 NVIDIA Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "source/val/validate.h"
17
18 #include "source/opcode.h"
19 #include "source/spirv_target_env.h"
20 #include "source/val/instruction.h"
21 #include "source/val/validate_scopes.h"
22 #include "source/val/validation_state.h"
23
24 namespace spvtools {
25 namespace val {
26 namespace {
27
ValidateUndef(ValidationState_t & _,const Instruction * inst)28 spv_result_t ValidateUndef(ValidationState_t& _, const Instruction* inst) {
29 if (_.IsVoidType(inst->type_id())) {
30 return _.diag(SPV_ERROR_INVALID_ID, inst)
31 << "Cannot create undefined values with void type";
32 }
33 if (_.HasCapability(SpvCapabilityShader) &&
34 _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
35 !_.IsPointerType(inst->type_id())) {
36 return _.diag(SPV_ERROR_INVALID_ID, inst)
37 << "Cannot create undefined values with 8- or 16-bit types";
38 }
39
40 return SPV_SUCCESS;
41 }
42
ValidateShaderClock(ValidationState_t & _,const Instruction * inst)43 spv_result_t ValidateShaderClock(ValidationState_t& _,
44 const Instruction* inst) {
45 const uint32_t scope = inst->GetOperandAs<uint32_t>(2);
46 if (auto error = ValidateScope(_, inst, scope)) {
47 return error;
48 }
49
50 bool is_int32 = false, is_const_int32 = false;
51 uint32_t value = 0;
52 std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
53 if (is_const_int32 && value != SpvScopeSubgroup && value != SpvScopeDevice) {
54 return _.diag(SPV_ERROR_INVALID_DATA, inst)
55 << _.VkErrorID(4652) << "Scope must be Subgroup or Device";
56 }
57
58 // Result Type must be a 64 - bit unsigned integer type or
59 // a vector of two - components of 32 -
60 // bit unsigned integer type
61 const uint32_t result_type = inst->type_id();
62 if (!(_.IsUnsignedIntScalarType(result_type) &&
63 _.GetBitWidth(result_type) == 64) &&
64 !(_.IsUnsignedIntVectorType(result_type) &&
65 _.GetDimension(result_type) == 2 && _.GetBitWidth(result_type) == 32)) {
66 return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
67 "vector of two components"
68 " of unsigned integer"
69 " or 64bit unsigned integer";
70 }
71
72 return SPV_SUCCESS;
73 }
74
ValidateAssumeTrue(ValidationState_t & _,const Instruction * inst)75 spv_result_t ValidateAssumeTrue(ValidationState_t& _, const Instruction* inst) {
76 const auto operand_type_id = _.GetOperandTypeId(inst, 0);
77 if (!operand_type_id || !_.IsBoolScalarType(operand_type_id)) {
78 return _.diag(SPV_ERROR_INVALID_ID, inst)
79 << "Value operand of OpAssumeTrueKHR must be a boolean scalar";
80 }
81 return SPV_SUCCESS;
82 }
83
ValidateExpect(ValidationState_t & _,const Instruction * inst)84 spv_result_t ValidateExpect(ValidationState_t& _, const Instruction* inst) {
85 const auto result_type = inst->type_id();
86 if (!_.IsBoolScalarOrVectorType(result_type) &&
87 !_.IsIntScalarOrVectorType(result_type)) {
88 return _.diag(SPV_ERROR_INVALID_ID, inst)
89 << "Result of OpExpectKHR must be a scalar or vector of integer "
90 "type or boolean type";
91 }
92
93 if (_.GetOperandTypeId(inst, 2) != result_type) {
94 return _.diag(SPV_ERROR_INVALID_ID, inst)
95 << "Type of Value operand of OpExpectKHR does not match the result "
96 "type ";
97 }
98 if (_.GetOperandTypeId(inst, 3) != result_type) {
99 return _.diag(SPV_ERROR_INVALID_ID, inst)
100 << "Type of ExpectedValue operand of OpExpectKHR does not match the "
101 "result type ";
102 }
103 return SPV_SUCCESS;
104 }
105
106 } // namespace
107
MiscPass(ValidationState_t & _,const Instruction * inst)108 spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
109 switch (inst->opcode()) {
110 case SpvOpUndef:
111 if (auto error = ValidateUndef(_, inst)) return error;
112 break;
113 default:
114 break;
115 }
116 switch (inst->opcode()) {
117 case SpvOpBeginInvocationInterlockEXT:
118 case SpvOpEndInvocationInterlockEXT:
119 _.function(inst->function()->id())
120 ->RegisterExecutionModelLimitation(
121 SpvExecutionModelFragment,
122 "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
123 "require Fragment execution model");
124
125 _.function(inst->function()->id())
126 ->RegisterLimitation([](const ValidationState_t& state,
127 const Function* entry_point,
128 std::string* message) {
129 const auto* execution_modes =
130 state.GetExecutionModes(entry_point->id());
131
132 auto find_interlock = [](const SpvExecutionMode& mode) {
133 switch (mode) {
134 case SpvExecutionModePixelInterlockOrderedEXT:
135 case SpvExecutionModePixelInterlockUnorderedEXT:
136 case SpvExecutionModeSampleInterlockOrderedEXT:
137 case SpvExecutionModeSampleInterlockUnorderedEXT:
138 case SpvExecutionModeShadingRateInterlockOrderedEXT:
139 case SpvExecutionModeShadingRateInterlockUnorderedEXT:
140 return true;
141 default:
142 return false;
143 }
144 };
145
146 bool found = false;
147 if (execution_modes) {
148 auto i = std::find_if(execution_modes->begin(),
149 execution_modes->end(), find_interlock);
150 found = (i != execution_modes->end());
151 }
152
153 if (!found) {
154 *message =
155 "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
156 "require a fragment shader interlock execution mode.";
157 return false;
158 }
159 return true;
160 });
161 break;
162 case SpvOpDemoteToHelperInvocationEXT:
163 _.function(inst->function()->id())
164 ->RegisterExecutionModelLimitation(
165 SpvExecutionModelFragment,
166 "OpDemoteToHelperInvocationEXT requires Fragment execution "
167 "model");
168 break;
169 case SpvOpIsHelperInvocationEXT: {
170 const uint32_t result_type = inst->type_id();
171 _.function(inst->function()->id())
172 ->RegisterExecutionModelLimitation(
173 SpvExecutionModelFragment,
174 "OpIsHelperInvocationEXT requires Fragment execution model");
175 if (!_.IsBoolScalarType(result_type))
176 return _.diag(SPV_ERROR_INVALID_DATA, inst)
177 << "Expected bool scalar type as Result Type: "
178 << spvOpcodeString(inst->opcode());
179 break;
180 }
181 case SpvOpReadClockKHR:
182 if (auto error = ValidateShaderClock(_, inst)) {
183 return error;
184 }
185 break;
186 case SpvOpAssumeTrueKHR:
187 if (auto error = ValidateAssumeTrue(_, inst)) {
188 return error;
189 }
190 break;
191 case SpvOpExpectKHR:
192 if (auto error = ValidateExpect(_, inst)) {
193 return error;
194 }
195 break;
196 default:
197 break;
198 }
199
200 return SPV_SUCCESS;
201 }
202
203 } // namespace val
204 } // namespace spvtools
205