blob: cc2dc401a6614be40237bcb0ac68b516c12167df [file] [log] [blame]
Uday Bondhugula832b17a2018-09-07 14:47:21 -07001//===- LoopUtils.cpp - Misc loop utilities for simplification //-----------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17//
18// This file implements miscellaneous loop simplification routines.
19//
20//===----------------------------------------------------------------------===//
21
22#include "mlir/IR/Builders.h"
23#include "mlir/IR/StandardOps.h"
24#include "mlir/IR/Statements.h"
25#include "mlir/IR/StmtVisitor.h"
26#include "mlir/Transforms/Passes.h"
27
28/// Promotes the loop body of a forStmt to its containing block if the forStmt
29/// was known to have a single iteration. Returns false otherwise.
30bool mlir::promoteIfSingleIteration(ForStmt *forStmt) {
31 Optional<uint64_t> tripCount = forStmt->getConstantTripCount();
32 if (!tripCount.hasValue() || !forStmt->hasConstantLowerBound())
33 return false;
34
35 if (tripCount.getValue() != 1)
36 return false;
37
38 // Replaces all IV uses to its single iteration value.
39 auto *mlFunc = forStmt->findFunction();
40 MLFuncBuilder topBuilder(&mlFunc->front());
41 auto constOp = topBuilder.create<ConstantAffineIntOp>(
42 forStmt->getLoc(), forStmt->getConstantLowerBound());
43 forStmt->replaceAllUsesWith(constOp->getResult());
44 // Move the statements to the containing block.
45 auto *block = forStmt->getBlock();
46 block->getStatements().splice(StmtBlock::iterator(forStmt),
47 forStmt->getStatements());
48 forStmt->eraseFromBlock();
49 return true;
50}
51
52/// Promotes all single iteration for stmt's in the MLFunction, i.e., moves
53/// their body into the containing StmtBlock.
54void mlir::promoteSingleIterationLoops(MLFunction *f) {
55 // Gathers all innermost loops through a post order pruned walk.
56 class LoopBodyPromoter : public StmtWalker<LoopBodyPromoter> {
57 public:
58 void visitForStmt(ForStmt *forStmt) { promoteIfSingleIteration(forStmt); }
59 };
60
61 LoopBodyPromoter fsw;
62 fsw.walkPostOrder(f);
63}