[llvm] [RISCV] Copy AVLs whose LiveIntervals aren't extendable in insertVSETVLI (PR #98342)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 19:35:29 PDT 2024


================
@@ -1149,15 +1143,26 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
                 .addImm(Info.encodeVTYPE());
   if (LIS) {
     LIS->InsertMachineInstrInMaps(*MI);
-    // Normally the AVL's live range will already extend past the inserted
-    // vsetvli because the pseudos below will already use the AVL. But this
-    // isn't always the case, e.g. PseudoVMV_X_S doesn't have an AVL operand or
-    // we've taken the AVL from the VL output of another vsetvli.
     LiveInterval &LI = LIS->getInterval(AVLReg);
     SlotIndex SI = LIS->getInstructionIndex(*MI).getRegSlot();
-    assert((LI.liveAt(SI) && LI.getVNInfoAt(SI) == Info.getAVLVNInfo()) ||
-           (!LI.liveAt(SI) && LI.containsOneValue()));
-    LIS->extendToIndices(LI, SI);
+    // If the AVL value isn't live at MI, do a quick check to see if it's easily
+    // extendable. Otherwise, we need to copy it.
+    if (LI.getVNInfoBefore(SI) != Info.getAVLVNInfo()) {
+      if (!LI.liveAt(SI) && LI.containsOneValue())
+        LIS->extendToIndices(LI, SI);
+      else {
+        Register AVLCopyReg =
+            MRI->createVirtualRegister(&RISCV::GPRNoX0RegClass);
+        MachineBasicBlock::iterator AVLDef =
----------------
lukel97 wrote:

> Are we guaranteed that the Slot_Block entry maps to an MachineInstr? Getting a MachineInstr can return nullptr, and I'm not clear about the exact circumstances.

Thanks for catching that, turns out`getInstructionFromIndex` does return null for that. I've added explicit handling for this to insert it with the other PHIs. We also didn't have any test cases that hit this with a PHI so I've added one. 

Sorry I see what you mean about removing the extend check now, I got it confused with the check on line 953. I can confirm that does work and is generic. 

https://github.com/llvm/llvm-project/pull/98342


More information about the llvm-commits mailing list