From eb76bc38ffc286e62fdb8f8d897b5de04b2575be Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Sun, 23 Jun 2024 20:20:59 +0800 Subject: [PATCH] [RISCV] Relax RISCVInsertVSETVLI output VL peeking to cover registers (#96200) If the AVL in a VSETVLIInfo is the output VL of a vsetvli with the same VLMAX, we treat it as the AVL of said vsetvli. This allows us to remove a true dependency as well as treating VSETVLIInfos as equal in more places and avoid toggles. We do this in two places, needVSETVLI and computeInfoForInstr. However we don't do this in computeInfoForInstr's vsetvli equivalent, getInfoForVSETVLI. We also have a restriction only in computeInfoForInstr that the AVL can't be a register as we want to avoid extending live ranges. This patch does two interlinked things: 1) It adds this AVL "peeking" to getInfoForVSETVLI 2) It relaxes the constraint that the AVL can't be a register in computeInfoForInstr, since it removes a use of the output VL which can actually reduce register pressure. E.g. see the diff in @vector_init_vsetvli_N and @test6 Now that getInfoForVSETVLI and computeInfoForInstr are consistent, we can remove the check in needVSETVLI. We also need to update how we update LiveIntervals in insertVSETVLI, as we can now end up needing to extend the LiveRange of the AVL across blocks. --- llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp | 41 ++++++++-------- .../RISCV/rvv/vsetvli-insert-crossbb.ll | 48 ++++++++++--------- llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll | 1 - .../RISCV/lsr-drop-solution.ll | 1 - 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp index 877535513c72..e8541dc2b5c8 100644 --- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp +++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp @@ -962,6 +962,17 @@ RISCVInsertVSETVLI::getInfoForVSETVLI(const MachineInstr &MI) const { } NewInfo.setVTYPE(MI.getOperand(2).getImm()); + // If AVL is defined by a vsetvli with the same VLMAX, we can replace the + // AVL operand with the AVL of the defining vsetvli. + if (NewInfo.hasAVLReg()) { + if (const MachineInstr *DefMI = NewInfo.getAVLDefMI(LIS); + DefMI && isVectorConfigInstr(*DefMI)) { + VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI); + if (DefInstrInfo.hasSameVLMAX(NewInfo)) + NewInfo.setAVL(DefInstrInfo); + } + } + return NewInfo; } @@ -1050,15 +1061,12 @@ RISCVInsertVSETVLI::computeInfoForInstr(const MachineInstr &MI) const { InstrInfo.setVTYPE(VLMul, SEW, TailAgnostic, MaskAgnostic); // If AVL is defined by a vsetvli with the same VLMAX, we can replace the - // AVL operand with the AVL of the defining vsetvli. We avoid general - // register AVLs to avoid extending live ranges without being sure we can - // kill the original source reg entirely. + // AVL operand with the AVL of the defining vsetvli. if (InstrInfo.hasAVLReg()) { if (const MachineInstr *DefMI = InstrInfo.getAVLDefMI(LIS); DefMI && isVectorConfigInstr(*DefMI)) { VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI); - if (DefInstrInfo.hasSameVLMAX(InstrInfo) && - (DefInstrInfo.hasAVLImm() || DefInstrInfo.hasAVLVLMAX())) + if (DefInstrInfo.hasSameVLMAX(InstrInfo)) InstrInfo.setAVL(DefInstrInfo); } } @@ -1146,9 +1154,13 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB, 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. - LIS->getInterval(AVLReg).extendInBlock( - LIS->getMBBStartIdx(&MBB), LIS->getInstructionIndex(*MI).getRegSlot()); + // 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); + // Need to get non-const VNInfo + VNInfo *VNI = LI.getValNumInfo(Info.getAVLVNInfo()->id); + LI.addSegment(LiveInterval::Segment( + VNI->def, LIS->getInstructionIndex(*MI).getRegSlot(), VNI)); } } @@ -1163,19 +1175,6 @@ bool RISCVInsertVSETVLI::needVSETVLI(const DemandedFields &Used, if (CurInfo.isCompatible(Used, Require, LIS)) return false; - // We didn't find a compatible value. If our AVL is a virtual register, - // it might be defined by a VSET(I)VLI. If it has the same VLMAX we need - // and the last VL/VTYPE we observed is the same, we don't need a - // VSETVLI here. - if (Require.hasAVLReg() && CurInfo.hasCompatibleVTYPE(Used, Require)) { - if (const MachineInstr *DefMI = Require.getAVLDefMI(LIS); - DefMI && isVectorConfigInstr(*DefMI)) { - VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI); - if (DefInfo.hasSameAVL(CurInfo) && DefInfo.hasSameVLMAX(CurInfo)) - return false; - } - } - return true; } diff --git a/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-crossbb.ll b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-crossbb.ll index 7eb6cacf1ca4..5a6364967eba 100644 --- a/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-crossbb.ll +++ b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-crossbb.ll @@ -234,24 +234,24 @@ if.end6: ; preds = %if.else5, %if.then4 define @test6(i64 %avl, i8 zeroext %cond, %a, %b) nounwind { ; CHECK-LABEL: test6: ; CHECK: # %bb.0: # %entry -; CHECK-NEXT: andi a3, a1, 1 -; CHECK-NEXT: vsetvli a2, a0, e64, m1, ta, ma -; CHECK-NEXT: bnez a3, .LBB5_3 +; CHECK-NEXT: andi a2, a1, 1 +; CHECK-NEXT: vsetvli zero, a0, e64, m1, ta, ma +; CHECK-NEXT: bnez a2, .LBB5_3 ; CHECK-NEXT: # %bb.1: # %if.else ; CHECK-NEXT: vfsub.vv v8, v8, v9 ; CHECK-NEXT: andi a1, a1, 2 ; CHECK-NEXT: beqz a1, .LBB5_4 ; CHECK-NEXT: .LBB5_2: # %if.then4 -; CHECK-NEXT: lui a0, %hi(.LCPI5_0) -; CHECK-NEXT: addi a0, a0, %lo(.LCPI5_0) -; CHECK-NEXT: vlse64.v v9, (a0), zero -; CHECK-NEXT: lui a0, %hi(.LCPI5_1) -; CHECK-NEXT: addi a0, a0, %lo(.LCPI5_1) -; CHECK-NEXT: vlse64.v v10, (a0), zero +; CHECK-NEXT: lui a1, %hi(.LCPI5_0) +; CHECK-NEXT: addi a1, a1, %lo(.LCPI5_0) +; CHECK-NEXT: vlse64.v v9, (a1), zero +; CHECK-NEXT: lui a1, %hi(.LCPI5_1) +; CHECK-NEXT: addi a1, a1, %lo(.LCPI5_1) +; CHECK-NEXT: vlse64.v v10, (a1), zero ; CHECK-NEXT: vfadd.vv v9, v9, v10 -; CHECK-NEXT: lui a0, %hi(scratch) -; CHECK-NEXT: addi a0, a0, %lo(scratch) -; CHECK-NEXT: vse64.v v9, (a0) +; CHECK-NEXT: lui a1, %hi(scratch) +; CHECK-NEXT: addi a1, a1, %lo(scratch) +; CHECK-NEXT: vse64.v v9, (a1) ; CHECK-NEXT: j .LBB5_5 ; CHECK-NEXT: .LBB5_3: # %if.then ; CHECK-NEXT: vfadd.vv v8, v8, v9 @@ -259,16 +259,16 @@ define @test6(i64 %avl, i8 zeroext %cond, @test8(i64 %avl, i8 zeroext %cond, @test9(i64 %avl, i8 zeroext %cond, This Inner Loop Header: Depth=1 -; CHECK-NEXT: vsetvli zero, a3, e64, m1, ta, ma +; CHECK-NEXT: vsetvli zero, a0, e64, m1, ta, ma ; CHECK-NEXT: vse64.v v8, (a1) ; CHECK-NEXT: add a2, a2, a3 ; CHECK-NEXT: add a1, a1, a4 diff --git a/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll index da0c1cfb5009..7f01fd4d945c 100644 --- a/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll +++ b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll @@ -258,7 +258,6 @@ entry: define @test14(i64 %avl, %a, %b) nounwind { ; CHECK-LABEL: test14: ; CHECK: # %bb.0: # %entry -; CHECK-NEXT: vsetvli a0, a0, e32, mf2, ta, ma ; CHECK-NEXT: vsetivli zero, 1, e64, m1, ta, ma ; CHECK-NEXT: vfadd.vv v8, v8, v9 ; CHECK-NEXT: vsetvli zero, a0, e64, m1, ta, ma diff --git a/llvm/test/Transforms/LoopStrengthReduce/RISCV/lsr-drop-solution.ll b/llvm/test/Transforms/LoopStrengthReduce/RISCV/lsr-drop-solution.ll index 1ab19081b7de..7353acd7228c 100644 --- a/llvm/test/Transforms/LoopStrengthReduce/RISCV/lsr-drop-solution.ll +++ b/llvm/test/Transforms/LoopStrengthReduce/RISCV/lsr-drop-solution.ll @@ -20,7 +20,6 @@ define ptr @foo(ptr %a0, ptr %a1, i64 %a2) { ; CHECK-NEXT: mv a3, a0 ; CHECK-NEXT: .LBB0_3: # %do.body ; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 -; CHECK-NEXT: vsetvli zero, a4, e8, m8, ta, ma ; CHECK-NEXT: vle8.v v8, (a1) ; CHECK-NEXT: vse8.v v8, (a3) ; CHECK-NEXT: add a3, a3, a4