[all-commits] [llvm/llvm-project] 9df63b: [mlir][Transforms] Add 1:N `matchAndRewrite` overl...

Matthias Springer via All-commits all-commits at lists.llvm.org
Fri Nov 29 16:28:09 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 9df63b2651b2435c02a7d825953ca2ddc65c778e
      https://github.com/llvm/llvm-project/commit/9df63b2651b2435c02a7d825953ca2ddc65c778e
  Author: Matthias Springer <me at m-sp.org>
  Date:   2024-11-30 (Sat, 30 Nov 2024)

  Changed paths:
    M mlir/include/mlir/Conversion/LLVMCommon/Pattern.h
    M mlir/include/mlir/Transforms/DialectConversion.h
    M mlir/lib/Dialect/Func/Transforms/DecomposeCallGraphTypes.cpp
    M mlir/lib/Dialect/Func/Transforms/FuncConversions.cpp
    M mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
    M mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp
    M mlir/lib/Dialect/SparseTensor/Transforms/Utils/SparseTensorDescriptor.h
    M mlir/lib/Transforms/Utils/DialectConversion.cpp
    M mlir/test/Transforms/decompose-call-graph-types.mlir
    M mlir/test/Transforms/test-legalizer.mlir
    M mlir/test/lib/Dialect/Test/TestOps.td
    M mlir/test/lib/Dialect/Test/TestPatterns.cpp

  Log Message:
  -----------
  [mlir][Transforms] Add 1:N `matchAndRewrite` overload (#116470)

This commit adds a new `matchAndRewrite` overload to `ConversionPattern`
to support 1:N replacements. This is the first of two main PRs that
merge the 1:1 and 1:N dialect conversion drivers.

The existing `matchAndRewrite` function supports only 1:1 replacements,
as can be seen from the `ArrayRef<Value>` parameter.
```c++
LogicalResult ConversionPattern::matchAndRewrite(
    Operation *op, ArrayRef<Value> operands /*adaptor values*/,
    ConversionPatternRewriter &rewriter) const;
```

This commit adds a `matchAndRewrite` overload that is called by the
dialect conversion driver. By default, this new overload dispatches to
the original 1:1 `matchAndRewrite` implementation. Existing
`ConversionPattern`s do not need to be changed as long as there are no
1:N type conversions or value replacements.
```c++
LogicalResult ConversionPattern::matchAndRewrite(
    Operation *op, ArrayRef<ValueRange> operands /*adaptor values*/,
    ConversionPatternRewriter &rewriter) const {
  // Note: getOneToOneAdaptorOperands produces a fatal error if at least one
  // ValueRange has 0 or more than 1 value.
  return matchAndRewrite(op, getOneToOneAdaptorOperands(operands), rewriter);
}
```

The `ConversionValueMapping`, which keeps track of value replacements
and materializations, still does not support 1:N replacements. We still
rely on argument materializations to convert N replacement values back
into a single value. The `ConversionValueMapping` will be generalized to
1:N mappings in the second main PR.

Before handing the adaptor values to a `ConversionPattern`, all argument
materializations are "unpacked". The `ConversionPattern` receives N
replacement values and does not see any argument materializations. This
implementation strategy allows us to use the 1:N infrastructure/API in
`ConversionPattern`s even though some functionality is still missing in
the driver. This strategy was chosen to keep the sizes of the PRs
smaller and to make it easier for downstream users to adapt to API
changes.

This commit also updates the the "decompose call graphs" transformation
and the "sparse tensor codegen" transformation to use the new 1:N
`ConversionPattern` API.

Note for LLVM conversion: If you are using a type converter with 1:N
type conversion rules or if your patterns are performing 1:N
replacements (via `replaceOpWithMultiple` or
`applySignatureConversion`), conversion pattern applications will start
failing (fatal LLVM error) with this error message: `pattern 'name' does
not support 1:N conversion`. The name of the failing pattern is shown in
the error message. These patterns must be updated to the new 1:N
`matchAndRewrite` API.



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