relax_float_ops_pass.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_RELAX_FLOAT_OPS_PASS_H_
  16. #define LIBSPIRV_OPT_RELAX_FLOAT_OPS_PASS_H_
  17. #include "source/opt/ir_builder.h"
  18. #include "source/opt/pass.h"
  19. namespace spvtools {
  20. namespace opt {
  21. class RelaxFloatOpsPass : public Pass {
  22. public:
  23. RelaxFloatOpsPass() : Pass() {}
  24. ~RelaxFloatOpsPass() 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| can have the RelaxedPrecision decoration applied
  33. // to it.
  34. bool IsRelaxable(Instruction* inst);
  35. // Return true if |inst| returns scalar, vector or matrix type with base
  36. // float and width 32
  37. bool IsFloat32(Instruction* inst);
  38. // Return true if |r_id| is decorated with RelaxedPrecision
  39. bool IsRelaxed(uint32_t r_id);
  40. // If |inst| is an instruction of float32-based type and is not decorated
  41. // RelaxedPrecision, add such a decoration to the module.
  42. bool ProcessInst(Instruction* inst);
  43. // Call ProcessInst on every instruction in |func|.
  44. bool ProcessFunction(Function* func);
  45. Pass::Status ProcessImpl();
  46. // Initialize state for converting to half
  47. void Initialize();
  48. struct hasher {
  49. size_t operator()(const spv::Op& op) const noexcept {
  50. return std::hash<uint32_t>()(uint32_t(op));
  51. }
  52. };
  53. // Set of float result core operations to be processed
  54. std::unordered_set<spv::Op, hasher> target_ops_core_f_rslt_;
  55. // Set of float operand core operations to be processed
  56. std::unordered_set<spv::Op, hasher> target_ops_core_f_opnd_;
  57. // Set of 450 extension operations to be processed
  58. std::unordered_set<uint32_t> target_ops_450_;
  59. // Set of sample operations
  60. std::unordered_set<spv::Op, hasher> sample_ops_;
  61. };
  62. } // namespace opt
  63. } // namespace spvtools
  64. #endif // LIBSPIRV_OPT_RELAX_FLOAT_OPS_PASS_H_