SpvPostProcess.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. case OpExtInst:
  116. switch (inst.getImmediateOperand(1)) {
  117. #if AMD_EXTENSIONS
  118. case GLSLstd450Frexp:
  119. case GLSLstd450FrexpStruct:
  120. if (getSpvVersion() < glslang::EShTargetSpv_1_3 && containsType(typeId, OpTypeInt, 16))
  121. addExtension(spv::E_SPV_AMD_gpu_shader_int16);
  122. break;
  123. case GLSLstd450InterpolateAtCentroid:
  124. case GLSLstd450InterpolateAtSample:
  125. case GLSLstd450InterpolateAtOffset:
  126. if (getSpvVersion() < glslang::EShTargetSpv_1_3 && containsType(typeId, OpTypeFloat, 16))
  127. addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
  128. break;
  129. #endif
  130. default:
  131. break;
  132. }
  133. break;
  134. default:
  135. if (basicTypeOp == OpTypeFloat && width == 16)
  136. addCapability(CapabilityFloat16);
  137. if (basicTypeOp == OpTypeInt && width == 16)
  138. addCapability(CapabilityInt16);
  139. if (basicTypeOp == OpTypeInt && width == 8)
  140. addCapability(CapabilityInt8);
  141. break;
  142. }
  143. }
  144. // Called for each instruction that resides in a block.
  145. void Builder::postProcess(const Instruction& inst)
  146. {
  147. // Add capabilities based simply on the opcode.
  148. switch (inst.getOpCode()) {
  149. case OpExtInst:
  150. switch (inst.getImmediateOperand(1)) {
  151. case GLSLstd450InterpolateAtCentroid:
  152. case GLSLstd450InterpolateAtSample:
  153. case GLSLstd450InterpolateAtOffset:
  154. addCapability(CapabilityInterpolationFunction);
  155. break;
  156. default:
  157. break;
  158. }
  159. break;
  160. case OpDPdxFine:
  161. case OpDPdyFine:
  162. case OpFwidthFine:
  163. case OpDPdxCoarse:
  164. case OpDPdyCoarse:
  165. case OpFwidthCoarse:
  166. addCapability(CapabilityDerivativeControl);
  167. break;
  168. case OpImageQueryLod:
  169. case OpImageQuerySize:
  170. case OpImageQuerySizeLod:
  171. case OpImageQuerySamples:
  172. case OpImageQueryLevels:
  173. addCapability(CapabilityImageQuery);
  174. break;
  175. #ifdef NV_EXTENSIONS
  176. case OpGroupNonUniformPartitionNV:
  177. addExtension(E_SPV_NV_shader_subgroup_partitioned);
  178. addCapability(CapabilityGroupNonUniformPartitionedNV);
  179. break;
  180. #endif
  181. default:
  182. break;
  183. }
  184. // Checks based on type
  185. if (inst.getTypeId() != NoType)
  186. postProcessType(inst, inst.getTypeId());
  187. for (int op = 0; op < inst.getNumOperands(); ++op) {
  188. if (inst.isIdOperand(op)) {
  189. // In blocks, these are always result ids, but we are relying on
  190. // getTypeId() to return NoType for things like OpLabel.
  191. if (getTypeId(inst.getIdOperand(op)) != NoType)
  192. postProcessType(inst, getTypeId(inst.getIdOperand(op)));
  193. }
  194. }
  195. }
  196. // Called for each instruction in a reachable block.
  197. void Builder::postProcessReachable(const Instruction& inst)
  198. {
  199. // did have code here, but questionable to do so without deleting the instructions
  200. }
  201. // comment in header
  202. void Builder::postProcess()
  203. {
  204. std::unordered_set<const Block*> reachableBlocks;
  205. std::unordered_set<Id> unreachableDefinitions;
  206. // Collect IDs defined in unreachable blocks. For each function, label the
  207. // reachable blocks first. Then for each unreachable block, collect the
  208. // result IDs of the instructions in it.
  209. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  210. Function* f = *fi;
  211. Block* entry = f->getEntryBlock();
  212. inReadableOrder(entry, [&reachableBlocks](const Block* b) { reachableBlocks.insert(b); });
  213. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  214. Block* b = *bi;
  215. if (reachableBlocks.count(b) == 0) {
  216. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++)
  217. unreachableDefinitions.insert(ii->get()->getResultId());
  218. }
  219. }
  220. }
  221. // Remove unneeded decorations, for unreachable instructions
  222. decorations.erase(std::remove_if(decorations.begin(), decorations.end(),
  223. [&unreachableDefinitions](std::unique_ptr<Instruction>& I) -> bool {
  224. Id decoration_id = I.get()->getIdOperand(0);
  225. return unreachableDefinitions.count(decoration_id) != 0;
  226. }),
  227. decorations.end());
  228. // Add per-instruction capabilities, extensions, etc.,
  229. // process all reachable instructions...
  230. for (auto bi = reachableBlocks.cbegin(); bi != reachableBlocks.cend(); ++bi) {
  231. const Block* block = *bi;
  232. const auto function = [this](const std::unique_ptr<Instruction>& inst) { postProcessReachable(*inst.get()); };
  233. std::for_each(block->getInstructions().begin(), block->getInstructions().end(), function);
  234. }
  235. // process all block-contained instructions
  236. for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
  237. Function* f = *fi;
  238. for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
  239. Block* b = *bi;
  240. for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++)
  241. postProcess(*ii->get());
  242. }
  243. }
  244. }
  245. }; // end spv namespace