graphics_robust_access_pass.cpp 45 KB

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