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 (!_.IsUnsigned64BitHandle(result_type)) {
63 return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
64 "vector of two components"
65 " of unsigned integer"
66 " or 64bit unsigned integer";
67 }
68
69 return SPV_SUCCESS;
70 }
71
ValidateAssumeTrue(ValidationState_t & _,const Instruction * inst)72 spv_result_t ValidateAssumeTrue(ValidationState_t& _, const Instruction* inst) {
73 const auto operand_type_id = _.GetOperandTypeId(inst, 0);
74 if (!operand_type_id || !_.IsBoolScalarType(operand_type_id)) {
75 return _.diag(SPV_ERROR_INVALID_ID, inst)
76 << "Value operand of OpAssumeTrueKHR must be a boolean scalar";
77 }
78 return SPV_SUCCESS;
79 }
80
ValidateExpect(ValidationState_t & _,const Instruction * inst)81 spv_result_t ValidateExpect(ValidationState_t& _, const Instruction* inst) {
82 const auto result_type = inst->type_id();
83 if (!_.IsBoolScalarOrVectorType(result_type) &&
84 !_.IsIntScalarOrVectorType(result_type)) {
85 return _.diag(SPV_ERROR_INVALID_ID, inst)
86 << "Result of OpExpectKHR must be a scalar or vector of integer "
87 "type or boolean type";
88 }
89
90 if (_.GetOperandTypeId(inst, 2) != result_type) {
91 return _.diag(SPV_ERROR_INVALID_ID, inst)
92 << "Type of Value operand of OpExpectKHR does not match the result "
93 "type ";
94 }
95 if (_.GetOperandTypeId(inst, 3) != result_type) {
96 return _.diag(SPV_ERROR_INVALID_ID, inst)
97 << "Type of ExpectedValue operand of OpExpectKHR does not match the "
98 "result type ";
99 }
100 return SPV_SUCCESS;
101 }
102
103 } // namespace
104
MiscPass(ValidationState_t & _,const Instruction * inst)105 spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
106 switch (inst->opcode()) {
107 case SpvOpUndef:
108 if (auto error = ValidateUndef(_, inst)) return error;
109 break;
110 default:
111 break;
112 }
113 switch (inst->opcode()) {
114 case SpvOpBeginInvocationInterlockEXT:
115 case SpvOpEndInvocationInterlockEXT:
116 _.function(inst->function()->id())
117 ->RegisterExecutionModelLimitation(
118 SpvExecutionModelFragment,
119 "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
120 "require Fragment execution model");
121
122 _.function(inst->function()->id())
123 ->RegisterLimitation([](const ValidationState_t& state,
124 const Function* entry_point,
125 std::string* message) {
126 const auto* execution_modes =
127 state.GetExecutionModes(entry_point->id());
128
129 auto find_interlock = [](const SpvExecutionMode& mode) {
130 switch (mode) {
131 case SpvExecutionModePixelInterlockOrderedEXT:
132 case SpvExecutionModePixelInterlockUnorderedEXT:
133 case SpvExecutionModeSampleInterlockOrderedEXT:
134 case SpvExecutionModeSampleInterlockUnorderedEXT:
135 case SpvExecutionModeShadingRateInterlockOrderedEXT:
136 case SpvExecutionModeShadingRateInterlockUnorderedEXT:
137 return true;
138 default:
139 return false;
140 }
141 };
142
143 bool found = false;
144 if (execution_modes) {
145 auto i = std::find_if(execution_modes->begin(),
146 execution_modes->end(), find_interlock);
147 found = (i != execution_modes->end());
148 }
149
150 if (!found) {
151 *message =
152 "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
153 "require a fragment shader interlock execution mode.";
154 return false;
155 }
156 return true;
157 });
158 break;
159 case SpvOpDemoteToHelperInvocationEXT:
160 _.function(inst->function()->id())
161 ->RegisterExecutionModelLimitation(
162 SpvExecutionModelFragment,
163 "OpDemoteToHelperInvocationEXT requires Fragment execution "
164 "model");
165 break;
166 case SpvOpIsHelperInvocationEXT: {
167 const uint32_t result_type = inst->type_id();
168 _.function(inst->function()->id())
169 ->RegisterExecutionModelLimitation(
170 SpvExecutionModelFragment,
171 "OpIsHelperInvocationEXT requires Fragment execution model");
172 if (!_.IsBoolScalarType(result_type))
173 return _.diag(SPV_ERROR_INVALID_DATA, inst)
174 << "Expected bool scalar type as Result Type: "
175 << spvOpcodeString(inst->opcode());
176 break;
177 }
178 case SpvOpReadClockKHR:
179 if (auto error = ValidateShaderClock(_, inst)) {
180 return error;
181 }
182 break;
183 case SpvOpAssumeTrueKHR:
184 if (auto error = ValidateAssumeTrue(_, inst)) {
185 return error;
186 }
187 break;
188 case SpvOpExpectKHR:
189 if (auto error = ValidateExpect(_, inst)) {
190 return error;
191 }
192 break;
193 default:
194 break;
195 }
196
197 return SPV_SUCCESS;
198 }
199
200 } // namespace val
201 } // namespace spvtools
202