Issue 244773
Summary [ARM][Thumb-2][PBQP] Crash due to ARMLoadStoreOpt::findFreeReg clobbering reserved BasePtr (r6)
Labels new issue
Assignees
Reporter alinas
    Root cause: #222450 (`9fcdae44a646`).

#226523 (`bd51cc5c5556`) fixed 3 of the 4 reg allocs, RegAllocPBQP was not updated.

Reproduces with `llc -mtriple=thumbv7-unknown-linux-gnueabihf -O2 -regalloc=pbqp repro.ll -o -` on

```
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "thumbv7-unknown-linux-gnueabihf"

declare void @escape(ptr, ptr)
declare i32 @bar(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)

define i32 @foo(i32 %n, i32 %cond, ptr readonly %p, i32 %c0) nounwind "frame-pointer"="all" {
entry:
  %buf = alloca [80 x i32], align 4
 %sz = shl i32 %n, 2
  %dyn = alloca i8, i32 %sz, align 8
  store i32 %c0, ptr %buf, align 4
  call void @escape(ptr nonnull %buf, ptr nonnull %dyn)
 %p10 = getelementptr inbounds i8, ptr %p, i32 40
  %c1 = load i32, ptr %p10, align 4
  %p11 = getelementptr inbounds i8, ptr %p, i32 44
  %c2 = load i32, ptr %p11, align 4
  %p12 = getelementptr inbounds i8, ptr %p, i32 48
 %c3 = load i32, ptr %p12, align 4
  %p13 = getelementptr inbounds i8, ptr %p, i32 52
  %c4 = load i32, ptr %p13, align 4
  %cmp = icmp eq i32 %cond, 0
  br i1 %cmp, label %exit, label %then

then:
  %x0 = load i32, ptr %p, align 4
  %p1 = getelementptr inbounds i8, ptr %p, i32 4
  %x1 = load i32, ptr %p1, align 4
  %p2 = getelementptr inbounds i8, ptr %p, i32 8
  %x2 = load i32, ptr %p2, align 4
  %p3 = getelementptr inbounds i8, ptr %p, i32 12
  %x3 = load i32, ptr %p3, align 4
  %p4 = getelementptr inbounds i8, ptr %p, i32 16
  %y0 = load i32, ptr %p4, align 4
  %p5 = getelementptr inbounds i8, ptr %p, i32 20
  %y1 = load i32, ptr %p5, align 4
  %call = call i32 @bar(i32 %c0, i32 %c1, i32 %y0, i32 %y1, i32 %c2, i32 %c3, i32 %x2, i32 %x1, i32 %x0, i32 %x3)
  br label %exit

exit:
  %ret = phi i32 [ %call, %then ], [ 0, %entry ]
  %b0 = load i32, ptr %buf, align 4
  %p14 = getelementptr inbounds i8, ptr %p, i32 56
  %c5 = load i32, ptr %p14, align 4
  %s0 = add i32 %c1, %c0
  %s1 = add i32 %s0, %c2
  %s2 = add i32 %s1, %c3
  %s3 = add i32 %s2, %c4
  %s4 = add i32 %s3, %ret
  %s5 = add i32 %s4, %b0
  %s6 = add i32 %s5, %c5
  ret i32 %s6
}
```

Additional info: When targeting 32-bit ARM Thumb-2 with `-mllvm -regalloc=pbqp` (`-O2`), `ARMLoadStoreOpt::findFreeReg` can allocate the reserved Thumb-2 base pointer `r6` (`BasePtr`) as a scratch base register for `stm.w` in functions with both a dynamic `alloca` and a local frame `>= 128` bytes. Overwriting `r6` corrupts subsequent `[r6, #imm]` stack/spill accesses.


AI suggested fixes to resolve:
1. Update `RegAllocPBQP` (`llvm/lib/CodeGen/RegAllocPBQP.cpp`) to require `MachineRegisterClassInfoWrapperPass` and call `RCI.updateReservedRegs(MF.getRegInfo().getReservedRegs())` immediately after `MF.getRegInfo().freezeReservedRegs()`, matching `RegAllocBase::init` and `RegAllocFast` (#226523).
2. Add `assert(RCI->hasCurrentReservedRegs() && "RegisterClassInfo misses a change to the reserved registers");` in `ARMLoadStoreOpt::runOnMachineFunction`, matching `AMDGPURewriteAGPRCopyMFMA` (#237446).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to