relax_float_ops_pass.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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() == spv::Op::OpExtInst &&
  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() == spv::Op::OpDecorate &&
  44. spv::Decoration(r_inst->GetSingleWordInOperand(1)) ==
  45. spv::Decoration::RelaxedPrecision)
  46. return true;
  47. return false;
  48. }
  49. bool RelaxFloatOpsPass::ProcessInst(Instruction* r_inst) {
  50. uint32_t r_id = r_inst->result_id();
  51. if (r_id == 0) return false;
  52. if (!IsFloat32(r_inst)) return false;
  53. if (IsRelaxed(r_id)) return false;
  54. if (!IsRelaxable(r_inst)) return false;
  55. get_decoration_mgr()->AddDecoration(
  56. r_id, uint32_t(spv::Decoration::RelaxedPrecision));
  57. return true;
  58. }
  59. bool RelaxFloatOpsPass::ProcessFunction(Function* func) {
  60. bool modified = false;
  61. cfg()->ForEachBlockInReversePostOrder(
  62. func->entry().get(), [&modified, this](BasicBlock* bb) {
  63. for (auto ii = bb->begin(); ii != bb->end(); ++ii)
  64. modified |= ProcessInst(&*ii);
  65. });
  66. return modified;
  67. }
  68. Pass::Status RelaxFloatOpsPass::ProcessImpl() {
  69. Pass::ProcessFunction pfn = [this](Function* fp) {
  70. return ProcessFunction(fp);
  71. };
  72. bool modified = context()->ProcessReachableCallTree(pfn);
  73. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  74. }
  75. Pass::Status RelaxFloatOpsPass::Process() {
  76. Initialize();
  77. return ProcessImpl();
  78. }
  79. void RelaxFloatOpsPass::Initialize() {
  80. target_ops_core_f_rslt_ = {
  81. spv::Op::OpLoad,
  82. spv::Op::OpPhi,
  83. spv::Op::OpVectorExtractDynamic,
  84. spv::Op::OpVectorInsertDynamic,
  85. spv::Op::OpVectorShuffle,
  86. spv::Op::OpCompositeExtract,
  87. spv::Op::OpCompositeConstruct,
  88. spv::Op::OpCompositeInsert,
  89. spv::Op::OpCopyObject,
  90. spv::Op::OpTranspose,
  91. spv::Op::OpConvertSToF,
  92. spv::Op::OpConvertUToF,
  93. spv::Op::OpFConvert,
  94. // spv::Op::OpQuantizeToF16,
  95. spv::Op::OpFNegate,
  96. spv::Op::OpFAdd,
  97. spv::Op::OpFSub,
  98. spv::Op::OpFMul,
  99. spv::Op::OpFDiv,
  100. spv::Op::OpFMod,
  101. spv::Op::OpVectorTimesScalar,
  102. spv::Op::OpMatrixTimesScalar,
  103. spv::Op::OpVectorTimesMatrix,
  104. spv::Op::OpMatrixTimesVector,
  105. spv::Op::OpMatrixTimesMatrix,
  106. spv::Op::OpOuterProduct,
  107. spv::Op::OpDot,
  108. spv::Op::OpSelect,
  109. };
  110. target_ops_core_f_opnd_ = {
  111. spv::Op::OpFOrdEqual,
  112. spv::Op::OpFUnordEqual,
  113. spv::Op::OpFOrdNotEqual,
  114. spv::Op::OpFUnordNotEqual,
  115. spv::Op::OpFOrdLessThan,
  116. spv::Op::OpFUnordLessThan,
  117. spv::Op::OpFOrdGreaterThan,
  118. spv::Op::OpFUnordGreaterThan,
  119. spv::Op::OpFOrdLessThanEqual,
  120. spv::Op::OpFUnordLessThanEqual,
  121. spv::Op::OpFOrdGreaterThanEqual,
  122. spv::Op::OpFUnordGreaterThanEqual,
  123. };
  124. target_ops_450_ = {
  125. GLSLstd450Round, GLSLstd450RoundEven, GLSLstd450Trunc, GLSLstd450FAbs,
  126. GLSLstd450FSign, GLSLstd450Floor, GLSLstd450Ceil, GLSLstd450Fract,
  127. GLSLstd450Radians, GLSLstd450Degrees, GLSLstd450Sin, GLSLstd450Cos,
  128. GLSLstd450Tan, GLSLstd450Asin, GLSLstd450Acos, GLSLstd450Atan,
  129. GLSLstd450Sinh, GLSLstd450Cosh, GLSLstd450Tanh, GLSLstd450Asinh,
  130. GLSLstd450Acosh, GLSLstd450Atanh, GLSLstd450Atan2, GLSLstd450Pow,
  131. GLSLstd450Exp, GLSLstd450Log, GLSLstd450Exp2, GLSLstd450Log2,
  132. GLSLstd450Sqrt, GLSLstd450InverseSqrt, GLSLstd450Determinant,
  133. GLSLstd450MatrixInverse,
  134. // TODO(greg-lunarg): GLSLstd450ModfStruct,
  135. GLSLstd450FMin, GLSLstd450FMax, GLSLstd450FClamp, GLSLstd450FMix,
  136. GLSLstd450Step, GLSLstd450SmoothStep, GLSLstd450Fma,
  137. // TODO(greg-lunarg): GLSLstd450FrexpStruct,
  138. GLSLstd450Ldexp, GLSLstd450Length, GLSLstd450Distance, GLSLstd450Cross,
  139. GLSLstd450Normalize, GLSLstd450FaceForward, GLSLstd450Reflect,
  140. GLSLstd450Refract, GLSLstd450NMin, GLSLstd450NMax, GLSLstd450NClamp};
  141. sample_ops_ = {spv::Op::OpImageSampleImplicitLod,
  142. spv::Op::OpImageSampleExplicitLod,
  143. spv::Op::OpImageSampleDrefImplicitLod,
  144. spv::Op::OpImageSampleDrefExplicitLod,
  145. spv::Op::OpImageSampleProjImplicitLod,
  146. spv::Op::OpImageSampleProjExplicitLod,
  147. spv::Op::OpImageSampleProjDrefImplicitLod,
  148. spv::Op::OpImageSampleProjDrefExplicitLod,
  149. spv::Op::OpImageFetch,
  150. spv::Op::OpImageGather,
  151. spv::Op::OpImageDrefGather,
  152. spv::Op::OpImageRead,
  153. spv::Op::OpImageSparseSampleImplicitLod,
  154. spv::Op::OpImageSparseSampleExplicitLod,
  155. spv::Op::OpImageSparseSampleDrefImplicitLod,
  156. spv::Op::OpImageSparseSampleDrefExplicitLod,
  157. spv::Op::OpImageSparseSampleProjImplicitLod,
  158. spv::Op::OpImageSparseSampleProjExplicitLod,
  159. spv::Op::OpImageSparseSampleProjDrefImplicitLod,
  160. spv::Op::OpImageSparseSampleProjDrefExplicitLod,
  161. spv::Op::OpImageSparseFetch,
  162. spv::Op::OpImageSparseGather,
  163. spv::Op::OpImageSparseDrefGather,
  164. spv::Op::OpImageSparseTexelsResident,
  165. spv::Op::OpImageSparseRead};
  166. }
  167. } // namespace opt
  168. } // namespace spvtools