loop_fission.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_OPT_LOOP_FISSION_H_
  15. #define SOURCE_OPT_LOOP_FISSION_H_
  16. #include <algorithm>
  17. #include <cstdint>
  18. #include <map>
  19. #include <utility>
  20. #include <vector>
  21. #include "source/opt/cfg.h"
  22. #include "source/opt/loop_dependence.h"
  23. #include "source/opt/loop_utils.h"
  24. #include "source/opt/module.h"
  25. #include "source/opt/pass.h"
  26. #include "source/opt/tree_iterator.h"
  27. namespace spvtools {
  28. namespace opt {
  29. class LoopFissionPass : public Pass {
  30. public:
  31. // Function used to determine if a given loop should be split. Takes register
  32. // pressure region for that loop as a parameter and returns true if the loop
  33. // should be split.
  34. using FissionCriteriaFunction =
  35. std::function<bool(const RegisterLiveness::RegionRegisterLiveness&)>;
  36. // Pass built with this constructor will split all loops regardless of
  37. // register pressure. Will not split loops more than once.
  38. LoopFissionPass();
  39. // Split the loop if the number of registers used in the loop exceeds
  40. // |register_threshold_to_split|. |split_multiple_times| flag determines
  41. // whether or not the pass should split loops after already splitting them
  42. // once.
  43. LoopFissionPass(size_t register_threshold_to_split,
  44. bool split_multiple_times = true);
  45. // Split loops whose register pressure meets the criteria of |functor|.
  46. LoopFissionPass(FissionCriteriaFunction functor,
  47. bool split_multiple_times = true)
  48. : split_criteria_(functor), split_multiple_times_(split_multiple_times) {}
  49. const char* name() const override { return "loop-fission"; }
  50. Pass::Status Process() override;
  51. // Checks if |loop| meets the register pressure criteria to be split.
  52. bool ShouldSplitLoop(const Loop& loop, IRContext* context);
  53. private:
  54. // Functor to run in ShouldSplitLoop to determine if the register pressure
  55. // criteria is met for splitting the loop.
  56. FissionCriteriaFunction split_criteria_;
  57. // Flag designating whether or not we should also split the result of
  58. // previously split loops if they meet the register presure criteria.
  59. bool split_multiple_times_;
  60. };
  61. } // namespace opt
  62. } // namespace spvtools
  63. #endif // SOURCE_OPT_LOOP_FISSION_H_