Skip to content

[AMDGPU] Use s_fmamk_f32 when the folded addend is inlinable - #218955

Merged
barbara-amd merged 4 commits into
llvm:mainfrom
barbara-amd:amdgpu-use-s_fmamk_f32-for-inlinable-addends
Sep 17, 2026
Merged

barbara-amd merged 4 commits into
llvm:mainfrom
barbara-amd:amdgpu-use-s_fmamk_f32-for-inlinable-addends

Conversation

@barbara-amd

Copy link
Copy Markdown
Contributor

When folding a constant into src2 of s_fmac_f32, the AK form (s_fmaak_f32) was always selected, which puts the folded addend in the literal slot. If that addend is an inline constant while one of the multiplicands is not, this wastes the literal slot and still requires an extra s_mov_b32 for the multiplicand.

Choose the instruction variant based on which operand actually needs the literal instead. If the folded addend is inlinable and a multiplicand is not, use the MK form (s_fmamk_f32), placing the non-inlinable multiplicand in the literal slot and encoding the addend inline.

@barbara-amd
barbara-amd force-pushed the amdgpu-use-s_fmamk_f32-for-inlinable-addends branch from 432a8a4 to fb96fe5 Compare August 27, 2026 08:36
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-amdgpu

Author: Barbara Mitic (barbara-amd)

Changes

When folding a constant into src2 of s_fmac_f32, the AK form (s_fmaak_f32) was always selected, which puts the folded addend in the literal slot. If that addend is an inline constant while one of the multiplicands is not, this wastes the literal slot and still requires an extra s_mov_b32 for the multiplicand.

Choose the instruction variant based on which operand actually needs the literal instead. If the folded addend is inlinable and a multiplicand is not, use the MK form (s_fmamk_f32), placing the non-inlinable multiplicand in the literal slot and encoding the addend inline.


Full diff: https://github.com/llvm/llvm-project/pull/218955.diff

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIFoldOperands.cpp (+49-3)
  • (modified) llvm/test/CodeGen/AMDGPU/fold-operands-scalar-fmac.mir (+32-8)
