xref: /llvm-project/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp (revision 4bcd08eb1c3bcd6ce580cc21b4e6d9f10286b9e6)
1*4bcd08ebSStephan Herhut //===- LoopSpecialization.cpp - scf.parallel/SCR.for specialization -------===//
2*4bcd08ebSStephan Herhut //
3*4bcd08ebSStephan Herhut // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4bcd08ebSStephan Herhut // See https://llvm.org/LICENSE.txt for license information.
5*4bcd08ebSStephan Herhut // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4bcd08ebSStephan Herhut //
7*4bcd08ebSStephan Herhut //===----------------------------------------------------------------------===//
8*4bcd08ebSStephan Herhut //
9*4bcd08ebSStephan Herhut // Specializes parallel loops and for loops for easier unrolling and
10*4bcd08ebSStephan Herhut // vectorization.
11*4bcd08ebSStephan Herhut //
12*4bcd08ebSStephan Herhut //===----------------------------------------------------------------------===//
13*4bcd08ebSStephan Herhut 
14*4bcd08ebSStephan Herhut #include "PassDetail.h"
15*4bcd08ebSStephan Herhut #include "mlir/Dialect/Affine/IR/AffineOps.h"
16*4bcd08ebSStephan Herhut #include "mlir/Dialect/SCF/Passes.h"
17*4bcd08ebSStephan Herhut #include "mlir/Dialect/SCF/SCF.h"
18*4bcd08ebSStephan Herhut #include "mlir/Dialect/StandardOps/IR/Ops.h"
19*4bcd08ebSStephan Herhut #include "mlir/IR/AffineExpr.h"
20*4bcd08ebSStephan Herhut #include "mlir/IR/BlockAndValueMapping.h"
21*4bcd08ebSStephan Herhut 
22*4bcd08ebSStephan Herhut using namespace mlir;
23*4bcd08ebSStephan Herhut using scf::ForOp;
24*4bcd08ebSStephan Herhut using scf::ParallelOp;
25*4bcd08ebSStephan Herhut 
26*4bcd08ebSStephan Herhut /// Rewrite a parallel loop with bounds defined by an affine.min with a constant
27*4bcd08ebSStephan Herhut /// into 2 loops after checking if the bounds are equal to that constant. This
28*4bcd08ebSStephan Herhut /// is beneficial if the loop will almost always have the constant bound and
29*4bcd08ebSStephan Herhut /// that version can be fully unrolled and vectorized.
30*4bcd08ebSStephan Herhut static void specializeParallelLoopForUnrolling(ParallelOp op) {
31*4bcd08ebSStephan Herhut   SmallVector<int64_t, 2> constantIndices;
32*4bcd08ebSStephan Herhut   constantIndices.reserve(op.upperBound().size());
33*4bcd08ebSStephan Herhut   for (auto bound : op.upperBound()) {
34*4bcd08ebSStephan Herhut     auto minOp = bound.getDefiningOp<AffineMinOp>();
35*4bcd08ebSStephan Herhut     if (!minOp)
36*4bcd08ebSStephan Herhut       return;
37*4bcd08ebSStephan Herhut     int64_t minConstant = std::numeric_limits<int64_t>::max();
38*4bcd08ebSStephan Herhut     for (AffineExpr expr : minOp.map().getResults()) {
39*4bcd08ebSStephan Herhut       if (auto constantIndex = expr.dyn_cast<AffineConstantExpr>())
40*4bcd08ebSStephan Herhut         minConstant = std::min(minConstant, constantIndex.getValue());
41*4bcd08ebSStephan Herhut     }
42*4bcd08ebSStephan Herhut     if (minConstant == std::numeric_limits<int64_t>::max())
43*4bcd08ebSStephan Herhut       return;
44*4bcd08ebSStephan Herhut     constantIndices.push_back(minConstant);
45*4bcd08ebSStephan Herhut   }
46*4bcd08ebSStephan Herhut 
47*4bcd08ebSStephan Herhut   OpBuilder b(op);
48*4bcd08ebSStephan Herhut   BlockAndValueMapping map;
49*4bcd08ebSStephan Herhut   Value cond;
50*4bcd08ebSStephan Herhut   for (auto bound : llvm::zip(op.upperBound(), constantIndices)) {
51*4bcd08ebSStephan Herhut     Value constant = b.create<ConstantIndexOp>(op.getLoc(), std::get<1>(bound));
52*4bcd08ebSStephan Herhut     Value cmp = b.create<CmpIOp>(op.getLoc(), CmpIPredicate::eq,
53*4bcd08ebSStephan Herhut                                  std::get<0>(bound), constant);
54*4bcd08ebSStephan Herhut     cond = cond ? b.create<AndOp>(op.getLoc(), cond, cmp) : cmp;
55*4bcd08ebSStephan Herhut     map.map(std::get<0>(bound), constant);
56*4bcd08ebSStephan Herhut   }
57*4bcd08ebSStephan Herhut   auto ifOp = b.create<scf::IfOp>(op.getLoc(), cond, /*withElseRegion=*/true);
58*4bcd08ebSStephan Herhut   ifOp.getThenBodyBuilder().clone(*op.getOperation(), map);
59*4bcd08ebSStephan Herhut   ifOp.getElseBodyBuilder().clone(*op.getOperation());
60*4bcd08ebSStephan Herhut   op.erase();
61*4bcd08ebSStephan Herhut }
62*4bcd08ebSStephan Herhut 
63*4bcd08ebSStephan Herhut /// Rewrite a for loop with bounds defined by an affine.min with a constant into
64*4bcd08ebSStephan Herhut /// 2 loops after checking if the bounds are equal to that constant. This is
65*4bcd08ebSStephan Herhut /// beneficial if the loop will almost always have the constant bound and that
66*4bcd08ebSStephan Herhut /// version can be fully unrolled and vectorized.
67*4bcd08ebSStephan Herhut static void specializeForLoopForUnrolling(ForOp op) {
68*4bcd08ebSStephan Herhut   auto bound = op.upperBound();
69*4bcd08ebSStephan Herhut   auto minOp = bound.getDefiningOp<AffineMinOp>();
70*4bcd08ebSStephan Herhut   if (!minOp)
71*4bcd08ebSStephan Herhut     return;
72*4bcd08ebSStephan Herhut   int64_t minConstant = std::numeric_limits<int64_t>::max();
73*4bcd08ebSStephan Herhut   for (AffineExpr expr : minOp.map().getResults()) {
74*4bcd08ebSStephan Herhut     if (auto constantIndex = expr.dyn_cast<AffineConstantExpr>())
75*4bcd08ebSStephan Herhut       minConstant = std::min(minConstant, constantIndex.getValue());
76*4bcd08ebSStephan Herhut   }
77*4bcd08ebSStephan Herhut   if (minConstant == std::numeric_limits<int64_t>::max())
78*4bcd08ebSStephan Herhut     return;
79*4bcd08ebSStephan Herhut 
80*4bcd08ebSStephan Herhut   OpBuilder b(op);
81*4bcd08ebSStephan Herhut   BlockAndValueMapping map;
82*4bcd08ebSStephan Herhut   Value constant = b.create<ConstantIndexOp>(op.getLoc(), minConstant);
83*4bcd08ebSStephan Herhut   Value cond =
84*4bcd08ebSStephan Herhut       b.create<CmpIOp>(op.getLoc(), CmpIPredicate::eq, bound, constant);
85*4bcd08ebSStephan Herhut   map.map(bound, constant);
86*4bcd08ebSStephan Herhut   auto ifOp = b.create<scf::IfOp>(op.getLoc(), cond, /*withElseRegion=*/true);
87*4bcd08ebSStephan Herhut   ifOp.getThenBodyBuilder().clone(*op.getOperation(), map);
88*4bcd08ebSStephan Herhut   ifOp.getElseBodyBuilder().clone(*op.getOperation());
89*4bcd08ebSStephan Herhut   op.erase();
90*4bcd08ebSStephan Herhut }
91*4bcd08ebSStephan Herhut 
92*4bcd08ebSStephan Herhut namespace {
93*4bcd08ebSStephan Herhut struct ParallelLoopSpecialization
94*4bcd08ebSStephan Herhut     : public SCFParallelLoopSpecializationBase<ParallelLoopSpecialization> {
95*4bcd08ebSStephan Herhut   void runOnFunction() override {
96*4bcd08ebSStephan Herhut     getFunction().walk(
97*4bcd08ebSStephan Herhut         [](ParallelOp op) { specializeParallelLoopForUnrolling(op); });
98*4bcd08ebSStephan Herhut   }
99*4bcd08ebSStephan Herhut };
100*4bcd08ebSStephan Herhut 
101*4bcd08ebSStephan Herhut struct ForLoopSpecialization
102*4bcd08ebSStephan Herhut     : public SCFForLoopSpecializationBase<ForLoopSpecialization> {
103*4bcd08ebSStephan Herhut   void runOnFunction() override {
104*4bcd08ebSStephan Herhut     getFunction().walk([](ForOp op) { specializeForLoopForUnrolling(op); });
105*4bcd08ebSStephan Herhut   }
106*4bcd08ebSStephan Herhut };
107*4bcd08ebSStephan Herhut } // namespace
108*4bcd08ebSStephan Herhut 
109*4bcd08ebSStephan Herhut std::unique_ptr<Pass> mlir::createParallelLoopSpecializationPass() {
110*4bcd08ebSStephan Herhut   return std::make_unique<ParallelLoopSpecialization>();
111*4bcd08ebSStephan Herhut }
112*4bcd08ebSStephan Herhut 
113*4bcd08ebSStephan Herhut std::unique_ptr<Pass> mlir::createForLoopSpecializationPass() {
114*4bcd08ebSStephan Herhut   return std::make_unique<ForLoopSpecialization>();
115*4bcd08ebSStephan Herhut }
116