Skip to content

[mlir][Transforms] Make 1:N function conversion pattern interface-based #92395

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
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
14 changes: 14 additions & 0 deletions mlir/include/mlir/Transforms/OneToNTypeConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ LogicalResult
applyPartialOneToNConversion(Operation *op, OneToNTypeConverter &typeConverter,
const FrozenRewritePatternSet &patterns);

/// Add a pattern to the given pattern list to convert the signature of a
/// FunctionOpInterface op with the given type converter. This only supports
/// ops which use FunctionType to represent their type. This is intended to be
/// used with the 1:N dialect conversion.
void populateOneToNFunctionOpInterfaceTypeConversionPattern(
StringRef functionLikeOpName, TypeConverter &converter,
RewritePatternSet &patterns);
template <typename FuncOpT>
void populateOneToNFunctionOpInterfaceTypeConversionPattern(
TypeConverter &converter, RewritePatternSet &patterns) {
populateOneToNFunctionOpInterfaceTypeConversionPattern(
FuncOpT::getOperationName(), converter, patterns);
}

} // namespace mlir

#endif // MLIR_TRANSFORMS_ONETONTYPECONVERSION_H
47 changes: 2 additions & 45 deletions mlir/lib/Dialect/Func/Transforms/OneToNFuncConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,50 +49,6 @@ class ConvertTypesInFuncCallOp : public OneToNOpConversionPattern<CallOp> {
}
};

class ConvertTypesInFuncFuncOp : public OneToNOpConversionPattern<FuncOp> {
public:
using OneToNOpConversionPattern<FuncOp>::OneToNOpConversionPattern;

LogicalResult
matchAndRewrite(FuncOp op, OpAdaptor adaptor,
OneToNPatternRewriter &rewriter) const override {
auto *typeConverter = getTypeConverter<OneToNTypeConverter>();

// Construct mapping for function arguments.
OneToNTypeMapping argumentMapping(op.getArgumentTypes());
if (failed(typeConverter->computeTypeMapping(op.getArgumentTypes(),
argumentMapping)))
return failure();

// Construct mapping for function results.
OneToNTypeMapping funcResultMapping(op.getResultTypes());
if (failed(typeConverter->computeTypeMapping(op.getResultTypes(),
funcResultMapping)))
return failure();

// Nothing to do if the op doesn't have any non-identity conversions for its
// operands or results.
if (!argumentMapping.hasNonIdentityConversion() &&
!funcResultMapping.hasNonIdentityConversion())
return failure();

// Update the function signature in-place.
auto newType = FunctionType::get(rewriter.getContext(),
argumentMapping.getConvertedTypes(),
funcResultMapping.getConvertedTypes());
rewriter.modifyOpInPlace(op, [&] { op.setType(newType); });

// Update block signatures.
if (!op.isExternal()) {
Region *region = &op.getBody();
Block *block = &region->front();
rewriter.applySignatureConversion(block, argumentMapping);
}

return success();
}
};

class ConvertTypesInFuncReturnOp : public OneToNOpConversionPattern<ReturnOp> {
public:
using OneToNOpConversionPattern<ReturnOp>::OneToNOpConversionPattern;
Expand Down Expand Up @@ -121,10 +77,11 @@ void populateFuncTypeConversionPatterns(TypeConverter &typeConverter,
patterns.add<
// clang-format off
ConvertTypesInFuncCallOp,
ConvertTypesInFuncFuncOp,
ConvertTypesInFuncReturnOp
// clang-format on
>(typeConverter, patterns.getContext());
populateOneToNFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(
typeConverter, patterns);
}

} // namespace mlir
59 changes: 59 additions & 0 deletions mlir/lib/Transforms/Utils/OneToNTypeConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "mlir/Transforms/OneToNTypeConversion.h"

#include "mlir/Interfaces/FunctionInterfaces.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/ADT/SmallSet.h"

Expand Down Expand Up @@ -412,4 +413,62 @@ applyPartialOneToNConversion(Operation *op, OneToNTypeConverter &typeConverter,
return success();
}

namespace {
class FunctionOpInterfaceSignatureConversion : public OneToNConversionPattern {
public:
FunctionOpInterfaceSignatureConversion(StringRef functionLikeOpName,
MLIRContext *ctx,
TypeConverter &converter)
: OneToNConversionPattern(converter, functionLikeOpName, /*benefit=*/1,
ctx) {}

LogicalResult matchAndRewrite(Operation *op, OneToNPatternRewriter &rewriter,
const OneToNTypeMapping &operandMapping,
const OneToNTypeMapping &resultMapping,
ValueRange convertedOperands) const override {
auto funcOp = cast<FunctionOpInterface>(op);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this guaranteed to work? I suppose that the base already tests for functionLikeOpName? But what if that op doesn't implement the FunctionOpInterface?. Maybe there should an assert here? (I guess it should not be a match failure because whoever used the pattern must have thought that the op actually does implement the interface...)

Copy link
Member Author

Choose a reason for hiding this comment

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

The pattern gets an operation name as a constructor arg and only such ops are matched. Using this pattern with ops that do not implement FunctionOpInterface is incorrect API usage. cast will fail in such a case (I think with a failed assertion internally).

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right! Thanks for helping me see it!

auto *typeConverter = getTypeConverter<OneToNTypeConverter>();

// Construct mapping for function arguments.
OneToNTypeMapping argumentMapping(funcOp.getArgumentTypes());
if (failed(typeConverter->computeTypeMapping(funcOp.getArgumentTypes(),
argumentMapping)))
return failure();

// Construct mapping for function results.
OneToNTypeMapping funcResultMapping(funcOp.getResultTypes());
if (failed(typeConverter->computeTypeMapping(funcOp.getResultTypes(),
funcResultMapping)))
return failure();

// Nothing to do if the op doesn't have any non-identity conversions for its
// operands or results.
if (!argumentMapping.hasNonIdentityConversion() &&
!funcResultMapping.hasNonIdentityConversion())
return failure();

// Update the function signature in-place.
auto newType = FunctionType::get(rewriter.getContext(),
argumentMapping.getConvertedTypes(),
funcResultMapping.getConvertedTypes());
rewriter.modifyOpInPlace(op, [&] { funcOp.setType(newType); });

// Update block signatures.
if (!funcOp.isExternal()) {
Region *region = &funcOp.getFunctionBody();
Block *block = &region->front();
rewriter.applySignatureConversion(block, argumentMapping);
}

return success();
}
};
} // namespace

void populateOneToNFunctionOpInterfaceTypeConversionPattern(
StringRef functionLikeOpName, TypeConverter &converter,
RewritePatternSet &patterns) {
patterns.add<FunctionOpInterfaceSignatureConversion>(
functionLikeOpName, patterns.getContext(), converter);
}
} // namespace mlir
Loading