623 Commits

Author SHA1 Message Date
Ryotaro Kasuga
2978b20af4
[Delinearization] Add validation for large size arrays (#169902)
This patch adds a check in validation for delinearization to ensure that
the offset calculation does not overflow. If it overflows, different
array accesses (e.g., `A[0][0]` and `A[1][0]`) could map to the same
linear index, leading to incorrect behavior.
For fixed-size arrays, the check is relatively straightforward. However,
for dynamic-size arrays (i.e., arrays where the size is not known at
compile time), it's difficult to prove this statically, and it going to
fail for almost all cases. Maybe we need to add some runtime checks or
reasoning based on `inbounds` like LAA does.

Fixes the test cases added in #169048.
2025-12-03 03:01:18 +00:00
Paul Walker
591c463e07
[LLVM][AArch64] Mark SVE integer intrinsics as speculatable. (#167915)
Exceptions include intrinsics that:
* take or return floating point data
* read or write FFR
* read or write memory
* read or write SME state
2025-11-18 11:37:51 +00:00
Alexander Belyaev
7ee0e0f956 Revert "[LICM] Sink unused l-invariant loads in preheader. #157559"
This reverts commit 469702c5d5cc4fa18c3a962afb971950a084f373.

https://github.com/llvm/llvm-project/issues/168048
2025-11-14 14:51:33 +01:00
Vigneshwar Jayakumar
469702c5d5
[LICM] Sink unused l-invariant loads in preheader. (#157559)
Unused loop invariant loads were not sunk from the preheader to the exit
block, increasing live range.

This commit moves the sinkUnusedInvariant logic from indvarsimplify to
LICM also adds functionality to sink unused load that's not
clobbered by the loop body.
2025-10-30 09:23:04 -05:00
paperchalice
249883d0c5
[test][Transforms] Remove unsafe-fp-math uses part 2 (NFC) (#164786)
Post cleanup for #164534.
2025-10-23 20:31:31 +08:00
Nikita Popov
573ca36753
[IR] Replace alignment argument with attribute on masked intrinsics (#163802)
The `masked.load`, `masked.store`, `masked.gather` and `masked.scatter`
intrinsics currently accept a separate alignment immarg. Replace this
with an `align` attribute on the pointer / vector of pointers argument.

This is the standard representation for alignment information on
intrinsics, and is already used by all other memory intrinsics. This
means the signatures now match llvm.expandload, llvm.vp.load, etc.
(Things like llvm.memcpy used to have a separate alignment argument as
well, but were already migrated a long time ago.)

It's worth noting that the masked.gather and masked.scatter intrinsics
previously accepted a zero alignment to indicate the ABI type alignment
of the element type. This special case is gone now: If the align
attribute is omitted, the implied alignment is 1, as usual. If ABI
alignment is desired, it needs to be explicitly emitted (which the
IRBuilder API already requires anyway).
2025-10-20 08:50:09 +00:00
Ramkumar Ramachandra
2a02d57efb
[IR] Mark vector intrinsics speculatable (#162334)
The vector intrinsics in question have no undefined behavior, and have
no other effect besides returning the result: they should hence be
marked speculatable.
2025-10-09 09:41:59 +01:00
Shilei Tian
2195fe7e01
[AMDGPU] Add the support for 45-bit buffer resource (#159702)
On new targets like `gfx1250`, the buffer resource (V#) now uses this
format:

```
base (57-bit): resource[56:0]
num_records (45-bit): resource[101:57]
reserved (6-bit): resource[107:102]
stride (14-bit): resource[121:108]
```

This PR changes the type of `num_records` from `i32` to `i64` in both
builtin and intrinsic, and also adds the support for lowering the new
format.

Fixes SWDEV-554034.

---------

Co-authored-by: Krzysztof Drewniak <Krzysztof.Drewniak@amd.com>
2025-09-24 11:12:02 -04:00
Weibo He
f99b7f91a9
Reapply "[Coroutines] Add llvm.coro.is_in_ramp and drop return value of llvm.coro.end (#155339)" (#159278)
As mentioned in #151067, current design of llvm.coro.end mixes two functionalities: querying where we are and lowering to some code. This patch separate these functionalities into independent intrinsics by introducing a new intrinsic llvm.coro.is_in_ramp.

Update a test in inline/ML, Reapply #155339
2025-09-24 11:29:25 +08:00
Nikita Popov
0969e2c420
[CaptureTracking] Fix handling for non-returning read-only calls (#158979)
We currently infer `captures(none)` for calls that are read-only,
nounwind and willreturn. As pointed out in
https://github.com/llvm/llvm-project/issues/129090, this is not correct
even with this set of pre-conditions, because the function could
conditionally cause UB depending on the address.

As such, change this logic to instead report `captures(address)`. This
also allows dropping the nounwind and willreturn checks, as these can
also only capture the address.
2025-09-17 11:27:06 +02:00
Weibo He
a264453750
Revert "Reapply "[Coroutines] Add llvm.coro.is_in_ramp and drop return value of llvm.coro.end #153404"" (#159236)
Reverts llvm/llvm-project#155339 because of CI fail
2025-09-17 12:04:13 +08:00
Weibo He
ef45c9756a
Reapply "[Coroutines] Add llvm.coro.is_in_ramp and drop return value of llvm.coro.end #153404" (#155339)
As mentioned in #151067, current design of llvm.coro.end mixes two
functionalities: querying where we are and lowering to some code. This
patch separate these functionalities into independent intrinsics by
introducing a new intrinsic llvm.coro.is_in_ramp.
2025-09-17 11:25:31 +08:00
Antonio Frighetto
370607065d
[llvm] Regenerate test checks including TBAA semantics (NFC)
Tests exercizing TBAA metadata (both purposefully and not), and
previously generated via UTC, have been regenerated and updated
to version 6.
2025-09-12 20:01:17 +02:00
Weibo He
1329af9c28
Revert "[LoopInfo] Pointer to stack object may not be loop invariant in a coroutine function (#149936)" (#157986)
Since #156788 has resolved #149604, we can revert this workaround now.
2025-09-12 09:27:08 +08:00
Nikita Popov
d1408667de
[MemorySSA] Don't create phi nodes in fixupDefs() (#156021)
The general flow when inserting MemoryDefs is:

 * Insert the def and set it's defining access (may insert phis)
 * Insert IDF phis
 * Update defining access for defs after the new one (fixupDefs)
 * Rename uses if requested

fixupDefs() uses getPreviousDef() which can create new MemoryPHIs, but
for which we're not going to insert IDF phis, so the required dominance
property may not hold.

I believe this is a leftover from a time before the "Insert IDF phis"
step existed. Now that step should already ensure that all necessary
MemoryPhis have been inserted, and we only need to update them.

The fixupDefs() implementation was also returning after updating a
single access, which is not right.

Fixes https://github.com/llvm/llvm-project/issues/47875.
Fixes https://github.com/llvm/llvm-project/issues/117157.
Fixes https://github.com/llvm/llvm-project/issues/152998.
Fixes https://github.com/llvm/llvm-project/issues/155161.
Fixes https://github.com/llvm/llvm-project/issues/155184.
2025-09-04 08:23:29 +00:00
Ruiling, Song
45dec71725
[IR] Allow nofree metadata to inttoptr (#153149)
Our GPU compiler usually construct pointers through inttoptr. The memory
was pre-allocated before the shader function execution and remains valid
through the execution of the shader function. This brings back the
expected behavior of instruction hoisting for the test
`hoist-speculatable-load.ll`, which was broken by #126117.
2025-09-02 21:50:47 +08:00
Chuanqi Xu
23c7ad94a1 Revert "[Coroutines] Add llvm.coro.is_in_ramp and drop return value of llvm.coro.end (#153404)"
This reverts commit 19a4f520952c2b87de43e7176f34be9906384a33.

See test failure in https://github.com/llvm/llvm-project/pull/153404
2025-08-25 17:55:04 +08:00
Weibo He
19a4f52095
[Coroutines] Add llvm.coro.is_in_ramp and drop return value of llvm.coro.end (#153404)
As mentioned in #151067, current design of `llvm.coro.end` mixes two
functionalities: querying where we are and lowering to some code. This
patch separate these functionalities into independent intrinsics by
introducing a new intrinsic `llvm.coro.is_in_ramp`.
2025-08-25 17:38:38 +08:00
Nikita Popov
5753ee2434 [LICM] Avoid assertion failure on stale MemoryDef
It can happen that the call is originally created as a MemoryDef,
and then later transforms show it is actually read-only and could
be a MemoryUse -- however, this is not guaranteed to be reflected
in MSSA.
2025-08-19 10:25:45 +02:00
Antonio Frighetto
33761df961
Revert "[SimpleLoopUnswitch] Record loops from unswitching non-trivial conditions"
This reverts commit e9de32fd159d30cfd6fcc861b57b7e99ec2742ab due to
multiple performance regressions observed across downstream Numba
benchmarks (https://github.com/llvm/llvm-project/issues/138509#issuecomment-3193855772).

While avoiding non-trivial unswitches on newly-cloned loops helps
mitigate the pathological case reported in https://github.com/llvm/llvm-project/issues/138509,
it may as well make the IR less friendly to vectorization / loop-
canonicalization (in the test reported, previously no select with
loop-carried dependence existed in the new specialized loops),
leading the abovementioned approach to be reconsidered.
2025-08-18 17:40:08 +02:00
Mircea Trofin
c971c25544
[licm] don't drop MD_prof when dropping other metadata (#152420)
Part of Issue #147390
2025-08-16 07:26:13 -07:00
Theodoros Theodoridis
d15b7a83a7
[llvm][LICM] Limit multi-use BOAssociation to FP and Vector (#149829)
Limit the re-association of BOps with multiple users to FP and Vector
arithmetic.
2025-08-14 11:56:55 +01:00
Mircea Trofin
374cbfd327
[licm] clone MD_prof when hoisting conditional branch (#152232)
The profiling - related metadata information for the hoisted conditional branch should be copied from the original branch, not from the current terminator of the block it's hoisted to.

The patch adds a way to disable the fix just so we can do an ablation test, after which the flag will be removed. The same flag will be reused for other similar fixes.

(This was identified through `profcheck` (see Issue #147390), and this PR addresses most of the test failures (when running under profcheck) under `Transforms/LICM`.)
2025-08-13 02:01:00 +02:00
weiguozhi
5e87792200
[LoopInfo] Pointer to stack object may not be loop invariant in a coroutine function (#149936)
A coroutine function may be split to ramp function and resume function,
and they have different stack frames, so a pointer to stack objects may
have different addresses depending on where it is used, so it's not a
loop invariant.

It temporarily fixes https://github.com/llvm/llvm-project/issues/149604.
2025-08-09 14:20:19 -07:00
Nikita Popov
c23b4fbdbb
[IR] Remove size argument from lifetime intrinsics (#150248)
Now that #149310 has restricted lifetime intrinsics to only work on
allocas, we can also drop the explicit size argument. Instead, the size
is implied by the alloca.

This removes the ability to only mark a prefix of an alloca alive/dead.
We never used that capability, so we should remove the need to handle
that possibility everywhere (though many key places, including stack
coloring, did not actually respect this).
2025-08-08 11:09:34 +02:00
Nikita Popov
0a41e7c87e
[LICM] Do not reassociate constant offset GEP (#151492)
LICM tries to reassociate GEPs in order to hoist an invariant GEP.
Currently, it also does this in the case where the GEP has a constant
offset.

This is usually undesirable. From a back-end perspective, constant GEPs
are usually free because they can be folded into addressing modes, so
this just increases register pressume. From a middle-end perspective,
keeping constant offsets last in the chain makes it easier to analyze
the relationship between multiple GEPs on the same base, especially
after CSE.

The worst that can happen here is if we start with something like

```
loop {
   p + 4*x
   p + 4*x + 1
   p + 4*x + 2
   p + 4*x + 3
}
```

And LICM converts it into:

```
p.1 = p + 1
p.2 = p + 2
p.3 = p + 3
loop {
   p + 4*x
   p.1 + 4*x
   p.2 + 4*x
   p.3 + 4*x
}
```

Which is much worse than leaving it for CSE to convert to:
```
loop {
   p2 = p + 4*x
   p2 + 1
   p2 + 2
   p2 + 3
}
```
2025-08-01 09:43:15 +02:00
Antonio Frighetto
e9de32fd15 [SimpleLoopUnswitch] Record loops from unswitching non-trivial conditions
Track newly-cloned loops coming from unswitching non-trivial invariant
conditions, so as to prevent conditions in such cloned blocks from
being unswitched again.

Fixes: https://github.com/llvm/llvm-project/issues/138509.
2025-07-24 10:27:52 +02:00
Shan Huang
1b136824bb
[DebugInfo][LICM] Fix debug value loss caused by hoisting the cmp instructions (#146640)
fix #146621
2025-07-07 13:46:56 +08:00
Jiachen (Yangyang) Wang
1ab0e7dd60
[LICM] Hoisting writeonly calls (#143799)
Adds support for hoisting `writeonly` calls in LICM.

This patch adds a missing optimization that allows hoisting of
`writeonly` function calls out of loops when it is safe to do so.
Previously, such calls were conservatively retained inside the loop
body, and the redundant calls were only reduced through unrolling,
relying on target-dependent heuristics.

Closes #143267

Testing:
- Modified previously negative tests for hoisting writeonly calls to be
instead positive
- Added test cases for hoisting of two writeonly calls where the
pointers do/do not alias
- Added a test case for not argmemonly writeonly calls.
2025-06-19 10:09:19 +02:00
Nikita Popov
a13b7cc00c
[LICM] Support hoisting of non-argmemonly readonly calls (#144497)
The code checking whether a readonly call is safe to hoist is
currently limited to only argmemonly calls. However, the actual
implementation does not depend on this in any way. It either
does an MSSA clobber walk on the memory access (which will take
all locations accessed by the call into account), or it will
look at all MemoryDefs in an entirely location-independent manner.

The current restriction dates back to the time when LICM still
supported AST, in which case this code *did* reason about the
individual pointer arguments.
2025-06-18 12:24:55 +02:00
Nikita Popov
308b97a5d4 [LICM] Regenerate test checks (NFC) 2025-06-17 10:33:51 +02:00
Nikita Popov
d517f15e09 [LICM] Regenerate test checks (NFC) 2025-06-12 12:30:52 +02:00
Nikita Popov
eea6969190
[LICM] Only check for provenance captures (#141731)
When performing scalar promotions, only consider provenance captures,
which may lead to non-thread-safe accesses. Address captures can be
ignored.
2025-05-28 11:57:30 +02:00
Florian Hahn
0c82e06bb6
[LICM] Use OverflowTracking to preserve NUW/NSW when reassociating. (#140404)
This enables preserving NSW when both adds have NSW and NUW.

For now, set AllKnownNonNegative/AllKnownNonZero to false when using in
LICM.

https://alive2.llvm.org/ce/z/uu79Xc

Depends on https://github.com/llvm/llvm-project/pull/140403.

PR: https://github.com/llvm/llvm-project/pull/140404
2025-05-20 11:18:56 +01:00
Florian Hahn
55531e33d3
[LICM] Add test reassociating ADD nuw nsw. 2025-05-17 22:12:00 +01:00
Florian Hahn
bf1d4a0710
[LICM] Preserve Disjoint flag on OR when hoisting. (#140266)
Update hoistBOAssociation to preserve Disjoint flags on the newly
created instructions if both ORs are disjoint.

Fixes https://github.com/llvm/llvm-project/issues/139625.

PR: https://github.com/llvm/llvm-project/pull/140266
2025-05-17 14:48:39 +01:00
Shan Huang
5971b41919
[DebugInfo][LICM] Salvage dbg_values for the dead instructions to erase (#138796)
fix #138684 .
2025-05-12 09:07:30 +08:00
Matt Arsenault
393c783a10 LICM: Avoid looking at use list of constant data (#134690)
The codegen test changes seem incidental. Either way,
sms-grp-order.ll seems to already not hit the original issue.
2025-04-13 17:06:38 +02:00
Yingwei Zheng
76e07d8ba5
[LibCall] Infer nocallback for libcalls (#135173)
This patch adds `nocallback` attributes for string/math libcalls. It
allows FuncAttributor to infer `norecurse` more precisely and encourages
more aggressive global optimization.
2025-04-12 15:11:54 +08:00
Jeremy Morse
792a6f8119
[RemoveDIs] Remove "try-debuginfo-iterators..." test flags (#130298)
These date back to when the non-intrinsic format of variable locations
was still being tested and was behind a compile-time flag, so not all
builds / bots would correctly run them. The solution at the time, to get
at least some test coverage, was to have tests opt-in to non-intrinsic
debug-info if it was built into LLVM.

Nowadays, non-intrinsic format is the default and has been on for more
than a year, there's no need for this flag to exist.

(I've downgraded the flag from "try" to explicitly requesting
non-intrinsic format in some places, so that we can deal with tests that
are explicitly about non-intrinsic format in their own commit).
2025-03-14 15:50:49 +00:00
Krzysztof Drewniak
f7d03707d1
[AMDGPU] Generalize amdgcn.make.buffer.rsrc to fat pointers (#126828)
Attempting to pass a `ptr addrspace(7)` to functions that take `ptr`
arguments produces undesirable `addrspacecast(addrspacecast(p8 x to p7)
to p0) => addrspacecast(p8 x to p0)` folds. This results in illegal GEP
operations on buffer resources, which can't be GEP'd. (However, note
that, while unimplemneted, addressspacecast from ptr addrspace(7) to ptr
is legal - it's just an effective address computation)

To resolve this problem, and thus prevent illegal
`getelementptr T, ptr addrspace(8) %x, ...` s from being produces, this
commit extends amdgcn.make.buffer.rsrc to also be variadic in its result
type, auto-upgrading old manglings.

The logic for handling a make.buffer.rsrc in instruction selection
remains untouched and expects the output type to be a ptr addrspace(8),
as does the Clang lowering for its builtin (the pointer-to-pointer
version might want a different name in clang). LowerBufferFatPointers
has been updated to lower
amdgcn.make.buffer.rsrc.p7.p* to amdgcn.make.buffer.rsrc.p8.p* .

This'll also make exposing buffer fat pointers in Clang easier, since
you don't have to cast between a `__amdgcn_rsrc_t` and a pointer.
2025-02-18 14:15:28 -06:00
Florian Hahn
f5d63ccb22 [LICM] Add test with deref assumption of GEP. 2025-02-16 20:55:22 +01:00
Florian Hahn
65640c1d4c
[AssumeBundles] Dereferenceable used in bundle only applies at assume. (#126117)
Update LangRef and code using `Dereferenceable` in assume bundles to
only use the information if it is safe at the point of use.

`Dereferenceable` in an assume bundle is only guaranteed at the point of
the assumption, but may not be guaranteed at later points, because the
pointer may have been freed.

Update code using `Dereferenceable` to only use it if the pointer cannot
be freed. This can further be refined to check if the pointer could be
freed between assume and use.

This follows up on https://github.com/llvm/llvm-project/pull/123196.

With that change, it should be safe to expose dereferenceable
assumptions more widely as in
https://github.com/llvm/llvm-project/pull/121789

PR: https://github.com/llvm/llvm-project/pull/126117
2025-02-13 20:41:23 +01:00
Nikita Popov
29441e4f5f
[IR] Convert from nocapture to captures(none) (#123181)
This PR removes the old `nocapture` attribute, replacing it with the new
`captures` attribute introduced in #116990. This change is
intended to be essentially NFC, replacing existing uses of `nocapture`
with `captures(none)` without adding any new analysis capabilities.
Making use of non-`none` values is left for a followup.

Some notes:
* `nocapture` will be upgraded to `captures(none)` by the bitcode
   reader.
* `nocapture` will also be upgraded by the textual IR reader. This is to
   make it easier to use old IR files and somewhat reduce the test churn in
   this PR.
* Helper APIs like `doesNotCapture()` will check for `captures(none)`.
* MLIR import will convert `captures(none)` into an `llvm.nocapture`
   attribute. The representation in the LLVM IR dialect should be updated
   separately.
2025-01-29 16:56:47 +01:00
Florian Hahn
b769758056
[Options] Use UseDerefAtPointSemantics cl::opt<bool>. (#123192)
It is used as boolean option, use cl::opt<bool> instead of
vl::opt<unsigned>.

PR: https://github.com/llvm/llvm-project/pull/123192
2025-01-16 14:07:03 +00:00
Ramkumar Ramachandra
22a280d392
LICM: teach hoistMinMax about samesign (#122730)
Follow up on 4a0d53a (PatternMatch: migrate to CmpPredicate) to get rid
of one of the FIXMEs it introduced by replacing a predicate comparison
with CmpPredicate::getMatching.
2025-01-13 20:03:58 +00:00
Florian Hahn
ab6677e7d6
[LICM] Only set AA metadata on hoisted load if it executes. (#117204)
https://github.com/llvm/llvm-project/pull/116220 clarified that
violations of aliasing metadata are UB.

Only set the AA metadata after hoisting a log, if it is guaranteed to
execute in the original loop.

PR: https://github.com/llvm/llvm-project/pull/117204
2024-11-26 14:16:16 +00:00
Florian Hahn
7f19b1e49c
[LICM] Add test showing incorrectly setting alasing metadata.
The new test illustrates a case where LICM introduces UB-implying
metadata on speculated loads that may not execute in the original
version.

The aliasing metadata behavior has been clarified in
 https://github.com/llvm/llvm-project/pull/116220.
2024-11-21 17:13:19 +00:00
DianQK
18b02bbf44
[LICM] allow MemoryAccess creation failure (#116813)
Fixes #116809.

After running some passes (SimpleLoopUnswitch, LoopInstSimplify, etc.),
MemorySSA might be outdated, and the instruction `I` may have become a
non-memory touching instruction.

LICM has already handled this, but it does not pass
`CreationMustSucceed=false` to `createDefinedAccess`.
2024-11-20 19:52:51 +08:00
Lee Wei
ead9ad2960
[llvm] Remove br i1 undef from some regression tests [NFC] (#116739)
This PR removes tests with br i1 undef under
`llvm/tests/Transforms/JumpThreading, LCSSA, LICM, LoopDeletion,
LoopIdiom`.
2024-11-19 08:12:25 +00:00