blob: 2eecb6f114fa458492ffc3419307c46d05655dc0 [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
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070022#include "mlir/Transforms/Passes.h"
23
24#include "mlir/Analysis/LoopAnalysis.h"
Uday Bondhugula832b17a2018-09-07 14:47:21 -070025#include "mlir/IR/Builders.h"
26#include "mlir/IR/StandardOps.h"
27#include "mlir/IR/Statements.h"
28#include "mlir/IR/StmtVisitor.h"
Uday Bondhugula832b17a2018-09-07 14:47:21 -070029
30/// Promotes the loop body of a forStmt to its containing block if the forStmt
31/// was known to have a single iteration. Returns false otherwise.
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070032// TODO(bondhugula): extend this for arbitrary affine bounds.
Uday Bondhugula832b17a2018-09-07 14:47:21 -070033bool mlir::promoteIfSingleIteration(ForStmt *forStmt) {
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070034 Optional<uint64_t> tripCount = getConstantTripCount(*forStmt);
Uday Bondhugula832b17a2018-09-07 14:47:21 -070035 if (!tripCount.hasValue() || !forStmt->hasConstantLowerBound())
36 return false;
37
38 if (tripCount.getValue() != 1)
39 return false;
40
41 // Replaces all IV uses to its single iteration value.
42 auto *mlFunc = forStmt->findFunction();
43 MLFuncBuilder topBuilder(&mlFunc->front());
44 auto constOp = topBuilder.create<ConstantAffineIntOp>(
45 forStmt->getLoc(), forStmt->getConstantLowerBound());
46 forStmt->replaceAllUsesWith(constOp->getResult());
47 // Move the statements to the containing block.
48 auto *block = forStmt->getBlock();
49 block->getStatements().splice(StmtBlock::iterator(forStmt),
50 forStmt->getStatements());
51 forStmt->eraseFromBlock();
52 return true;
53}
54
55/// Promotes all single iteration for stmt's in the MLFunction, i.e., moves
56/// their body into the containing StmtBlock.
57void mlir::promoteSingleIterationLoops(MLFunction *f) {
58 // Gathers all innermost loops through a post order pruned walk.
59 class LoopBodyPromoter : public StmtWalker<LoopBodyPromoter> {
60 public:
61 void visitForStmt(ForStmt *forStmt) { promoteIfSingleIteration(forStmt); }
62 };
63
64 LoopBodyPromoter fsw;
65 fsw.walkPostOrder(f);
66}