Commit Graph

99 Commits

Author SHA1 Message Date
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
Alex Zinenko
dd81c6b8d3 [mlir] integration tests for transform dialect matchers
Add integration tests exercising transform dialect matchers for slightly
larger compositions of structured ops, namely reductions and matrix
multiplications with optional leading and trailing elementwise
operations.

Reviewed By: qcolombet

Differential Revision: https://reviews.llvm.org/D154440
2023-07-05 10:43:30 +00:00
Matthias Springer
4106557a28 [mlir][transform] Improve transform.get_closest_isolated_parent
* Rename op to `transform.get_parent_op`
* Match parents by "is isolated from above" and/or op name, or just the direct parent.
* Deduplication of result payload ops is optional.

Differential Revision: https://reviews.llvm.org/D154071
2023-07-04 16:23:08 +02:00
Matthias Springer
fa1a23a720 [mlir][transform] Add transform.apply_licm op
This op applies loop-invariant code motion to the targeted loop-like op.

Differential Revision: https://reviews.llvm.org/D154327
2023-07-03 15:28:53 +02:00
Matthias Springer
2c95ede4d1 [mlir][transform] Add transform.apply_cse op
This op applies CSE to the targeted op. This is similar to `transform.apply_registered_pass "cse"`, but it retains handles in the body of the targeted op.

Differential Revision: https://reviews.llvm.org/D154099
2023-07-03 08:50:50 +02:00
Matthias Springer
22259281c0 [mlir][transform][NFC] Store all Mappings in region stack
Do not swap the Mappings when entering a region that is isolated from above. Simply push another Mappings struct to the stack and prevent invalid accesses during lookups.

Differential Revision: https://reviews.llvm.org/D153765
2023-06-26 17:35:26 +02:00
Alex Zinenko
135e5bf894 [mlir][transform] don't wrap a warning into silenceable failure
Wrapping a warning into a silenceable failure will result in the warning
being interpreted as an error, which it is not.

Reviewed By: nicolasvasilache

Differential Revision: https://reviews.llvm.org/D153546
2023-06-23 08:20:53 +00:00
Quinn Dawkins
193803968f [mlir][Transform] Allow parameter and value types in merge_handles op
Similar to operation handles, merging handles for other types can be useful to
avoid repetition of common transformations across a set of parameters.
For example, forming a list of static values for comparison rather than
comparing the parameters one at a time.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D153240
2023-06-20 13:54:38 -04:00
Matthias Springer
c63d2b2c71 [mlir][transform] Add TransformRewriter
All `apply` functions now have a `TransformRewriter &` parameter. This rewriter should be used to modify the IR. It has a `TrackingListener` attached and updates the internal handle-payload mappings based on rewrites.

Implementations no longer need to create their own `TrackingListener` and `IRRewriter`. Error checking is integrated into `applyTransform`. Tracking listener errors are reported only for ops with the `ReportTrackingListenerFailuresOpTrait` trait attached, allowing for a gradual migration. Furthermore, errors can be silenced with an op attribute.

Additional API will be added to `TransformRewriter` in subsequent revisions. This revision just adds an "empty" `TransformRewriter` class and updates all `apply` implementations.

Differential Revision: https://reviews.llvm.org/D152427
2023-06-20 10:49:59 +02:00
Matthias Springer
18ec203084 [mlir][transform] Add ApplyRegisteredPassOp transform op
This transform op runs a pass on the target op.

Differential Revision: https://reviews.llvm.org/D153143
2023-06-20 08:55:22 +02:00
Matthias Springer
726d076784 [mlir][transform] ApplyPatternsOp: Add check to prevent modifying the transform IR
Add an extra check to make sure that transform IR is not getting modified by this op while it is being interpreted. This generally dangerous and we may want to enforce this for all transform ops that modify the payload in the future.

Users should generally try to apply patterns only to the piece of IR where it is needed (e.g., a matched function) and not the entire module (which may contain the transform IR).

This revision is in response to a crash in a downstream compiler that was caused by a dead `transform.structured.match` op that was removed by the GreedyPatternRewriteDriver's DCE while the enclosing sequence was being interpreted.

Differential Revision: https://reviews.llvm.org/D153113
2023-06-19 08:58:40 +02:00
Matthias Springer
ebf4ab1dc3 [mlir][transform][NFC] Move TrackingListener to TransformInterfaces.h
A TransformRewriter (with attached TrackingListener) will be added to an interface method in a subsequent revision.

