loop_fusion_pass.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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_FUSION_PASS_H_
  15. #define SOURCE_OPT_LOOP_FUSION_PASS_H_
  16. #include "source/opt/pass.h"
  17. namespace spvtools {
  18. namespace opt {
  19. // Implements a loop fusion pass.
  20. // This pass will look for adjacent loops that are compatible and legal to be
  21. // fused. It will fuse all such loops as long as the register usage for the
  22. // fused loop stays under the threshold defined by |max_registers_per_loop|.
  23. class LoopFusionPass : public Pass {
  24. public:
  25. explicit LoopFusionPass(size_t max_registers_per_loop)
  26. : Pass(), max_registers_per_loop_(max_registers_per_loop) {}
  27. const char* name() const override { return "loop-fusion"; }
  28. // Processes the given |module|. Returns Status::Failure if errors occur when
  29. // processing. Returns the corresponding Status::Success if processing is
  30. // successful to indicate whether changes have been made to the module.
  31. Status Process() override;
  32. private:
  33. // Fuse loops in |function| if compatible, legal and the fused loop won't use
  34. // too many registers.
  35. bool ProcessFunction(Function* function);
  36. // The maximum number of registers a fused loop is allowed to use.
  37. size_t max_registers_per_loop_;
  38. };
  39. } // namespace opt
  40. } // namespace spvtools
  41. #endif // SOURCE_OPT_LOOP_FUSION_PASS_H_