relax_float_ops_pass.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (c) 2019 The Khronos Group Inc.
  2. // Copyright (c) 2019 Valve Corporation
  3. // Copyright (c) 2019 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "relax_float_ops_pass.h"
  17. #include "source/opt/ir_builder.h"
  18. namespace spvtools {
  19. namespace opt {
  20. bool RelaxFloatOpsPass::IsRelaxable(Instruction* inst) {
  21. return target_ops_core_f_rslt_.count(inst->opcode()) != 0 ||
  22. target_ops_core_f_opnd_.count(inst->opcode()) != 0 ||
  23. sample_ops_.count(inst->opcode()) != 0 ||
  24. (inst->opcode() == SpvOpExtInst &&
  25. inst->GetSingleWordInOperand(0) ==
  26. context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  27. target_ops_450_.count(inst->GetSingleWordInOperand(1)) != 0);
  28. }
  29. bool RelaxFloatOpsPass::IsFloat32(Instruction* inst) {
  30. uint32_t ty_id;
  31. if (target_ops_core_f_opnd_.count(inst->opcode()) != 0) {
  32. uint32_t opnd_id = inst->GetSingleWordInOperand(0);
  33. Instruction* opnd_inst = get_def_use_mgr()->GetDef(opnd_id);
  34. ty_id = opnd_inst->type_id();
  35. } else {
  36. ty_id = inst->type_id();
  37. if (ty_id == 0) return false;
  38. }
  39. return IsFloat(ty_id, 32);
  40. }
  41. bool RelaxFloatOpsPass::IsRelaxed(uint32_t r_id) {
  42. for (auto r_inst : get_decoration_mgr()->GetDecorationsFor(r_id, false))
  43. if (r_inst->opcode() == SpvOpDecorate &&
  44. r_inst->GetSingleWordInOperand(1) == SpvDecorationRelaxedPrecision)
  45. return true;
  46. return false;
  47. }
  48. bool RelaxFloatOpsPass::ProcessInst(Instruction* r_inst) {
  49. uint32_t r_id = r_inst->result_id();
  50. if (r_id == 0) return false;
  51. if (!IsFloat32(r_inst)) return false;
  52. if (IsRelaxed(r_id)) return false;
  53. if (!IsRelaxable(r_inst)) return false;
  54. get_decoration_mgr()->AddDecoration(r_id, SpvDecorationRelaxedPrecision);
  55. return true;
  56. }
  57. bool RelaxFloatOpsPass::ProcessFunction(Function* func) {
  58. bool modified = false;
  59. cfg()->ForEachBlockInReversePostOrder(
  60. func->entry().get(), [&modified, this](BasicBlock* bb) {
  61. for (auto ii = bb->begin(); ii != bb->end(); ++ii)
  62. modified |= ProcessInst(&*ii);
  63. });
  64. return modified;
  65. }
  66. Pass::Status RelaxFloatOpsPass::ProcessImpl() {
  67. Pass::ProcessFunction pfn = [this](Function* fp) {
  68. return ProcessFunction(fp);
  69. };
  70. bool modified = context()->ProcessEntryPointCallTree(pfn);
  71. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  72. }
  73. Pass::Status RelaxFloatOpsPass::Process() {
  74. Initialize();
  75. return ProcessImpl();
  76. }
  77. void RelaxFloatOpsPass::Initialize() {
  78. target_ops_core_f_rslt_ = {
  79. SpvOpLoad,
  80. SpvOpPhi,
  81. SpvOpVectorExtractDynamic,
  82. SpvOpVectorInsertDynamic,
  83. SpvOpVectorShuffle,
  84. SpvOpCompositeExtract,
  85. SpvOpCompositeConstruct,
  86. SpvOpCompositeInsert,
  87. SpvOpCopyObject,
  88. SpvOpTranspose,
  89. SpvOpConvertSToF,
  90. SpvOpConvertUToF,
  91. SpvOpFConvert,
  92. // SpvOpQuantizeToF16,
  93. SpvOpFNegate,
  94. SpvOpFAdd,
  95. SpvOpFSub,
  96. SpvOpFMul,
  97. SpvOpFDiv,
  98. SpvOpFMod,
  99. SpvOpVectorTimesScalar,
  100. SpvOpMatrixTimesScalar,
  101. SpvOpVectorTimesMatrix,
  102. SpvOpMatrixTimesVector,
  103. SpvOpMatrixTimesMatrix,
  104. SpvOpOuterProduct,
  105. SpvOpDot,
  106. SpvOpSelect,
  107. };
  108. target_ops_core_f_opnd_ = {
  109. SpvOpFOrdEqual,
  110. SpvOpFUnordEqual,
  111. SpvOpFOrdNotEqual,
  112. SpvOpFUnordNotEqual,
  113. SpvOpFOrdLessThan,
  114. SpvOpFUnordLessThan,
  115. SpvOpFOrdGreaterThan,
  116. SpvOpFUnordGreaterThan,
  117. SpvOpFOrdLessThanEqual,
  118. SpvOpFUnordLessThanEqual,
  119. SpvOpFOrdGreaterThanEqual,
  120. SpvOpFUnordGreaterThanEqual,
  121. };
  122. target_ops_450_ = {
  123. GLSLstd450Round, GLSLstd450RoundEven, GLSLstd450Trunc, GLSLstd450FAbs,
  124. GLSLstd450FSign, GLSLstd450Floor, GLSLstd450Ceil, GLSLstd450Fract,
  125. GLSLstd450Radians, GLSLstd450Degrees, GLSLstd450Sin, GLSLstd450Cos,
  126. GLSLstd450Tan, GLSLstd450Asin, GLSLstd450Acos, GLSLstd450Atan,
  127. GLSLstd450Sinh, GLSLstd450Cosh, GLSLstd450Tanh, GLSLstd450Asinh,
  128. GLSLstd450Acosh, GLSLstd450Atanh, GLSLstd450Atan2, GLSLstd450Pow,
  129. GLSLstd450Exp, GLSLstd450Log, GLSLstd450Exp2, GLSLstd450Log2,
  130. GLSLstd450Sqrt, GLSLstd450InverseSqrt, GLSLstd450Determinant,
  131. GLSLstd450MatrixInverse,
  132. // TODO(greg-lunarg): GLSLstd450ModfStruct,
  133. GLSLstd450FMin, GLSLstd450FMax, GLSLstd450FClamp, GLSLstd450FMix,
  134. GLSLstd450Step, GLSLstd450SmoothStep, GLSLstd450Fma,
  135. // TODO(greg-lunarg): GLSLstd450FrexpStruct,
  136. GLSLstd450Ldexp, GLSLstd450Length, GLSLstd450Distance, GLSLstd450Cross,
  137. GLSLstd450Normalize, GLSLstd450FaceForward, GLSLstd450Reflect,
  138. GLSLstd450Refract, GLSLstd450NMin, GLSLstd450NMax, GLSLstd450NClamp};
  139. sample_ops_ = {SpvOpImageSampleImplicitLod,
  140. SpvOpImageSampleExplicitLod,
  141. SpvOpImageSampleDrefImplicitLod,
  142. SpvOpImageSampleDrefExplicitLod,
  143. SpvOpImageSampleProjImplicitLod,
  144. SpvOpImageSampleProjExplicitLod,
  145. SpvOpImageSampleProjDrefImplicitLod,
  146. SpvOpImageSampleProjDrefExplicitLod,
  147. SpvOpImageFetch,
  148. SpvOpImageGather,
  149. SpvOpImageDrefGather,
  150. SpvOpImageRead,
  151. SpvOpImageSparseSampleImplicitLod,
  152. SpvOpImageSparseSampleExplicitLod,
  153. SpvOpImageSparseSampleDrefImplicitLod,
  154. SpvOpImageSparseSampleDrefExplicitLod,
  155. SpvOpImageSparseSampleProjImplicitLod,
  156. SpvOpImageSparseSampleProjExplicitLod,
  157. SpvOpImageSparseSampleProjDrefImplicitLod,
  158. SpvOpImageSparseSampleProjDrefExplicitLod,
  159. SpvOpImageSparseFetch,
  160. SpvOpImageSparseGather,
  161. SpvOpImageSparseDrefGather,
  162. SpvOpImageSparseTexelsResident,
  163. SpvOpImageSparseRead};
  164. }
  165. } // namespace opt
  166. } // namespace spvtools