Commit Graph

125 Commits

Author SHA1 Message Date
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
Nicolas Vasilache
8483d18be5 [mlir][Transform] Relax the applicability of transform.foreach_match to also take into account the op itself 2023-10-30 11:53:04 +00:00
Nicolas Vasilache
0c9358948b [mlir][Transform] NFC - Add a C++ builder for NamedSequenceOp 2023-10-23 16:38:49 +00: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
long.chen
80815dfbd8 [mlir] remove some GCC warning #68409 (#68528) 2023-10-09 22:20:18 -07: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
085075a5e3 [mlir][transform] Check for invalidated iterators on payload values (#66472)
Same as #66369 but for payload values. (#66369 added checks only for
payload operations.)

It was necessary to change the signature of `getPayloadValues` to return
an iterator. This is now similar to payload operations.

Fixes an issue in #66369 where the `LLVM_ENABLE_ABI_BREAKING_CHECKS`
check was inverted.
2023-09-25 15:42:35 +02:00
Jinyun (Joey) Ye
214ce4da35 [MLIR][Transform] Fix PrintOp::build with StringRef (#67052)
transform::PrintOp::build(OpBuilder &builder, OperationState &result,
StringRef name) does not set name correctly. Calling
PrintOp::build(builder, result, "whatever name") is going to end up with
a PrintOp with no name.

This patch fixes it by replicating the approach from tablegen created
code. Refer to
build/mlir/include/mlir/Dialect/Transform/IR/TransformOps.cpp.inc
2023-09-25 09:58:28 +02:00
vic
02981c9635 [MLIR][IR] Rename Block::hasTerminator to mightHaveTerminator (#66870)
This `Block` member function introduced in
87d77d3cfb may be misleading to users as
the last operation in the block might have not been registered, so there
would be no way to ensure that is a terminator.

---------

Signed-off-by: Victor Perez <victor.perez@codeplay.com>
2023-09-21 16:43:25 +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
Christopher Bate
e2d39f799b [mlir][Transform] Add updateConversionTarget to ConversionPatternDescriptorOpInterface
This change adds a method to modify the ConversionTarget used during
`transform.apply_conversion_patterns` to the
`ConversionPatternDescriptorOpInterface`. This is needed when the TypeConverter
is used to dictate the dynamic legality of operations, as in "structural"
conversion patterns present in, for example, the SCF and func dialects.

As a first use case/test, this change also adds a
`transform.apply_patterns.scf.structural_conversions` operation to the SCF
dialect.

Reviewed By: springerm

Differential Revision: https://reviews.llvm.org/D158672
2023-09-14 11:39:47 -06: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
Oleksandr "Alex" Zinenko
e55e36de7a [mlir] alloc-to-alloca conversion for memref (#65335)
Introduce a simple conversion of a memref.alloc/dealloc pair into an
alloca in the same scope. Expose it as a transform op and a pattern.

Allocas typically lower to stack allocations as opposed to alloc/dealloc
that lower to significantly more expensive malloc/free calls. In
addition, this can be combined with allocation hoisting from loops to
further improve performance.
2023-09-05 17:58:22 +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
Martin Erhart
34a35a8b24 [mlir] Move FunctionInterfaces to Interfaces directory and inherit from CallableOpInterface
Functions are always callable operations and thus every operation
implementing the `FunctionOpInterface` also implements the
`CallableOpInterface`. The only exception was the FuncOp in the toy
example. To make implementation of the `FunctionOpInterface` easier,
this commit lets `FunctionOpInterface` inherit from
`CallableOpInterface` and merges some of their methods. More precisely,
the `CallableOpInterface` has methods to get the argument and result
attributes and a method to get the result types of the callable region.
These methods are always implemented the same way as their analogues in
`FunctionOpInterface` and thus this commit moves all the argument and
result attribute handling methods to the callable interface as well as
the methods to get the argument and result types. The
`FuntionOpInterface` then does not have to declare them as well, but
just inherits them from the `CallableOpInterface`.
Adding the inheritance relation also required to move the
`FunctionOpInterface` from the IR directory to the Interfaces directory
since IR should not depend on Interfaces.

Reviewed By: jpienaar, springerm

Differential Revision: https://reviews.llvm.org/D157988
2023-08-31 11:28:23 +00:00
Markus Böck
4dd744ac9c Reland "[mlir] Use a type for representing branch points in RegionBranchOpInterface"
This reverts commit b26bb30b46.
2023-08-30 09:31:54 +02:00
Markus Böck
b26bb30b46 Revert "[mlir] Use a type for representing branch points in RegionBranchOpInterface"
This reverts commit 024f562da6.

Forgot to update flang
2023-08-29 20:17:50 +02:00
Markus Böck
024f562da6 [mlir] Use a type for representing branch points in RegionBranchOpInterface
The current implementation is not very ergonomic or descriptive: It uses `std::optional<unsigned>` where `std::nullopt` represents the parent op and `unsigned` is the region number.
This doesn't give us any useful methods specific to region control flow and makes the code fragile to changes due to now taking the region number into account.

This patch introduces a new type called `RegionBranchPoint`, replacing all uses of `std::optional<unsigned>` in the interface. It can be implicitly constructed from a region or a `RegionSuccessor`, can be compared with a region to check whether the branch point is branching from the parent, adds `isParent` to check whether we are coming from a parent op and adds `RegionSuccessor::parent` as a descriptive way to indicate branching from the parent.

Differential Revision: https://reviews.llvm.org/D159116
2023-08-29 20:02:23 +02:00
Nicolas Vasilache
920c461219 [mlir][Transform] Add support to drive conversions of func to LLVM with TD
This revision adds a `transform.apply_conversion_patterns.func.func_to_llvm` transformation.

It is unclear at this point whether this should be spelled out as a standalone transformation
or whether it should resemble `transform.apply_conversion_patterns.dialect_to_llvm "fun"`.

This is dependent on how we want to handle the type converter creation.
In particular the current implementation exhibits the fact that
`transform.apply_conversion_patterns.memref.memref_to_llvm_type_converter` was not rich enough
and did not match the LowerToLLVMOptions.

Keeping those options in sync across all the passes that lower to LLVM is very error prone.
Instead, we should have a single `to_llvm_type_converter`.

Differential Revision: https://reviews.llvm.org/D157553
2023-08-10 13:17:00 +00: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
Markus Böck
138df29820 [mlir] Revamp RegionBranchOpInterface successor mechanism
The `RegionBranchOpInterface` had a few fundamental issues caused by the API design of `getSuccessorRegions`.

It always required passing values for the `operands` parameter. This is problematic as the operands parameter actually changes meaning depending on which predecessor `index` is referring to. If coming from a region, you'd have to find a `RegionBranchTerminatorOpInterface` in that region, get its operand count, and then create a `SmallVector` of that size.
This is not only inconvenient, but also error-prone, which has lead to a bug in the implementation of a previously existing `getSuccessorRegions` overload.

Additionally, this made the method dual-use, trying to serve two different use-cases: 1) Trying to determine possible control flow edges between regions and 2) Trying to determine the region being branched to based on constant operands.

This patch fixes these issues by changing the interface methods and adding new ones:
* The `operands` argument of `getSuccessorRegions` has been removed. The method is now only responsible for returning possible control flow edges between regions.
* An optional `getEntrySuccessorRegions` method has been added. This is used to determine which regions are branched to from the parent op based on constant operands of the parent op. By default, it calls `getSuccessorRegions`. This is analogous to `getSuccessorForOperands` from `BranchOpInterface`.
* Add `getSuccessorRegions` to `RegionBranchTerminatorOpInterface`. This is used to get the possible successors of the terminator based on constant operands. By default, it calls the containing `RegionBranchOpInterface`s `getSuccessorRegions` method.
* `getSuccessorEntryOperands` was renamed to `getEntrySuccessorOperands` for consistency.

Differential Revision: https://reviews.llvm.org/D157506
2023-08-10 10:27:27 +02:00
Nicolas Vasilache
99475f5b4a [mlir][transform] Add NVGPU to NVVM conversion via transform.apply_conversion_patterns
Differential Revision: https://reviews.llvm.org/D157501
2023-08-09 14:09:57 +00: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
7ec88f06d5 [mlir][memref][transform] Add vector_to_llvm conversion patterns
These patterns are exposed via a new "apply_conversion_patterns" op.

Also provide a new type converter that converts from memref to LLVM types. Conversion patterns that lower to LLVM are special: they require an `LLVMTypeConverter`; a normal `TypeConverter` is not enough. This revision also adds a new interface method to pattern descriptor ops to verify that the default type converter of the enclosing "apply_conversion_patterns" op is compatible with the set of patterns. At the moment, a simple `StringRef` is used. This can evolve to a richer type in the future if needed.

Differential Revision: https://reviews.llvm.org/D157369
2023-08-09 11:27:53 +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
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