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" 15a54f4eaeSMogball #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" 16c25b20c0SAlex Zinenko #include "mlir/Dialect/SCF/Passes.h" 17c25b20c0SAlex Zinenko #include "mlir/Dialect/SCF/SCF.h" 18c25b20c0SAlex Zinenko #include "mlir/Dialect/SCF/Transforms.h" 1980966447SAlexander Belyaev #include "mlir/Dialect/SCF/Utils.h" 20c25b20c0SAlex Zinenko #include "mlir/Dialect/StandardOps/IR/Ops.h" 21c25b20c0SAlex Zinenko 22c25b20c0SAlex Zinenko using namespace mlir; 23c25b20c0SAlex Zinenko using namespace mlir::scf; 24c25b20c0SAlex Zinenko 25c25b20c0SAlex Zinenko /// Tile a parallel loop of the form 2660f443bbSAlex Zinenko /// scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3) 27c25b20c0SAlex Zinenko /// step (%arg4, %arg5) 28c25b20c0SAlex Zinenko /// 29c25b20c0SAlex Zinenko /// into 3060f443bbSAlex Zinenko /// scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3) 31c25b20c0SAlex Zinenko /// step (%arg4*tileSize[0], 32c25b20c0SAlex Zinenko /// %arg5*tileSize[1]) 33cd730816STobias Gysi /// scf.parallel (%j0, %j1) = (0, 0) to (min(%arg4*tileSize[0], %arg2-%i0) 34cd730816STobias Gysi /// min(%arg5*tileSize[1], %arg3-%i1)) 35c25b20c0SAlex Zinenko /// step (%arg4, %arg5) 361e60678cSStephan Herhut /// 372d45e332Stashuang.zk /// or, when no-min-max-bounds is true, into 382d45e332Stashuang.zk /// scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3) 392d45e332Stashuang.zk /// step (%arg4*tileSize[0], 402d45e332Stashuang.zk /// %arg5*tileSize[1]) 412d45e332Stashuang.zk /// scf.parallel (%j0, %j1) = (0, 0) to (%arg4*tileSize[0], 422d45e332Stashuang.zk /// %arg5*tileSize[1]) 432d45e332Stashuang.zk /// step (%arg4, %arg5) 442d45e332Stashuang.zk /// %inbound = (%j0 * %arg4 + %i0 < %arg2) && 452d45e332Stashuang.zk /// (%j1 * %arg5 + %i1 < %arg3) 462d45e332Stashuang.zk /// scf.if (%inbound) 472d45e332Stashuang.zk /// .... 482d45e332Stashuang.zk /// 491e60678cSStephan Herhut /// where the uses of %i0 and %i1 in the loop body are replaced by 501e60678cSStephan Herhut /// %i0 + j0 and %i1 + %j1. 511e60678cSStephan Herhut // 52c25b20c0SAlex Zinenko /// The old loop is replaced with the new one. 5309c18a66SAlexander Belyaev std::pair<ParallelOp, ParallelOp> 542d45e332Stashuang.zk mlir::scf::tileParallelLoop(ParallelOp op, ArrayRef<int64_t> tileSizes, 552d45e332Stashuang.zk bool noMinMaxBounds) { 56c25b20c0SAlex Zinenko OpBuilder b(op); 57a54f4eaeSMogball auto zero = b.create<arith::ConstantIndexOp>(op.getLoc(), 0); 58c25b20c0SAlex Zinenko SmallVector<Value, 2> tileSizeConstants; 59*c0342a2dSJacques Pienaar tileSizeConstants.reserve(op.getUpperBound().size()); 60*c0342a2dSJacques Pienaar for (size_t i = 0, end = op.getUpperBound().size(); i != end; ++i) { 61c25b20c0SAlex Zinenko if (i < tileSizes.size()) 62c25b20c0SAlex Zinenko tileSizeConstants.push_back( 63a54f4eaeSMogball b.create<arith::ConstantIndexOp>(op.getLoc(), tileSizes[i])); 64c25b20c0SAlex Zinenko else 65c25b20c0SAlex Zinenko // Just pick 1 for the remaining dimensions. 66a54f4eaeSMogball tileSizeConstants.push_back( 67a54f4eaeSMogball b.create<arith::ConstantIndexOp>(op.getLoc(), 1)); 68c25b20c0SAlex Zinenko } 69c25b20c0SAlex Zinenko 70c25b20c0SAlex Zinenko // Create the outer loop with adjusted steps. 71c25b20c0SAlex Zinenko SmallVector<Value, 2> newSteps; 72*c0342a2dSJacques Pienaar newSteps.reserve(op.getStep().size()); 73*c0342a2dSJacques Pienaar for (auto step : llvm::zip(op.getStep(), tileSizeConstants)) { 74a54f4eaeSMogball newSteps.push_back(b.create<arith::MulIOp>(op.getLoc(), std::get<0>(step), 75a54f4eaeSMogball std::get<1>(step))); 76c25b20c0SAlex Zinenko } 77*c0342a2dSJacques Pienaar auto outerLoop = b.create<ParallelOp>(op.getLoc(), op.getLowerBound(), 78*c0342a2dSJacques Pienaar op.getUpperBound(), newSteps); 79c25b20c0SAlex Zinenko b.setInsertionPointToStart(outerLoop.getBody()); 80c25b20c0SAlex Zinenko 81c25b20c0SAlex Zinenko // Compute min(size, dim - offset) to avoid out-of-bounds accesses. 82c25b20c0SAlex Zinenko auto minMap = AffineMap::get( 83c25b20c0SAlex Zinenko /*dimCount=*/3, /*symbolCount=*/0, 84c25b20c0SAlex Zinenko {getAffineDimExpr(/*position=*/0, b.getContext()), 85c25b20c0SAlex Zinenko getAffineDimExpr(/*position=*/1, b.getContext()) - 86c25b20c0SAlex Zinenko getAffineDimExpr(/*position=*/2, b.getContext())}, 87c25b20c0SAlex Zinenko b.getContext()); 88c25b20c0SAlex Zinenko 89c25b20c0SAlex Zinenko // Create the inner loop with adjusted bounds. 90c25b20c0SAlex Zinenko SmallVector<Value, 2> newBounds; 91*c0342a2dSJacques Pienaar newBounds.reserve(op.getUpperBound().size()); 922d45e332Stashuang.zk bool needInboundCheck = false; 93*c0342a2dSJacques Pienaar for (auto dim : 94*c0342a2dSJacques Pienaar llvm::zip(outerLoop.getLowerBound(), outerLoop.getUpperBound(), 95*c0342a2dSJacques Pienaar outerLoop.getStep(), outerLoop.getInductionVars(), 96*c0342a2dSJacques Pienaar op.getStep(), tileSizeConstants)) { 97cd730816STobias Gysi Value lowerBound, upperBound, newStep, iv, step, tileSizeConstant; 98cd730816STobias Gysi std::tie(lowerBound, upperBound, newStep, iv, step, tileSizeConstant) = dim; 99cd730816STobias Gysi // Collect the statically known loop bounds 100cd730816STobias Gysi auto lowerBoundConstant = 101a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(lowerBound.getDefiningOp()); 102cd730816STobias Gysi auto upperBoundConstant = 103a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(upperBound.getDefiningOp()); 104a54f4eaeSMogball auto stepConstant = 105a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(step.getDefiningOp()); 106cd730816STobias Gysi auto tileSize = 107a54f4eaeSMogball cast<arith::ConstantIndexOp>(tileSizeConstant.getDefiningOp()).value(); 108cd730816STobias Gysi // If the loop bounds and the loop step are constant and if the number of 109cd730816STobias Gysi // loop iterations is an integer multiple of the tile size, we use a static 110cd730816STobias Gysi // bound for the inner loop. 111cd730816STobias Gysi if (lowerBoundConstant && upperBoundConstant && stepConstant) { 112a54f4eaeSMogball auto numIterations = llvm::divideCeil(upperBoundConstant.value() - 113a54f4eaeSMogball lowerBoundConstant.value(), 114a54f4eaeSMogball stepConstant.value()); 115cd730816STobias Gysi if (numIterations % tileSize == 0) { 116cd730816STobias Gysi newBounds.push_back(newStep); 117cd730816STobias Gysi continue; 118cd730816STobias Gysi } 119cd730816STobias Gysi } 1202d45e332Stashuang.zk 1212d45e332Stashuang.zk // For InboundCheck mode, just use the variable outer step 1222d45e332Stashuang.zk if (noMinMaxBounds) { 1232d45e332Stashuang.zk newBounds.push_back(newStep); 1242d45e332Stashuang.zk needInboundCheck = true; 1252d45e332Stashuang.zk continue; 1262d45e332Stashuang.zk } 1272d45e332Stashuang.zk 128cd730816STobias Gysi // Otherwise, we dynamically compute the bound for 129cd730816STobias Gysi // each iteration of the outer loop. 130cd730816STobias Gysi newBounds.push_back( 131cd730816STobias Gysi b.create<AffineMinOp>(op.getLoc(), b.getIndexType(), minMap, 132cd730816STobias Gysi ValueRange{newStep, upperBound, iv})); 133c25b20c0SAlex Zinenko } 134c25b20c0SAlex Zinenko auto innerLoop = b.create<ParallelOp>( 135c25b20c0SAlex Zinenko op.getLoc(), SmallVector<Value, 2>(newBounds.size(), zero), newBounds, 136*c0342a2dSJacques Pienaar op.getStep()); 137c25b20c0SAlex Zinenko 1382d45e332Stashuang.zk if (noMinMaxBounds && needInboundCheck) { 1391e60678cSStephan Herhut b.setInsertionPointToStart(innerLoop.getBody()); 1402d45e332Stashuang.zk // Insert in-bound check 1412d45e332Stashuang.zk Value inbound = 142a54f4eaeSMogball b.create<arith::ConstantIntOp>(op.getLoc(), 1, b.getIntegerType(1)); 1432d45e332Stashuang.zk for (auto dim : 144*c0342a2dSJacques Pienaar llvm::zip(outerLoop.getUpperBound(), outerLoop.getInductionVars(), 145*c0342a2dSJacques Pienaar innerLoop.getInductionVars(), innerLoop.getStep())) { 1462d45e332Stashuang.zk Value outerUpperBound, outerIV, innerIV, innerStep; 1472d45e332Stashuang.zk std::tie(outerUpperBound, outerIV, innerIV, innerStep) = dim; 1482d45e332Stashuang.zk // %in_bound = %in_bound && 1492d45e332Stashuang.zk // (%inner_iv * %inner_step + %outer_iv < %outer_upper_bound) 150a54f4eaeSMogball Value index = b.create<arith::AddIOp>( 151a54f4eaeSMogball op.getLoc(), b.create<arith::MulIOp>(op.getLoc(), innerIV, innerStep), 1522d45e332Stashuang.zk outerIV); 153a54f4eaeSMogball Value dimInbound = b.create<arith::CmpIOp>( 154a54f4eaeSMogball op.getLoc(), arith::CmpIPredicate::ult, index, outerUpperBound); 155a54f4eaeSMogball inbound = b.create<arith::AndIOp>(op.getLoc(), inbound, dimInbound); 1562d45e332Stashuang.zk } 1572d45e332Stashuang.zk auto ifInbound = b.create<IfOp>(op.getLoc(), 1582d45e332Stashuang.zk /*resultTypes*/ ArrayRef<Type>{}, inbound, 1592d45e332Stashuang.zk /*hasElseRegion*/ false); 160*c0342a2dSJacques Pienaar ifInbound.getThenRegion().takeBody(op.getRegion()); 161*c0342a2dSJacques Pienaar Block &thenBlock = ifInbound.getThenRegion().front(); 1622d45e332Stashuang.zk b.setInsertionPointToStart(innerLoop.getBody()); 1632d45e332Stashuang.zk for (auto ivs : llvm::enumerate(llvm::zip(innerLoop.getInductionVars(), 1642d45e332Stashuang.zk outerLoop.getInductionVars()))) { 165a54f4eaeSMogball auto newIndex = b.create<arith::AddIOp>( 166a54f4eaeSMogball op.getLoc(), std::get<0>(ivs.value()), std::get<1>(ivs.value())); 1672d45e332Stashuang.zk thenBlock.getArgument(ivs.index()) 1682d45e332Stashuang.zk .replaceAllUsesExcept(newIndex, newIndex); 1692d45e332Stashuang.zk } 1702d45e332Stashuang.zk thenBlock.eraseArguments(llvm::to_vector<4>( 1712d45e332Stashuang.zk llvm::seq((unsigned)0, thenBlock.getNumArguments()))); 1722d45e332Stashuang.zk } else { 173*c0342a2dSJacques Pienaar innerLoop.getRegion().takeBody(op.getRegion()); 1742d45e332Stashuang.zk b.setInsertionPointToStart(innerLoop.getBody()); 1752d45e332Stashuang.zk for (auto ivs : llvm::zip(innerLoop.getInductionVars(), 1762d45e332Stashuang.zk outerLoop.getInductionVars())) { 1772d45e332Stashuang.zk Value innerIndex = std::get<0>(ivs); 178a54f4eaeSMogball auto newIndex = b.create<arith::AddIOp>(op.getLoc(), std::get<0>(ivs), 179a54f4eaeSMogball std::get<1>(ivs)); 1802d45e332Stashuang.zk innerIndex.replaceAllUsesExcept(newIndex, newIndex); 1812d45e332Stashuang.zk } 1821e60678cSStephan Herhut } 1831e60678cSStephan Herhut 184c25b20c0SAlex Zinenko op.erase(); 18509c18a66SAlexander Belyaev return std::make_pair(outerLoop, innerLoop); 186c25b20c0SAlex Zinenko } 187c25b20c0SAlex Zinenko 188c25b20c0SAlex Zinenko namespace { 189c25b20c0SAlex Zinenko struct ParallelLoopTiling 1904bcd08ebSStephan Herhut : public SCFParallelLoopTilingBase<ParallelLoopTiling> { 191c25b20c0SAlex Zinenko ParallelLoopTiling() = default; 1922d45e332Stashuang.zk explicit ParallelLoopTiling(ArrayRef<int64_t> tileSizes, 1932d45e332Stashuang.zk bool noMinMaxBounds = false) { 194c25b20c0SAlex Zinenko this->tileSizes = tileSizes; 1952d45e332Stashuang.zk this->noMinMaxBounds = noMinMaxBounds; 196c25b20c0SAlex Zinenko } 197c25b20c0SAlex Zinenko 198c25b20c0SAlex Zinenko void runOnFunction() override { 1996484567fSFrederik Gossen SmallVector<ParallelOp, 2> innermostPloops; 20080966447SAlexander Belyaev getInnermostParallelLoops(getFunction().getOperation(), 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> 2112d45e332Stashuang.zk mlir::createParallelLoopTilingPass(ArrayRef<int64_t> tileSizes, 2122d45e332Stashuang.zk bool noMinMaxBounds) { 2132d45e332Stashuang.zk return std::make_unique<ParallelLoopTiling>(tileSizes, noMinMaxBounds); 214c25b20c0SAlex Zinenko } 215