xref: /aosp_15_r20/external/angle/third_party/spirv-tools/src/source/val/validate_misc.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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(spv::Capability::Shader) &&
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) {
54     spv::Scope scope_val{value};
55     if (spvIsVulkanEnv(_.context()->target_env)) {
56       if (scope_val != spv::Scope::Subgroup &&
57           scope_val != spv::Scope::Device) {
58         return _.diag(SPV_ERROR_INVALID_DATA, inst)
59                << _.VkErrorID(4652) << "Scope must be Subgroup or Device";
60       }
61     } else if (spvIsOpenCLEnv(_.context()->target_env)) {
62       if (scope_val != spv::Scope::Workgroup &&
63           scope_val != spv::Scope::Subgroup &&
64           scope_val != spv::Scope::Device) {
65         return _.diag(SPV_ERROR_INVALID_DATA, inst)
66                << "Scope must be Subgroup, Workgroup, or Device";
67       }
68     }
69   }
70 
71   // Result Type must be a 64 - bit unsigned integer type or
72   // a vector of two - components of 32 -
73   // bit unsigned integer type
74   const uint32_t result_type = inst->type_id();
75   if (!_.IsUnsigned64BitHandle(result_type)) {
76     return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
77                                                    "vector of two components"
78                                                    " of unsigned integer"
79                                                    " or 64bit unsigned integer";
80   }
81 
82   return SPV_SUCCESS;
83 }
84 
ValidateAssumeTrue(ValidationState_t & _,const Instruction * inst)85 spv_result_t ValidateAssumeTrue(ValidationState_t& _, const Instruction* inst) {
86   const auto operand_type_id = _.GetOperandTypeId(inst, 0);
87   if (!operand_type_id || !_.IsBoolScalarType(operand_type_id)) {
88     return _.diag(SPV_ERROR_INVALID_ID, inst)
89            << "Value operand of OpAssumeTrueKHR must be a boolean scalar";
90   }
91   return SPV_SUCCESS;
92 }
93 
ValidateExpect(ValidationState_t & _,const Instruction * inst)94 spv_result_t ValidateExpect(ValidationState_t& _, const Instruction* inst) {
95   const auto result_type = inst->type_id();
96   if (!_.IsBoolScalarOrVectorType(result_type) &&
97       !_.IsIntScalarOrVectorType(result_type)) {
98     return _.diag(SPV_ERROR_INVALID_ID, inst)
99            << "Result of OpExpectKHR must be a scalar or vector of integer "
100               "type or boolean type";
101   }
102 
103   if (_.GetOperandTypeId(inst, 2) != result_type) {
104     return _.diag(SPV_ERROR_INVALID_ID, inst)
105            << "Type of Value operand of OpExpectKHR does not match the result "
106               "type ";
107   }
108   if (_.GetOperandTypeId(inst, 3) != result_type) {
109     return _.diag(SPV_ERROR_INVALID_ID, inst)
110            << "Type of ExpectedValue operand of OpExpectKHR does not match the "
111               "result type ";
112   }
113   return SPV_SUCCESS;
114 }
115 
116 }  // namespace
117 
MiscPass(ValidationState_t & _,const Instruction * inst)118 spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
119   switch (inst->opcode()) {
120     case spv::Op::OpUndef:
121       if (auto error = ValidateUndef(_, inst)) return error;
122       break;
123     default:
124       break;
125   }
126   switch (inst->opcode()) {
127     case spv::Op::OpBeginInvocationInterlockEXT:
128     case spv::Op::OpEndInvocationInterlockEXT:
129       _.function(inst->function()->id())
130           ->RegisterExecutionModelLimitation(
131               spv::ExecutionModel::Fragment,
132               "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
133               "require Fragment execution model");
134 
135       _.function(inst->function()->id())
136           ->RegisterLimitation([](const ValidationState_t& state,
137                                   const Function* entry_point,
138                                   std::string* message) {
139             const auto* execution_modes =
140                 state.GetExecutionModes(entry_point->id());
141 
142             auto find_interlock = [](const spv::ExecutionMode& mode) {
143               switch (mode) {
144                 case spv::ExecutionMode::PixelInterlockOrderedEXT:
145                 case spv::ExecutionMode::PixelInterlockUnorderedEXT:
146                 case spv::ExecutionMode::SampleInterlockOrderedEXT:
147                 case spv::ExecutionMode::SampleInterlockUnorderedEXT:
148                 case spv::ExecutionMode::ShadingRateInterlockOrderedEXT:
149                 case spv::ExecutionMode::ShadingRateInterlockUnorderedEXT:
150                   return true;
151                 default:
152                   return false;
153               }
154             };
155 
156             bool found = false;
157             if (execution_modes) {
158               auto i = std::find_if(execution_modes->begin(),
159                                     execution_modes->end(), find_interlock);
160               found = (i != execution_modes->end());
161             }
162 
163             if (!found) {
164               *message =
165                   "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
166                   "require a fragment shader interlock execution mode.";
167               return false;
168             }
169             return true;
170           });
171       break;
172     case spv::Op::OpDemoteToHelperInvocationEXT:
173       _.function(inst->function()->id())
174           ->RegisterExecutionModelLimitation(
175               spv::ExecutionModel::Fragment,
176               "OpDemoteToHelperInvocationEXT requires Fragment execution "
177               "model");
178       break;
179     case spv::Op::OpIsHelperInvocationEXT: {
180       const uint32_t result_type = inst->type_id();
181       _.function(inst->function()->id())
182           ->RegisterExecutionModelLimitation(
183               spv::ExecutionModel::Fragment,
184               "OpIsHelperInvocationEXT requires Fragment execution model");
185       if (!_.IsBoolScalarType(result_type))
186         return _.diag(SPV_ERROR_INVALID_DATA, inst)
187                << "Expected bool scalar type as Result Type: "
188                << spvOpcodeString(inst->opcode());
189       break;
190     }
191     case spv::Op::OpReadClockKHR:
192       if (auto error = ValidateShaderClock(_, inst)) {
193         return error;
194       }
195       break;
196     case spv::Op::OpAssumeTrueKHR:
197       if (auto error = ValidateAssumeTrue(_, inst)) {
198         return error;
199       }
200       break;
201     case spv::Op::OpExpectKHR:
202       if (auto error = ValidateExpect(_, inst)) {
203         return error;
204       }
205       break;
206     default:
207       break;
208   }
209 
210   return SPV_SUCCESS;
211 }
212 
213 }  // namespace val
214 }  // namespace spvtools
215