8356 Commits

Author SHA1 Message Date
Akshat Oke
6c87ec4f4d
[AMDGPU][NPM] Port SIModeRegister to NPM (#129014) 2025-03-04 10:51:03 +05:30
Matt Arsenault
8476a5d480
SplitKit: Fix rematerialization undoing subclass based split (#122110)
This fixes an allocation failure in the new test.

In cases where getLargestLegalSuperClass can inflate the register class,
rematerialization could effectively undo a split which was done to
inflate
the register class, if the defining instruction can only write a
subclass
and the use can read the superclass.

Some of the x86 tests changes look like improvements, but some are
likely regressions.

I'm not entirely sure this is the correct place to fix this. It also
seems more complicated than necessary, but the decision to change
the register class is far removed from the point where the decision
to split the virtual register is made. I'm also also not sure if this
should be considering the register classes of all the use indexes
in getUseSlots, rather than just checking if this use index instruction
reads the register.
2025-03-04 10:04:14 +07:00
Jeffrey Byrnes
3963d21482
[MachineSink] Fix typo in loop sinking (#127133)
Failure to sink a candidate should not block us from attempting to sink
other candidates. There are mechanisms in place to handle the case where
the failed to be sunk instruction uses an instruction that gets sunk (we
do not delete the original instruction corresponding with the sunk
instruction if it still has uses).
2025-03-03 17:30:12 -08:00
Stanislav Mekhanoshin
6c9a9d9fe2
[AMDGPU] Set inst_pref_size to maximum (#126981)
On gfx11 and gfx12 set initial instruction prefetch size to a
minimum of kernel size and maximum allowed value.

Fixes: SWDEV-513122
2025-03-03 10:40:31 -08:00
Frederik Harwath
ba9bd22e1b
[AMDGPU] Account for existing SDWA selections (#123221)
The si-peephole-sdwa pass adjusts the selections on sdwa instructions to
the selections on their operands during its conversions. For instance,
if an instruction selects `BYTE_0` and its operand selects `WORD_1`, the
combined selection should be `BYTE_2`, i.e. "`BYTE_0` of `WORD_1`". The
existing implementation does not always handle this correctly in some
complex situations with instructions across different basic blocks as
demonstrated by the test cases included in this PR.

This PR adds an additional selection combination step to the conversion
to fix this issue. It reverts the changes made by PR #123942 which had
disabled the conversion of preexisting SDWA instructions completely as a
quick fix.

---------

Co-authored-by: Jeffrey Byrnes <Jeffrey.Byrnes@amd.com>
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-03-03 17:07:28 +01:00
Akshat Oke
77f44a9642
[CodeGen][NewPM] Port MachineSink to NPM (#115434)
Targets can set the EnableSinkAndFold option in CGPassBuilderOptions for
the NPM pipeline in buildCodeGenPipeline(... &Opts, ...)
2025-03-03 15:49:37 +05:30
Matt Arsenault
49a533a485
AMDGPU: Stop introducing v_accvgpr_write_b32 for reg-to-reg copy (#129059)
This was trying to hack around the intermediate VGPR requirement
to copy to AGPRs on gfx908. We should still use a copy for all
reg-to-reg cases. This should matter less these days, as we
reserve a VGPR to handle it when required (and no end to end tests
need updating).

This was also an obstacle to handling this fold for input registers
which are larger than 32-bits.
2025-03-03 16:22:47 +07:00
Matt Arsenault
7be8b78f82
AMDGPU: Add mir test for agpr constant reg_sequence handling (#129058) 2025-03-03 16:19:02 +07:00
Pedro Lobo
3c80d9b8dd
[Instruction] Set metadata to poison on deletion (#129449)
Represent extant metadata uses of a deleted instruction with `poison`
instead of `undef`.
2025-03-03 07:17:01 +07:00
Johannes Doerfert
992b451f08
[Utils][UnifyLoopExits] Avoid costly updates if nothing changed (#129179)
If the ControlFlowHub did not perform any change to the control flow,
there is no need to repair SSA, update the loop structure, and verify a
bunch of things. This is not completely NFC though, repairSSA introduced
PHI nodes with a single entry that are now missing.

My code went from 400+ seconds to 1 second, since no loop required the
exits to be unified, but there were many "complex" loops.
2025-02-28 10:14:31 -08:00
Brox Chen
db973cea7c
[AMDGPU][True16][CodeGen] True16 Add OpSel when optimizing exec mask (#128928)
True16 Add OpSel when optimizing exec mask

True16 VOPCX have the opsel argument. Add it when we create these
instructions in SIOptimizeExecMasking.

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-02-28 09:41:05 -05:00
Matt Arsenault
6a46cf4dc6
AMDGPU/GlobalISel: Restore disabled test (#129001) 2025-02-28 17:24:28 +07:00
Krzysztof Drewniak
f8cc509b69
Reapply "[AMDGPU] Handle memcpy()-like ops in LowerBufferFatPointers (#126621)" (#129078)
This reverts commit 1559a65efaf327f9c72e14d4bb1834f076e7fc20.

Fixed test (I suspect broken by unrelated change in the merge)
2025-02-27 11:26:13 -06:00
Brox Chen
8635b8eb51
[AMDGPU][True16][MC] true16 for v_alignbit_b32 (#119409)
Support true16 format for v_alignbit_b32 in MC.

Since we are replacing `v_alignbit_b32` to
`v_alignbit_b32_t16/v_alignbit_b32_fake16` in Post-GFX11, have to update
the CodeGen pattern for `v_alignbit_b32_fake16` to get CodeGen test
passing. There is no pattern modified/created, but just replacing the
`v_alignbit_b32` with fake16 format.

Some of the true16 CodeGen test are impacted since `v_alignbit_b32`
selection are removed in Post-GFX11 while `v_alignbit_b32_t16` are not
yet supported. The CodeGen patch for `v_alignbit_b32_t16` will be done
in the following patch.
2025-02-27 11:00:01 -05:00
Matt Arsenault
a88f4f1962 AMDGPU: Fix a test typo reading a partially undefined vector
This avoids a surprising test diff in a future commit that
happened to change the read registers to something else. Also
migrate from undef to poison.
2025-02-27 20:17:20 +07:00
Matt Arsenault
040860accb AMDGPU: Add a mir variant of a regalloc failure test
I have a pending patch which improves the codegen in the original IR
version, such that the allocation no longer fails. I'm still trying
to preserve the failure from IR, but add a version with a snapshot
of the current MIR before the failing RA run.
2025-02-27 20:05:38 +07:00
Lucas Ramirez
15e295d30a
[MachineScheduler][AMDGPU] Allow scheduling of single-MI regions (#128739)
The MI scheduler skips regions containing a single MI during scheduling.
This can prevent targets that perform multi-stage scheduling and move
MIs between regions during some stages to reason correctly about the
entire IR, since some MIs will not be assigned to a region at the
beginning.

This makes the machine scheduler no longer skip single-MI regions. Only
a few unit tests are affected (mainly those which check for the
scheduler's debug output).
2025-02-27 11:27:07 +01:00
Matt Arsenault
a3165398db
AMDGPU: Fix overly conservative immediate operand check (#127563)
The real legality check is peformed later anyway, so this was
unnecessarily blocking immediate folds in handled cases.

This also stops folding s_fmac_f32 to s_fmamk_f32 in a few tests,
but that seems better. The globalisel changes look suspicious,
it may be mishandling constants for VOP3P instructions.
2025-02-27 08:42:09 +07:00
Matt Arsenault
4be4133a9f
AMDGPU: Do not try to commute instruction with same input register (#127562)
There's little point to trying to commute an instruction if the
two operands are already the same.

This avoids an assertion in a future patch, but this likely isn't the
correct fix. The worklist management in SIFoldOperands is dodgy, and
we should probably fix it to work like PeepholeOpt (i.e. stop looking
at use lists, and fold from users). This is an extension of the already
handled special case which it's trying to avoid folding an instruction
which is already being folded.
2025-02-27 08:39:03 +07:00
Matt Arsenault
d708bfb3c0
AMDGPU: Fix si-fix-sgpr-copies asserting on VReg_1 phi (#128903) 2025-02-27 06:49:47 +07:00
Kazu Hirata
1559a65efa Revert "[AMDGPU] Handle memcpy()-like ops in LowerBufferFatPointers (#126621)"
This reverts commit 469757efafebdd5772d993fca4dc0dfa7cbda17c.

Multiple buildbot failures have been reported:
https://github.com/llvm/llvm-project/pull/126621
2025-02-26 14:35:07 -08:00
Daniel Thornburgh
02128342d2
Revert "DAG: Preserve range metadata when load is narrowed" (#128948)
Reverts llvm/llvm-project#128144

Breaks clang prod x64 build (seen in Fuchsia toolchain)
2025-02-26 14:14:55 -08:00
Krzysztof Drewniak
469757efaf
[AMDGPU] Handle memcpy()-like ops in LowerBufferFatPointers (#126621)
Since LowerBufferFatPointers runs before PreISelIntrinsicLowering, which
normally handles unsupported memcpy()s,, and since you can't have a
`noalias {ptr addrspace(8), i32}` becasue it crashes later passes,
manually expand memcpy()s involving buffer fat pointers to loops.

Additionally, though they're unlikely to be used, this commit adds
support for memset().

This commit doesn't implement writing direct-to-LDS loads as the
intrinsics, but leaves the option in the future.
2025-02-26 16:03:32 -06:00
Brox Chen
364b97f23b
[AMDGPU][True16][CodeGen] 16bit spill support in true16 mode (#128060)
Enables 16-bit values to be spilled to scratch.

Note, the memory instructions used are defined as reading and writing
VGPR_32, but do not clobber the unspecified 16-bits of those registers,
and so spills and reloads of lo and hi halves of the registers work.
2025-02-26 16:17:20 -05:00
Brox Chen
dfda75f2e5
[AMDGPU][True16][CodeGen] fix test for true16 codegen valu op (#128905)
This is a NFC change. Update the test file and fix the build

https://github.com/llvm/llvm-project/pull/124797 is causing a build
issue
2025-02-26 23:55:29 +07:00
Brox Chen
bb62af7d14
[AMDGPU][True16][CodeGen] true16 codegen for valu op (#124797)
true16 selection for valu ops, enable `real-true16` attribute and update
the codegen test
2025-02-26 10:50:49 -05:00
Pierre van Houtryve
0f0d3fb6b5
[AMDGPU] Do not allow M0 as v_readlane_b32 dst (#128867)
See #128851 - this is the same patch, but for v_readlane_b32.

This instruction is used much less often so there were less changes
required.
2025-02-26 14:13:39 +01:00
Pierre van Houtryve
5231736329
[AMDGPU] Do not allow M0 as v_readfirstlane_b32 dst (#128851)
M0 can only be written to by the SALU, so `v_readfirstlane_b32 m0` is
effectively useless. Represent this by restricting the dest RC of that
instruction to `SReg_32_XM0` which excludes M0.

There is a lot of test changes due to the register class changing, but
most changes are trivial. In some cases, an extra register and
`s_mov_b32` is needed.

Fixes SWDEV-513269
2025-02-26 13:14:03 +01:00
LU-JOHN
d8bcb53780
DAG: Preserve range metadata when load is narrowed (#128144)
In DAGCombiner.cpp preserve range metadata when load is narrowed to load
LSBs if original range metadata bounds can fit in the narrower type.

Utilize preserved range metadata to reduce 64-bit shl to 32-bit shl.

---------

Signed-off-by: John Lu <John.Lu@amd.com>
2025-02-26 15:40:49 +07:00
Matt Arsenault
1a114fa302
RegAlloc: Use new approach to handling failed allocations (#128469)
This fixes an assert after allocation failure.

Rather than collecting failed virtual registers and hacking
on the uses after the fact, directly hack on the uses and rewrite
the registers to the dummy assignment immediately.

Previously we were bypassing LiveRegMatrix and directly assigning
in the VirtRegMap. This resulted in inconsistencies where illegal
overlapping assignments were missing. Rather than try to hack in
some system to manage these in LiveRegMatrix (i.e. hacking around
cases with invalid iterators), avoid this by directly using the
physreg. This should also allow removal of special casing in
virtregrewriter for failed allocations.
2025-02-26 15:34:47 +07:00
Matt Arsenault
e160c35c9e
Reapply "RegAlloc: Fix verifier error after failed allocation (#119690)" (#128400)
Reapply "RegAlloc: Fix verifier error after failed allocation (#119690)"

This reverts commit 0c50054820799578be8f62b6fd2cc3fbc751c01e.

Reapply with more fixes to avoid expensive_checks failures. Make sure to
call splitSeparateComponents after shrinkToUses, and update the VirtRegMap
with the split registers. Also set undef on all physical register aliases to
the assigned register.

Move physreg handling. Not sure if necessary

Remove intervals from regunits. Not sure if necessary
2025-02-26 15:31:48 +07:00
Matt Arsenault
75aff78f64
RegAllocFast: Fix verifier errors after assigning to reserved registers (#128281) 2025-02-26 13:22:53 +07:00
Matt Arsenault
b5dd1fedc5
VirtRegRewriter: Fix verifier errors after regalloc failures (#128280) 2025-02-26 13:19:49 +07:00
Akshat Oke
852923822f
[AMDGPU][NewPM] Port AMDGPUInsertDelayAlu to NPM (#128003) 2025-02-26 09:50:09 +05:30
Brox Chen
c8b40867d1
[AMDGPU][True16][CodeGen] test fix for uaddsat/usubsat true16 selection (#128784)
This is a NFC change. Update the test file and fix the build

https://github.com/llvm/llvm-project/pull/128233 is causing a build
issue. This is caused by PR
https://github.com/llvm/llvm-project/pull/127945 being merged while the
128233 is pending for review.
2025-02-25 20:04:57 -05:00
Eli Friedman
1b39328d74
[CodeGen] Fix MachineInstr::isSafeToMove handling of inline asm. (#126807)
Even if an inline asm doesn't have memory effects, we can't assume it's
safe to speculate: it could trap, or cause undefined behavior. At the
LLVM IR level, this is handled correctly: we don't speculate inline asm
(unless it's marked "speculatable", but I don't think anyone does that).
Codegen also needs to respect this restriction.

This change stops Early If Conversion and similar passes from
speculating an INLINEASM MachineInstr.

Some uses of isSafeToMove probably could be switched to a different API:
isSafeToMove assumes you're hoisting, but we could handle some forms of
sinking more aggressively. But I'll leave that for a followup, if it
turns out to be relevant.

See also discussion on gcc bugtracker
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102150 .
2025-02-25 15:29:12 -08:00
Brox Chen
e6f6a1e863
[AMDGPU][True16][CodeGen] uaddsat/usubsat true16 selection in gisel (#128233)
Enable gisel selection for uaddsat and usubsat in true16 flow

This patch includes:

1. Added VGPR_16_Lo128/VGPR_16 to register bank and update register info
for recognizing 16bit regclass id and bit width
2. uaddsat/usubsat test update
2025-02-25 17:09:34 -05:00
Matt Arsenault
1affadb7c6
AMDGPU: Drop legacy r600.read.global.size intrinsics from amdgcn (#128700)
These ancient intrinsics were still consumed by the backend for libclc,
which no longer uses them.
2025-02-25 22:21:03 +07:00
Vikash Gupta
352c48f278
[SelectionDAG] Utilizing target hook convertSelectOfConstantsToMath for SelectwithConstant (#127599)
The Target hook convertSelectOfConstantsToMath() needs to be used within
SimplifySelectCC helper combine function in SelectionDAG Isel, where
generic select folding with constants is happening into simple maths op
using the condition as it is.

It necessarily fixes #121145.
2025-02-25 20:32:24 +05:30
Matt Arsenault
f95ad44068
AMDGPU: Mark v_mov_b64_pseudo as a VOP1 instruction (#128677)
This is mostly true, and it tricks the rematerialization
code into handling this without special casing it.
2025-02-25 18:22:32 +07:00
Vikash Gupta
49f60b4e09
[AMDGPU][NFC] Added test for live-in CSR SGPR used partially giving MachineVerifier error (#126696) 2025-02-25 14:32:37 +05:30
Matt Arsenault
d7903c9f28 AMDGPU: Add more codegen tests for readfirstlane 2025-02-25 14:45:26 +07:00
Matt Arsenault
06f3079235
AMDGPU: More consistently use the fold list instead of direct mutation (#127612)
There were 2 parallel fold check mechanisms, so consistently use the
fold list. The worklist management here is still not good. Other types
of folds are not using it, and we should probably rewrite the pass to
look more like peephole-opt.

This should be an alternative fix to skipping commute if the operands
are the same (#127562). The new test is still not broken as-is, but
demonstrates failures in a future patch.
2025-02-25 12:02:03 +07:00
Sumanth Gundapaneni
4c9e14b3ad
[AMDGPU] Update PromoteAlloca to handle GEPs with variable offset. (#122342)
In case of variable offset of a GEP that can be optimized out, promote
alloca is updated to use the refereshed index to avoid an assertion.

Issue found by fuzzer.

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-02-24 13:36:30 -06:00
Matt Arsenault
6aea6308d1
AMDGPU: Fix creating illegally typed readfirstlane in atomic optimizer (#128388)
We need to promote 8/16-bit cases to 32-bit. Unfortunately we are
missing demanded bits optimizations on readfirstlane, so we end up
emitting
an and instruction on the input. I'm also surprised this pass isn't
handling
half or bfloat yet.
2025-02-24 18:39:49 +07:00
Simon Pilgrim
7de64925da
[DAG] shouldReduceLoadWidth - hasOneUse should check just the loaded value - not the chain (#128167)
The hasOneUse check was failing in any case where the load was part of a chain - we should only be checking if the loaded value has one use, and any updates to the chain should be handled by the fold calling shouldReduceLoadWidth.

I've updated the x86 implementation to match, although it has no effect here yet (I'm still looking at how to improve the x86 implementation) as the inner for loop was discarding chain uses anyway.

By using SDValue::hasOneUse instead this patch exposes a missing dependency on the LLVMSelectionDAG library in a lot of tools + unittests, which resulted in having to make SDNode::hasNUsesOfValue inline.

Noticed while fighting the x86 regressions in #122671
2025-02-24 11:09:41 +00:00
Yeaseen
96c723374a
[llvm] Remove br i1 undef from some llvm/test/CodeGen tests (#128272) 2025-02-23 09:23:33 +00:00
Matt Arsenault
ccad5e7744
AMDGPU: Respect amdgpu-no-agpr in functions and with calls (#128147)
Remove the MIR scan to detect whether AGPRs are used or not,
and the special case for callable functions. This behavior was
confusing, and not overridable. The amdgpu-no-agpr attribute was
intended to avoid this imprecise heuristic for how many AGPRs to
allocate. It was also too confusing to make this interact with
the pending amdgpu-num-agpr replacement for amdgpu-no-agpr.

Also adds an xfail-ish test where the register allocator asserts
after allocation fails which I ran into.

Future work should reintroduce a more refined MIR scan to estimate
AGPR pressure for how to split AGPRs and VGPRs.
2025-02-23 09:00:37 +07:00
Matt Arsenault
1bb43068f1
PeepholeOpt: Allow introducing subregister uses on reg_sequence (#127052)
This reverts d246cc618adc52fdbd69d44a2a375c8af97b6106. We now handle
composing subregister extracts through reg_sequence.
2025-02-22 09:16:14 +07:00
Brox Chen
61c6e0061c
[AMDGPU][True16][CodeGen] flat/global/scratch load/store pseudo for true16 (#127945)
T16D16 table is implemented in
https://github.com/llvm/llvm-project/pull/127673

this is a follow up patch to add load/store pseudo for:
flat_store 
global_load/global_store
scratch_load/scratch_store

in true16 mode and updated the codegen test file
2025-02-21 17:06:48 -05:00