Skip to content

[InlineSpiller][AMDGPU] Implement subreg reload during RA spill - #175002

Open
cdevadas wants to merge 3 commits into
users/cdevadas/precommit-subreg-reload-testfrom
users/cdevadas/subreg-reload
Open

cdevadas wants to merge 3 commits into
users/cdevadas/precommit-subreg-reload-testfrom
users/cdevadas/subreg-reload

Conversation

@cdevadas

@cdevadas cdevadas commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

Currently, when a virtual register is partially used, the
entire tuple is restored from the spilled location, even if
only a subset of its sub-registers is needed. This patch
introduces support for partial reloads by analyzing actual
register usage and restoring only the required sub-registers.
This improvement enhances register allocation efficiency,
particularly for cases involving tuple virtual registers.
For AMDGPU, this change brings considerable improvements
in workloads that involve matrix operations, large vectors,
and complex control flows.

cdevadas commented Jan 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Outdated

// If the target doesn't support subreg reload, fallback to restoring the
// full tuple.
if (SubReg && !TRI.shouldEnableSubRegReload(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.

Check this before going through all the trouble of computing SubReg.

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.

The check MRI.subRegLivenessEnabled() added above skips it for the unintended targets. This is mainly to restrict when targets further want to constrain this optimization for the identified SubReg. For instance, we don't want this optimization for 16-bit subreg accesses. I think the comment I added here is misleading. I need to refine that to include the point I just mentioned. Will do.

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.

I don't get why we need to check that. The target can decide to ignore the subreg if it doesn't support it.

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.

Yes, the targets can choose to ignore the subreg field passed to them within their loadRegFromStackSlot. However, that info (whether the target implemented subreg reload or not) should be returned to this callsite as we remove the subreg field in the use instruction as part of this optimization when the target truly implements the partial reload by constructing a concrete class for the subreg access. See the transition explained here.

%tuple:VReg_128 = ... ; 128-bit tuple (4x32-bit)
SPILL_V128 %tuple to stack
...
; Later, only need sub1 (second 32-bit component)
; Current implementation - restore full.
%reload:Vreg_128 = RESTORE_V128, ofst:0
%val = USE %reload.sub1

; With subreg reload implemented.
%reload:VGPR_32 = RESTORE_V32, ofst:4
%val = USE %reload // drop the subreg.
The subreg fields are dropped in the InlineSpiller at https://github.com/llvm/llvm-project/pull/175002/files#diff-855df7e3f96ef7f3f499fdafba308dde780d710f717f19158a2f39059c8a6f5dR1305.
To pass the subreg info always inside loadRegFromStackSlot and let the targets decide whether to implement it or not, requires some changes in the way how InlineSpiller and this target hook interact.

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.

I've updated the API name and documentation to reflect its use better.

@cdevadas
cdevadas force-pushed the users/cdevadas/subreg-reload branch from 2a12cb5 to 986a1a5 Compare January 12, 2026 13:29
@cdevadas
cdevadas force-pushed the users/cdevadas/precommit-subreg-reload-test branch 2 times, most recently from 7676097 to 2b2772b Compare January 12, 2026 15:29
@cdevadas
cdevadas force-pushed the users/cdevadas/subreg-reload branch from 986a1a5 to b221906 Compare January 12, 2026 15:29
cdevadas added a commit that referenced this pull request Jan 12, 2026
…for subreg-reload

This preparatory patch introduces an additional argument to the target hook
loadRegFromStackSlot. Ths is essential for targets to handle subregister-specific
reload in the future. See how this is used for AMDGPU target with PR #175002.
Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Resolved
cdevadas added a commit that referenced this pull request Jan 13, 2026
…for subreg-reload (#175581)

This preparatory patch introduces an additional argument to the target hook
loadRegFromStackSlot. Ths is essential for targets to handle subregister-specific
reload in the future. See how this is used for AMDGPU target with PR #175002.
@cdevadas
cdevadas force-pushed the users/cdevadas/precommit-subreg-reload-test branch from 2b2772b to 12d1aa0 Compare January 13, 2026 05:07
@cdevadas
cdevadas force-pushed the users/cdevadas/subreg-reload branch from b221906 to 070b3e9 Compare January 13, 2026 05:07
@cdevadas

Copy link
Copy Markdown
Contributor Author

Rebased the code after merging PR #175581.

/// To enable subreg reload of register tuples during RA. This would
/// eventually improve the register allocation for the functions that involve
/// subreg uses instead of the entire tuple.
virtual bool shouldEnableSubRegReload(unsigned SubReg) const { return false; }

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.

I'll have to look at the details later in the PR, but can't we always produce the subreg info and that's on the targets to decide whether they want to leverage it?

Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Resolved
Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Resolved
Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Outdated

// If the target doesn't support subreg reload, fallback to restoring the
// full tuple.
if (SubReg && !TRI.shouldEnableSubRegReload(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.

I don't get why we need to check that. The target can decide to ignore the subreg if it doesn't support it.

Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Outdated

const TargetRegisterClass *OrigRC = MRI.getRegClass(Reg);
const TargetRegisterClass *NewRC =
SubReg ? TRI.getSubRegisterClass(OrigRC, SubReg) : nullptr;

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.

Could the target figure that out on their own within insertReload?

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.

I guess my previous comment justifies why I want to handle it here instead of the insertReload or the target hook itself. They both need to return a status whether the subreg reload is truly implemented or not.

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.

What I'm saying is: Assume we always reload the full tuple so that we don't have to handle the update of the subreg uses here.

The question is can this be easily folded later on?

To get back to your example.
Instead of generating:

; Later, only need sub1 (second 32-bit component)
; Current implementation - restore full.
%reload:Vreg_128 = RESTORE_V128, ofst:0
%val = USE %reload.sub1

This patch wants to generate:

%reload32:Vreg_32 = RESTORE_V32, ofst:Y
%val = USE %reload32

What I'm saying is could we generate:

%reload32:Vreg_32 = RESTORE_V32, ofst:Y
%reload = INSERT_SUBREG undef, %reload32, sub1 <-- At the API boundary assume we fully restore
%val = USE %reload.sub1 <-- this part doesn't need to be updated here

What I'm saying here is a lot of the "complexity" in this patch is changing %reload in %reload32, i.e., change the register class and the subreg idx. I'm wondering if we can do that clean-up later.

I'm trying to get a read of the trade-off between ease to understand the code vs. doing the whole transformation in one-shot/several-shots.

@qcolombet qcolombet Jan 26, 2026 •

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.

FWIW, the reason I'm trying to split the update of the subreg idx from the insertReload and so on is because the handling of the LaneMask is hacky and although it works for AMDGPU, that may not be generally correct.
Hence, at least for now, I would rather this piece of code to live in the backend-specific part.

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.

The subreg reload brings two advantages.

  1. Currently, when a tuple is reloaded, the full tuple becomes live at the reload point, even if only a subset of its components is actually needed. On targets like AMDGPU, this creates difficulties later during the expansion of the reload pseudo-instruction into individual reload operations, because the unused or undefined subregisters still appear live. They are often patched with ad hoc fixups such as inserting implicit-def or implicit operands for the unneeded tuple components to avoid miscompilations. The subreg reload fixes this broken liveness info for partial uses of tuples chosen for spilling. It avoids introducing spurious undef subregs and eliminates the need for such hacky post-RA workarounds.
  2. Trimming down the registers really helps improve the allocation. Instead of the full tuple, we ensure RA reloads only the relevant subregs.

It is not clear to me how RA will see %reload = INSERT_SUBREG undef, .. (the one you suggested). We may miss the two advantages I mentioned here.

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.

The subreg reload fixes this broken liveness info for partial uses of tuples chosen for spilling

I agree, but I'm saying let's do the subreg reload but from the rest of the code this is as if the full register is reloaded.

Trimming down the registers really helps improve the allocation. Instead of the full tuple, we ensure RA reloads only the relevant subregs.

I agree as well. What I'm trying to push for is move the complexity of dealing with the subreg/regclass on the target itself (if possible) and in a dedicated helper for the cleanup (next sentence).
After %reload = INSERT_SUBREG undef, .. is generated, we can peephole it to generate the simplified sequence that you generate in one-shot here. That would separate the update of the register class and the subregister index in two different part.

if (MO.isUse()) {
if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
if (SubReg && !MI.isBundled())
MO.setSubReg(0);

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.

Just a thought that goes in the same direction as hiding the whole target complexity within insertReload (https://github.com/llvm/llvm-project/pull/175002/changes#r2723162638).
Could we expect target to return a full register?
E.g.,

; mir pseudo code
subreg = reload subreg
NewVReg = insert_subreg undef, subreg, subidx
= use NewVReg.subidx

That way we don't have to deal with the register class updates and the subregidx adjustment.
The question would be is this copy (insert_subreg) get properly folded later?

The reason I'm suggesting that is that I don't like the logic that this creates (maybe I need to stare at it longer), because I feel it is counter intuitive to do if subreg ==> setSubReg(0).

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.

I didn't quite follow what you proposed here with the pseudo code. Can you give an example, as I stated with the other 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.

@easyonaadit
easyonaadit force-pushed the users/cdevadas/subreg-reload branch from 070b3e9 to 829c8e9 Compare July 31, 2026 09:20
@easyonaadit
easyonaadit force-pushed the users/cdevadas/precommit-subreg-reload-test branch from 12d1aa0 to 841b269 Compare July 31, 2026 09:20
@easyonaadit
easyonaadit force-pushed the users/cdevadas/subreg-reload branch from 829c8e9 to ee9bc03 Compare September 29, 2026 13:01
@easyonaadit
easyonaadit force-pushed the users/cdevadas/precommit-subreg-reload-test branch from 841b269 to 4bc23d4 Compare September 29, 2026 13:01
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

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

@easyonaadit
easyonaadit force-pushed the users/cdevadas/subreg-reload branch from ee9bc03 to 7a5fcfb Compare September 29, 2026 13:34
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 142490 tests passed
  • 3347 tests skipped

✅ The build succeeded and all tests passed.

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 175264 tests passed
  • 5304 tests skipped

✅ The build succeeded and all tests passed.

cdevadas and others added 2 commits September 30, 2026 17:10
Currently, when a virtual register is partially used, the
entire tuple is restored from the spilled location, even if
only a subset of its sub-registers is needed. This patch
introduces support for partial reloads by analyzing actual
register usage and restoring only the required sub-registers.
This improvement enhances register allocation efficiency,
particularly for cases involving tuple virtual registers.
For AMDGPU, this change brings considerable improvements
in workloads that involve matrix operations, large vectors,
and complex control flows.
Avoiding Lanebitemask manipulations.
Moved most of generic the calculations to use TargetRegisterInfo APIs.
Added a new API to get the covering subreg index given a lanebitmask.
@easyonaadit
easyonaadit force-pushed the users/cdevadas/precommit-subreg-reload-test branch from 4bc23d4 to a23820b Compare September 30, 2026 11:48
@easyonaadit
easyonaadit force-pushed the users/cdevadas/subreg-reload branch from 8653a63 to 94a6d82 Compare September 30, 2026 11:48
@easyonaadit

Copy link
Copy Markdown
Contributor

Ping.

This branch has not been deployed

No deployments
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.

6 participants