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]) 31*cd730816STobias Gysi /// scf.parallel (%j0, %j1) = (0, 0) to (min(%arg4*tileSize[0], %arg2-%i0) 32*cd730816STobias 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()); 77*cd730816STobias Gysi for (auto dim : llvm::zip(outerLoop.lowerBound(), outerLoop.upperBound(), 78*cd730816STobias Gysi outerLoop.step(), outerLoop.getInductionVars(), 79*cd730816STobias Gysi op.step(), tileSizeConstants)) { 80*cd730816STobias Gysi Value lowerBound, upperBound, newStep, iv, step, tileSizeConstant; 81*cd730816STobias Gysi std::tie(lowerBound, upperBound, newStep, iv, step, tileSizeConstant) = dim; 82*cd730816STobias Gysi // Collect the statically known loop bounds 83*cd730816STobias Gysi auto lowerBoundConstant = 84*cd730816STobias Gysi dyn_cast_or_null<ConstantIndexOp>(lowerBound.getDefiningOp()); 85*cd730816STobias Gysi auto upperBoundConstant = 86*cd730816STobias Gysi dyn_cast_or_null<ConstantIndexOp>(upperBound.getDefiningOp()); 87*cd730816STobias Gysi auto stepConstant = dyn_cast_or_null<ConstantIndexOp>(step.getDefiningOp()); 88*cd730816STobias Gysi auto tileSize = 89*cd730816STobias Gysi cast<ConstantIndexOp>(tileSizeConstant.getDefiningOp()).getValue(); 90*cd730816STobias Gysi // If the loop bounds and the loop step are constant and if the number of 91*cd730816STobias Gysi // loop iterations is an integer multiple of the tile size, we use a static 92*cd730816STobias Gysi // bound for the inner loop. 93*cd730816STobias Gysi if (lowerBoundConstant && upperBoundConstant && stepConstant) { 94*cd730816STobias Gysi auto numIterations = llvm::divideCeil(upperBoundConstant.getValue() - 95*cd730816STobias Gysi lowerBoundConstant.getValue(), 96*cd730816STobias Gysi stepConstant.getValue()); 97*cd730816STobias Gysi if (numIterations % tileSize == 0) { 98*cd730816STobias Gysi newBounds.push_back(newStep); 99*cd730816STobias Gysi continue; 100*cd730816STobias Gysi } 101*cd730816STobias Gysi } 102*cd730816STobias Gysi // Otherwise, we dynamically compute the bound for 103*cd730816STobias Gysi // each iteration of the outer loop. 104*cd730816STobias Gysi newBounds.push_back( 105*cd730816STobias Gysi b.create<AffineMinOp>(op.getLoc(), b.getIndexType(), minMap, 106*cd730816STobias 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*cd730816STobias Gysi /// Get a list of most nested parallel loops. Assumes that ParallelOps are 130*cd730816STobias Gysi /// only directly nested. 131c25b20c0SAlex Zinenko static bool getInnermostNestedLoops(Block *block, 132c25b20c0SAlex Zinenko SmallVectorImpl<ParallelOp> &loops) { 133c25b20c0SAlex Zinenko bool hasInnerLoop = false; 134c25b20c0SAlex Zinenko for (auto parallelOp : block->getOps<ParallelOp>()) { 135c25b20c0SAlex Zinenko hasInnerLoop = true; 136c25b20c0SAlex Zinenko if (!getInnermostNestedLoops(parallelOp.getBody(), loops)) 137c25b20c0SAlex Zinenko loops.push_back(parallelOp); 138c25b20c0SAlex Zinenko } 139c25b20c0SAlex Zinenko return hasInnerLoop; 140c25b20c0SAlex Zinenko } 141c25b20c0SAlex Zinenko 142c25b20c0SAlex Zinenko namespace { 143c25b20c0SAlex Zinenko struct ParallelLoopTiling 1444bcd08ebSStephan Herhut : public SCFParallelLoopTilingBase<ParallelLoopTiling> { 145c25b20c0SAlex Zinenko ParallelLoopTiling() = default; 146c25b20c0SAlex Zinenko explicit ParallelLoopTiling(ArrayRef<int64_t> tileSizes) { 147c25b20c0SAlex Zinenko this->tileSizes = tileSizes; 148c25b20c0SAlex Zinenko } 149c25b20c0SAlex Zinenko 150c25b20c0SAlex Zinenko void runOnFunction() override { 151c25b20c0SAlex Zinenko SmallVector<ParallelOp, 2> mostNestedParallelOps; 152c25b20c0SAlex Zinenko for (Block &block : getFunction()) { 153c25b20c0SAlex Zinenko getInnermostNestedLoops(&block, mostNestedParallelOps); 154c25b20c0SAlex Zinenko } 155c25b20c0SAlex Zinenko for (ParallelOp pLoop : mostNestedParallelOps) { 156*cd730816STobias Gysi // FIXME: Add reduction support. 157*cd730816STobias Gysi if (pLoop.getNumReductions() == 0) 158c25b20c0SAlex Zinenko tileParallelLoop(pLoop, tileSizes); 159c25b20c0SAlex Zinenko } 160c25b20c0SAlex Zinenko } 161c25b20c0SAlex Zinenko }; 162c25b20c0SAlex Zinenko } // namespace 163c25b20c0SAlex Zinenko 164c25b20c0SAlex Zinenko std::unique_ptr<Pass> 165c25b20c0SAlex Zinenko mlir::createParallelLoopTilingPass(ArrayRef<int64_t> tileSizes) { 166c25b20c0SAlex Zinenko return std::make_unique<ParallelLoopTiling>(tileSizes); 167c25b20c0SAlex Zinenko } 168