269 Commits

Author SHA1 Message Date
Matt Arsenault
5f3eea7ef2
AMDGPU: Fix not folding splat immediate into VGPR MFMA src2 (#150628) 2025-07-26 13:54:49 +09:00
Stanislav Mekhanoshin
006858cd4d
[AMDGPU] Prevent folding of FI with scale_offset on gfx1250 (#149894)
SS forms of SCRATCH_LOAD_DWORD do not support SCALE_OFFSET,
so if this bit is used SCRATCH_LOAD_DWORD_SADDR cannot be formed.
This generally shall not happen because FI is not supposed to
be scaled, but add this as a precaution.
2025-07-21 15:05:43 -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 #​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
Matt Arsenault
e1f224b99a
AMDGPU: Handle folding vector splats of inline split f64 inline immediates (#140878)
Recognize a reg_sequence with 32-bit elements that produce a 64-bit
splat value. This enables folding f64 constants into mfma operands
2025-06-26 07:45:49 +09:00
Matt Arsenault
472c9141f9
AMDGPU: Fix tracking subreg defs when folding through reg_sequence (#140608)
We weren't fully respecting the type of a def of an immediate vs.
the type at the use point. Refactor the folding logic to track the
value to fold, as well as a subregister to apply to the underlying
value. This is similar to how PeepholeOpt tracks subregisters (though
only for pure copy-like instructions, no constants).

Fixes #139317
2025-06-26 07:42:55 +09:00
Matt Arsenault
80064b6e32
AMDGPU: Try constant fold after folding immediate (#141862)
This helps avoid some regressions in a future patch. The or 0
pattern appears in the division tests because the reduce 64-bit
bit operation to a 32-bit one with half identity value is only
implemented for constants. We could fix that by using computeKnownBits.
Additionally the pattern disappears if I optimize the IR division
expansion, so that IR should probably be emitted more optimally in
the first place.
2025-06-10 11:44:44 +09:00
Daniil Fukalov
5208f722d8
[AMDGPU] Fix SIFoldOperandsImpl::canUseImmWithOpSel() for VOP3 packed [B]F16 imms. (#142142)
VOP3 instructions ignore opsel source modifiers, so a constant that
contains two different [B]F16 imms cannot be encoded into instruction
with an src opsel.

E.g. without the fix the following instructions

`s_mov_b32 s0, 0x40003c00 // <half 1.0, half 2.0>`
`v_cvt_scalef32_pk_fp8_f16 v0, s0, v2`

lose `2.0` imm and are folded into

`v_cvt_scalef32_pk_fp8_f16 v1, 1.0, 1.0`

Fixes SWDEV-531672
2025-05-30 16:38:07 +02:00
Matt Arsenault
65b90c59ce
AMDGPU: Remove redundant operand folding checks (#140587)
This was pre-filtering out a specific situation from being
added to the fold candidate list. The operand legality will
ultimately be checked with isOperandLegal before the fold is
performed, so I don't see the plus in pre-filtering this one
case.
2025-05-29 19:38:45 +02:00
Matt Arsenault
1b07c589b2
AMDGPU: Delete seemingly dead s_fmaak_f32/s_fmamk_f32 folding code (#140580)
No tests fail with this. I'm not sure I understand the comment,
there can't be any folding into an operand that had to already
be a constant. I tried different combinations of immediates to these
instructions but never hit the condition.
2025-05-29 19:36:05 +02:00
Fabian Ritter
fb27867bd5
[AMDGPU] SIFoldOperands: Delay foldCopyToVGPROfScalarAddOfFrameIndex (#141558)
foldCopyToVGPROfScalarAddOfFrameIndex transforms s_adds whose results are copied
to vector registers into v_adds. We don't want to do that if foldInstOperand
(which so far runs later) can fold the sreg->vreg copy away.
This patch therefore delays foldCopyToVGPROfScalarAddOfFrameIndex until after
foldInstOperand.

This avoids unnecessary movs in the flat-scratch-svs.ll test and also avoids
regressions in an upcoming patch to enable ISD::PTRADD nodes.
2025-05-27 11:30:51 +02:00
Rahul Joshi
52c2e45c11
[NFC][CodeGen] Adopt MachineFunctionProperties convenience accessors (#141101) 2025-05-23 08:30:29 -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
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
Ivan Kosarev
c290f48a45
[AMDGPU][NFC] Remove unused operand types. (#139062) 2025-05-08 12:48:25 +01:00
Akhilesh Moorthy
9c9013f703
[AMDGPU] Handle MachineOperandType global address in SIFoldOperands. (#135424)
This patch handles the global operand type properly, fixing the
bug : Assertion `(isFI() || isCPI() || isTargetIndex() ||
isJTI()) && "Wrong MachineOperand accessor"` failed.

Fixes SWDEV-504645

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-05-05 18:12:35 +02:00
Kazu Hirata
d144c13ae5
[Target] Remove unused local variables (NFC) (#138443) 2025-05-04 07:56:38 -07:00
mssefat
71039bbc58
[AMDGPU] Fix register class constraints for si-fold-operands pass when folding immediate into copies (#131387)
Fixes https://github.com/llvm/llvm-project/issues/130020

This fixes an issue where the si-fold-operands pass would incorrectly
fold immediate values into COPY instructions targeting av_32 registers.

The pass now checks register class constraints before attempting to fold
the immediate.
2025-04-30 17:36:46 -05:00
Mariusz Sikora
1a48e1df45
[AMDGPU] Do not fold COPY with implicit operands (#136003)
Folding may remove COPY from inside of the divergent loop.
2025-04-22 13:33:06 +02:00
Kazu Hirata
e7c07a0210
[AMDGPU] Construct SmallVector with iterator ranges (NFC) (#136415) 2025-04-19 09:09:41 -07:00
Brox Chen
066787b9bd
[AMDGPU][True16][CodeGen] fold clamp update for true16 (#128919)
Check through COPY for possible clamp folding for v_mad_mixhi_f16 isel
2025-04-02 17:10:53 -04:00
Brox Chen
fb0e7b5f16
[AMDGPU][True16][CodeGen] Implement sgpr folding in true16 (#128929)
We haven't implemented 16 bit SGPRs. Currently allow 32-bit SGPRs to be
folded into True16 bit instructions taking 16 bit values. Also use
sgpr_32 when Imm is copied to spgr_lo16 so it could be further folded.
This improves generated code quality.
2025-04-02 16:08:26 -04:00
Valery Pykhtin
af0b0ce665
[AMDGPU] Fix SIFoldOperandsImpl::tryFoldZeroHighBits when met non-reg src1 operand. (#133761)
This happens when a constant is propagated to a V_AND 0xFFFF, reg
instruction.

Fixes failures like:

```
llc: /github/llvm-project/llvm/include/llvm/CodeGen/MachineOperand.h:366: llvm::Register llvm::MachineOperand::getReg() const: Assertion `isReg() && "This is not a register operand!"' failed.
Stack dump:
0.      Program arguments: /github/llvm-project/build/Debug/bin/llc -mtriple=amdgcn -mcpu=gfx1101 -verify-machineinstrs -run-pass si-fold-operands /github/llvm-project/llvm/test/CodeGen/AMDGPU/fold-zero-high-bits-skips-non-reg.mir -o -
1.      Running pass 'Function Pass Manager' on module '/github/llvm-project/llvm/test/CodeGen/AMDGPU/fold-zero-high-bits-skips-non-reg.mir'.
2.      Running pass 'SI Fold Operands' on function '@test_tryFoldZeroHighBits_skips_nonreg'
...
#12 0x00007f5a55005cfc llvm::MachineOperand::getReg() const /github/llvm-project/llvm/include/llvm/CodeGen/MachineOperand.h:0:5
#13 0x00007f5a555c6bf5 (anonymous namespace)::SIFoldOperandsImpl::tryFoldZeroHighBits(llvm::MachineInstr&) const /github/llvm-project/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp:1459:36
#14 0x00007f5a555c63ad (anonymous namespace)::SIFoldOperandsImpl::run(llvm::MachineFunction&) /github/llvm-project/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp:2455:11
#15 0x00007f5a555c6780 (anonymous namespace)::SIFoldOperandsLegacy::runOnMachineFunction
```
2025-04-01 10:27:58 +02:00
Matt Arsenault
f75d75b889
AMDGPU: Use MFPropsModifier modifier in SIFoldOperands (#127752)
This doesn't appear to work. I do not get an error in the new PM.

---------

Co-authored-by: Akshat Oke <Akshat.Oke@amd.com>
2025-03-17 13:37:34 +07:00
Matt Arsenault
3e53aeae94
AMDGPU: Make frame index folding logic consistent with eliminateFrameIndex (#129633)
This adds handling of s_add_u32, which is handled and removes handling of
s_or_b32 and s_and_b32, which are not. I was working on handling them
in #102345, but need to finish that patch. This fixes a regression
exposed by a3165398db0736588daedb07650195502592e567 where the
final instruction would use two literals.
2025-03-05 10:09:30 +07:00
Matt Arsenault
c8f4c35a66
AMDGPU: Correctly handle folding immediates into subregister use operands (#129664)
This fixes a miscompile where a 64-bit materialize incorrectly folds
into
a sub1 use operand.

We currently do not see many subregister use operands. Incidentally,
there are also SIFoldOperands bugs that prevent this fold from
appearing here. Pre-fix folding of 32-bit subregister uses from 64-bit
materializes, in preparation for future patches.

The existing APIs are awkward since they expect to have a fully formed
instruction with operands to use, and not something new which needs
to be created.
2025-03-05 01:06:11 +07:00
Matt Arsenault
b3d5056c79
AMDGPU: Try to perform copy to agpr from reg_sequence at the copy (#129463)
SIFoldOperands is frustratingly written in a def-folds-into-use
iteration pattern, with a few random cases starting at the uses.
We were handling this case by looking at the reg_sequence, and finding
the copy. This did not work for the most basic pattern of materializing
a vector constant that started in SGPRs. It just happens there is an
optimization bug in SelectionDAG that produced the expected pattern.

Perform an additional attempt at the fold rooted at the copy. This
mostly shows test improvements. There were some tricky updates to
perform. remaining-virtual-register-operands.ll managed to stop failing
the allocator, so needed to be tricked into failing again. I also do
not understand what schedule-xdl-resource.ll is trying to do for the test
so this changes it to some random output that exists in the debug output.
2025-03-04 14:41:56 +07:00
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
ba400e862e
AMDGPU: Use helper function for use/def chain walk (#129052)
PeepholeOpt has a nicer version of this which handles more
cases.
2025-02-28 00:50:24 +07:00
Matt Arsenault
816e7cdb13
AMDGPU: Factor agpr reg_sequence folding into a function (#129002) 2025-02-27 20:01:43 +07: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
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
Kazu Hirata
0963f0d645
[AMDGPU] Avoid repeated hash lookups (NFC) (#128393) 2025-02-22 21:03:23 -08:00
Brox Chen
210036a22e
[AMDGPU][True16][CodeGen] true16 codegen pattern for fma (#127240)
Previous PR https://github.com/llvm/llvm-project/pull/122950 get
reverted since it hit the buildbot failure. Another patch get merged
when this PR is under review, and thus causing one test not up to date.

repen this PR and fixed the issue.
2025-02-19 11:37:24 -05:00
Matt Arsenault
cd10c01767
AMDGPU: Handle subregister uses in SIFoldOperands constant folding (#127485) 2025-02-18 17:19:53 +07:00
Matt Arsenault
6a360b313d
AMDGPU: Remove redundant inline constant check (#127582)
We don't really care of this is an inline constant, only if it
will be legal.
2025-02-18 15:37:20 +07:00
Matt Arsenault
b5b8a59a53
AMDGPU: Implement getRequiredProperties for SIFoldOperands (#127522)
Fix the broken MIR tests violating isSSA.
2025-02-18 08:22:45 +07:00
Brox Chen
cf1165cb9c
Revert "[AMDGPU][True16][CodeGen] true16 codegen pattern for fma (#12… (#127175)
Reverting this patch since it raise buildbot failure

This reverts commit 2a7487cc2e0fb8bd91784e2d9636a65baa6d90ed.
2025-02-14 02:28:45 -05:00
Brox Chen
2a7487cc2e
[AMDGPU][True16][CodeGen] true16 codegen pattern for fma (#122950)
true16 codegen pattern for f16 fma.

created a duplicated shrink-mad-fma-gfx10.mir from shrink-mad-fma to
seperate pre-GFX11 and GFX11 mir test.
2025-02-14 02:16:00 -05:00
Rahul Joshi
bee9664970
[TableGen] Emit OpName as an enum class instead of a namespace (#125313)
- Change InstrInfoEmitter to emit OpName as an enum class
  instead of an anonymous enum in the OpName namespace.
- This will help clearly distinguish between values that are 
  OpNames vs just operand indices and should help avoid
  bugs due to confusion between the two.
- Rename OpName::OPERAND_LAST to NUM_OPERAND_NAMES.
- Emit declaration of getOperandIdx() along with the OpName
  enum so it doesn't have to be repeated in various headers.
- Also updated AMDGPU, RISCV, and WebAssembly backends
  to conform to the new definition of OpName (mostly
  mechanical changes).
2025-02-12 08:19:30 -08:00
bcahoon
17c8c1c509
[AMDGPU] Do not fold into v_accvpr_mov/write/read (#120475)
In SIFoldOperands, leave copies for moving between agpr and vgpr
registers. The register coalescer is able to handle the copies
more efficiently than v_accvgpr_mov, v_accvgpr_write, and
v_accvgpr_read. Otherwise, the compiler generates unneccesary
instructions such as v_accvgpr_mov a0, a0.
2025-01-07 09:25:01 -06:00
Brox Chen
ce831a231a
[AMDGPU][True16][MC] true16 for v_fma_f16 (#119477)
Support true16 format for v_fma_f16 in MC.

Since we are replacing v_fma_f16 to v_fma_f16_t16/v_fma_f16_fake16 in
Post-GFX11, have to update the CodeGen pattern for v_fma_f16_fake16 to
get CodeGen test passing. There is no pattern modified/created, but just
replacing the v_fma_f16 with fake16 format.
2025-01-06 15:02:04 -05:00
Matt Arsenault
5911fbb39d
AMDGPU: Do not fold copy to physreg from operation on frame index (#115977) 2024-11-12 21:35:51 -08:00
Matt Arsenault
4fb43c47dd
AMDGPU: Fold more scalar operations on frame index to VALU (#115059)
Further extend workaround for the lack of proper regbankselect
for frame indexes.
2024-11-07 19:02:20 -08:00
Matt Arsenault
aa7941289e
AMDGPU: Fold copy of scalar add of frame index (#115058)
This is a pre-optimization to avoid a regression in a future
commit. Currently we almost always emit frame index with
a v_mov_b32 and use vector adds for the pointer operations. We
need to consider the users of the frame index (or rather, the
transitive users of derived pointer operations) to know whether
the value will be used in a vector or scalar context. This saves
an sgpr->vgpr copy.

This optimization could be more general for any opcode that's
trivially convertible from a scalar to vector form (although this
is a workaround for a proper regbankselect).
2024-11-06 09:10:58 -08:00
Brox Chen
e8644e3b47
[AMDGPU][True16][MC] VOP2 update instructions with fake16 format (#114436)
Some old "t16" VOP2 instructions are actually in fake16 format. Correct
and update test file
2024-11-05 16:12:49 -05:00
Jay Foad
a156362e93
[AMDGPU] Fix machine verification failure after SIFoldOperandsImpl::tryFoldOMod (#113544)
Fixes #54201
2024-10-29 14:59:37 +00:00
Matt Arsenault
ef91cd3f01
AMDGPU: Handle folding frame indexes into add with immediate (#110738) 2024-10-19 12:33:03 -07:00
Akshat Oke
2adc94cd6c
AMDGPU/NewPM: Port SIFoldOperands to new pass manager (#105801) 2024-08-29 11:34:54 +05:30
Brox Chen
ae059a1f9f
[AMDGPU][True16][CodeGen] support v_mov_b16 and v_swap_b16 in true16 format (#102198)
support v_swap_b16 in true16 format.
update tableGen pattern and folding for v_mov_b16.

---------

Co-authored-by: guochen2 <guochen2@amd.com>
2024-08-08 16:52:59 -04:00