Skip to content

Commit f2ed002

Browse files
authored
[RISCV] Fix RISCVInsertVSETVLI coalescing clobbering VL def segment (#167712)
This fixes an assert when compiling llvm-test-suite with -march=rva23u64 -O3 that started appearing sometime this week. We get "Cannot overlap two segments with differing ValID's" because we try to coalescse these two vsetvlis: %x:gprnox0 = COPY $x8 dead $x0 = PseudoVSETIVLI 1, 208, implicit-def $vl, implicit-def $vtype %y:gprnox0 = COPY %x %v:vr = COPY $v8, implicit $vtype %x = PseudoVSETVLI %x, 208, implicit-def $vl, implicit-def $vtype --> %x:gprnox0 = COPY $x8 %x = PseudoVSETVLI %x, 208, implicit-def $vl, implicit-def $vtype %y:gprnox0 = COPY %x %v:vr = COPY $v8, implicit $vtype However to do so would cause us to extend the segment of the new value of %x up past the first segment, which overlaps. This fixes it by checking that its safe to extend the segment, by simply making sure the interval isn't live at the first vsetvli. This unfortunately causes a regression in the existing coalesce_vl_avl_same_reg test because even though we could coalesce the vsetvlis there, we now bail. I couldn't think of an easy way to handle this safely, but I don't think this is an important case to handle: After testing this patch on SPEC CPU 2017 there are no codegen changes.
1 parent 70eb4b0 commit f2ed002

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,14 @@ bool RISCVInsertVSETVLI::canMutatePriorConfig(
17551755
if (!VNI || !PrevVNI || VNI != PrevVNI)
17561756
return false;
17571757
}
1758+
1759+
// If we define VL and need to move the definition up, check we can extend
1760+
// the live interval upwards from MI to PrevMI.
1761+
Register VL = MI.getOperand(0).getReg();
1762+
if (VL.isVirtual() && LIS &&
1763+
LIS->getInterval(VL).overlaps(LIS->getInstructionIndex(PrevMI),
1764+
LIS->getInstructionIndex(MI)))
1765+
return false;
17581766
}
17591767

17601768
assert(PrevMI.getOperand(2).isImm() && MI.getOperand(2).isImm());

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,46 @@ entry:
879879
%3 = tail call { <vscale x 8 x i8>, i64 } @llvm.riscv.vleff(<vscale x 8 x i8> poison, ptr %p, i64 %2)
880880
ret void
881881
}
882+
883+
; This will create a live interval in such a way we can't coalesce two vsetvlis,
884+
; see the corresponding .mir test for more details. Make sure we check for this
885+
; and don't crash.
886+
define void @coalesce_vl_clobber(ptr %p) {
887+
; CHECK-LABEL: coalesce_vl_clobber:
888+
; CHECK: # %bb.0: # %entry
889+
; CHECK-NEXT: li a2, 0
890+
; CHECK-NEXT: li a1, 0
891+
; CHECK-NEXT: vsetivli zero, 0, e8, mf2, ta, ma
892+
; CHECK-NEXT: vmclr.m v8
893+
; CHECK-NEXT: vmv.v.i v9, 0
894+
; CHECK-NEXT: vmv1r.v v0, v8
895+
; CHECK-NEXT: vmerge.vim v9, v9, 1, v0
896+
; CHECK-NEXT: .LBB43_1: # %vector.body
897+
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
898+
; CHECK-NEXT: vsetivli zero, 1, e8, m1, ta, ma
899+
; CHECK-NEXT: vmv1r.v v0, v8
900+
; CHECK-NEXT: slli a3, a1, 32
901+
; CHECK-NEXT: vsetvli a1, a2, e8, mf8, ta, ma
902+
; CHECK-NEXT: vsetivli zero, 0, e8, mf2, ta, mu
903+
; CHECK-NEXT: vmv.v.i v10, 0
904+
; CHECK-NEXT: srli a3, a3, 32
905+
; CHECK-NEXT: vmerge.vim v10, v10, 1, v0
906+
; CHECK-NEXT: vslideup.vx v10, v9, a3, v0.t
907+
; CHECK-NEXT: vsetvli zero, zero, e8, mf2, ta, ma
908+
; CHECK-NEXT: vmsne.vi v0, v10, 0, v0.t
909+
; CHECK-NEXT: vsetvli zero, a1, e32, m2, ta, ma
910+
; CHECK-NEXT: vmv.v.i v10, 0
911+
; CHECK-NEXT: vse32.v v10, (a0), v0.t
912+
; CHECK-NEXT: li a2, 1
913+
; CHECK-NEXT: j .LBB43_1
914+
entry:
915+
br label %vector.body
916+
917+
vector.body:
918+
%avl = phi i64 [ 0, %entry ], [ 1, %vector.body ]
919+
%prev.evl = phi i32 [ 0, %entry ], [ %0, %vector.body ]
920+
%0 = tail call i32 @llvm.experimental.get.vector.length(i64 %avl, i32 1, i1 true)
921+
%1 = tail call <vscale x 4 x i1> @llvm.experimental.vp.splice(<vscale x 4 x i1> zeroinitializer, <vscale x 4 x i1> zeroinitializer, i32 0, <vscale x 4 x i1> zeroinitializer, i32 %prev.evl, i32 0)
922+
tail call void @llvm.vp.store(<vscale x 4 x float> zeroinitializer, ptr %p, <vscale x 4 x i1> %1, i32 %0)
923+
br label %vector.body
924+
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@
100100
ret void
101101
}
102102

