[all-commits] [llvm/llvm-project] 337707: [mlir][Transforms] Dialect conversion: Context-awa...

Matthias Springer via All-commits all-commits at lists.llvm.org
Wed Aug 27 00:14:14 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 337707a5417dbdc8751c2a11eda920e250417b5a
      https://github.com/llvm/llvm-project/commit/337707a5417dbdc8751c2a11eda920e250417b5a
  Author: Matthias Springer <me at m-sp.org>
  Date:   2025-08-27 (Wed, 27 Aug 2025)

  Changed paths:
    M mlir/docs/DialectConversion.md
    M mlir/include/mlir/Transforms/DialectConversion.h
    M mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
    M mlir/lib/Transforms/Utils/DialectConversion.cpp
    A mlir/test/Transforms/test-context-aware-type-converter.mlir
    M mlir/test/Transforms/test-legalize-type-conversion.mlir
    M mlir/test/lib/Dialect/Test/TestPatterns.cpp

  Log Message:
  -----------
  [mlir][Transforms] Dialect conversion: Context-aware type conversions (#140434)

This commit adds support for context-aware type conversions: type
conversion rules that can return different types depending on the IR.

There is no change for existing (context-unaware) type conversion rules:
```c++
// Example: Conversion any integer type to f32.
converter.addConversion([](IntegerType t) {
  return Float32Type::get(t.getContext());
}
```

There is now an additional overload to register context-aware type
conversion rules:
```c++
// Example: Type conversion rule for integers, depending on the context:
// Get the defining op of `v`, read its "increment" attribute and return an
// integer with a bitwidth that is increased by "increment".
converter.addConversion([](Value v) -> std::optional<Type> {
  auto intType = dyn_cast<IntegerType>(v.getType());
  if (!intType)
    return std::nullopt;
  Operation *op = v.getDefiningOp();
  if (!op)
    return std::nullopt;
  auto incrementAttr = op->getAttrOfType<IntegerAttr>("increment");
  if (!incrementAttr)
    return std::nullopt;
  return IntegerType::get(v.getContext(),
                          intType.getWidth() + incrementAttr.getInt());
});
```

For performance reasons, the type converter caches the result of type
conversions. This is no longer possible when there context-aware type
conversions because each conversion could compute a different type
depending on the context. There is no performance degradation when there
are only context-unaware type conversions.

Note: This commit just adds context-aware type conversions to the
dialect conversion framework. There are many existing patterns that
still call `converter.convertType(someValue.getType())`. These should be
gradually updated in subsequent commits to call
`converter.convertType(someValue)`.

Co-authored-by: Markus Böck <markus.boeck02 at gmail.com>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list