10070 Commits

Author SHA1 Message Date
LU-JOHN
7585ab05d6
[AMDGPU] Enable shift64 hazard recognition for gfx9 (#183839)
Enable shift64 hazard recognition for gfx9 cores.

---------

Signed-off-by: John Lu <John.Lu@amd.com>
2026-02-28 08:59:55 -06:00
Son Tuan Vu
27d654c4c4
[AMDGPU] Fix piggybacking after commute in AMDGPULowerVGPREncoding (#183778)
After successfully commuting an instruction to be compatible with the
current VGPR MSB mode, update CurrentMode with the commuted
instruction's mode requirements. This locks in the mode bits the
commuted instruction relies on, preventing later instructions from
piggybacking and corrupting those bits.

Without this fix, a subsequent instruction needing a different mode
could piggyback onto the preceding s_set_vgpr_msb and change mode bits
that the commuted instruction depends on. For example, a nullopt src1
position (treated as 0) could be overwritten to a different value,
causing incorrect register encoding for the commuted instruction.

The fix still allows compatible piggybacking - instructions that only
add new mode bits without changing existing ones can still piggyback.
2026-02-27 22:16:14 -08:00
Aiden Grossman
8bd8d8e6de
[AMDGPU] Remove extra pipes from load-saddr-offset-imm.ll (#183874)
This test uses opt to run instcombin and then pipes that into llc which
has its output piped into FileCheck. Before this patch, the test also
piped in the source file into llc as well, which caused issues with a
downstream test executor that executes the lines in bash. However, these
extra pipes don't make sense anyways, so remove them.
2026-02-28 01:54:38 +00:00
Jay Foad
fff2f0ba78
[AMDGPU] Handle GFX1250 hazards between WMMA and VOPD (#183573)
Hazards between WMMA and VALU were handled in #149865 but this only
worked for regular VOP* VALU encodings, not for VOPD.

Fixes: #183546
2026-02-27 19:51:53 +00:00
Aiden Grossman
403fd7679f
[SlotIndexes] Further pack indices to improve spill placement time (#182640)
This patch makes it so that renumbering indices when inserting
instructions into the SlotIndexes analysis renumbers the entire list if
the list is otherwise densely packed. This fixes a case we saw on
AArch64 with a lot of spills where every single spill instruction
insertion required a renumbering of most of the instructions in a large
function, making the operation approximately quadratic.

This is not NFC as heuristics depend on the SlotIndex numbers, although
this should mostly be a wash as LRs should be extended ~equally.
2026-02-27 10:55:20 -08:00
Harrison Hao
1afd7d40af
[AMDGPU] Support i8/i16 GEP indices when promoting allocas to vectors (#175489)
Allow promote alloca to vector to form a vector element index from
i8/i16
GEPs when the dynamic offset is known to be element size aligned.

Example:
```llvm
%alloca = alloca <3 x float>, addrspace(5)
%idx = select i1 %idx_select, i32 0, i32 4
%p = getelementptr inbounds i8, ptr addrspace(5) %alloca, i32 %idx
```
Or:
```llvm
%alloca = alloca <3 x float>, addrspace(5)
%idx = select i1 %idx_select, i32 0, i32 2
%p = getelementptr inbounds i16, ptr addrspace(5) %alloca, i32 %idx
```
2026-02-27 18:24:43 +08:00
Ruiling, Song
d149830b98
[AMDGPU] Pre-Commit tests for handle mbcnt in computeKnownBitsFromOperator (#178607)
For PR #183229
2026-02-27 08:45:05 +08:00
Jay Foad
b13718cd10
[AMDGPU] Fix encoding of inline constants in v_fma_mix_f32_bf16 (#183487)
BF16 source operands use F32 inline constant values, so set OP_SEL to
select the high half of the constant, since BF16 encoding matches the
high 16 bits of F32 encoding. This behaviour is different from F16
source operands which use F16 constant values in the low 16 bits.

Fixes: #183337
2026-02-26 19:30:20 +00:00
Jay Foad
9608d0a561
[AMDGPU] Fix typo in test file name. NFC. (#183570) 2026-02-26 17:25:23 +00:00
Matt Arsenault
3a8081cedc
AMDGPU: Stop checking for r600 in printf pass (#183536)
Avoid adding the pass to the pipeline in the first place.
2026-02-26 17:18:36 +01:00
Brox Chen
d161a220bc
[AMDGPU] lower fptrunc.round(rtz) to cvt_pkrtz_f16_f32 (#177069)
Lower fptrunc.round(rtz) to cvt_pkrtz_f16_f32 and remove setreg
2026-02-26 10:23:55 -05:00
Brox Chen
6df45ed1b8
[AMDGPU][True16] change vdst_in regclass and remove opsel for cvt_pk_f8_fp32 pattern (#179995)
Two changes:

1. int_amdgcn_cvt_pk_fp/bf8_f32 takes a i32 as a tied input, and this
tied input is passed to cvt_pk_f8_fp32 as a tied vdst_in. In true16 mode
the vdst_in is in vgpr16. Since this i32 could be in sreg32, need to
copy it to vgpr32 first before extract_subreg.

2. remove SRCMODS.DST_OP_SEL from cvt_pk_f8_fp32 pattern in true16 mode.
The opsel is not needed since the hi/lo16 will be clobbered by the
following register rewrite/allocation pass and this const opsel causes
conflictions
2026-02-26 10:19:18 -05:00
LU-JOHN
b56137d2a0
[AMDGPU] Ensure all DPP instructions are convergent (#183334)
All DPP instructions in AMDGPUGenInstrInfo.inc are verified to now be
marked as convergent. This is necessary to prevent DPP instructions from
being incorrectly sunk by machine-sink.

Signed-off-by: John Lu <John.Lu@amd.com>
2026-02-26 08:57:48 -06:00
Matt Arsenault
8b41ad4430
AMDGPU: Stop adding uniform-work-group-size=false (#183502)
This is one of the string attributes that takes a boolean
value for no reason. There is no point in ever writing this
with an explicit false. Stop adding the noise and reporting
an unnecessary change.
2026-02-26 14:53:20 +00:00
Dark Steve
254cb2a326
[AMDGPU] Hoist WMMA coexecution hazard V_NOPs from loops to preheaders (#176895)
On GFX1250, V_NOPs inserted for WMMA coexecution hazards are placed at
the use-site. When the hazard-consuming instruction is inside a loop and
the WMMA is outside, these NOPs execute every iteration even though the
hazard only needs to be covered once.

This patch hoists the V_NOPs to the loop preheader, reducing executions
from N iterations to 1.

```
Example (assuming a hazard requiring K V_NOPs):
  Before:
    bb.0 (preheader): WMMA writes vgpr0
    bb.1 (loop):      V_NOP xK, VALU reads vgpr0, branch bb.1
                      -> K NOPs executed per iteration

  After:
    bb.0 (preheader): WMMA writes vgpr0, V_NOP xK
    bb.1 (loop):      VALU reads vgpr0, branch bb.1
                      -> K NOPs executed once
```

For nested loops, V_NOPs are hoisted to the outermost preheader where no
WMMA hazard exists within the loop.
Hoisting is restricted to strict preheaders (not any single predecessor)
to avoid introducing V_NOPs on unrelated control flow paths.

The optimization is controlled by `-amdgpu-wmma-vnop-hoisting` (default:
on).

Fixes: SWDEV-573407
2026-02-26 17:19:00 +05:30
David Stuttard
cd68939326
[AMDGPU] Add attribute for FWD_PROGRESS (#181675)
Added an attribute for FWD_PROGRESS that allows it to be
turned off for some shaders.
2026-02-26 11:35:36 +00:00
Jeffrey Byrnes
122e79c35f
[MISched] Advance HazardRec past stalls before calling EmitInstruction (#182977)
There are three calls to bumpCycle in bumpNode. Prior to the first call,
we calculate NextCycle as the next cycle in which all of a given
instruction's required hardware resources (as defined by the SchedModel)
are available. Any gap between this calculated NextCycle and CurrCycle
measures stalls that must occur before we can schedule the given
instruction.

The second and third call handle adjustments that occur during or after
issuing of the instruction (e.g. if the number of microops exceeds the
issue width).

According to the documentation of HazardRec->EmitInstruction, we should
call this method when an instruction is emitted: "This callback is
invoked when an instruction is emitted, to advance the hazard state."

In the context of bumpNode, this implies that it should be called after
we bumpCycle for stalls that must occur before issue of the
instructions, but before those that occur during or after. This PR moves
the placement to do that.

In practice, this affects schedulers that use both the SchedModel and
HazardRec. Suppose we have instructions A, B and C, and partial schedule
AB. Also, suppose instruction A exclusively holds ProcResource X for 2
cycles, and B uses ProcResource X, and there is a HazardRec hazard
between B and C which requires 1 cycle stall.

Currently, we call HazardRec->EmitInstruction on B before we call
HazardRec->AdvanceCycle for the stall between A->B. Then, when deciding
whether to schedule C, HazardRec sees that a cycle has already occurred
after B, so we do not need to stall.

After this change, we HazardRec->EmitInstruction on B after we call
HazardRec->AdvanceCycle for the stall between A->B. So, HazardRec
accurately places the stall cycle between A and B. Then, when deciding
whether to schedule C, HazardRec accurately sees that no cycles have
occurred after B, so we do need to stall for 1 cycle.
2026-02-25 15:24:54 -08:00
Daniil Fukalov
3031ba9545
[CodeGen] Expand power-of-2 div/rem at IR level in ExpandIRInsts. (#180654)
Previously, power-of-2 div/rem operations wider than
MaxLegalDivRemBitWidth were excluded from IR expansion and left for
backend peephole optimizations. Some backends can fail to process such
instructions in case we switch off DAGCombiner.

Now ExpandIRInsts expands them into shift/mask sequences:
- udiv X, 2^C  ->  lshr X, C
- urem X, 2^C  ->  and X, (2^C - 1)
- sdiv X, 2^C  ->  bias adjustment + ashr X, C
- srem X, 2^C  ->  X - (((X + Bias) >> C) << C)

Special cases handled:
- Division/remainder by 1 or -1 (identity, negation, or zero)
- Exact division (sdiv exact skips bias, produces ashr exact)
- Negative power-of-2 divisors (result is negated)
- INT_MIN divisor (correct via countr_zero on bit pattern)

Proofs: https://alive2.llvm.org/ce/z/Y-iWm-

Assisted-by: Cursor // Claude Opus 4.6
2026-02-25 22:54:34 +01:00
Stanislav Mekhanoshin
33fd75f55d
[AMDGPU] Add gfx12-5-generic subtarget (#183381)
This is functionally equivalent to gfx1250.
2026-02-25 13:34:48 -08:00
Matt Arsenault
4832c33f66
AMDGPU: Implement expansion for f64 exp (#182539)
I asked AI to port the device libs reference implementation.
It mostly worked, though it got the compares wrong and also
missed a fold that happened in compiler. With that fixed I get
identical DAG output, and almost the same globalisel output (differing
by an inverted compare and select). Also adjusted some stylistic
choices.
2026-02-25 21:06:13 +01:00
addmisol
b830bcfde3
[AMDGPU]Fix compute num sign bits unsigned underflow (#182723)
Fixes #182677

The `BFE_I32` case in `ComputeNumSignBitsForTargetNode` was not masking
the width operand with `& 0x1f`, unlike other BFE operations in the same
  file. Since the hardware instruction only uses the low 5 bits of the
  width field, values >= 32 passed via `@llvm.amdgcn.sbfe.i32` caused
  unsigned integer underflow in the calculation:

      unsigned SignBits = 32 - Width->getZExtValue() + 1;

  When width > 33, this underflows, producing incorrect SignBits values.
  When width == 33, SignBits becomes 0, violating the expected return
  range of [1, BitWidth]. This led to assertion failures and
  miscompilation where subsequent BFE narrowing operations were
  incorrectly eliminated.

  This patch:
  - Masks the width value with `& 0x1f` to match hardware behavior
  - Handles width == 0 (after masking) by returning 32 sign bits
  - Adds regression tests for width values >= 32
2026-02-25 09:55:09 +00:00
hidekisaito
75171b07c1
[AMDGPU] Add LIT tests for DS loop waitcnt prefetch flushed (tests-only PR) (#183217)
Test-only PR for PR #175658, and remove -O3 from PR #172728 LIT test per
post-merge comment
2026-02-24 17:37:32 -08:00
LU-JOHN
f3c5ab0388
[AMDGPU][NFC] Pre-commit memcpy test with complex constant length (#182170)
Test memcpy lowering with complex constant length.  Length is given by:

`i64 add (i64 sub (i64 16, i64 ptrtoint (ptr addrspacecast (ptr
addrspace(4) null to ptr) to i64)), i64 13)`

Thus, loop guard should not be needed.

---------

Signed-off-by: John Lu <John.Lu@amd.com>
2026-02-24 11:02:45 -06:00
Stanislav Mekhanoshin
9829d082af
[AMDGPU] Update gfx1250 sched model to latest table (#182923) 2026-02-23 13:25:07 -08:00
Shilei Tian
a56993a694
[AMDGPU] Remove FeaturePromoteAlloca (#177636)
It looks like `+promote-alloca` is always enabled, and `-promote-alloca`
is simply used as a switch to toggle the pass.
2026-02-23 15:24:57 -05:00
zGoldthorpe
9926ea9888
[AMDGPU][ISel] Reduce 64-bit setcc to upper 32 bits if lower 32 bits are known (#181238)
Truncate 64-bit integral `setcc`s to their upper 32-bit operands if
enough information is known about their lower 32-bit operands, subsuming
the special cases handled in #177662.

Alive2 verification for analogous IR transformations:
[xdATxK](https://alive2.llvm.org/ce/z/xdATxK)
2026-02-23 11:15:31 -07:00
Shilei Tian
36445f7b8f
[AMDGPU] Fix caller/callee mismatch in SGPR assignment for inreg args (#182754)
On the callee side, `LowerFormalArguments` marks SGPR0-3 as allocated in
`CCState` before running the CC analysis. On the caller side,
`LowerCall` (and GlobalISel's `lowerCall`/`lowerTailCall`) added the
scratch resource to `RegsToPass` without marking it in `CCState`. This
caused `CC_AMDGPU_Func` to treat SGPR0-3 as available on the caller
side, assigning user inreg args there, while the callee skipped them
without marking it in `CCState`. This caused `CC_AMDGPU_Func` to treat
SGPR0-3 as available on the caller side, assigning user inreg args
there, while the callee skipped them.
2026-02-23 18:13:41 +00:00
Nikita Popov
13530d4470
[AMDGPUCtorDtorLowering] Remove unnecessary pointer arithmetic (#182869)
This code was computing `begin + ((end - begin) exact/ 8) * 8`, which is
a very complicated way to spell end.

This is the AMDGPU edition of
https://github.com/llvm/llvm-project/pull/182269.
2026-02-23 17:04:49 +01:00
Shilei Tian
239e14baa4
[NFC][AMDGPU] Add test showing caller/callee SGPR mismatch for inreg args (#182753)
Add a test demonstrating a bug where the caller and callee disagree on
which SGPRs hold user inreg arguments when there are enough to reach the
SGPR0-3 range.

On the callee side, `LowerFormalArguments` marks SGPR0-3 as allocated in
`CCState` before the CC analysis runs. On the caller side, `LowerCall`
adds the scratch resource to `RegsToPass` without marking SGPR0-3 in
`CCState`. This causes `CC_AMDGPU_Func` to assign user inreg args to
SGPR0-3 on the caller side (they appear free) while the callee skips
them.

In the test, the caller writes arg 0 (value 42) to s0, but the callee
reads arg 0 from s16.
2026-02-23 10:49:50 -05:00
hjagasiaAMD
4f30127f44
[AMDGPU] fix eliminateFrameIndex to use SGPR frame index (#178991)
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2026-02-23 09:12:38 -06:00
Shilei Tian
a9645ebadf
[AMDGPU] Move advanceBeforeNext before advanceToNext (#182808)
The document of `advanceToNext` says, `advanceBeforeNext` has to be
called first.
2026-02-23 10:00:03 -05:00
Björn Pettersson
5e5e300d07
[SelectionDAG] Fix bug related to demanded bits/elts for BITCAST (#145902)
When we have a BITCAST and the source type is a vector with smaller
elements compared to the destination type, then we need to demand all
the source elements that make up the demanded elts for the result when
doing recursive calls to SimplifyDemandedBits,
SimplifyDemandedVectorElts and SimplifyMultipleUseDemandedBits. Problem
is that those simplifications are allowed to turn non-demanded elements
of a vector into POISON, so unless we demand all source elements that
make up the result there is a risk that the result would be more
poisonous (even for demanded elts) after the simplification.

The patch fixes some bugs in SimplifyMultipleUseDemandedBits and
SimplifyDemandedBits for situations when we did not consider the problem
described above. Now we make sure that we also demand vector elements
that "must not be turned into poison" even if those elements correspond
to bits that does not need to be defined according to the DemandedBits
mask.

Fixes #138513
2026-02-23 14:38:07 +01:00
Dark Steve
9e6a6be8a8
[AMDGPU] Remove AMDGPUArgumentUsageInfo pass (#182490)
`AMDGPUArgumentUsageInfo` provided a per-function map that
`lowerFormalArguments` would write each function's implicit argument
register layout into, and `passSpecialInputs` would read back when
lowering calls to look up the callee's layout. This per-function map is
redundant for all non-entry callees, which already use the same
`FixedABIFunctionInfo` register layout.

GlobalISel already used `FixedABIFunctionInfo` unconditionally. This
change makes SelectionDAG do the same.
2026-02-23 18:47:01 +05:30
Changpeng Fang
99dc561c7d
[AMDGPU] Use a general form of intrinsic for tensor load/store (#182334)
The intrinsic has five arguments for the tensor descriptor (D#), while the fifth one is reserved for future targets, and it will be silently ignored in codegen for gfx1250.
  For tensor up to 2D, only the first two D# groups are meaningful and the rest should be zero-initialized.
2026-02-20 17:28:32 -08:00
vangthao95
faf050cd13
AMDGPU/GlobalISel: Regbanklegalize rules for G_FMIN*/MAX* (#179778) 2026-02-20 09:44:58 -08:00
zGoldthorpe
20dba979f7
[AMDGPU] Add target features to guard DPP controls (#182391)
This patch adds target features:
- `+dpp-wavefront-shifts`, for DPP `wave_shl/rol/shr/ror`
- `+dpp-row-bcast`, for DPP `row_bcast15/31`

These DPP controls are not available in gfx10+, so these target features
enable `AMDGPURemoveIncompatibleFunctions` to remove functions that rely
on these controls when compiling for newer GPUs.
2026-02-20 07:59:10 -07:00
LU-JOHN
e0f2b1b4c6
[AMDGPU] Ensure all PERMLANE instructions are marked as convergent (#182162)
All PERMLANE instructions in AMDGPUGenInstrInfo.inc were verified to now
be marked as convergent. This is necessary to prevent PERMLANE
instructions from being incorrectly sunk by machine-sink.

---------

Signed-off-by: John Lu <John.Lu@amd.com>
2026-02-20 07:02:47 -06:00
Petar Avramovic
f5889e59e6
AMDGPU/GlobalISel: Regbanklegalize rules for INTRIN_IMAGE (#179810)
Regbanklegalize rules for INTRIN_IMAGE loads and stores.
Because of very large number of different type signatures, rule specifies
only function for lowering (waterfall lowering of RsrcIdx operand if needed)
and this function also applies register banks.
2026-02-20 13:34:13 +01:00
Anshil Gandhi
1084aef234
[AMDGPU] Add legalization rules for G_AMDGPU_BUFFER_ATOMIC_FADD (#175258)
Support G_AMDGPU_BUFFER_ATOMIC_FADD for f32, f64 and <2 x f16>.
2026-02-20 11:30:05 +00:00
Chinmay Deshpande
918ef6cfaf
[AMDGPU][GISel] Add RegbankLegalize Rules for G_ASSERT_ALIGN (#182353) 2026-02-19 12:30:31 -08:00
Tony Linthicum
ea27498a5c
[AMDGPU] Fix handling of setting register classes in MFMA scheduler rewrite stage (#181047)
Fixes problem with setting/resetting register classes in the MFMA
rewrite sched stage. The code assumed that the dest and OpC would be the
same class. This is not true if one uses subregs. This fixes issue
#177696.
2026-02-19 21:01:03 +01:00
michaelselehov
ed0ba3cb45
[AMDGPU] Align loop headers to prevent instruction fetch split on GFX950 (#181999)
On GFX9, the instruction sequencer fetches 32 bytes at a time. When an
8-byte instruction at a loop header straddles a 32-byte fetch window
boundary, the sequencer must perform two fetches after a backward
branch, incurring a delay. On GFX950, this causes additional performance
issues.

This patch adds 32-byte alignment (.p2align 5, , 4) for loop headers on
GFX950 when the first real instruction is 8 bytes. At most one s_nop (4
bytes, 1 quad-cycle before the loop) is used for padding. If more than 4
bytes of padding were needed, the 8-byte instruction would not straddle
a 32-byte boundary anyway, so alignment is skipped.

Note: the alignment decision is made during block-placement, before
si-insert-waitcnts. In loops where a 4-byte S_WAITCNT is later inserted
as the first instruction, the alignment becomes redundant but mostly
harmless (at most one extra s_nop per affected loop).

Assisted-by: Claude (Anthropic)
2026-02-19 14:18:44 -05:00
Chinmay Deshpande
f437315bc3
[AMDGPU][GISel] Add RegBankLegalize support for G_SI_CALL (#165747) 2026-02-19 09:48:21 -08:00
vangthao95
f446cbb590
AMDGPU/GlobalISel: RegBankLegalize for amdgcn_bitop3 (#181877) 2026-02-19 09:41:34 -08:00
hidekisaito
969a326386
[AMDGPU] BackOffBarrier feature added to gfx1250; Removed incorrect "DS Store drain" check. (#179818)
Missing BackOffBarrier feature added to gfx1250.
Checking for S_BARRIER only does not imply prior DS Stores getting
drained.
2026-02-19 08:41:44 -08:00
Jay Foad
78798b20d9
[AMDGPU] Fix duplicate RUN lines in tests (#182286) 2026-02-19 16:07:20 +00:00
Matt Arsenault
fdc4274e2f
AMDGPU: Perform libcall recognition to replace fast OpenCL pow (#182135)
If a float-typed call site is marked with afn, replace the 4
flavors of pow with a faster variant.

This transforms pow, powr, pown, and rootn to __pow_fast,
__powr_fast, __pown_fast, and __rootn_fast if available. Also
attempts to handle all of the same basic folds on the new fast
variants that were already performed with the base forms. This
maintains optimizations with OpenCL when the device libs unsafe
math control library is deleted. This maintains the status quo
of how libcalls work, and only handles 4 new entry points. This
only helps with the elimination of the control library, and not
general libcall emission problems.

This makes no practical difference for HIP, which is the status
quo for libcall optimizations. AMDGPULibCalls recognizes the OpenCL
mangled names. e.g., OpenCL float "pow" is really _Z3powff but the
HIP provided function "powf" is really named _ZL4powfff, and std::pow
with float is _ZL3powff. The pass still runs for HIP, so by accident
if you used the OpenCL mangled function names, this would trigger.

Since the functions cannot yet be relied on from the library,
introduce a temporary module flag check. I'm not planning on emitting
it anywhere and it's a poor substitute for versioning the target.
2026-02-19 11:49:32 +01:00
Alex MacLean
3e3713c2f5
[GlobalIsel] Use aext in ctlz_zero_undef widenScalar expansion (#181506)
Use `G_ANYEXT` instead of `G_ZEXT` when widening the source of
`G_CTLZ_ZERO_UNDEF`. The extended upper bits are immediately shifted out
by the subsequent left-shift, so zero-extending is unnecessarily
constraining.

Before:
```
    %wide = G_ZEXT %src
    %shifted = G_SHL %wide, sizeDiff
    %result = G_CTLZ_ZERO_UNDEF %shifted
```
After:
```
    %wide = G_ANYEXT %src
    %shifted = G_SHL %wide, sizeDiff
    %result = G_CTLZ_ZERO_UNDEF %shifted
```
2026-02-18 21:38:17 -08:00
Stanislav Mekhanoshin
d15c6ee288
[AMDGPU] Commute instructions to avoid VGPR MSB changes (#181918) 2026-02-18 19:44:50 -08:00
Matt Arsenault
dee675d1d6 AMDGPU: Update another stale test
Missed in 82a586d0095b2f2fe908c967864ce75c15d39381
2026-02-18 22:02:14 +01:00