42786 Commits

Author SHA1 Message Date
Afonso Rafael (Gafanhoto)
9096c9cda3
[LoopFusion] Remove the InvalidDependencies duplicates (#187744)
If the function dependencesAllowFusion returns false, in fuseCandidates
the reportLoopFusion function is used to increment InvalidDependencies
and to emit a OptimizationRemarkMissed. If both dependencesAllowFusion
and reportLoopFusion increment InvalidDependencies, statistics will
appear duplicated
2026-03-23 16:13:46 -04:00
Owen Anderson
9acfa56d8f
[Coro] Preserve program address spaces correctly in CoroSplit. (#188002) 2026-03-23 20:40:50 +01:00
Alan Zhao
c624851037
[LoopVectorize] Fix an integer narrowing conversion in getPredBlockCostDivisor(...) (#187605)
`LoopVectorizationCostModel::getPredBlockCostDivisor(...)` may return
large `uint64_t` values that get coerced to an `unsigned` by
`VPCostContext::getPredBlockCostDivisor(...)`, which can cause division
by zero.

Fixes #187584
2026-03-23 17:22:05 +00:00
Alexey Bataev
af37ac8aee [SLP]Use reduction root explicitly from reduction analysis to avoid non-determinism
Initially, the reduction root was detected using the last member of the UserIgnoreList set, which is unordered. Better to use the reduction root explicitly to avoid non-determinism in the reduction parent block, which may cause incorrect scale factor estimation for the reduction cost.
2026-03-23 09:46:33 -07:00
Alexey Bataev
5b7ad38d6b [SLP]Fix codegen of compares with consts, being trunced
If the const values have more active bits, than requested by the another
operand of the compare, such constants should not be trunced to avoid
miscompilation
2026-03-23 07:49:19 -07:00
Kiva
f4b523d669
[PatternMatch][NFC] Add m_IToFP and m_FPToI (#188040)
Added two patterns for IR pattern matching, `m_IToFP` and `m_FPToI`
which are basically shortcuts of `m_CombinedOr(..., ...)`

> if there isn't already one, PatternMatch should have an m_ItoFP which
covers both

_Originally posted by @arsenm in
https://github.com/llvm/llvm-project/pull/185826#discussion_r2967473936_

/cc @arsenm
2026-03-23 14:10:18 +00:00
Alexey Bataev
85f529dda1 Revert "[SLP]Fix codegen of compares with consts, being trunced"
This reverts commit 16e0cc8308379857ecd69e6fe1aaf71e15b94910 to add
a new test case for the miscompile
2026-03-23 06:27:08 -07:00
Alexey Bataev
16e0cc8308 [SLP]Fix codegen of compares with consts, being trunced
If the const values have more active bits, than requested by the another
operand of the compare, such constants should not be trunced to avoid
miscompilation
2026-03-23 06:04:08 -07:00
Benjamin Maxwell
249b086545
[LV] Fix crash when extends are not widened in partial reduction matching (#187782)
Fixes https://github.com/llvm/llvm-project/pull/185821#issuecomment-4098933551
2026-03-23 10:30:19 +00:00
Alexey Bataev
b2ba79578b [SLP]Fix patterns for compile time blow up with ordered reductions
Excluded patterns, leading to compile time blow up for integer ordered
reductions.
2026-03-22 13:42:54 -07:00
Florian Hahn
c079372099 [VPlan] Add m_VPPhi pattern matcher and use in removeDeadRecipes (NFC).
Add m_VPPhi to match VPPhi instructions with exactly 2 operands.

Split off from https://github.com/llvm/llvm-project/pull/156262.
2026-03-22 19:49:47 +00:00
Alexey Bataev
88f830aed8 [SLP]Do not try to reduced instruction, marked for deletion in previous attempts
Need to skip instructions, which were vectorized and marked for deletion
to prevent a compiler crash
2026-03-22 10:10:48 -07:00
Takashi Idobe
1075a2fa7e
[Instcombine] Write Instcombine pass to strength reduce lock xadd to lock sub (#184715)
Resolves: https://github.com/llvm/llvm-project/issues/174933

The issue goes into a case where fetch_sub(n) is properly optimized but
fetch_add(neg(n)) is not optimized to the same code.

Although the issue is tagged for x86 I assumed this be best handled
outside of the backends so I put this in InstCombine.
2026-03-21 20:45:29 +00:00
Florian Hahn
d3c7f23d2f
[VPlan] Remove unused VPlan friend declaration from VPValue (NFC)
VPlan only accesses VPValue through public APIs (getUnderlyingValue(),
etc.), so the friend declaration is unnecessary.
2026-03-21 20:37:05 +00:00
Alexey Bataev
616240369e [SLP]Do not consider copyable node with SplitVectorize parent
If the copyables are schedulable and the parent node is plit vectorize,
need to skip the scheduling analysis for such nodes to avoid a compiler
crash
2026-03-21 06:56:59 -07:00
Alexey Bataev
db143fb2b9 [SLP][NFC]Use block number instead of pointer for stable sorting, NFC 2026-03-21 04:30:32 -07:00
Chang Lin
0ae9aaf539
[InstCombine] When rewriting GEPs, check that the types match. (#186886) 2026-03-20 17:37:00 -07:00
vporpo
720615f495
[SandboxVec][DAG] Fix DAG update when user is scheduled (#187148)
This patch fixes the update of the DAGNode UnscheduledSucc counter when
a use edge is modified. This is the result of a setOperand() or a RAUW
(and friends) operation.

Before this patch we would not check if the User (i.e., the consumer of
the use-def edge) is scheduled and we would update the definition's
UnscheduledSucc counter, resulting in counting errors.

For example, consider the following IR:
```
  %A = ...
  %B = ...
  %U = %A  ; scheduled
```
Note that %U's DAGNode is marked as "scheduled" while %A and %B are not.

If we change %U's operand from %A to %B then we should not attempt to
update %A's or %B's UnscheduledSuccs because %U is scheduled so it
should not get counted as an "unscheduled" successor.
2026-03-20 15:43:27 -07:00
Alexey Bataev
b260861b38 [SLP]Update values after ordered vectorization
Need to update matching between the original reduced values and their
vectorized matches after ordered reduction vectorization to avoid
a compiler crash
2026-03-20 13:33:40 -07:00
Alexey Bataev
94e366ef20
[SLP] Initial support for ordered reductions
Patch models ordered reductions as a series of extractelements for the
cases which cannot be modeled as unordered reductions.

Fixes #50590

Reviewers: RKSimon, hiraditya

Pull Request: https://github.com/llvm/llvm-project/pull/182644
2026-03-20 13:45:14 -04:00
Alexey Bataev
2bb0fa46a8
[SLP]Prefer copyable over alternate
If the instructions state is alternate and/or contains non-directly
matching instructions, need to check if it is better to represent such
operations as non-alternate with copyables.
To do this, we need to compare operands between the instructions in their
different representations and choose the best one for optimal
vectorization.

Reviewers: RKSimon, hiraditya

Pull Request: https://github.com/llvm/llvm-project/pull/183777
2026-03-20 11:59:59 -04:00
Florian Hahn
19b0c68ee0
[VPlan] Skip epilogue vectorization if dead after narrowing IGs. (#187016)
When narrowing interleave groups, the main vector loop processes IC
iterations instead of VF * IC. Update selectEpilogueVectorizationFactor
to use the effective VF, checking if the canonical IV controlling the
loop now steps by UF instead of VFxUF.

This avoids epilogue vectorization with dead epilogue vector loops and
also prevents crashes in cases where we can prove both the epilogue and
scalar loop are dead.

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

PR: https://github.com/llvm/llvm-project/pull/187016
2026-03-20 12:33:16 +00:00
Ramkumar Ramachandra
1dfd268f10
[VPlan] Simplify mul x, -1 -> sub 0, x (#187551)
Simplify exactly as InstCombine does. A follow-up would include
simplifying add x, (sub 0, y) -> sub x, y.

Alive2 proof: https://alive2.llvm.org/ce/z/Af7QiD
2026-03-20 12:07:51 +00:00
Florian Hahn
21f439f132
[LoopRotate] Use SCEV exit counts to improve rotation profitability (#187483)
Most loop transformations, like unrolling and vectorization, expect the
latch branch to be countable. Allow rotation, if it turns the latch from
uncountable to countable.

This use SCEV to check for countable exits, if CheckExitCount set.
Currently it is not set for the LPM1 run (where SCEV is not used by
other passes), only in LPM.

With that compile-time impact is mostly neutral

https://llvm-compile-time-tracker.com/compare.php?from=eba342d0ba930a404a026c80aada51c43974f0db&to=2e676337b45fae63ce9498116d8e6e43772363c5&stat=instructions:u

ClamAV is consistently slower (~+0.15%) and 7zip faster in most cases
(~-0.13%)

Across a large test set based on C/C++ workloads, this rotates ~0.8%
more loops with ~2.68M rotated loops.

For the test set, ~2.7% more loops are runtime-unrolled and +6.36% more
early exit loops vectorized on ARM64 macOS.

This fixes a regression where std::ranges::find_last loops stopped
being runtime-unrolled after
5f648c370e
which changed the loop
structure so we stopped rotating.

https://clang.godbolt.org/z/6baeE1av6

Based on https://github.com/llvm/llvm-project/pull/162654.

Co-authored-by:  Marek Sedláček <mr.mareksedlacek@gmail.com>

PR: https://github.com/llvm/llvm-project/pull/187483
2026-03-20 10:21:15 +00:00
Benjamin Maxwell
4b17135d14
[LV] Simplify matchExtendedReductionOperand() (NFCI) (#185821)
This updates `matchExtendedReductionOperand` so the simple case of
`UpdateR(PrevValue, ext(...))` is matched first as an early exit. The
binop matching is then flattened to remove the extra layer of the
`MatchExtends` lambda.
2026-03-20 09:29:28 +00:00
Sander de Smalen
a971089cb8
[LV] Explain why a less profitable VF was chosen (NFCI) (#187469)
I was very puzzled the other day when it showed that VF 8 had a cost of
X and VF 16 had a cost of X/2, yet it still choose VF 8. This PR adds
some extra debug output to explain why this happens.
2026-03-20 07:21:17 +00:00
Weibo He
5599d60187
[Coroutines][NFC] Elide coro.free based on frame instead of coro.id (#187627)
Part 2/4: Implement HALO for coroutines that flow off final suspend.
Parent PR approved in https://github.com/llvm/llvm-project/pull/185336,
with no change since then

Since `coro.id` is unavailable in resumers, Elide `coro.free` based on
frame instead of `coro.id`
2026-03-20 15:05:51 +08:00
Bruce Changlong Xu
cbab7e65a7
[AMDGPU] Minor cleanups in offload plugin and AMDGPUEmitPrintf. NFC. (#187587)
Use empty() in assert, brace-init instead of std::make_pair in the
AMDGPU offload plugin, and fix a comment typo in AMDGPUEmitPrintf.
2026-03-19 18:16:47 -04:00
Florian Hahn
fd3cf1c160
[LV] Move dereferenceability check from Legal to VPlan (NFC) (#185323)
Instead of checking dereferenceability early during
LoopVectorizationLegality, defer the check to VPlan construction via
areAllLoadsDereferenceable.

This in preparation for supporting early exit vectorization of
non-dereferencable loads, e.g. via speculative loads
(https://discourse.llvm.org/t/rfc-provide-intrinsics-for-speculative-loads/89692)
or first-faulting loads. Detection in VPlan allows easily replacing
potentially non-deref loads with other loads as needed.

PR: https://github.com/llvm/llvm-project/pull/185323
2026-03-19 19:21:45 +00:00
Grigory Pastukhov
f66bd8e81a
[LLVM] Add flatten function attribute to LLVM IR and implement recursive inlining in AlwaysInliner (#174899)
This adds a new function-level `flatten` LLVM IR attribute and
implements support for it in the AlwaysInliner pass, bringing LLVM's
behavior in line with GCC.

Previously, the `flatten` attribute only existed as a Clang attribute,
which was lowered to `alwaysinline` on individual call sites. Per the
RFC discussion [1], the consensus was to match GCC semantics:
recursively inline the entire call tree into the
flattened function, rather than just immediate call sites.

This PR:
- Adds the `flatten` function attribute to LLVM IR
- Implements recursive inlining of all viable callees in AlwaysInliner
- Uses inline history tracking to detect and stop at recursive call
cycles
- Emits optimization remarks when inlining is skipped due to recursion

A follow-up patch will update Clang to emit the LLVM `flatten` attribute
on
functions instead of marking individual call sites with `alwaysinline`.

[1]
https://discourse.llvm.org/t/rfc-function-level-flatten-depth-attribute-for-depth-limited-inlining
2026-03-19 11:25:46 -07:00
Alexey Bataev
7d76a3122d
[SLP]Improve analysis for the shl-based reduced values with copyables (#185485)
shl-based reduced values in many cases serve as a bitcast/bswap-based
transfromation root, but need to improve analysis for better matching.
This patch merges reduction candidates into a single reduced value
array, if there are only 2 different candidate arrays, one of them has
only single element, the second is a list of shl instructions. Also,
sorts these shl instructions by their shift amount and merges with the
single candidate, if it is profitable to have a copyable reduction.
2026-03-19 14:16:53 -04:00
vporpo
467cf7caed
[SandboxIR] Implement UncondBrInst and CondBrInst (#187196)
This patch implements the unconditional and conditional branch
instructions mirroring the newly added LLVM IR instructions.

So now we have two new classes UncondBrInst and CondBrInst inheriting
from BranchInst.

The original Br opcode has been removed in favor of UncondBr and CondBr.
2026-03-19 10:28:32 -07:00
Weiwen He
2ec08b3194
[LSV] Added check for mismatched GEP strides in getConstantOffsetComplexAddrs (#186671)
When two GEPs A & B with variable indices have source element types of
different sizes (e.g. i8 vs i32), their offsets cannot be constants.
Currently the `getConstantOffsetComplexAddrs` does not check this and
simply use the stride of GEP A assuming they are equal, resulting in
miscompile.

Add a check that the source element types are the same, otherwise bail
out.

Fixes #186327
2026-03-19 17:10:30 +01:00
Alexey Bataev
9050794e06
[SLP]Improve reductions for copyables/split nodes
The original support for copyables leads to a regression in x264 in
RISCV, this patch improves detection of the copyable candidates by more
precise checking of the profitability and adds and extra check for
splitnode reduction, if it is profitable.

Fixes #184313

Reviewers: hiraditya, RKSimon

Pull Request: https://github.com/llvm/llvm-project/pull/185697
2026-03-19 12:03:05 -04:00
Joseph Huber
d18a784d41
[compiler-rt] Define GPU specific handling of profiling functions (#185763)
Summary:
The changes in https://www.github.com/llvm/llvm-project/pull/185552
allowed us to
start building the standard `libclang_rt.profile.a` for GPU targets.
This PR expands this by adding an optimized GPU routine for counter
increment and removing the special-case handling of these functions in
the OpenMP runtime.

Vast majority of these functions are boilerplate, but we should be able
to do more interesting things with this in the future, like value or
memory profiling.
2026-03-19 10:51:48 -05:00
Florian Hahn
cdaf29f84d
Revert "[LV] Simplify and unify resume value handling for epilogue vec." (#187504)
Reverts llvm/llvm-project#185969

This is suspected to cause a miscompile in 549.fotonik3d_r from SPEC 2017 FP
2026-03-19 14:38:37 +00:00
Graham Hunter
b227fab5a6
[NFC][LV] Introduce enums for uncountable exit detail and style (#184808)
Recursively splitting out some work from #183318; this covers
the enums for early exit loop type (none, readonly, readwrite)
and the style used (just readonly and
masked-handle-ee-in-scalar-tail for now) and refactoring for
basic use of those enums.
2026-03-19 14:17:25 +00:00
Florian Hahn
78a8f00977
Revert "[VPlan] Create header phis once regions have been created (NFC)."
This reverts commit 91b928f919364b29e241821fc639b9ef56dab1a5.

This complicates some analysis that need the happen on the scalar VPlan,
before regions have been created, e.g.
https://github.com/llvm/llvm-project/pull/185323/.
2026-03-19 12:53:12 +00:00
Abu
1078a1dabd
Lowering ~x | (x - 1) to ~blsi(x) (#186722)
Alive2 proof: 
https://alive2.llvm.org/ce/z/bK93Cn

I've implemented a fold in `InstCombineAndOrXor.cpp` to canonicalize `~x
| (x - 1)` to `~(x & -x)` which enables the CodeGen to emit the `blsi`
instruction.

I've also added a test in `CodeGen/X86`.

Fixes #184055

---------

Co-authored-by: Tim Gymnich <tim@gymni.ch>
2026-03-19 20:45:56 +08:00
Alexey Bataev
582fa78753
[SLP]Do not match buildvector node, if current node is part of its combined nodes
If current buildvector node is part of the combined nodes of the
matching candidate node, this matching candidate must be considered as
non-matching to prevent wrong def-use chain

Reviewers: 

Pull Request: https://github.com/llvm/llvm-project/pull/187491
2026-03-19 08:15:32 -04:00
John Brawn
191c84b822
[VPlan] Permit derived IV in isHeaderMask (#187360)
When matching scalar steps of the canonical IV, also match a derived IV
of the canonical IV if the derivation is essentially a no-op. Fixes a
failure in the mve-reg-pressure-spills.ll test when expensive checks are
enabled.
2026-03-19 12:05:07 +00:00
Alexis Engelke
0d05c882ce
[Support] Use block numbers for LoopInfo BBMap (#103400)
Replace the DenseMap from blocks to their innermost loop a vector
indexed by block numbers, when possible. Supporting number updates is
not trivial as we don't store a list of basic blocks, so this is not
implemented.

NB: I'm generally not happy with the way loops are stored. As I think
that there's room for improvement, I don't want to touch the
representation at this point.

Pull Request: https://github.com/llvm/llvm-project/pull/103400
2026-03-19 11:18:06 +01:00
vporpo
77066a3d33
[SandboxVec][SeedCollection] Aux pass argument for enabling different types (#155079)
This patch implements the auxiliary pass argument, a secondary argument
that can be passed to Sandbox IR passes.

It also updates the SeedCollection pass to accept the auxiliary argument
"enable-diff-types", which enables collection of seeds of different
types. This will be used in a follow-up patch.
2026-03-18 17:11:01 -07:00
Elvis Wang
53f8f3b017
Reland [LV] Replace remaining LogicalAnd to vp.merge in EVL optimization. (#184068) (#187199)
This patch replace the remaining LogicalAnd to vp.merge in the second
pass to not break the `m_RemoveMask` pattern in the optimizeMaskToEVL.

Also skip cost model comparison when the plan contains `vp_merge` which
won't be calculated by the legacy model.

This can help to remove header mask for FindLast reduction (CSA) loops.

Original PR: https://github.com/llvm/llvm-project/pull/184068
Original built-bot failure:
https://lab.llvm.org/buildbot/#/builders/213/builds/2497
2026-03-19 07:56:42 +08:00
Alexey Bataev
abdcde9bbc
[SLP] Loop aware cost model/tree building
Currently, SLP vectorizer do not care about loops and their trip count.
It may lead to inefficient vectorization in some cases. Patch adds loop
nest-aware tree building and cost estimation.
When it comes to tree building, it now checks that tree do not span
across different loop nests. The nodes from other loop nests are
immediate buildvector nodes.
The cost model adds the knowledge about loop trip count. If it is
unknown, the default value is used, controlled by the
-slp-cost-loop-min-trip-count=<value> option. The cost of the vector
nodes in the loop is multiplied by the number of iteration (trip count),
because each vector node will be executed the trip count number of
times. This allows better cost estimation.

Original Reviewers:
jdenny-ornl, vporpo, hiraditya, RKSimon

Original PR: https://github.com/llvm/llvm-project/pull/150450

Recommit after revert in c7bd3062f1dac975cf9b706f457b3c55b4bf57ff and in 4e500bd0015042b0cd4b7c87b81caeea06072d24

Reviewers: 

Pull Request: https://github.com/llvm/llvm-project/pull/187391
2026-03-18 17:54:01 -04:00
Florian Hahn
fce100e26e
[VPlan] Fix masked_cond expansion.
masked_cond is used to combine early-exit conditions with masks from
predicate. The early-exit condition should only be evaluated if the mask
is true. Emit the mask first, to avoid incorrect poison propagation.

Fixes https://github.com/llvm/llvm-project/issues/187061.
2026-03-18 20:26:04 +00:00
Aleksandr Popov
d4b86e5617
[LSR] skip ephemeral IV users when collecting IV chains (#187282)
IVUsers records ephemeral values used only by `llvm.assume` as IV
operands in the Processed set. As a result, `CollectChains` picks them
up and builds unnecessary increment chains. Fix this by checking
`IVUsers::isEphemeral` before collecting the chains.

Fixes #187270
2026-03-18 20:50:10 +01:00
Hari Limaye
17158b2ab1
[InstCombine] Fix comment in SimplifyDemandedUseBits (NFC) (#187126)
Fix the values in the truth table comment for the combine

  add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
2026-03-18 18:45:56 +00:00
Florian Hahn
0ea2e5813f
[VPlan] Account for early-exit dispatch blocks when updating LI. (#185618)
Now that we can vectorize loops with multiple early exits, we emit
dispatch blocks after the middle block to go to a specific exit or
continue in the dispatch chain.

With that, we need to be a bit more careful when it comes to picking the
loop the dispatch block belongs to. The dispatch block will belong to
the innermost loop of all exit blocks reachable from the current block.

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

PR: https://github.com/llvm/llvm-project/pull/185618
2026-03-18 18:37:34 +00:00
Justin Fargnoli
a2891ff85c
Reapply "[LoopUnroll] Remove computeUnrollCount()'s return value" (#187104)
Address
https://github.com/llvm/llvm-project/pull/184529#issuecomment-4074393657
by checking the loop's metadata prior to unrolling.
2026-03-18 17:55:56 +00:00