8972 Commits

Author SHA1 Message Date
Shilei Tian
e801a10b44
[AMDGPU] Add the code generation support for llvm.[sin/cos].bf16 (#149631)
This is a partial support because some other instructions have not been upstreamed yet.
2025-07-21 11:01:59 -04:00
Shilei Tian
ba81903196
[gfx1250][SDAG] Lower unsafe bf16 divisions (#149628)
Co-authored-by: Kosarev, Ivan <Ivan.Kosarev@amd.com>
2025-07-21 10:58:08 -04:00
Shilei Tian
ce345cc793
[NFC][AMDGPU] Add an IR test for v_cvt_f16_bf8 (#149627)
This was left during the upstream.

Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-21 10:54:55 -04:00
Shilei Tian
5d3b0578d8
[NFC][AMDGPU] Add an IR test for v_sqrt_bf16 (#149629)
This was left during the upstream.

Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-21 09:40:14 -04:00
David Green
3b8adcfd92
[GlobalISel] Add computeNumSignBits for ASHR (#139503) 2025-07-21 10:23:26 +01:00
Jay Foad
5a9c201bc9
[CodeGen] Fix INLINEASM regclass numbers to match names in tests (#142359)
INLINEASM operands like "2097162 /* regdef:SReg_32 */" tend to get
broken over time as the register class definitions get updated, so the
numbers change. Fix the numbers to match the names in the comments.
2025-07-21 10:23:08 +01:00
Jeremy Morse
040bffc633
[DebugInfo][AMDGPU] Convert a debug-intrinsic method to debug records (#149505)
It appears this wasn't handled in the initial migration a year ago,
seemingly because it didn't lead to any test failures. Find and interpret
debug records in the same way the original code handled intrinsics. Note
that we drop a call to copyMetadata: debug records can't carry additional
metadata like instructions, nothing relies on this in AMDGPU AFAIUI.
2025-07-21 10:07:14 +01:00
Diana Picus
20d8398825
[AMDGPU] ISel & PEI for whole wave functions (#145858)
Whole wave functions are functions that will run with a full EXEC mask.
They will not be invoked directly, but instead will be launched by way
of a new intrinsic, `llvm.amdgcn.call.whole.wave` (to be added in
a future patch). These functions are meant as an alternative to the
`llvm.amdgcn.init.whole.wave` or `llvm.amdgcn.strict.wwm` intrinsics.

Whole wave functions will set EXEC to -1 in the prologue and restore the
original value of EXEC in the epilogue. They must have a special first
argument, `i1 %active`, that is going to be mapped to EXEC. They may
have either the default calling convention or amdgpu_gfx. The inactive
lanes need to be preserved for all registers used, active lanes only for
the CSRs.

At the IR level, arguments to a whole wave function (other than
`%active`) contain poison in their inactive lanes. Likewise, the return
value for the inactive lanes is poison.

This patch contains the following work:
* 2 new pseudos, SI_SETUP_WHOLE_WAVE_FUNC and SI_WHOLE_WAVE_FUNC_RETURN
  used for managing the EXEC mask. SI_SETUP_WHOLE_WAVE_FUNC will return
  a SReg_1 representing `%active`, which needs to be passed into
  SI_WHOLE_WAVE_FUNC_RETURN.
* SelectionDAG support for generating these 2 new pseudos and the
  special handling of %active. Since the return may be in a different
  basic block, it's difficult to add the virtual reg for %active to
  SI_WHOLE_WAVE_FUNC_RETURN, so we initially generate an IMPLICIT_DEF
  which is later replaced via a custom inserter.
* Expansion of the 2 pseudos during prolog/epilog insertion. PEI also
  marks any used VGPRs as WWM registers, which are then spilled and
  restored with the usual logic.

Future patches will include the `llvm.amdgcn.call.whole.wave` intrinsic
and a lot of optimization work (especially in order to reduce spills
around function calls).

---------

Co-authored-by: Matt Arsenault <Matthew.Arsenault@amd.com>
Co-authored-by: Shilei Tian <i@tianshilei.me>
2025-07-21 10:39:09 +02:00
Jasmine Tang
e7ac49977a
[InstSimplify] Add poison propagation for trivially vectorizable intrinsics (#149243)
Fixes https://github.com/llvm/llvm-project/issues/146769

Test cases added to
`llvm/test/Transforms/InstSimplify/fold-intrinsics.ll`
2025-07-19 19:37:21 -07:00
Shilei Tian
d46de86ca4
[NFC][AMDGPU] Re-enable two tests previously disabled due to missing upstream features (#149568)
This PR re-enables two tests that were previously disabled because they
depended on features not yet upstreamed.
2025-07-18 17:04:34 -04:00
Jeffrey Byrnes
695660cdfd
[AMDGPU] Provide control to force VGPR MFMA form (#148079)
This gives an override to the user to force select VGPR form of MFMA.
Eventually we will drop this in favor of compiler making better
decisions, but this provides a mechanism for users to address the cases
where MayNeedAGPRs favors the AGPR form and performance is degraded due
to poor RA.
2025-07-18 13:53:17 -07:00
Stanislav Mekhanoshin
cfa918bec1
[AMDGPU] Select flat GVS atomics on gfx1250 (#149554) 2025-07-18 12:31:29 -07:00
Shilei Tian
2c50e4cac2
[AMDGPU] Add support for v_sat_pk4_i4_[i8,u8] on gfx1250 (#149528)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
Co-authored-by: Foad, Jay <Jay.Foad@amd.com>
2025-07-18 13:08:50 -04:00
Shilei Tian
e11d28faee
[AMDGPU] Add support for v_permlane16_swap_b32 on gfx1250 (#149518)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-18 13:05:08 -04:00
Brox Chen
5138b61a25
[AMDGPU][True16][Codegen] remove packed build_vector pattern from true16 (#148715)
Some of the packed build_vector use vgpr_32 for i16/f16/bf16. 

In gfx11, bf16 arithmetic get promoted to f32 and this is done via v2i16
pack. In true16 mode this v2i16 pack is selected to a
build_vector/v_lshlrev pattern which only accepts VGPR32. This causes
isel to insert an illegal copy "vgpr32 = copy vgpr16" between def and
use. In the end this illegal copy confuses cse pass and trigger wrong
code elimination.

Remove the packed build_vector pattern from true16. After removal, ISel
will use vgpr16 build_vector patterns instead.
2025-07-18 12:55:11 -04:00
Shilei Tian
95b69e0e70
[AMDGPU] Add support for v_prng_b32 on gfx1250 (#149450)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-18 10:59:47 -04:00
Matt Arsenault
d883d5fecf AMDGPU: Add testcase with bad regalloc behavior
This demonstrates poor allocation due to not ordering
AV classes relative to the A and V classes
2025-07-18 17:46:54 +09:00
Matt Arsenault
8f3e78f971
AMDGPU: Add pass to replace constant materialize with AV pseudos (#149292)
If we have a v_mov_b32 or v_accvgpr_write_b32 with an inline immediate,
replace it with a pseudo which writes to the combined AV_* class. This
relaxes the operand constraints, which will allow the allocator to
inflate the register class to AV_* to potentially avoid spilling.

The allocator does not know how to replace an instruction to enable
the change of register class. I originally tried to do this by changing
all of the places we introduce v_mov_b32 with immediate, but it's along
tail of niche cases that require manual updating. Plus we can restrict
this to only run on functions where we know we will be allocating AGPRs.
2025-07-18 17:15:38 +09:00
Fabian Ritter
efedd49a22
[AMDGPU][SDAG] Test ISD::PTRADD handling in VOP3 patterns (#143880)
Pre-committing tests to show improvements in a follow-up PR.
2025-07-18 10:04:51 +02:00
Fabian Ritter
daa6de37ba
[AMDGPU][SDAG] Add target-specific ISD::PTRADD combines (#143673)
This patch adds several (AMDGPU-)target-specific DAG combines for
ISD::PTRADD nodes that reproduce existing similar transforms for
ISD::ADD nodes. There is no functional change intended for the existing
target-specific PTRADD combine.

For SWDEV-516125.
2025-07-18 10:00:54 +02:00
Matt Arsenault
176ae32de0
AMDGPU: Fix introducing use of killed vgpr in gfx908 agpr copy (#149291)
When searching for an existing VGPR source for an AGPR to AGPR
copy on gfx908, this wasn't verifying the vgpr wasn't killed by
other prior uses.
2025-07-18 15:34:47 +09:00
Matt Arsenault
1614c3b3c7
AMDGPU: Always use AV spill pseudos on targets with AGPRs (#149099)
This increases allocator freedom to inflate register classes
to the AV class, we don't need to introduce a new restriction
by basing the opcode on the current virtual register class.
Ideally we would avoid this if we don't have any allocatable
AGPRs for the function, but it probably doesn't make much
difference in the end result if they are excluded from the
final allocation order.
2025-07-18 15:31:50 +09:00
Vikram Hegde
06528070fc
[CodeGen][NPM] Clear MachineFunctions without using PA (#148113)
same as https://github.com/llvm/llvm-project/pull/139517

This replaces the InvalidateAnalysisPass<MachineFunctionAnalysis> pass.

There are no cross-function analysis requirements right now, so clearing
all analyses works for the last pass in the pipeline.

Having the InvalidateAnalysisPass<MachineFunctionAnalysis>() is causing
a problem with ModuleToCGSCCPassAdaptor by deleting machine functions
for other functions and ending up with exactly one correctly compiled
MF, with the rest being vanished.

This is because ModuleToCGSCCPAdaptor propagates PassPA (received from
the CGSCCToFunctionPassAdaptor that runs the actual codegen pipeline on
MFs) to the next SCC. That causes MFA invalidation on functions in the
next SCC.

For us, PassPA happens to be returned from
invalidate<machine-function-analysis> which abandons the
MachineFunctionAnalysis. So while the first function runs through the
pipeline normally, invalidate also deletes the functions in the next SCC
before its pipeline is run. (this seems to be the intended mechanism of
the CG adaptor to allow cross-SCC invalidations.

Co-authored-by : Oke, Akshat
<[Akshat.Oke@amd.com](mailto:Akshat.Oke@amd.com)>
2025-07-18 11:58:01 +05:30
Shoreshen
5fe9b52352
Add FABS to canCreateUndefOrPoison (#149440)
FABS will not create undef/poison, add it into canCreateUndefOrPoison
return false
2025-07-18 14:17:15 +08:00
Shoreshen
f761d73265
[AMDGPU] Add freeze for LowerSELECT (#148796)
Trying to solve https://github.com/llvm/llvm-project/issues/147635

Add freeze for legalizer when breaking i64 select to 2 i32 select.

Several tests changed, still need to investigate why.

---------

Co-authored-by: Shilei Tian <i@tianshilei.me>
2025-07-18 13:29:33 +08:00
Jorn Tuyls
03fe1a493d
[AMDGPU] Fix sgpr to vreg_1 copy analysis (#149181) 2025-07-18 14:08:50 +09:00
Shilei Tian
aecd44818a
[AMDGPU] Add support for v_tanh_f16 on gfx1250 (#149439)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-18 00:21:04 -04:00
Matt Arsenault
a131ce9603
AMDGPU: Handle av imm pseudo in si-fix-sgpr-copies phi fold (#149263) 2025-07-18 09:03:37 +09:00
Stanislav Mekhanoshin
0b6df5485e
[AMDGPU] Reenable tanh real-true16 run line. NFC. (#149411) 2025-07-17 16:11:25 -07:00
Stanislav Mekhanoshin
c15a50ad22
[AMDGPU] More flatGVS gfx1250 patterns (#149410) 2025-07-17 16:10:59 -07:00
Changpeng Fang
70046cd2b5
AMDGPU: Remove the dot4 test in insert-delay-alu-wmma-xdl.mir, NFC (#149375)
This is irrelevant, and caused a failure in downstream.

Fixes: SWDEV-544025
2025-07-17 14:26:09 -07:00
Stanislav Mekhanoshin
422a250b0b
[AMDGPU] add tests for Change FLAT SADDR to VADDR form in moveToVALU. NFC. (#149392) 2025-07-17 14:18:52 -07:00
Shilei Tian
7e105fbdbe
[AMDGPU] Add support for v_tanh_f32 on gfx1250 (#149360)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-17 15:42:35 -04:00
Shilei Tian
fd5fc76c91
[AMDGPU] Add support for v_cos_bf16 on gfx1250 (#149355)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-17 14:43:34 -04:00
jyli0116
fc5c5a934d
[GlobalISel] Allow expansion of srem by constant in prelegalizer (#148845)
This patch allows srem by a constant to be expanded more efficiently to
avoid the need for expensive sdiv instructions. This is the last part of
the patches which fixes #118090
2025-07-17 14:43:58 +01:00
Shilei Tian
a102342990
[AMDGPU] Add support for v_sin_bf16 on gfx1250 (#149241)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-17 08:49:45 -04:00
Shilei Tian
a6b5ece75e
[AMDGPU] Add support for v_exp_bf16 on gfx1250 (#149229)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-17 08:46:01 -04:00
Jeffrey Byrnes
b291d1a71f
[TII] Do not fold undef copies (#147392)
RegallocBase::cleanupFailedVReg hacks up the state of the liveness in
order to facilitate producing valid IR. During this process, we may end
up producing undef copies.

If the destination of these copies is a spill candidate, we will attempt
to fold the source register when issuing the spill. The undef of the
source is not propagated to storeRegToStackSlot , thus we end up
dropping the undef, issuing a spill, and producing an illegal liveness
state.

This checks for undef copies, and, if found, inserts a kill instead of
spill.
2025-07-17 18:09:52 +09:00
Vikram Hegde
72c61a6a25
[AMDGPU][NPM] Fill in addPreSched2 passes (#148112)
same as https://github.com/llvm/llvm-project/pull/139516

Co-authored-by : Oke, Akshat
<[Akshat.Oke@amd.com](mailto:Akshat.Oke@amd.com)>
2025-07-17 12:26:27 +05:30
Changpeng Fang
b52cf756ce
AMDGPU: Treat WMMA XDL ops as TRANS in S_DELAY_ALU insertion for gfx1250 (#149208)
WMMA XDL instructions are tracked as TRANs ops and the compiler should
consider them the same as TRANS in S_DELAY_ALU insertion. We use a searchable
table for the InsertDelayAlu pass to recognize these WMMA XDL instructions.

Co-authored-by: Stefan Stipanovic <Stefan.Stipanovic@amd.com>
2025-07-16 17:07:48 -07:00
Stanislav Mekhanoshin
26b0b279de
[AMDGPU] Select flat GVS stores on gfx1250 (#149203) 2025-07-16 16:36:41 -07:00
Shilei Tian
ad6d5d2821
[AMDGPU] Add support for v_log_bf16 on gfx1250 (#149201)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-16 19:09:34 -04:00
Shilei Tian
7d2a58e87d
[AMDGPU] Add support for v_rsq_bf16 on gfx1250 (#149194)
Co-authored-by: Mekhanoshin, Stanislav <Stanislav.Mekhanoshin@amd.com>
2025-07-16 19:06:03 -04:00
Stanislav Mekhanoshin
703501e661
[AMDGPU] Select flat GVS loads on gfx1250 (#149183) 2025-07-16 15:06:37 -07:00
Daniil Fukalov
b7f6abdd05
[AMDGPU] Try to reuse register with the constant from compare in v_cndmask (#148740)
For some targets, the optimization X == Const ? X : Y -> X == Const ?
Const : Y can cause extra register usage or redundant immediate encoding
for the constant in cndmask generated from the ternary operation.

This patch detects such cases and reuses the register from the compare
instruction that already holds the constant, instead of materializing it
again for cndmask.

The optimization avoids immediates that can be encoded into cndmask
instruction (including +-0.0), as well as !isNormal() constants.

The change is reworked on the base of #131146

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-07-16 23:18:44 +02:00
Changpeng Fang
560e7df689
AMDGPU: Handle the co-execution hazards for TRANS for gfx1250 (#149024)
For the co-execution of the TRANS ops, the requirement is: 1 independent
op or V_NOP (since TRANS takes 2 cycles) after the trans op before its
sources can be overwritten or the output of the trans op can be used.
2025-07-16 10:58:54 -07:00
Jun Wang
ba271cc073
Revert "[AMDGPU][MC] Allow op_sel in v_alignbit_b32 etc in GFX9 and GFX10 (#142188) (#149138)
This reverts commit ce7851f6b7d59e50f92cb4e8dbfd801576c8b641.
The intrinsic llvm.amdgcn.alignbyte was not properly handled for gfx10.
2025-07-16 10:58:47 -07:00
macurtis-amd
402b989693
AMDGPU: Fix assert when multi operands to update after folding imm (#148205)
In the original motivating test case,
[FoldList](d8a2141ff9/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp (L1764))
had entries:
```
  #0: UseMI: %224:sreg_32 = S_OR_B32 %219.sub0:sreg_64, %219.sub1:sreg_64, implicit-def dead $scc
      UseOpNo: 1

  #1: UseMI: %224:sreg_32 = S_OR_B32 %219.sub0:sreg_64, %219.sub1:sreg_64, implicit-def dead $scc
      UseOpNo: 2
```
After calling
[updateOperand(#0)](d8a2141ff9/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp (L1773)),
[tryConstantFoldOp(#0.UseMI)](d8a2141ff9/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp (L1786))
removed operand 1, and entry #&#8203;1.UseOpNo was no longer valid,
resulting in an
[assert](4a35214bdd/llvm/include/llvm/ADT/ArrayRef.h (L452)).

This change defers constant folding until all operands have been updated
so that UseOpNo values remain stable.
2025-07-16 06:37:08 -05:00
Pierre van Houtryve
29e14c3b44
[AMDGPU] Remove widen-16-bit-ops from CGP (#145483)
This was already off by default so there is no codegen change.
2025-07-16 10:14:19 +02:00
Pierre van Houtryve
7d52b72239
[AMDGPU] Compute GISel KnownBits for S_BFE instructions (#141588)
Next patches in the stack will emit them in the RegBankCombiner. With this, S_BFE instructions will hopefully interfere less with optimizations.
2025-07-16 09:56:45 +02:00