|
9 | 9 | #include "iree/compiler/Dialect/Flow/Conversion/TensorToFlow/Utils.h" |
10 | 10 | #include "iree/compiler/Dialect/Flow/IR/FlowDialect.h" |
11 | 11 | #include "iree/compiler/Dialect/Flow/IR/FlowOps.h" |
| 12 | +#include "mlir/Dialect/Affine/IR/AffineOps.h" |
12 | 13 | #include "mlir/Dialect/Arith/IR/Arith.h" |
13 | 14 | #include "mlir/Dialect/Arith/Utils/Utils.h" |
14 | 15 | #include "mlir/Dialect/Linalg/IR/Linalg.h" |
@@ -174,6 +175,74 @@ struct ConvertTensorCastPattern : public OpRewritePattern<tensor::CastOp> { |
174 | 175 | } |
175 | 176 | }; |
176 | 177 |
|
| 178 | +struct ConvertTensorConcatPattern : public OpRewritePattern<tensor::ConcatOp> { |
| 179 | + using OpRewritePattern<tensor::ConcatOp>::OpRewritePattern; |
| 180 | + |
| 181 | + LogicalResult matchAndRewrite(tensor::ConcatOp concatOp, |
| 182 | + PatternRewriter &rewriter) const override { |
| 183 | + if (concatOp->getParentOfType<IREE::Flow::DispatchRegionOp>() || |
| 184 | + concatOp->getParentOfType<IREE::Flow::DispatchWorkgroupsOp>()) { |
| 185 | + return failure(); |
| 186 | + } |
| 187 | + if (concatOp.getDim() != 0) { |
| 188 | + return rewriter.notifyMatchFailure( |
| 189 | + concatOp, "only outer-dim concat lowering supported"); |
| 190 | + } |
| 191 | + assert(cast<RankedTensorType>(concatOp.getInputs().front().getType()) |
| 192 | + .getRank() != 0 && |
| 193 | + "concat cannot be of zero-rank tensors"); |
| 194 | + |
| 195 | + Location loc = concatOp.getLoc(); |
| 196 | + SmallVector<SmallVector<OpFoldResult>> inputShapes; |
| 197 | + inputShapes.reserve(concatOp.getInputs().size()); |
| 198 | + // Note the output shape is computed directly without using |
| 199 | + // `reifyResultShapes` since we need the `inputShapes` anyway and using the |
| 200 | + // method would create duplicate `tensor.dim` operations. |
| 201 | + SmallVector<OpFoldResult> outputShape; |
| 202 | + AffineExpr addExpr = |
| 203 | + rewriter.getAffineSymbolExpr(0) + rewriter.getAffineSymbolExpr(1); |
| 204 | + SmallVector<OpFoldResult> concatOffsets; |
| 205 | + concatOffsets.reserve(concatOp.getInputs().size()); |
| 206 | + for (auto [index, input] : llvm::enumerate(concatOp.getInputs())) { |
| 207 | + SmallVector<OpFoldResult> inputShape = |
| 208 | + tensor::getMixedSizes(rewriter, input.getLoc(), input); |
| 209 | + if (index == 0) { |
| 210 | + outputShape = inputShape; |
| 211 | + concatOffsets.push_back(rewriter.getIndexAttr(0)); |
| 212 | + } else { |
| 213 | + concatOffsets.push_back(outputShape[0]); |
| 214 | + outputShape[0] = affine::makeComposedFoldedAffineApply( |
| 215 | + rewriter, loc, addExpr, {outputShape[0], inputShape[0]}); |
| 216 | + } |
| 217 | + inputShapes.emplace_back(std::move(inputShape)); |
| 218 | + } |
| 219 | + |
| 220 | + Value replacement = rewriter.create<tensor::EmptyOp>( |
| 221 | + loc, outputShape, concatOp.getType().getElementType()); |
| 222 | + |
| 223 | + SmallVector<int64_t> resultStaticDims; |
| 224 | + SmallVector<Value> resultDynamicDims; |
| 225 | + dispatchIndexOpFoldResults(outputShape, resultDynamicDims, |
| 226 | + resultStaticDims); |
| 227 | + Value zero = rewriter.create<arith::ConstantIndexOp>(loc, 0); |
| 228 | + // Generate the `flow.tensor.update` operations for the concat. |
| 229 | + for (auto [index, input] : llvm::enumerate(concatOp.getInputs())) { |
| 230 | + SmallVector<int64_t> inputStaticShape; |
| 231 | + SmallVector<Value> inputDynamicShape; |
| 232 | + dispatchIndexOpFoldResults(inputShapes[index], inputDynamicShape, |
| 233 | + inputStaticShape); |
| 234 | + SmallVector<Value> offsets(inputStaticShape.size(), zero); |
| 235 | + offsets[0] = |
| 236 | + getValueOrCreateConstantIndexOp(rewriter, loc, concatOffsets[index]); |
| 237 | + replacement = rewriter.create<IREE::Flow::TensorUpdateOp>( |
| 238 | + loc, replacement.getType(), replacement, resultDynamicDims, offsets, |
| 239 | + input, inputDynamicShape); |
| 240 | + } |
| 241 | + rewriter.replaceOp(concatOp, replacement); |
| 242 | + return success(); |
| 243 | + } |
| 244 | +}; |
| 245 | + |
177 | 246 | struct ConvertTensorFromElementsPattern |
178 | 247 | : public OpRewritePattern<tensor::FromElementsOp> { |
179 | 248 | using OpRewritePattern<tensor::FromElementsOp>::OpRewritePattern; |
@@ -316,14 +385,14 @@ struct ConvertTensorReshapePattern : public OpRewritePattern<TensorReshapeOp> { |
316 | 385 |
|
317 | 386 | void populateTensorToFlowConversionPatterns(MLIRContext *context, |
318 | 387 | RewritePatternSet &patterns) { |
319 | | - patterns |
320 | | - .insert<ConvertLinalgFillPattern, ConvertTensorBitcastPattern, |
321 | | - ConvertTensorCastPattern, ConvertTensorExtractPattern, |
322 | | - ConvertTensorExtractSlicePattern, ConvertTensorInsertSlicePattern, |
323 | | - ConvertTensorInsertPattern, ConvertTensorFromElementsPattern, |
324 | | - ConvertTensorDialectReshapeOpPattern, |
325 | | - ConvertTensorReshapePattern<tensor::CollapseShapeOp>, |
326 | | - ConvertTensorReshapePattern<tensor::ExpandShapeOp>>(context); |
| 388 | + patterns.insert<ConvertLinalgFillPattern, ConvertTensorBitcastPattern, |
| 389 | + ConvertTensorCastPattern, ConvertTensorConcatPattern, |
| 390 | + ConvertTensorExtractPattern, ConvertTensorExtractSlicePattern, |
| 391 | + ConvertTensorInsertSlicePattern, ConvertTensorInsertPattern, |
| 392 | + ConvertTensorFromElementsPattern, |
| 393 | + ConvertTensorDialectReshapeOpPattern, |
| 394 | + ConvertTensorReshapePattern<tensor::CollapseShapeOp>, |
| 395 | + ConvertTensorReshapePattern<tensor::ExpandShapeOp>>(context); |
327 | 396 | } |
328 | 397 |
|
329 | 398 | } // namespace mlir::iree_compiler::IREE::Flow |
0 commit comments