26 Commits

Author SHA1 Message Date
Tobias Gysi
7e54ae24d8
[mlir][llvm] Do not inline variadic functions (#77241)
This revision updates the llvm dialect inliner to explicitly disallow
the inlining of variadic functions. Already previously the inlining
failed if the number of function arguments did not match the number of
call arguments. After the change, inlining checks the function is not
variadic and it does not contain a va_start intrinsic.
2024-01-08 08:30:10 +01:00
Tobias Gysi
9971b9ab19
[mlir][llvm] Improve alloca handling during inlining (#75961)
This revision changes the alloca handling in the LLVM inliner.
It ensures that alloca operations, even those nested within a
region operation, can be relocated to the entry block of the function,
or the closest ancestor region that is marked with either the
isolated from above or automatic allocation scope trait.

While the LLVM dialect does not have any region operations,
the inlining interface may be used on IR that mixes different
dialects.
2023-12-21 08:11:17 +01:00
Johannes de Fine Licht
ed5813c4aa
[MLIR][LLVM] Remove disallowlist from LLVM inliner (#75303)
The disallowlist was used as a migration strategy while support was
extended to more side effecting operations. We now (to the best of our
knowledge) support all side effecting operations, so never fail
`isLegalToInline` on any LLVM operation.

There is no test included, because that's exactly the reason for this
change: there are no more unsupported operations in inlining; the
existing tests for unsupported inlines have already been burninated.
2023-12-13 10:31:27 +01:00
Oleksandr "Alex" Zinenko
8169c157d5
[mlir] fix overflow in LLVM dialect inlining (#72878)
Don't use unsigned for sizes as they may be larger than that type can
hold.

Fixes #72678.
2023-11-21 18:27:00 +01:00
Mehdi Amini
26a0b27736
Make MLIR Value more consistent in terms of const "correctness" (NFC) (#72765)
MLIR can't really be const-correct (it would need a `ConstValue` class
alongside the `Value` class really, like `ArrayRef` and
`MutableArrayRef`). This is however making is more consistent: method
that are directly modifying the Value shouldn't be marked const.
2023-11-20 20:52:15 -08:00
Nicolas Vasilache
8c0ec793ee
[mlir][LLVM] Allow call_intrinsic to inline (#70940)
LLVM::CallIntrinsicOp was previously overlooked from the
isLegalToInline.
2023-11-01 15:42:19 +01:00
Tobias Gysi
dc2c4fc784 [mlir][llvm] Allow inlining for dbg label
The revision adds the dbg label intrinsic to the allow list of
operations that are legal to inline.

Reviewed By: zero9178

Differential Revision: https://reviews.llvm.org/D159359
2023-09-01 14:55:38 +00:00
Mikhail Goncharov
0a0aff2d24 fix unused variable warnings in conditionals
warning was updated in 92023b15099012a657da07ebf49dd7d94a260f84
2023-08-30 19:09:27 +02:00
Markus Böck
f9173c2958 [mlir][LLVM] Convert noalias parameters into alias scopes during inlining
Currently, inlining a function with a `noalias` parameter leads to a large loss of optimization potential as the `noalias` parameter, an important hint for alias analysis, is lost completely.

This patch fixes this with the same approach as LLVM by annotating all users of the `noalias` parameter with appropriate alias and noalias scope lists.
The implementation done here is not as sophisticated as LLVMs, which has more infrastructure related to escaping and captured pointers, but should work in the majority of important cases.
Any deficiency can be addressed in future patches.

Related LLVM code: 27ade4b554/llvm/lib/Transforms/Utils/InlineFunction.cpp (L1090)

Differential Revision: https://reviews.llvm.org/D155712
2023-07-20 15:05:28 +02:00
Markus Böck
b82acf8a14 [mlir][LLVM] Handle access groups during inlining
Handling access groups is luckily rather trivial: Any access groups from the call instruction are simply appended to any memory operations.
This is similar to one of the steps when handling alias scopes.
This patch nevertheless implements it as a separate function purely for readability purposes as it uses a different interface than alias scopes.

Differential Revision: https://reviews.llvm.org/D155795
2023-07-20 10:45:15 +02:00
Markus Böck
cde72af3db [mlir][LLVM] Append call ops alias scopes to inlined operations
Currently when inlining, any alias scope information previously attached to the call op is lost. This leads to a loss of information that could be used by alias analysis to determine that two memory access operations do not alias.

This patch fixes this issue by also taking any alias scopes of the call operation into account. These can then simply be appended onto any inlined operations.

This is analogous to the following code in LLVM: 1768c4597e/llvm/lib/Transforms/Utils/InlineFunction.cpp (L940)

Differential Revision: https://reviews.llvm.org/D155595
2023-07-18 16:26:17 +02:00
Markus Böck
dd28cc707c [mlir][LLVM] Perform deep clone of alias scopes during inlining
This is the first and most basic and important step for inlining memory operations with alias scopes.

For correctness, it is required that any alias scopes of inlined operations are replaced with deep copies. This is necessary as otherwise the same function could be inlined twice in one function, and suddenly the alias scopes extended.
A simple example would be `foo(a, b); foo(a2, b2)`. `a` and `a2` may alias. If `foo` is inlined in both instances, the store and load operations from `foo` may suddenly claim that `a` and `a2` do not alias if we were to keep the original alias scopes.

This is analogous to the following class/code in LLVM: 4eef2e30d6/llvm/lib/Transforms/Utils/InlineFunction.cpp (L985)

Differential Revision: https://reviews.llvm.org/D155479
2023-07-18 13:23:40 +02:00
Christian Ulmann
48b126e30b [mlir][llvm] Ensure immediate usage in intrinsics
This commit changes intrinsics that have immarg parameter attributes to
model these parameters as attributes, instead of operands. Using
operands only works if the operation is an `llvm.mlir.constant`,
otherwise the exported LLVMIR is invalid.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D151692
2023-06-12 06:57:42 +00:00
Johannes de Fine Licht
c7e06ce4b7 [MLIR][LLVM] Allow inlining llvm.intr.assume.
This operation is not marked pure because it needs to survive DCE, but
is otherwise safe to inline.

Reviewed By: Dinistro

Differential Revision: https://reviews.llvm.org/D151070
2023-05-22 12:00:31 +00:00
Johannes de Fine Licht
235eed2646 [MLIR][LLVM] Allow inlining noalias attributes.
We can not yet generate new aliasing metadata based on these arguments
because their global nature does not allow parallel inlining, but since
this is not necessary for correctness, we allow inlining functions that
have arguments with the noalias attribute for now.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D151067
2023-05-22 11:55:06 +00: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
Johannes de Fine Licht
7df43bdb42 [MLIR][LLVM] Support inlining of LLVM atomic operations.
These don't require any special handling, apart checking for unsupported
metadata, which is already implemented.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D150255
2023-05-10 10:54:34 +00:00
Johannes de Fine Licht
11ee125cc1 [MLIR][LLVM] Realign allocas to avoid dynamic realignment in inliner.
When the natural stack alignment is not set or is larger than or equal
to the target alignment required by a read-only byval argument defined
by an alloca, avoid the copy by just realigning the alloca to the target
alignment.

The code to check existing alignment is reorganized a bit to avoid
redundant casts.

This also includes a bugfix for passing a null DataLayoutInterface to
the DataLayout constructor when no parent op defines
DataLayoutInterface, and will now pass ModuleOp instead in this case.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D148557
2023-04-19 18:39:54 +00:00
Johannes de Fine Licht
7387972352 [MLIR][LLVM] Inline LLVM::StackSaveOp and LLVM::StackRestoreOp.
Support LLVM::StackSaveOp and LLVM::StackRestoreOp in the LLVM dialect
inliner in MLIR.

Inserts new LLVM::StackSaveOp and LLVM::StackRestoreOp intrinsics when
dynamic allocas are detected in the inlined blocks. This may result in
multiple saves/restores in the same block if some are already present in
the caller, which is legal IR, but is cleaned up in LLVM. There is not
yet a canonicalization pattern for this on LLVM dialect in MLIR.

Reviewed By: Dinistro

Differential Revision: https://reviews.llvm.org/D148011
2023-04-13 08:27:29 +00:00
Tobias Gysi
0fb4ac55bb [mlir] Make the inliner arg/res handlers more concise.
The revision ensures the newly introduced argument
and result handlers cannot be used for type conversion.
Instead use the existing materializeCallConversion hook to
perform type conversions.

Reviewed By: Dinistro

Differential Revision: https://reviews.llvm.org/D147605
2023-04-11 06:34:32 +00:00
Johannes de Fine Licht
76c83b3595 [MLIR][LLVM] Put byval static allocas in the entry block.
Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D147311
2023-04-03 09:08:35 +00:00
Johannes de Fine Licht
a33018d01b [MLIR][LLVM] Allowlist more side-effecting operations in LLVM inliner.
These operations do not on their own require special handling to be
inlined, and can just be inlined as is.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D147148
2023-03-30 11:41:51 +00:00
Johannes de Fine Licht
2abdc0f7df [MLIR][LLVM] Take the alignment attribute into account during inlining.
This is a subset of the full LLVM functionality to detect whether
realignment is necessary, conservatively copying byval arguments
whenever we cannot prove that the alignment requirement is met.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D147049
2023-03-29 10:22:09 +00:00
Johannes de Fine Licht
f737d4ad88 [MLIR][LLVM] Extend argument/result/function attribute support.
Use a disallowlist approach to deselect attributes on arguments and
return values that are not yet supported, but allow the rest to be
present when inlining.

To achieve fast lookup in `isLegalToInline`, the set of unsupported
function attributes is cached on the inliner interface object during
construction. By using a set of StringAttrs this should just amount to a
pointer lookup.

Depends on D146633

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D146729
2023-03-27 10:52:03 +00:00
Johannes de Fine Licht
a94d2bd5b5 [MLIR][LLVM] Add debug output to the LLVM inliner.
This revealed a test case that wasn't hitting the intended branch
because the inlinees had no function definition.

Depends on D146628

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D146633
2023-03-24 09:33:42 +01:00
Johannes de Fine Licht
a7c574d0c1 [MLIR][LLVM] Move the LLVM inliner interface into a separate file.
A fully fledged LLVM inliner will require a lot of logic. Since
`LLVMDialect.cpp` is large enough as it is, preemptively outline the
inlining logic into a separate `.cpp` file. This will also allow us to
add a `DEBUG_TYPE` for debugging the inliner.

The name `LLVMInlining` was chosen over `LLVMInlinerInterface` to keep
the option open for exposing inlining functionality even when not
invoked through the `DialectInlinerInterface`.

Depends on D146616

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D146628
2023-03-23 14:22:25 +01:00