8688 Commits

Author SHA1 Message Date
Shilei Tian
0f1277d0b3
[NFC][AMDGPU] Move flat_atomic.ll to llvm/test/CodeGen/AMDGPU/ (#141126) 2025-05-23 08:15:26 -04:00
zGoldthorpe
bb7e559740
[AMDGPU] Correct bitshift legality transformation for small vectors (#140940)
Fix for a bug found by the AMD fuzzing project.

The legaliser would originally try to widen a small vector such as `<4 x
i1>` to a single `i16` during the legalisation of bitshifts, as it was
not originally written with consideration for vector operands. This
patch simply adds a guard to prohibit this transformation and allow
other legalisation transformations to step in.
2025-05-23 10:56:21 +02:00
Matt Arsenault
db0bac0ef2
AMDGPU: Form v_med_f32 from minimumnum/maximumnum immediate pattern (#141048)
This makes little difference in the final output, as we manage to form this
after these are lowered to the _ieee operations. This does result in fewer steps
in the DAG, and helps prepare for changing the handling of minnum/maxnum.
2025-05-22 21:04:47 +02:00
Matt Arsenault
57e9097ad2
AMDGPU: Add baseline v_med3_f32 tests from minimumnum/maximumnum (#141047) 2025-05-22 21:01:49 +02:00
Lucas Ramirez
1b34722302
[AMDGPU] Fix computation of waves/EU maximum (#140921)
This fixes an issue in the waves/EU range calculation wherein, if the
`amdgpu-waves-per-eu` attribute exists and is valid, the entire
attribute may be spuriously and completely ignored if workgroup sizes
and LDS usage restrict the maximum achievable occupancy below the
subtarget maximum. In such cases, we should still honor the requested
minimum number of waves/EU, even if the requested maximum is higher than
the actually achievable maximum (but still within subtarget
specification).

As such, the added unit test `empty_at_least_2_lds_limited`'s waves/EU
range should be [2,4] after this patch, when it is currently [1,4] (i.e,
as if `amdgpu-waves-per-eu` was not specified at all).

Before e377dc4 the default maximum waves/EU was always set to the
subtarget maximum, trivially avoiding the issue.
2025-05-22 02:45:59 +02:00
Brox Chen
7f62800fb8
[AMDGPU][True16][CodeGen] select vgpr16 for asm inline 16bit vreg (#140946)
select vgpr16 for asm inline 16bit vreg in true16 mode
2025-05-21 17:47:28 -04:00
Vigneshwar Jayakumar
e12cbd8339
[AMDGPU] Fix scale opsel flags for scaled MFMA operations (#140183)
Fix for src scale opsel flags encoding and ASM parsing for gfx950 scaled MFMA.
2025-05-21 12:30:22 -05:00
Matt Arsenault
2e2bbcacf8
AMDGPU/GlobalISel: Start legalizing minimumnum and maximumnum (#140900)
This is the bare minimum to get the intrinsic to compile for AMDGPU,
and it's not optimal. We need to follow along closer with the existing
G_FMINNUM/G_FMAXNUM with custom lowering to handle the IEEE=0 case
better.

Just re-use the existing lowering for the old semantics for
G_FMINNUM/G_FMAXNUM. This does not change G_FMINNUM/G_FMAXNUM's
treatment,
nor try to handle the general expansion without an underlying min/max
variant (or with G_FMINIMUM/G_FMAXIMUM).
2025-05-21 17:00:45 +02:00
Robert Imschweiler
dc29901efb
[AMDGPU] PromoteAlloca: handle out-of-bounds GEP for shufflevector (#139700)
This LLVM defect was identified via the AMD Fuzzing project.

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-05-21 15:28:30 +02:00
Shilei Tian
0f2a469951 Revert "[AMDGPU] remove move instruction if there is no user of it (#136735)"
This reverts commit 883afa4ef93d824ec11981ccad04af1cd1e4ce29 since it is not
technically sound.
2025-05-20 23:12:09 -04:00
Alan Li
ada2fbfe36
[GISel] Fix ShuffleVector assert (#139769)
Fixes issue: https://github.com/llvm/llvm-project/issues/139752

When G_SHUFFLE_VECTOR has only 1 element then it is possible the vector
is decayed into a scalar.
2025-05-20 21:25:31 -04:00
Krzysztof Drewniak
6b9da28b2b
[AMDGPU][LowerBufferFatPointers] Handle addrspacecast null to p7 (#140775)
Some application code operating on generic pointers (that then gete
initialized to buffer fat pointers) may perform tests against nullptr.
After address space inference, this results in comparisons against
`addrspacecast (ptr null to ptr addrspace(7))`, which were crashing.

However, while general casts to ptr addrspace(7) from generic pointers
aren't supposted, it is possible to cast null pointers to the all-zerose
bufer resource and 0 offset, which this patch adds.

It also adds a TODO for casting _out_ of buffer resources, which isn't
implemented here but could be.
2025-05-20 16:13:01 -07:00
Shilei Tian
d2992423e3
[Attributor] Don't replace addrspacecast (ptr null to ptr addrspace(x)) with ptr addrspace(x) null (#126779)
`ConstantPointerNull` represents a pointer with value 0, but it doesn’t
necessarily mean a `nullptr`. `ptr addrspace(x) null` is not the same as
`addrspacecast (ptr null to ptr addrspace(x))` if the `nullptr` in AS X
is not
zero. Therefore, we can't simply replace it.

Fixes #115083.
2025-05-20 18:08:42 -04:00
Matt Arsenault
5aa3171f2c
AMDGPU: Add regression test for multiple frame index lowering (#140784)
Failures appeared after https://github.com/llvm/llvm-project/pull/140587 but this case wasn't covered
2025-05-20 22:37:43 +02:00
Brox Chen
7e9d9dba9c
[AMDGPU][True16][CodeGen] update test fmax3/fmin3 test with true16 mode (#140752)
This is a NFC patch.

This patch duplicate GFX11plus runlines and apply them with
"+mattr=+real-true16" and "+mattr=-real-true16" on fmax3/fmin3 tests,
and putting '-real-true16' on gisel testline. And then update the test
with the update script
2025-05-20 12:33:41 -04:00
Chinmay Deshpande
e264cff6fd
[NFC][AMDGPU] Update tests to use autogened CHECKs (#140648) 2025-05-19 19:47:40 -07:00
Alexander Richardson
07e2ba445d
[AMDGPU] Set AS8 address width to 48 bits
Of the 128-bits of buffer descriptor only 48 bits are address bits, so
following the discussion on https://discourse.llvm.org/t/clarifiying-the-semantics-of-ptrtoint/83987/54,
the logic conclusion is to set the index width to 48 bits instead of
the current value of 128.

Most of the test changes are mechanical datalayout updates, but there
is one actual change: the ptrmask test now uses .i48 instead of .i128
and I had to update SelectionDAGBuilder to correctly extend the mask.

Reviewed By: krzysz00

Pull Request: https://github.com/llvm/llvm-project/pull/139419
2025-05-19 17:26:05 -07:00
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