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" 18c25b20c0SAlex Zinenko #include "mlir/Dialect/StandardOps/IR/Ops.h" 19c25b20c0SAlex Zinenko 20c25b20c0SAlex Zinenko using namespace mlir; 21c25b20c0SAlex Zinenko using namespace mlir::scf; 22c25b20c0SAlex Zinenko 23c25b20c0SAlex Zinenko /// Tile a parallel loop of the form 2460f443bbSAlex Zinenko /// scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3) 25c25b20c0SAlex Zinenko /// step (%arg4, %arg5) 26c25b20c0SAlex Zinenko /// 27c25b20c0SAlex Zinenko /// into 2860f443bbSAlex Zinenko /// scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3) 29c25b20c0SAlex Zinenko /// step (%arg4*tileSize[0], 30c25b20c0SAlex Zinenko /// %arg5*tileSize[1]) 31cd730816STobias Gysi /// scf.parallel (%j0, %j1) = (0, 0) to (min(%arg4*tileSize[0], %arg2-%i0) 32cd730816STobias Gysi /// min(%arg5*tileSize[1], %arg3-%i1)) 33c25b20c0SAlex Zinenko /// step (%arg4, %arg5) 341e60678cSStephan Herhut /// 351e60678cSStephan Herhut /// where the uses of %i0 and %i1 in the loop body are replaced by 361e60678cSStephan Herhut /// %i0 + j0 and %i1 + %j1. 371e60678cSStephan Herhut // 38c25b20c0SAlex Zinenko /// The old loop is replaced with the new one. 39c25b20c0SAlex Zinenko void mlir::scf::tileParallelLoop(ParallelOp op, ArrayRef<int64_t> tileSizes) { 40c25b20c0SAlex Zinenko OpBuilder b(op); 41c25b20c0SAlex Zinenko auto zero = b.create<ConstantIndexOp>(op.getLoc(), 0); 42c25b20c0SAlex Zinenko SmallVector<Value, 2> tileSizeConstants; 43c25b20c0SAlex Zinenko tileSizeConstants.reserve(op.upperBound().size()); 44c25b20c0SAlex Zinenko for (size_t i = 0, end = op.upperBound().size(); i != end; ++i) { 45c25b20c0SAlex Zinenko if (i < tileSizes.size()) 46c25b20c0SAlex Zinenko tileSizeConstants.push_back( 47c25b20c0SAlex Zinenko b.create<ConstantIndexOp>(op.getLoc(), tileSizes[i])); 48c25b20c0SAlex Zinenko else 49c25b20c0SAlex Zinenko // Just pick 1 for the remaining dimensions. 50c25b20c0SAlex Zinenko tileSizeConstants.push_back(b.create<ConstantIndexOp>(op.getLoc(), 1)); 51c25b20c0SAlex Zinenko } 52c25b20c0SAlex Zinenko 53c25b20c0SAlex Zinenko // Create the outer loop with adjusted steps. 54c25b20c0SAlex Zinenko SmallVector<Value, 2> newSteps; 55c25b20c0SAlex Zinenko newSteps.reserve(op.step().size()); 56c25b20c0SAlex Zinenko for (auto step : llvm::zip(op.step(), tileSizeConstants)) { 57c25b20c0SAlex Zinenko newSteps.push_back( 58c25b20c0SAlex Zinenko b.create<MulIOp>(op.getLoc(), std::get<0>(step), std::get<1>(step))); 59c25b20c0SAlex Zinenko } 60c25b20c0SAlex Zinenko auto outerLoop = b.create<ParallelOp>(op.getLoc(), op.lowerBound(), 61c25b20c0SAlex Zinenko op.upperBound(), newSteps); 62c25b20c0SAlex Zinenko b.setInsertionPointToStart(outerLoop.getBody()); 63c25b20c0SAlex Zinenko 64c25b20c0SAlex Zinenko // Compute min(size, dim - offset) to avoid out-of-bounds accesses. 65c25b20c0SAlex Zinenko // FIXME: Instead of using min, we want to replicate the tail. This would give 66c25b20c0SAlex Zinenko // the inner loop constant bounds for easy vectorization. 67c25b20c0SAlex Zinenko auto minMap = AffineMap::get( 68c25b20c0SAlex Zinenko /*dimCount=*/3, /*symbolCount=*/0, 69c25b20c0SAlex Zinenko {getAffineDimExpr(/*position=*/0, b.getContext()), 70c25b20c0SAlex Zinenko getAffineDimExpr(/*position=*/1, b.getContext()) - 71c25b20c0SAlex Zinenko getAffineDimExpr(/*position=*/2, b.getContext())}, 72c25b20c0SAlex Zinenko b.getContext()); 73c25b20c0SAlex Zinenko 74c25b20c0SAlex Zinenko // Create the inner loop with adjusted bounds. 75c25b20c0SAlex Zinenko SmallVector<Value, 2> newBounds; 76c25b20c0SAlex Zinenko newBounds.reserve(op.upperBound().size()); 77cd730816STobias Gysi for (auto dim : llvm::zip(outerLoop.lowerBound(), outerLoop.upperBound(), 78cd730816STobias Gysi outerLoop.step(), outerLoop.getInductionVars(), 79cd730816STobias Gysi op.step(), tileSizeConstants)) { 80cd730816STobias Gysi Value lowerBound, upperBound, newStep, iv, step, tileSizeConstant; 81cd730816STobias Gysi std::tie(lowerBound, upperBound, newStep, iv, step, tileSizeConstant) = dim; 82cd730816STobias Gysi // Collect the statically known loop bounds 83cd730816STobias Gysi auto lowerBoundConstant = 84cd730816STobias Gysi dyn_cast_or_null<ConstantIndexOp>(lowerBound.getDefiningOp()); 85cd730816STobias Gysi auto upperBoundConstant = 86cd730816STobias Gysi dyn_cast_or_null<ConstantIndexOp>(upperBound.getDefiningOp()); 87cd730816STobias Gysi auto stepConstant = dyn_cast_or_null<ConstantIndexOp>(step.getDefiningOp()); 88cd730816STobias Gysi auto tileSize = 89cd730816STobias Gysi cast<ConstantIndexOp>(tileSizeConstant.getDefiningOp()).getValue(); 90cd730816STobias Gysi // If the loop bounds and the loop step are constant and if the number of 91cd730816STobias Gysi // loop iterations is an integer multiple of the tile size, we use a static 92cd730816STobias Gysi // bound for the inner loop. 93cd730816STobias Gysi if (lowerBoundConstant && upperBoundConstant && stepConstant) { 94cd730816STobias Gysi auto numIterations = llvm::divideCeil(upperBoundConstant.getValue() - 95cd730816STobias Gysi lowerBoundConstant.getValue(), 96cd730816STobias Gysi stepConstant.getValue()); 97cd730816STobias Gysi if (numIterations % tileSize == 0) { 98cd730816STobias Gysi newBounds.push_back(newStep); 99cd730816STobias Gysi continue; 100cd730816STobias Gysi } 101cd730816STobias Gysi } 102cd730816STobias Gysi // Otherwise, we dynamically compute the bound for 103cd730816STobias Gysi // each iteration of the outer loop. 104cd730816STobias Gysi newBounds.push_back( 105cd730816STobias Gysi b.create<AffineMinOp>(op.getLoc(), b.getIndexType(), minMap, 106cd730816STobias Gysi ValueRange{newStep, upperBound, iv})); 107c25b20c0SAlex Zinenko } 108c25b20c0SAlex Zinenko auto innerLoop = b.create<ParallelOp>( 109c25b20c0SAlex Zinenko op.getLoc(), SmallVector<Value, 2>(newBounds.size(), zero), newBounds, 110c25b20c0SAlex Zinenko op.step()); 111c25b20c0SAlex Zinenko 112c25b20c0SAlex Zinenko // Steal the body of the old parallel loop and erase it. 113c25b20c0SAlex Zinenko innerLoop.region().takeBody(op.region()); 1141e60678cSStephan Herhut 1151e60678cSStephan Herhut // Insert computation for new index vectors and replace uses. 1161e60678cSStephan Herhut b.setInsertionPointToStart(innerLoop.getBody()); 1171e60678cSStephan Herhut for (auto ivs : 1181e60678cSStephan Herhut llvm::zip(innerLoop.getInductionVars(), outerLoop.getInductionVars())) { 1191e60678cSStephan Herhut Value inner_index = std::get<0>(ivs); 1201e60678cSStephan Herhut AddIOp newIndex = 1211e60678cSStephan Herhut b.create<AddIOp>(op.getLoc(), std::get<0>(ivs), std::get<1>(ivs)); 1221e60678cSStephan Herhut inner_index.replaceAllUsesExcept( 1231e60678cSStephan Herhut newIndex, SmallPtrSet<Operation *, 1>{newIndex.getOperation()}); 1241e60678cSStephan Herhut } 1251e60678cSStephan Herhut 126c25b20c0SAlex Zinenko op.erase(); 127c25b20c0SAlex Zinenko } 128c25b20c0SAlex Zinenko 129*6484567fSFrederik Gossen /// Get a list of most nested parallel loops. 130*6484567fSFrederik Gossen static bool getInnermostPloops(Operation *rootOp, 131*6484567fSFrederik Gossen SmallVectorImpl<ParallelOp> &result) { 132*6484567fSFrederik Gossen assert(rootOp != nullptr && "Root operation must not be a nullptr."); 133*6484567fSFrederik Gossen bool rootEnclosesPloops = false; 134*6484567fSFrederik Gossen for (Region ®ion : rootOp->getRegions()) { 135*6484567fSFrederik Gossen for (Block &block : region.getBlocks()) { 136*6484567fSFrederik Gossen for (Operation &op : block) { 137*6484567fSFrederik Gossen bool enclosesPloops = getInnermostPloops(&op, result); 138*6484567fSFrederik Gossen rootEnclosesPloops |= enclosesPloops; 139*6484567fSFrederik Gossen if (auto ploop = dyn_cast<ParallelOp>(op)) { 140*6484567fSFrederik Gossen rootEnclosesPloops = true; 141*6484567fSFrederik Gossen 142*6484567fSFrederik Gossen // Collect ploop if it is an innermost one. 143*6484567fSFrederik Gossen if (!enclosesPloops) 144*6484567fSFrederik Gossen result.push_back(ploop); 145c25b20c0SAlex Zinenko } 146*6484567fSFrederik Gossen } 147*6484567fSFrederik Gossen } 148*6484567fSFrederik Gossen } 149*6484567fSFrederik Gossen return rootEnclosesPloops; 150c25b20c0SAlex Zinenko } 151c25b20c0SAlex Zinenko 152c25b20c0SAlex Zinenko namespace { 153c25b20c0SAlex Zinenko struct ParallelLoopTiling 1544bcd08ebSStephan Herhut : public SCFParallelLoopTilingBase<ParallelLoopTiling> { 155c25b20c0SAlex Zinenko ParallelLoopTiling() = default; 156c25b20c0SAlex Zinenko explicit ParallelLoopTiling(ArrayRef<int64_t> tileSizes) { 157c25b20c0SAlex Zinenko this->tileSizes = tileSizes; 158c25b20c0SAlex Zinenko } 159c25b20c0SAlex Zinenko 160c25b20c0SAlex Zinenko void runOnFunction() override { 161*6484567fSFrederik Gossen SmallVector<ParallelOp, 2> innermostPloops; 162*6484567fSFrederik Gossen getInnermostPloops(getFunction().getOperation(), innermostPloops); 163*6484567fSFrederik Gossen for (ParallelOp ploop : innermostPloops) { 164cd730816STobias Gysi // FIXME: Add reduction support. 165*6484567fSFrederik Gossen if (ploop.getNumReductions() == 0) 166*6484567fSFrederik Gossen tileParallelLoop(ploop, tileSizes); 167c25b20c0SAlex Zinenko } 168c25b20c0SAlex Zinenko } 169c25b20c0SAlex Zinenko }; 170c25b20c0SAlex Zinenko } // namespace 171c25b20c0SAlex Zinenko 172c25b20c0SAlex Zinenko std::unique_ptr<Pass> 173c25b20c0SAlex Zinenko mlir::createParallelLoopTilingPass(ArrayRef<int64_t> tileSizes) { 174c25b20c0SAlex Zinenko return std::make_unique<ParallelLoopTiling>(tileSizes); 175c25b20c0SAlex Zinenko } 176