Skip to content

[mlir][Transforms] Dialect conversion: Improve signature conversion API #81997

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
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions mlir/include/mlir/Transforms/DialectConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,8 @@ class ConversionPatternRewriter final : public PatternRewriter {
/// Apply a signature conversion to the entry block of the given region. This
/// replaces the entry block with a new block containing the updated
/// signature. The new entry block to the region is returned for convenience.
/// If no block argument types are changing, the entry original block will be
/// left in place and returned.
///
/// If provided, `converter` will be used for any materializations.
Block *
Expand All @@ -671,8 +673,11 @@ class ConversionPatternRewriter final : public PatternRewriter {
const TypeConverter *converter = nullptr);

/// Convert the types of block arguments within the given region. This
/// replaces each block with a new block containing the updated signature. The
/// entry block may have a special conversion if `entryConversion` is
/// replaces each block with a new block containing the updated signature. If
/// an updated signature would match the current signature, the respective
/// block is left in place as is.
///
/// The entry block may have a special conversion if `entryConversion` is
/// provided. On success, the new entry block to the region is returned for
/// convenience. Otherwise, failure is returned.
FailureOr<Block *> convertRegionTypes(
Expand All @@ -681,7 +686,8 @@ class ConversionPatternRewriter final : public PatternRewriter {

/// Convert the types of block arguments within the given region except for
/// the entry region. This replaces each non-entry block with a new block
/// containing the updated signature.
/// containing the updated signature. If an updated signature would match the
/// current signature, the respective block is left in place as is.
///
/// If special conversion behavior is needed for the non-entry blocks (for
/// example, we need to convert only a subset of a BB arguments), such
Expand Down
10 changes: 3 additions & 7 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,8 @@ FailureOr<Block *> ArgConverter::convertSignature(
Block *block, const TypeConverter *converter,
ConversionValueMapping &mapping,
SmallVectorImpl<BlockArgument> &argReplacements) {
// Check if the block was already converted.
// * If the block is mapped in `conversionInfo`, it is a converted block.
// * If the block is detached, conservatively assume that it is going to be
// deleted; it is likely the old block (before it was converted).
if (conversionInfo.count(block) || !block->getParent())
return block;
assert(block->getParent() && "cannot convert signature of detached block");

// If a converter wasn't provided, and the block wasn't already converted,
// there is nothing we can do.
if (!converter)
Expand All @@ -570,7 +566,7 @@ Block *ArgConverter::applySignatureConversion(
// If no arguments are being changed or added, there is nothing to do.
unsigned origArgCount = block->getNumArguments();
auto convertedTypes = signatureConversion.getConvertedTypes();
if (origArgCount == 0 && convertedTypes.empty())
if (llvm::equal(block->getArgumentTypes(), convertedTypes))
return block;

// Split the block at the beginning to get a new block to use for the updated
Expand Down