Skip to content

Commit 50b0a49

Browse files
committed
[RISCV] Copy AVLs whose LiveIntervals aren't extendable in insertVSETVLI
Currently we do a simple non-exhaustive check to see if a LiveInterval is extendable before forwarding an AVL. But we also need to check for this when we're extending the live range via merging the VSETVLIInfos in transferBefore with equally zero AVLs. Rather than trying to conservatively prevent these cases, this inserts a copy of the AVL instead if we don't know we'll be able to extend it. This is likely to be more robust, and even if the extra copy is undesirable these cases should be rare in practice.
1 parent 748a6ad commit 50b0a49

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -950,12 +950,6 @@ void RISCVInsertVSETVLI::forwardVSETVLIAVL(VSETVLIInfo &Info) const {
950950
VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI);
951951
if (!DefInstrInfo.hasSameVLMAX(Info))
952952
return;
953-
// If the AVL is a register with multiple definitions, don't forward it. We
954-
// might not be able to extend its LiveInterval without clobbering other val
955-
// nums.
956-
if (DefInstrInfo.hasAVLReg() &&
957-
!LIS->getInterval(DefInstrInfo.getAVLReg()).containsOneValue())
958-
return;
959953
Info.setAVL(DefInstrInfo);
960954
}
961955

@@ -1149,15 +1143,26 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
11491143
.addImm(Info.encodeVTYPE());
11501144
if (LIS) {
11511145
LIS->InsertMachineInstrInMaps(*MI);
1152-
// Normally the AVL's live range will already extend past the inserted
1153-
// vsetvli because the pseudos below will already use the AVL. But this
1154-
// isn't always the case, e.g. PseudoVMV_X_S doesn't have an AVL operand or
1155-
// we've taken the AVL from the VL output of another vsetvli.
11561146
LiveInterval &LI = LIS->getInterval(AVLReg);
11571147
SlotIndex SI = LIS->getInstructionIndex(*MI).getRegSlot();
1158-
assert((LI.liveAt(SI) && LI.getVNInfoAt(SI) == Info.getAVLVNInfo()) ||
1159-
(!LI.liveAt(SI) && LI.containsOneValue()));
1160-
LIS->extendToIndices(LI, SI);
1148+
// If the AVL value isn't live at MI, do a quick check to see if it's easily
1149+
// extendable. Otherwise, we need to copy it.
1150+
if (LI.getVNInfoBefore(SI) != Info.getAVLVNInfo()) {
1151+
if (!LI.liveAt(SI) && LI.containsOneValue())
1152+
LIS->extendToIndices(LI, SI);
1153+
else {
1154+
Register AVLCopyReg =
1155+
MRI->createVirtualRegister(&RISCV::GPRNoX0RegClass);
1156+
MachineBasicBlock::iterator AVLDef =
1157+
LIS->getInstructionFromIndex(Info.getAVLVNInfo()->def);
1158+
auto AVLCopy = BuildMI(*AVLDef->getParent(), std::next(AVLDef), DL,
1159+
TII->get(RISCV::COPY), AVLCopyReg)
1160+
.addReg(AVLReg);
1161+
LIS->InsertMachineInstrInMaps(*AVLCopy);
1162+
MI->getOperand(1).setReg(AVLCopyReg);
1163+
LIS->createAndComputeVirtRegInterval(AVLCopyReg);
1164+
}
1165+
}
11611166
}
11621167
}
11631168

llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-crossbb.ll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,12 +1126,13 @@ exit:
11261126
ret void
11271127
}
11281128

1129-
; Check that we don't forward an AVL if we wouldn't be able to extend its
1130-
; LiveInterval without clobbering other val nos.
1131-
define <vscale x 4 x i32> @unforwardable_avl(i64 %n, <vscale x 4 x i32> %v, i1 %cmp) {
1132-
; CHECK-LABEL: unforwardable_avl:
1129+
; Check that if we forward an AVL whose value is clobbered in its LiveInterval
1130+
; we emit a copy instead.
1131+
define <vscale x 4 x i32> @clobbered_forwarded_avl(i64 %n, <vscale x 4 x i32> %v, i1 %cmp) {
1132+
; CHECK-LABEL: clobbered_forwarded_avl:
11331133
; CHECK: # %bb.0: # %entry
1134-
; CHECK-NEXT: vsetvli a2, a0, e32, m2, ta, ma
1134+
; CHECK-NEXT: mv a2, a0
1135+
; CHECK-NEXT: vsetvli zero, a0, e32, m2, ta, ma
11351136
; CHECK-NEXT: andi a1, a1, 1
11361137
; CHECK-NEXT: .LBB27_1: # %for.body
11371138
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1

llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-crossbb.mir

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
ret void
135135
}
136136

137-
define void @unforwardable_avl() {
137+
define void @clobberred_forwarded_avl() {
138138
ret void
139139
}
140140

@@ -995,16 +995,17 @@ body: |
995995
PseudoBR %bb.1
996996
...
997997
---
998-
name: unforwardable_avl
998+
name: clobberred_forwarded_avl
999999
tracksRegLiveness: true
10001000
body: |
1001-
; CHECK-LABEL: name: unforwardable_avl
1001+
; CHECK-LABEL: name: clobberred_forwarded_avl
10021002
; CHECK: bb.0:
10031003
; CHECK-NEXT: successors: %bb.1(0x80000000)
10041004
; CHECK-NEXT: liveins: $x10, $v8m2
10051005
; CHECK-NEXT: {{ $}}
10061006
; CHECK-NEXT: %avl:gprnox0 = COPY $x10
1007-
; CHECK-NEXT: %outvl:gprnox0 = PseudoVSETVLI %avl, 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
1007+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gprnox0 = COPY %avl
1008+
; CHECK-NEXT: dead %outvl:gprnox0 = PseudoVSETVLI %avl, 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
10081009
; CHECK-NEXT: {{ $}}
10091010
; CHECK-NEXT: bb.1:
10101011
; CHECK-NEXT: successors: %bb.2(0x80000000)
@@ -1017,7 +1018,7 @@ body: |
10171018
; CHECK-NEXT: {{ $}}
10181019
; CHECK-NEXT: dead [[PseudoVSETVLIX0_:%[0-9]+]]:gpr = PseudoVSETVLIX0 killed $x0, 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
10191020
; CHECK-NEXT: renamable $v10m2 = PseudoVADD_VV_M2 undef renamable $v10m2, renamable $v8m2, renamable $v8m2, -1, 5 /* e32 */, 0 /* tu, mu */, implicit $vl, implicit $vtype
1020-
; CHECK-NEXT: dead $x0 = PseudoVSETVLI %outvl, 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
1021+
; CHECK-NEXT: dead $x0 = PseudoVSETVLI [[COPY]], 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
10211022
; CHECK-NEXT: renamable $v8m2 = PseudoVADD_VV_M2 undef renamable $v8m2, killed renamable $v10m2, renamable $v8m2, $noreg, 5 /* e32 */, 0 /* tu, mu */, implicit $vl, implicit $vtype
10221023
; CHECK-NEXT: PseudoRET implicit $v8m2
10231024
bb.0:

0 commit comments

Comments
 (0)