8671 Commits

Author SHA1 Message Date
Matt Arsenault
36018494fd
AMDGPU: Check for subreg match when folding through reg_sequence (#140582)
We need to consider the use instruction's intepretation of the bits,
not the defined immediate without use context. This will regress
some cases where we previously coud match f64 inline constants. We
can restore them by either using pseudo instructions to materialize f64
constants, or recognizing reg_sequence decomposed into 32-bit pieces for them
(which essentially means recognizing every other input is a 0).

Fixes #139908
2025-05-19 21:44:44 +02:00
Matt Arsenault
2b7cc2b03e
AMDGPU: Add more mfma with constant splat input tests (#140581)
Baseline tests for #139317
2025-05-19 21:41:57 +02:00
Harrison Hao
5c3784023f
[NFC][AMDGPU] Add test for unfold-masked-merge-scalar-variablemask.ll (#140093)
This enables DAGCombine to recognize and form and_not + or patterns when
hasAndNot is true, allowing better SALU codegen for masked merge idioms.
For example:
(A & (B ^ C)) ^ C → (A & B) | (~A & C)

References:
* https://reviews.llvm.org/D45563#change-EG4Z6zDTofCc
* https://bugs.llvm.org/show_bug.cgi?id=6773
* https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
2025-05-20 00:24:54 +08:00
Krzysztof Drewniak
4bdd116b80
[AMDGPU] Add a new amdgcn.load.to.lds intrinsic (#137425)
This PR adds a amdgns_load_to_lds intrinsic that abstracts over loads to
LDS from global (address space 1) pointers and buffer fat pointers
(address space 7), since they use the same API and "gather from a
pointer to LDS" is something of an abstract operation.

This commit adds the intrinsic and its lowerings for addrspaces 1 and 7,
and updates the MLIR wrappers to use it (loosening up the restrictions
on loads to LDS along the way to match the ground truth from target
features).

It also plumbs the intrinsic through to clang.
2025-05-19 07:15:04 -07:00
Simon Pilgrim
01bf2d71d0 [AMDGPU] select-undef.ll - regenerate test checks
Ensure we see all the codegen to help find any lurking unexpected behaviours
2025-05-18 13:51:33 +01:00
Shilei Tian
f2165b9d58 Revert "[AMDGPU] Add flag to prevent reruns of LowerModuleLDS (#129520)"
This reverts commit aa9f8596b01fef013ab62c20e61fc96d165f60f7 because it made
some assumptions that may not be valid.
2025-05-17 21:41:59 -04:00
Matt Arsenault
4ddab1252f
AMDGPU: Move reg_sequence splat handling (#140313)
This code clunkily tried to find a splat reg_sequence by
looking at every use of the reg_sequence, and then looking
back at the reg_sequence to see if it's a splat. Extract this
into a separate helper function to help clean this up. This now
parses whether the reg_sequence forms a splat once, and defers the
legal inline immediate check to the use check (which is really use
context dependent)

The one regression is in globalisel, which has an extra
copy that should have been separately folded out. It was getting
dealt with by the handling of foldable copies in tryToFoldACImm.

This is preparation for #139908 and #139317
2025-05-17 08:18:01 +02:00
Shilei Tian
578741b5e8
[AMDGPU][Attributor] Rework update of AAAMDWavesPerEU (#123995)
Currently, we use `AAAMDWavesPerEU` to iteratively update values based
on attributes from the associated function, potentially propagating
user-annotated values, along with `AAAMDFlatWorkGroupSize`. Similarly,
we have `AAAMDFlatWorkGroupSize`. However, since the value calculated
through the flat workgroup size always dominates the user annotation
(i.e., the attribute), running `AAAMDWavesPerEU` iteratively is
unnecessary if no user-annotated value exists.

This PR completely rewrites how the `amdgpu-waves-per-eu` attribute is
handled in `AMDGPUAttributor`. The key changes are as follows:

- `AAAMDFlatWorkGroupSize` remains unchanged.
- `AAAMDWavesPerEU` now only propagates user-annotated values.
- A new function is added to check and update `amdgpu-waves-per-eu`
based on the following rules:
- No waves per eu, no flat workgroup size: Assume a flat workgroup size
of `1,1024` and compute waves per eu based on this.
- No waves per eu, flat workgroup size exists: Use the provided flat
workgroup size to compute waves-per-eu.
- Waves per eu exists, no flat workgroup size: This is a tricky case. In
this PR, we assume a flat workgroup size of `1,1024`, but this can be
adjusted if a different approach is preferred. Alternatively, we could
directly use the user-annotated value.
- Both waves per eu and flat workgroup size exist: If there’s a
conflict, the value derived from the flat workgroup size takes
precedence over waves per eu.

This PR also updates the logic for merging two waves per eu pairs. The
current implementation, which uses `clampStateAndIndicateChange` to
compute a union, might not be ideal. If we think from ensure proper
resource allocation perspective, for instance, if one pair specifies a
minimum of 2 waves per eu, and another specifies a minimum of 4, we
should guarantee that 4 waves per eu can be supported, as failing to do
so could result in excessive resource allocation per wave. A similar
principle applies to the upper bound. Thus, the PR uses the following
approach for merging two pairs, `lo_a,up_a` and `lo_b,up_b`: `max(lo_a,
lo_b), max(up_a, up_b)`. This ensures that resource allocation adheres
to the stricter constraints from both inputs.

Fix #123092.
2025-05-17 01:01:09 -04:00
Chinmay Deshpande
437195efbf
[NFC][AMDGPU] Update tests to use autogened CHECKs (#140311) 2025-05-16 17:22:19 -07:00
Jay Foad
e30301aaec
[MachineCopyPropagation] Make use of lane mask info in basic block liveins (#140248) 2025-05-16 16:54:34 +01:00
Harrison Hao
25c4478f38
[NFC][AMDGPU] Convert commute-compares.ll to autogenerated CHECK lines (#140076) 2025-05-16 19:34:20 +08:00
Pierre van Houtryve
aacebaeab5
[AMDGPU] Do not promote uniform i16 operations to i32 in CGP (#140208)
For the majority of cases, this is a neutral or positive change.
There are even testcases that greatly benefit from it, but some regressions are possible.
There is #140040 for GlobalISel that'd need to be fixed but it's only a one instruction regression and I think it can be fixed later.

Solves #64591
2025-05-16 10:31:03 +02:00
Pierre van Houtryve
fd85ffb4c4
[AMDGPU] Handle min/max in isNarrowingProfitable (#140206)
Introduces a slight regression in some cases but it'll even out once we
disable the promotion in CGP.
2025-05-16 10:16:44 +02:00
Changpeng Fang
f01f08292e
AMDGPU: Make v2f32 -> v2f16 legal when target supports v_cvt_pk_f16_f32 (#139956)
If targets support v_cvt_pk_f16_f32 instruction, v2f32 -> v2f16 should
be legal. However, SelectionDAG does not allow us to specify the source
type in the legalization rules. To workaround this, we make FP_ROUND
Custom for v2f16 then set up v2f32 -> v2f16 to be legal during custom
lowering.

Fixes: SWDEV-532608 -- expected v_cvt_pk_f16_f32 was not generated.
2025-05-15 15:09:21 -07:00
Pierre van Houtryve
aa9f8596b0
[AMDGPU] Add flag to prevent reruns of LowerModuleLDS (#129520)
FullLTO has to run this early before module splitting occurs otherwise
module splitting won't work as expected. There was a targeted fix for
fortran on another branch that disables the LTO run but that'd break
full LTO module splitting entirely.

Test changes are due to metadata indexes shifting.

See #122891
2025-05-15 09:54:21 +02:00
Shoreshen
a1664e5015
[AMDGPU] Remove verification failing cases due to bundle (#139868)
This is a fix up PR for https://github.com/llvm/llvm-project/pull/136112

There are test cases failing machine instruction verifier due to bundle
(see this
issue:https://github.com/llvm/llvm-project/issues/139102#issuecomment-2863620759)
2025-05-14 19:00:12 +08:00
Pierre van Houtryve
4e63e0457c
[AMDGPU] Canonicalize G_ZEXT of the shift amount in RegBankCombiner (#131792)
Canonicalize it to a G_AND instead so that ISel patterns can pick it
up and ignore it, as the shift instructions only read low bits.
G_ZEXT would be lowered to a v/s_and anyway in most cases.

I'm also looking at making a DAG version of this in a separate patch.
2025-05-14 10:48:51 +02:00
Frederik Harwath
1377535d99
[AMDGPU] si-peephole-sdwa: Fix cndmask vcc use for wave32 (#139541)
Before V_CNDMASK_B32_e64 gets converted to SDWA form, a conversion to
V_CNDMASK_B32_e32 occurs.
The vcc use of this instruction must be fixed into a vcc_lo use for wave32.
This fix only happens after the final conversion to the SDWA form. This led
to a compiler error in situations where the conversion to SDWA aborts.

Make sure that the vcc-fix gets applied even if the SDWA conversion is
not completed.

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-05-14 07:37:01 +02:00
Shoreshen
c5331276cb
AMDGPU: Add sgpr bit convert tests (#136112)
Add inreg test for sgpr purpose

This is the second PR after
https://github.com/llvm/llvm-project/pull/135729.

To test sgpr inputs and outputs, using inreg cases for bit-conversions

---------

Co-authored-by: Matt Arsenault <Matthew.Arsenault@amd.com>
2025-05-14 09:55:37 +08:00
Lucas Ramirez
6456ee056f
Reapply "[AMDGPU][Scheduler] Refactor ArchVGPR rematerialization during scheduling (#125885)" (#139548)
This reapplies 067caaa and 382a085 (reverting b35f6e2) with fixes to
issues detected by the address sanitizer (MIs have to be removed from
live intervals before being removed from their parent MBB).

Original commit description below.

AMDGPU scheduler's `PreRARematStage` attempts to increase function
occupancy w.r.t. ArchVGPR usage by rematerializing trivial
ArchVGPR-defining instruction next to their single use. It first
collects all eligible trivially rematerializable instructions in the
function, then sinks them one-by-one while recomputing occupancy in all
affected regions each time to determine if and when it has managed to
increase overall occupancy. If it does, changes are committed to the
scheduler's state; otherwise modifications to the IR are reverted and
the scheduling stage gives up.

In both cases, this scheduling stage currently involves repeated queries
for up-to-date occupancy estimates and some state copying to enable
reversal of sinking decisions when occupancy is revealed not to
increase. The current implementation also does not accurately track
register pressure changes in all regions affected by sinking decisions.

This commit refactors this scheduling stage, improving RP tracking and
splitting the stage into two distinct steps to avoid repeated occupancy
queries and IR/state rollbacks.

- Analysis and collection (`canIncreaseOccupancyOrReduceSpill`). The
number of ArchVGPRs to save to reduce spilling or increase function
occupancy by 1 (when there is no spilling) is computed. Then,
instructions eligible for rematerialization are collected, stopping as
soon as enough have been identified to be able to achieve our goal
(according to slightly optimistic heuristics). If there aren't enough of
such instructions, the scheduling stage stops here.
- Rematerialization (`rematerialize`). Instructions collected in the
first step are rematerialized one-by-one. Now we are able to directly
update the scheduler's state since we have already done the occupancy
analysis and know we won't have to rollback any state. Register
pressures for impacted regions are recomputed only once, as opposed to
at every sinking decision.

In the case where the stage attempted to increase occupancy, and if both
rematerializations alone and rescheduling after were unable to improve
occupancy, then all rematerializations are rollbacked.
2025-05-13 11:11:00 +02:00
Brox Chen
89f8267340
[AMDGPU][True16][CodeGen] update more GFX11Plus codegen test with true16 mode (#138600)
This is a NFC patch.

This patch duplicate GFX11plus runlines and apply them with
"+mattr=+real-true16" and "+mattr=-real-true16" on more gfx11/gfx12
tests. And then update the test with the update script
2025-05-12 12:19:46 -04:00
Simon Pilgrim
f1043b1643 [AMDGPU] combine_vloads.ll regenerate test checks
Make it easier to detect regressions
2025-05-12 12:33:53 +01:00
Simon Pilgrim
4e535601b0 [AMDGPU] ashr.v2i16.ll - regenerate to match shl/lshr v2i16 test coverage 2025-05-12 10:35:12 +01:00
Austin Kerbow
2c9a46cce3
[AMDGPU] Move kernarg preload logic to separate pass (#130434)
Moves kernarg preload logic to its own module pass. Cloned function
declarations are removed when preloading hidden arguments. The inreg
attribute is now added in this pass instead of AMDGPUAttributor. The
rest of the logic is copied from AMDGPULowerKernelArguments which now
only check whether an arguments is marked inreg to avoid replacing
direct uses of preloaded arguments. This change requires test updates to
remove inreg from lit tests with kernels that don't actually want
preloading.
2025-05-11 21:18:11 -07:00
Vitaly Buka
b35f6e26a5
Revert "[AMDGPU][Scheduler] Refactor ArchVGPR rematerialization during scheduling (#125885)" (#139341)
And related "[AMDGPU] Regenerate mfma-loop.ll test"

Introduce memory error detected by Asan #125885.

This reverts commit 382a085a95b0abeac77b150b7b644b372bd08e78.
This reverts commit 067caaafb58a156d0d77229422607782a639f5b5.
2025-05-09 17:51:46 -07:00
Matt Arsenault
cccb4fc4b8
AMDGPU: Test more types with minimumnum and maximumnum (#139242)
We had custom lowering for the wider vectors of f16, but missing
test coverage for them. Also add more vector tests for bf16, and
split the bf16 cases into separate files so we can add globalisel
run lines.
2025-05-09 20:29:14 +02:00
Emma Pilkington
7babf22461
[StructurizeCFG] Stop setting DebugLocs in flow blocks (#139088)
Flow blocks are generated code that don't really correspond to any
location in the source, so principally they should have empty DebugLocs.
Practically, setting these debug locs leads to redundant is_stmts being
generated after #108251, causing stepping test failures in the ROCm GDB
test suite.

Fixes SWDEV-502134
2025-05-09 14:22:14 -04:00
Matt Arsenault
790ce0ec94
AMDGPU: Add minimumnum/maximumnum to list of canonicalizing opcodes (#139124)
This makes no difference in the test, as these always expand now.
2025-05-09 18:36:12 +02:00
Matt Arsenault
e4751d5cbc
AMDGPU: Add minimumnum/maximumnum tests with amdgpu-ieee=0 (#139145)
With the IEEE bit disabled, the hardware instructions have the
same behavior as these operations.
2025-05-09 08:17:49 +02:00
Matt Arsenault
6cf84e036e
AMDGPU: Test more subtargets in minimumnum/maximumnum tests (#139144) 2025-05-09 08:15:09 +02:00
Matt Arsenault
f13ff77842
AMDGPU: Form min3/max3 from minimumnum/maximumnum (#139137) 2025-05-09 08:12:54 +02:00
Matt Arsenault
458e2416ca
AMDGPU: Add baseline tests for min3/max3 from minimumnum/maximumnum (#139136) 2025-05-09 08:10:06 +02:00
Matt Arsenault
912df60b08
AMDGPU: Handle minimumnum/maximumnum in fneg combines (#139133) 2025-05-09 08:07:01 +02:00
Matt Arsenault
07f36f21e9
AMDGPU: Add baseline tests for fneg with min/max intrinsics (#139132)
Copy the minnum and maxnum tests into versions with minimum/maximum
and minimumnum/maximumnum.
2025-05-09 08:04:09 +02:00
Stanislav Mekhanoshin
d2c5fbe9ea
[AMDGPU] Legalize vector fminimum and fmaximum with VOP3P (#138971)
Co-authored-by: Matt Arsenault <Matthew.Arsenault@amd.com>
2025-05-08 22:31:27 -07:00
Brox Chen
13b2f7c785
[AMDGPU][True16][CodeGen] add fake16 to gisel test (#138588)
This is a NFC patch.

Add '-mattr=-real-true16' to gfx11/gfx12 test. 

GISEL is not fully supported in true16 mode yet. However we might want
to turn on true16 mode for SDAG as default first. This patch is
preparing for this mode shift in the short future so we can have a small
patch to turn it on
2025-05-08 17:37:08 -04:00
Chinmay Deshpande
3a5af231fd
[GlobalISel][AMDGPU] Fix handling of v2i128 type for AND, OR, XOR (#138574)
Current behavior crashes the compiler.

This bug was found using the AMDGPU Fuzzing project.

Fixes SWDEV-508816.
2025-05-08 19:31:28 +02:00
Brox Chen
9d907a2bb1
AMDGPU][True16][CodeGen] FP_Round f64 to f16 in true16 (#128911)
Update the f64 to f16 lowering for targets which support f16 types. 

For unsafe mode, lowered to two FP_ROUND. (This patch
https://reviews.llvm.org/D154528 stops from combining these two FP_ROUND
back). In safe mode, select LowerF64ToF16 (round-to-nearest-even
rounding mode)
2025-05-08 13:30:09 -04:00
pvanhout
e9df48e8a4 Revert "(reland) [GlobalISel] Diagnose inline assembly constraint lowering errors (#139049)"
This reverts commit 534d221b63bb52f64e1f3ad3c40cfb87323d28ec.
2025-05-08 15:03:42 +02:00
pvanhout
382a085a95 [AMDGPU] Regenerate mfma-loop.ll test
#125885 did not update the test.
2025-05-08 14:46:20 +02:00
Pierre van Houtryve
534d221b63
(reland) [GlobalISel] Diagnose inline assembly constraint lowering errors (#139049)
The initial patch (#135782 caused issues because it emits an error, and llc is sensitive to it.
It also caused compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp to fail.

Use warnings instead + reject lowering. That way, the fallback path is used without llc/clang returning a failure code.
If fallback isn't enabled then the warnings provide context as to why lowering failed.

Original commit description for #135782:

Instead of printing something to dbgs (which is not visible to all users),
emit a diagnostic like the DAG does. We still crash later because we fail to
select the inline assembly, but at least now users will know why it's crashing.

In a future patch we could also recover from the error like the DAG does, so the
lowering can keep going until it either crashes or gives a different error later.
2025-05-08 13:22:57 +02:00
Lucas Ramirez
067caaafb5
[AMDGPU][Scheduler] Refactor ArchVGPR rematerialization during scheduling (#125885)
AMDGPU scheduler's `PreRARematStage` attempts to increase function
occupancy w.r.t. ArchVGPR usage by rematerializing trivial
ArchVGPR-defining instruction next to their single use. It first
collects all eligible trivially rematerializable instructions in the
function, then sinks them one-by-one while recomputing occupancy in all
affected regions each time to determine if and when it has managed to
increase overall occupancy. If it does, changes are committed to the
scheduler's state; otherwise modifications to the IR are reverted and
the scheduling stage gives up.

In both cases, this scheduling stage currently involves repeated queries
for up-to-date occupancy estimates and some state copying to enable
reversal of sinking decisions when occupancy is revealed not to
increase. The current implementation also does not accurately track
register pressure changes in all regions affected by sinking decisions.

This commit refactors this scheduling stage, improving RP tracking and
splitting the stage into two distinct steps to avoid repeated occupancy
queries and IR/state rollbacks.

- Analysis and collection (`canIncreaseOccupancyOrReduceSpill`). The
number of ArchVGPRs to save to reduce spilling or increase function
occupancy by 1 (when there is no spilling) is computed. Then,
instructions eligible for rematerialization are collected, stopping as
soon as enough have been identified to be able to achieve our goal
(according to slightly optimistic heuristics). If there aren't enough of
such instructions, the scheduling stage stops here.
- Rematerialization (`rematerialize`). Instructions collected in the
first step are rematerialized one-by-one. Now we are able to directly
update the scheduler's state since we have already done the occupancy
analysis and know we won't have to rollback any state. Register
pressures for impacted regions are recomputed only once, as opposed to
at every sinking decision.

In the case where the stage attempted to increase occupancy, and if both
rematerializations alone and rescheduling after were unable to improve
occupancy, then all rematerializations are rollbacked.
2025-05-08 12:51:06 +02:00
Iris Shi
f9783c559f
[InstCombine] Fix frexp(frexp(x)) -> frexp(x) fold (#138837)
Fixes #138819

When frexp is applied twice, the second result should be zero.
2025-05-08 00:37:46 +08:00
Brox Chen
09d01be856
[AMDGPU][True16][CodeGen] replace subreg_to_reg to req_sequence (#138746)
Since subreg_to_reg is considered broken in llvm, replace subreg_to_reg
to reg_sequence
2025-05-07 10:28:10 -04:00
pvanhout
0db040576d Revert "[GlobalISel] Diagnose inline assembly constraint lowering errors (#135782)"
This reverts commit c22081c320340d0e7542b247ee093ca515509b52.
2025-05-07 14:50:45 +02:00
Pierre van Houtryve
c22081c320
[GlobalISel] Diagnose inline assembly constraint lowering errors (#135782)
Instead of printing something to dbgs (which is not visible to all users),
emit a diagnostic like the DAG does. We still crash later because we fail to
select the inline assembly, but at least now users will know why it's crashing.

In a future patch we could also recover from the error like the DAG does, so the
lowering can keep going until it either crashes or gives a different error later.
2025-05-07 14:13:25 +02:00
Pierre van Houtryve
c3a638caab
[GlobalISel] Fix silently dropped MIFlags on selected instructions (#138851)
We used uint16 for flags but flags now go up to 24 bits, so all flags in bits 16-24 were lost.

Fixes #110801
2025-05-07 14:07:16 +02:00
Pierre van Houtryve
0d0eed419f
[AMDGPU][Legalizer] Widen i16 G_SEXT_INREG (#131308)
It's better to widen them to avoid it being lowered into a G_ASHR + G_SHL. With this change we just extend to i32 then trunc the result.
2025-05-07 10:22:15 +02:00
Jeffrey Byrnes
7fa721ac6c
[AMDGPU] Extend test coverage for cross RC register coalescing (#138617)
[[Change-Id:
I36894fc36e6e6214930fae67f2ca35999abf3b88](https://github.com/llvm/llvm-project/pull/132137)
](https://github.com/llvm/llvm-project/pull/132137) landed, but was
breaking bots. In the rebase, I accidentally brought in unintended
changes. I have reverted the commit
8b9ae65d51
and fixed the rebase.

This PR is to land the original patch / extension to the test.

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-05-06 18:07:30 -07:00
Matthias Braun
675cb70641
Register assembly printer passes (#138348)
Register assembly printer passes in the pass registry.

This makes it possible to use `llc -start-before=<target>-asm-printer ...` in tests.

Adds a `char &ID` parameter to the AssemblyPrinter constructor to allow
targets to use the `INITIALIZE_PASS` macros and register the pass in the
pass registry. This currently has a default parameter so it won't break
any targets that have not been updated.
2025-05-06 18:01:17 -07:00