Skip to content

[RISCV] Let LiveIntervals::shrinkToUses compute dead AVLs #90629

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lukel97
Copy link
Contributor

@lukel97 lukel97 commented Apr 30, 2024

We currently manually remove dead AVL defs if they are ADDIs used to materialize immediates > 31.
shrinkToUses already computes dead instructions though, so this patch uses it to generalize to any type of dead def.

As previously noted, shrinkToUses only checks if the instruction is dead so we need to also check if it's safe to use. Here we reuse the checks in LiveRangeEdit::eliminateDeadDef.

This fixes #95865 by removing the dead AVL from the LiveIntervals instruction map

@lukel97 lukel97 requested a review from BeMg April 30, 2024 17:22
@llvmbot
Copy link
Member

llvmbot commented Apr 30, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Luke Lau (lukel97)

Changes

We can simplify removing dead AVL immediates > 31 by using the dead argument to shrinkToUses, since it will already compute dead values.


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

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp (+3-11)
diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
index 216dc78085204e..f4ff17f7b65585 100644
--- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
@@ -1648,17 +1648,9 @@ bool RISCVCoalesceVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) {
           if (NextMI->getOperand(1).isReg())
             NextMI->getOperand(1).setReg(RISCV::NoRegister);
 
-          if (OldVLReg && OldVLReg.isVirtual()) {
-            // NextMI no longer uses OldVLReg so shrink its LiveInterval.
-            LIS->shrinkToUses(&LIS->getInterval(OldVLReg));
-
-            MachineInstr *VLOpDef = MRI->getUniqueVRegDef(OldVLReg);
-            if (VLOpDef && TII->isAddImmediate(*VLOpDef, OldVLReg) &&
-                MRI->use_nodbg_empty(OldVLReg)) {
-              VLOpDef->eraseFromParent();
-              LIS->removeInterval(OldVLReg);
-            }
-          }
+          // NextMI no longer uses OldVLReg so shrink its LiveInterval.
+          if (OldVLReg && OldVLReg.isVirtual())
+            LIS->shrinkToUses(&LIS->getInterval(OldVLReg), &ToDelete);
           MI.setDesc(NextMI->getDesc());
         }
         MI.getOperand(2).setImm(NextMI->getOperand(2).getImm());

@lukel97 lukel97 changed the title [RISCV] Let LiveIntervals::shrinkToUses compute dead immediate. NFC [RISCV] Let LiveIntervals::shrinkToUses compute dead immediates. NFC Apr 30, 2024
}
// NextMI no longer uses OldVLReg so shrink its LiveInterval.
if (OldVLReg && OldVLReg.isVirtual())
LIS->shrinkToUses(&LIS->getInterval(OldVLReg), &ToDelete);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this change is correct. It looks like ShrinkToUses only considers whether definitions are dead, and expects it's callers to validate that the instructions are safe to actually delete. This code doesn't do the required checks, and thus this seemingly obvious change actually introduces a correctness issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, looks like RegisterCoalescer is the main user of this which eventually passes the dead instructions to LiveRangeEdit::eliminateDeadDefs, which in turn checks for bundles, inline asm and isSafeToMove

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR should now perform the same checks as LiveRangeEdit::eliminateDeadDef (isn't bundled, isn't asm, isSafeToMove)

@lukel97 lukel97 changed the title [RISCV] Let LiveIntervals::shrinkToUses compute dead immediates. NFC [RISCV] Let LiveIntervals::shrinkToUses compute dead AVLs May 6, 2024
@lukel97 lukel97 force-pushed the insert-vsetvli-coalesce-simplify-dead-removal branch from d25a4d2 to 983ceef Compare May 6, 2024 06:30
@lukel97 lukel97 force-pushed the insert-vsetvli-coalesce-simplify-dead-removal branch from 983ceef to e448199 Compare June 25, 2024 06:17
We can simplify removing dead AVL immediates > 31 by using the dead argument to shrinkToUses, since it will already compute dead values.
@lukel97 lukel97 force-pushed the insert-vsetvli-coalesce-simplify-dead-removal branch from e448199 to 803dba0 Compare June 25, 2024 06:23
@lukel97
Copy link
Contributor Author

lukel97 commented Jun 25, 2024

The issue reported in #95865 was caused by us forgetting to remove the dead AVL from the LiveIntervals instruction map, but it turns out this PR fixes this by reusing the same code path for the dead AVL and vsetvli. I've rebased this and included the reproducer as a test case cc @patrick-rivos

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.

[RISC-V] Assertion '!mi2iMap.contains(&MI) && "Instr already indexed."' failed
3 participants