<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/97026>97026</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[RISCV] Ran out of register after enable post-ra vsetvl insertion pass
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:RISC-V,
llvm:regalloc
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
BeMg
</td>
</tr>
</table>
<pre>
The following testcase causes the greedy register allocator to run out of registers. This occurs after enabling the post-RA vsetvl insertion pass, but I don't think the primary reason is the vsetvli pass. The post-RA vsetvl insertion pass changes Scheduling and the corresponding LiveIntervals. It seems the greedy register allocator makes suboptimal register decisions based on its heuristic under high register pressure (interference between VRM8, VRN2M4...), leading to a register shortage.
https://godbolt.org/z/vzG79r3jr
```
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64-unknown-linux-gnu"
define i32 @main(<vscale x 8 x i32> %0, <vscale x 8 x i8> %1, <vscale x 8 x i32> %2, <vscale x 8 x i1> %3) #0 {
entry:
%4 = tail call <vscale x 8 x i64> @llvm.riscv.vle.nxv8i64.i64(<vscale x 8 x i64> zeroinitializer, ptr null, i64 0)
%5 = tail call <vscale x 8 x i8> @llvm.riscv.vle.nxv8i8.i64(<vscale x 8 x i8> zeroinitializer, ptr null, i64 0)
call void @llvm.riscv.vse.nxv8i16.i64(<vscale x 8 x i16> zeroinitializer, ptr null, i64 0)
%6 = tail call <vscale x 8 x i32> @llvm.riscv.vxor.mask.nxv8i32.i32.i64(<vscale x 8 x i32> zeroinitializer, <vscale x 8 x i32> zeroinitializer, i32 0, <vscale x 8 x i1> zeroinitializer, i64 0, i64 0)
%tobool.not.i.i.i = icmp eq ptr null, null
br i1 %tobool.not.i.i.i, label %if.then.i.i.i, label %_ZSt13__check_facetISt5ctypeIcEERKT_PS3_.exit.i.i
if.then.i.i.i: ; preds = %entry
unreachable
_ZSt13__check_facetISt5ctypeIcEERKT_PS3_.exit.i.i: ; preds = %entry
%tobool.not.i3.i.i = icmp eq i8 0, 0
br i1 %tobool.not.i3.i.i, label %if.end.i.i.i, label %if.then.i4.i.i
if.then.i4.i.i: ; preds = %_ZSt13__check_facetISt5ctypeIcEERKT_PS3_.exit.i.i
%7 = load i8, ptr null, align 1
br label %_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_.exit
if.end.i.i.i: ; preds = %_ZSt13__check_facetISt5ctypeIcEERKT_PS3_.exit.i.i
store i32 0, ptr null, align 4
br label %_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_.exit
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_.exit: ; preds = %if.end.i.i.i, %if.then.i4.i.i
%call1.i135 = call ptr null(ptr null, i8 0)
%8 = call { <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8> } @llvm.riscv.vloxseg5.mask.nxv8i8.nxv8i16.i64(<vscale x 8 x i8> shufflevector (<vscale x 8 x i8> insertelement (<vscale x 8 x i8> poison, i8 1, i64 0), <vscale x 8 x i8> poison, <vscale x 8 x i32> zeroinitializer), <vscale x 8 x i8> shufflevector (<vscale x 8 x i8> insertelement (<vscale x 8 x i8> poison, i8 1, i64 0), <vscale x 8 x i8> poison, <vscale x 8 x i32> zeroinitializer), <vscale x 8 x i8> shufflevector (<vscale x 8 x i8> insertelement (<vscale x 8 x i8> poison, i8 1, i64 0), <vscale x 8 x i8> poison, <vscale x 8 x i32> zeroinitializer), <vscale x 8 x i8> shufflevector (<vscale x 8 x i8> insertelement (<vscale x 8 x i8> poison, i8 1, i64 0), <vscale x 8 x i8> poison, <vscale x 8 x i32> zeroinitializer), <vscale x 8 x i8> %1, ptr null, <vscale x 8 x i16> zeroinitializer, <vscale x 8 x i1> zeroinitializer, i64 0, i64 0)
%9 = extractvalue { <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8> } %8, 0
call void @llvm.riscv.vsseg2.nxv8i8.i64(<vscale x 8 x i8> %9, <vscale x 8 x i8> zeroinitializer, ptr null, i64 0)
call void @llvm.riscv.vsseg3.nxv8i8.i64(<vscale x 8 x i8> zeroinitializer, <vscale x 8 x i8> shufflevector (<vscale x 8 x i8> insertelement (<vscale x 8 x i8> poison, i8 1, i64 0), <vscale x 8 x i8> poison, <vscale x 8 x i32> zeroinitializer), <vscale x 8 x i8> zeroinitializer, ptr null, i64 0)
%10 = call <vscale x 8 x i32> @llvm.riscv.vnsra.mask.nxv8i32.nxv8i64.nxv8i32.i64(<vscale x 8 x i32> %6, <vscale x 8 x i64> %4, <vscale x 8 x i32> %2, <vscale x 8 x i1> %3, i64 0, i64 0)
%11 = call <vscale x 8 x i32> @llvm.riscv.vsmul.mask.nxv8i32.nxv8i32.i64(<vscale x 8 x i32> zeroinitializer, <vscale x 8 x i32> zeroinitializer, <vscale x 8 x i32> zeroinitializer, <vscale x 8 x i1> zeroinitializer, i64 0, i64 0, i64 0)
call void @llvm.riscv.vsseg3.nxv8i8.i64(<vscale x 8 x i8> %5, <vscale x 8 x i8> %1, <vscale x 8 x i8> zeroinitializer, ptr null, i64 0)
br label %if.end66
if.end66: ; preds = %_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_.exit
%call67 = call i1 null(i64 0, i64 0)
br label %if.end71
if.end71: ; preds = %if.end66
call void @llvm.riscv.vsseg2.nxv8i8.i64(<vscale x 8 x i8> zeroinitializer, <vscale x 8 x i8> zeroinitializer, ptr null, i64 0)
call void @llvm.riscv.vsseg2.nxv8i32.i64(<vscale x 8 x i32> %10, <vscale x 8 x i32> %0, ptr null, i64 0)
call void @llvm.riscv.vsseg2.nxv8i32.i64(<vscale x 8 x i32> %11, <vscale x 8 x i32> zeroinitializer, ptr null, i64 0)
ret i32 0
}
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: read)
declare <vscale x 8 x i8> @llvm.riscv.vle.nxv8i8.i64(<vscale x 8 x i8>, ptr nocapture, i64) #1
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: read)
declare <vscale x 8 x i64> @llvm.riscv.vle.nxv8i64.i64(<vscale x 8 x i64>, ptr nocapture, i64) #1
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.riscv.vse.nxv8i16.i64(<vscale x 8 x i16>, ptr nocapture, i64) #2
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare <vscale x 8 x i32> @llvm.riscv.vxor.mask.nxv8i32.i32.i64(<vscale x 8 x i32>, <vscale x 8 x i32>, i32, <vscale x 8 x i1>, i64, i64 immarg) #3
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(read)
declare { <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8> } @llvm.riscv.vloxseg5.mask.nxv8i8.nxv8i16.i64(<vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, ptr nocapture, <vscale x 8 x i16>, <vscale x 8 x i1>, i64, i64 immarg) #4
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.riscv.vsseg2.nxv8i8.i64(<vscale x 8 x i8>, <vscale x 8 x i8>, ptr nocapture, i64) #2
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.riscv.vsseg3.nxv8i8.i64(<vscale x 8 x i8>, <vscale x 8 x i8>, <vscale x 8 x i8>, ptr nocapture, i64) #2
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare <vscale x 8 x i32> @llvm.riscv.vnsra.mask.nxv8i32.nxv8i64.nxv8i32.i64(<vscale x 8 x i32>, <vscale x 8 x i64>, <vscale x 8 x i32>, <vscale x 8 x i1>, i64, i64 immarg) #3
; Function Attrs: nounwind memory(none)
declare <vscale x 8 x i32> @llvm.riscv.vsmul.mask.nxv8i32.nxv8i32.i64(<vscale x 8 x i32>, <vscale x 8 x i32>, <vscale x 8 x i32>, <vscale x 8 x i1>, i64 immarg, i64, i64 immarg) #5
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.riscv.vsseg2.nxv8i32.i64(<vscale x 8 x i32>, <vscale x 8 x i32>, ptr nocapture, i64) #2
; uselistorder directives
uselistorder i64 0, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 15, 16, 17, 18, 19 }
uselistorder ptr @llvm.riscv.vsseg2.nxv8i8.i64, { 1, 0 }
uselistorder ptr @llvm.riscv.vsseg3.nxv8i8.i64, { 1, 0 }
uselistorder ptr @llvm.riscv.vsseg2.nxv8i32.i64, { 1, 0 }
attributes #0 = { "target-features"="+64bit,+a,+c,+d,+experimental,+experimental-zvfbfmin,+f,+m,+relax,+zba,+zbb,+zfh,+zfhmin,+zicsr,+zifencei,+zve32f,+zve32x,+zve64f,+zve64x,+zvfh,+zvfhmin,+zvl128b,+zvl256b,+zvl32b,+zvl64b" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: read) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #4 = { nocallback nofree nosync nounwind willreturn memory(read) }
attributes #5 = { nounwind memory(none) }
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWl9v6yoS_zT0BcWKseM4D3lIT09X1e5Zrdqj87AvEcZjm1sMWcBp2k-_Aqf508RJ2qTnVrq3f2xsPMP8fsAwg6DG8FICjNHgGg1urmhjK6XH1_CjvMpU_jz-WQEulBDqicsSWzCWUQOY0caAwbYCXGqA_BlrKLmxoDEVQjFqlcZWYd1IrBqLVbH6wAT4Z8UNVow12mBaOCGQNBO-hQrwTBnbu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