Differential Revision: https://reviews.llvm.org/D152426
2023-06-09 11:52:49 +02:00
Matthias Springer
572b171fb5 [mlir][transform] TrackingListener: Distinguish between failure and "should be dropped"
When looking for replacement ops (`findReplacementOp`) distinguish between "no replacement could be found" and "this op should be dropped from the mapping". The latter case will be utilized in a subsequent revision when a payload op is mapped to a consumed handle.

Differential Revision: https://reviews.llvm.org/D152375
2023-06-09 11:40:32 +02:00
Matthias Springer
223a0f6374 [mlir][transform] Add builder to ApplyPatternsOp
Add a builder that takes a callback to construct the body of the op.

Differential Revision: https://reviews.llvm.org/D152352
2023-06-07 12:06:55 +02:00
Matthias Springer
cc7f52432b [mlir][transform] Use separate ops instead of PatternRegistry
* Remove `transform::PatternRegistry`.
* Add a new op for each currently registered pattern set.
* Change names of vector dialect pattern selector ops, so that they are consistent with the remaining code base.
* Remove redundant `transform.vector.extract_address_computations` op.

Differential Revision: https://reviews.llvm.org/D152249
2023-06-06 11:53:03 +02:00
Matthias Springer
976d25ed9e [mlir][vector] Use transform.apply_patterns in vector tests
All vector transform ops are now `PatternDescriptorOpInterface` ops that merely select the patterns. The patterns are applied by the `apply_patterns` op. This is to ensure that ops are properly tracked. (TrackingListener is used in the implementation of `apply_patterns`.) Furthermore, handles are no longer invalidated when applying patterns in the vector tests.

Differential Revision: https://reviews.llvm.org/D152174
2023-06-06 09:27:05 +02:00
Matthias Springer
5a10f207cc [mlir][transform] Add region to ApplyPatternsOp
Patterns should be selected by adding ops that implement `PatternDescriptorOpInterface` to the region of `apply_pattern` ops. Such ops can have operands, allowing for pattern parameterization. The existing way of selecting patterns from the PatternRegistry is deprecated.

Differential Revision: https://reviews.llvm.org/D152167
2023-06-06 09:09:41 +02:00
Matthias Springer
60f06bc5bb [mlir][transform] ApplyPatternsOp: Register canonicalization patterns
Also support replacing payload ops with ConstantLike ops in the TrackingListener, even if the replacement op does not have the same name. (Not supported for ops with multiple results, as this would require splitting the handle.)

