convert_to_half_pass.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2019 Valve Corporation
  2. // Copyright (c) 2019 LunarG Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #ifndef LIBSPIRV_OPT_CONVERT_TO_HALF_PASS_H_
  16. #define LIBSPIRV_OPT_CONVERT_TO_HALF_PASS_H_
  17. #include "source/opt/ir_builder.h"
  18. #include "source/opt/pass.h"
  19. namespace spvtools {
  20. namespace opt {
  21. class ConvertToHalfPass : public Pass {
  22. public:
  23. ConvertToHalfPass() : Pass() {}
  24. ~ConvertToHalfPass() override = default;
  25. IRContext::Analysis GetPreservedAnalyses() override {
  26. return IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping;
  27. }
  28. // See optimizer.hpp for pass user documentation.
  29. Status Process() override;
  30. const char* name() const override { return "convert-to-half-pass"; }
  31. private:
  32. // Return true if |inst| is an arithmetic, composite or phi op that can be
  33. // of type float16
  34. bool IsArithmetic(Instruction* inst);
  35. // Return true if |inst| returns scalar, vector or matrix type with base
  36. // float and |width|
  37. bool IsFloat(Instruction* inst, uint32_t width);
  38. bool IsStruct(Instruction* inst);
  39. // Return true if |inst| is decorated with RelaxedPrecision
  40. bool IsDecoratedRelaxed(Instruction* inst);
  41. // Return true if |id| has been added to the relaxed id set
  42. bool IsRelaxed(uint32_t id);
  43. // Add |id| to the relaxed id set
  44. void AddRelaxed(uint32_t id);
  45. // Return true if the instruction's operands can be relaxed
  46. bool CanRelaxOpOperands(Instruction* inst);
  47. // Return type id for float with |width|
  48. analysis::Type* FloatScalarType(uint32_t width);
  49. // Return type id for vector of length |vlen| of float of |width|
  50. analysis::Type* FloatVectorType(uint32_t v_len, uint32_t width);
  51. // Return type id for matrix of |v_cnt| vectors of length identical to
  52. // |vty_id| of float of |width|
  53. analysis::Type* FloatMatrixType(uint32_t v_cnt, uint32_t vty_id,
  54. uint32_t width);
  55. // Return equivalent to float type |ty_id| with |width|
  56. uint32_t EquivFloatTypeId(uint32_t ty_id, uint32_t width);
  57. // Append instructions to builder to convert value |*val_idp| to type
  58. // |ty_id| but with |width|. Set |*val_idp| to the new id.
  59. void GenConvert(uint32_t* val_idp, uint32_t width, Instruction* inst);
  60. // Remove RelaxedPrecision decoration of |id|.
  61. bool RemoveRelaxedDecoration(uint32_t id);
  62. // Add |inst| to relaxed instruction set if warranted. Specifically, if
  63. // it is float32 and either decorated relaxed or a composite or phi
  64. // instruction where all operands are relaxed or all uses are relaxed.
  65. bool CloseRelaxInst(Instruction* inst);
  66. // If |inst| is an arithmetic, phi, extract or convert instruction of float32
  67. // base type and decorated with RelaxedPrecision, change it to the equivalent
  68. // float16 based type instruction. Specifically, insert instructions to
  69. // convert all operands to float16 (if needed) and change its type to the
  70. // equivalent float16 type. Otherwise, insert instructions to convert its
  71. // operands back to their original types, if needed.
  72. bool GenHalfInst(Instruction* inst);
  73. // Gen code for relaxed arithmetic |inst|
  74. bool GenHalfArith(Instruction* inst);
  75. // Gen code for relaxed phi |inst|
  76. bool ProcessPhi(Instruction* inst, uint32_t from_width, uint32_t to_width);
  77. // Gen code for relaxed convert |inst|
  78. bool ProcessConvert(Instruction* inst);
  79. // Gen code for image reference |inst|
  80. bool ProcessImageRef(Instruction* inst);
  81. // Process default non-relaxed |inst|
  82. bool ProcessDefault(Instruction* inst);
  83. // If |inst| is an FConvert of a matrix type, decompose it to a series
  84. // of vector extracts, converts and inserts into an Undef. These are
  85. // generated by GenHalfInst because they are easier to manipulate, but are
  86. // invalid so we need to clean them up.
  87. bool MatConvertCleanup(Instruction* inst);
  88. // Call GenHalfInst on every instruction in |func|.
  89. // If code is generated for an instruction, replace the instruction
  90. // with the new instructions that are generated.
  91. bool ProcessFunction(Function* func);
  92. Pass::Status ProcessImpl();
  93. // Initialize state for converting to half
  94. void Initialize();
  95. struct hasher {
  96. size_t operator()(const spv::Op& op) const noexcept {
  97. return std::hash<uint32_t>()(uint32_t(op));
  98. }
  99. };
  100. // Set of core operations to be processed
  101. std::unordered_set<spv::Op, hasher> target_ops_core_;
  102. // Set of 450 extension operations to be processed
  103. std::unordered_set<uint32_t> target_ops_450_;
  104. // Set of all sample operations, including dref and non-dref operations
  105. std::unordered_set<spv::Op, hasher> image_ops_;
  106. // Set of only dref sample operations
  107. std::unordered_set<spv::Op, hasher> dref_image_ops_;
  108. // Set of operations that can be marked as relaxed
  109. std::unordered_set<spv::Op, hasher> closure_ops_;
  110. // Set of ids of all relaxed instructions
  111. std::unordered_set<uint32_t> relaxed_ids_set_;
  112. // Ids of all converted instructions
  113. std::unordered_set<uint32_t> converted_ids_;
  114. };
  115. } // namespace opt
  116. } // namespace spvtools
  117. #endif // LIBSPIRV_OPT_CONVERT_TO_HALF_PASS_H_