Skip to content

Commit d7073c5

Browse files
[mlir][Transforms] Dialect conversion: Make materializations optional (#104668)
This commit makes source/target/argument materializations (via the `TypeConverter` API) optional. By default (`ConversionConfig::buildMaterializations = true`), the dialect conversion infrastructure tries to legalize all unresolved materializations right after the main transformation process has succeeded. If at least one unresolved materialization fails to resolve, the dialect conversion fails. (With an error message such as `failed to legalize unresolved materialization ...`.) Automatic materializations through the `TypeConverter` API can now be deactivated. In that case, every unresolved materialization will show up as a `builtin.unrealized_conversion_cast` op in the output IR. There used to be a complex and error-prone analysis in the dialect conversion that predicted the future uses of unresolved materializations. Based on that logic, some casts (that were deemed to unnecessary) were folded. This analysis was needed because folding happened at a point of time when some IR changes (e.g., op replacements) had not materialized yet. This commit removes that analysis. Any folding of cast ops now happens after all other IR changes have been materialized and the uses can directly be queried from the IR. This simplifies the analysis significantly. And certain helper data structures such as `inverseMapping` are no longer needed for the analysis. The folding itself is done by `reconcileUnrealizedCasts` (which also exists as a standalone pass). After casts have been folded, the remaining casts are materialized through the `TypeConverter`, as usual. This last step can be deactivated in the `ConversionConfig`. `ConversionConfig::buildMaterializations = false` can be used to debug error messages such as `failed to legalize unresolved materialization ...`. (It is also useful in case automatic materializations are not needed.) The materializations that failed to resolve can then be seen as `builtin.unrealized_conversion_cast` ops in the resulting IR. (This is better than running with `-debug`, because `-debug` shows IR where some IR changes have not been materialized yet.)
1 parent 4dbaef6 commit d7073c5

File tree

5 files changed

+118
-298
lines changed

5 files changed

+118
-298
lines changed

mlir/include/mlir/Transforms/DialectConversion.h

+11
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,17 @@ struct ConversionConfig {
11241124
// already been modified) and iterators into past IR state cannot be
11251125
// represented at the moment.
11261126
RewriterBase::Listener *listener = nullptr;
1127+
1128+
/// If set to "true", the dialect conversion attempts to build source/target/
1129+
/// argument materializations through the type converter API in lieu of
1130+
/// builtin.unrealized_conversion_cast ops. The conversion process fails if
1131+
/// at least one materialization could not be built.
1132+
///
1133+
/// If set to "false", the dialect conversion does not does not build any
1134+
/// custom materializations and instead inserts
1135+
/// builtin.unrealized_conversion_cast ops to ensure that the resulting IR
1136+
/// is valid.
1137+
bool buildMaterializations = true;
11271138
};
11281139

11291140
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)