103+
define void @coalesce_vl_clobber() {
104+
ret void
105+
}
106+
103107
define void @vsetvli_vleff() {
104108
ret void
105109
}
@@ -624,10 +628,33 @@ body: |
624628
; CHECK: liveins: $x8, $v8
625629
; CHECK-NEXT: {{ $}}
626630
; CHECK-NEXT: %x:gprnox0 = COPY $x8
631+
; CHECK-NEXT: dead $x0 = PseudoVSETIVLI 1, 208 /* e32, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
632+
; CHECK-NEXT: dead %v:vr = COPY $v8, implicit $vtype
627633
; CHECK-NEXT: dead %x:gprnox0 = PseudoVSETVLI %x, 208 /* e32, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
634+
%x:gprnox0 = COPY $x8
635+
dead $x0 = PseudoVSETIVLI 1, 208, implicit-def $vl, implicit-def $vtype
636+
%v:vr = COPY $v8, implicit $vtype
637+
%x = PseudoVSETVLI %x, 208, implicit-def $vl, implicit-def $vtype
638+
...
639+
---
640+
# Because of the %y:gprnox0 = COPY %x, we can't extend the live range of %x from
641+
# the second vsetvli to the first vsetvli when coalescing.
642+
name: coalesce_vl_clobber
643+
tracksRegLiveness: true
644+
body: |
645+
bb.0:
646+
liveins: $x8, $v8
647+
; CHECK-LABEL: name: coalesce_vl_clobber
648+
; CHECK: liveins: $x8, $v8
649+
; CHECK-NEXT: {{ $}}
650+
; CHECK-NEXT: %x:gprnox0 = COPY $x8
651+
; CHECK-NEXT: dead $x0 = PseudoVSETIVLI 1, 208 /* e32, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
652+
; CHECK-NEXT: dead %y:gprnox0 = COPY %x
628653
; CHECK-NEXT: dead %v:vr = COPY $v8, implicit $vtype
654+
; CHECK-NEXT: dead %x:gprnox0 = PseudoVSETVLI %x, 208 /* e32, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
629655
%x:gprnox0 = COPY $x8
630656
dead $x0 = PseudoVSETIVLI 1, 208, implicit-def $vl, implicit-def $vtype
657+
%y:gprnox0 = COPY %x
631658
%v:vr = COPY $v8, implicit $vtype
632659
%x = PseudoVSETVLI %x, 208, implicit-def $vl, implicit-def $vtype
633660
...

0 commit comments

Comments
 (0)