xref: /llvm-project/mlir/lib/Dialect/Affine/Transforms/AffineLoopNormalize.cpp (revision 4c48f016effde67d500fc95290096aec9f3bdb70)
1 //===- AffineLoopNormalize.cpp - AffineLoopNormalize Pass -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a normalizer for affine loop-like ops.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/Affine/Passes.h"
14 
15 #include "mlir/Dialect/Affine/IR/AffineOps.h"
16 #include "mlir/Dialect/Affine/Utils.h"
17 #include "mlir/Dialect/Func/IR/FuncOps.h"
18 
19 namespace mlir {
20 namespace affine {
21 #define GEN_PASS_DEF_AFFINELOOPNORMALIZE
22 #include "mlir/Dialect/Affine/Passes.h.inc"
23 } // namespace affine
24 } // namespace mlir
25 
26 using namespace mlir;
27 using namespace mlir::affine;
28 
29 namespace {
30 
31 /// Normalize affine.parallel ops so that lower bounds are 0 and steps are 1.
32 /// As currently implemented, this pass cannot fail, but it might skip over ops
33 /// that are already in a normalized form.
34 struct AffineLoopNormalizePass
35     : public affine::impl::AffineLoopNormalizeBase<AffineLoopNormalizePass> {
AffineLoopNormalizePass__anon436e028b0111::AffineLoopNormalizePass36   explicit AffineLoopNormalizePass(bool promoteSingleIter) {
37     this->promoteSingleIter = promoteSingleIter;
38   }
39 
runOnOperation__anon436e028b0111::AffineLoopNormalizePass40   void runOnOperation() override {
41     getOperation().walk([&](Operation *op) {
42       if (auto affineParallel = dyn_cast<AffineParallelOp>(op))
43         normalizeAffineParallel(affineParallel);
44       else if (auto affineFor = dyn_cast<AffineForOp>(op))
45         (void)normalizeAffineFor(affineFor, promoteSingleIter);
46     });
47   }
48 };
49 
50 } // namespace
51 
52 std::unique_ptr<OperationPass<func::FuncOp>>
createAffineLoopNormalizePass(bool promoteSingleIter)53 mlir::affine::createAffineLoopNormalizePass(bool promoteSingleIter) {
54   return std::make_unique<AffineLoopNormalizePass>(promoteSingleIter);
55 }
56