Skip to content

Commit db782b4

Browse files
authored
[RISCV] Don't forward AVL in VSETVLIInfo if it would clobber other definitions (llvm#97264)
This fixes a crash found when compiling OpenBLAS with -mllvm -verify-machineinstrs. When we "forward" the AVL from the output of a vsetvli, we might have to extend the LiveInterval of the AVL to where insert the new vsetvli. Most of the time we are able to extend the LiveInterval because there's only one val num (definition) for the register. But PHI elimination can assign multiple values to the same register, in which case we end up clobbering a different val num when extending: %x = PseudoVSETVLI %avl, ... %avl = ADDI ... %v = PseudoVADD ..., avl=%x ; %avl is forwarded to PseudoVADD: %x = PseudoVSETVLI %avl, ... %avl = ADDI ... %v = PseudoVADD ..., avl=%avl Here there's no way to extend the %avl from the vsetvli since %avl is redefined, i.e. we have two val nums. This fixes it by only forwarding it when we have exactly one val num, where it should be safe to extend it.
1 parent 23aff11 commit db782b4

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

Diff for: llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,12 @@ void RISCVInsertVSETVLI::forwardVSETVLIAVL(VSETVLIInfo &Info) const {
956956
VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI);
957957
if (!DefInstrInfo.hasSameVLMAX(Info))
958958
return;
959+
// If the AVL is a register with multiple definitions, don't forward it. We
960+
// might not be able to extend its LiveInterval without clobbering other val
961+
// nums.
962+
if (DefInstrInfo.hasAVLReg() &&
963+
!LIS->getInterval(DefInstrInfo.getAVLReg()).containsOneValue())
964+
return;
959965
Info.setAVL(DefInstrInfo);
960966
}
961967

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

+36
Original file line numberDiff line numberDiff line change
@@ -1125,3 +1125,39 @@ exit:
11251125
call void @llvm.riscv.vse.nxv8i8(<vscale x 8 x i8> %1, ptr %p, i64 1)
11261126
ret void
11271127
}
1128+
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:
1133+
; CHECK: # %bb.0: # %entry
1134+
; CHECK-NEXT: vsetvli a2, a0, e32, m2, ta, ma
1135+
; CHECK-NEXT: andi a1, a1, 1
1136+
; CHECK-NEXT: .LBB27_1: # %for.body
1137+
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
1138+
; CHECK-NEXT: addi a0, a0, 1
1139+
; CHECK-NEXT: bnez a1, .LBB27_1
1140+
; CHECK-NEXT: # %bb.2: # %for.cond.cleanup
1141+
; CHECK-NEXT: vsetvli a0, zero, e32, m2, ta, ma
1142+
; CHECK-NEXT: vadd.vv v10, v8, v8
1143+
; CHECK-NEXT: vsetvli zero, a2, e32, m2, ta, ma
1144+
; CHECK-NEXT: vadd.vv v8, v10, v8
1145+
; CHECK-NEXT: ret
1146+
entry:
1147+
%0 = tail call i64 @llvm.riscv.vsetvli.i64(i64 %n, i64 2, i64 1)
1148+
br label %for.body
1149+
1150+
for.body:
1151+
; Use %n in a PHI here so its virtual register is assigned to a second time here.
1152+
%1 = phi i64 [ %3, %for.body ], [ %n, %entry ]
1153+
%2 = tail call i64 @llvm.riscv.vsetvli.i64(i64 %1, i64 0, i64 0)
1154+
%3 = add i64 %1, 1
1155+
br i1 %cmp, label %for.body, label %for.cond.cleanup
1156+
1157+
for.cond.cleanup:
1158+
%4 = tail call <vscale x 4 x i32> @llvm.riscv.vadd.nxv2f32.nxv2f32.i64(<vscale x 4 x i32> undef, <vscale x 4 x i32> %v, <vscale x 4 x i32> %v, i64 -1)
1159+
; VL toggle needed here: If the %n AVL was forwarded here we wouldn't be able
1160+
; to extend it's LiveInterval because it would clobber the assignment at %1.
1161+
%5 = tail call <vscale x 4 x i32> @llvm.riscv.vadd.nxv2f32.nxv2f32.i64(<vscale x 4 x i32> undef, <vscale x 4 x i32> %4, <vscale x 4 x i32> %v, i64 %0)
1162+
ret <vscale x 4 x i32> %5
1163+
}

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

+44
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@
134134
ret void
135135
}
136136

137+
define void @unforwardable_avl() {
138+
ret void
139+
}
140+
137141
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>)
138142

139143
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
@@ -990,3 +994,43 @@ body: |
990994
%x:gpr = PseudoVMV_X_S undef $noreg, 6
991995
PseudoBR %bb.1
992996
...
997+
---
998+
name: unforwardable_avl
999+
tracksRegLiveness: true
1000+
body: |
1001+
; CHECK-LABEL: name: unforwardable_avl
1002+
; CHECK: bb.0:
1003+
; CHECK-NEXT: successors: %bb.1(0x80000000)
1004+
; CHECK-NEXT: liveins: $x10, $v8m2
1005+
; CHECK-NEXT: {{ $}}
1006+
; CHECK-NEXT: %avl:gprnox0 = COPY $x10
1007+
; CHECK-NEXT: %outvl:gprnox0 = PseudoVSETVLI %avl, 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
1008+
; CHECK-NEXT: {{ $}}
1009+
; CHECK-NEXT: bb.1:
1010+
; CHECK-NEXT: successors: %bb.2(0x80000000)
1011+
; CHECK-NEXT: liveins: $v8m2
1012+
; CHECK-NEXT: {{ $}}
1013+
; CHECK-NEXT: dead %avl:gprnox0 = ADDI %avl, 1
1014+
; CHECK-NEXT: {{ $}}
1015+
; CHECK-NEXT: bb.2:
1016+
; CHECK-NEXT: liveins: $v8m2
1017+
; CHECK-NEXT: {{ $}}
1018+
; CHECK-NEXT: dead [[PseudoVSETVLIX0_:%[0-9]+]]:gpr = PseudoVSETVLIX0 killed $x0, 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
1019+
; 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: renamable $v8m2 = PseudoVADD_VV_M2 undef renamable $v8m2, killed renamable $v10m2, renamable $v8m2, $noreg, 5 /* e32 */, 0 /* tu, mu */, implicit $vl, implicit $vtype
1022+
; CHECK-NEXT: PseudoRET implicit $v8m2
1023+
bb.0:
1024+
liveins: $x10, $v8m2
1025+
%avl:gprnox0 = COPY $x10
1026+
%outvl:gprnox0 = PseudoVSETVLI %avl:gprnox0, 209, implicit-def dead $vl, implicit-def dead $vtype
1027+
1028+
bb.1:
1029+
liveins: $v8m2
1030+
%avl:gprnox0 = ADDI %avl:gprnox0, 1
1031+
1032+
bb.2:
1033+
liveins: $v8m2
1034+
renamable $v10m2 = PseudoVADD_VV_M2 undef renamable $v10m2, renamable $v8m2, renamable $v8m2, -1, 5, 0
1035+
renamable $v8m2 = PseudoVADD_VV_M2 undef renamable $v8m2, killed renamable $v10m2, killed renamable $v8m2, %outvl:gprnox0, 5, 0
1036+
PseudoRET implicit $v8m2

Diff for: llvm/test/Transforms/LoopStrengthReduce/RISCV/lsr-drop-solution.ll

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ define ptr @foo(ptr %a0, ptr %a1, i64 %a2) {
2020
; CHECK-NEXT: mv a3, a0
2121
; CHECK-NEXT: .LBB0_3: # %do.body
2222
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
23+
; CHECK-NEXT: vsetvli zero, a4, e8, m8, ta, ma
2324
; CHECK-NEXT: vle8.v v8, (a1)
2425
; CHECK-NEXT: vse8.v v8, (a3)
2526
; CHECK-NEXT: add a3, a3, a4

0 commit comments

Comments
 (0)