• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2019 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 // This pass injects code in a graphics shader to implement guarantees
16 // satisfying Vulkan's robustBufferAccess rules.  Robust access rules permit
17 // an out-of-bounds access to be redirected to an access of the same type
18 // (load, store, etc.) but within the same root object.
19 //
20 // We assume baseline functionality in Vulkan, i.e. the module uses
21 // logical addressing mode, without VK_KHR_variable_pointers.
22 //
23 //    - Logical addressing mode implies:
24 //      - Each root pointer (a pointer that exists other than by the
25 //        execution of a shader instruction) is the result of an OpVariable.
26 //
27 //      - Instructions that result in pointers are:
28 //          OpVariable
29 //          OpAccessChain
30 //          OpInBoundsAccessChain
31 //          OpFunctionParameter
32 //          OpImageTexelPointer
33 //          OpCopyObject
34 //
35 //      - Instructions that use a pointer are:
36 //          OpLoad
37 //          OpStore
38 //          OpAccessChain
39 //          OpInBoundsAccessChain
40 //          OpFunctionCall
41 //          OpImageTexelPointer
42 //          OpCopyMemory
43 //          OpCopyObject
44 //          all OpAtomic* instructions
45 //
46 // We classify pointer-users into:
47 //  - Accesses:
48 //    - OpLoad
49 //    - OpStore
50 //    - OpAtomic*
51 //    - OpCopyMemory
52 //
53 //  - Address calculations:
54 //    - OpAccessChain
55 //    - OpInBoundsAccessChain
56 //
57 //  - Pass-through:
58 //    - OpFunctionCall
59 //    - OpFunctionParameter
60 //    - OpCopyObject
61 //
62 // The strategy is:
63 //
64 //  - Handle only logical addressing mode. In particular, don't handle a module
65 //    if it uses one of the variable-pointers capabilities.
66 //
67 //  - Don't handle modules using capability RuntimeDescriptorArrayEXT.  So the
68 //    only runtime arrays are those that are the last member in a
69 //    Block-decorated struct.  This allows us to feasibly/easily compute the
70 //    length of the runtime array. See below.
71 //
72 //  - The memory locations accessed by OpLoad, OpStore, OpCopyMemory, and
73 //    OpAtomic* are determined by their pointer parameter or parameters.
74 //    Pointers are always (correctly) typed and so the address and number of
75 //    consecutive locations are fully determined by the pointer.
76 //
77 //  - A pointer value originates as one of few cases:
78 //
79 //    - OpVariable for an interface object or an array of them: image,
80 //      buffer (UBO or SSBO), sampler, sampled-image, push-constant, input
81 //      variable, output variable. The execution environment is responsible for
82 //      allocating the correct amount of storage for these, and for ensuring
83 //      each resource bound to such a variable is big enough to contain the
84 //      SPIR-V pointee type of the variable.
85 //
86 //    - OpVariable for a non-interface object.  These are variables in
87 //      Workgroup, Private, and Function storage classes.  The compiler ensures
88 //      the underlying allocation is big enough to store the entire SPIR-V
89 //      pointee type of the variable.
90 //
91 //    - An OpFunctionParameter. This always maps to a pointer parameter to an
92 //      OpFunctionCall.
93 //
94 //      - In logical addressing mode, these are severely limited:
95 //        "Any pointer operand to an OpFunctionCall must be:
96 //          - a memory object declaration, or
97 //          - a pointer to an element in an array that is a memory object
98 //          declaration, where the element type is OpTypeSampler or OpTypeImage"
99 //
100 //      - This has an important simplifying consequence:
101 //
102 //        - When looking for a pointer to the structure containing a runtime
103 //          array, you begin with a pointer to the runtime array and trace
104 //          backward in the function.  You never have to trace back beyond
105 //          your function call boundary.  So you can't take a partial access
106 //          chain into an SSBO, then pass that pointer into a function.  So
107 //          we don't resort to using fat pointers to compute array length.
108 //          We can trace back to a pointer to the containing structure,
109 //          and use that in an OpArrayLength instruction. (The structure type
110 //          gives us the member index of the runtime array.)
111 //
112 //        - Otherwise, the pointer type fully encodes the range of valid
113 //          addresses. In particular, the type of a pointer to an aggregate
114 //          value fully encodes the range of indices when indexing into
115 //          that aggregate.
116 //
117 //    - The pointer is the result of an access chain instruction.  We clamp
118 //      indices contributing to address calculations.  As noted above, the
119 //      valid ranges are either bound by the length of a runtime array, or
120 //      by the type of the base pointer.  The length of a runtime array is
121 //      the result of an OpArrayLength instruction acting on the pointer of
122 //      the containing structure as noted above.
123 //
124 //      - Access chain indices are always treated as signed, so:
125 //        - Clamp the upper bound at the signed integer maximum.
126 //        - Use SClamp for all clamping.
127 //
128 //    - TODO(dneto): OpImageTexelPointer:
129 //      - Clamp coordinate to the image size returned by OpImageQuerySize
130 //      - If multi-sampled, clamp the sample index to the count returned by
131 //        OpImageQuerySamples.
132 //      - If not multi-sampled, set the sample index to 0.
133 //
134 //  - Rely on the external validator to check that pointers are only
135 //    used by the instructions as above.
136 //
137 //  - Handles OpTypeRuntimeArray
138 //    Track pointer back to original resource (pointer to struct), so we can
139 //    query the runtime array size.
140 //
141 
142 #include "graphics_robust_access_pass.h"
143 
144 #include <functional>
145 #include <initializer_list>
146 #include <utility>
147 
148 #include "function.h"
149 #include "ir_context.h"
150 #include "pass.h"
151 #include "source/diagnostic.h"
152 #include "source/util/make_unique.h"
153 #include "spirv-tools/libspirv.h"
154 #include "spirv/unified1/GLSL.std.450.h"
155 #include "type_manager.h"
156 #include "types.h"
157 
158 namespace spvtools {
159 namespace opt {
160 
161 using opt::Instruction;
162 using opt::Operand;
163 using spvtools::MakeUnique;
164 
GraphicsRobustAccessPass()165 GraphicsRobustAccessPass::GraphicsRobustAccessPass() : module_status_() {}
166 
Process()167 Pass::Status GraphicsRobustAccessPass::Process() {
168   module_status_ = PerModuleState();
169 
170   ProcessCurrentModule();
171 
172   auto result = module_status_.failed
173                     ? Status::Failure
174                     : (module_status_.modified ? Status::SuccessWithChange
175                                                : Status::SuccessWithoutChange);
176 
177   return result;
178 }
179 
Fail()180 spvtools::DiagnosticStream GraphicsRobustAccessPass::Fail() {
181   module_status_.failed = true;
182   // We don't really have a position, and we'll ignore the result.
183   return std::move(
184       spvtools::DiagnosticStream({}, consumer(), "", SPV_ERROR_INVALID_BINARY)
185       << name() << ": ");
186 }
187 
IsCompatibleModule()188 spv_result_t GraphicsRobustAccessPass::IsCompatibleModule() {
189   auto* feature_mgr = context()->get_feature_mgr();
190   if (!feature_mgr->HasCapability(spv::Capability::Shader))
191     return Fail() << "Can only process Shader modules";
192   if (feature_mgr->HasCapability(spv::Capability::VariablePointers))
193     return Fail() << "Can't process modules with VariablePointers capability";
194   if (feature_mgr->HasCapability(
195           spv::Capability::VariablePointersStorageBuffer))
196     return Fail() << "Can't process modules with VariablePointersStorageBuffer "
197                      "capability";
198   if (feature_mgr->HasCapability(spv::Capability::RuntimeDescriptorArrayEXT)) {
199     // These have a RuntimeArray outside of Block-decorated struct.  There
200     // is no way to compute the array length from within SPIR-V.
201     return Fail() << "Can't process modules with RuntimeDescriptorArrayEXT "
202                      "capability";
203   }
204 
205   {
206     auto* inst = context()->module()->GetMemoryModel();
207     const auto addressing_model =
208         spv::AddressingModel(inst->GetSingleWordOperand(0));
209     if (addressing_model != spv::AddressingModel::Logical)
210       return Fail() << "Addressing model must be Logical.  Found "
211                     << inst->PrettyPrint();
212   }
213   return SPV_SUCCESS;
214 }
215 
ProcessCurrentModule()216 spv_result_t GraphicsRobustAccessPass::ProcessCurrentModule() {
217   auto err = IsCompatibleModule();
218   if (err != SPV_SUCCESS) return err;
219 
220   ProcessFunction fn = [this](opt::Function* f) { return ProcessAFunction(f); };
221   module_status_.modified |= context()->ProcessReachableCallTree(fn);
222 
223   // Need something here.  It's the price we pay for easier failure paths.
224   return SPV_SUCCESS;
225 }
226 
ProcessAFunction(opt::Function * function)227 bool GraphicsRobustAccessPass::ProcessAFunction(opt::Function* function) {
228   // Ensure that all pointers computed inside a function are within bounds.
229   // Find the access chains in this block before trying to modify them.
230   std::vector<Instruction*> access_chains;
231   std::vector<Instruction*> image_texel_pointers;
232   for (auto& block : *function) {
233     for (auto& inst : block) {
234       switch (inst.opcode()) {
235         case spv::Op::OpAccessChain:
236         case spv::Op::OpInBoundsAccessChain:
237           access_chains.push_back(&inst);
238           break;
239         case spv::Op::OpImageTexelPointer:
240           image_texel_pointers.push_back(&inst);
241           break;
242         default:
243           break;
244       }
245     }
246   }
247   for (auto* inst : access_chains) {
248     ClampIndicesForAccessChain(inst);
249     if (module_status_.failed) return module_status_.modified;
250   }
251 
252   for (auto* inst : image_texel_pointers) {
253     if (SPV_SUCCESS != ClampCoordinateForImageTexelPointer(inst)) break;
254   }
255   return module_status_.modified;
256 }
257 
ClampIndicesForAccessChain(Instruction * access_chain)258 void GraphicsRobustAccessPass::ClampIndicesForAccessChain(
259     Instruction* access_chain) {
260   Instruction& inst = *access_chain;
261 
262   auto* constant_mgr = context()->get_constant_mgr();
263   auto* def_use_mgr = context()->get_def_use_mgr();
264   auto* type_mgr = context()->get_type_mgr();
265   const bool have_int64_cap =
266       context()->get_feature_mgr()->HasCapability(spv::Capability::Int64);
267 
268   // Replaces one of the OpAccessChain index operands with a new value.
269   // Updates def-use analysis.
270   auto replace_index = [this, &inst, def_use_mgr](uint32_t operand_index,
271                                                   Instruction* new_value) {
272     inst.SetOperand(operand_index, {new_value->result_id()});
273     def_use_mgr->AnalyzeInstUse(&inst);
274     module_status_.modified = true;
275     return SPV_SUCCESS;
276   };
277 
278   // Replaces one of the OpAccesssChain index operands with a clamped value.
279   // Replace the operand at |operand_index| with the value computed from
280   // signed_clamp(%old_value, %min_value, %max_value).  It also analyzes
281   // the new instruction and records that them module is modified.
282   // Assumes %min_value is signed-less-or-equal than %max_value. (All callees
283   // use 0 for %min_value).
284   auto clamp_index = [&inst, type_mgr, this, &replace_index](
285                          uint32_t operand_index, Instruction* old_value,
286                          Instruction* min_value, Instruction* max_value) {
287     auto* clamp_inst =
288         MakeSClampInst(*type_mgr, old_value, min_value, max_value, &inst);
289     return replace_index(operand_index, clamp_inst);
290   };
291 
292   // Ensures the specified index of access chain |inst| has a value that is
293   // at most |count| - 1.  If the index is already a constant value less than
294   // |count| then no change is made.
295   auto clamp_to_literal_count =
296       [&inst, this, &constant_mgr, &type_mgr, have_int64_cap, &replace_index,
297        &clamp_index](uint32_t operand_index, uint64_t count) -> spv_result_t {
298     Instruction* index_inst =
299         this->GetDef(inst.GetSingleWordOperand(operand_index));
300     const auto* index_type =
301         type_mgr->GetType(index_inst->type_id())->AsInteger();
302     assert(index_type);
303     const auto index_width = index_type->width();
304 
305     if (count <= 1) {
306       // Replace the index with 0.
307       return replace_index(operand_index, GetValueForType(0, index_type));
308     }
309 
310     uint64_t maxval = count - 1;
311 
312     // Compute the bit width of a viable type to hold |maxval|.
313     // Look for a bit width, up to 64 bits wide, to fit maxval.
314     uint32_t maxval_width = index_width;
315     while ((maxval_width < 64) && (0 != (maxval >> maxval_width))) {
316       maxval_width *= 2;
317     }
318     // Determine the type for |maxval|.
319     uint32_t next_id = context()->module()->IdBound();
320     analysis::Integer signed_type_for_query(maxval_width, true);
321     auto* maxval_type =
322         type_mgr->GetRegisteredType(&signed_type_for_query)->AsInteger();
323     if (next_id != context()->module()->IdBound()) {
324       module_status_.modified = true;
325     }
326     // Access chain indices are treated as signed, so limit the maximum value
327     // of the index so it will always be positive for a signed clamp operation.
328     maxval = std::min(maxval, ((uint64_t(1) << (maxval_width - 1)) - 1));
329 
330     if (index_width > 64) {
331       return this->Fail() << "Can't handle indices wider than 64 bits, found "
332                              "constant index with "
333                           << index_width << " bits as index number "
334                           << operand_index << " of access chain "
335                           << inst.PrettyPrint();
336     }
337 
338     // Split into two cases: the current index is a constant, or not.
339 
340     // If the index is a constant then |index_constant| will not be a null
341     // pointer.  (If index is an |OpConstantNull| then it |index_constant| will
342     // not be a null pointer.)  Since access chain indices must be scalar
343     // integers, this can't be a spec constant.
344     if (auto* index_constant = constant_mgr->GetConstantFromInst(index_inst)) {
345       auto* int_index_constant = index_constant->AsIntConstant();
346       int64_t value = 0;
347       // OpAccessChain indices are treated as signed.  So get the signed
348       // constant value here.
349       if (index_width <= 32) {
350         value = int64_t(int_index_constant->GetS32BitValue());
351       } else if (index_width <= 64) {
352         value = int_index_constant->GetS64BitValue();
353       }
354       if (value < 0) {
355         return replace_index(operand_index, GetValueForType(0, index_type));
356       } else if (uint64_t(value) <= maxval) {
357         // Nothing to do.
358         return SPV_SUCCESS;
359       } else {
360         // Replace with maxval.
361         assert(count > 0);  // Already took care of this case above.
362         return replace_index(operand_index,
363                              GetValueForType(maxval, maxval_type));
364       }
365     } else {
366       // Generate a clamp instruction.
367       assert(maxval >= 1);
368       assert(index_width <= 64);  // Otherwise, already returned above.
369       if (index_width >= 64 && !have_int64_cap) {
370         // An inconsistent module.
371         return Fail() << "Access chain index is wider than 64 bits, but Int64 "
372                          "is not declared: "
373                       << index_inst->PrettyPrint();
374       }
375       // Widen the index value if necessary
376       if (maxval_width > index_width) {
377         // Find the wider type.  We only need this case if a constant array
378         // bound is too big.
379 
380         // From how we calculated maxval_width, widening won't require adding
381         // the Int64 capability.
382         assert(have_int64_cap || maxval_width <= 32);
383         if (!have_int64_cap && maxval_width >= 64) {
384           // Be defensive, but this shouldn't happen.
385           return this->Fail()
386                  << "Clamping index would require adding Int64 capability. "
387                  << "Can't clamp 32-bit index " << operand_index
388                  << " of access chain " << inst.PrettyPrint();
389         }
390         index_inst = WidenInteger(index_type->IsSigned(), maxval_width,
391                                   index_inst, &inst);
392       }
393 
394       // Finally, clamp the index.
395       return clamp_index(operand_index, index_inst,
396                          GetValueForType(0, maxval_type),
397                          GetValueForType(maxval, maxval_type));
398     }
399     return SPV_SUCCESS;
400   };
401 
402   // Ensures the specified index of access chain |inst| has a value that is at
403   // most the value of |count_inst| minus 1, where |count_inst| is treated as an
404   // unsigned integer. This can log a failure.
405   auto clamp_to_count = [&inst, this, &constant_mgr, &clamp_to_literal_count,
406                          &clamp_index,
407                          &type_mgr](uint32_t operand_index,
408                                     Instruction* count_inst) -> spv_result_t {
409     Instruction* index_inst =
410         this->GetDef(inst.GetSingleWordOperand(operand_index));
411     const auto* index_type =
412         type_mgr->GetType(index_inst->type_id())->AsInteger();
413     const auto* count_type =
414         type_mgr->GetType(count_inst->type_id())->AsInteger();
415     assert(index_type);
416     if (const auto* count_constant =
417             constant_mgr->GetConstantFromInst(count_inst)) {
418       uint64_t value = 0;
419       const auto width = count_constant->type()->AsInteger()->width();
420       if (width <= 32) {
421         value = count_constant->AsIntConstant()->GetU32BitValue();
422       } else if (width <= 64) {
423         value = count_constant->AsIntConstant()->GetU64BitValue();
424       } else {
425         return this->Fail() << "Can't handle indices wider than 64 bits, found "
426                                "constant index with "
427                             << index_type->width() << "bits";
428       }
429       return clamp_to_literal_count(operand_index, value);
430     } else {
431       // Widen them to the same width.
432       const auto index_width = index_type->width();
433       const auto count_width = count_type->width();
434       const auto target_width = std::max(index_width, count_width);
435       // UConvert requires the result type to have 0 signedness.  So enforce
436       // that here.
437       auto* wider_type = index_width < count_width ? count_type : index_type;
438       if (index_type->width() < target_width) {
439         // Access chain indices are treated as signed integers.
440         index_inst = WidenInteger(true, target_width, index_inst, &inst);
441       } else if (count_type->width() < target_width) {
442         // Assume type sizes are treated as unsigned.
443         count_inst = WidenInteger(false, target_width, count_inst, &inst);
444       }
445       // Compute count - 1.
446       // It doesn't matter if 1 is signed or unsigned.
447       auto* one = GetValueForType(1, wider_type);
448       auto* count_minus_1 = InsertInst(
449           &inst, spv::Op::OpISub, type_mgr->GetId(wider_type), TakeNextId(),
450           {{SPV_OPERAND_TYPE_ID, {count_inst->result_id()}},
451            {SPV_OPERAND_TYPE_ID, {one->result_id()}}});
452       auto* zero = GetValueForType(0, wider_type);
453       // Make sure we clamp to an upper bound that is at most the signed max
454       // for the target type.
455       const uint64_t max_signed_value =
456           ((uint64_t(1) << (target_width - 1)) - 1);
457       // Use unsigned-min to ensure that the result is always non-negative.
458       // That ensures we satisfy the invariant for SClamp, where the "min"
459       // argument we give it (zero), is no larger than the third argument.
460       auto* upper_bound =
461           MakeUMinInst(*type_mgr, count_minus_1,
462                        GetValueForType(max_signed_value, wider_type), &inst);
463       // Now clamp the index to this upper bound.
464       return clamp_index(operand_index, index_inst, zero, upper_bound);
465     }
466     return SPV_SUCCESS;
467   };
468 
469   const Instruction* base_inst = GetDef(inst.GetSingleWordInOperand(0));
470   const Instruction* base_type = GetDef(base_inst->type_id());
471   Instruction* pointee_type = GetDef(base_type->GetSingleWordInOperand(1));
472 
473   // Walk the indices from earliest to latest, replacing indices with a
474   // clamped value, and updating the pointee_type.  The order matters for
475   // the case when we have to compute the length of a runtime array.  In
476   // that the algorithm relies on the fact that that the earlier indices
477   // have already been clamped.
478   const uint32_t num_operands = inst.NumOperands();
479   for (uint32_t idx = 3; !module_status_.failed && idx < num_operands; ++idx) {
480     const uint32_t index_id = inst.GetSingleWordOperand(idx);
481     Instruction* index_inst = GetDef(index_id);
482 
483     switch (pointee_type->opcode()) {
484       case spv::Op::OpTypeMatrix:  // Use column count
485       case spv::Op::OpTypeVector:  // Use component count
486       {
487         const uint32_t count = pointee_type->GetSingleWordOperand(2);
488         clamp_to_literal_count(idx, count);
489         pointee_type = GetDef(pointee_type->GetSingleWordOperand(1));
490       } break;
491 
492       case spv::Op::OpTypeArray: {
493         // The array length can be a spec constant, so go through the general
494         // case.
495         Instruction* array_len = GetDef(pointee_type->GetSingleWordOperand(2));
496         clamp_to_count(idx, array_len);
497         pointee_type = GetDef(pointee_type->GetSingleWordOperand(1));
498       } break;
499 
500       case spv::Op::OpTypeStruct: {
501         // SPIR-V requires the index to be an OpConstant.
502         // We need to know the index literal value so we can compute the next
503         // pointee type.
504         if (index_inst->opcode() != spv::Op::OpConstant ||
505             !constant_mgr->GetConstantFromInst(index_inst)
506                  ->type()
507                  ->AsInteger()) {
508           Fail() << "Member index into struct is not a constant integer: "
509                  << index_inst->PrettyPrint(
510                         SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
511                  << "\nin access chain: "
512                  << inst.PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
513           return;
514         }
515         const auto num_members = pointee_type->NumInOperands();
516         const auto* index_constant =
517             constant_mgr->GetConstantFromInst(index_inst);
518         // Get the sign-extended value, since access index is always treated as
519         // signed.
520         const auto index_value = index_constant->GetSignExtendedValue();
521         if (index_value < 0 || index_value >= num_members) {
522           Fail() << "Member index " << index_value
523                  << " is out of bounds for struct type: "
524                  << pointee_type->PrettyPrint(
525                         SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
526                  << "\nin access chain: "
527                  << inst.PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
528           return;
529         }
530         pointee_type = GetDef(pointee_type->GetSingleWordInOperand(
531             static_cast<uint32_t>(index_value)));
532         // No need to clamp this index.  We just checked that it's valid.
533       } break;
534 
535       case spv::Op::OpTypeRuntimeArray: {
536         auto* array_len = MakeRuntimeArrayLengthInst(&inst, idx);
537         if (!array_len) {  // We've already signaled an error.
538           return;
539         }
540         clamp_to_count(idx, array_len);
541         if (module_status_.failed) return;
542         pointee_type = GetDef(pointee_type->GetSingleWordOperand(1));
543       } break;
544 
545       default:
546         Fail() << " Unhandled pointee type for access chain "
547                << pointee_type->PrettyPrint(
548                       SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
549     }
550   }
551 }
552 
GetGlslInsts()553 uint32_t GraphicsRobustAccessPass::GetGlslInsts() {
554   if (module_status_.glsl_insts_id == 0) {
555     // This string serves double-duty as raw data for a string and for a vector
556     // of 32-bit words
557     const char glsl[] = "GLSL.std.450";
558     // Use an existing import if we can.
559     for (auto& inst : context()->module()->ext_inst_imports()) {
560       if (inst.GetInOperand(0).AsString() == glsl) {
561         module_status_.glsl_insts_id = inst.result_id();
562       }
563     }
564     if (module_status_.glsl_insts_id == 0) {
565       // Make a new import instruction.
566       module_status_.glsl_insts_id = TakeNextId();
567       std::vector<uint32_t> words = spvtools::utils::MakeVector(glsl);
568       auto import_inst = MakeUnique<Instruction>(
569           context(), spv::Op::OpExtInstImport, 0, module_status_.glsl_insts_id,
570           std::initializer_list<Operand>{
571               Operand{SPV_OPERAND_TYPE_LITERAL_STRING, std::move(words)}});
572       Instruction* inst = import_inst.get();
573       context()->module()->AddExtInstImport(std::move(import_inst));
574       module_status_.modified = true;
575       context()->AnalyzeDefUse(inst);
576       // Invalidates the feature manager, since we added an extended instruction
577       // set import.
578       context()->ResetFeatureManager();
579     }
580   }
581   return module_status_.glsl_insts_id;
582 }
583 
GetValueForType(uint64_t value,const analysis::Integer * type)584 opt::Instruction* opt::GraphicsRobustAccessPass::GetValueForType(
585     uint64_t value, const analysis::Integer* type) {
586   auto* mgr = context()->get_constant_mgr();
587   assert(type->width() <= 64);
588   std::vector<uint32_t> words;
589   words.push_back(uint32_t(value));
590   if (type->width() > 32) {
591     words.push_back(uint32_t(value >> 32u));
592   }
593   const auto* constant = mgr->GetConstant(type, words);
594   return mgr->GetDefiningInstruction(
595       constant, context()->get_type_mgr()->GetTypeInstruction(type));
596 }
597 
WidenInteger(bool sign_extend,uint32_t bit_width,Instruction * value,Instruction * before_inst)598 opt::Instruction* opt::GraphicsRobustAccessPass::WidenInteger(
599     bool sign_extend, uint32_t bit_width, Instruction* value,
600     Instruction* before_inst) {
601   analysis::Integer unsigned_type_for_query(bit_width, false);
602   auto* type_mgr = context()->get_type_mgr();
603   auto* unsigned_type = type_mgr->GetRegisteredType(&unsigned_type_for_query);
604   auto type_id = context()->get_type_mgr()->GetId(unsigned_type);
605   auto conversion_id = TakeNextId();
606   auto* conversion = InsertInst(
607       before_inst, (sign_extend ? spv::Op::OpSConvert : spv::Op::OpUConvert),
608       type_id, conversion_id, {{SPV_OPERAND_TYPE_ID, {value->result_id()}}});
609   return conversion;
610 }
611 
MakeUMinInst(const analysis::TypeManager & tm,Instruction * x,Instruction * y,Instruction * where)612 Instruction* GraphicsRobustAccessPass::MakeUMinInst(
613     const analysis::TypeManager& tm, Instruction* x, Instruction* y,
614     Instruction* where) {
615   // Get IDs of instructions we'll be referencing. Evaluate them before calling
616   // the function so we force a deterministic ordering in case both of them need
617   // to take a new ID.
618   const uint32_t glsl_insts_id = GetGlslInsts();
619   uint32_t smin_id = TakeNextId();
620   const auto xwidth = tm.GetType(x->type_id())->AsInteger()->width();
621   const auto ywidth = tm.GetType(y->type_id())->AsInteger()->width();
622   assert(xwidth == ywidth);
623   (void)xwidth;
624   (void)ywidth;
625   auto* smin_inst = InsertInst(
626       where, spv::Op::OpExtInst, x->type_id(), smin_id,
627       {
628           {SPV_OPERAND_TYPE_ID, {glsl_insts_id}},
629           {SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {GLSLstd450UMin}},
630           {SPV_OPERAND_TYPE_ID, {x->result_id()}},
631           {SPV_OPERAND_TYPE_ID, {y->result_id()}},
632       });
633   return smin_inst;
634 }
635 
MakeSClampInst(const analysis::TypeManager & tm,Instruction * x,Instruction * min,Instruction * max,Instruction * where)636 Instruction* GraphicsRobustAccessPass::MakeSClampInst(
637     const analysis::TypeManager& tm, Instruction* x, Instruction* min,
638     Instruction* max, Instruction* where) {
639   // Get IDs of instructions we'll be referencing. Evaluate them before calling
640   // the function so we force a deterministic ordering in case both of them need
641   // to take a new ID.
642   const uint32_t glsl_insts_id = GetGlslInsts();
643   uint32_t clamp_id = TakeNextId();
644   const auto xwidth = tm.GetType(x->type_id())->AsInteger()->width();
645   const auto minwidth = tm.GetType(min->type_id())->AsInteger()->width();
646   const auto maxwidth = tm.GetType(max->type_id())->AsInteger()->width();
647   assert(xwidth == minwidth);
648   assert(xwidth == maxwidth);
649   (void)xwidth;
650   (void)minwidth;
651   (void)maxwidth;
652   auto* clamp_inst = InsertInst(
653       where, spv::Op::OpExtInst, x->type_id(), clamp_id,
654       {
655           {SPV_OPERAND_TYPE_ID, {glsl_insts_id}},
656           {SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {GLSLstd450SClamp}},
657           {SPV_OPERAND_TYPE_ID, {x->result_id()}},
658           {SPV_OPERAND_TYPE_ID, {min->result_id()}},
659           {SPV_OPERAND_TYPE_ID, {max->result_id()}},
660       });
661   return clamp_inst;
662 }
663 
MakeRuntimeArrayLengthInst(Instruction * access_chain,uint32_t operand_index)664 Instruction* GraphicsRobustAccessPass::MakeRuntimeArrayLengthInst(
665     Instruction* access_chain, uint32_t operand_index) {
666   // The Index parameter to the access chain at |operand_index| is indexing
667   // *into* the runtime-array.  To get the number of elements in the runtime
668   // array we need a pointer to the Block-decorated struct that contains the
669   // runtime array. So conceptually we have to go 2 steps backward in the
670   // access chain.  The two steps backward might forces us to traverse backward
671   // across multiple dominating instructions.
672   auto* type_mgr = context()->get_type_mgr();
673 
674   // How many access chain indices do we have to unwind to find the pointer
675   // to the struct containing the runtime array?
676   uint32_t steps_remaining = 2;
677   // Find or create an instruction computing the pointer to the structure
678   // containing the runtime array.
679   // Walk backward through pointer address calculations until we either get
680   // to exactly the right base pointer, or to an access chain instruction
681   // that we can replicate but truncate to compute the address of the right
682   // struct.
683   Instruction* current_access_chain = access_chain;
684   Instruction* pointer_to_containing_struct = nullptr;
685   while (steps_remaining > 0) {
686     switch (current_access_chain->opcode()) {
687       case spv::Op::OpCopyObject:
688         // Whoops. Walk right through this one.
689         current_access_chain =
690             GetDef(current_access_chain->GetSingleWordInOperand(0));
691         break;
692       case spv::Op::OpAccessChain:
693       case spv::Op::OpInBoundsAccessChain: {
694         const int first_index_operand = 3;
695         // How many indices in this access chain contribute to getting us
696         // to an element in the runtime array?
697         const auto num_contributing_indices =
698             current_access_chain == access_chain
699                 ? operand_index - (first_index_operand - 1)
700                 : current_access_chain->NumInOperands() - 1 /* skip the base */;
701         Instruction* base =
702             GetDef(current_access_chain->GetSingleWordInOperand(0));
703         if (num_contributing_indices == steps_remaining) {
704           // The base pointer points to the structure.
705           pointer_to_containing_struct = base;
706           steps_remaining = 0;
707           break;
708         } else if (num_contributing_indices < steps_remaining) {
709           // Peel off the index and keep going backward.
710           steps_remaining -= num_contributing_indices;
711           current_access_chain = base;
712         } else {
713           // This access chain has more indices than needed.  Generate a new
714           // access chain instruction, but truncating the list of indices.
715           const int base_operand = 2;
716           // We'll use the base pointer and the indices up to but not including
717           // the one indexing into the runtime array.
718           Instruction::OperandList ops;
719           // Use the base pointer
720           ops.push_back(current_access_chain->GetOperand(base_operand));
721           const uint32_t num_indices_to_keep =
722               num_contributing_indices - steps_remaining - 1;
723           for (uint32_t i = 0; i <= num_indices_to_keep; i++) {
724             ops.push_back(
725                 current_access_chain->GetOperand(first_index_operand + i));
726           }
727           // Compute the type of the result of the new access chain.  Start at
728           // the base and walk the indices in a forward direction.
729           auto* constant_mgr = context()->get_constant_mgr();
730           std::vector<uint32_t> indices_for_type;
731           for (uint32_t i = 0; i < ops.size() - 1; i++) {
732             uint32_t index_for_type_calculation = 0;
733             Instruction* index =
734                 GetDef(current_access_chain->GetSingleWordOperand(
735                     first_index_operand + i));
736             if (auto* index_constant =
737                     constant_mgr->GetConstantFromInst(index)) {
738               // We only need 32 bits. For the type calculation, it's sufficient
739               // to take the zero-extended value. It only matters for the struct
740               // case, and struct member indices are unsigned.
741               index_for_type_calculation =
742                   uint32_t(index_constant->GetZeroExtendedValue());
743             } else {
744               // Indexing into a variably-sized thing like an array.  Use 0.
745               index_for_type_calculation = 0;
746             }
747             indices_for_type.push_back(index_for_type_calculation);
748           }
749           auto* base_ptr_type = type_mgr->GetType(base->type_id())->AsPointer();
750           auto* base_pointee_type = base_ptr_type->pointee_type();
751           auto* new_access_chain_result_pointee_type =
752               type_mgr->GetMemberType(base_pointee_type, indices_for_type);
753           const uint32_t new_access_chain_type_id = type_mgr->FindPointerToType(
754               type_mgr->GetId(new_access_chain_result_pointee_type),
755               base_ptr_type->storage_class());
756 
757           // Create the instruction and insert it.
758           const auto new_access_chain_id = TakeNextId();
759           auto* new_access_chain =
760               InsertInst(current_access_chain, current_access_chain->opcode(),
761                          new_access_chain_type_id, new_access_chain_id, ops);
762           pointer_to_containing_struct = new_access_chain;
763           steps_remaining = 0;
764           break;
765         }
766       } break;
767       default:
768         Fail() << "Unhandled access chain in logical addressing mode passes "
769                   "through "
770                << current_access_chain->PrettyPrint(
771                       SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET |
772                       SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
773         return nullptr;
774     }
775   }
776   assert(pointer_to_containing_struct);
777   auto* pointee_type =
778       type_mgr->GetType(pointer_to_containing_struct->type_id())
779           ->AsPointer()
780           ->pointee_type();
781 
782   auto* struct_type = pointee_type->AsStruct();
783   const uint32_t member_index_of_runtime_array =
784       uint32_t(struct_type->element_types().size() - 1);
785   // Create the length-of-array instruction before the original access chain,
786   // but after the generation of the pointer to the struct.
787   const auto array_len_id = TakeNextId();
788   analysis::Integer uint_type_for_query(32, false);
789   auto* uint_type = type_mgr->GetRegisteredType(&uint_type_for_query);
790   auto* array_len = InsertInst(
791       access_chain, spv::Op::OpArrayLength, type_mgr->GetId(uint_type),
792       array_len_id,
793       {{SPV_OPERAND_TYPE_ID, {pointer_to_containing_struct->result_id()}},
794        {SPV_OPERAND_TYPE_LITERAL_INTEGER, {member_index_of_runtime_array}}});
795   return array_len;
796 }
797 
ClampCoordinateForImageTexelPointer(opt::Instruction * image_texel_pointer)798 spv_result_t GraphicsRobustAccessPass::ClampCoordinateForImageTexelPointer(
799     opt::Instruction* image_texel_pointer) {
800   // TODO(dneto): Write tests for this code.
801   // TODO(dneto): Use signed-clamp
802   (void)(image_texel_pointer);
803   return SPV_SUCCESS;
804 
805   // Do not compile this code until it is ready to be used.
806 #if 0
807   // Example:
808   //   %texel_ptr = OpImageTexelPointer %texel_ptr_type %image_ptr %coord
809   //   %sample
810   //
811   // We want to clamp %coord components between vector-0 and the result
812   // of OpImageQuerySize acting on the underlying image.  So insert:
813   //     %image = OpLoad %image_type %image_ptr
814   //     %query_size = OpImageQuerySize %query_size_type %image
815   //
816   // For a multi-sampled image, %sample is the sample index, and we need
817   // to clamp it between zero and the number of samples in the image.
818   //     %sample_count = OpImageQuerySamples %uint %image
819   //     %max_sample_index = OpISub %uint %sample_count %uint_1
820   // For non-multi-sampled images, the sample index must be constant zero.
821 
822   auto* def_use_mgr = context()->get_def_use_mgr();
823   auto* type_mgr = context()->get_type_mgr();
824   auto* constant_mgr = context()->get_constant_mgr();
825 
826   auto* image_ptr = GetDef(image_texel_pointer->GetSingleWordInOperand(0));
827   auto* image_ptr_type = GetDef(image_ptr->type_id());
828   auto image_type_id = image_ptr_type->GetSingleWordInOperand(1);
829   auto* image_type = GetDef(image_type_id);
830   auto* coord = GetDef(image_texel_pointer->GetSingleWordInOperand(1));
831   auto* samples = GetDef(image_texel_pointer->GetSingleWordInOperand(2));
832 
833   // We will modify the module, at least by adding image query instructions.
834   module_status_.modified = true;
835 
836   // Declare the ImageQuery capability if the module doesn't already have it.
837   auto* feature_mgr = context()->get_feature_mgr();
838   if (!feature_mgr->HasCapability(spv::Capability::ImageQuery)) {
839     auto cap = MakeUnique<Instruction>(
840         context(), spv::Op::OpCapability, 0, 0,
841         std::initializer_list<Operand>{
842             {SPV_OPERAND_TYPE_CAPABILITY, {spv::Capability::ImageQuery}}});
843     def_use_mgr->AnalyzeInstDefUse(cap.get());
844     context()->AddCapability(std::move(cap));
845     feature_mgr->Analyze(context()->module());
846   }
847 
848   // OpImageTexelPointer is used to translate a coordinate and sample index
849   // into an address for use with an atomic operation.  That is, it may only
850   // used with what Vulkan calls a "storage image"
851   // (OpTypeImage parameter Sampled=2).
852   // Note: A storage image never has a level-of-detail associated with it.
853 
854   // Constraints on the sample id:
855   //  - Only 2D images can be multi-sampled: OpTypeImage parameter MS=1
856   //    only if Dim=2D.
857   //  - Non-multi-sampled images (OpTypeImage parameter MS=0) must use
858   //    sample ID to a constant 0.
859 
860   // The coordinate is treated as unsigned, and should be clamped against the
861   // image "size", returned by OpImageQuerySize. (Note: OpImageQuerySizeLod
862   // is only usable with a sampled image, i.e. its image type has Sampled=1).
863 
864   // Determine the result type for the OpImageQuerySize.
865   // For non-arrayed images:
866   //   non-Cube:
867   //     - Always the same as the coordinate type
868   //   Cube:
869   //     - Use all but the last component of the coordinate (which is the face
870   //       index from 0 to 5).
871   // For arrayed images (in Vulkan the Dim is 1D, 2D, or Cube):
872   //   non-Cube:
873   //     - A vector with the components in the coordinate, and one more for
874   //       the layer index.
875   //   Cube:
876   //     - The same as the coordinate type: 3-element integer vector.
877   //     - The third component from the size query is the layer count.
878   //     - The third component in the texel pointer calculation is
879   //       6 * layer + face, where 0 <= face < 6.
880   //   Cube: Use all but the last component of the coordinate (which is the face
881   //   index from 0 to 5).
882   const auto dim = SpvDim(image_type->GetSingleWordInOperand(1));
883   const bool arrayed = image_type->GetSingleWordInOperand(3) == 1;
884   const bool multisampled = image_type->GetSingleWordInOperand(4) != 0;
885   const auto query_num_components = [dim, arrayed, this]() -> int {
886     const int arrayness_bonus = arrayed ? 1 : 0;
887     int num_coords = 0;
888     switch (dim) {
889       case spv::Dim::Buffer:
890       case SpvDim1D:
891         num_coords = 1;
892         break;
893       case spv::Dim::Cube:
894         // For cube, we need bounds for x, y, but not face.
895       case spv::Dim::Rect:
896       case SpvDim2D:
897         num_coords = 2;
898         break;
899       case SpvDim3D:
900         num_coords = 3;
901         break;
902       case spv::Dim::SubpassData:
903       case spv::Dim::Max:
904         return Fail() << "Invalid image dimension for OpImageTexelPointer: "
905                       << int(dim);
906         break;
907     }
908     return num_coords + arrayness_bonus;
909   }();
910   const auto* coord_component_type = [type_mgr, coord]() {
911     const analysis::Type* coord_type = type_mgr->GetType(coord->type_id());
912     if (auto* vector_type = coord_type->AsVector()) {
913       return vector_type->element_type()->AsInteger();
914     }
915     return coord_type->AsInteger();
916   }();
917   // For now, only handle 32-bit case for coordinates.
918   if (!coord_component_type) {
919     return Fail() << " Coordinates for OpImageTexelPointer are not integral: "
920                   << image_texel_pointer->PrettyPrint(
921                          SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
922   }
923   if (coord_component_type->width() != 32) {
924     return Fail() << " Expected OpImageTexelPointer coordinate components to "
925                      "be 32-bits wide. They are "
926                   << coord_component_type->width() << " bits. "
927                   << image_texel_pointer->PrettyPrint(
928                          SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
929   }
930   const auto* query_size_type =
931       [type_mgr, coord_component_type,
932        query_num_components]() -> const analysis::Type* {
933     if (query_num_components == 1) return coord_component_type;
934     analysis::Vector proposed(coord_component_type, query_num_components);
935     return type_mgr->GetRegisteredType(&proposed);
936   }();
937 
938   const uint32_t image_id = TakeNextId();
939   auto* image =
940       InsertInst(image_texel_pointer, spv::Op::OpLoad, image_type_id, image_id,
941                  {{SPV_OPERAND_TYPE_ID, {image_ptr->result_id()}}});
942 
943   const uint32_t query_size_id = TakeNextId();
944   auto* query_size =
945       InsertInst(image_texel_pointer, spv::Op::OpImageQuerySize,
946                  type_mgr->GetTypeInstruction(query_size_type), query_size_id,
947                  {{SPV_OPERAND_TYPE_ID, {image->result_id()}}});
948 
949   auto* component_1 = constant_mgr->GetConstant(coord_component_type, {1});
950   const uint32_t component_1_id =
951       constant_mgr->GetDefiningInstruction(component_1)->result_id();
952   auto* component_0 = constant_mgr->GetConstant(coord_component_type, {0});
953   const uint32_t component_0_id =
954       constant_mgr->GetDefiningInstruction(component_0)->result_id();
955 
956   // If the image is a cube array, then the last component of the queried
957   // size is the layer count.  In the query, we have to accommodate folding
958   // in the face index ranging from 0 through 5. The inclusive upper bound
959   // on the third coordinate therefore is multiplied by 6.
960   auto* query_size_including_faces = query_size;
961   if (arrayed && (dim == spv::Dim::Cube)) {
962     // Multiply the last coordinate by 6.
963     auto* component_6 = constant_mgr->GetConstant(coord_component_type, {6});
964     const uint32_t component_6_id =
965         constant_mgr->GetDefiningInstruction(component_6)->result_id();
966     assert(query_num_components == 3);
967     auto* multiplicand = constant_mgr->GetConstant(
968         query_size_type, {component_1_id, component_1_id, component_6_id});
969     auto* multiplicand_inst =
970         constant_mgr->GetDefiningInstruction(multiplicand);
971     const auto query_size_including_faces_id = TakeNextId();
972     query_size_including_faces = InsertInst(
973         image_texel_pointer, spv::Op::OpIMul,
974         type_mgr->GetTypeInstruction(query_size_type),
975         query_size_including_faces_id,
976         {{SPV_OPERAND_TYPE_ID, {query_size_including_faces->result_id()}},
977          {SPV_OPERAND_TYPE_ID, {multiplicand_inst->result_id()}}});
978   }
979 
980   // Make a coordinate-type with all 1 components.
981   auto* coordinate_1 =
982       query_num_components == 1
983           ? component_1
984           : constant_mgr->GetConstant(
985                 query_size_type,
986                 std::vector<uint32_t>(query_num_components, component_1_id));
987   // Make a coordinate-type with all 1 components.
988   auto* coordinate_0 =
989       query_num_components == 0
990           ? component_0
991           : constant_mgr->GetConstant(
992                 query_size_type,
993                 std::vector<uint32_t>(query_num_components, component_0_id));
994 
995   const uint32_t query_max_including_faces_id = TakeNextId();
996   auto* query_max_including_faces = InsertInst(
997       image_texel_pointer, spv::Op::OpISub,
998       type_mgr->GetTypeInstruction(query_size_type),
999       query_max_including_faces_id,
1000       {{SPV_OPERAND_TYPE_ID, {query_size_including_faces->result_id()}},
1001        {SPV_OPERAND_TYPE_ID,
1002         {constant_mgr->GetDefiningInstruction(coordinate_1)->result_id()}}});
1003 
1004   // Clamp the coordinate
1005   auto* clamp_coord = MakeSClampInst(
1006       *type_mgr, coord, constant_mgr->GetDefiningInstruction(coordinate_0),
1007       query_max_including_faces, image_texel_pointer);
1008   image_texel_pointer->SetInOperand(1, {clamp_coord->result_id()});
1009 
1010   // Clamp the sample index
1011   if (multisampled) {
1012     // Get the sample count via OpImageQuerySamples
1013     const auto query_samples_id = TakeNextId();
1014     auto* query_samples = InsertInst(
1015         image_texel_pointer, spv::Op::OpImageQuerySamples,
1016         constant_mgr->GetDefiningInstruction(component_0)->type_id(),
1017         query_samples_id, {{SPV_OPERAND_TYPE_ID, {image->result_id()}}});
1018 
1019     const auto max_samples_id = TakeNextId();
1020     auto* max_samples = InsertInst(image_texel_pointer, spv::Op::OpImageQuerySamples,
1021                                    query_samples->type_id(), max_samples_id,
1022                                    {{SPV_OPERAND_TYPE_ID, {query_samples_id}},
1023                                     {SPV_OPERAND_TYPE_ID, {component_1_id}}});
1024 
1025     auto* clamp_samples = MakeSClampInst(
1026         *type_mgr, samples, constant_mgr->GetDefiningInstruction(coordinate_0),
1027         max_samples, image_texel_pointer);
1028     image_texel_pointer->SetInOperand(2, {clamp_samples->result_id()});
1029 
1030   } else {
1031     // Just replace it with 0.  Don't even check what was there before.
1032     image_texel_pointer->SetInOperand(2, {component_0_id});
1033   }
1034 
1035   def_use_mgr->AnalyzeInstUse(image_texel_pointer);
1036 
1037   return SPV_SUCCESS;
1038 #endif
1039 }
1040 
InsertInst(opt::Instruction * where_inst,spv::Op opcode,uint32_t type_id,uint32_t result_id,const Instruction::OperandList & operands)1041 opt::Instruction* GraphicsRobustAccessPass::InsertInst(
1042     opt::Instruction* where_inst, spv::Op opcode, uint32_t type_id,
1043     uint32_t result_id, const Instruction::OperandList& operands) {
1044   module_status_.modified = true;
1045   auto* result = where_inst->InsertBefore(
1046       MakeUnique<Instruction>(context(), opcode, type_id, result_id, operands));
1047   context()->get_def_use_mgr()->AnalyzeInstDefUse(result);
1048   auto* basic_block = context()->get_instr_block(where_inst);
1049   context()->set_instr_block(result, basic_block);
1050   return result;
1051 }
1052 
1053 }  // namespace opt
1054 }  // namespace spvtools
1055