Commit Graph

158 Commits

Author SHA1 Message Date
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
a8cfa7cbdf [mlir][TD] Allow op printing flags as transform.print attrs (#86846)
Introduce 3 new optional attributes to the `transform.print` ops:
* `assume_verified`
* `use_local_scope`
* `skip_regions`

The primary motivation is to allow printing on large inputs that
otherwise take forever to print and verify. For the full context, see
this IREE issue: https://github.com/openxla/iree/issues/16901.

Also add some tests and fix the op description.
2024-04-01 12:32:23 -04:00
Oleksandr "Alex" Zinenko
0b790572b1 [mlir] propagate silenceable failures in transform.foreach_match (#86956)
The original implementation was eagerly reporting silenceable failures
from actions as definite failures. Since silenceable failures are
intended for cases when the IR has not been irreversibly modified, it's
okay to propagate them as silenceable failures of the parent op.

Fixes #86834.
2024-03-28 18:52:10 +01:00
Oleksandr "Alex" Zinenko
fa1b807bef [mlir] fix crash in transform.print verification (#86679)
Transform op trait verification calls `getEffects`, and since trait
verification runs before op verification, this call cannot assume the op
to be valid. However, the operand getters now return a `TypedValue` that
unconditionally casts the value to the expected type, leading to an
assertion failure. Use the untyped mechanism instead.

Fixes #84701.
2024-03-27 02:58:06 +01:00
Matthias Springer
c1029b6a9b [mlir][Transform] apply_conversion_patterns: Update handles (#83950)
Until now, `transform.apply_conversion_patterns` consumed the target
handle and potentially invalidated handles. This commit adds tracking
functionality similar to `transform.apply_patterns`, such that handles
are no longer invalidated, but updated based on op replacements
performed by the dialect conversion.

This new functionality is hidden behind a `preserve_handles` attribute
for now.
2024-03-10 12:10:53 +09:00
Oleksandr "Alex" Zinenko
5468f88413 [mlir] update remaining transform tests to main pass (#81279)
Use the main transform interpreter pass instead of the test pass. The
only tests that are not updated are specific to the operation of the
test pass.
2024-02-28 11:06:53 +01:00
Joshua Cao
7d055af14b [mlir][Symbol] Add verification that symbol's parent is a SymbolTable (#80590)
Following the discussion in
https://discourse.llvm.org/t/symboltable-and-symbol-parent-child-relationship/75446,
we should enforce that a symbol's immediate parent is a symbol table.

I changed some tests to pass the verification. In most cases, we can
wrap the func with a module, change the func to another op with regions
i.e. scf.if, or change the expected error message.

---------

Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
2024-02-05 22:59:03 -08:00
Quinn Dawkins
5caab8bbc0 [mlir][transform] Add transform.get_operand op (#78397)
Similar to `transform.get_result`, except it returns a handle to the
operand indicated by a positional specification, same as is defined for
the linalg match ops.

Additionally updates `get_result` to take the same positional specification.
This makes the use case of wanting to get all of the results of an
operation easier by no longer requiring the user to reconstruct the list
of results one-by-one.
2024-01-18 09:33:14 -05:00
Oleksandr "Alex" Zinenko
2798b72ae7 [mlir] introduce debug transform dialect extension (#77595)
Introduce a new extension for simple print-debugging of the transform
dialect scripts. The initial version of this extension consists of two
ops that are printing the payload objects associated with transform
dialect values. Similar ops were already available in the test extenion
and several downstream projects, and were extensively used for testing.
2024-01-12 13:24:02 +01:00
Oleksandr "Alex" Zinenko
633d9184f5 [mlir] introduce transform.collect_matching (#76724)
Introduce a new match combinator into the transform dialect. This
operation collects all operations that are yielded by a satisfactory
match into its results. This is a simpler version of `foreach_match`
that can be inserted directly into existing transform scripts.
2024-01-09 13:18:57 +01:00
Oleksandr "Alex" Zinenko
71c17424b5 [mlir][TD] update more tests to use the "main" interpreter pass (#76963)
Update several tests under mlir/test/Dialect/Transform to use the "main"
transform interpreter pass with named entry points rather than the test
interpreter pass.

This helped discover a logic error in the expensive checks mechanism
that was exiting too early.
2024-01-04 21:33:51 +01:00
Oleksandr "Alex" Zinenko
f90b609004 [mlir] introduce transform.num_associations (#76723)
Add a new transform operation that creates a new parameter containing the number of payload objects (operations, values or attributes) associated with the argument. This is useful in matching and for debugging purposes. This replaces three ad-hoc operations previously provided by the test extension.
2024-01-03 13:33:18 +01:00
Billy Zhu
34a65980d7 [MLIR] Erase location of folded constants (#75415)
Follow up to the discussion from #75258, and serves as an alternate
solution for #74670.

Set the location to Unknown for deduplicated / moved / materialized
constants by OperationFolder. This makes sure that the folded constants
don't end up with an arbitrary location of one of the original ops that
became it, and that hoisted ops don't confuse the stepping order.
2023-12-21 09:54:48 -08:00
Kunwar Grover
282d501476 [mlir][Transform] Fix crash with invalid ir for transform libraries (#75649)
This patch fixes a crash caused when the transform library interpreter
is given an IR that fails to parse.
2023-12-19 23:16:19 +05:30
Matthias Springer
e8ae0e72b7 [mlir][transform] TrackingListener: Improve dead handles detection (#74290)
The tracking listener should not report op replacement errors for
payload ops that are not mapped to any live handles. The handle liveless
analysis did not work properly with transform IR that has named
sequences.

A handle is live if it has a user after the transform op that is
currently being applied. With named sequences, we need to maintain a
stack of currently applied transform ops. That stack already exists
(`regionStack`), the only thing that's missing is the current transform
op for each stack frame.

This commit fixes #72931.
2023-12-06 16:32:22 +09:00
Matthias Springer
b9fe461e73 [mlir][transform] LISH: Add transform op (#70630)
Add a transform op for loop-invariant subset hoisting. Delete the old
transform op from the Linalg dialect.
2023-11-05 11:40:51 +09:00
Matthias Springer
04736c7f7a [mlir][SCF] Use transform.get_parent_op instead of transform.loop.get_parent_for (#70757)
Add a new attribute to `get_parent_op` to get the n-th parent. Remove
`transform.loop.get_parent_for`, which is no longer needed.
2023-10-31 18:36:40 +09:00
Ingo Müller
99c15eb49b [mlir][transform] Handle multiple library preloading passes. (#69705)
This is a new attempt at #69320.

The transform dialect stores a "library module" that the preload pass
can populate. Until now, each pass registered an additional module by
simply pushing it to a vector; however, the interpreter only used the
first of them. This commit turns the registration into "loading", i.e.,
each newly added module gets merged into the existing one. This allows
the loading to be split into several passes, and using the library in
the interpreter now takes all of them into account. While this design
avoids repeated merging every time the library is accessed, it requires
that the implementation of merging modules lives in the
TransformDialect target (since it at the dialect depend on each
other).

This resolves https://github.com/llvm/llvm-project/issues/69111.
2023-10-25 09:52:30 +02:00
Ingo Müller
f07718b708 [mlir][transform] Improve error when merging of modules fails. (#69331)
This resolved #69112.
2023-10-24 16:39:52 +02:00
martin-luecke
9ccf01fbf7 [mlir][transform] Support for multiple top-level transform ops (#69615)
This adds a flag to the `TransformDialectInterpreter` that relaxes the
requirement for only a single top-level transform op.
This is useful for supporting transforms that take transform IR as
payload.

This also aligns the function `findTopLevelTransform`
[here](7b0f4c9db5 (diff-551f92bb609487ccf981daf9571f0f1b1703ab2330560a388a5f0d133e520be4L59))
with its documentation:
In the presence of multiple top-level transform ops it now correctly
returns the first of them after reporting the error instead of returning
a `nullptr`.
2023-10-20 11:42:20 +02:00
Ingo Müller
3517b67ef0 Reapply "[mlir][transform] Support symlinks in module loading. Reorganize tests. (#69329)"
This reverts commit c122b9727a but fixes
tests that were added between submitting #69329 for review and landing
it for the first time.
2023-10-19 09:04:49 +00:00
Ingo Müller
c122b9727a Revert "[mlir][transform] Support symlinks in module loading. Reorganize tests. (#69329)"
This reverts commit f681225700. That
commit changed the organization of the tests of the transform dialect
interpreter but did not take into account some tests that were added in
the meantime.
2023-10-19 08:51:15 +00:00
Ingo Müller
f681225700 [mlir][transform] Support symlinks in module loading. Reorganize tests. (#69329)
A recent commit (#69190) broke the bazel builds. Turns out that Bazel
uses symlinks for providing the test files, which the path expansion of
the module loading mechanism did not handle correctly. This PR fixes
that.

It also reorganizes the tests better: It puts all `.mlir` files that are
included by some other test into a common `include` folder. This greatly
simplifies the definition of the dependencies between the different
`.mlir` files in Bazel's `BUILD` file. The commit also adds a comment to
all included files why these aren't tested themselves direclty and uses
the `%{fs-sep}` expansion for paths more consistently. Finally, it
uncomments all but one of the tests excluded in Bazel because they seem
to run now. (The remaining one includes a file that it itself a test, so
it would have to live *in* and *outside* of the `include` folder.)
2023-10-19 10:45:33 +02:00
Nicolas Vasilache
ebdb0cbef5 Add missing test from #68661 2023-10-18 11:04:01 +00:00
Ingo Müller
22e3bf4eaf [mlir][transform] Fix new interpreter and library preloading passes. (#69190)
This PR fixes the two recently added passes from #68661, which were
non-functional and untested. In particular:
* The passes did not declare their dependent dialects, so they could not
run at all in the most simple cases.
* The mechanism of loading the library module in the initialization of
the intepreter pass is broken by design (but, fortunately, also not
necessary). This is because the initialization of all passes happens
before the execution of any other pass, so the "preload library" pass
has not run yet at the time the interpreter pass gets initialized.
Instead, the library is now loaded every time the interpreter pass is
run. This should not be exceedingly expensive, since it only consists of
looking up the library in the dialect. Also, this removes the library
module from the pass state, making it possible in the future to preload
libraries in several passes.
* The PR adds tests for the two passes, which were completely untested
previously.
2023-10-17 12:32:16 +02:00
Nicolas Vasilache
1bf0870934 [mlir][Transform] Create a transform interpreter and a preloader pass (#68661)
This revision provides the ability to use an arbitrary named sequence op
as
the entry point to a transform dialect strategy.

It is also a step towards better transform dialect usage in pass
pipelines
that need to preload a transform library rather thanparse it on the fly.

The interpreter itself is significantly simpler than its testing
counterpart
by avoiding payload/debug root tags and multiple shared modules.

In the process, the NamedSequenceOp::apply function is adapted to allow
it
being an entry point.

NamedSequenceOp is **not** extended to take the PossibleTopLevelTrait at
this
time, because the implementation of the trait is specific to allowing
one
top-level dangling op with a region such as SequenceOp or
AlternativesOp.
In particular, the verifier of PossibleTopLevelTrait does not allow for
an
empty body, which is necessary to declare a NamedSequenceOp that gets
linked
in separately before application.

In the future, we should dispense with the PossibleTopLevelTrait
altogether
and always enter the interpreter with a NamedSequenceOp.

Lastly, relevant TD linking utilities are moved to
TransformInterpreterUtils
and reused from there.
2023-10-11 14:56:09 -07:00
Ingo Müller
6a2071cc6a [mlir][transform] Allow passing various library files to interpreter. (#67120)
The transfrom interpreter accepts an argument to a "library" file with
named sequences. This patch exteneds this functionality such that (1)
several such individual files are accepted and (2) folders can be passed
in, in which all `*.mlir` files are loaded.
2023-10-06 12:52:49 +02:00
Ingo Müller
787689943d [mlir][transform] Fix handling of transitive include in interpreter. (#67560)
Until now, the interpreter would only load those symbols from the
provided library files that were declared in the main transform module.
However, sequences in the library may include other sequences on their
own. Until now, if such sequences were not *also* declared in the main
transform module, the interpreter would fail to resolve them. Forward
declaring all of them is undesirable as it defeats the purpose of
encapsulation into library modules.

This PR implements a kind of linker for transform scripts to solve this
problem. The linker merges all symbols of the library module into the
main module before interpreting the latter. Symbols whose names collide
are handled as follows: (1) if they are both functions (in the sense of
`FunctionOpInterface`) with compatible signatures, one is external, and
the other one is public, then they are merged; (2) of one of them is
private, that one is renamed; and (3) an error is raised otherwise.

One consequence of this change is that the loading of the library files
in the interpreter pass is not idempotent anymore, i.e., subsequent 
interpreter passes cannot (and need not) load the same library files again
since would lead to doubly defined symbols.
2023-10-06 10:56:57 +02:00
Nicolas Vasilache
98341df053 [mlir][Transform] Add a transform.match.operation_empty op to allow s… (#68319)
…pecifying negative conditions

In the process, get_parent_op gains an attribute to allow it to return
empty handles explicitly and still succeed.
2023-10-06 09:25:24 +02:00
Matthias Springer
6187354095 [mlir][transform] Fix crash when consuming an op in a named sequence (#67437)
Fix a crash when consuming an op in a named sequence, when the same op
is also mapped in the caller's mapping. Ops must be removed from *all*
mappings during the "expensive checks". Otherwise, we may have dangling
pointers in the mappings data structures, which interfere with the
expensive checks.
2023-09-27 12:40:41 +02:00
Ingo Müller
f40e620956 Reapply "[mlir][transform] Improve error message of tracking listener. (#66987)"
This commit reapplies #66987, which got original contained a memory leak
and got reverted by 78c8ab5844. The leak
is now fixed.

Original description:

This PR extends the error message of the tracking listener when
replacement ops cannot be found. That may happen if the applied patterns
replace an op by an op of a different kind or by block arguments.
However, this only matters if there are alive handles to the replaced
op. The new error message mentions that explicitly and reports the alive
handles.
2023-09-26 12:56:38 +00:00
Oleksandr "Alex" Zinenko
96ff0255f2 [mlir] cleanup of structured.tile* transform ops (#67320)
Rename and restructure tiling-related transform ops from the structured
extension to be more homogeneous. In particular, all ops now follow a
consistent naming scheme:

 - `transform.structured.tile_using_for`;
 - `transform.structured.tile_using_forall`;
 - `transform.structured.tile_reduction_using_for`;
 - `transform.structured.tile_reduction_using_forall`.

This drops the "_op" naming artifact from `tile_to_forall_op` that
shouldn't have been included in the first place, consistently specifies
the name of the control flow op to be produced for loops (instead of
`tile_reduction_using_scf` since `scf.forall` also belongs to `scf`),
and opts for the `using` connector to avoid ambiguity.

The loops produced by tiling are now systematically placed as *trailing*
results of the transform op. While this required changing 3 out of 4 ops
(except for `tile_using_for`), this is the only choice that makes sense
when producing multiple `scf.for` ops that can be associated with a
variadic number of handles. This choice is also most consistent with
*other* transform ops from the structured extension, in particular with
fusion ops, that produce the structured op as the leading result and the
loop as the trailing result.
2023-09-26 09:14:29 +02:00
Vitaly Buka
78c8ab5844 Revert "[mlir][transform] Improve error message of tracking listener. (#66987)"
Breaks https://lab.llvm.org/buildbot/#/builders/5/builds/36953

This reverts commit a7530452fd.
2023-09-25 09:07:17 -07:00
Ingo Müller
a7530452fd [mlir][transform] Improve error message of tracking listener. (#66987)
This PR extends the error message of the tracking listener when
replacement ops cannot be found. That may happen if the applied patterns
replace an op by an op of a different kind or by block arguments.
However, this only matters if there are alive handles to the replaced
op. The new error message mentions that explicitly and reports the alive
handles.
2023-09-25 13:56:59 +02:00
Ingo Müller
69bc1cbbff [mlir][linalg][transform] Rename {masked_vectorize => vectorize => vectorize_children_and...}. (#66575)
This PR renames the vectorization transform ops as follows:

* `structured.masked_vectorize` => `structured.vectorize`. This reflects
the fact that since [recently](https://reviews.llvm.org/D157774) the op
can also handle the unmasked case.
* `structured.vectorize` =>
`structured.vectorize_children_and_applies_patterns`. This reflects the
fact that the op does not just vectorize the given payload op but all
vectorizable children contained in it, and applies patterns before and
after for preparation and clean-up.

This rename was discussed first
[here](https://reviews.llvm.org/D157774).

The PR also adapts and cleans ups the tablegen description of the
`VectorizeChildrenAndApplyPatternsOp` (formerly `VectorizeOp`).
2023-09-21 15:38:29 +02:00
Oleksandr "Alex" Zinenko
a2a1dbb518 [mlir] avoid crash in transform.sequence verifier (#66756)
The verifier was unconditionally accessing the body block terminator,
but it's not guaranteed that the block has one in general.
2023-09-19 13:28:53 +02:00
Ingo Müller
68033aaac5 [mlir][transform] Fix crash in transform.get_parent_op. (#66492)
The previous implementation crashed if run on a `builtin.module` using
an `op_name` filter (because the initial value of `parent` in the while
loop was a `nullptr`). This PR fixes the crash and adds a test.
2023-09-15 21:32:17 +02:00
Matthias Springer
4f63252d5d [mlir][transform] Fix crash when op is erased during transform.foreach (#66357)
Fixes a crash when an op, that is mapped to handle that a
`transform.foreach` iterates over, was erased (through the
`TrackingRewriter`). Erasing an op removes it from all mappings and
invalidates iterators. This is already taken care of when an op is
iterating over payload ops in its `apply` method, but not when another
transform op is erasing a tracked payload op.
2023-09-14 14:59:36 +02:00
Matthias Springer
2f8690b1e2 [mlir][transform] ApplyRegisteredPassOp: Support pass pipelines
The same transform op can now be used to apply registered pass pipelines.

This revision also adds a helper function for querying `PassPipelineInfo` objects and moves the corresponding `lookup` function for `PassInfo` objects to the `PassInfo` class.

Differential Revision: https://reviews.llvm.org/D159211
2023-09-04 15:11:24 +02:00
Matthias Springer
6ebeecf452 [mlir][transform] ApplyConversionPatternsOp: Make conversion target optional
A conversion target is not needed. In a partial dialect conversion, ops are rewritten when possible. The dialect conversion succeeds if there are no illegal ops in the resulting IR.

Differential Revision: https://reviews.llvm.org/D157595
2023-08-10 13:53:25 +02:00
Matthias Springer
1102489891 [mlir][transform] Fix typo in ApplyConversionPatternsOp
Also add extra test cases for partial/full conversion.

Differential Revision: https://reviews.llvm.org/D157594
2023-08-10 13:52:56 +02:00
Mehdi Amini
363b655920 Finish renaming getOperandSegmentSizeAttr() from operand_segment_sizes to operandSegmentSizes
This renaming started with the native ODS support for properties, this is completing it.

A mass automated textual rename seems safe for most codebases.
Drop also the ods prefix to keep the accessors the same as they were before
this change:
 properties.odsOperandSegmentSizes
reverts back to:
 properties.operandSegementSizes

The ODS prefix was creating divergence between all the places and make it harder to
be consistent.

Reviewed By: jpienaar

Differential Revision: https://reviews.llvm.org/D157173
2023-08-09 19:37:01 -07:00
Matthias Springer
0bb4d4d32f [mlir][transform] Add ApplyToLLVMConversionPatternsOp
This op populates conversion patterns by querying the
ConvertToLLVMPatternInterface. Only dialects that support this interface
are supported.

Differential Revision: https://reviews.llvm.org/D157487
2023-08-09 13:44:47 +02:00
Matthias Springer
0cd5e02847 [mlir][transform] Improve assembly format of apply_conversion_patterns
This change was extracted from D157278.

Differential Revision: https://reviews.llvm.org/D157482
2023-08-09 11:07:22 +02:00
Matthias Springer
bcfdb3e4bc [mlir][transform] Add apply_conversion_patterns op
This transform op applies a dialect conversion to the targeted ops. Its design is similar to `apply_patterns`.

Patterns are specified in the first region of `apply_conversion_patterns`. They must implement the `ConversionPatternDescriptorOpInterface`. Regular rewrite patterns and dialect conversion patterns should not be mixed, so the interface is separate from the `PatternDescriptorOpInterface`.

The type converter is specified as the single op of the second region. It is optional; if no type converter is specified, it is expected that pattern descriptors provide their own type converters. If both the pattern descriptors and the `apply_conversion_patterns` op specify a type converter, the type converter of the pattern descriptor is used.

Differential Revision: https://reviews.llvm.org/D157109
2023-08-07 08:49:55 +02:00
Quinn Dawkins
024ae73fa1 [mlir][Transform] Add transform.any_param type
Introduces a generic parameter type intended for transform dialect use
cases that uses/manipulates the underlying attribute (e.g. op
annotation). Includes a disclaimer that mixing parameter based and
attribute based control is discouraged.

Differential Revision: https://reviews.llvm.org/D155980
2023-07-27 14:10:29 -04:00
Matthias Springer
c2d5d348a8 [mlir][transform] Add transform.apply_dce op
Add a transform that eliminates dead operations. This is useful after certain transforms (such as fusion) that create/clone new IR but leave the original IR in place.

Differential Revision: https://reviews.llvm.org/D155954
2023-07-22 08:25:02 +02:00
Matthias Springer
20245ed4de [mlir][transform] Add apply_cse option to transform.apply_patterns op
Applying the canonicalizer and CSE in an interleaved fashion is useful after bufferization (and maybe other transforms) to fold away self copies.

Differential Revision: https://reviews.llvm.org/D155933
2023-07-21 15:13:56 +02:00
Matthias Springer
894fdbc719 [mlir][transform] Add transform.select op
This transform op can be used to select all payload ops with a given name from a handle.

Differential Revision: https://reviews.llvm.org/D154956
2023-07-11 16:16:56 +02:00
Matthias Springer
7dfcd4b7c9 [mlir][transform] Add VerifyOp
This transform op runs the verifier on the targeted payload ops. It is for debugging only.

Differential Revision: https://reviews.llvm.org/D154711
2023-07-07 15:30:32 +02:00