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" 15*a54f4eaeSMogball #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); 57*a54f4eaeSMogball auto zero = b.create<arith::ConstantIndexOp>(op.getLoc(), 0); 58c25b20c0SAlex Zinenko SmallVector<Value, 2> tileSizeConstants; 59c25b20c0SAlex Zinenko tileSizeConstants.reserve(op.upperBound().size()); 60c25b20c0SAlex Zinenko for (size_t i = 0, end = op.upperBound().size(); i != end; ++i) { 61c25b20c0SAlex Zinenko if (i < tileSizes.size()) 62c25b20c0SAlex Zinenko tileSizeConstants.push_back( 63*a54f4eaeSMogball b.create<arith::ConstantIndexOp>(op.getLoc(), tileSizes[i])); 64c25b20c0SAlex Zinenko else 65c25b20c0SAlex Zinenko // Just pick 1 for the remaining dimensions. 66*a54f4eaeSMogball tileSizeConstants.push_back( 67*a54f4eaeSMogball 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; 72c25b20c0SAlex Zinenko newSteps.reserve(op.step().size()); 73c25b20c0SAlex Zinenko for (auto step : llvm::zip(op.step(), tileSizeConstants)) { 74*a54f4eaeSMogball newSteps.push_back(b.create<arith::MulIOp>(op.getLoc(), std::get<0>(step), 75*a54f4eaeSMogball std::get<1>(step))); 76c25b20c0SAlex Zinenko } 77c25b20c0SAlex Zinenko auto outerLoop = b.create<ParallelOp>(op.getLoc(), op.lowerBound(), 78c25b20c0SAlex Zinenko op.upperBound(), 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; 91c25b20c0SAlex Zinenko newBounds.reserve(op.upperBound().size()); 922d45e332Stashuang.zk bool needInboundCheck = false; 93cd730816STobias Gysi for (auto dim : llvm::zip(outerLoop.lowerBound(), outerLoop.upperBound(), 94cd730816STobias Gysi outerLoop.step(), outerLoop.getInductionVars(), 95cd730816STobias Gysi op.step(), 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 = 100*a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(lowerBound.getDefiningOp()); 101cd730816STobias Gysi auto upperBoundConstant = 102*a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(upperBound.getDefiningOp()); 103*a54f4eaeSMogball auto stepConstant = 104*a54f4eaeSMogball dyn_cast_or_null<arith::ConstantIndexOp>(step.getDefiningOp()); 105cd730816STobias Gysi auto tileSize = 106*a54f4eaeSMogball 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) { 111*a54f4eaeSMogball auto numIterations = llvm::divideCeil(upperBoundConstant.value() - 112*a54f4eaeSMogball lowerBoundConstant.value(), 113*a54f4eaeSMogball 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, 135c25b20c0SAlex Zinenko op.step()); 136c25b20c0SAlex Zinenko 1372d45e332Stashuang.zk if (noMinMaxBounds && needInboundCheck) { 1381e60678cSStephan Herhut b.setInsertionPointToStart(innerLoop.getBody()); 1392d45e332Stashuang.zk // Insert in-bound check 1402d45e332Stashuang.zk Value inbound = 141*a54f4eaeSMogball b.create<arith::ConstantIntOp>(op.getLoc(), 1, b.getIntegerType(1)); 1422d45e332Stashuang.zk for (auto dim : 1432d45e332Stashuang.zk llvm::zip(outerLoop.upperBound(), outerLoop.getInductionVars(), 1442d45e332Stashuang.zk innerLoop.getInductionVars(), innerLoop.step())) { 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) 149*a54f4eaeSMogball Value index = b.create<arith::AddIOp>( 150*a54f4eaeSMogball op.getLoc(), b.create<arith::MulIOp>(op.getLoc(), innerIV, innerStep), 1512d45e332Stashuang.zk outerIV); 152*a54f4eaeSMogball Value dimInbound = b.create<arith::CmpIOp>( 153*a54f4eaeSMogball op.getLoc(), arith::CmpIPredicate::ult, index, outerUpperBound); 154*a54f4eaeSMogball 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); 1592d45e332Stashuang.zk ifInbound.thenRegion().takeBody(op.region()); 1602d45e332Stashuang.zk Block &thenBlock = ifInbound.thenRegion().front(); 1612d45e332Stashuang.zk b.setInsertionPointToStart(innerLoop.getBody()); 1622d45e332Stashuang.zk for (auto ivs : llvm::enumerate(llvm::zip(innerLoop.getInductionVars(), 1632d45e332Stashuang.zk outerLoop.getInductionVars()))) { 164*a54f4eaeSMogball auto newIndex = b.create<arith::AddIOp>( 165*a54f4eaeSMogball 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 { 1722d45e332Stashuang.zk innerLoop.region().takeBody(op.region()); 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); 177*a54f4eaeSMogball auto newIndex = b.create<arith::AddIOp>(op.getLoc(), std::get<0>(ivs), 178*a54f4eaeSMogball 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; 1912d45e332Stashuang.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 197c25b20c0SAlex Zinenko void runOnFunction() override { 1986484567fSFrederik Gossen SmallVector<ParallelOp, 2> innermostPloops; 19980966447SAlexander Belyaev getInnermostParallelLoops(getFunction().getOperation(), innermostPloops); 2006484567fSFrederik Gossen for (ParallelOp ploop : innermostPloops) { 201cd730816STobias Gysi // FIXME: Add reduction support. 2026484567fSFrederik Gossen if (ploop.getNumReductions() == 0) 2032d45e332Stashuang.zk tileParallelLoop(ploop, tileSizes, noMinMaxBounds); 204c25b20c0SAlex Zinenko } 205c25b20c0SAlex Zinenko } 206c25b20c0SAlex Zinenko }; 207c25b20c0SAlex Zinenko } // namespace 208c25b20c0SAlex Zinenko 209c25b20c0SAlex Zinenko std::unique_ptr<Pass> 2102d45e332Stashuang.zk mlir::createParallelLoopTilingPass(ArrayRef<int64_t> tileSizes, 2112d45e332Stashuang.zk bool noMinMaxBounds) { 2122d45e332Stashuang.zk return std::make_unique<ParallelLoopTiling>(tileSizes, noMinMaxBounds); 213c25b20c0SAlex Zinenko } 214