Commit Graph

1435 Commits

Author SHA1 Message Date
Christian Ulmann
0b5b2027f9 [MLIR][SROA] Reuse allocators to avoid rewalking the IR (#91971)
This commit extends the SROA interfaces to ensure the interface
instantiations can communicate newly created allocators to the
algorithm. This ensures that the SROA implementation does no longer
require re-walking the IR to find new allocators.
2024-05-14 10:26:27 +02:00
Christian Ulmann
eeafc9daa1 [MLIR][Mem2Reg] Fix multi slot handling & move retry handling (#91464)
This commit fixes Mem2Regs mutli-slot allocator handling and extends the
test dialect to test this.

Additionally, this modifies Mem2Reg's API to always attempt a full
promotion on all the passed in "allocators". This ensures that the pass
does not require unnecessary walks over the regions and improves caching
benefits.
2024-05-13 07:37:41 +02:00
Christian Ulmann
c6efcc925c [MLIR][Mem2Reg] Improve performance by avoiding recomputations (#91444)
This commit ensures that Mem2Reg reuses the `DominanceInfo` as well as
block index maps to avoid expensive recomputations. Due to the recent
migration to `OpBuilder`, the promotion of a slot does no longer replace
blocks. Having stable blocks makes the `DominanceInfo` preservable and
additionally allows to cache block index maps between different
promotions.

Performance measurements on very large functions show an up to 4x
speedup by these changes.
2024-05-08 15:53:14 +02:00
Christian Ulmann
084e2b53d2 [MLIR][Interfaces] Change MemorySlotInterface to use OpBuilder (#91341)
This commit changes the `MemorySlotInterface` back to using `OpBuilder`
instead of a rewriter. This was originally introduced in
https://reviews.llvm.org/D150432 but it was shown that patterns are a
bad idea for both Mem2Reg and SROA.
Mem2Reg suffers from the usage of a rewriter due to being forced to
create new basic blocks. This is an issue, as it leads to the
invalidation of the dominance information, which can be expensive to
recompute.
2024-05-08 07:40:15 +02:00
Christian Ulmann
6e9ea6ea68 [MLIR][LLVM][Mem2Reg] Extends support for partial stores (#89740)
This commit enhances the LLVM dialect's Mem2Reg interfaces to support
partial stores to memory slots. To achieve this support, the `getStored`
interface method has to be extended with a parameter of the reaching
definition, which is now necessary to produce the resulting value after
this store.
2024-04-24 14:28:15 +02:00
Christian Ulmann
4513050f52 [MLIR] Harmonize the behavior of the folding API functions (#88508)
This commit changes `OpBuilder::tryFold` to behave more similarly to
`Operation::fold`. Concretely, this ensures that even an in-place fold
returns `success`.
This is necessary to fix a bug in the dialect conversion that occurred
when an in-place folding made an operation legal. The dialect conversion
infrastructure did not check if the result of an in-place folding
legalized the operation and just went ahead and tried to apply pattern
anyways.

The added test contains a simplified version of a breakage we observed
downstream.
2024-04-23 08:05:55 +02:00
Congcong Cai
60baaf153d [mlir] fix typo in mem2reg [NFC] 2024-04-20 19:52:12 +08:00
Christian Ulmann
ac39fa740b [MLIR][Mem2Reg][LLVM] Enhance partial load support (#89094)
This commit improves LLVM dialect's Mem2Reg interfaces to support
promotions of partial loads from larger memory slots. To support this,
the Mem2Reg interface methods are extended with additional data layout
parameters. The data layout is required to determine type sizes to
produce correct conversion sequences.

Note: There will be additional followups that introduce a similar
functionality for stores, and there are plans to support accesses into
the middle of memory slots.
2024-04-18 13:09:16 +02:00
Adrian Kuegel
962534c4b4 [mlir] Apply ClangTidy BugProne patch
This time for real.
2024-04-11 10:28:10 +00:00
Adrian Kuegel
a8b461603b [mlir] Apply ClangTidy BugProne fix
forwarding reference passed to std::move(), which may unexpectedly cause
lvalues to be moved; use std::forward() instead.
2024-04-11 10:25:53 +00:00
Mehdi Amini
60c5c4ccad [MLIR] Don't check for key before inserting in map in GreedyPatternRewriteDriver worklist (NFC) (#88148)
This is a common anti-pattern (any volunteer for a clang-tidy check?).

This does not show real word significant impact though.
2024-04-09 19:33:53 +02:00
Fabian Mora
220cdf940e [mlir] Add requiresReplacedValues and visitReplacedValues to PromotableOpInterface (#86792)
Add `requiresReplacedValues` and `visitReplacedValues` methods to
`PromotableOpInterface`. These methods allow `PromotableOpInterface` ops
to transforms definitions mutated by a `store`.

This change is necessary to correctly handle the promotion of
`LLVM_DbgDeclareOp`.

---------

Co-authored-by: Théo Degioanni <30992420+Moxinilian@users.noreply.github.com>
2024-04-04 13:34:46 -04:00
Slava Zakharin
133156c138 [mlir][inliner] Assert that no external nodes passed to the profitability hook. (#85489)
Fixes #85400
2024-04-02 12:50:32 -07:00
Mitch Phillips
56aeac47ab Revert "[mlir] Reland the dialect conversion hanging use fix (#87297)"
This reverts commit 49a4ec20a8.

Reason: Broke the ASan build bot with a memory leak. See the comments at
https://github.com/llvm/llvm-project/pull/87297
for more information.
2024-04-02 14:46:56 +02:00
Ivan Butygin
5b66b6a32a [mlir][pass] Add composite pass utility (#87166)
Composite pass allows to run sequence of passes in the loop until fixed
point or maximum number of iterations is reached. The usual candidates
are canonicalize+CSE as canonicalize can open more opportunities for CSE
and vice-versa.
2024-04-02 13:30:45 +03:00
Rob Suderman
49a4ec20a8 [mlir] Reland the dialect conversion hanging use fix (#87297)
Dialect conversion sometimes can have a hanging use of an argument.
Ensured that argument uses are dropped before removing the block.
2024-04-01 19:22:49 -07:00
Ivan Butygin
1079fc4f54 [mlir][pass] Add errorHandler param to Pass::initializeOptions (#87289)
There is no good way to report detailed errors from inside
`Pass::initializeOptions` function as context may not be available at
this point and writing directly to `llvm::errs()` is not composable.

See
https://github.com/llvm/llvm-project/pull/87166#discussion_r1546426763

* Add error handler callback to `Pass::initializeOptions`
* Update `PassOptions::parseFromString` to support custom error stream
instead of using `llvm::errs()` directly.
* Update default `Pass::initializeOptions` implementation to propagate
error string from `parseFromString` to new error handler.
* Update `MapMemRefStorageClassPass` to report error details using new
API.
2024-04-02 02:43:04 +03:00
Jakub Kuderski
971b852546 [mlir][NFC] Simplify type checks with isa predicates (#87183)
For more context on isa predicates, see:
https://github.com/llvm/llvm-project/pull/83753.
2024-04-01 11:40:09 -04:00
Mehdi Amini
23941019c0 Revert "[mlir]Fix dialect conversion drop uses" (#87205)
Reverts llvm/llvm-project#86991

Some bots are broken with a leak being detected now.
2024-03-31 23:25:51 +02:00
Rob Suderman
0030fc4ac7 [mlir]Fix dialect conversion drop uses (#86991)
Before deleting the block we need to drop uses to the surrounding args.
If this is not performed dialect conversion failures can result in a
failure to remove args (despite the block having no remaining uses).
2024-03-29 15:04:40 -07:00
mlevesquedion
ddc9892999 Add operands to worklist when only used by deleted op (#86990)
I believe the existing check to determine if an operand should be added
is incorrect: `operand.use_empty() || operand.hasOneUse()`. This is
because these checks do not take into account the fact that the op is
being deleted. It hasn't been deleted yet, so `operand.use_empty()`
cannot be true, and `operand.hasOneUse()` may be true if the op being
deleted is the only user of the operand and it only uses it once, but it
will fail if the operand is used more than once (e.g. something like
`add %0, %0`).

Instead, check if the op being deleted is the only _user_ of the
operand. If so, add the operand to the worklist.

Fixes #86765
2024-03-29 21:38:41 +01:00
Fabian Tschopp
5d187898f6 [mlir][inliner] Return early if the inliningThreshold is 0U or -1U. (#86287)
Computing the inlinling profitability can be costly due to walking the
graph when counting the number of operations.

This PR addresses that by returning early if the threshold is set to
never or always inline.
2024-03-23 00:09:11 +01:00
Christian Ulmann
98c6bc531d [MLIR][SROA][Mem2Reg] Add data layout to interface methods (#85644)
This commit expends the Mem2Reg and SROA interface methods with passed
in handles to a `DataLayout` structure. This is done to avoid
superfluous retreiving of data layouts during each conversion of
intrinsics.

This change, additionally, enables subsequent changes to make the LLVM
dialect implementation of these interfaces type agnostic.
2024-03-20 14:21:53 +01:00
Christian Ulmann
63897a595a [MLIR][SROA] Replace pattern based approach with a one-shot one (#85437)
This commit changes MLIR's SROA implementation back from being pattern
based into a full pass. This is beneficial for upcoming changes that
rely more heavily on the datalayout.

Unfortunately, this change required substantial test changes, as the
IRBuilder no cleans up the IR.
2024-03-18 08:33:09 +01:00
Christian Ulmann
3e2992f070 [MLIR][Mem2Reg] Replace pattern based approach with a bulk one (#85426)
This commit changes MLIR's Mem2Reg implementation back from being
pattern based into a full pass. Using Mem2Reg as a pattern is
wasteful, as each application can invalidate the dominance info.
Applying changes in bulk allows for reuse of the same dominance info.

Unfortunately, this requires some test changes, due to the `IRBuilder`
not simplifying IR.
2024-03-18 08:32:50 +01:00
Slava Zakharin
732f5368cd [RFC][mlir] Add profitability callback to the Inliner. (#84258)
Discussion at https://discourse.llvm.org/t/inliner-cost-model/2992

This change adds a callback that reports whether inlining
of the particular call site (communicated via ResolvedCall argument)
is profitable or not. The default MLIR inliner pass behavior
is unchanged, i.e. the callback always returns true.
This callback may be used to customize the inliner behavior
based on the target specifics (like target instructions costs),
profitability of the inlining for further optimizations
(e.g. if inlining may enable loop optimizations or scalar optimizations
due to object shape propagation), optimization levels (e.g. -Os inlining
may be quite different from -Ofast inlining), etc.

One of the questions is whether the ResolvedCall entity represents
enough of the context for the custom inlining models to come up with
the profitability decision. I think we can start with this and
extend it as necessary.

---------

Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
2024-03-13 08:23:10 -07:00
Matthias Springer
2a30684557 [mlir][Transforms] Use correct listener in dialect conversion (#84861)
There was a typo in the dialect conversion: `RewriterBase::Listener`
should be used instead of `ForwardingListener`.
2024-03-12 10:51:11 +09:00
Congcong Cai
ad23127222 [mlir][inline] avoid inline self-recursive function (#83092) 2024-03-12 06:49:09 +08:00
Matthias Springer
f1aa783788 [mlir][IR] Fix overload resolution on MSVC build (#84589)
#82629 added additional overloads to `replaceAllUsesWith` and
`replaceUsesWithIf`. This caused a build breakage with MSVC when called
with ops that can implicitly convert to `Value`.

```
external/llvm-project/mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp(881): error C2666: 'mlir::RewriterBase::replaceAllUsesWith': 2 overloads have similar conversions
external/llvm-project/mlir/include\mlir/IR/PatternMatch.h(631): note: could be 'void mlir::RewriterBase::replaceAllUsesWith(mlir::Operation *,mlir::ValueRange)'
external/llvm-project/mlir/include\mlir/IR/PatternMatch.h(626): note: or       'void mlir::RewriterBase::replaceAllUsesWith(mlir::ValueRange,mlir::ValueRange)'
external/llvm-project/mlir/include\mlir/IR/PatternMatch.h(616): note: or       'void mlir::RewriterBase::replaceAllUsesWith(mlir::Value,mlir::Value)'
external/llvm-project/mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp(882): note: while trying to match the argument list '(mlir::tensor::ExtractSliceOp, T)'
        with
        [
            T=mlir::Value
        ]
```

Note: The LLVM build bots (Linux and Windows) did not break, this seems
to be an issue with `Tools\MSVC\14.29.30133\bin\HostX64\x64\cl.exe`.

This change renames the newly added overloads to `replaceAllOpUsesWith`
and `replaceOpUsesWithIf`.
2024-03-11 17:36:18 +09:00
Matthias Springer
9b6bd7093c [mlir][IR] Add listener notifications for pattern begin/end (#84131)
This commit adds two new notifications to `RewriterBase::Listener`:
* `notifyPatternBegin`: Called when a pattern application begins during
a greedy pattern rewrite or dialect conversion.
* `notifyPatternEnd`: Called when a pattern application finishes during
a greedy pattern rewrite or dialect conversion.

The listener infrastructure already provides a `notifyMatchFailure`
callback that notifies about the reason for a pattern match failure. The
two new notifications provide additional information about pattern
applications.

This change is in preparation of improving the handle update mechanism
in the `apply_conversion_patterns` transform op.
2024-03-10 12:12:50 +09:00
Matthias Springer
60a20bd697 [mlir][Transforms] Add listener support to dialect conversion (#83425)
This commit adds listener support to the dialect conversion. Similarly
to the greedy pattern rewrite driver, an optional listener can be
specified in the configuration object.

Listeners are notified only if the dialect conversion succeeds. In case
of a failure, where some IR changes are first performed and then rolled
back, no notifications are sent.

Due to the fact that some kinds of rewrite are reflected in the IR
immediately and some in a delayed fashion, there are certain limitations
when attaching a listener; these are documented in `ConversionConfig`.
To summarize, users are always notified about all rewrites that
happened, but the notifications are sent all at once at the very end,
and not interleaved with the actual IR changes.

This change is in preparation improvements to
`transform.apply_conversion_patterns`, which currently invalidates all
handles. In the future, it can use a listener to update handles
accordingly, similar to `transform.apply_patterns`.
2024-03-08 10:34:45 +09:00
Matthias Springer
ddaf040ea9 [mlir][Transforms][NFC] Make signature conversion more efficient (#83922)
During block signature conversion, a new block is inserted and ops are
moved from the old block to the new block. This commit changes the
implementation such that ops are moved in bulk (`splice`) instead of
one-by-one; that's what `splitBlock` is doing.

This also makes it possible to pass the new block argument types
directly to `createBlock` instead of using `addArgument` (which bypasses
the rewriter). This doesn't change anything from a technical point of
view (there is no rewriter API for adding arguments at the moment), but
the implementation reads a bit nicer.
2024-03-08 10:06:24 +09:00
Matthias Springer
59a92019fb [mlir][IR] Make replaceOp / replaceAllUsesWith API consistent (#82629)
* `replaceOp` replaces all uses of the original op and erases the old
op.
* `replaceAllUsesWith` replaces all uses of the original op/value/block.
It does not erase any IR.

This commit renames `replaceOpWithIf` to `replaceUsesWithIf`.
`replaceOpWithIf` was a misnomer because the function never erases the
original op. Similarly, `replaceOpWithinBlock` is renamed to
`replaceUsesWithinBlock`. (No "operation replaced" is sent because the
op is not erased.)

Also improve comments.
2024-03-07 10:26:22 +09:00
Slava Zakharin
2542d34522 [mlir][inliner] Refactor MLIR inliner pass and utils. (#84059)
This is just code refactoring done as a preparation for adding
MLIR inliner cost model hook(s).
Related discussion: https://discourse.llvm.org/t/inliner-cost-model/2992

The logic of SCC-based MLIR inliner is separated into the Inliner
implementation. The MLIR inliner pass becomes, well, just a pass
that invokes the SCC-based MLIR inliner.
2024-03-06 10:19:58 -08:00
Thomas Preud'homme
a9304edf20 Fix remaining build failures with GCC 8.3 (#83266)
When compiling for GCC 8.x (< 8.4), SFINAE is disabled for
iterator_range constructor causing ambiguous resolution to construct an
OperandRange from a MutableOperatorRange, even in the presence of a
static_cast<OperatorRange>. This adds an explicit conversion method to
lift the ambiguity.

Tested with a full MLIR build with GCC 8.3.
2024-03-05 19:32:27 +00:00
Mehdi Amini
65a8e3a400 [MLIR] Fix crash in notifyBlockInserted() debug output (NFC)
notifyBlockInserted can be called when inserting a block in a region before
the op is built (like when building a scf::ForOp). This make us defensive
by checking the parent op before printing it.
2024-03-04 18:39:07 -08:00
Matthias Springer
310a278812 [mlir][Transforms][NFC] Simplify handling of erased IR (#83423)
The dialect conversion uses a `SingleEraseRewriter` to ensure that an
op/block is not erased twice. This can happen during the "commit" phase
when an unresolved materialization is inserted into a block and the
enclosing op is erased by the user. In that case, the unresolved
materialization should not be erased a second time later in the "commit"
phase.

This problem cannot happen during "rollback", so ops/block can be erased
directly without using the rewriter. With this change, the
`SingleEraseRewriter` is used only during "commit"/"cleanup". At that
point, the dialect conversion is guaranteed to succeed and no rollback
can happen. Therefore, it is not necessary to store the number of erased
IR objects (because we will never "reset" the rewriter to previous a
previous state).
2024-03-04 18:21:43 +09:00
Matthias Springer
aaf5c818b3 [mlir][Transforms][NFC] Simplify BlockTypeConversionRewrite (#83286)
When a block signature is converted during dialect conversion, a
`BlockTypeConversionRewrite` object is stored in the stack of rewrites.
Such an object represents multiple steps:
- Splitting the old block, i.e., creating a new block and moving all
operations over.
- Rewriting block arguments.
- Erasing the old block.

We have dedicated `IRRewrite` objects that represent "creating a block",
"moving an op" and "erasing a block". This commit reuses those rewrite
objects, so that there is less work to do in
`BlockTypeConversionRewrite::rollback` and
`BlockTypeConversionRewrite::commit`/`cleanup`.

Note: This change is in preparation of adding listener support to the
dialect conversion. The less work is done in a `commit` function, the
fewer notifications will have to be sent.
2024-03-04 18:06:00 +09:00
Matthias Springer
a282109411 [mlir][Transforms] Encapsulate dialect conversion options in ConversionConfig (#83754)
This commit adds a new `ConversionConfig` struct that allows users to
customize the dialect conversion. This configuration is similar to
`GreedyRewriteConfig` for the greedy pattern rewrite driver.

A few existing options are moved to this objects, simplifying the
dialect conversion API.

This is a re-upload of #82250. The Windows build breakage was fixed in #83768.

This reverts commit 60fbd60501.
2024-03-04 15:56:37 +09:00
Matthias Springer
9606655fbb [mlir][Transforms] Fix use-after-free when accessing replaced block args (#83646)
This commit fixes a bug in a dialect conversion. Currently, when a block
is replaced via a signature conversion, the block is erased during the
"commit" phase. This is problematic because the block arguments may
still be referenced internal data structures of the dialect conversion
(`mapping`). Blocks should be treated same as ops: they should be erased
during the "cleanup" phase.

Note: The test case fails without this fix when running with ASAN, but
may pass when running without ASAN.
2024-03-04 11:09:39 +09:00
Mehdi Amini
60fbd60501 Revert "[mlir][Transforms] Encapsulate dialect conversion options in ConversionConfig (#83662)
This reverts commit 5f1319bb38.

A FIR test is broken on Windows
2024-03-02 14:41:40 -08:00
Matthias Springer
926a19bf0b [mlir][Transforms][NFC] Remove SplitBlockRewrite (#82777)
When splitting a block during a dialect conversion, a
`SplitBlockRewrite` object is stored in the dialect conversion state.
This commit removes `SplitBlockRewrite`. Instead, a combination of
`CreateBlockRewrite` and multiple `MoveOperationRewrite` is used.

This change simplifies the internal state of the dialect conversion and
is also needed to properly support listeners.

`RewriteBase::splitBlock` is now no longer virtual. All necessary
information for committing/rolling back a split block rewrite can be
deduced from `Listener::notifyBlockInserted` and
`Listener::notifyOperationInserted` (which is also called when moving an
operation).
2024-02-28 14:26:02 +01:00
Matthias Springer
6008cd40b7 [mlir][Transforms] Dialect conversion: Assert when accessing erased ops (#83132)
The dialect conversion maintains sets of "ignored" and "replaced" ops.
This change simplifies the two sets, such that all nested ops are
included. (This was previously not the case and sometimes only the
parent op was included.)

This change allows for more aggressive assertions to prevent incorrect
rewriter API usage. E.g., accessing ops/blocks/regions within an erased
op.

A concrete example: I have seen conversion patterns in downstream
projects where an op is replaced with a new op, and the region of the
old op is afterwards inlined into the newly created op. This is invalid
rewriter API usage: ops that were replaced/erased should not be
accessed. Nested ops will be considered "ignored", even if they are
moved to a different region after the region's parent op was erased
(which is illegal API usage). Instead, create a new op, inline the
regions, then replace the old op with the new op.
2024-02-28 10:22:45 +01:00
Matthias Springer
7b66b5d6c2 [mlir][Transforms] Track erased ops separately (#83051)
#83023 fixed a performance regression related to "ignored" ops. This
broke some downstream projects that access ops after they were replaced
(an API violation). This change restores the original behavior before
#83023 (but without the performance regression), to give downstream
users more time to fix their code.
2024-02-26 20:49:52 +01:00
Matthias Springer
45732b6454 [mlir][Transforms] Fix compile time regression in dialect conversion (#83023)
The dialect conversion does not directly erase ops that are
replaced/erased with a rewriter. Instead, the op stays in place and is
erased at the end if the dialect conversion succeeds. However, ops that
were replaced/erased are ignored from that point on.

#81757 introduced a compile time regression that made the check whether
an op is ignored or not more expensive. Whether an op is ignored or not
is queried many times throughout a dialect conversion, so the check must
be fast.

After this change, replaced ops are stored in the `ignoredOps` set. This
also simplifies the dialect conversion a bit.
2024-02-26 17:40:56 +01:00
Matthias Springer
5840aa95e3 [mlir][Transforms] Fix crash in dialect conversion (#82783)
This is a follow-up to #82333. It is possible that the target block of a
`BlockTypeConversionRewrite` is detached, so the `MLIRContext` cannot be
taken from the block.
2024-02-23 17:26:39 +01:00
Matthias Springer
7bb08ee826 [mlir][Transforms][NFC] Decouple ConversionPatternRewriterImpl from ConversionPatternRewriter (#82333)
`ConversionPatternRewriterImpl` no longer maintains a reference to the
respective `ConversionPatternRewriter`. An `MLIRContext` is sufficient.
This commit simplifies the internal state of
`ConversionPatternRewriterImpl`.
2024-02-23 11:55:24 +01:00
Matthias Springer
5f1319bb38 [mlir][Transforms] Encapsulate dialect conversion options in ConversionConfig (#82250)
This commit adds a new `ConversionConfig` struct that allows users to
customize the dialect conversion. This configuration is similar to
`GreedyRewriteConfig` for the greedy pattern rewrite driver.

A few existing options are moved to this objects, simplifying the
dialect conversion API.
2024-02-23 11:28:05 +01:00
Matthias Springer
a622b21f46 [mlir][Transforms] Make ConversionPatternRewriter constructor private (#82244)
`ConversionPatternRewriter` objects should not be constructed outside of
dialect conversions. Some IR modifications performed through a
`ConversionPatternRewriter` are reflected in the IR in a delayed fashion
(e.g., only when the dialect conversion is guaranteed to succeed). Using
a `ConversionPatternRewriter` outside of the dialect conversion is
incorrect API usage and can bring the IR in an inconsistent state.

Migration guide: Use `IRRewriter` instead of
`ConversionPatternRewriter`.
2024-02-23 10:31:55 +01:00
Matthias Springer
59ff4d131c [mlir][Transforms][NFC] Turn unresolved materializations into IRRewrites (#81761)
This commit is a refactoring of the dialect conversion. The dialect
conversion maintains a list of "IR rewrites" that can be committed (upon
success) or rolled back (upon failure).

This commit turns the creation of unresolved materializations
(`unrealized_conversion_cast`) into `IRRewrite` objects. After this
commit, all steps in `applyRewrites` and `discardRewrites` are calls to
`IRRewrite::commit` and `IRRewrite::rollback`.
2024-02-23 10:15:12 +01:00