Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveIntervals] repairIntervalsInRange: recompute width changes #78564

Merged
merged 1 commit into from
Mar 11, 2024

Conversation

perlfu
Copy link
Contributor

@perlfu perlfu commented Jan 18, 2024

Extend repairIntervalsInRange to completely recompute the interva for a register if subregister defs exist without precise subrange matches (LaneMask exactly matching subregister).
This occurs when register sequences are lowered to copies such that the size of the copies do not match any uses of the subregisters formed (i.e. during twoaddressinstruction).

The subranges without this change are probably legal, but do not match those generated by live interval computation. This creates problems with other code that assumes subranges precisely cover all subregisters defined, e.g. shrinkToUses().

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 18, 2024

@llvm/pr-subscribers-llvm-regalloc

@llvm/pr-subscribers-backend-amdgpu

Author: Carl Ritson (perlfu)

Changes

Extend repairIntervalsInRange to completely recompute the interva for a register if subregister defs exist without precise subrange matches (LaneMask exactly matching subregister).
This occurs when register sequences are lowered to copies such that the size of the copies do not match any uses of the subregisters formed (i.e. during twoaddressinstruction).

The subranges without this change are probably legal, but do not match those generated by live interval computation. This creates problems with other code that assumes subranges precisely cover all subregisters defined, e.g. shrinkToUses().


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/LiveIntervals.cpp (+23-6)
  • (modified) llvm/test/CodeGen/AMDGPU/lds-misaligned-bug.ll (+1)
diff --git a/llvm/lib/CodeGen/LiveIntervals.cpp b/llvm/lib/CodeGen/LiveIntervals.cpp
index 68fff9bc221d0b..53693387a3e856 100644
--- a/llvm/lib/CodeGen/LiveIntervals.cpp
+++ b/llvm/lib/CodeGen/LiveIntervals.cpp
@@ -1666,13 +1666,30 @@ LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB,
     for (const MachineOperand &MO : MI.operands()) {
       if (MO.isReg() && MO.getReg().isVirtual()) {
         Register Reg = MO.getReg();
-        // If the new instructions refer to subregs but the old instructions did
-        // not, throw away any old live interval so it will be recomputed with
-        // subranges.
         if (MO.getSubReg() && hasInterval(Reg) &&
-            !getInterval(Reg).hasSubRanges() &&
-            MRI->shouldTrackSubRegLiveness(Reg))
-          removeInterval(Reg);
+            MRI->shouldTrackSubRegLiveness(Reg)) {
+          LiveInterval &LI = getInterval(Reg);
+          if (!LI.hasSubRanges()) {
+            // If the new instructions refer to subregs but the old instructions
+            // did not, throw away any old live interval so it will be
+            // recomputed with subranges.
+            removeInterval(Reg);
+          } else if (MO.isDef()) {
+            // Similarly if a subreg def has no precise subrange match then
+            // assume we need to recompute all subranges.
+            unsigned SubReg = MO.getSubReg();
+            LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg);
+            bool hasMatch = false;
+            for (auto &SR : LI.subranges()) {
+              if (SR.LaneMask != Mask)
+                continue;
+              hasMatch = true;
+              break;
+            }
+            if (!hasMatch)
+              removeInterval(Reg);
+          }
+        }
         if (!hasInterval(Reg)) {
           createAndComputeVirtRegInterval(Reg);
           // Don't bother to repair a freshly calculated live interval.
diff --git a/llvm/test/CodeGen/AMDGPU/lds-misaligned-bug.ll b/llvm/test/CodeGen/AMDGPU/lds-misaligned-bug.ll
index 3a8f06ba59a129..01af3346523827 100644
--- a/llvm/test/CodeGen/AMDGPU/lds-misaligned-bug.ll
+++ b/llvm/test/CodeGen/AMDGPU/lds-misaligned-bug.ll
@@ -5,6 +5,7 @@
 ; RUN: llc -mtriple=amdgcn -mcpu=gfx1010 -verify-machineinstrs -mattr=+cumode,+unaligned-access-mode < %s | FileCheck -check-prefixes=GCN,UNALIGNED,VECT %s
 ; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -verify-machineinstrs < %s | FileCheck -check-prefixes=GCN,ALIGNED,VECT %s
 ; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -verify-machineinstrs -mattr=+cumode < %s | FileCheck -check-prefixes=GCN,ALIGNED,VECT %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -verify-machineinstrs -mattr=+cumode -early-live-intervals < %s | FileCheck -check-prefixes=GCN,ALIGNED,VECT %s
 ; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -verify-machineinstrs -mattr=+cumode,+unaligned-access-mode < %s | FileCheck -check-prefixes=GCN,UNALIGNED,VECT %s
 
 ; GCN-LABEL: test_local_misaligned_v2:

Copy link
Contributor

@jayfoad jayfoad left a comment

Choose a reason for hiding this comment

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

I think splitting copies sometimes produces instructions like this, where the def is narrow but there is also an implicit-def of the full width register:

$vgpr0 = V_MOV_B32_e32 $sgpr0, implicit $exec, implicit-def $vgpr0_vgpr1, implicit $sgpr0_sgpr1

How should these be handled? Your patch will call removeInterval on them.

llvm/lib/CodeGen/LiveIntervals.cpp Outdated Show resolved Hide resolved
@perlfu
Copy link
Contributor Author

perlfu commented Jan 21, 2024

$vgpr0 = V_MOV_B32_e32 $sgpr0, implicit $exec, implicit-def $vgpr0_vgpr1, implicit $sgpr0_sgpr1

How should these be handled? Your patch will call removeInterval on them.

That's an interesting point.
This change will not break those, as they will be recomputed in the code below.
It creates some inefficiency as we perform unnecessary recomputations; however, is it worth the cost to detect them?
I guess we could simply disable this logic for implicit defs?

Extend repairIntervalsInRange to completely recompute the interva
for a register if subregister defs exist without precise subrange
matches (LaneMask exactly matching subregister).
This occurs when register sequences are lowered to copies such
that the size of the copies do not match any uses of the
subregisters formed (i.e. during twoaddressinstruction).

The subranges without this change are probably legal, but do not
match those generated by live interval computation.  This creates
problems with other code that assumes subranges precisely cover
all subregisters defined, e.g. shrinkToUses().
@perlfu perlfu force-pushed the lis-repair-recompute-subranges branch from abbbd9d to d20a524 Compare January 30, 2024 03:35
@perlfu
Copy link
Contributor Author

perlfu commented Jan 30, 2024

  • Update to use llvm::none_of
  • Rebase

@perlfu perlfu merged commit 4a21e3a into llvm:main Mar 11, 2024
4 checks passed
@perlfu perlfu deleted the lis-repair-recompute-subranges branch March 11, 2024 06:24
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.

None yet

4 participants