Skip to content

Commit 2aa96fc

Browse files
[mlir][Transforms] Dialect conversion: Skip materializations when running without converter (#101318)
TODO: test case
1 parent 7583c48 commit 2aa96fc

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,37 +1316,43 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
13161316
continue;
13171317
}
13181318

1319-
// This is a 1->1+ mapping. 1->N mappings are not fully supported in the
1320-
// dialect conversion. Therefore, we need an argument materialization to
1321-
// turn the replacement block arguments into a single SSA value that can be
1322-
// used as a replacement.
1319+
// This is a 1->1+ mapping.
13231320
auto replArgs =
13241321
newBlock->getArguments().slice(inputMap->inputNo, inputMap->size);
1322+
1323+
// When there is no type converter, assume that the new block argument
1324+
// types are legal. This is reasonable to assume because they were
1325+
// specified by the user.
1326+
// FIXME: This won't work for 1->N conversions because multiple output
1327+
// types are not supported in parts of the dialect conversion. In such a
1328+
// case, we currently use the original block argument type (produced by
1329+
// the argument materialization).
1330+
if (!converter && replArgs.size() == 1) {
1331+
mapping.map(origArg, replArgs[0]);
1332+
appendRewrite<ReplaceBlockArgRewrite>(block, origArg);
1333+
continue;
1334+
}
1335+
1336+
// 1->N mappings are not fully supported in the dialect conversion.
1337+
// Therefore, we need an argument materialization to turn the replacement
1338+
// block arguments into a single SSA value (of the original type) that can
1339+
// be used as a replacement.
13251340
Value argMat = buildUnresolvedMaterialization(
13261341
MaterializationKind::Argument, newBlock, newBlock->begin(),
13271342
origArg.getLoc(), /*inputs=*/replArgs, origArgType, converter);
13281343
mapping.map(origArg, argMat);
13291344
appendRewrite<ReplaceBlockArgRewrite>(block, origArg);
13301345

1346+
// Now legalize the type by building a target materialization.
13311347
Type legalOutputType;
1332-
if (converter) {
1348+
if (converter)
13331349
legalOutputType = converter->convertType(origArgType);
1334-
} else if (replArgs.size() == 1) {
1335-
// When there is no type converter, assume that the new block argument
1336-
// types are legal. This is reasonable to assume because they were
1337-
// specified by the user.
1338-
// FIXME: This won't work for 1->N conversions because multiple output
1339-
// types are not supported in parts of the dialect conversion. In such a
1340-
// case, we currently use the original block argument type (produced by
1341-
// the argument materialization).
1342-
legalOutputType = replArgs[0].getType();
1343-
}
13441350
if (legalOutputType && legalOutputType != origArgType) {
13451351
Value targetMat = buildUnresolvedTargetMaterialization(
13461352
origArg.getLoc(), argMat, legalOutputType, converter);
13471353
mapping.map(argMat, targetMat);
1354+
appendRewrite<ReplaceBlockArgRewrite>(block, origArg);
13481355
}
1349-
appendRewrite<ReplaceBlockArgRewrite>(block, origArg);
13501356
}
13511357

13521358
appendRewrite<BlockTypeConversionRewrite>(newBlock, block, converter);

mlir/test/Transforms/test-legalize-type-conversion.mlir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ func.func @test_block_argument_not_converted() {
103103
// Make sure argument type changes aren't implicitly forwarded.
104104
func.func @test_signature_conversion_no_converter() {
105105
"test.signature_conversion_no_converter"() ({
106-
// expected-error@below {{failed to legalize unresolved materialization from ('f64') to 'f32' that remained live after conversion}}
106+
// expected-error@below {{failed to materialize conversion for block argument #0 that remained live after conversion, type was 'f32'}}
107107
^bb0(%arg0: f32):
108+
// expected-note@below{{see existing live user here}}
108109
"test.type_consumer"(%arg0) : (f32) -> ()
109110
"test.return"(%arg0) : (f32) -> ()
110111
}) : () -> ()

0 commit comments

Comments
 (0)