|
| 1 | +//===- ReifyResultShapes.cpp - Reify result shapes ------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, 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 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This transform reifies result shapes of `ReifyRankedShapedTypeOpInterface` |
| 10 | +// operations with ranked `memref` and `tensor` results. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "mlir/Dialect/MemRef/Transforms/Passes.h" |
| 15 | + |
| 16 | +#include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 17 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 18 | +#include "mlir/Dialect/MemRef/Transforms/Transforms.h" |
| 19 | +#include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 20 | +#include "mlir/Interfaces/InferTypeOpInterface.h" |
| 21 | +#include "llvm/Support/InterleavedRange.h" |
| 22 | + |
| 23 | +#define DEBUG_TYPE "reify-result-shapes" |
| 24 | +#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE << "]: ") |
| 25 | + |
| 26 | +namespace mlir { |
| 27 | +namespace memref { |
| 28 | +#define GEN_PASS_DEF_REIFYRESULTSHAPESPASS |
| 29 | +#include "mlir/Dialect/MemRef/Transforms/Passes.h.inc" |
| 30 | +} // namespace memref |
| 31 | +} // namespace mlir |
| 32 | + |
| 33 | +using namespace mlir; |
| 34 | + |
| 35 | +LogicalResult |
| 36 | +mlir::memref::reifyOpResultShapes(RewriterBase &rewriter, |
| 37 | + ReifyRankedShapedTypeOpInterface op) { |
| 38 | + LLVM_DEBUG({ DBGS() << " reifying op: " << op << "\n"; }); |
| 39 | + // Get the reified out shapes. |
| 40 | + ReifiedRankedShapedTypeDims reifiedResultShapes; |
| 41 | + if (failed(mlir::reifyResultShapes(rewriter, op, reifiedResultShapes)) || |
| 42 | + reifiedResultShapes.empty()) { |
| 43 | + return op.emitError() << "failed to get the reified shapes"; |
| 44 | + } |
| 45 | + |
| 46 | + bool modified = false; |
| 47 | + // Compute the new output types. |
| 48 | + SmallVector<Type> outTypes; |
| 49 | + for (const auto &[oldTy, reifiedShape] : |
| 50 | + llvm::zip(op->getResultTypes(), reifiedResultShapes)) { |
| 51 | + // Skip if it's not a memref or tensor type. |
| 52 | + if (!isa<RankedTensorType, MemRefType>(oldTy)) { |
| 53 | + outTypes.push_back(oldTy); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + ShapedType shapedTy = dyn_cast<ShapedType>(oldTy); |
| 58 | + |
| 59 | + SmallVector<int64_t> shape = llvm::to_vector(shapedTy.getShape()); |
| 60 | + for (auto &&[dim, ofr] : llvm::zip_equal(shape, reifiedShape)) { |
| 61 | + std::optional<int64_t> maybeCst = getConstantIntValue(ofr); |
| 62 | + // If the reified dim is dynamic set it appropriately. |
| 63 | + if (!maybeCst.has_value()) { |
| 64 | + dim = ShapedType::kDynamic; |
| 65 | + continue; |
| 66 | + } |
| 67 | + // Set the static dim. |
| 68 | + dim = *maybeCst; |
| 69 | + } |
| 70 | + |
| 71 | + // If the shape didn't change continue. |
| 72 | + if (shape == shapedTy.getShape()) { |
| 73 | + outTypes.push_back(oldTy); |
| 74 | + continue; |
| 75 | + } |
| 76 | + modified = true; |
| 77 | + outTypes.push_back(shapedTy.cloneWith(shape, shapedTy.getElementType())); |
| 78 | + } |
| 79 | + |
| 80 | + // Return if we don't need to update. |
| 81 | + if (!modified) { |
| 82 | + LLVM_DEBUG({ DBGS() << "- op doesn't require update\n"; }); |
| 83 | + return success(); |
| 84 | + } |
| 85 | + |
| 86 | + LLVM_DEBUG({ |
| 87 | + DBGS() << "- oldTypes: " << llvm::interleaved_array(op->getResultTypes()) |
| 88 | + << " \n"; |
| 89 | + DBGS() << "- outTypes: " << llvm::interleaved_array(outTypes) << " \n"; |
| 90 | + }); |
| 91 | + |
| 92 | + // We now have outTypes that need to be turned to cast ops. |
| 93 | + Location loc = op->getLoc(); |
| 94 | + SmallVector<Value> newResults; |
| 95 | + Operation *newOp = rewriter.clone(*op); |
| 96 | + for (auto [reifiedTy, oldRes] : llvm::zip(outTypes, op->getResults())) { |
| 97 | + OpResult newRes = newOp->getResult(oldRes.getResultNumber()); |
| 98 | + Type oldTy = oldRes.getType(); |
| 99 | + // Continue if the type remained invariant or is not shaped. |
| 100 | + if (oldTy == reifiedTy || !isa<MemRefType, RankedTensorType>(oldTy)) { |
| 101 | + newResults.push_back(newRes); |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + // Update the type. |
| 106 | + newRes.setType(reifiedTy); |
| 107 | + if (isa<RankedTensorType>(reifiedTy)) { |
| 108 | + newResults.push_back(rewriter.create<tensor::CastOp>(loc, oldTy, newRes)); |
| 109 | + } else { |
| 110 | + assert(isa<MemRefType>(reifiedTy) && "expected a memref type"); |
| 111 | + newResults.push_back(rewriter.create<memref::CastOp>(loc, oldTy, newRes)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + LLVM_DEBUG({ |
| 116 | + DBGS() << "- reified results " << llvm::interleaved_array(newResults) |
| 117 | + << "\n"; |
| 118 | + }); |
| 119 | + rewriter.replaceOp(op, newResults); |
| 120 | + return success(); |
| 121 | +} |
| 122 | + |
| 123 | +//===----------------------------------------------------------------------===// |
| 124 | +// Pass registration |
| 125 | +//===----------------------------------------------------------------------===// |
| 126 | + |
| 127 | +namespace { |
| 128 | +struct ReifyResultShapesPass final |
| 129 | + : public memref::impl::ReifyResultShapesPassBase<ReifyResultShapesPass> { |
| 130 | + void runOnOperation() override; |
| 131 | +}; |
| 132 | +} // namespace |
| 133 | + |
| 134 | +void ReifyResultShapesPass::runOnOperation() { |
| 135 | + SmallVector<ReifyRankedShapedTypeOpInterface> ops; |
| 136 | + getOperation()->walk( |
| 137 | + [&](ReifyRankedShapedTypeOpInterface op) { ops.push_back(op); }); |
| 138 | + IRRewriter rewriter(&getContext()); |
| 139 | + for (ReifyRankedShapedTypeOpInterface op : ops) { |
| 140 | + rewriter.setInsertionPoint(op); |
| 141 | + if (failed(memref::reifyOpResultShapes(rewriter, op))) |
| 142 | + return signalPassFailure(); |
| 143 | + } |
| 144 | +} |
0 commit comments