12cbf0b3dSlorenzo chelini //===- 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"
15a54f4eaeSMogball #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
168b68da2cSAlex Zinenko #include "mlir/Dialect/SCF/IR/SCF.h"
178b68da2cSAlex Zinenko #include "mlir/Dialect/SCF/Transforms/Passes.h"
188b68da2cSAlex Zinenko #include "mlir/Dialect/SCF/Transforms/Transforms.h"
19f40475c7SAdrian Kuegel #include "mlir/Dialect/SCF/Utils/Utils.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 ///
362d45e332Stashuang.zk /// or, when no-min-max-bounds is true, into
372d45e332Stashuang.zk /// scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3)
382d45e332Stashuang.zk /// step (%arg4*tileSize[0],
392d45e332Stashuang.zk /// %arg5*tileSize[1])
402d45e332Stashuang.zk /// scf.parallel (%j0, %j1) = (0, 0) to (%arg4*tileSize[0],
412d45e332Stashuang.zk /// %arg5*tileSize[1])
422d45e332Stashuang.zk /// step (%arg4, %arg5)
432d45e332Stashuang.zk /// %inbound = (%j0 * %arg4 + %i0 < %arg2) &&
442d45e332Stashuang.zk /// (%j1 * %arg5 + %i1 < %arg3)
452d45e332Stashuang.zk /// scf.if (%inbound)
462d45e332Stashuang.zk /// ....
472d45e332Stashuang.zk ///
481e60678cSStephan Herhut /// where the uses of %i0 and %i1 in the loop body are replaced by
491e60678cSStephan Herhut /// %i0 + j0 and %i1 + %j1.
502cbf0b3dSlorenzo chelini ///
51c25b20c0SAlex Zinenko /// The old loop is replaced with the new one.
5209c18a66SAlexander Belyaev std::pair<ParallelOp, ParallelOp>
tileParallelLoop(ParallelOp op,ArrayRef<int64_t> tileSizes,bool noMinMaxBounds)532d45e332Stashuang.zk mlir::scf::tileParallelLoop(ParallelOp op, ArrayRef<int64_t> tileSizes,
542d45e332Stashuang.zk bool noMinMaxBounds) {
55c25b20c0SAlex Zinenko OpBuilder b(op);
56a54f4eaeSMogball auto zero = b.create<arith::ConstantIndexOp>(op.getLoc(), 0);
57c25b20c0SAlex Zinenko SmallVector<Value, 2> tileSizeConstants;
58c0342a2dSJacques Pienaar tileSizeConstants.reserve(op.getUpperBound().size());
59c0342a2dSJacques Pienaar for (size_t i = 0, end = op.getUpperBound().size(); i != end; ++i) {
60c25b20c0SAlex Zinenko if (i < tileSizes.size())
61c25b20c0SAlex Zinenko tileSizeConstants.push_back(
62a54f4eaeSMogball b.create<arith::ConstantIndexOp>(op.getLoc(), tileSizes[i]));
63c25b20c0SAlex Zinenko else
64c25b20c0SAlex Zinenko // Just pick 1 for the remaining dimensions.
65a54f4eaeSMogball tileSizeConstants.push_back(
66a54f4eaeSMogball b.create<arith::ConstantIndexOp>(op.getLoc(), 1));
67c25b20c0SAlex Zinenko }
68c25b20c0SAlex Zinenko
69c25b20c0SAlex Zinenko // Create the outer loop with adjusted steps.
70c25b20c0SAlex Zinenko SmallVector<Value, 2> newSteps;
71c0342a2dSJacques Pienaar newSteps.reserve(op.getStep().size());
72c0342a2dSJacques Pienaar for (auto step : llvm::zip(op.getStep(), tileSizeConstants)) {
73a54f4eaeSMogball newSteps.push_back(b.create<arith::MulIOp>(op.getLoc(), std::get<0>(step),
74a54f4eaeSMogball std::get<1>(step)));
75c25b20c0SAlex Zinenko }
76c0342a2dSJacques Pienaar auto outerLoop = b.create<ParallelOp>(op.getLoc(), op.getLowerBound(),
77c0342a2dSJacques Pienaar op.getUpperBound(), newSteps);
78c25b20c0SAlex Zinenko b.setInsertionPointToStart(outerLoop.getBody());
79c25b20c0SAlex Zinenko
80c25b20c0SAlex Zinenko // Compute min(size, dim - offset) to avoid out-of-bounds accesses.
81c25b20c0SAlex Zinenko auto minMap = AffineMap::get(
82c25b20c0SAlex Zinenko /*dimCount=*/3, /*symbolCount=*/0,
83c25b20c0SAlex Zinenko {getAffineDimExpr(/*position=*/0, b.getContext()),
84c25b20c0SAlex Zinenko getAffineDimExpr(/*position=*/1, b.getContext()) -
85c25b20c0SAlex Zinenko getAffineDimExpr(/*position=*/2, b.getContext())},
86c25b20c0SAlex Zinenko b.getContext());
87c25b20c0SAlex Zinenko
88c25b20c0SAlex Zinenko // Create the inner loop with adjusted bounds.
89c25b20c0SAlex Zinenko SmallVector<Value, 2> newBounds;
90c0342a2dSJacques Pienaar newBounds.reserve(op.getUpperBound().size());
912d45e332Stashuang.zk bool needInboundCheck = false;
92c0342a2dSJacques Pienaar for (auto dim :
93c0342a2dSJacques Pienaar llvm::zip(outerLoop.getLowerBound(), outerLoop.getUpperBound(),
94c0342a2dSJacques Pienaar outerLoop.getStep(), outerLoop.getInductionVars(),
95c0342a2dSJacques Pienaar op.getStep(), tileSizeConstants)) {
96cd730816STobias Gysi Value lowerBound, upperBound, newStep, iv, step, tileSizeConstant;
97cd730816STobias Gysi std::tie(lowerBound, upperBound, newStep, iv, step, tileSizeConstant) = dim;
98cd730816STobias Gysi // Collect the statically known loop bounds
99cd730816STobias Gysi auto lowerBoundConstant =
100a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(lowerBound.getDefiningOp());
101cd730816STobias Gysi auto upperBoundConstant =
102a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(upperBound.getDefiningOp());
103a54f4eaeSMogball auto stepConstant =
104a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(step.getDefiningOp());
105cd730816STobias Gysi auto tileSize =
106a54f4eaeSMogball cast<arith::ConstantIndexOp>(tileSizeConstant.getDefiningOp()).value();
107cd730816STobias Gysi // If the loop bounds and the loop step are constant and if the number of
108cd730816STobias Gysi // loop iterations is an integer multiple of the tile size, we use a static
109cd730816STobias Gysi // bound for the inner loop.
110cd730816STobias Gysi if (lowerBoundConstant && upperBoundConstant && stepConstant) {
111a54f4eaeSMogball auto numIterations = llvm::divideCeil(upperBoundConstant.value() -
112a54f4eaeSMogball lowerBoundConstant.value(),
113a54f4eaeSMogball stepConstant.value());
114cd730816STobias Gysi if (numIterations % tileSize == 0) {
115cd730816STobias Gysi newBounds.push_back(newStep);
116cd730816STobias Gysi continue;
117cd730816STobias Gysi }
118cd730816STobias Gysi }
1192d45e332Stashuang.zk
1202d45e332Stashuang.zk // For InboundCheck mode, just use the variable outer step
1212d45e332Stashuang.zk if (noMinMaxBounds) {
1222d45e332Stashuang.zk newBounds.push_back(newStep);
1232d45e332Stashuang.zk needInboundCheck = true;
1242d45e332Stashuang.zk continue;
1252d45e332Stashuang.zk }
1262d45e332Stashuang.zk
127cd730816STobias Gysi // Otherwise, we dynamically compute the bound for
128cd730816STobias Gysi // each iteration of the outer loop.
129cd730816STobias Gysi newBounds.push_back(
130cd730816STobias Gysi b.create<AffineMinOp>(op.getLoc(), b.getIndexType(), minMap,
131cd730816STobias Gysi ValueRange{newStep, upperBound, iv}));
132c25b20c0SAlex Zinenko }
133c25b20c0SAlex Zinenko auto innerLoop = b.create<ParallelOp>(
134c25b20c0SAlex Zinenko op.getLoc(), SmallVector<Value, 2>(newBounds.size(), zero), newBounds,
135c0342a2dSJacques Pienaar op.getStep());
136c25b20c0SAlex Zinenko
1372d45e332Stashuang.zk if (noMinMaxBounds && needInboundCheck) {
1381e60678cSStephan Herhut b.setInsertionPointToStart(innerLoop.getBody());
1392d45e332Stashuang.zk // Insert in-bound check
1402d45e332Stashuang.zk Value inbound =
141a54f4eaeSMogball b.create<arith::ConstantIntOp>(op.getLoc(), 1, b.getIntegerType(1));
1422d45e332Stashuang.zk for (auto dim :
143c0342a2dSJacques Pienaar llvm::zip(outerLoop.getUpperBound(), outerLoop.getInductionVars(),
144c0342a2dSJacques Pienaar innerLoop.getInductionVars(), innerLoop.getStep())) {
1452d45e332Stashuang.zk Value outerUpperBound, outerIV, innerIV, innerStep;
1462d45e332Stashuang.zk std::tie(outerUpperBound, outerIV, innerIV, innerStep) = dim;
1472d45e332Stashuang.zk // %in_bound = %in_bound &&
1482d45e332Stashuang.zk // (%inner_iv * %inner_step + %outer_iv < %outer_upper_bound)
149a54f4eaeSMogball Value index = b.create<arith::AddIOp>(
150a54f4eaeSMogball op.getLoc(), b.create<arith::MulIOp>(op.getLoc(), innerIV, innerStep),
1512d45e332Stashuang.zk outerIV);
152a54f4eaeSMogball Value dimInbound = b.create<arith::CmpIOp>(
153a54f4eaeSMogball op.getLoc(), arith::CmpIPredicate::ult, index, outerUpperBound);
154a54f4eaeSMogball inbound = b.create<arith::AndIOp>(op.getLoc(), inbound, dimInbound);
1552d45e332Stashuang.zk }
1562d45e332Stashuang.zk auto ifInbound = b.create<IfOp>(op.getLoc(),
1572d45e332Stashuang.zk /*resultTypes*/ ArrayRef<Type>{}, inbound,
1582d45e332Stashuang.zk /*hasElseRegion*/ false);
159c0342a2dSJacques Pienaar ifInbound.getThenRegion().takeBody(op.getRegion());
160c0342a2dSJacques Pienaar Block &thenBlock = ifInbound.getThenRegion().front();
1612d45e332Stashuang.zk b.setInsertionPointToStart(innerLoop.getBody());
162e4853be2SMehdi Amini for (const auto &ivs : llvm::enumerate(llvm::zip(
163e4853be2SMehdi Amini innerLoop.getInductionVars(), outerLoop.getInductionVars()))) {
164a54f4eaeSMogball auto newIndex = b.create<arith::AddIOp>(
165a54f4eaeSMogball op.getLoc(), std::get<0>(ivs.value()), std::get<1>(ivs.value()));
1662d45e332Stashuang.zk thenBlock.getArgument(ivs.index())
1672d45e332Stashuang.zk .replaceAllUsesExcept(newIndex, newIndex);
1682d45e332Stashuang.zk }
1692d45e332Stashuang.zk thenBlock.eraseArguments(llvm::to_vector<4>(
1702d45e332Stashuang.zk llvm::seq((unsigned)0, thenBlock.getNumArguments())));
1712d45e332Stashuang.zk } else {
172c0342a2dSJacques Pienaar innerLoop.getRegion().takeBody(op.getRegion());
1732d45e332Stashuang.zk b.setInsertionPointToStart(innerLoop.getBody());
1742d45e332Stashuang.zk for (auto ivs : llvm::zip(innerLoop.getInductionVars(),
1752d45e332Stashuang.zk outerLoop.getInductionVars())) {
1762d45e332Stashuang.zk Value innerIndex = std::get<0>(ivs);
177a54f4eaeSMogball auto newIndex = b.create<arith::AddIOp>(op.getLoc(), std::get<0>(ivs),
178a54f4eaeSMogball std::get<1>(ivs));
1792d45e332Stashuang.zk innerIndex.replaceAllUsesExcept(newIndex, newIndex);
1802d45e332Stashuang.zk }
1811e60678cSStephan Herhut }
1821e60678cSStephan Herhut
183c25b20c0SAlex Zinenko op.erase();
18409c18a66SAlexander Belyaev return std::make_pair(outerLoop, innerLoop);
185c25b20c0SAlex Zinenko }
186c25b20c0SAlex Zinenko
187c25b20c0SAlex Zinenko namespace {
188c25b20c0SAlex Zinenko struct ParallelLoopTiling
1894bcd08ebSStephan Herhut : public SCFParallelLoopTilingBase<ParallelLoopTiling> {
190c25b20c0SAlex Zinenko ParallelLoopTiling() = default;
ParallelLoopTiling__anon315911750111::ParallelLoopTiling1912d45e332Stashuang.zk explicit ParallelLoopTiling(ArrayRef<int64_t> tileSizes,
1922d45e332Stashuang.zk bool noMinMaxBounds = false) {
193c25b20c0SAlex Zinenko this->tileSizes = tileSizes;
1942d45e332Stashuang.zk this->noMinMaxBounds = noMinMaxBounds;
195c25b20c0SAlex Zinenko }
196c25b20c0SAlex Zinenko
runOnOperation__anon315911750111::ParallelLoopTiling19741574554SRiver Riddle void runOnOperation() override {
198*54998986SStella Laurenzo auto *parentOp = getOperation();
1996484567fSFrederik Gossen SmallVector<ParallelOp, 2> innermostPloops;
200*54998986SStella Laurenzo getInnermostParallelLoops(parentOp, innermostPloops);
2016484567fSFrederik Gossen for (ParallelOp ploop : innermostPloops) {
202cd730816STobias Gysi // FIXME: Add reduction support.
2036484567fSFrederik Gossen if (ploop.getNumReductions() == 0)
2042d45e332Stashuang.zk tileParallelLoop(ploop, tileSizes, noMinMaxBounds);
205c25b20c0SAlex Zinenko }
206c25b20c0SAlex Zinenko }
207c25b20c0SAlex Zinenko };
208c25b20c0SAlex Zinenko } // namespace
209c25b20c0SAlex Zinenko
210c25b20c0SAlex Zinenko std::unique_ptr<Pass>
createParallelLoopTilingPass(ArrayRef<int64_t> tileSizes,bool noMinMaxBounds)2112d45e332Stashuang.zk mlir::createParallelLoopTilingPass(ArrayRef<int64_t> tileSizes,
2122d45e332Stashuang.zk bool noMinMaxBounds) {
2132d45e332Stashuang.zk return std::make_unique<ParallelLoopTiling>(tileSizes, noMinMaxBounds);
214c25b20c0SAlex Zinenko }
215