SpvPostProcess.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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.hpp"
  44. namespace spv {
  45. #include "GLSL.std.450.h"
  46. #include "GLSL.ext.KHR.h"
  47. #include "GLSL.ext.EXT.h"
  48. #include "GLSL.ext.AMD.h"
  49. #include "GLSL.ext.NV.h"
  50. #include "GLSL.ext.ARM.h"
  51. }
  52. namespace spv {
  53. #ifndef GLSLANG_WEB
  54. // Hook to visit each operand type and result type of an instruction.
  55. // Will be called multiple times for one instruction, once for each typed
  56. // operand and the result.
  57. void Builder::postProcessType(const Instruction& inst, Id typeId)
  58. {
  59. // Characterize the type being questioned
  60. Id basicTypeOp = getMostBasicTypeClass(typeId);
  61. int width = 0;
  62. if (basicTypeOp == OpTypeFloat || basicTypeOp == OpTypeInt)
  63. width = getScalarTypeWidth(typeId);
  64. // Do opcode-specific checks
  65. switch (inst.getOpCode()) {
  66. case OpLoad:
  67. case OpStore:
  68. if (basicTypeOp == OpTypeStruct) {
  69. if (containsType(typeId, OpTypeInt, 8))
  70. addCapability(CapabilityInt8);
  71. if (containsType(typeId, OpTypeInt, 16))
  72. addCapability(CapabilityInt16);
  73. if (containsType(typeId, OpTypeFloat, 16))
  74. addCapability(CapabilityFloat16);
  75. } else {
  76. StorageClass storageClass = getStorageClass(inst.getIdOperand(0));
  77. if (width == 8) {
  78. switch (storageClass) {
  79. case StorageClassPhysicalStorageBufferEXT:
  80. case StorageClassUniform:
  81. case StorageClassStorageBuffer:
  82. case StorageClassPushConstant:
  83. break;
  84. default:
  85. addCapability(CapabilityInt8);
  86. break;
  87. }
  88. } else if (width == 16) {
  89. switch (storageClass) {
  90. case StorageClassPhysicalStorageBufferEXT:
  91. case StorageClassUniform:
  92. case StorageClassStorageBuffer:
  93. case StorageClassPushConstant:
  94. case StorageClassInput:
  95. case StorageClassOutput:
  96. break;
  97. default:
  98. if (basicTypeOp == OpTypeInt)
  99. addCapability(CapabilityInt16);
  100. if (basicTypeOp == OpTypeFloat)
  101. addCapability(CapabilityFloat16);
  102. break;
  103. }
  104. }
  105. }
  106. break;
  107. case OpCopyObject:
  108. break;
  109. case OpFConvert:
  110. case OpSConvert:
  111. case OpUConvert:
  112. // Look for any 8/16-bit storage capabilities. If there are none, assume that
  113. // the convert instruction requires the Float16/Int8/16 capability.
  114. if (containsType(typeId, OpTypeFloat, 16) || containsType(typeId, OpTypeInt, 16)) {
  115. bool foundStorage = false;
  116. for (auto it = capabilities.begin(); it != capabilities.end(); ++it) {
  117. spv::Capability cap = *it;
  118. if (cap == spv::CapabilityStorageInputOutput16 ||
  119. cap == spv::CapabilityStoragePushConstant16 ||
  120. cap == spv::CapabilityStorageUniformBufferBlock16 ||
  121. cap == spv::CapabilityStorageUniform16) {
  122. foundStorage = true;
  123. break;
  124. }
  125. }
  126. if (!foundStorage) {
  127. if (containsType(typeId, OpTypeFloat, 16))
  128. addCapability(CapabilityFloat16);
  129. if (containsType(typeId, OpTypeInt, 16))
  130. addCapability(CapabilityInt16);
  131. }
  132. }
  133. if (containsType(typeId, OpTypeInt, 8)) {
  134. bool foundStorage = false;
  135. for (auto it = capabilities.begin(); it != capabilities.end(); ++it) {
  136. spv::Capability cap = *it;
  137. if (cap == spv::CapabilityStoragePushConstant8 ||
  138. cap == spv::CapabilityUniformAndStorageBuffer8BitAccess ||
  139. cap == spv::CapabilityStorageBuffer8BitAccess) {
  140. foundStorage = true;
  141. break;
  142. }
  143. }
  144. if (!foundStorage) {
  145. addCapability(CapabilityInt8);
  146. }
  147. }
  148. break;
  149. case OpExtInst:
  150. switch (inst.getImmediateOperand(1)) {
  151. case GLSLstd450Frexp:
  152. case GLSLstd450FrexpStruct:
  153. if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, OpTypeInt, 16))
  154. addExtension(spv::E_SPV_AMD_gpu_shader_int16);
  155. break;
  156. case GLSLstd450InterpolateAtCentroid:
  157. case GLSLstd450InterpolateAtSample:
  158. case GLSLstd450InterpolateAtOffset:
  159. if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, OpTypeFloat, 16))
  160. addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
  161. break;
  162. default:
  163. break;
  164. }
  165. break;
  166. case OpAccessChain:
  167. case OpPtrAccessChain:
  168. if (isPointerType(typeId))
  169. break;
  170. if (basicTypeOp == OpTypeInt) {
  171. if (width == 16)
  172. addCapability(CapabilityInt16);
  173. else if (width == 8)
  174. addCapability(CapabilityInt8);
  175. }
  176. default:
  177. if (basicTypeOp == OpTypeInt) {
  178. if (width == 16)
  179. addCapability(CapabilityInt16);
  180. else if (width == 8)
  181. addCapability(CapabilityInt8);
  182. else if (width == 64)
  183. addCapability(CapabilityInt64);
  184. } else if (basicTypeOp == OpTypeFloat) {
  185. if (width == 16)
  186. addCapability(CapabilityFloat16);
  187. else if (width == 64)
  188. addCapability(CapabilityFloat64);
  189. }
  190. break;
  191. }
  192. }
  193. // Called for each instruction that resides in a block.
  194. void Builder::postProcess(Instruction& inst)
  195. {
  196. // Add capabilities based simply on the opcode.
  197. switch (inst.getOpCode()) {
  198. case OpExtInst:
  199. switch (inst.getImmediateOperand(1)) {
  200. case GLSLstd450InterpolateAtCentroid:
  201. case GLSLstd450InterpolateAtSample:
  202. case GLSLstd450InterpolateAtOffset:
  203. addCapability(CapabilityInterpolationFunction);
  204. break;
  205. default:
  206. break;
  207. }
  208. break;
  209. case OpDPdxFine:
  210. case OpDPdyFine:
  211. case OpFwidthFine:
  212. case OpDPdxCoarse:
  213. case OpDPdyCoarse:
  214. case OpFwidthCoarse:
  215. addCapability(CapabilityDerivativeControl);
  216. break;
  217. case OpImageQueryLod:
  218. case OpImageQuerySize:
  219. case OpImageQuerySizeLod:
  220. case OpImageQuerySamples:
  221. case OpImageQueryLevels:
  222. addCapability(CapabilityImageQuery);
  223. break;
  224. case OpGroupNonUniformPartitionNV:
  225. addExtension(E_SPV_NV_shader_subgroup_partitioned);
  226. addCapability(CapabilityGroupNonUniformPartitionedNV);
  227. break;
  228. case OpLoad:
  229. case OpStore:
  230. {
  231. // For any load/store to a PhysicalStorageBufferEXT, walk the accesschain
  232. // index list to compute the misalignment. The pre-existing alignment value
  233. // (set via Builder::AccessChain::alignment) only accounts for the base of
  234. // the reference type and any scalar component selection in the accesschain,
  235. // and this function computes the rest from the SPIR-V Offset decorations.
  236. Instruction *accessChain = module.getInstruction(inst.getIdOperand(0));
  237. if (accessChain->getOpCode() == OpAccessChain) {
  238. Instruction *base = module.getInstruction(accessChain->getIdOperand(0));
  239. // Get the type of the base of the access chain. It must be a pointer type.
  240. Id typeId = base->getTypeId();
  241. Instruction *type = module.getInstruction(typeId);
  242. assert(type->getOpCode() == OpTypePointer);
  243. if (type->getImmediateOperand(0) != StorageClassPhysicalStorageBufferEXT) {
  244. break;
  245. }
  246. // Get the pointee type.
  247. typeId = type->getIdOperand(1);
  248. type = module.getInstruction(typeId);
  249. // Walk the index list for the access chain. For each index, find any
  250. // misalignment that can apply when accessing the member/element via
  251. // Offset/ArrayStride/MatrixStride decorations, and bitwise OR them all
  252. // together.
  253. int alignment = 0;
  254. for (int i = 1; i < accessChain->getNumOperands(); ++i) {
  255. Instruction *idx = module.getInstruction(accessChain->getIdOperand(i));
  256. if (type->getOpCode() == OpTypeStruct) {
  257. assert(idx->getOpCode() == OpConstant);
  258. unsigned int c = idx->getImmediateOperand(0);
  259. const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
  260. if (decoration.get()->getOpCode() == OpMemberDecorate &&
  261. decoration.get()->getIdOperand(0) == typeId &&
  262. decoration.get()->getImmediateOperand(1) == c &&
  263. (decoration.get()->getImmediateOperand(2) == DecorationOffset ||
  264. decoration.get()->getImmediateOperand(2) == DecorationMatrixStride)) {
  265. alignment |= decoration.get()->getImmediateOperand(3);
  266. }
  267. };
  268. std::for_each(decorations.begin(), decorations.end(), function);
  269. // get the next member type
  270. typeId = type->getIdOperand(c);
  271. type = module.getInstruction(typeId);
  272. } else if (type->getOpCode() == OpTypeArray ||
  273. type->getOpCode() == OpTypeRuntimeArray) {
  274. const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
  275. if (decoration.get()->getOpCode() == OpDecorate &&
  276. decoration.get()->getIdOperand(0) == typeId &&
  277. decoration.get()->getImmediateOperand(1) == DecorationArrayStride) {
  278. alignment |= decoration.get()->getImmediateOperand(2);
  279. }
  280. };
  281. std::for_each(decorations.begin(), decorations.end(), function);
  282. // Get the element type
  283. typeId = type->getIdOperand(0);
  284. type = module.getInstruction(typeId);
  285. } else {
  286. // Once we get to any non-aggregate type, we're done.
  287. break;
  288. }
  289. }
  290. assert(inst.getNumOperands() >= 3);
  291. unsigned int memoryAccess = inst.getImmediateOperand((inst.getOpCode() == OpStore) ? 2 : 1);
  292. assert(memoryAccess & MemoryAccessAlignedMask);
  293. static_cast<void>(memoryAccess);
  294. // Compute the index of the alignment operand.
  295. int alignmentIdx = 2;
  296. if (inst.getOpCode() == OpStore)
  297. alignmentIdx++;
  298. // Merge new and old (mis)alignment
  299. alignment |= inst.getImmediateOperand(alignmentIdx);
  300. // Pick the LSB
  301. alignment = alignment & ~(alignment & (alignment-1));
  302. // update the Aligned operand
  303. inst.setImmediateOperand(alignmentIdx, alignment);
  304. }
  305. break;
  306. }
  307. default:
  308. break;
  309. }
  310. // Checks based on type
  311. if (inst.getTypeId() != NoType)
  312. postProcessType(inst, inst.getTypeId());
  313. for (int op = 0; op < inst.getNumOperands(); ++op) {
  314. if (inst.isIdOperand(op)) {
  315. // In blocks, these are always result ids, but we are relying on
  316. // getTypeId() to return NoType for things like OpLabel.
  317. if (getTypeId(inst.getIdOperand(op)) != NoType)
  318. postProcessType(inst, getTypeId(inst.getIdOperand(op)));
  319. }
  320. }
  321. }
  322. #endif
  323. // comment in header
  324. void Builder::postProcessCFG()
  325. {
  326. // reachableBlocks is the set of blockss reached via control flow, or which are
  327. // unreachable continue targert or unreachable merge.
  328. std::unordered_set<const Block*> reachableBlocks;
  329. std::unordered_map<Block*, Block*> headerForUnreachableContinue;
  330. std::unordered_set<Block*> unreachableMerges;
  331. std::unordered_set<Id> unreachableDefinitions;
  332. // Collect IDs defined in unreachable blocks. For each function, label the
  333. // reachable blocks first. Then for each unreachable block, collect the
  334. // result IDs of the instructions in it.
  335. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  336. Function* f = *fi;
  337. Block* entry = f->getEntryBlock();
  338. inReadableOrder(entry,
  339. [&reachableBlocks, &unreachableMerges, &headerForUnreachableContinue]
  340. (Block* b, ReachReason why, Block* header) {
  341. reachableBlocks.insert(b);
  342. if (why == ReachDeadContinue) headerForUnreachableContinue[b] = header;
  343. if (why == ReachDeadMerge) unreachableMerges.insert(b);
  344. });
  345. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  346. Block* b = *bi;
  347. if (unreachableMerges.count(b) != 0 || headerForUnreachableContinue.count(b) != 0) {
  348. auto ii = b->getInstructions().cbegin();
  349. ++ii; // Keep potential decorations on the label.
  350. for (; ii != b->getInstructions().cend(); ++ii)
  351. unreachableDefinitions.insert(ii->get()->getResultId());
  352. } else if (reachableBlocks.count(b) == 0) {
  353. // The normal case for unreachable code. All definitions are considered dead.
  354. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ++ii)
  355. unreachableDefinitions.insert(ii->get()->getResultId());
  356. }
  357. }
  358. }
  359. // Modify unreachable merge blocks and unreachable continue targets.
  360. // Delete their contents.
  361. for (auto mergeIter = unreachableMerges.begin(); mergeIter != unreachableMerges.end(); ++mergeIter) {
  362. (*mergeIter)->rewriteAsCanonicalUnreachableMerge();
  363. }
  364. for (auto continueIter = headerForUnreachableContinue.begin();
  365. continueIter != headerForUnreachableContinue.end();
  366. ++continueIter) {
  367. Block* continue_target = continueIter->first;
  368. Block* header = continueIter->second;
  369. continue_target->rewriteAsCanonicalUnreachableContinue(header);
  370. }
  371. // Remove unneeded decorations, for unreachable instructions
  372. decorations.erase(std::remove_if(decorations.begin(), decorations.end(),
  373. [&unreachableDefinitions](std::unique_ptr<Instruction>& I) -> bool {
  374. Id decoration_id = I.get()->getIdOperand(0);
  375. return unreachableDefinitions.count(decoration_id) != 0;
  376. }),
  377. decorations.end());
  378. }
  379. #ifndef GLSLANG_WEB
  380. // comment in header
  381. void Builder::postProcessFeatures() {
  382. // Add per-instruction capabilities, extensions, etc.,
  383. // Look for any 8/16 bit type in physical storage buffer class, and set the
  384. // appropriate capability. This happens in createSpvVariable for other storage
  385. // classes, but there isn't always a variable for physical storage buffer.
  386. for (int t = 0; t < (int)groupedTypes[OpTypePointer].size(); ++t) {
  387. Instruction* type = groupedTypes[OpTypePointer][t];
  388. if (type->getImmediateOperand(0) == (unsigned)StorageClassPhysicalStorageBufferEXT) {
  389. if (containsType(type->getIdOperand(1), OpTypeInt, 8)) {
  390. addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
  391. addCapability(spv::CapabilityStorageBuffer8BitAccess);
  392. }
  393. if (containsType(type->getIdOperand(1), OpTypeInt, 16) ||
  394. containsType(type->getIdOperand(1), OpTypeFloat, 16)) {
  395. addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
  396. addCapability(spv::CapabilityStorageBuffer16BitAccess);
  397. }
  398. }
  399. }
  400. // process all block-contained instructions
  401. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  402. Function* f = *fi;
  403. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  404. Block* b = *bi;
  405. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++)
  406. postProcess(*ii->get());
  407. // For all local variables that contain pointers to PhysicalStorageBufferEXT, check whether
  408. // there is an existing restrict/aliased decoration. If we don't find one, add Aliased as the
  409. // default.
  410. for (auto vi = b->getLocalVariables().cbegin(); vi != b->getLocalVariables().cend(); vi++) {
  411. const Instruction& inst = *vi->get();
  412. Id resultId = inst.getResultId();
  413. if (containsPhysicalStorageBufferOrArray(getDerefTypeId(resultId))) {
  414. bool foundDecoration = false;
  415. const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
  416. if (decoration.get()->getIdOperand(0) == resultId &&
  417. decoration.get()->getOpCode() == OpDecorate &&
  418. (decoration.get()->getImmediateOperand(1) == spv::DecorationAliasedPointerEXT ||
  419. decoration.get()->getImmediateOperand(1) == spv::DecorationRestrictPointerEXT)) {
  420. foundDecoration = true;
  421. }
  422. };
  423. std::for_each(decorations.begin(), decorations.end(), function);
  424. if (!foundDecoration) {
  425. addDecoration(resultId, spv::DecorationAliasedPointerEXT);
  426. }
  427. }
  428. }
  429. }
  430. }
  431. // If any Vulkan memory model-specific functionality is used, update the
  432. // OpMemoryModel to match.
  433. if (capabilities.find(spv::CapabilityVulkanMemoryModelKHR) != capabilities.end()) {
  434. memoryModel = spv::MemoryModelVulkanKHR;
  435. addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5);
  436. }
  437. // Add Aliased decoration if there's more than one Workgroup Block variable.
  438. if (capabilities.find(spv::CapabilityWorkgroupMemoryExplicitLayoutKHR) != capabilities.end()) {
  439. assert(entryPoints.size() == 1);
  440. auto &ep = entryPoints[0];
  441. std::vector<Id> workgroup_variables;
  442. for (int i = 0; i < (int)ep->getNumOperands(); i++) {
  443. if (!ep->isIdOperand(i))
  444. continue;
  445. const Id id = ep->getIdOperand(i);
  446. const Instruction *instr = module.getInstruction(id);
  447. if (instr->getOpCode() != spv::OpVariable)
  448. continue;
  449. if (instr->getImmediateOperand(0) == spv::StorageClassWorkgroup)
  450. workgroup_variables.push_back(id);
  451. }
  452. if (workgroup_variables.size() > 1) {
  453. for (size_t i = 0; i < workgroup_variables.size(); i++)
  454. addDecoration(workgroup_variables[i], spv::DecorationAliased);
  455. }
  456. }
  457. }
  458. #endif
  459. // comment in header
  460. void Builder::postProcess() {
  461. postProcessCFG();
  462. #ifndef GLSLANG_WEB
  463. postProcessFeatures();
  464. #endif
  465. }
  466. }; // end spv namespace