[all-commits] [llvm/llvm-project] c63947: [mlir][Transforms] Dialect Conversion: Fix folder ...

Matthias Springer via All-commits all-commits at lists.llvm.org
Sun Jul 27 03:01:23 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: c639475974c4fe95951e2598e65a2be07ed05af1
      https://github.com/llvm/llvm-project/commit/c639475974c4fe95951e2598e65a2be07ed05af1
  Author: Matthias Springer <me at m-sp.org>
  Date:   2025-07-27 (Sun, 27 Jul 2025)

  Changed paths:
    M mlir/lib/Transforms/Utils/DialectConversion.cpp
    M mlir/test/Transforms/test-legalize-type-conversion.mlir

  Log Message:
  -----------
  [mlir][Transforms] Dialect Conversion: Fix folder implementation (#150775)

Operation folders can do two things:

1. Modify IR (in-place op modification). Failing to legalize an in-place
folded operation does not trigger an immediate rollback. This happens
only if the driver decides to try a different lowering path, requiring
it to roll back a bunch of modifications, including the application of
the folder.
2. Create new IR (constant op materialization of a folded attribute).
Failing to legalize a newly created constant op triggers an immediate
rollback.

In-place op modifications should be guarded by
`startOpModification`/`finalizeOpModification` because they are no
different from other in-place op modifications. (They just happen
outside of a pattern, but that does not mean that we should not track
those changes; we are tracking everything else.) This commit adds those
two function calls.

This commit also moves the `rewriter.replaceOp(op, replacementValues);`
function call before the loop nest that legalizes the newly created
constant ops (and therefore `replacementValues`). Conceptually, the
folded op must be replaced before attempting to legalize the constants
because the constant ops may themselves be replaced as part of their own
legalization process. The previous implementation happened to work in
the current conversion driver, but is incompatible with the One-Shot
Dialect Conversion driver, which expects to see the most recent IR at
all time.

>From an end-user perspective, this commit should be NFC. A common
folder-rollback pattern that is exercised by multiple tests cases: A
`memref.dim` is folded to `arith.constant`, but `arith.constant` is not
marked as legal as per the conversion target, triggering a rollback.

Note: Folding is generally unsafe in a dialect conversion (see #92683),
but that's a different issue. (In a One-Shot Dialect Conversion, it will
no longer be unsafe.)



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