Commit Graph

467 Commits

Author SHA1 Message Date
Matthias Springer
a4c470555b [mlir][linalg] Fix builder API usage in RegionBuilderHelper (#87451)
Operations must be created with the supplied builder. Otherwise, the
dialect conversion / greedy pattern rewrite driver can break.

This commit fixes a crash in the dialect conversion:
```
within split at llvm-project/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalid.mlir:1 offset :8:8: error: failed to legalize operation 'tosa.add'
  %0 = tosa.add %1, %arg2 : (tensor<10x10xf32>, tensor<*xf32>) -> tensor<*xf32>
       ^
within split at llvm-project/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalid.mlir:1 offset :8:8: note: see current operation: %9 = "tosa.add"(%8, %arg2) : (tensor<10x10xf32>, tensor<*xf32>) -> tensor<*xf32>
mlir-opt: llvm-project/mlir/include/mlir/IR/UseDefLists.h:198: mlir::IRObjectWithUseList<mlir::OpOperand>::~IRObjectWithUseList() [OperandType = mlir::OpOperand]: Assertion `use_empty() && "Cannot destroy a value that still has uses!"' failed.
```

This commit is the proper fix for #87297 (which was reverted).
2024-04-04 11:17:59 +09: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
Quinn Dawkins
205dce6029 [mlir][linalg] Add a folder for transpose(fill) -> fill (#83623)
This is similar to the existing folder for a linalg.copy. Transposing a
filled tensor is the same as filling the destination of the transpose.
2024-03-02 17:47:16 -05:00
Diego Caballero
b9a071dc39 [mlir][Linalg] Add folders for linalg.transpose (#81709)
This PR adds folders for linalg transpose ops with only one dimension or
an identity permutation. The folding removes the `linalg.transpose` and
just propagates the input tensor.
2024-02-20 17:39:58 -08:00
srcarroll
50b8a3c01c [mlir][linalg] Refactor EraseIdentityGenericOp to be reused by other LinalgOps (#80466)
This refactored pattern rewrite is intended to be reused by any
`LinalgOp`'s canonicalization pattern for removing identity ops.
Additionally, this canonicalization has been applied to `BroadCastOp`.
2024-02-12 12:53:17 -06:00
Mehdi Amini
5a467b84c4 Apply clang-tidy fixes for llvm-else-after-return in LinalgOps.cpp (NFC) 2024-01-18 16:39:20 -08:00
MaheshRavishankar
974ded9725 [mlir][Linalg] Change linalg.transpose to use the output indexing map as identity. (#77951)
This makes it consistent with how other linalg operations represent
indexing maps.
2024-01-12 14:17:51 -08:00
Matthias Springer
0a8e3dd432 [mlir][Interfaces] DestinationStyleOpInterface: Rename hasTensor/BufferSemantics (#77574)
Rename interface functions as follows:
* `hasTensorSemantics` -> `hasPureTensorSemantics`
* `hasBufferSemantics` -> `hasPureBufferSemantics`

These two functions return "true" if the op has tensor/buffer operands
but not buffer/tensor operands.

Also drop the "ranked" part from the interface, i.e., do not distinguish
between ranked/unranked types.

The new function names describe the functions more accurately. They also
align their semantics with the notion of "tensor semantics" with the
bufferization framework. (An op is supposed to be bufferized if it has
tensor operands, and we don't care if it also has memref operands.)

This change is in preparation of #75273, which adds
`BufferizableOpInterface::hasTensorSemantics`. By renaming the functions
in the `DestinationStyleOpInterface`, we can avoid name clashes between
the two interfaces.
2024-01-12 10:02:54 +01:00
Matthias Springer
9b21866fea [mlir][linalg] Fix invalid IR in FoldInsertPadIntoFill (#74418)
`FoldInsertPadIntoFill` used to generate an invalid
`tensor.insert_slice` op:
```
error: expected type to be 'tensor<?x?x?xf32>' or a rank-reduced version. (size mismatch)
```

This commit fixes tests such as
`mlir/test/Dialect/Linalg/canonicalize.mlir` when verifying the IR after
each pattern application (#74270).
2023-12-19 14:17:54 +09:00
Vivian
fc74db466b [mlir][Linalg] Fix foldFillPackIntoFillOp to work for general cases (#74148) 2023-12-01 16:36:03 -08:00
MaheshRavishankar
42cd9aeec2 Fold linalg.fill -> linalg.copy (#72920) 2023-11-21 06:20:34 -08:00
long.chen
1609f1c2a5 [mlir][affine][nfc] cleanup deprecated T.cast style functions (#71269)
detail see the docment: https://mlir.llvm.org/deprecation/

Not all changes are made manually, most of them are made through a clang
tool I wrote https://github.com/lipracer/cpp-refactor.
2023-11-14 13:01:19 +08:00
Nicolas Vasilache
a0989a7af3 [mlir][Linalg] Fix Linalg behavior in the context of vector elemental… (#71041)
… types
2023-11-02 18:20:59 +01:00
long.chen
5979e1dfb1 [mlir] Fix empty-tensor-elimination around self-copies (#68129)
* Fixes #67977, a crash in `empty-tensor-elimination`.
* Also improves `linalg.copy` canonicalization.
* Also improves indentation indentation in `mlir-linalg-ods-yaml-gen.cpp`.
2023-10-05 12:04:20 +02:00
Abhishek Varma
070d2114b1 [mlir][Linalg] Fix SoftmaxOp's reify result shape calculation (#67790)
-- SoftmaxOp's `reifyResultShapes` function was wrongly casting it as a
`LinalgOp`.
-- This commit thus adds a fix to SoftmaxOp's reify result shape
calculation.

Signed-off-by: Abhishek Varma <abhishek@nod-labs.com>
2023-09-29 10:55:35 -07:00
Matthias Springer
0b2197b0cf [mlir][Interfaces] Clean up DestinationStyleOpInterface (#67015)
* "init" operands are specified with `MutableOperandRange` (which gives
access to the underlying `OpOperand *`). No more magic numbers.
* Remove most interface methods and make them helper functions. Only
`getInitsMutable` should be implemented.
* Provide separate helper functions for accessing mutable/immutable
operands (`OpOperand`/`Value`, in line with #66515): `getInitsMutable`
and `getInits` (same naming convention as auto-generated op accessors).
`getInputOperands` was not renamed because this function cannot return a
`MutableOperandRange` (because the operands are not necessarily
consecutive). `OpOperandVector` is no longer needed.
* The new `getDpsInits`/`getDpsInitsMutable` is more efficient than the
old `getDpsInitOperands` because no `SmallVector` is created. The new
functions return a range of operands.
* Fix a bug in `getDpsInputOperands`: out-of-bounds operands were
potentially returned.
2023-09-21 18:04:08 +02:00
Daniil Dudkin
c46a04339a [mlir][arith] Rename AtomicRMWKind's maxfmaximumf, minfminimumf (#66135)
This patch is part of a larger initiative aimed at fixing floating-point
`max` and `min` operations in MLIR:
https://discourse.llvm.org/t/rfc-fix-floating-point-max-and-min-operations-in-mlir/72671.

This commit renames `maxf` and `minf` enumerators of `AtomicRMWKind`
to better reflect the current naming scheme and the goals of the RFC.
2023-09-14 01:09:37 +03:00
Daniil Dudkin
8a6e54c9b3 [mlir][arith] Rename operations: maxfmaximumf, minfminimumf (#65800)
This patch is part of a larger initiative aimed at fixing floating-point `max` and `min` operations in MLIR: https://discourse.llvm.org/t/rfc-fix-floating-point-max-and-min-operations-in-mlir/72671.

This commit addresses Task 1.2 of the mentioned RFC. By renaming these operations, we align their names with LLVM intrinsics that have corresponding semantics.
2023-09-11 22:02:19 -07:00
MaheshRavishankar
fd8349cdb3 [mlir][Linalg] Move linalg.fill -> linalg.pack pattern into fill canonicalization patterns. (#66002)
This pattern fits better with the other canonicalization patterns that
exist for `linalg.fill`.
2023-09-11 13:41:38 -07:00
Lorenzo Chelini
059da566bc [MLIR][Lianlg] Add memory effects for softmax
Reviewed By: qcolombet

Differential Revision: https://reviews.llvm.org/D157629
2023-08-14 11:42:39 +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
Quentin Colombet
f559e73fad [mlir] Support fast-math friendly constants for identity value
Add an option to the family of `getIdentity` helper functions so that it is
possible to produce fast-math friendly constants.

For instance, for maxf the identity value is `-inf`, however, if the related
operations are lowered with fast-math (`noinf` in particular), then the value
becomes `poison` and chances are the whole codegen is not going to do what we
want.

To avoid this problem, we add an option to `getIdentity` and friends that
specifies whether a finite value needs to be produced or not.

The patch is NFC for all the code but the lowering of `linalg::softmax`
because we know we lower that with fast-math down the line.

I didn't audit the rest of the code to check if it would make sense to set
this boolean in more places.

Note: It feels kind of wrong to have to know what the lowering may do, but
I don't know what the right (at least short-term) solution is. Long term,
we may want a special "neutral element" attribute for the respective ops. I
didn't think too much about the implications for that.

Differential Revision: https://reviews.llvm.org/D156471
2023-08-09 14:22:18 +02:00
Felix Schneider
8184737ce7 [mlir][Linalg] Clarify error message in YieldOp verification NFC
The number of values yielded from a LinalgOp's payload has to match
the number of inits / outs operands of the LinalgOp.
These two numbers got mixed up in the respective error message, this
patch clarifies the message and updates the tests.

Reviewed By: nicolasvasilache, mehdi_amini

Differential Revision: https://reviews.llvm.org/D153124
2023-08-07 22:12:05 -07:00
Surya Jasper
d84450bc51 [mlir][linalg] Canonicalize tensor.extract(linalg.fill)
Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D156008
2023-08-04 09:34:21 -07:00
Lorenzo Chelini
4fb25ca51c [MLIR][Linalg] Preserve DPS when decomposing Softmax
Preserve destination passing style (DPS) when decomposing
`linalg.Softmax`; instead of creating a new empty, which may materialize
as a new buffer after bufferization, use the result directly.

Reviewed By: qcolombet

Differential Revision: https://reviews.llvm.org/D155942
2023-07-21 18:03:26 +02:00
Quentin Colombet
9be8219f60 [mlir][Linalg] Add an interface to decompose complex ops
This patch adds an interface, named AggregatedOpInterface, that decomposes
complex operations into simpler ones.

For now, make the interface specific to Linalg because although the concept
is general, the way to materialize it needs some maturing.

Use that interface with the softmax operator.

Differential Revision: https://reviews.llvm.org/D154363
2023-07-18 19:06:36 +02:00
Nicolas Vasilache
39427a4fbb [mlir][Linalg] Fold/erase self-copy linalg.copy on buffers
Differential Revision: https://reviews.llvm.org/D155203
2023-07-13 16:38:02 +02:00
Renato Golin
5861b4c6de [MLIR][Linalg] Add more arith named ops to linalg (take 2)
Re-apply eda47fdd25 after implementing __truediv__ for TensorUse.

[MLIR][Linalg] Add more arith named ops to linalg

Following up the 'add' named op, here are the remaining basic arithmetic
and maths, including a 'div_unsigned' for integer unsigned values. In the
same pattern as 'matmul_unsigned', the simply named 'div' assumes signed
values and the '_unsigned' variation handles the unsigned values.

It's a bit odd, but there doesn't seem to be a easy way to restrict to
specific types to make 'div_unsigned' only work with integers in the
structured ops framework.

Same as 'add', these have strict semantics regarding casts.

Unary math ops will need some massaging, so I split these ones for now
as I continue working on them.

Differential Revision: https://reviews.llvm.org/D154524
2023-07-06 13:58:37 +01:00
Renato Golin
93d038a0ea Revert "[MLIR][Linalg] Add more arith named ops to linalg"
This reverts commit eda47fdd25.

It failed on NVidia, AMD and Windows bots. Investigating.
2023-07-05 22:02:23 +01:00
Renato Golin
eda47fdd25 [MLIR][Linalg] Add more arith named ops to linalg
Following up the 'add' named op, here are the remaining basic arithmetic
and maths, including a 'div_unsigned' for integer unsigned values. In the
same pattern as 'matmul_unsigned', the simply named 'div' assumes signed
values and the '_unsigned' variation handles the unsigned values.

It's a bit odd, but there doesn't seem to be a easy way to restrict to
specific types to make 'div_unsigned' only work with integers in the
structured ops framework.

Same as 'add', these have strict semantics regarding casts.

Unary math ops will need some massaging, so I split these ones for now
as I continue working on them.

Differential Revision: https://reviews.llvm.org/D154524
2023-07-05 19:29:56 +01:00
Quentin Colombet
cf244b55c3 [mlir][Linalg] Remove useless include
This include was not needed in the end and creates a creating circular
dependencies (linalg <-> linalg/util).

Just remove it.

NFC
2023-07-03 12:47:22 +02:00
Quentin Colombet
273600ccea [mlir][Linalg] Implement the tiling interface for softmax
Original implementation from Harsh Menon <harsh@nod-labs.com>.

Differential Revision: https://reviews.llvm.org/D153421
2023-07-03 10:49:16 +02:00
Matthias Springer
be6d96e9f8 [mlir][linalg] Remove redundant dimension size helper functions
Differential Revision: https://reviews.llvm.org/D154211
2023-07-03 09:07:18 +02:00
Quentin Colombet
2d9b9103b4 [mlir][Linalg] Add a softmax op
This patch adds a softmax op.
For now, nothing interesting happens, we can only do a round trip.
Later patches will add the tiling interface and the lowering of this op to
a sequence of simpler ops.

This is graduating the linag_ext.softmax op from iree to LLVM.

Original implementation from Harsh Menon <harsh@nod-labs.com>
Nicolas Vasilache <nicolas.vasilache@gmail.com> co-authored this patch.

Differential Revision: https://reviews.llvm.org/D153422
2023-06-29 12:57:06 +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
Kazu Hirata
6b451c8012 [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-05-06 08:56:25 -07:00
Mehdi Amini
f6ac7e3c6d Adopt Properties to store operations inherent Attributes in the Linalg dialect
This is part of an on-going migration to adopt Properties inside MLIR.

Differential Revision: https://reviews.llvm.org/D148299
2023-05-05 16:58:20 -07:00
Rahul Kayaith
6089d612a5 [mlir] Prevent implicit downcasting to interfaces
Currently conversions to interfaces may happen implicitly (e.g.
`Attribute -> TypedAttr`), failing a runtime assert if the interface
isn't actually implemented. This change marks the `Interface(ValueT)`
constructor as explicit so that a cast is required.

Where it was straightforward to I adjusted code to not require casts,
otherwise I just made them explicit.

Depends on D148491, D148492

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D148493
2023-04-20 16:31:54 -04:00
Rahul Kayaith
00e3566d6c [mlir][arith] Add arith.constant materialization helper
This adds `arith::ConstantOp::materialize`, which builds a constant from
an attribute and type only if it would result in a valid op. This is
useful for dialect `materializeConstant` hooks, and allows for removing
the previous `Attribute, Type` builder which was only used during
materialization.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D148491
2023-04-20 16:31:52 -04:00
Matthias Springer
4c48f016ef [mlir][Affine][NFC] Wrap dialect in "affine" namespace
This cleanup aligns the affine dialect with all the other dialects.

Differential Revision: https://reviews.llvm.org/D148687
2023-04-20 11:19:21 +09:00
Matthias Springer
eb7f355725 [mlir][NFC] Minor cleanups around ShapedType
* Remove unnecessary casts.
* Use concrete shaped types (e.g., `MemRefType`, `RankedTensorType`) instead of `ShapedType` when possible.
* Minor documentation cleanups.

Differential Revision: https://reviews.llvm.org/D148488
2023-04-19 11:30:45 +09:00
Alex Zinenko
07b52b52be [mlir] fix a crash in linalg.generic parser
Report an error when the `iterator_types` attribute is missing.

Reviewed By: nicolasvasilache

Differential Revision: https://reviews.llvm.org/D148015
2023-04-11 15:35:58 +00:00
Amy Wang
6ac632ad83 [MLIR][Linalg] Generate unique LibraryCallName for LinalgOps.
When lowering LinalgToStandard for named UnaryFn/BinaryFn ops, ensure
the fun name appears in the generated library name. Further, for
linalg.copy to/from different address spaces, ensure the to/from
address spaces are appended onto the library name for uniqueness.
This fixes the lowering error with the linalg.copy testcase shown in
this patch.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D145467
2023-03-20 13:50:22 -04:00
Kazu Hirata
ce14f7b18f [mlir] Use Use *{Set,Map}::contains (NFC) 2023-03-14 21:48:49 -07:00
Matthias Springer
758329dc7c [mlir][NFC] reifyResultShapes: Add extra error checking
This change adds a new helper function `mlir::reifyResultShapes` that calls the corresponding interface method and also checks the result produced by the implementation when running in debug mode. Bugs due to incorrect interface implementations can be difficult to debug.

This helper function also reduces the amount of code needed at call sites: the cast to `ReifyRankedShapedTypeOpInterface` is done in the helper function.

Differential Revision: https://reviews.llvm.org/D145777
2023-03-10 11:37:54 +01:00
Matthias Springer
2a5b13e722 [mlir][Interfaces] ReifyRankedShapedTypeOpInterface returns OpFoldResults
`reifyResultShapes` now returns `OpFoldResult`s instead of `Value`s. This is often more efficient because many transformations immediately attempt to extract a constant from the reified values.

Differential Revision: https://reviews.llvm.org/D145250
2023-03-06 08:41:28 +01:00
Tom Eccles
b0d575310f [mlir][Linalg] Fix ignoring nodiscard return value
ff94419a28 changed the return value of appendMangledType() to
LogicalResult, which is marked as nodiscard. Ignoring the result
generates a warning when building with clang.

Reviewed By: nicolasvasilache, chelini

Differential Revision: https://reviews.llvm.org/D142202
2023-01-20 04:51:21 -08:00
Nicolas Vasilache
ff94419a28 [mlir][Linalg] Fix crash in LinalgToStandard
Properly handle `appendMangledType` failure instead of asserting.

Fixes #59986.
2023-01-20 00:17:55 -08:00
Kazu Hirata
0a81ace004 [mlir] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to remove #include "llvm/ADT/Optional.h".

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-01-14 01:25:58 -08:00
Kazu Hirata
a1fe1f5f77 [mlir] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

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-01-13 21:05:06 -08:00