Skip to content

Commit faf9295

Browse files
authored
[RISCV] Fix a bug where AVL is the last MI in MBB. (#144668)
When `AVL` is the last MI, `std::next(II)` equals `MBB.end()`, and calling `II->getParent()` at that point will cause an error. --------- Co-authored-by: yanming <[email protected]>
1 parent 2bcdfa1 commit faf9295

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,25 +1119,26 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
11191119
LIS->InsertMachineInstrInMaps(*MI);
11201120
LiveInterval &LI = LIS->getInterval(AVLReg);
11211121
SlotIndex SI = LIS->getInstructionIndex(*MI).getRegSlot();
1122+
const VNInfo *CurVNI = Info.getAVLVNInfo();
11221123
// If the AVL value isn't live at MI, do a quick check to see if it's easily
11231124
// extendable. Otherwise, we need to copy it.
1124-
if (LI.getVNInfoBefore(SI) != Info.getAVLVNInfo()) {
1125+
if (LI.getVNInfoBefore(SI) != CurVNI) {
11251126
if (!LI.liveAt(SI) && LI.containsOneValue())
11261127
LIS->extendToIndices(LI, SI);
11271128
else {
11281129
Register AVLCopyReg =
11291130
MRI->createVirtualRegister(&RISCV::GPRNoX0RegClass);
1131+
MachineBasicBlock *MBB = LIS->getMBBFromIndex(CurVNI->def);
11301132
MachineBasicBlock::iterator II;
1131-
if (Info.getAVLVNInfo()->isPHIDef())
1132-
II = LIS->getMBBFromIndex(Info.getAVLVNInfo()->def)->getFirstNonPHI();
1133+
if (CurVNI->isPHIDef())
1134+
II = MBB->getFirstNonPHI();
11331135
else {
1134-
II = LIS->getInstructionFromIndex(Info.getAVLVNInfo()->def);
1136+
II = LIS->getInstructionFromIndex(CurVNI->def);
11351137
II = std::next(II);
11361138
}
11371139
assert(II.isValid());
1138-
auto AVLCopy =
1139-
BuildMI(*II->getParent(), II, DL, TII->get(RISCV::COPY), AVLCopyReg)
1140-
.addReg(AVLReg);
1140+
auto AVLCopy = BuildMI(*MBB, II, DL, TII->get(RISCV::COPY), AVLCopyReg)
1141+
.addReg(AVLReg);
11411142
LIS->InsertMachineInstrInMaps(*AVLCopy);
11421143
MI->getOperand(1).setReg(AVLCopyReg);
11431144
LIS->createAndComputeVirtRegInterval(AVLCopyReg);

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@
142142
ret void
143143
}
144144

145+
define void @avl_is_last_instr() {
146+
ret void
147+
}
148+
145149
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>)
146150

147151
declare <vscale x 1 x i64> @llvm.riscv.vadd.nxv1i64.nxv1i64.i64(<vscale x 1 x i64>, <vscale x 1 x i64>, <vscale x 1 x i64>, i64) #1
@@ -1099,3 +1103,31 @@ body: |
10991103
renamable $v10m2 = PseudoVADD_VV_M2 undef renamable $v10m2, %v, %v, -1, 5, 0
11001104
renamable $v8m2 = PseudoVADD_VV_M2 undef renamable $v8m2, killed renamable $v10m2, killed %v, %outvl:gprnox0, 5, 0
11011105
PseudoRET implicit $v8m2
1106+
...
1107+
---
1108+
name: avl_is_last_instr
1109+
tracksRegLiveness: true
1110+
body: |
1111+
; CHECK-LABEL: name: avl_is_last_instr
1112+
; CHECK: bb.0:
1113+
; CHECK-NEXT: successors: %bb.1(0x80000000)
1114+
; CHECK-NEXT: liveins: $x10
1115+
; CHECK-NEXT: {{ $}}
1116+
; CHECK-NEXT: %avl:gprnox0 = COPY $x10
1117+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gprnox0 = COPY %avl
1118+
; CHECK-NEXT: {{ $}}
1119+
; CHECK-NEXT: bb.1:
1120+
; CHECK-NEXT: dead %avl:gprnox0 = ADDI %avl, -1
1121+
; CHECK-NEXT: dead $x0 = PseudoVSETIVLI 1, 192 /* e8, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
1122+
; CHECK-NEXT: $v8 = PseudoVMV_S_X undef renamable $v8, $x0, 1, 3 /* e8 */, implicit $vl, implicit $vtype
1123+
; CHECK-NEXT: dead $x0 = PseudoVSETVLI [[COPY]], 192 /* e8, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
1124+
; CHECK-NEXT: $v8 = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, $noreg, 3 /* e8 */, 3 /* ta, ma */, implicit $vl, implicit $vtype
1125+
bb.0:
1126+
liveins: $x10
1127+
%avl:gprnox0 = COPY $x10
1128+
1129+
bb.1:
1130+
%vl:gprnox0 = PseudoVSETVLI %avl:gprnox0, 192, implicit-def dead $vl, implicit-def dead $vtype
1131+
%avl:gprnox0 = ADDI %avl:gprnox0, -1
1132+
$v8 = PseudoVMV_S_X undef renamable $v8, $x0, 1, 3
1133+
$v8 = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, %vl:gprnox0, 3, 3

0 commit comments

Comments
 (0)