• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <algorithm>
16 
17 #include "source/opcode.h"
18 #include "source/val/instruction.h"
19 #include "source/val/validate.h"
20 #include "source/val/validation_state.h"
21 
22 namespace spvtools {
23 namespace val {
24 namespace {
25 
26 // Returns true if |a| and |b| are instructions defining pointers that point to
27 // types logically match and the decorations that apply to |b| are a subset
28 // of the decorations that apply to |a|.
DoPointeesLogicallyMatch(val::Instruction * a,val::Instruction * b,ValidationState_t & _)29 bool DoPointeesLogicallyMatch(val::Instruction* a, val::Instruction* b,
30                               ValidationState_t& _) {
31   if (a->opcode() != SpvOpTypePointer || b->opcode() != SpvOpTypePointer) {
32     return false;
33   }
34 
35   const auto& dec_a = _.id_decorations(a->id());
36   const auto& dec_b = _.id_decorations(b->id());
37   for (const auto& dec : dec_b) {
38     if (std::find(dec_a.begin(), dec_a.end(), dec) == dec_a.end()) {
39       return false;
40     }
41   }
42 
43   uint32_t a_type = a->GetOperandAs<uint32_t>(2);
44   uint32_t b_type = b->GetOperandAs<uint32_t>(2);
45 
46   if (a_type == b_type) {
47     return true;
48   }
49 
50   Instruction* a_type_inst = _.FindDef(a_type);
51   Instruction* b_type_inst = _.FindDef(b_type);
52 
53   return _.LogicallyMatch(a_type_inst, b_type_inst, true);
54 }
55 
ValidateFunction(ValidationState_t & _,const Instruction * inst)56 spv_result_t ValidateFunction(ValidationState_t& _, const Instruction* inst) {
57   const auto function_type_id = inst->GetOperandAs<uint32_t>(3);
58   const auto function_type = _.FindDef(function_type_id);
59   if (!function_type || SpvOpTypeFunction != function_type->opcode()) {
60     return _.diag(SPV_ERROR_INVALID_ID, inst)
61            << "OpFunction Function Type <id> " << _.getIdName(function_type_id)
62            << " is not a function type.";
63   }
64 
65   const auto return_id = function_type->GetOperandAs<uint32_t>(1);
66   if (return_id != inst->type_id()) {
67     return _.diag(SPV_ERROR_INVALID_ID, inst)
68            << "OpFunction Result Type <id> " << _.getIdName(inst->type_id())
69            << " does not match the Function Type's return type <id> "
70            << _.getIdName(return_id) << ".";
71   }
72 
73   const std::vector<SpvOp> acceptable = {
74       SpvOpGroupDecorate,
75       SpvOpDecorate,
76       SpvOpEnqueueKernel,
77       SpvOpEntryPoint,
78       SpvOpExecutionMode,
79       SpvOpExecutionModeId,
80       SpvOpFunctionCall,
81       SpvOpGetKernelNDrangeSubGroupCount,
82       SpvOpGetKernelNDrangeMaxSubGroupSize,
83       SpvOpGetKernelWorkGroupSize,
84       SpvOpGetKernelPreferredWorkGroupSizeMultiple,
85       SpvOpGetKernelLocalSizeForSubgroupCount,
86       SpvOpGetKernelMaxNumSubgroups,
87       SpvOpName};
88   for (auto& pair : inst->uses()) {
89     const auto* use = pair.first;
90     if (std::find(acceptable.begin(), acceptable.end(), use->opcode()) ==
91             acceptable.end() &&
92         !use->IsNonSemantic() && !use->IsDebugInfo()) {
93       return _.diag(SPV_ERROR_INVALID_ID, use)
94              << "Invalid use of function result id " << _.getIdName(inst->id())
95              << ".";
96     }
97   }
98 
99   return SPV_SUCCESS;
100 }
101 
ValidateFunctionParameter(ValidationState_t & _,const Instruction * inst)102 spv_result_t ValidateFunctionParameter(ValidationState_t& _,
103                                        const Instruction* inst) {
104   // NOTE: Find OpFunction & ensure OpFunctionParameter is not out of place.
105   size_t param_index = 0;
106   size_t inst_num = inst->LineNum() - 1;
107   if (inst_num == 0) {
108     return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
109            << "Function parameter cannot be the first instruction.";
110   }
111 
112   auto func_inst = &_.ordered_instructions()[inst_num];
113   while (--inst_num) {
114     func_inst = &_.ordered_instructions()[inst_num];
115     if (func_inst->opcode() == SpvOpFunction) {
116       break;
117     } else if (func_inst->opcode() == SpvOpFunctionParameter) {
118       ++param_index;
119     }
120   }
121 
122   if (func_inst->opcode() != SpvOpFunction) {
123     return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
124            << "Function parameter must be preceded by a function.";
125   }
126 
127   const auto function_type_id = func_inst->GetOperandAs<uint32_t>(3);
128   const auto function_type = _.FindDef(function_type_id);
129   if (!function_type) {
130     return _.diag(SPV_ERROR_INVALID_ID, func_inst)
131            << "Missing function type definition.";
132   }
133   if (param_index >= function_type->words().size() - 3) {
134     return _.diag(SPV_ERROR_INVALID_ID, inst)
135            << "Too many OpFunctionParameters for " << func_inst->id()
136            << ": expected " << function_type->words().size() - 3
137            << " based on the function's type";
138   }
139 
140   const auto param_type =
141       _.FindDef(function_type->GetOperandAs<uint32_t>(param_index + 2));
142   if (!param_type || inst->type_id() != param_type->id()) {
143     return _.diag(SPV_ERROR_INVALID_ID, inst)
144            << "OpFunctionParameter Result Type <id> "
145            << _.getIdName(inst->type_id())
146            << " does not match the OpTypeFunction parameter "
147               "type of the same index.";
148   }
149 
150   // Validate that PhysicalStorageBuffer have one of Restrict, Aliased,
151   // RestrictPointer, or AliasedPointer.
152   auto param_nonarray_type_id = param_type->id();
153   while (_.GetIdOpcode(param_nonarray_type_id) == SpvOpTypeArray) {
154     param_nonarray_type_id =
155         _.FindDef(param_nonarray_type_id)->GetOperandAs<uint32_t>(1u);
156   }
157   if (_.GetIdOpcode(param_nonarray_type_id) == SpvOpTypePointer) {
158     auto param_nonarray_type = _.FindDef(param_nonarray_type_id);
159     if (param_nonarray_type->GetOperandAs<uint32_t>(1u) ==
160         SpvStorageClassPhysicalStorageBuffer) {
161       // check for Aliased or Restrict
162       const auto& decorations = _.id_decorations(inst->id());
163 
164       bool foundAliased = std::any_of(
165           decorations.begin(), decorations.end(), [](const Decoration& d) {
166             return SpvDecorationAliased == d.dec_type();
167           });
168 
169       bool foundRestrict = std::any_of(
170           decorations.begin(), decorations.end(), [](const Decoration& d) {
171             return SpvDecorationRestrict == d.dec_type();
172           });
173 
174       if (!foundAliased && !foundRestrict) {
175         return _.diag(SPV_ERROR_INVALID_ID, inst)
176                << "OpFunctionParameter " << inst->id()
177                << ": expected Aliased or Restrict for PhysicalStorageBuffer "
178                   "pointer.";
179       }
180       if (foundAliased && foundRestrict) {
181         return _.diag(SPV_ERROR_INVALID_ID, inst)
182                << "OpFunctionParameter " << inst->id()
183                << ": can't specify both Aliased and Restrict for "
184                   "PhysicalStorageBuffer pointer.";
185       }
186     } else {
187       const auto pointee_type_id =
188           param_nonarray_type->GetOperandAs<uint32_t>(2);
189       const auto pointee_type = _.FindDef(pointee_type_id);
190       if (SpvOpTypePointer == pointee_type->opcode() &&
191           pointee_type->GetOperandAs<uint32_t>(1u) ==
192               SpvStorageClassPhysicalStorageBuffer) {
193         // check for AliasedPointer/RestrictPointer
194         const auto& decorations = _.id_decorations(inst->id());
195 
196         bool foundAliased = std::any_of(
197             decorations.begin(), decorations.end(), [](const Decoration& d) {
198               return SpvDecorationAliasedPointer == d.dec_type();
199             });
200 
201         bool foundRestrict = std::any_of(
202             decorations.begin(), decorations.end(), [](const Decoration& d) {
203               return SpvDecorationRestrictPointer == d.dec_type();
204             });
205 
206         if (!foundAliased && !foundRestrict) {
207           return _.diag(SPV_ERROR_INVALID_ID, inst)
208                  << "OpFunctionParameter " << inst->id()
209                  << ": expected AliasedPointer or RestrictPointer for "
210                     "PhysicalStorageBuffer pointer.";
211         }
212         if (foundAliased && foundRestrict) {
213           return _.diag(SPV_ERROR_INVALID_ID, inst)
214                  << "OpFunctionParameter " << inst->id()
215                  << ": can't specify both AliasedPointer and "
216                     "RestrictPointer for PhysicalStorageBuffer pointer.";
217         }
218       }
219     }
220   }
221 
222   return SPV_SUCCESS;
223 }
224 
ValidateFunctionCall(ValidationState_t & _,const Instruction * inst)225 spv_result_t ValidateFunctionCall(ValidationState_t& _,
226                                   const Instruction* inst) {
227   const auto function_id = inst->GetOperandAs<uint32_t>(2);
228   const auto function = _.FindDef(function_id);
229   if (!function || SpvOpFunction != function->opcode()) {
230     return _.diag(SPV_ERROR_INVALID_ID, inst)
231            << "OpFunctionCall Function <id> " << _.getIdName(function_id)
232            << " is not a function.";
233   }
234 
235   auto return_type = _.FindDef(function->type_id());
236   if (!return_type || return_type->id() != inst->type_id()) {
237     return _.diag(SPV_ERROR_INVALID_ID, inst)
238            << "OpFunctionCall Result Type <id> " << _.getIdName(inst->type_id())
239            << "s type does not match Function <id> "
240            << _.getIdName(return_type->id()) << "s return type.";
241   }
242 
243   const auto function_type_id = function->GetOperandAs<uint32_t>(3);
244   const auto function_type = _.FindDef(function_type_id);
245   if (!function_type || function_type->opcode() != SpvOpTypeFunction) {
246     return _.diag(SPV_ERROR_INVALID_ID, inst)
247            << "Missing function type definition.";
248   }
249 
250   const auto function_call_arg_count = inst->words().size() - 4;
251   const auto function_param_count = function_type->words().size() - 3;
252   if (function_param_count != function_call_arg_count) {
253     return _.diag(SPV_ERROR_INVALID_ID, inst)
254            << "OpFunctionCall Function <id>'s parameter count does not match "
255               "the argument count.";
256   }
257 
258   for (size_t argument_index = 3, param_index = 2;
259        argument_index < inst->operands().size();
260        argument_index++, param_index++) {
261     const auto argument_id = inst->GetOperandAs<uint32_t>(argument_index);
262     const auto argument = _.FindDef(argument_id);
263     if (!argument) {
264       return _.diag(SPV_ERROR_INVALID_ID, inst)
265              << "Missing argument " << argument_index - 3 << " definition.";
266     }
267 
268     const auto argument_type = _.FindDef(argument->type_id());
269     if (!argument_type) {
270       return _.diag(SPV_ERROR_INVALID_ID, inst)
271              << "Missing argument " << argument_index - 3
272              << " type definition.";
273     }
274 
275     const auto parameter_type_id =
276         function_type->GetOperandAs<uint32_t>(param_index);
277     const auto parameter_type = _.FindDef(parameter_type_id);
278     if (!parameter_type || argument_type->id() != parameter_type->id()) {
279       if (!_.options()->before_hlsl_legalization ||
280           !DoPointeesLogicallyMatch(argument_type, parameter_type, _)) {
281         return _.diag(SPV_ERROR_INVALID_ID, inst)
282                << "OpFunctionCall Argument <id> " << _.getIdName(argument_id)
283                << "s type does not match Function <id> "
284                << _.getIdName(parameter_type_id) << "s parameter type.";
285       }
286     }
287 
288     if (_.addressing_model() == SpvAddressingModelLogical) {
289       if (parameter_type->opcode() == SpvOpTypePointer &&
290           !_.options()->relax_logical_pointer) {
291         SpvStorageClass sc = parameter_type->GetOperandAs<SpvStorageClass>(1u);
292         // Validate which storage classes can be pointer operands.
293         switch (sc) {
294           case SpvStorageClassUniformConstant:
295           case SpvStorageClassFunction:
296           case SpvStorageClassPrivate:
297           case SpvStorageClassWorkgroup:
298           case SpvStorageClassAtomicCounter:
299             // These are always allowed.
300             break;
301           case SpvStorageClassStorageBuffer:
302             if (!_.features().variable_pointers) {
303               return _.diag(SPV_ERROR_INVALID_ID, inst)
304                      << "StorageBuffer pointer operand "
305                      << _.getIdName(argument_id)
306                      << " requires a variable pointers capability";
307             }
308             break;
309           default:
310             return _.diag(SPV_ERROR_INVALID_ID, inst)
311                    << "Invalid storage class for pointer operand "
312                    << _.getIdName(argument_id);
313         }
314 
315         // Validate memory object declaration requirements.
316         if (argument->opcode() != SpvOpVariable &&
317             argument->opcode() != SpvOpFunctionParameter) {
318           const bool ssbo_vptr = _.features().variable_pointers &&
319                                  sc == SpvStorageClassStorageBuffer;
320           const bool wg_vptr = _.HasCapability(SpvCapabilityVariablePointers) &&
321                                sc == SpvStorageClassWorkgroup;
322           const bool uc_ptr = sc == SpvStorageClassUniformConstant;
323           if (!ssbo_vptr && !wg_vptr && !uc_ptr) {
324             return _.diag(SPV_ERROR_INVALID_ID, inst)
325                    << "Pointer operand " << _.getIdName(argument_id)
326                    << " must be a memory object declaration";
327           }
328         }
329       }
330     }
331   }
332   return SPV_SUCCESS;
333 }
334 
335 }  // namespace
336 
FunctionPass(ValidationState_t & _,const Instruction * inst)337 spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst) {
338   switch (inst->opcode()) {
339     case SpvOpFunction:
340       if (auto error = ValidateFunction(_, inst)) return error;
341       break;
342     case SpvOpFunctionParameter:
343       if (auto error = ValidateFunctionParameter(_, inst)) return error;
344       break;
345     case SpvOpFunctionCall:
346       if (auto error = ValidateFunctionCall(_, inst)) return error;
347       break;
348     default:
349       break;
350   }
351 
352   return SPV_SUCCESS;
353 }
354 
355 }  // namespace val
356 }  // namespace spvtools
357