Skip to content

[AMDGPU][Codegen] Don't incorrectly shrink S_LOAD_DWORDX{16,8}_IMM during remat - #225209

Merged
lucas-rami merged 3 commits into
llvm:mainfrom
lucas-rami:fix-remat-214725
Oct 26, 2026
Merged

lucas-rami merged 3 commits into
llvm:mainfrom
lucas-rami:fix-remat-214725

Conversation

@lucas-rami

@lucas-rami lucas-rami commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

This came up after #214725 but is a separate issue.

SIInstrInfo::reMaterialize should not shrink the width of S_LOAD_DWORDX{16,8}_IMM if

  1. UsedLanes is the full lanemask, keeping this consistent with S_MOV_64 handling after [AMDGPU] Shrink S_MOV_B64 to S_MOV_B32 during rematerialization #184333,
  2. or if there is more than one user of the rematerialized register. The current implementation only checks that the instruction being rematerialized before has a single use of the register.

The added test fails without this patch because of the incorrect one-user check (the load is shrunk despite the full register width being used).


As a sidenote, I find it surprising that the reMaterialize override performs these opcode changes silently. From name and documentation I would expect it to create an instruction whose def is in the same register class at least. I especially find it weird that for S_LOAD_DWORDX{16,8}_IMM we check for a single user and modify it directly from the hook. To me this feels like this shouldn't be the role of reMaterialize. If people agree I can address that in the future.

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-amdgpu

Author: Lucas Ramirez (lucas-rami)

Changes

This came up after #214725 but is a separate issue.

SIInstrInfo::reMaterialize should not shrink the width of S_LOAD_DWORDX{16,8}_IMM if

  1. UsedLanes is the full lanemask, keeping this consistent with S_MOVE_64 handling after #184333,
  2. or if there is more than one user of the rematerialized register. The current implementation only checks that the instruction being rematerialized before has a single use of the register.

The added test fails without this patch because of the incorrect one-user check.


As a sidenote, I find it surprising that the reMaterialize override performs these opcode changes silently. From name and documentation I would expect it to create an instruction whose def is in the same register class at least. I especially find it weird that for S_LOAD_DWORDX{16,8}_IMM we check for a single user and modify it directly from the hook. To me this feels like this shouldn't be the role of reMaterialize. If people agree I can address that in the future.


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

3 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (+7-9)
  • (modified) llvm/test/CodeGen/AMDGPU/machine-scheduler-sink-trivial-remats.mir (+92)
  • (modified) llvm/test/CodeGen/AMDGPU/remat-smrd.mir (+2-4)
The server is unavailable at this time. Please wait a few minutes before you try again.

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@IgWod IgWod 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.

Thanks! LGTM, but I'll let someone more familiar with this code to approve it.

@arsenm

arsenm commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

As a sidenote, I find it surprising that the reMaterialize override performs these opcode changes silently. From name and documentation I would expect it to create an instruction whose def is in the same register class at least. I especially find it weird that for S_LOAD_DWORDX{16,8}_IMM we check for a single user and modify it directly from the hook. To me this feels like this shouldn't be the role of reMaterialize. If people agree I can address that in the future.

Yes, I don't think this was implemented the right way and I had a difficult time extending this to other cases

@lucas-rami

Copy link
Copy Markdown
Contributor Author

ping

@jayfoad

jayfoad commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Sorry for the verbosity but Claude seems to have found a real correctness problem (with a repro and a simple fix):


Which callers can reach the new code

  • MachineSink and Rematerializer call reMaterialize with the default
    UsedLanes (all lanes), so with this PR they now always break before the
    use search.
  • SplitKit::defFromParent only rematerializes isAsCheapAsAMove
    instructions, so not SMEM loads.
  • That leaves InlineSpiller::reMaterializeFor:
    • it passes UsedLanes from VirtReg's sub-ranges;
    • Orig is the instruction at the original register's value number;
    • it rewrites the use at MI itself afterwards. For each of MI's
      operands that still read VirtReg, it does setReg(NewVReg) and keeps
      the sub-register index.

