Skip to content

Commit

Permalink
[mlir][Transforms] Dialect conversion: Make materializations optional (
Browse files Browse the repository at this point in the history
…#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.)

Note: This is a reupload of #104668, but with correct handling of cyclic unrealized_conversion_casts that may be generated by the dialect conversion.
  • Loading branch information
matthias-springer committed Sep 3, 2024
1 parent 0797c18 commit 6fd9571
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 298 deletions.
11 changes: 11 additions & 0 deletions mlir/include/mlir/Transforms/DialectConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,17 @@ struct ConversionConfig {
// already been modified) and iterators into past IR state cannot be
// represented at the moment.
RewriterBase::Listener *listener = nullptr;

/// If set to "true", the dialect conversion attempts to build source/target/
/// argument materializations through the type converter API in lieu of
/// builtin.unrealized_conversion_cast ops. The conversion process fails if
/// at least one materialization could not be built.
///
/// If set to "false", the dialect conversion does not does not build any
/// custom materializations and instead inserts
/// builtin.unrealized_conversion_cast ops to ensure that the resulting IR
/// is valid.
bool buildMaterializations = true;
};

//===----------------------------------------------------------------------===//
Expand Down
Loading

0 comments on commit 6fd9571

Please sign in to comment.