diff --git a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
index 9d1fc8deb9356..059e98c51e306 100644
--- a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
+++ b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
@@ -859,13 +859,50 @@ bool SIFoldOperandsImpl::tryAddToFoldList(
     if (!OpToFold.isImm())
       return false;
 
-    const bool TryAK = OpNo == 3;
+    bool TryAK = OpNo == 3;
+
+    const MCInstrDesc &MKDesc = TII->get(AMDGPU::S_FMAMK_F32);
+    std::optional<int64_t> ImmToFold = OpToFold.getEffectiveImmVal();
+    unsigned KFromOpNo = 0;
+    int64_t KImm = 0;
+    if (TryAK && ImmToFold &&
+        TII->isInlineConstant(*ImmToFold, MKDesc.operands()[3].OperandType)) {
+      // Prefer src1: it already sits in the FMAMK K slot, so no commute.
+      for (unsigned FactorOpNo : {2, 1}) {
+        MachineOperand &Factor = MI->getOperand(FactorOpNo);
+        std::optional<int64_t> FactorImm = TII->getImmOrMaterializedImm(Factor);
+        if (!FactorImm || TII->isInlineConstant(*MI, FactorOpNo, *FactorImm))
+          continue;
+        const unsigned KeptOpNo = (FactorOpNo == 1) ? 2 : 1;
+        const MachineOperand &Kept = MI->getOperand(KeptOpNo);
+        if (Kept.isImm() ? !TII->isInlineConstant(*MI, KeptOpNo)
+                         : !Kept.isReg())
+          continue;
+        // s_fmamk_f32 keeps the literal in operand 2, so commute if it sits in
+        // operand 1.
+        if (FactorOpNo == 1 &&
+            !TII->commuteInstruction(*MI, /*NewMI=*/false, 1, 2))
+          continue;
+        KFromOpNo = FactorOpNo;
+        KImm = *FactorImm;
+        TryAK = false;
+        break;
+      }
+    }
+
     const unsigned NewOpc = TryAK ? AMDGPU::S_FMAAK_F32 : AMDGPU::S_FMAMK_F32;
+    // OpToFold lands in the K slot, unless a multiplicand has claimed it.
+    const unsigned FoldOpNo = (TryAK || KFromOpNo) ? 3 : 2;
+
+    // Replace the register the constant was materialized into by the literal.
+    const MachineOperand OrigSrc1 = MI->getOperand(2);
+    const bool KWasReg = KFromOpNo && OrigSrc1.isReg();
     MI->setDesc(TII->get(NewOpc));
+    if (KWasReg)
+      MI->getOperand(2).ChangeToImmediate(KImm);
 
     // We have to fold into operand which would be Imm not into OpNo.
-    bool FoldAsFMAAKorMK =
-        tryAddToFoldList(FoldList, MI, TryAK ? 3 : 2, OpToFold);
+    bool FoldAsFMAAKorMK = tryAddToFoldList(FoldList, MI, FoldOpNo, OpToFold);
     if (FoldAsFMAAKorMK) {
       // Untie Src2 of fmac.
       MI->untieRegOperand(3);
@@ -886,6 +923,15 @@ bool SIFoldOperandsImpl::tryAddToFoldList(
       return true;
     }
     MI->setDesc(TII->get(Opc));
+    if (KWasReg) {
+      MachineOperand &Src1 = MI->getOperand(2);
+      Src1.ChangeToRegister(OrigSrc1.getReg(), /*isDef=*/false, /*isImp=*/false,
+                            OrigSrc1.isKill(), /*isDead=*/false,
+                            OrigSrc1.isUndef());
+      Src1.setSubReg(OrigSrc1.getSubReg());
+    }
+    if (KFromOpNo == 1)
+      TII->commuteInstruction(*MI, /*NewMI=*/false, 1, 2);
     return false;
   };
 
diff --git a/llvm/test/CodeGen/AMDGPU/fold-operands-scalar-fmac.mir b/llvm/test/CodeGen/AMDGPU/fold-operands-scalar-fmac.mir
index a245ae7e243cb..0beeba8eaf575 100644
--- a/llvm/test/CodeGen/AMDGPU/fold-operands-scalar-fmac.mir
+++ b/llvm/test/CodeGen/AMDGPU/fold-operands-scalar-fmac.mir
@@ -1,5 +1,6 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
 # RUN: llc -mtriple=amdgpu11.50 -verify-machineinstrs -run-pass=si-fold-operands  %s -o - | FileCheck %s
+# RUN: llc -mtriple=amdgpu12.01 -verify-machineinstrs -run-pass=si-fold-operands  %s -o - | FileCheck %s
 
 ---
 name:            fmac_fold_inlinable_src0_to_fmamk
@@ -123,18 +124,17 @@ body:             |
 ...
 
 ---
-name:            fmac_fold_inlinable_src2_to_fmaak_noninlinable_src0_to_fmamk
+name:            fmac_fold_inlinable_src2_noninlinable_src0_to_fmamk
 tracksRegLiveness: true
 body:             |
   bb.0:
     liveins: $sgpr0
 
-    ; CHECK-LABEL: name: fmac_fold_inlinable_src2_to_fmaak_noninlinable_src0_to_fmamk
+    ; CHECK-LABEL: name: fmac_fold_inlinable_src2_noninlinable_src0_to_fmamk
     ; CHECK: liveins: $sgpr0
     ; CHECK-NEXT: {{  $}}
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:sreg_32 = COPY $sgpr0
-    ; CHECK-NEXT: %noninlinable:sreg_32 = S_MOV_B32 1234567890
-    ; CHECK-NEXT: %fma:sreg_32 = nofpexcept S_FMAAK_F32 %noninlinable, [[COPY]], 1056964608, implicit $mode
+    ; CHECK-NEXT: %fma:sreg_32 = nofpexcept S_FMAMK_F32 [[COPY]], 1234567890, 1056964608, implicit $mode
     ; CHECK-NEXT: $sgpr0 = COPY %fma
     %0:sreg_32 = COPY $sgpr0
     %inlinable:sreg_32 = S_MOV_B32 1056964608
@@ -143,18 +143,17 @@ body:             |
     $sgpr0 = COPY %fma
 ...
 ---
-name:            fmac_fold_inlinable_src2_to_fmaak_noninlinable_src1_to_fmamk
+name:            fmac_fold_inlinable_src2_noninlinable_src1_to_fmamk
 tracksRegLiveness: true
 body:             |
   bb.0:
     liveins: $sgpr0
 
-    ; CHECK-LABEL: name: fmac_fold_inlinable_src2_to_fmaak_noninlinable_src1_to_fmamk
+    ; CHECK-LABEL: name: fmac_fold_inlinable_src2_noninlinable_src1_to_fmamk
     ; CHECK: liveins: $sgpr0
     ; CHECK-NEXT: {{  $}}
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:sreg_32 = COPY $sgpr0
-    ; CHECK-NEXT: %noninlinable:sreg_32 = S_MOV_B32 1234567890
-    ; CHECK-NEXT: %fma:sreg_32 = nofpexcept S_FMAAK_F32 [[COPY]], %noninlinable, 1056964608, implicit $mode
+    ; CHECK-NEXT: %fma:sreg_32 = nofpexcept S_FMAMK_F32 [[COPY]], 1234567890, 1056964608, implicit $mode
     ; CHECK-NEXT: $sgpr0 = COPY %fma
     %0:sreg_32 = COPY $sgpr0
     %inlinable:sreg_32 = S_MOV_B32 1056964608
@@ -238,3 +237,28 @@ body:             |
     %fma:sreg_32 = nofpexcept S_FMAC_F32 %noninlinable, %noninlinable, %0, implicit $mode
     $sgpr0 = COPY %fma
 ...
+---
+name:            fmac_fold_shared_noninlinable_src1_to_fmamk
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $sgpr0, $sgpr1
+
+    ; CHECK-LABEL: name: fmac_fold_shared_noninlinable_src1_to_fmamk
+    ; CHECK: liveins: $sgpr0, $sgpr1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:sreg_32 = COPY $sgpr0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:sreg_32 = COPY $sgpr1
+    ; CHECK-NEXT: %fma0:sreg_32 = nofpexcept S_FMAMK_F32 [[COPY]], 1090519040, 1056964608, implicit $mode
+    ; CHECK-NEXT: %fma1:sreg_32 = nofpexcept S_FMAMK_F32 [[COPY1]], 1090519040, 1056964608, implicit $mode
+    ; CHECK-NEXT: $sgpr0 = COPY %fma0
+    ; CHECK-NEXT: $sgpr1 = COPY %fma1
+    %0:sreg_32 = COPY $sgpr0
+    %1:sreg_32 = COPY $sgpr1
+    %inlinable:sreg_32 = S_MOV_B32 1056964608
+    %noninlinable:sreg_32 = S_MOV_B32 1090519040
+    %fma0:sreg_32 = nofpexcept S_FMAC_F32 %0, %noninlinable, %inlinable, implicit $mode
+    %fma1:sreg_32 = nofpexcept S_FMAC_F32 %1, %noninlinable, %inlinable, implicit $mode
+    $sgpr0 = COPY %fma0
+    $sgpr1 = COPY %fma1
+...

@mgcarrasco

Copy link
Copy Markdown
Contributor

Hi! This comment was generated using fuzz-fill, a new tool we're building to help improve LLVM test coverage by spotting lines introduced in a PR that don't yet appear to be exercised by the full test suite, including any tests added in this PR.

Looking at PR head fb96fe5, we found 2 lines added or modified here that we couldn't find a test for:

  1. MachineOperand &Src1 = MI->getOperand(2);
  2. TII->commuteInstruction(*MI, /*NewMI=*/false, 1, 2);

If it makes sense, adding a regression test that validates these lines would be great. And if any of them are already tested and we missed it, we'd love to hear so we can improve the tool. Feel free to give us some feedback on whether this review was useful from your perspective. Thanks!

@barbara-amd
barbara-amd requested review from arsenm and jayfoad August 27, 2026 09:57
@jayfoad
jayfoad requested a review from mbrkusanin August 27, 2026 10:36
@jayfoad

jayfoad commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

There's some discussion on #140580 which might be related.

@mbrkusanin

Copy link
Copy Markdown
Contributor

There is some duplicated logic here because it tries to fold multiple operands into s_fmac at the same time. tryAddToFoldList was designed to fold one operand at a time.

Original implementation would either convert s_fmac to s_fmaak or s_fmamk when it runs into a literal. Afterwards if it runs into another literal and the first one was an inline const it might try to convert fmaak<->fmamk, but that code has since been deleted because it became dead due to changes in how isOperandLegal works. Restoring code from #140580 that Jay mentioned and placing it before if (!IsLegal) should work.

@mbrkusanin

Copy link
Copy Markdown
Contributor

placing it before if (!IsLegal) should work.

actually, putting it in if (!IsLegal) { ... } with other special cases seem like more appropriate.

…linable

When folding a constant into src2 of s_fmac_f32, the AK form
(s_fmaak_f32) was always selected, which puts the folded addend in the
literal slot. If that addend is an inline constant while one of the
multiplicands is not, this wastes the literal slot and still requires an
extra s_mov_b32 for the multiplicand.

Choose the instruction variant based on which operand actually needs the
literal instead. If the folded addend is inlinable and a multiplicand is
not, use the MK form (s_fmamk_f32), placing the non-inlinable
multiplicand in the literal slot and encoding the addend inline.

The instruction size is unchanged and the redundant s_mov_b32 is
eliminated.
@barbara-amd
barbara-amd force-pushed the amdgpu-use-s_fmamk_f32-for-inlinable-addends branch from fb96fe5 to d1768fd Compare September 14, 2026 13:04

@mbrkusanin mbrkusanin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nit

@barbara-amd
barbara-amd force-pushed the amdgpu-use-s_fmamk_f32-for-inlinable-addends branch from d1768fd to ac64bc9 Compare September 14, 2026 14:17
…linable

Folding an inlinable addend into src2 of s_fmac_f32 produces s_fmaak_f32
and occupies the literal slot. A later fold of a non-inlinable
multiplicand then still needs an extra s_mov_b32.

If the existing Imm is inlinable, convert between s_fmaak_f32 and
s_fmamk_f32 so the new constant takes the literal slot and the addend is
encoded inline.

This restores the fmaak/fmamk conversion removed by llvm#140580.
@barbara-amd
barbara-amd force-pushed the amdgpu-use-s_fmamk_f32-for-inlinable-addends branch from ac64bc9 to da5b9b1 Compare September 14, 2026 14:19
@barbara-amd
barbara-amd merged commit 66ae28f into llvm:main Sep 17, 2026
12 checks passed
yebinchon pushed a commit to yebinchon/llvm-project that referenced this pull request Sep 18, 2026
…8955)

When folding a constant into src2 of s_fmac_f32, the AK form
(s_fmaak_f32) was always selected, which puts the folded addend in the
literal slot. If that addend is an inline constant while one of the
multiplicands is not, this wastes the literal slot and still requires an
extra s_mov_b32 for the multiplicand.

Choose the instruction variant based on which operand actually needs the
literal instead. If the folded addend is inlinable and a multiplicand is
not, use the MK form (s_fmamk_f32), placing the non-inlinable
multiplicand in the literal slot and encoding the addend inline.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants