[llvm] [RISCV] Support postRA vsetvl insertion pass (PR #70549)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 05:19:31 PDT 2024


================
@@ -51,6 +51,38 @@ static cl::opt<bool> UseStrictAsserts(
 
 namespace {
 
+// For the SSA form, we could just use the getVRegDef to take Reaching
+// definition. For the non-SSA, we retrieve reaching definition for specific
+// register from LiveInterval/VNInfo.
+template <typename T>
+static T *getReachingDefMI(Register Reg, T *MI, const MachineRegisterInfo *MRI,
+                           const LiveIntervals *LIS) {
+  
+  if (MRI->isSSA() || !MI)
+    return MRI->getUniqueVRegDef(Reg);
+
+  // For O0 situation
+  if (!LIS)
+    return nullptr;
----------------
lukel97 wrote:

I think this arises because we only mark LiveIntervals as `addUsedIfAvailable` and not `addRequired`, and IIRC this is because we still have the option to run the pass pre-RA and post-RA. 

What do you think about removing the flag in this PR so we always have LiveIntervals?

Off the top of my head there's a few reasons why I think we should do this.

Strategically:
- (Probably the most important) We would be able see the test diffs in this PR
- The next LLVM release isn't for a while so the sooner we can enable this by default, the more time we will have to iron out any issues.
- #88295 has landed and there doesn't seem to be any issues so far, so we should be able to move forward more confidently now that we have a proof point that using LiveIntervals is possible

And then in terms of the actual code:
- We won't need to handle the -O0 case where both MRI and LIS are null
- We shouldn't need to pass around MRI as much (we'll only need it to create a new virtual register for VLMAX)
- `hasUndefinedMergeOp` becomes just MachineOperand::isUndef()
- We won't need two versions of `needVSETVLIPHI`
- We can let LIS handle deleting any dead ADDIs from immediates > 31 (which would mean we wouldn't need #90598):
```diff
-          // If the AVL was an immediate > 31, then it would have been emitted
-          // as an ADDI. However, the ADDI might not have been used in the
-          // vsetvli, or a vsetvli might not have been emitted, so it may be
-          // dead now.
-          if (VLOpDef && TII->isAddImmediate(*VLOpDef, Reg) &&
-              MRI->use_nodbg_empty(Reg))
-            removeMIAndFixupLI(VLOpDef, LIS);
-          if (IsVirtVLOpReg)
-            fixupModifyVRegLI(VLOpReg, LIS);
+          SmallVector<MachineInstr *> DeadMIs;
+          if (LIS) {
+            LIS->shrinkToUses(&LI, &DeadMIs);
+            for (MachineInstr *DeadMI : DeadMIs) {
+              LIS->RemoveMachineInstrFromMaps(*DeadMI);
+              DeadMI->eraseFromParent();
+            }
+          }
```



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


More information about the llvm-commits mailing list