|
| 1 | +// Copyright 2025 The IREE Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | + |
| 7 | +#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenAttrs.h" |
| 8 | +#include "iree/compiler/Codegen/LLVMCPU/Passes.h" |
| 9 | +#include "iree/compiler/Codegen/LLVMCPU/Utils.h" |
| 10 | +#include "llvm/Support/DebugLog.h" |
| 11 | +#include "llvm/Support/InterleavedRange.h" |
| 12 | +#include "llvm/Support/LogicalResult.h" |
| 13 | +#include "mlir/Dialect/Linalg/Transforms/Transforms.h" |
| 14 | +#include "mlir/Dialect/MemRef/Transforms/Transforms.h" |
| 15 | +#include "mlir/Dialect/SCF/IR/SCF.h" |
| 16 | +#include "mlir/Dialect/SCF/Transforms/Patterns.h" |
| 17 | +#include "mlir/Dialect/SCF/Transforms/TileUsingInterface.h" |
| 18 | +#include "mlir/Dialect/SCF/Transforms/Transforms.h" |
| 19 | +#include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 20 | +#include "mlir/IR/BuiltinTypeInterfaces.h" |
| 21 | +#include "mlir/IR/Iterators.h" |
| 22 | +#include "mlir/Interfaces/TilingInterface.h" |
| 23 | +#include "mlir/Pass/Pass.h" |
| 24 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 25 | + |
| 26 | +#define DEBUG_TYPE "iree-llvmcpu-tile-to-vector-size" |
| 27 | + |
| 28 | +namespace mlir::iree_compiler { |
| 29 | + |
| 30 | +#define GEN_PASS_DEF_LLVMCPUTILETOVECTORSIZEPASS |
| 31 | +#include "iree/compiler/Codegen/LLVMCPU/Passes.h.inc" |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +struct LLVMCPUTileToVectorSizePass final |
| 36 | + : impl::LLVMCPUTileToVectorSizePassBase<LLVMCPUTileToVectorSizePass> { |
| 37 | + using impl::LLVMCPUTileToVectorSizePassBase< |
| 38 | + LLVMCPUTileToVectorSizePass>::LLVMCPUTileToVectorSizePassBase; |
| 39 | + |
| 40 | + void getDependentDialects(DialectRegistry ®istry) const override { |
| 41 | + registry.insert<arith::ArithDialect, scf::SCFDialect>(); |
| 42 | + } |
| 43 | + |
| 44 | + void runOnOperation() override; |
| 45 | +}; |
| 46 | + |
| 47 | +static std::optional<SmallVector<int64_t>> |
| 48 | +getTileSizesForEachDims(linalg::LinalgOp op) { |
| 49 | + IREE::Codegen::LoweringConfigAttrInterface loweringConfig = |
| 50 | + getLoweringConfig(op); |
| 51 | + SmallVector<bool> scalableFlags = loweringConfig.getVectorScalableFlags(); |
| 52 | + if (llvm::count(scalableFlags, true) > 0) { |
| 53 | + return std::nullopt; |
| 54 | + } |
| 55 | + |
| 56 | + unsigned numLoops = op.getNumLoops(); |
| 57 | + std::optional<SmallVector<int64_t>> vectorSizes = |
| 58 | + loweringConfig.getVectorSizes(); |
| 59 | + if (!vectorSizes || vectorSizes->size() != numLoops) { |
| 60 | + return std::nullopt; |
| 61 | + } |
| 62 | + LDBG() << "configured vector sizes: " |
| 63 | + << llvm::interleaved_array(vectorSizes.value()); |
| 64 | + |
| 65 | + SmallVector<int64_t> result(numLoops, 0); |
| 66 | + for (unsigned dim = 0; dim < numLoops; ++dim) { |
| 67 | + SmallVector<std::pair<Value, unsigned>> operandDimPairs; |
| 68 | + op.mapIterationSpaceDimToAllOperandDims(dim, operandDimPairs); |
| 69 | + if (operandDimPairs.empty()) { |
| 70 | + return std::nullopt; |
| 71 | + } |
| 72 | + |
| 73 | + Value firstOperand = operandDimPairs[0].first; |
| 74 | + unsigned firstOperandDim = operandDimPairs[0].second; |
| 75 | + |
| 76 | + // Trivial case: `dim` size is available in the operand type. |
| 77 | + int64_t dimSize = llvm::cast<ShapedType>(firstOperand.getType()) |
| 78 | + .getShape()[firstOperandDim]; |
| 79 | + int64_t vectorDimSize = vectorSizes.value()[dim]; |
| 80 | + if (ShapedType::isStatic(dimSize) && dimSize > vectorDimSize) { |
| 81 | + LDBG() << "set dim #" << dim << " size (" << dimSize |
| 82 | + << ") with vector size: " << vectorDimSize; |
| 83 | + result[dim] = vectorDimSize; |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // If a `tensor.extract_slice` op can not be found, the operand is not tiled |
| 88 | + // at all. It implies that the dimension is not yet tiled. `tensor.empty` is |
| 89 | + // part of tiling artifacts that can be used to infer tiling sizes. |
| 90 | + if (!isa_and_present<tensor::EmptyOp, tensor::ExtractSliceOp>( |
| 91 | + firstOperand.getDefiningOp())) { |
| 92 | + LDBG() << "set dim #" << dim |
| 93 | + << " size (untiled) with vector size: " << vectorDimSize; |
| 94 | + result[dim] = vectorDimSize; |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + // Use ValueBounds analysis to infer `dim` size upper bound. |
| 99 | + std::optional<int64_t> maybeDimSize; |
| 100 | + FailureOr<DimBoundSize> maybeDimBound; |
| 101 | + for (auto [operand, operandDim] : operandDimPairs) { |
| 102 | + FailureOr<int64_t> maybeDimBoundSize = |
| 103 | + ValueBoundsConstraintSet::computeConstantBound( |
| 104 | + presburger::BoundType::UB, {operand, operandDim}, |
| 105 | + /*stopCondition=*/nullptr, /*closedUB=*/true); |
| 106 | + if (succeeded(maybeDimBoundSize)) { |
| 107 | + maybeDimSize = maybeDimBoundSize.value(); |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + // Assume that the unknown dimension size implies the dimension is already |
| 112 | + // tiled. It means that the dimension is definitely tiled, but it is hard to |
| 113 | + // infer the tile size. It usually happens in fusion case, so the pass |
| 114 | + // assumes that it is not needed. |
| 115 | + if (maybeDimSize && maybeDimSize.value() > vectorDimSize) { |
| 116 | + LDBG() << "set dim #" << dim << " size (" << maybeDimSize.value() |
| 117 | + << ") with vector size: " << vectorDimSize; |
| 118 | + result[dim] = vectorDimSize; |
| 119 | + } else { |
| 120 | + LDBG() << "dim #" << dim << " either is tiled to vector size (" |
| 121 | + << vectorDimSize << ") or has complex size computation"; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return result; |
| 126 | +} |
| 127 | + |
| 128 | +void LLVMCPUTileToVectorSizePass::runOnOperation() { |
| 129 | + MLIRContext *context = &getContext(); |
| 130 | + FunctionOpInterface funcOp = getOperation(); |
| 131 | + SmallVector<linalg::LinalgOp> candidates; |
| 132 | + funcOp.walk([&](linalg::LinalgOp op) { |
| 133 | + // XXX(hanchung): linalg.fill usually follow the reduction consumer ops, so |
| 134 | + // the additional tiling is not needed. Otherwise, it results in an |
| 135 | + // additional loops before converting it to a vector. We may need to fix the |
| 136 | + // lowering config issue, but it is a fair stopgap in practice. |
| 137 | + if (isa<linalg::FillOp>(op)) { |
| 138 | + return; |
| 139 | + } |
| 140 | + IREE::Codegen::LoweringConfigAttrInterface loweringConfig = |
| 141 | + getLoweringConfig(op); |
| 142 | + if (!loweringConfig) { |
| 143 | + return; |
| 144 | + } |
| 145 | + if (!loweringConfig.getVectorSizes().has_value()) { |
| 146 | + return; |
| 147 | + } |
| 148 | + candidates.push_back(op); |
| 149 | + }); |
| 150 | + |
| 151 | + IRRewriter rewriter(context); |
| 152 | + for (linalg::LinalgOp op : candidates) { |
| 153 | + LDBG() << "candidate: " << op; |
| 154 | + std::optional<SmallVector<int64_t>> tileSizes = getTileSizesForEachDims(op); |
| 155 | + if (!tileSizes) { |
| 156 | + LDBG() << "all the dimensions are either tiled or target scalable tile " |
| 157 | + "sizes"; |
| 158 | + continue; |
| 159 | + } |
| 160 | + if (llvm::all_of(tileSizes.value(), [](int64_t val) { return val == 0; })) { |
| 161 | + LDBG() << "skip the op because tile sizes are all zeros"; |
| 162 | + continue; |
| 163 | + } |
| 164 | + LDBG() << "tileSizes: " << llvm::interleaved_array(tileSizes.value()); |
| 165 | + |
| 166 | + auto tilingInterfaceOp = cast<TilingInterface>(op.getOperation()); |
| 167 | + scf::SCFTilingOptions options; |
| 168 | + setSCFTileSizes(options, tilingInterfaceOp, std::move(tileSizes.value()), |
| 169 | + /*tileScalableFlags=*/{}); |
| 170 | + FailureOr<scf::SCFTilingResult> tiledResults = |
| 171 | + scf::tileUsingSCF(rewriter, tilingInterfaceOp, options); |
| 172 | + if (failed(tiledResults)) { |
| 173 | + LDBG() << "failed to tile the op"; |
| 174 | + return signalPassFailure(); |
| 175 | + } |
| 176 | + rewriter.replaceOp(op, tiledResults->replacements); |
| 177 | + } |
| 178 | + |
| 179 | + RewritePatternSet patterns = |
| 180 | + linalg::getLinalgTilingCanonicalizationPatterns(context); |
| 181 | + scf::populateSCFForLoopCanonicalizationPatterns(patterns); |
| 182 | + tensor::populateFoldTensorEmptyPatterns(patterns); |
| 183 | + memref::populateResolveRankedShapedTypeResultDimsPatterns(patterns); |
| 184 | + context->getLoadedDialect<tensor::TensorDialect>() |
| 185 | + ->getCanonicalizationPatterns(patterns); |
| 186 | + if (failed(applyPatternsGreedily(funcOp, std::move(patterns)))) { |
| 187 | + LDBG() << "----- cleanup failed -----"; |
| 188 | + return signalPassFailure(); |
| 189 | + } |
| 190 | +} |
| 191 | +} // namespace |
| 192 | +} // namespace mlir::iree_compiler |
0 commit comments