Differential Revision: https://reviews.llvm.org/D152127
2023-06-05 11:37:43 +02:00
Jie Fu
cd8cdbd915 [mlir] Fix unqualified call to 'std::move' in TransformOps.cpp
/data/llvm-project/mlir/lib/Dialect/Transform/IR/TransformOps.cpp:230:35: error: unqualified call to 'std::move' [-Werror,-Wunqualified-std-cast-call]
  patterns.try_emplace(attr, [f = move(fn)](RewritePatternSet &patternSet) {
                                  ^
                                  std::
1 error generated.
2023-06-02 22:06:46 +08:00
Matthias Springer
4b48063b52 [mlir][vector][transform] Register vector dialect patterns
Differential Revision: https://reviews.llvm.org/D151983
2023-06-02 15:59:56 +02:00
Matthias Springer
0b52fa900a [mlir][transform] Add ApplyPatternsOp and PatternRegistry
Add a new transform op that applies patterns to a targeted payload op. Patterns can be registered by transform dialect extensions in a pattern registry.

Differential Revision: https://reviews.llvm.org/D151983
2023-06-02 14:58:20 +02:00
Matthias Springer
000bc58b63 [mlir][transform] Utilize op interface instead of tensor::TrackingListener
Add a new interface `FindPayloadReplacementOpInterface` to specify ops that should be skipped when looking for payload replacement ops. Such ops are typically metadata-only ops.

With this change, we no longer need to maintain a custom TrackingListener in the tensor dialect.

Note: `CastOpInterface` by itself is not sufficient. Some metadata-only ops such as "tensor.reshape" are not casts, and it would be incorrect for them to implement the `CastOpInterface`.

Differential Revision: https://reviews.llvm.org/D151888
2023-06-02 14:50:43 +02:00
Quinn Dawkins
5e7ac2503a [mlir][transform] Add op for adding attributes to payload IR
The ability to add attributes to payload IR is useful functionality
independent of any dialect. This is added here through `transform.annotate`
by enabling attributes tied to a `TransformParamTypeInterface` (which
internally refers to an Attribute) to be added to a target operation by
name.

The AnnotateOp does not produce a new handle as no existing handles
should be affected by adding an attribute. Existing attributes on
the payload with the same name will be overwritten.

Differential Revision: https://reviews.llvm.org/D151689
2023-05-30 11:46:18 -04:00
Alex Zinenko
94d608d410 [mlir] move PDL-related transform ops into an extension
The initial bring-up of the Transform dialect relied on PDL to provide
the default handle type (`!pdl.operation`) and the matching capability.
Both are now provided natively by the Transform dialect removing the
reason to have a hard dependency on the PDL dialect and its interpreter.
Move PDL-related transform operations into a separate extension.

This requires us to introduce a dialect state extension mechanism into
the Transform dialect so it no longer needs to know about PDL constraint
functions that may be injected by extensions similarly to operations and
types. This mechanism will be reused to connect pattern application
drivers and the Transform dialect.

This completes the restructuring of the Transform dialect to remove
overrilance on PDL.

Note to downstreams: flow that are using `!pdl.operation` with Transform
dialect operations will now require `transform::PDLExtension` to be
applied to the transform dialect in order to provide the transform
handle type interface for `!pdl.operation`.

Reviewed By: springerm

Differential Revision: https://reviews.llvm.org/D151104
2023-05-24 12:25:06 +00:00
Matthias Springer
0e37ef08d4 [mlir][transform] Use TrackingListener-aware iterator for getPayloadOps
Instead of returning an `ArrayRef<Operation *>`, return at iterator that skips ops that were erased/replaced while iterating over the payload ops.

This fixes an issue in conjuction with TrackingListener, where a tracked op was erased during the iteration. Elements may not be removed from an array while iterating over it; this invalidates the iterator.

When ops are erased/removed via `replacePayloadOp`, they are not immediately removed from the mappings data structure. Instead, they are set to `nullptr`. `nullptr`s are not enumerated by `getPayloadOps`. At the end of each transformation, `nullptr`s are removed from the mapping data structure.

Differential Revision: https://reviews.llvm.org/D149847
2023-05-15 10:31:24 +02:00
Matthias Springer
7d436d56b6 [mlir][transform] TrackingListener: Allow existing ops as replacements
The TrackingListener was unnecessarily strict. Existing ops are now allowed when updating payload ops mappings due to `replaceOp` in the TrackingListener.

Differential Revision: https://reviews.llvm.org/D150429
2023-05-12 15:07:20 +02:00
Tres Popp
c1fa60b4cd [mlir] Update method cast calls to function calls
The MLIR classes Type/Attribute/Operation/Op/Value support
cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast
functionality in addition to defining methods with the same name.
This change begins the migration of uses of the method to the
corresponding function call as has been decided as more consistent.

Note that there still exist classes that only define methods directly,
such as AffineExpr, and this does not include work currently to support
a functional cast/isa call.

Context:

* https://mlir.llvm.org/deprecation/ at "Use the free function variants for dyn_cast/cast/isa/…"
* Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443

Implementation:
This follows a previous patch that updated calls
`op.cast<T>()-> cast<T>(op)`. However some cases could not handle an
unprefixed `cast` call due to occurrences of variables named cast, or
occurring inside of class definitions which would resolve to the method.
All C++ files that did not work automatically with `cast<T>()` are
updated here to `llvm::cast` and similar with the intention that they
can be easily updated after the methods are removed through a
find-replace.

See https://github.com/llvm/llvm-project/compare/main...tpopp:llvm-project:tidy-cast-check
for the clang-tidy check that is used and then update printed
occurrences of the function to include `llvm::` before.

One can then run the following:
```
ninja -C $BUILD_DIR clang-tidy

run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\
                 -export-fixes /tmp/cast/casts.yaml mlir/*\
                 -header-filter=mlir/ -fix

rm -rf $BUILD_DIR/tools/mlir/**/*.inc
```

Differential Revision: https://reviews.llvm.org/D150348
2023-05-12 11:21:30 +02:00
Matthias Springer
709098fb38 [mlir][transform] SplitHandleOp: add additional distribution options
Add options to handle cases where there are not enough or too many payload ops mapped to the given handle.

Differential Revision: https://reviews.llvm.org/D149955
2023-05-09 11:38:18 +02:00
Matthias Springer
288529e730 [mlir][transform] Clean up SplitHandlesOp
* Rename to `SplitHandleOp`: it splits a single handle.
* Drop `num_result_handles` attribute: it is redundant and can be inferred from the number of results.
* Improve documentation and minor code cleanups.

Differential Revision: https://reviews.llvm.org/D149937
2023-05-05 22:29:43 +09:00
Alex Zinenko
30ce0c9508 [mlir] properly handle an edge case in transform.split_handles
The `transform.split_handles` transform op has an edge case that is
supposed to propagate empty handle state to all resulting handles
regardless of their number. Actually set empty payload for them instead
of leaving them unset.

Reviewed By: springerm

Differential Revision: https://reviews.llvm.org/D149652
2023-05-05 12:48:20 +00:00
Alex Zinenko
3fe7127d48 [mlir] add structured (Linalg) transform op matchers
Add a set of transform operations into the "structured" extension of the
Transform dialect that allow one to select transformation targets more
specifically than the currently available matching. In particular, add
the mechanism for identifying the producers of operands (input and init
in destination-passing style) and users of results, as well as
mechanisms for reasoning about the shape of the iteration space.

Additionally, add several transform operations to manipulate parameters
that could be useful to implement more advanced selectors. Specifically,
new operations let one produce and compare parameter values to implement
shape-driven transformations.

New operations are placed in separate files to decrease compilation
time. Some relayering of the extension is necessary to avoid repeated
generation of enums.

Depends on D148013
Depends on D148014
Depends on D148015

Reviewed By: chelini

Differential Revision: https://reviews.llvm.org/D148017
2023-04-13 12:37:51 +00:00
Alex Zinenko
63c9d2b14e [mlir] Add transform.foreach_match
Add a new transform op combinator that implements an "if-then-else"
style of mechanism for applying transformations. Its main purpose is to
serve as a higher-level driver when applying multiple transform scripts
to potentially overlapping pieces of the payload IR. This is similar to
how the various rewrite drivers operate in C++, but at a higher level
and with more declarative expressions. This is not intended to replace
existing pattern-based rewrites, but to to drive more complex
transformations that are exposed in the transform dialect and are too
complex to be expressed as simple declarative rewrites.

Reviewed By: springerm

Differential Revision: https://reviews.llvm.org/D148013
2023-04-12 11:01:38 +00:00
Matthias Springer
905e932441 [mlir][transform] TrackingListener: Drop mappings of tracked ops when all handles are dead
No replacement ops are needed for tracked ops who's handles are all dead.

Differential Revision: https://reviews.llvm.org/D147510
2023-04-12 15:56:22 +09:00
Matthias Springer
07fef178e8 [mlir][transform] Better debugging facilites for invalid API usage
This revision adds additional "expensive-checks" checks to the transform dialect that detect the most common cases of:

* Missing `consumesHandle` side effects on transform ops.
* Patterns that remove operations but do not notify the transform dialect.

In essence, these additional checks are looking for dangling pointers to erased payload ops in the transform dialect state and crash the program execution (by dereferencing free'd memory) or triggering an assertion failure. It is recommended to run these extra checks with ASAN. Otherwise, certain failures may not be detected. The ASAN error message can also be used to find the faulty transform op/pattern.

This change also fixes a few faulty transform ops.

Differential Revision: https://reviews.llvm.org/D147447
2023-04-12 15:49:28 +09:00
Matthias Springer
ea2d2d10ee [mlir][transform] Dump match failures in TrackingListener
Differential Revision: https://reviews.llvm.org/D147997
2023-04-12 13:04:56 +09:00
Alex Zinenko
4110934120 [mlir] add readonly/consume annotations to transform named sequences
Use the argument attribute mechanism for function-like operations to
annotate the arguments of named transform sequences as consuming or only
reading the handles passed as arguments. This makes it possible to
correctly specify handle invalidation for external named sequences by
requiring their declarations to always provide such annotations.
Additionally, these annotations remove the need to analyze the body of
a named sequence to understand its effects on the arguments. Make them
required for named sequences that are called from the same file, in
addition to external sequences.

Provide a convenience pass that infers annotations by analyzing bodies
of named sequences provided they are not called from the same file.

Reviewed By: springerm

Differential Revision: https://reviews.llvm.org/D147223
2023-04-04 09:38:00 +00:00
Matthias Springer
244a4e75ae [mlir][transform] Add optional error checking to TrackingListener
Derived classes can implement `notifyPayloadReplacementNotFound` for custom error checking.

Differential Revision: https://reviews.llvm.org/D147206
2023-03-30 11:10:38 +02:00
Matthias Springer
5d9e858ae6 [mlir][transform] Add TrackingListener
This change makes it possible to use a greedy pattern rewrite as part of a transform op, even if the transform op does not invalidate the target handle (in particular transform ops without `FunctionalStyleTransformOpTrait`) and the targeted op is not isolated from above.

The listener API allows us to track replacements of ops with values, but not ops with ops. Therefore, the TrackingListener is conservative: If an op is replaced with values that all have the same defining op and the defining op is of the same type as the original op, it is safe to assume that the op was replaced with an equivalent op. Otherwise, the op mapping is dropped. When this is not good enough, transforms can track values instead or provide a custom `findReplacementOp` function.

Differential Revision: https://reviews.llvm.org/D147039
2023-03-29 11:05:04 +02:00
Alex Zinenko
92c6946840 [mlir] support external named transform libraries
Introduce support for external definitions of named sequences in the
transform dialect by letting the TransformInterpreterPassBase read a
"library" MLIR file. This file is expected to contain definitions for
named sequences that are only declared in the main transformation
script. This allows for sharing non-trivial transform combinations
without duplication.

This patch provides only the minimal plumbing for a single textual IR
file. Further changes are possible to support multiple libraries and
bytecode files.

Reviewed By: nicolasvasilache

Differential Revision: https://reviews.llvm.org/D146961
2023-03-28 09:47:19 +00:00
Alex Zinenko
fb409a2822 [mlir] Transform dialect: add named sequences
Named sequences introduce an additional abstraction and reuse capability
to the transform dialect. They can be though of as macros parameterized
with handles that can be invoked in places where a transform dialect
operation is expected. Such reuse was previously not possible in the
dialect and required dynamic construction of the transform IR from the
client language. Named sequences are intentionally restricted to
disallow recursion, as it could make the dialect accidentally
Turing-complete, which isn't desired at this point.

Reviewed By: springerm

Differential Revision: https://reviews.llvm.org/D146433
2023-03-21 14:53:54 +00:00
Nicolas Vasilache
1cff4cbda3 [mlir][Transform] NFC - Various API cleanups and use RewriterBase in lieu of PatternRewriter
Depends on: D145685

Differential Revision: https://reviews.llvm.org/D145977
2023-03-14 04:23:12 -07:00
Nicolas Vasilache
cf2d80b9a2 [mlir][Transform] Fix transform::SequenceOp builder
Previously, the wrond type would be used and would not matche the number of locations triggering
an assertion failures in `createBlock`.
2023-03-02 01:11:08 -08:00
Adrian Kuegel
01b9d355d5 [mlir] Use the same name as the generated parameter name (NFC).
When commenting for which parameter a value is passed, the same name
should be used as is used for the real parameter. In this case, the
parameter name is generated from the TransformOps.td file.
2023-02-27 14:01:20 +01:00
Matthias Springer
4cf936d01f [mlir][transform] Add transform.get_defining_op op
This op is the inverse of `transform.get_result`.

Differential Revision: https://reviews.llvm.org/D144409
2023-02-21 09:03:25 +01:00
Matthias Springer
3ef062a4bd [mlir][transform] Add transform.get_result op
This transform op returns a value handle pointing to the specified OpResult of the targeted op.

Differential Revision: https://reviews.llvm.org/D144087
2023-02-15 14:16:50 +01:00
Kazu Hirata
05423905d0 [mlir] Use std::optional instead of llvm::Optional (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-02-14 20:33:36 -08:00
Kohei Yamaguchi
749f7f918d [mlir][transform] Fix apply WithPDLPatternsOp with non-pattern op
Fix https://github.com/llvm/llvm-project/issues/60209

Fix crash with segmentation fault when transform::WithPDLPatternsOp is
applied with non-pattern op. Added check for existing transform ops with
pattern op.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D143474
2023-02-09 10:03:01 +01:00