[llvm] [RISCV] Set DebugLoc of epilogue (PR #74702)

Wang Pengcheng via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 7 00:13:58 PST 2023


https://github.com/wangpc-pp created https://github.com/llvm/llvm-project/pull/74702

There is a problem I found when discussing at
https://discourse.llvm.org/t/llvm-generates-wrong-dwarf-line-table-to-gdb/75454/10:
if we set a breakpoint to the end of a function, the debugger will
stop at the place after restoring registers, not before it, which
is a wrong behavior I think.

There are two problems here actually:

1. Instructions for restoring registers don't have debug line info
  as we just set it to `DebugLoc()`.
2. We can't recognize the right epilogue beginning `epilogue_begin`.
  This is a feature introduced by https://reviews.llvm.org/D133376.
  In short, the epilogue beginning will be the first instruction
  with flag `FrameDestroy`. The problem for RISCV target is that
  we only set `FrameDestroy` flag for stack pointer recovering
  instructions(IIUC).

This PR fixes the first problem and the fix is copied from MIPS target.

As for second problem, I don't have a fix now and I think it may
be hard to fix, so I will leave it there.

I don't know how to test this, and there is no test coverage in tree,
any suggestions on how to test this are welcome.


>From 13951a9c8fbd5fe31c929e82afb73f0be75d1cd0 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Thu, 7 Dec 2023 15:50:00 +0800
Subject: [PATCH] [RISCV] Set DebugLoc of epilogue

There is a problem I found when discussing at
https://discourse.llvm.org/t/llvm-generates-wrong-dwarf-line-table-to-gdb/75454/10:
if we set a breakpoint to the end of a function, the debugger will
stop at the place after restoring registers, not before it, which
is a wrong behavior I think.

There are two problems here actually:

1. Instructions for restoring registers don't have debug line info
  as we just set it to `DebugLoc()`.
2. We can't recognize the right epilogue beginning `epilogue_begin`.
  This is a feature introduced by https://reviews.llvm.org/D133376.
  In short, the epilogue beginning will be the first instruction
  with flag `FrameDestroy`. The problem for RISCV target is that
  we only set `FrameDestroy` flag for stack pointer recovering
  instructions(IIUC).

This PR fixes the first problem and the fix is copied from MIPS target.

As for second problem, I don't have a fix now and I think it may
be hard to fix, so I will leave it there.

I don't know how to test this, and there is no test coverage in tree,
any suggestions on how to test this are welcome.
---
 llvm/lib/Target/RISCV/RISCVInstrInfo.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
index 1dcff7eb563e2..6169e6b8e1fa4 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
@@ -681,6 +681,10 @@ void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
                                           const TargetRegisterClass *RC,
                                           const TargetRegisterInfo *TRI,
                                           Register VReg) const {
+  DebugLoc DL;
+  if (I != MBB.end())
+    DL = I->getDebugLoc();
+
   MachineFunction *MF = MBB.getParent();
   MachineFrameInfo &MFI = MF->getFrameInfo();
 
@@ -741,7 +745,7 @@ void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
         MemoryLocation::UnknownSize, MFI.getObjectAlign(FI));
 
     MFI.setStackID(FI, TargetStackID::ScalableVector);
-    BuildMI(MBB, I, DebugLoc(), get(Opcode), DstReg)
+    BuildMI(MBB, I, DL, get(Opcode), DstReg)
         .addFrameIndex(FI)
         .addMemOperand(MMO);
   } else {
@@ -749,7 +753,7 @@ void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
         MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
         MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
 
-    BuildMI(MBB, I, DebugLoc(), get(Opcode), DstReg)
+    BuildMI(MBB, I, DL, get(Opcode), DstReg)
         .addFrameIndex(FI)
         .addImm(0)
         .addMemOperand(MMO);



More information about the llvm-commits mailing list