160f443bbSAlex Zinenko //===- ParallelLoopTiling.cpp - Tiles scf.parallel ---------------===//
2c25b20c0SAlex Zinenko //
3c25b20c0SAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c25b20c0SAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
5c25b20c0SAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c25b20c0SAlex Zinenko //
7c25b20c0SAlex Zinenko //===----------------------------------------------------------------------===//
8c25b20c0SAlex Zinenko //
9c25b20c0SAlex Zinenko // This file implements loop tiling on parallel loops.
10c25b20c0SAlex Zinenko //
11c25b20c0SAlex Zinenko //===----------------------------------------------------------------------===//
12c25b20c0SAlex Zinenko 
13c25b20c0SAlex Zinenko #include "PassDetail.h"
14c25b20c0SAlex Zinenko #include "mlir/Dialect/Affine/IR/AffineOps.h"
15c25b20c0SAlex Zinenko #include "mlir/Dialect/SCF/Passes.h"
16c25b20c0SAlex Zinenko #include "mlir/Dialect/SCF/SCF.h"
17c25b20c0SAlex Zinenko #include "mlir/Dialect/SCF/Transforms.h"
1880966447SAlexander Belyaev #include "mlir/Dialect/SCF/Utils.h"
19c25b20c0SAlex Zinenko #include "mlir/Dialect/StandardOps/IR/Ops.h"
20c25b20c0SAlex Zinenko 
21c25b20c0SAlex Zinenko using namespace mlir;
22c25b20c0SAlex Zinenko using namespace mlir::scf;
23c25b20c0SAlex Zinenko 
24c25b20c0SAlex Zinenko /// Tile a parallel loop of the form
2560f443bbSAlex Zinenko ///   scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3)
26c25b20c0SAlex Zinenko ///                                            step (%arg4, %arg5)
27c25b20c0SAlex Zinenko ///
28c25b20c0SAlex Zinenko /// into
2960f443bbSAlex Zinenko ///   scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3)
30c25b20c0SAlex Zinenko ///                                            step (%arg4*tileSize[0],
31c25b20c0SAlex Zinenko ///                                                  %arg5*tileSize[1])
32cd730816STobias Gysi ///     scf.parallel (%j0, %j1) = (0, 0) to (min(%arg4*tileSize[0], %arg2-%i0)
33cd730816STobias Gysi ///                                          min(%arg5*tileSize[1], %arg3-%i1))
34c25b20c0SAlex Zinenko ///                                      step (%arg4, %arg5)
351e60678cSStephan Herhut ///
36*2d45e332Stashuang.zk /// or, when no-min-max-bounds is true, into
37*2d45e332Stashuang.zk ///   scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3)
38*2d45e332Stashuang.zk ///                                            step (%arg4*tileSize[0],
39*2d45e332Stashuang.zk ///                                                  %arg5*tileSize[1])
40*2d45e332Stashuang.zk ///     scf.parallel (%j0, %j1) = (0, 0) to (%arg4*tileSize[0],
41*2d45e332Stashuang.zk ///                                          %arg5*tileSize[1])
42*2d45e332Stashuang.zk ///                                      step (%arg4, %arg5)
43*2d45e332Stashuang.zk ///        %inbound = (%j0 * %arg4 + %i0 < %arg2) &&
44*2d45e332Stashuang.zk ///                   (%j1 * %arg5 + %i1 < %arg3)
45*2d45e332Stashuang.zk ///        scf.if (%inbound)
46*2d45e332Stashuang.zk ///          ....
47*2d45e332Stashuang.zk ///
481e60678cSStephan Herhut /// where the uses of %i0 and %i1 in the loop body are replaced by
491e60678cSStephan Herhut /// %i0 + j0 and %i1 + %j1.
501e60678cSStephan Herhut //
51c25b20c0SAlex Zinenko /// The old loop is replaced with the new one.
5209c18a66SAlexander Belyaev std::pair<ParallelOp, ParallelOp>
53*2d45e332Stashuang.zk mlir::scf::tileParallelLoop(ParallelOp op, ArrayRef<int64_t> tileSizes,
54*2d45e332Stashuang.zk                             bool noMinMaxBounds) {
55c25b20c0SAlex Zinenko   OpBuilder b(op);
56c25b20c0SAlex Zinenko   auto zero = b.create<ConstantIndexOp>(op.getLoc(), 0);
57c25b20c0SAlex Zinenko   SmallVector<Value, 2> tileSizeConstants;
58c25b20c0SAlex Zinenko   tileSizeConstants.reserve(op.upperBound().size());
59c25b20c0SAlex Zinenko   for (size_t i = 0, end = op.upperBound().size(); i != end; ++i) {
60c25b20c0SAlex Zinenko     if (i < tileSizes.size())
61c25b20c0SAlex Zinenko       tileSizeConstants.push_back(
62c25b20c0SAlex Zinenko           b.create<ConstantIndexOp>(op.getLoc(), tileSizes[i]));
63c25b20c0SAlex Zinenko     else
64c25b20c0SAlex Zinenko       // Just pick 1 for the remaining dimensions.
65c25b20c0SAlex Zinenko       tileSizeConstants.push_back(b.create<ConstantIndexOp>(op.getLoc(), 1));
66c25b20c0SAlex Zinenko   }
67c25b20c0SAlex Zinenko 
68c25b20c0SAlex Zinenko   // Create the outer loop with adjusted steps.
69c25b20c0SAlex Zinenko   SmallVector<Value, 2> newSteps;
70c25b20c0SAlex Zinenko   newSteps.reserve(op.step().size());
71c25b20c0SAlex Zinenko   for (auto step : llvm::zip(op.step(), tileSizeConstants)) {
72c25b20c0SAlex Zinenko     newSteps.push_back(
73c25b20c0SAlex Zinenko         b.create<MulIOp>(op.getLoc(), std::get<0>(step), std::get<1>(step)));
74c25b20c0SAlex Zinenko   }
75c25b20c0SAlex Zinenko   auto outerLoop = b.create<ParallelOp>(op.getLoc(), op.lowerBound(),
76c25b20c0SAlex Zinenko                                         op.upperBound(), newSteps);
77c25b20c0SAlex Zinenko   b.setInsertionPointToStart(outerLoop.getBody());
78c25b20c0SAlex Zinenko 
79c25b20c0SAlex Zinenko   // Compute min(size, dim - offset) to avoid out-of-bounds accesses.
80c25b20c0SAlex Zinenko   auto minMap = AffineMap::get(
81c25b20c0SAlex Zinenko       /*dimCount=*/3, /*symbolCount=*/0,
82c25b20c0SAlex Zinenko       {getAffineDimExpr(/*position=*/0, b.getContext()),
83c25b20c0SAlex Zinenko        getAffineDimExpr(/*position=*/1, b.getContext()) -
84c25b20c0SAlex Zinenko            getAffineDimExpr(/*position=*/2, b.getContext())},
85c25b20c0SAlex Zinenko       b.getContext());
86c25b20c0SAlex Zinenko 
87c25b20c0SAlex Zinenko   // Create the inner loop with adjusted bounds.
88c25b20c0SAlex Zinenko   SmallVector<Value, 2> newBounds;
89c25b20c0SAlex Zinenko   newBounds.reserve(op.upperBound().size());
90*2d45e332Stashuang.zk   bool needInboundCheck = false;
91cd730816STobias Gysi   for (auto dim : llvm::zip(outerLoop.lowerBound(), outerLoop.upperBound(),
92cd730816STobias Gysi                             outerLoop.step(), outerLoop.getInductionVars(),
93cd730816STobias Gysi                             op.step(), tileSizeConstants)) {
94cd730816STobias Gysi     Value lowerBound, upperBound, newStep, iv, step, tileSizeConstant;
95cd730816STobias Gysi     std::tie(lowerBound, upperBound, newStep, iv, step, tileSizeConstant) = dim;
96cd730816STobias Gysi     // Collect the statically known loop bounds
97cd730816STobias Gysi     auto lowerBoundConstant =
98cd730816STobias Gysi         dyn_cast_or_null<ConstantIndexOp>(lowerBound.getDefiningOp());
99cd730816STobias Gysi     auto upperBoundConstant =
100cd730816STobias Gysi         dyn_cast_or_null<ConstantIndexOp>(upperBound.getDefiningOp());
101cd730816STobias Gysi     auto stepConstant = dyn_cast_or_null<ConstantIndexOp>(step.getDefiningOp());
102cd730816STobias Gysi     auto tileSize =
103cd730816STobias Gysi         cast<ConstantIndexOp>(tileSizeConstant.getDefiningOp()).getValue();
104cd730816STobias Gysi     // If the loop bounds and the loop step are constant and if the number of
105cd730816STobias Gysi     // loop iterations is an integer multiple of the tile size, we use a static
106cd730816STobias Gysi     // bound for the inner loop.
107cd730816STobias Gysi     if (lowerBoundConstant && upperBoundConstant && stepConstant) {
108cd730816STobias Gysi       auto numIterations = llvm::divideCeil(upperBoundConstant.getValue() -
109cd730816STobias Gysi                                                 lowerBoundConstant.getValue(),
110cd730816STobias Gysi                                             stepConstant.getValue());
111cd730816STobias Gysi       if (numIterations % tileSize == 0) {
112cd730816STobias Gysi         newBounds.push_back(newStep);
113cd730816STobias Gysi         continue;
114cd730816STobias Gysi       }
115cd730816STobias Gysi     }
116*2d45e332Stashuang.zk 
117*2d45e332Stashuang.zk     // For InboundCheck mode, just use the variable outer step
118*2d45e332Stashuang.zk     if (noMinMaxBounds) {
119*2d45e332Stashuang.zk       newBounds.push_back(newStep);
120*2d45e332Stashuang.zk       needInboundCheck = true;
121*2d45e332Stashuang.zk       continue;
122*2d45e332Stashuang.zk     }
123*2d45e332Stashuang.zk 
124cd730816STobias Gysi     // Otherwise, we dynamically compute the bound for
125cd730816STobias Gysi     // each iteration of the outer loop.
126cd730816STobias Gysi     newBounds.push_back(
127cd730816STobias Gysi         b.create<AffineMinOp>(op.getLoc(), b.getIndexType(), minMap,
128cd730816STobias Gysi                               ValueRange{newStep, upperBound, iv}));
129c25b20c0SAlex Zinenko   }
130c25b20c0SAlex Zinenko   auto innerLoop = b.create<ParallelOp>(
131c25b20c0SAlex Zinenko       op.getLoc(), SmallVector<Value, 2>(newBounds.size(), zero), newBounds,
132c25b20c0SAlex Zinenko       op.step());
133c25b20c0SAlex Zinenko 
134*2d45e332Stashuang.zk   if (noMinMaxBounds && needInboundCheck) {
1351e60678cSStephan Herhut     b.setInsertionPointToStart(innerLoop.getBody());
136*2d45e332Stashuang.zk     // Insert in-bound check
137*2d45e332Stashuang.zk     Value inbound =
138*2d45e332Stashuang.zk         b.create<ConstantOp>(op.getLoc(), b.getIntegerType(1),
139*2d45e332Stashuang.zk                              b.getIntegerAttr(b.getIntegerType(1), 1));
140*2d45e332Stashuang.zk     for (auto dim :
141*2d45e332Stashuang.zk          llvm::zip(outerLoop.upperBound(), outerLoop.getInductionVars(),
142*2d45e332Stashuang.zk                    innerLoop.getInductionVars(), innerLoop.step())) {
143*2d45e332Stashuang.zk       Value outerUpperBound, outerIV, innerIV, innerStep;
144*2d45e332Stashuang.zk       std::tie(outerUpperBound, outerIV, innerIV, innerStep) = dim;
145*2d45e332Stashuang.zk       // %in_bound = %in_bound &&
146*2d45e332Stashuang.zk       //             (%inner_iv * %inner_step + %outer_iv < %outer_upper_bound)
147*2d45e332Stashuang.zk       Value index = b.create<AddIOp>(
148*2d45e332Stashuang.zk           op.getLoc(), b.create<MulIOp>(op.getLoc(), innerIV, innerStep),
149*2d45e332Stashuang.zk           outerIV);
150*2d45e332Stashuang.zk       Value dimInbound = b.create<CmpIOp>(op.getLoc(), CmpIPredicate::ult,
151*2d45e332Stashuang.zk                                           index, outerUpperBound);
152*2d45e332Stashuang.zk       inbound = b.create<AndOp>(op.getLoc(), inbound, dimInbound);
153*2d45e332Stashuang.zk     }
154*2d45e332Stashuang.zk     auto ifInbound = b.create<IfOp>(op.getLoc(),
155*2d45e332Stashuang.zk                                     /*resultTypes*/ ArrayRef<Type>{}, inbound,
156*2d45e332Stashuang.zk                                     /*hasElseRegion*/ false);
157*2d45e332Stashuang.zk     ifInbound.thenRegion().takeBody(op.region());
158*2d45e332Stashuang.zk     Block &thenBlock = ifInbound.thenRegion().front();
159*2d45e332Stashuang.zk     b.setInsertionPointToStart(innerLoop.getBody());
160*2d45e332Stashuang.zk     for (auto ivs : llvm::enumerate(llvm::zip(innerLoop.getInductionVars(),
161*2d45e332Stashuang.zk                                               outerLoop.getInductionVars()))) {
162*2d45e332Stashuang.zk       AddIOp newIndex = b.create<AddIOp>(op.getLoc(), std::get<0>(ivs.value()),
163*2d45e332Stashuang.zk                                          std::get<1>(ivs.value()));
164*2d45e332Stashuang.zk       thenBlock.getArgument(ivs.index())
165*2d45e332Stashuang.zk           .replaceAllUsesExcept(newIndex, newIndex);
166*2d45e332Stashuang.zk     }
167*2d45e332Stashuang.zk     thenBlock.eraseArguments(llvm::to_vector<4>(
168*2d45e332Stashuang.zk         llvm::seq((unsigned)0, thenBlock.getNumArguments())));
169*2d45e332Stashuang.zk   } else {
170*2d45e332Stashuang.zk     innerLoop.region().takeBody(op.region());
171*2d45e332Stashuang.zk     b.setInsertionPointToStart(innerLoop.getBody());
172*2d45e332Stashuang.zk     for (auto ivs : llvm::zip(innerLoop.getInductionVars(),
173*2d45e332Stashuang.zk                               outerLoop.getInductionVars())) {
174*2d45e332Stashuang.zk       Value innerIndex = std::get<0>(ivs);
1751e60678cSStephan Herhut       AddIOp newIndex =
1761e60678cSStephan Herhut           b.create<AddIOp>(op.getLoc(), std::get<0>(ivs), std::get<1>(ivs));
177*2d45e332Stashuang.zk       innerIndex.replaceAllUsesExcept(newIndex, newIndex);
178*2d45e332Stashuang.zk     }
1791e60678cSStephan Herhut   }
1801e60678cSStephan Herhut 
181c25b20c0SAlex Zinenko   op.erase();
18209c18a66SAlexander Belyaev   return std::make_pair(outerLoop, innerLoop);
183c25b20c0SAlex Zinenko }
184c25b20c0SAlex Zinenko 
185c25b20c0SAlex Zinenko namespace {
186c25b20c0SAlex Zinenko struct ParallelLoopTiling
1874bcd08ebSStephan Herhut     : public SCFParallelLoopTilingBase<ParallelLoopTiling> {
188c25b20c0SAlex Zinenko   ParallelLoopTiling() = default;
189*2d45e332Stashuang.zk   explicit ParallelLoopTiling(ArrayRef<int64_t> tileSizes,
190*2d45e332Stashuang.zk                               bool noMinMaxBounds = false) {
191c25b20c0SAlex Zinenko     this->tileSizes = tileSizes;
192*2d45e332Stashuang.zk     this->noMinMaxBounds = noMinMaxBounds;
193c25b20c0SAlex Zinenko   }
194c25b20c0SAlex Zinenko 
195c25b20c0SAlex Zinenko   void runOnFunction() override {
1966484567fSFrederik Gossen     SmallVector<ParallelOp, 2> innermostPloops;
19780966447SAlexander Belyaev     getInnermostParallelLoops(getFunction().getOperation(), innermostPloops);
1986484567fSFrederik Gossen     for (ParallelOp ploop : innermostPloops) {
199cd730816STobias Gysi       // FIXME: Add reduction support.
2006484567fSFrederik Gossen       if (ploop.getNumReductions() == 0)
201*2d45e332Stashuang.zk         tileParallelLoop(ploop, tileSizes, noMinMaxBounds);
202c25b20c0SAlex Zinenko     }
203c25b20c0SAlex Zinenko   }
204c25b20c0SAlex Zinenko };
205c25b20c0SAlex Zinenko } // namespace
206c25b20c0SAlex Zinenko 
207c25b20c0SAlex Zinenko std::unique_ptr<Pass>
208*2d45e332Stashuang.zk mlir::createParallelLoopTilingPass(ArrayRef<int64_t> tileSizes,
209*2d45e332Stashuang.zk                                    bool noMinMaxBounds) {
210*2d45e332Stashuang.zk   return std::make_unique<ParallelLoopTiling>(tileSizes, noMinMaxBounds);
211c25b20c0SAlex Zinenko }
212