Skip to content

[mlir][Transforms][NFC] Make signature conversion more efficient #83922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,22 +1281,17 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
ConversionPatternRewriter &rewriter, Block *block,
const TypeConverter *converter,
TypeConverter::SignatureConversion &signatureConversion) {
MLIRContext *ctx = rewriter.getContext();
OpBuilder::InsertionGuard g(rewriter);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Insertion guard is needed because OpBuilder::createBlock changes the insertion point.


// If no arguments are being changed or added, there is nothing to do.
unsigned origArgCount = block->getNumArguments();
auto convertedTypes = signatureConversion.getConvertedTypes();
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)
Expand All @@ -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.
Expand All @@ -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
Expand Down