[Mlir-commits] [mlir] [mlir][Transforms][NFC] Make signature conversion more efficient (PR #83922)
Matthias Springer
llvmlistbot at llvm.org
Mon Mar 4 15:13:57 PST 2024
https://github.com/matthias-springer created https://github.com/llvm/llvm-project/pull/83922
During block signature conversion, a new block is inserted and ops are moved from the old block to the new block. This commit changes the implementation such that ops are moved in bulk (`splice`) instead of one-by-one; that's what `splitBlock` is doing.
This also makes it possible to pass the new block argument types directly to `createBlock` instead of using `addArgument` (which bypasses the rewriter). This doesn't change anything from a technical point of view (there is no rewriter API for adding arguments at the moment), but the implementation reads a bit nicer.
>From 4ea3fc3f686937d2f3e2f23c34197343aae2c0be Mon Sep 17 00:00:00 2001
From: Matthias Springer <springerm at google.com>
Date: Mon, 4 Mar 2024 23:09:20 +0000
Subject: [PATCH] [mlir][Transforms][NFC] Make signature conversion more
efficient
During block signature conversion, a new block is inserted and ops are moved from the old block to the new block. This commit changes the implementation such that ops are moved in bulk (`splice`) instead of one-by-one; that's what `splitBlock` is doing.
This also makes it possible to pass the new block argument types directly to `createBlock` instead of using `addArgument` (which bypasses the rewriter). This doesn't change anything from a technical point of view (there is no rewriter API for adding arguments at the moment), but the implementation reads a bit nicer.
---
.../Transforms/Utils/DialectConversion.cpp | 27 ++++++++++---------
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 9dc806730d01a1..3cfa2a66633e1a 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -1281,7 +1281,7 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
ConversionPatternRewriter &rewriter, Block *block,
const TypeConverter *converter,
TypeConverter::SignatureConversion &signatureConversion) {
- MLIRContext *ctx = rewriter.getContext();
+ OpBuilder::InsertionGuard g(rewriter);
// If no arguments are being changed or added, there is nothing to do.
unsigned origArgCount = block->getNumArguments();
@@ -1289,14 +1289,9 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
if (llvm::equal(block->getArgumentTypes(), convertedTypes))
return block;
- // Split the block at the beginning to get a new block to use for the updated
- // signature.
- Block *newBlock = rewriter.splitBlock(block, block->begin());
- block->replaceAllUsesWith(newBlock);
-
- // Map all new arguments to the location of the argument they originate from.
+ // Compute the locations of all block arguments in the new block.
SmallVector<Location> newLocs(convertedTypes.size(),
- Builder(ctx).getUnknownLoc());
+ rewriter.getUnknownLoc());
for (unsigned i = 0; i < origArgCount; ++i) {
auto inputMap = signatureConversion.getInputMapping(i);
if (!inputMap || inputMap->replacementValue)
@@ -1306,9 +1301,16 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
newLocs[inputMap->inputNo + j] = origLoc;
}
- SmallVector<Value, 4> newArgRange(
- newBlock->addArguments(convertedTypes, newLocs));
- ArrayRef<Value> newArgs(newArgRange);
+ // Insert a new block with the converted block argument types and move all ops
+ // from the old block to the new block.
+ Block *newBlock =
+ rewriter.createBlock(block->getParent(), std::next(block->getIterator()),
+ convertedTypes, newLocs);
+ appendRewrite<InlineBlockRewrite>(newBlock, block, newBlock->end());
+ newBlock->getOperations().splice(newBlock->end(), block->getOperations());
+
+ // Replace all uses of the old block with the new block.
+ block->replaceAllUsesWith(newBlock);
// Remap each of the original arguments as determined by the signature
// conversion.
@@ -1333,7 +1335,8 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
}
// Otherwise, this is a 1->1+ mapping.
- auto replArgs = newArgs.slice(inputMap->inputNo, inputMap->size);
+ auto replArgs =
+ newBlock->getArguments().slice(inputMap->inputNo, inputMap->size);
Value newArg;
// If this is a 1->1 mapping and the types of new and replacement arguments
More information about the Mlir-commits
mailing list