Skip to content

[mlir][Transforms] Dialect Conversion: Add 1:N op replacement test case #121271

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 1 commit into from
Dec 29, 2024
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
24 changes: 23 additions & 1 deletion mlir/test/Transforms/test-legalizer.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func.func @fold_legalization() -> i32 {
// -----

// CHECK-LABEL: func @convert_detached_signature()
// CHECK: "test.legal_op_with_region"() ({
// CHECK: "test.legal_op"() ({
// CHECK: ^bb0(%arg0: f64):
// CHECK: "test.return"() : () -> ()
// CHECK: }) : () -> ()
Expand Down Expand Up @@ -483,3 +483,25 @@ func.func @test_1_to_n_block_signature_conversion() {
"test.return"() : () -> ()
}

// -----

// CHECK: notifyOperationInserted: test.step_1
// CHECK: notifyOperationReplaced: test.multiple_1_to_n_replacement
// CHECK: notifyOperationErased: test.multiple_1_to_n_replacement
// CHECK: notifyOperationInserted: test.legal_op
// CHECK: notifyOperationReplaced: test.step_1
// CHECK: notifyOperationErased: test.step_1

// CHECK-LABEL: func @test_multiple_1_to_n_replacement()
// CHECK: %[[legal_op:.*]]:4 = "test.legal_op"() : () -> (f16, f16, f16, f16)
// TODO: There should be a single cast (i.e., a single target materialization).
// This is currently not possible due to 1:N limitations of the conversion
// mapping. Instead, we have 3 argument materializations.
// CHECK: %[[cast1:.*]] = "test.cast"(%[[legal_op]]#2, %[[legal_op]]#3) : (f16, f16) -> f16
// CHECK: %[[cast2:.*]] = "test.cast"(%[[legal_op]]#0, %[[legal_op]]#1) : (f16, f16) -> f16
// CHECK: %[[cast3:.*]] = "test.cast"(%[[cast2]], %[[cast1]]) : (f16, f16) -> f16
// CHECK: "test.valid"(%[[cast3]]) : (f16) -> ()
func.func @test_multiple_1_to_n_replacement() {
%0 = "test.multiple_1_to_n_replacement"() : () -> (f16)
"test.invalid"(%0) : (f16) -> ()
}
51 changes: 47 additions & 4 deletions mlir/test/lib/Dialect/Test/TestPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ struct TestDetachedSignatureConversion : public ConversionPattern {
ConversionPatternRewriter &rewriter) const final {
if (op->getNumRegions() != 1)
return failure();
OperationState state(op->getLoc(), "test.legal_op_with_region", operands,
OperationState state(op->getLoc(), "test.legal_op", operands,
op->getResultTypes(), {}, BlockRange());
Region *newRegion = state.addRegion();
rewriter.inlineRegionBefore(op->getRegion(0), *newRegion,
Expand Down Expand Up @@ -1234,6 +1234,49 @@ class TestRepetitive1ToNConsumer : public ConversionPattern {
}
};

/// A pattern that tests two back-to-back 1 -> 2 op replacements.
class TestMultiple1ToNReplacement : public ConversionPattern {
public:
TestMultiple1ToNReplacement(MLIRContext *ctx, const TypeConverter &converter)
: ConversionPattern(converter, "test.multiple_1_to_n_replacement", 1,
ctx) {}
LogicalResult
matchAndRewrite(Operation *op, ArrayRef<ValueRange> operands,
ConversionPatternRewriter &rewriter) const final {
// Helper function that replaces the given op with a new op of the given
// name and doubles each result (1 -> 2 replacement of each result).
auto replaceWithDoubleResults = [&](Operation *op, StringRef name) {
SmallVector<Type> types;
for (Type t : op->getResultTypes()) {
types.push_back(t);
types.push_back(t);
}
OperationState state(op->getLoc(), name,
/*operands=*/{}, types, op->getAttrs());
auto *newOp = rewriter.create(state);
SmallVector<ValueRange> repls;
for (size_t i = 0, e = op->getNumResults(); i < e; ++i)
repls.push_back(newOp->getResults().slice(2 * i, 2));
rewriter.replaceOpWithMultiple(op, repls);
return newOp;
};

// Replace test.multiple_1_to_n_replacement with test.step_1.
Operation *repl1 = replaceWithDoubleResults(op, "test.step_1");
// Now replace test.step_1 with test.legal_op.
// TODO: Ideally, it should not be necessary to reset the insertion point
// here. Based on the API calls, it looks like test.step_1 is entirely
// erased. But that's not the case: an argument materialization will
// survive. And that argument materialization will be used by the users of
// `op`. If we don't reset the insertion point here, we get dominance
// errors. This will be fixed when we have 1:N support in the conversion
// value mapping.
rewriter.setInsertionPoint(repl1);
replaceWithDoubleResults(repl1, "test.legal_op");
return success();
}
};

} // namespace

namespace {
Expand Down Expand Up @@ -1319,7 +1362,8 @@ struct TestLegalizePatternDriver
TestUndoPropertiesModification, TestEraseOp,
TestRepetitive1ToNConsumer>(&getContext());
patterns.add<TestDropOpSignatureConversion, TestDropAndReplaceInvalidOp,
TestPassthroughInvalidOp>(&getContext(), converter);
TestPassthroughInvalidOp, TestMultiple1ToNReplacement>(
&getContext(), converter);
patterns.add<TestDuplicateBlockArgs>(converter, &getContext());
mlir::populateAnyFunctionOpInterfaceTypeConversionPattern(patterns,
converter);
Expand All @@ -1330,8 +1374,7 @@ struct TestLegalizePatternDriver
target.addLegalOp<ModuleOp>();
target.addLegalOp<LegalOpA, LegalOpB, LegalOpC, TestCastOp, TestValidOp,
TerminatorOp, OneRegionOp>();
target.addLegalOp(
OperationName("test.legal_op_with_region", &getContext()));
target.addLegalOp(OperationName("test.legal_op", &getContext()));
target
.addIllegalOp<ILLegalOpF, TestRegionBuilderOp, TestOpWithRegionFold>();
target.addDynamicallyLegalOp<TestReturnOp>([](TestReturnOp op) {
Expand Down
Loading