SpvPostProcess.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // Copyright (C) 2016-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_set>
  40. #include <algorithm>
  41. #include "SpvBuilder.h"
  42. #include "spirv.hpp"
  43. #include "GlslangToSpv.h"
  44. #include "SpvBuilder.h"
  45. namespace spv {
  46. #include "GLSL.std.450.h"
  47. #include "GLSL.ext.KHR.h"
  48. #include "GLSL.ext.EXT.h"
  49. #ifdef AMD_EXTENSIONS
  50. #include "GLSL.ext.AMD.h"
  51. #endif
  52. #ifdef NV_EXTENSIONS
  53. #include "GLSL.ext.NV.h"
  54. #endif
  55. }
  56. namespace spv {
  57. // Hook to visit each operand type and result type of an instruction.
  58. // Will be called multiple times for one instruction, once for each typed
  59. // operand and the result.
  60. void Builder::postProcessType(const Instruction& inst, Id typeId)
  61. {
  62. // Characterize the type being questioned
  63. Id basicTypeOp = getMostBasicTypeClass(typeId);
  64. int width = 0;
  65. if (basicTypeOp == OpTypeFloat || basicTypeOp == OpTypeInt)
  66. width = getScalarTypeWidth(typeId);
  67. // Do opcode-specific checks
  68. switch (inst.getOpCode()) {
  69. case OpLoad:
  70. case OpStore:
  71. if (basicTypeOp == OpTypeStruct) {
  72. if (containsType(typeId, OpTypeInt, 8))
  73. addCapability(CapabilityInt8);
  74. if (containsType(typeId, OpTypeInt, 16))
  75. addCapability(CapabilityInt16);
  76. if (containsType(typeId, OpTypeFloat, 16))
  77. addCapability(CapabilityFloat16);
  78. } else {
  79. StorageClass storageClass = getStorageClass(inst.getIdOperand(0));
  80. if (width == 8) {
  81. switch (storageClass) {
  82. case StorageClassUniform:
  83. case StorageClassStorageBuffer:
  84. case StorageClassPushConstant:
  85. break;
  86. default:
  87. addCapability(CapabilityInt8);
  88. break;
  89. }
  90. } else if (width == 16) {
  91. switch (storageClass) {
  92. case StorageClassUniform:
  93. case StorageClassStorageBuffer:
  94. case StorageClassPushConstant:
  95. case StorageClassInput:
  96. case StorageClassOutput:
  97. break;
  98. default:
  99. if (basicTypeOp == OpTypeInt)
  100. addCapability(CapabilityInt16);
  101. if (basicTypeOp == OpTypeFloat)
  102. addCapability(CapabilityFloat16);
  103. break;
  104. }
  105. }
  106. }
  107. break;
  108. case OpAccessChain:
  109. case OpPtrAccessChain:
  110. case OpCopyObject:
  111. case OpFConvert:
  112. case OpSConvert:
  113. case OpUConvert:
  114. break;
  115. default:
  116. if (basicTypeOp == OpTypeFloat && width == 16)
  117. addCapability(CapabilityFloat16);
  118. if (basicTypeOp == OpTypeInt && width == 16)
  119. addCapability(CapabilityInt16);
  120. if (basicTypeOp == OpTypeInt && width == 8)
  121. addCapability(CapabilityInt8);
  122. break;
  123. }
  124. }
  125. // Called for each instruction that resides in a block.
  126. void Builder::postProcess(const Instruction& inst)
  127. {
  128. // Add capabilities based simply on the opcode.
  129. switch (inst.getOpCode()) {
  130. case OpExtInst:
  131. switch (inst.getImmediateOperand(1)) {
  132. case GLSLstd450InterpolateAtCentroid:
  133. case GLSLstd450InterpolateAtSample:
  134. case GLSLstd450InterpolateAtOffset:
  135. addCapability(CapabilityInterpolationFunction);
  136. break;
  137. default:
  138. break;
  139. }
  140. break;
  141. case OpDPdxFine:
  142. case OpDPdyFine:
  143. case OpFwidthFine:
  144. case OpDPdxCoarse:
  145. case OpDPdyCoarse:
  146. case OpFwidthCoarse:
  147. addCapability(CapabilityDerivativeControl);
  148. break;
  149. case OpImageQueryLod:
  150. case OpImageQuerySize:
  151. case OpImageQuerySizeLod:
  152. case OpImageQuerySamples:
  153. case OpImageQueryLevels:
  154. addCapability(CapabilityImageQuery);
  155. break;
  156. #ifdef NV_EXTENSIONS
  157. case OpGroupNonUniformPartitionNV:
  158. addExtension(E_SPV_NV_shader_subgroup_partitioned);
  159. addCapability(CapabilityGroupNonUniformPartitionedNV);
  160. break;
  161. #endif
  162. default:
  163. break;
  164. }
  165. // Checks based on type
  166. if (inst.getTypeId() != NoType)
  167. postProcessType(inst, inst.getTypeId());
  168. for (int op = 0; op < inst.getNumOperands(); ++op) {
  169. if (inst.isIdOperand(op)) {
  170. // In blocks, these are always result ids, but we are relying on
  171. // getTypeId() to return NoType for things like OpLabel.
  172. if (getTypeId(inst.getIdOperand(op)) != NoType)
  173. postProcessType(inst, getTypeId(inst.getIdOperand(op)));
  174. }
  175. }
  176. }
  177. // Called for each instruction in a reachable block.
  178. void Builder::postProcessReachable(const Instruction& inst)
  179. {
  180. // did have code here, but questionable to do so without deleting the instructions
  181. }
  182. // comment in header
  183. void Builder::postProcess()
  184. {
  185. std::unordered_set<const Block*> reachableBlocks;
  186. std::unordered_set<Id> unreachableDefinitions;
  187. // Collect IDs defined in unreachable blocks. For each function, label the
  188. // reachable blocks first. Then for each unreachable block, collect the
  189. // result IDs of the instructions in it.
  190. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  191. Function* f = *fi;
  192. Block* entry = f->getEntryBlock();
  193. inReadableOrder(entry, [&reachableBlocks](const Block* b) { reachableBlocks.insert(b); });
  194. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  195. Block* b = *bi;
  196. if (reachableBlocks.count(b) == 0) {
  197. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++)
  198. unreachableDefinitions.insert(ii->get()->getResultId());
  199. }
  200. }
  201. }
  202. // Remove unneeded decorations, for unreachable instructions
  203. decorations.erase(std::remove_if(decorations.begin(), decorations.end(),
  204. [&unreachableDefinitions](std::unique_ptr<Instruction>& I) -> bool {
  205. Id decoration_id = I.get()->getIdOperand(0);
  206. return unreachableDefinitions.count(decoration_id) != 0;
  207. }),
  208. decorations.end());
  209. // Add per-instruction capabilities, extensions, etc.,
  210. // process all reachable instructions...
  211. for (auto bi = reachableBlocks.cbegin(); bi != reachableBlocks.cend(); ++bi) {
  212. const Block* block = *bi;
  213. const auto function = [this](const std::unique_ptr<Instruction>& inst) { postProcessReachable(*inst.get()); };
  214. std::for_each(block->getInstructions().begin(), block->getInstructions().end(), function);
  215. }
  216. // process all block-contained instructions
  217. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  218. Function* f = *fi;
  219. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  220. Block* b = *bi;
  221. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++)
  222. postProcess(*ii->get());
  223. }
  224. }
  225. }
  226. }; // end spv namespace