[PATCH] D126861: [RISCV] Fix missing stack pointer recover

Kito Cheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 2 02:12:03 PDT 2022


kito-cheng created this revision.
Herald added subscribers: sunshaoce, VincentWu, luke957, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, hiraditya, arichardson.
Herald added a project: All.
kito-cheng requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

In order to make sure the stack point is right through the EH region,
we also need to restore stack pointer from the frame pointer if we
don't preserve stack space within prologue/epilogue for outgoing variables,
normally it's just checking the variable sized object is present or not
is enough, but we also don't preserve that at prologue/epilogue when
have vector objects in stack.

Example to show what happened:

  try {
    sp adjust for outgoing args. // 1. Sp changed.
    func_call  // 2. Exception raised
    sp restore // Oh, not restored
  } catch {
    // 3. And now we are here.
  }
  
  // 4. Prepare to return!, restore return address from stack, but...sp is wrong.
  // 5. Screw up!


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126861

Files:
  llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
  llvm/test/CodeGen/RISCV/miss-sp-restore-eh.ll


Index: llvm/test/CodeGen/RISCV/miss-sp-restore-eh.ll
===================================================================
--- llvm/test/CodeGen/RISCV/miss-sp-restore-eh.ll
+++ llvm/test/CodeGen/RISCV/miss-sp-restore-eh.ll
@@ -48,6 +48,7 @@
 ; CHECK-NEXT:    lw s1, 0(a0)
 ; CHECK-NEXT:    call __cxa_end_catch at plt
 ; CHECK-NEXT:    mv a0, s1
+; CHECK-NEXT:    addi sp, s0, -32
 ; CHECK-NEXT:    ld ra, 24(sp) # 8-byte Folded Reload
 ; CHECK-NEXT:    ld s0, 16(sp) # 8-byte Folded Reload
 ; CHECK-NEXT:    ld s1, 8(sp) # 8-byte Folded Reload
Index: llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
@@ -637,7 +637,15 @@
   // Restore the stack pointer using the value of the frame pointer. Only
   // necessary if the stack pointer was modified, meaning the stack size is
   // unknown.
-  if (RI->hasStackRealignment(MF) || MFI.hasVarSizedObjects()) {
+  //
+  // In order to make sure the stack point is right through the EH region,
+  // we also need to restore stack pointer from the frame pointer if we
+  // don't preserve stack space within prologue/epilogue for outgoing variables,
+  // normally it's just checking the variable sized object is present or not
+  // is enough, but we also don't preserve that at prologue/epilogue when
+  // have vector objects in stack.
+  if (RI->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
+      !hasReservedCallFrame(MF)) {
     assert(hasFP(MF) && "frame pointer should not have been eliminated");
     adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg, -FPOffset,
               MachineInstr::FrameDestroy);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126861.433688.patch
Type: text/x-patch
Size: 1726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220602/eacd7349/attachment.bin>


More information about the llvm-commits mailing list