After greedy splits the original register, Orig's def register is a split
product. With sub-register liveness, SplitKit::buildCopy copies only the
live lanes:
undef %7.sub0_..._sub7 = lr-split COPY %6.sub0_..._sub7.
That sub-register COPY can be the only use of Orig's def register, and
it is not at I. The new whole-function use search accepts it.

Reproducer

Same RUN line as remat-smrd.mir. It passes on main, and fails with this PR
(applied to origin/main) with:

*** Bad machine code: Invalid subregister index for virtual register ***
- instruction: S_NOP 0, implicit killed %9.sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7:sgpr_256
Register class SGPR_256 does not support subreg index sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7

It still fails without -verify-machineinstrs (LLVM ERROR from the
assertions build). The MIR just before the error shows both halves of the
problem:

dead undef %7.sub0_..._sub7:sgpr_512 = lr-split COPY %9:sgpr_256      <- remote use redirected to %9, read before its def
%9:sgpr_256 = S_LOAD_DWORDX8_IMM %0, 0, 0                             <- shrunk remat at I
S_NOP 0, implicit killed %9.sub0_..._sub7:sgpr_256                    <- use at I never rewritten by reMaterialize;
                                                                         InlineSpiller kept the 512-bit subreg index
  1. The remote lr-split COPY now reads %9 before %9 is defined, and
    %9 is not live there. That is broken liveness, or wrong code in a build
    without assertions.
  2. The use that should have been rewritten (the S_NOP at I) wasn't. So
    InlineSpiller substitutes the 256-bit NewVReg with a 512-bit-only
    sub-register index.

Found by instrumenting the PR's hook to log whenever the use it found was not
at I:

  • across all 5,206 CodeGen/AMDGPU tests there were 4 hits, all at I;
  • out of 2,080 generated remat-smrd-style variants there was 1 hit not at
    I, which is this one;
  • smaller variants (two uses of the sub-range instead of three) don't split
    this way.

Suggested addition to remat-smrd.mir, with CHECK lines generated with the
fix below:

---
name:            test_remat_s_load_dword_immx16_subreg_remote_use
tracksRegLiveness: true
body:             |
  bb.0:
    liveins: $sgpr8_sgpr9
    ; GCN-LABEL: name: test_remat_s_load_dword_immx16_subreg_remote_use
    ; GCN: liveins: $sgpr8_sgpr9
    ; GCN-NEXT: {{  $}}
    ; GCN-NEXT: renamable $sgpr0_sgpr1 = COPY $sgpr8_sgpr9
    ; GCN-NEXT: renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11_sgpr12_sgpr13_sgpr14_sgpr15_sgpr16_sgpr17_sgpr18_sgpr19 = S_LOAD_DWORDX16_IMM renamable $sgpr0_sgpr1, 128, 0 :: (dereferenceable invariant load (s512), align 4, addrspace 4)
    ; GCN-NEXT: S_NOP 0, implicit killed renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11_sgpr12_sgpr13_sgpr14_sgpr15_sgpr16_sgpr17_sgpr18_sgpr19
    ; GCN-NEXT: renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11_sgpr12_sgpr13_sgpr14_sgpr15_sgpr16_sgpr17_sgpr18_sgpr19 = S_LOAD_DWORDX16_IMM renamable $sgpr0_sgpr1, 0, 0 :: (dereferenceable invariant load (s512), align 4, addrspace 4)
    ; GCN-NEXT: S_NOP 0, implicit killed renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11
    ; GCN-NEXT: renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11_sgpr12_sgpr13_sgpr14_sgpr15_sgpr16_sgpr17_sgpr18_sgpr19 = S_LOAD_DWORDX16_IMM renamable $sgpr0_sgpr1, 192, 0 :: (dereferenceable invariant load (s512), align 4, addrspace 4)
    ; GCN-NEXT: S_NOP 0, implicit killed renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11_sgpr12_sgpr13_sgpr14_sgpr15_sgpr16_sgpr17_sgpr18_sgpr19
    ; GCN-NEXT: renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11_sgpr12_sgpr13_sgpr14_sgpr15_sgpr16_sgpr17_sgpr18_sgpr19 = S_LOAD_DWORDX16_IMM renamable $sgpr0_sgpr1, 0, 0 :: (dereferenceable invariant load (s512), align 4, addrspace 4)
    ; GCN-NEXT: S_NOP 0, implicit renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11
    ; GCN-NEXT: S_NOP 0, implicit killed renamable $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11
    ; GCN-NEXT: S_ENDPGM 0, implicit killed renamable $sgpr0_sgpr1
    %0:sreg_64_xexec = COPY $sgpr8_sgpr9
    %1:sgpr_512 = S_LOAD_DWORDX16_IMM %0, 0, 0 :: (invariant dereferenceable load (s512), align 4, addrspace 4)
    %2:sgpr_512 = S_LOAD_DWORDX16_IMM %0, 128, 0 :: (invariant dereferenceable load (s512), align 4, addrspace 4)
    S_NOP 0, implicit %2
    S_NOP 0, implicit %1.sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7
    %3:sgpr_512 = S_LOAD_DWORDX16_IMM %0, 192, 0 :: (invariant dereferenceable load (s512), align 4, addrspace 4)
    S_NOP 0, implicit %3
    S_NOP 0, implicit %1.sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7
    S_NOP 0, implicit %1.sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7
    S_ENDPGM 0, implicit %0
