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