SpvPostProcess.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //
  2. // Copyright (C) 2018 Google, Inc.
  3. //
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. //
  18. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  19. // contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. // Post-processing for SPIR-V IR, in internal form, not standard binary form.
  36. //
  37. #include <cassert>
  38. #include <cstdlib>
  39. #include <unordered_map>
  40. #include <unordered_set>
  41. #include <algorithm>
  42. #include "SpvBuilder.h"
  43. #include "spirv.hpp11"
  44. #include "spvUtil.h"
  45. namespace spv {
  46. #include "GLSL.std.450.h"
  47. #include "GLSL.ext.KHR.h"
  48. #include "GLSL.ext.EXT.h"
  49. #include "GLSL.ext.AMD.h"
  50. #include "GLSL.ext.NV.h"
  51. #include "GLSL.ext.ARM.h"
  52. #include "GLSL.ext.QCOM.h"
  53. }
  54. namespace spv {
  55. // Hook to visit each operand type and result type of an instruction.
  56. // Will be called multiple times for one instruction, once for each typed
  57. // operand and the result.
  58. void Builder::postProcessType(const Instruction& inst, Id typeId)
  59. {
  60. // Characterize the type being questioned
  61. Op basicTypeOp = getMostBasicTypeClass(typeId);
  62. int width = 0;
  63. if (basicTypeOp == Op::OpTypeFloat || basicTypeOp == Op::OpTypeInt)
  64. width = getScalarTypeWidth(typeId);
  65. // Do opcode-specific checks
  66. switch (inst.getOpCode()) {
  67. case Op::OpLoad:
  68. case Op::OpStore:
  69. if (basicTypeOp == Op::OpTypeStruct) {
  70. if (containsType(typeId, Op::OpTypeInt, 8))
  71. addCapability(Capability::Int8);
  72. if (containsType(typeId, Op::OpTypeInt, 16))
  73. addCapability(Capability::Int16);
  74. if (containsType(typeId, Op::OpTypeFloat, 16))
  75. addCapability(Capability::Float16);
  76. } else {
  77. StorageClass storageClass = getStorageClass(inst.getIdOperand(0));
  78. if (width == 8) {
  79. switch (storageClass) {
  80. case StorageClass::PhysicalStorageBufferEXT:
  81. case StorageClass::Uniform:
  82. case StorageClass::StorageBuffer:
  83. case StorageClass::PushConstant:
  84. break;
  85. default:
  86. addCapability(Capability::Int8);
  87. break;
  88. }
  89. } else if (width == 16) {
  90. switch (storageClass) {
  91. case StorageClass::PhysicalStorageBufferEXT:
  92. case StorageClass::Uniform:
  93. case StorageClass::StorageBuffer:
  94. case StorageClass::PushConstant:
  95. case StorageClass::Input:
  96. case StorageClass::Output:
  97. break;
  98. default:
  99. if (basicTypeOp == Op::OpTypeInt)
  100. addCapability(Capability::Int16);
  101. if (basicTypeOp == Op::OpTypeFloat)
  102. addCapability(Capability::Float16);
  103. break;
  104. }
  105. }
  106. }
  107. break;
  108. case Op::OpCopyObject:
  109. break;
  110. case Op::OpFConvert:
  111. case Op::OpSConvert:
  112. case Op::OpUConvert:
  113. // Look for any 8/16-bit storage capabilities. If there are none, assume that
  114. // the convert instruction requires the Float16/Int8/16 capability.
  115. if (containsType(typeId, Op::OpTypeFloat, 16) || containsType(typeId, Op::OpTypeInt, 16)) {
  116. bool foundStorage = false;
  117. for (auto it = capabilities.begin(); it != capabilities.end(); ++it) {
  118. spv::Capability cap = *it;
  119. if (cap == spv::Capability::StorageInputOutput16 ||
  120. cap == spv::Capability::StoragePushConstant16 ||
  121. cap == spv::Capability::StorageUniformBufferBlock16 ||
  122. cap == spv::Capability::StorageUniform16) {
  123. foundStorage = true;
  124. break;
  125. }
  126. }
  127. if (!foundStorage) {
  128. if (containsType(typeId, Op::OpTypeFloat, 16))
  129. addCapability(Capability::Float16);
  130. if (containsType(typeId, Op::OpTypeInt, 16))
  131. addCapability(Capability::Int16);
  132. }
  133. }
  134. if (containsType(typeId, Op::OpTypeInt, 8)) {
  135. bool foundStorage = false;
  136. for (auto it = capabilities.begin(); it != capabilities.end(); ++it) {
  137. spv::Capability cap = *it;
  138. if (cap == spv::Capability::StoragePushConstant8 ||
  139. cap == spv::Capability::UniformAndStorageBuffer8BitAccess ||
  140. cap == spv::Capability::StorageBuffer8BitAccess) {
  141. foundStorage = true;
  142. break;
  143. }
  144. }
  145. if (!foundStorage) {
  146. addCapability(Capability::Int8);
  147. }
  148. }
  149. break;
  150. case Op::OpExtInst:
  151. switch (inst.getImmediateOperand(1)) {
  152. case GLSLstd450Frexp:
  153. case GLSLstd450FrexpStruct:
  154. if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, Op::OpTypeInt, 16))
  155. addExtension(spv::E_SPV_AMD_gpu_shader_int16);
  156. break;
  157. case GLSLstd450InterpolateAtCentroid:
  158. case GLSLstd450InterpolateAtSample:
  159. case GLSLstd450InterpolateAtOffset:
  160. if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, Op::OpTypeFloat, 16))
  161. addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
  162. break;
  163. default:
  164. break;
  165. }
  166. break;
  167. case Op::OpAccessChain:
  168. case Op::OpPtrAccessChain:
  169. if (isPointerType(typeId))
  170. break;
  171. if (basicTypeOp == Op::OpTypeInt) {
  172. if (width == 16)
  173. addCapability(Capability::Int16);
  174. else if (width == 8)
  175. addCapability(Capability::Int8);
  176. }
  177. break;
  178. default:
  179. if (basicTypeOp == Op::OpTypeInt) {
  180. if (width == 16)
  181. addCapability(Capability::Int16);
  182. else if (width == 8)
  183. addCapability(Capability::Int8);
  184. else if (width == 64)
  185. addCapability(Capability::Int64);
  186. } else if (basicTypeOp == Op::OpTypeFloat) {
  187. if (width == 16)
  188. addCapability(Capability::Float16);
  189. else if (width == 64)
  190. addCapability(Capability::Float64);
  191. }
  192. break;
  193. }
  194. }
  195. // Called for each instruction that resides in a block.
  196. void Builder::postProcess(Instruction& inst)
  197. {
  198. // Add capabilities based simply on the opcode.
  199. switch (inst.getOpCode()) {
  200. case Op::OpExtInst:
  201. switch (inst.getImmediateOperand(1)) {
  202. case GLSLstd450InterpolateAtCentroid:
  203. case GLSLstd450InterpolateAtSample:
  204. case GLSLstd450InterpolateAtOffset:
  205. addCapability(Capability::InterpolationFunction);
  206. break;
  207. default:
  208. break;
  209. }
  210. break;
  211. case Op::OpDPdxFine:
  212. case Op::OpDPdyFine:
  213. case Op::OpFwidthFine:
  214. case Op::OpDPdxCoarse:
  215. case Op::OpDPdyCoarse:
  216. case Op::OpFwidthCoarse:
  217. addCapability(Capability::DerivativeControl);
  218. break;
  219. case Op::OpImageQueryLod:
  220. case Op::OpImageQuerySize:
  221. case Op::OpImageQuerySizeLod:
  222. case Op::OpImageQuerySamples:
  223. case Op::OpImageQueryLevels:
  224. addCapability(Capability::ImageQuery);
  225. break;
  226. case Op::OpGroupNonUniformPartitionNV:
  227. addExtension(E_SPV_NV_shader_subgroup_partitioned);
  228. addCapability(Capability::GroupNonUniformPartitionedNV);
  229. break;
  230. case Op::OpLoad:
  231. case Op::OpStore:
  232. {
  233. // For any load/store to a PhysicalStorageBufferEXT, walk the accesschain
  234. // index list to compute the misalignment. The pre-existing alignment value
  235. // (set via Builder::AccessChain::alignment) only accounts for the base of
  236. // the reference type and any scalar component selection in the accesschain,
  237. // and this function computes the rest from the SPIR-V Offset decorations.
  238. Instruction *accessChain = module.getInstruction(inst.getIdOperand(0));
  239. if (accessChain->getOpCode() == Op::OpAccessChain) {
  240. Instruction *base = module.getInstruction(accessChain->getIdOperand(0));
  241. // Get the type of the base of the access chain. It must be a pointer type.
  242. Id typeId = base->getTypeId();
  243. Instruction *type = module.getInstruction(typeId);
  244. assert(type->getOpCode() == Op::OpTypePointer);
  245. if (type->getImmediateOperand(0) != StorageClass::PhysicalStorageBufferEXT) {
  246. break;
  247. }
  248. // Get the pointee type.
  249. typeId = type->getIdOperand(1);
  250. type = module.getInstruction(typeId);
  251. // Walk the index list for the access chain. For each index, find any
  252. // misalignment that can apply when accessing the member/element via
  253. // Offset/ArrayStride/MatrixStride decorations, and bitwise OR them all
  254. // together.
  255. int alignment = 0;
  256. for (int i = 1; i < accessChain->getNumOperands(); ++i) {
  257. Instruction *idx = module.getInstruction(accessChain->getIdOperand(i));
  258. if (type->getOpCode() == Op::OpTypeStruct) {
  259. assert(idx->getOpCode() == Op::OpConstant);
  260. unsigned int c = idx->getImmediateOperand(0);
  261. const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
  262. if (decoration.get()->getOpCode() == Op::OpMemberDecorate &&
  263. decoration.get()->getIdOperand(0) == typeId &&
  264. decoration.get()->getImmediateOperand(1) == c &&
  265. (decoration.get()->getImmediateOperand(2) == Decoration::Offset ||
  266. decoration.get()->getImmediateOperand(2) == Decoration::MatrixStride)) {
  267. alignment |= decoration.get()->getImmediateOperand(3);
  268. }
  269. };
  270. std::for_each(decorations.begin(), decorations.end(), function);
  271. // get the next member type
  272. typeId = type->getIdOperand(c);
  273. type = module.getInstruction(typeId);
  274. } else if (type->getOpCode() == Op::OpTypeArray ||
  275. type->getOpCode() == Op::OpTypeRuntimeArray) {
  276. const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
  277. if (decoration.get()->getOpCode() == Op::OpDecorate &&
  278. decoration.get()->getIdOperand(0) == typeId &&
  279. decoration.get()->getImmediateOperand(1) == Decoration::ArrayStride) {
  280. alignment |= decoration.get()->getImmediateOperand(2);
  281. }
  282. };
  283. std::for_each(decorations.begin(), decorations.end(), function);
  284. // Get the element type
  285. typeId = type->getIdOperand(0);
  286. type = module.getInstruction(typeId);
  287. } else {
  288. // Once we get to any non-aggregate type, we're done.
  289. break;
  290. }
  291. }
  292. assert(inst.getNumOperands() >= 3);
  293. auto const memoryAccess = (MemoryAccessMask)inst.getImmediateOperand((inst.getOpCode() == Op::OpStore) ? 2 : 1);
  294. assert(anySet(memoryAccess, MemoryAccessMask::Aligned));
  295. static_cast<void>(memoryAccess);
  296. // Compute the index of the alignment operand.
  297. int alignmentIdx = 2;
  298. if (inst.getOpCode() == Op::OpStore)
  299. alignmentIdx++;
  300. // Merge new and old (mis)alignment
  301. alignment |= inst.getImmediateOperand(alignmentIdx);
  302. // Pick the LSB
  303. alignment = alignment & ~(alignment & (alignment-1));
  304. // update the Aligned operand
  305. inst.setImmediateOperand(alignmentIdx, alignment);
  306. }
  307. break;
  308. }
  309. default:
  310. break;
  311. }
  312. // Checks based on type
  313. if (inst.getTypeId() != NoType)
  314. postProcessType(inst, inst.getTypeId());
  315. for (int op = 0; op < inst.getNumOperands(); ++op) {
  316. if (inst.isIdOperand(op)) {
  317. // In blocks, these are always result ids, but we are relying on
  318. // getTypeId() to return NoType for things like OpLabel.
  319. if (getTypeId(inst.getIdOperand(op)) != NoType)
  320. postProcessType(inst, getTypeId(inst.getIdOperand(op)));
  321. }
  322. }
  323. }
  324. // comment in header
  325. void Builder::postProcessCFG()
  326. {
  327. // reachableBlocks is the set of blockss reached via control flow, or which are
  328. // unreachable continue targert or unreachable merge.
  329. std::unordered_set<const Block*> reachableBlocks;
  330. std::unordered_map<Block*, Block*> headerForUnreachableContinue;
  331. std::unordered_set<Block*> unreachableMerges;
  332. std::unordered_set<Id> unreachableDefinitions;
  333. // Collect IDs defined in unreachable blocks. For each function, label the
  334. // reachable blocks first. Then for each unreachable block, collect the
  335. // result IDs of the instructions in it.
  336. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  337. Function* f = *fi;
  338. Block* entry = f->getEntryBlock();
  339. inReadableOrder(entry,
  340. [&reachableBlocks, &unreachableMerges, &headerForUnreachableContinue]
  341. (Block* b, ReachReason why, Block* header) {
  342. reachableBlocks.insert(b);
  343. if (why == ReachDeadContinue) headerForUnreachableContinue[b] = header;
  344. if (why == ReachDeadMerge) unreachableMerges.insert(b);
  345. });
  346. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  347. Block* b = *bi;
  348. if (unreachableMerges.count(b) != 0 || headerForUnreachableContinue.count(b) != 0) {
  349. auto ii = b->getInstructions().cbegin();
  350. ++ii; // Keep potential decorations on the label.
  351. for (; ii != b->getInstructions().cend(); ++ii)
  352. unreachableDefinitions.insert(ii->get()->getResultId());
  353. } else if (reachableBlocks.count(b) == 0) {
  354. // The normal case for unreachable code. All definitions are considered dead.
  355. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ++ii)
  356. unreachableDefinitions.insert(ii->get()->getResultId());
  357. }
  358. }
  359. }
  360. // Modify unreachable merge blocks and unreachable continue targets.
  361. // Delete their contents.
  362. for (auto mergeIter = unreachableMerges.begin(); mergeIter != unreachableMerges.end(); ++mergeIter) {
  363. (*mergeIter)->rewriteAsCanonicalUnreachableMerge();
  364. }
  365. for (auto continueIter = headerForUnreachableContinue.begin();
  366. continueIter != headerForUnreachableContinue.end();
  367. ++continueIter) {
  368. Block* continue_target = continueIter->first;
  369. Block* header = continueIter->second;
  370. continue_target->rewriteAsCanonicalUnreachableContinue(header);
  371. }
  372. // Remove unneeded decorations, for unreachable instructions
  373. for (auto decorationIter = decorations.begin(); decorationIter != decorations.end();) {
  374. Id decorationId = (*decorationIter)->getIdOperand(0);
  375. if (unreachableDefinitions.count(decorationId) != 0) {
  376. decorationIter = decorations.erase(decorationIter);
  377. } else {
  378. ++decorationIter;
  379. }
  380. }
  381. }
  382. // comment in header
  383. void Builder::postProcessFeatures() {
  384. // Add per-instruction capabilities, extensions, etc.,
  385. // Look for any 8/16 bit type in physical storage buffer class, and set the
  386. // appropriate capability. This happens in createSpvVariable for other storage
  387. // classes, but there isn't always a variable for physical storage buffer.
  388. for (int t = 0; t < (int)groupedTypes[enumCast(Op::OpTypePointer)].size(); ++t) {
  389. Instruction* type = groupedTypes[enumCast(Op::OpTypePointer)][t];
  390. if (type->getImmediateOperand(0) == (unsigned)StorageClass::PhysicalStorageBufferEXT) {
  391. if (containsType(type->getIdOperand(1), Op::OpTypeInt, 8)) {
  392. addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
  393. addCapability(spv::Capability::StorageBuffer8BitAccess);
  394. }
  395. if (containsType(type->getIdOperand(1), Op::OpTypeInt, 16) ||
  396. containsType(type->getIdOperand(1), Op::OpTypeFloat, 16)) {
  397. addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
  398. addCapability(spv::Capability::StorageBuffer16BitAccess);
  399. }
  400. }
  401. }
  402. // process all block-contained instructions
  403. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  404. Function* f = *fi;
  405. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  406. Block* b = *bi;
  407. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++)
  408. postProcess(*ii->get());
  409. // For all local variables that contain pointers to PhysicalStorageBufferEXT, check whether
  410. // there is an existing restrict/aliased decoration. If we don't find one, add Aliased as the
  411. // default.
  412. for (auto vi = b->getLocalVariables().cbegin(); vi != b->getLocalVariables().cend(); vi++) {
  413. const Instruction& inst = *vi->get();
  414. Id resultId = inst.getResultId();
  415. if (containsPhysicalStorageBufferOrArray(getDerefTypeId(resultId))) {
  416. bool foundDecoration = false;
  417. const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
  418. if (decoration.get()->getIdOperand(0) == resultId &&
  419. decoration.get()->getOpCode() == Op::OpDecorate &&
  420. (decoration.get()->getImmediateOperand(1) == spv::Decoration::AliasedPointerEXT ||
  421. decoration.get()->getImmediateOperand(1) == spv::Decoration::RestrictPointerEXT)) {
  422. foundDecoration = true;
  423. }
  424. };
  425. std::for_each(decorations.begin(), decorations.end(), function);
  426. if (!foundDecoration) {
  427. addDecoration(resultId, spv::Decoration::AliasedPointerEXT);
  428. }
  429. }
  430. }
  431. }
  432. }
  433. // If any Vulkan memory model-specific functionality is used, update the
  434. // OpMemoryModel to match.
  435. if (capabilities.find(spv::Capability::VulkanMemoryModelKHR) != capabilities.end()) {
  436. memoryModel = spv::MemoryModel::VulkanKHR;
  437. addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5);
  438. }
  439. // Add Aliased decoration if there's more than one Workgroup Block variable.
  440. if (capabilities.find(spv::Capability::WorkgroupMemoryExplicitLayoutKHR) != capabilities.end()) {
  441. assert(entryPoints.size() == 1);
  442. auto &ep = entryPoints[0];
  443. std::vector<Id> workgroup_variables;
  444. for (int i = 0; i < (int)ep->getNumOperands(); i++) {
  445. if (!ep->isIdOperand(i))
  446. continue;
  447. const Id id = ep->getIdOperand(i);
  448. const Instruction *instr = module.getInstruction(id);
  449. if (instr->getOpCode() != spv::Op::OpVariable)
  450. continue;
  451. if (instr->getImmediateOperand(0) == spv::StorageClass::Workgroup)
  452. workgroup_variables.push_back(id);
  453. }
  454. if (workgroup_variables.size() > 1) {
  455. for (size_t i = 0; i < workgroup_variables.size(); i++)
  456. addDecoration(workgroup_variables[i], spv::Decoration::Aliased);
  457. }
  458. }
  459. }
  460. // SPIR-V requires that any instruction consuming the result of an OpSampledImage
  461. // be in the same block as the OpSampledImage instruction. This pass goes finds
  462. // uses of OpSampledImage where that is not the case and duplicates the
  463. // OpSampledImage to be immediately before the instruction that consumes it.
  464. // The old OpSampledImage is left in place, potentially with no users.
  465. void Builder::postProcessSamplers()
  466. {
  467. // first, find all OpSampledImage instructions and store them in a map.
  468. std::map<Id, Instruction*> sampledImageInstrs;
  469. for (auto f: module.getFunctions()) {
  470. for (auto b: f->getBlocks()) {
  471. for (auto &i: b->getInstructions()) {
  472. if (i->getOpCode() == spv::Op::OpSampledImage) {
  473. sampledImageInstrs[i->getResultId()] = i.get();
  474. }
  475. }
  476. }
  477. }
  478. // next find all uses of the given ids and rewrite them if needed.
  479. for (auto f: module.getFunctions()) {
  480. for (auto b: f->getBlocks()) {
  481. auto &instrs = b->getInstructions();
  482. for (size_t idx = 0; idx < instrs.size(); idx++) {
  483. Instruction *i = instrs[idx].get();
  484. for (int opnum = 0; opnum < i->getNumOperands(); opnum++) {
  485. // Is this operand of the current instruction the result of an OpSampledImage?
  486. if (i->isIdOperand(opnum) &&
  487. sampledImageInstrs.count(i->getIdOperand(opnum)))
  488. {
  489. Instruction *opSampImg = sampledImageInstrs[i->getIdOperand(opnum)];
  490. if (i->getBlock() != opSampImg->getBlock()) {
  491. Instruction *newInstr = new Instruction(getUniqueId(),
  492. opSampImg->getTypeId(),
  493. spv::Op::OpSampledImage);
  494. newInstr->addIdOperand(opSampImg->getIdOperand(0));
  495. newInstr->addIdOperand(opSampImg->getIdOperand(1));
  496. newInstr->setBlock(b);
  497. // rewrite the user of the OpSampledImage to use the new instruction.
  498. i->setIdOperand(opnum, newInstr->getResultId());
  499. // insert the new OpSampledImage right before the current instruction.
  500. instrs.insert(instrs.begin() + idx,
  501. std::unique_ptr<Instruction>(newInstr));
  502. idx++;
  503. }
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. // comment in header
  511. void Builder::postProcess(bool compileOnly)
  512. {
  513. // postProcessCFG needs an entrypoint to determine what is reachable, but if we are not creating an "executable" shader, we don't have an entrypoint
  514. if (!compileOnly)
  515. postProcessCFG();
  516. postProcessFeatures();
  517. postProcessSamplers();
  518. }
  519. } // end spv namespace