Skip to content

Commit c017c94

Browse files
committed
[CFIFixup] Fixup CFI for split functions with synchronous uwtables
Commit 6e54fcc disables CFI fixup for functions with synchronous tables, breaking CFI for split functions. Instead, we can disable *block-level* CFI fixup for functions with synchronous tables. Unwind tables can be: - N/A (not present) - Asynchronous - Synchronous Functions without unwind tables don't need CFI fixup (since they don't care about CFI). Functions with asynchronous unwind tables must be accurate for each basic block, so full CFI fixup is necessary. Functions with synchronous unwind tables only need to be accurate for each function (specifically, the portion of a function in a given section). Disabling CFI fixup entirely for functions with synchronous uwtables may break CFI for a function split between two sections. The portion in the first section may have valid CFI, while the portion in the second section is missing a call frame. Ex: ``` (.text.hot) Foo (BB1): <Call frame information> ... BB2: ... (.text.split) BB3: ... BB4: <epilogue> ``` Even if `Foo` has a synchronous unwind table, we still need to insert call frame information into `BB3` so that unwinding the call stack from `BB3` or `BB4` works properly.
1 parent 2e9b586 commit c017c94

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

llvm/include/llvm/CodeGen/TargetFrameLowering.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,16 @@ class TargetFrameLowering {
229229

230230
/// Returns true if we may need to fix the unwind information for the
231231
/// function.
232-
virtual bool enableCFIFixup(MachineFunction &MF) const;
232+
virtual bool enableCFIFixup(const MachineFunction &MF) const;
233+
234+
/// enableBlockLevelCFIFixup - Returns true if we may need to fix up the
235+
/// function at a basic block level (e.g. for async unwind tables).
236+
virtual bool enableBlockLevelCFIFixup(const MachineFunction &MF) const {
237+
return enableCFIFixup(MF);
238+
};
233239

234240
/// Emit CFI instructions that recreate the state of the unwind information
235-
/// upon fucntion entry.
241+
/// upon function entry.
236242
virtual void resetCFIToInitialState(MachineBasicBlock &MBB) const {}
237243

238244
/// Replace a StackProbe stub (if any) with the actual probe code inline

llvm/lib/CodeGen/CFIFixup.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include "llvm/CodeGen/TargetSubtargetInfo.h"
8181
#include "llvm/MC/MCAsmInfo.h"
8282
#include "llvm/MC/MCDwarf.h"
83+
#include "llvm/Support/CodeGen.h"
8384
#include "llvm/Target/TargetMachine.h"
8485

8586
#include <iterator>
@@ -252,6 +253,11 @@ fixupBlock(MachineBasicBlock &CurrBB, const BlockFlagsVector &BlockInfo,
252253
if (!Info.Reachable)
253254
return false;
254255

256+
// If we don't need to fix up CFI at the block level, we only need to fix up
257+
// the first basic block in the section.
258+
if (!TFL.enableBlockLevelCFIFixup(MF) && !CurrBB.isBeginSection())
259+
return false;
260+
255261
// If the previous block and the current block are in the same section,
256262
// the frame info will propagate from the previous block to the current one.
257263
const BlockFlags &PrevInfo =

llvm/lib/CodeGen/TargetFrameLoweringImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const
3535
return false;
3636
}
3737

38-
bool TargetFrameLowering::enableCFIFixup(MachineFunction &MF) const {
38+
bool TargetFrameLowering::enableCFIFixup(const MachineFunction &MF) const {
3939
return MF.needsFrameMoves() &&
4040
!MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
4141
}

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,8 +2612,14 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
26122612
}
26132613
}
26142614

2615-
bool AArch64FrameLowering::enableCFIFixup(MachineFunction &MF) const {
2615+
bool AArch64FrameLowering::enableCFIFixup(const MachineFunction &MF) const {
26162616
return TargetFrameLowering::enableCFIFixup(MF) &&
2617+
MF.getInfo<AArch64FunctionInfo>()->needsDwarfUnwindInfo(MF);
2618+
}
2619+
2620+
bool AArch64FrameLowering::enableBlockLevelCFIFixup(
2621+
const MachineFunction &MF) const {
2622+
return enableCFIFixup(MF) &&
26172623
MF.getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo(MF);
26182624
}
26192625

llvm/lib/Target/AArch64/AArch64FrameLowering.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class AArch64FrameLowering : public TargetFrameLowering {
3636
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
3737
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
3838

39-
bool enableCFIFixup(MachineFunction &MF) const override;
39+
bool enableCFIFixup(const MachineFunction &MF) const override;
40+
41+
bool enableBlockLevelCFIFixup(const MachineFunction &MF) const override;
4042

4143
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
4244

llvm/test/CodeGen/AArch64/cfi-fixup-multi-section.mir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ body: |
285285
; CHECK-NEXT: successors: %bb.5(0x80000000)
286286
; CHECK-NEXT: liveins: $w0
287287
; CHECK-NEXT: {{ $}}
288+
; CHECK-NEXT: frame-setup CFI_INSTRUCTION escape 0x16, 0x12, 0x02, 0x82, 0x78
289+
; CHECK-NEXT: frame-setup CFI_INSTRUCTION negate_ra_sign_state
290+
; CHECK-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 16
291+
; CHECK-NEXT: frame-setup CFI_INSTRUCTION offset $w30, -16
288292
; CHECK-NEXT: renamable $w0 = nsw SUBWri killed renamable $w0, 1, 0
289293
; CHECK-NEXT: BL @g, csr_aarch64_aapcs_scs, implicit-def dead $lr, implicit $sp, implicit killed $w0, implicit-def $sp, implicit-def $w0
290294
; CHECK-NEXT: renamable $w0 = nsw ADDWri killed renamable $w0, 1, 0

0 commit comments

Comments
 (0)