...

Fix

Restore the old locality, and keep the PR's whole-function single-use check:

    if (!UseMO || UseMO->getParent() != &*I ||
        UseMO->getSubReg() == AMDGPU::NoSubRegister)
      break;

With that change on top of the PR:

  • the reproducer is verifier-clean and keeps the X16 loads;
  • all 5,206 CodeGen/AMDGPU tests pass, including the PR's updated
    remat-smrd.mir and machine-scheduler-sink-trivial-remats.mir.

@lucas-rami

Copy link
Copy Markdown
Contributor Author

I think the fix suggested by Claude make sense and I have implemented it. Though it makes me dislike even more the fact that we are doing conditional opcode optimization and value replacing in the rematerialization hook. I will address this in a future patch.

@qcolombet

Copy link
Copy Markdown
Contributor

@lucas-rami, I think the opcode change is fine, but the register class change scares me. Looks like it got introduced in #68778

assert(MRI.use_nodbg_empty(DestReg) && "DestReg should have no users yet.");

// Look for a single use of the register at the insertion point that is also
// a subreg.

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.

We're trying to establish that because of the change in register class later on?
I.e., no users mean we can do whatever we want?
Sounds like a fine workaround but we shouldn't change the register class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We're trying to establish that because of the change in register class later on? I.e., no users mean we can do whatever we want?

No opcode change happens when there are no users. It only happens when there is a subreg use of the rematerialized register at the insertion point that allows to shrink the load (possibly with a change in constant offset). The original logic did not check whether any other use existed; if there was one that e.g., used the whole register, then the original full-width load would need to be kept around in addition to the narrowed one to serve these lanes, making the rematerialization possibly useless. This interacts badly with callers of reMaterialize which have no consistent view of what the method might do beyond rematerializing the requested instruction.

This PR adds the constraint that the use must be the only one to avoid this situation, but I still think it is a bad idea to have MI-user-based opcode/register class changes because it remains a bit mysterious to the calling code. The somewhat recently added UsedLanes argument is IMO a better way to drive these optimizations as it allows the user to clearly signal whether they are only interested in a subset of the rematerialized register's lanes. At which point callers would be responsible for adjusting uses based on any opcode optimization the hook is capable of doing.

@lucas-rami
lucas-rami merged commit bc0e355 into llvm:main Oct 26, 2026
11 checks passed
@lucas-rami
lucas-rami deleted the fix-remat-214725 branch December 1, 2026 12:48
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.

5 participants