Skip to content

Commit ad4a582

Browse files
authored
[llvm] Consistently respect naked fn attribute in TargetFrameLowering::hasFP() (#106014)
Some targets (e.g. PPC and Hexagon) already did this. I think it's best to do this consistently so that frontend authors don't run into inconsistent results when they emit `naked` functions. For example, in Zig, we had to change our emit code to also set `frame-pointer=none` to get reliable results across targets. Note: I don't have commit access.
1 parent b497010 commit ad4a582

File tree

72 files changed

+1085
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1085
-90
lines changed

llvm/include/llvm/CodeGen/TargetFrameLowering.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ class TargetFrameLowering {
280280
/// hasFP - Return true if the specified function should have a dedicated
281281
/// frame pointer register. For most targets this is true only if the function
282282
/// has variable sized allocas or if frame pointer elimination is disabled.
283-
virtual bool hasFP(const MachineFunction &MF) const = 0;
283+
/// For all targets, this is false if the function has the naked attribute
284+
/// since there is no prologue to set up the frame pointer.
285+
bool hasFP(const MachineFunction &MF) const {
286+
return !MF.getFunction().hasFnAttribute(Attribute::Naked) && hasFPImpl(MF);
287+
}
284288

285289
/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
286290
/// not required, we reserve argument space for call sites in the function
@@ -477,6 +481,9 @@ class TargetFrameLowering {
477481
/// targets can emit remarks based on the final frame layout.
478482
virtual void emitRemarks(const MachineFunction &MF,
479483
MachineOptimizationRemarkEmitter *ORE) const {};
484+
485+
protected:
486+
virtual bool hasFPImpl(const MachineFunction &MF) const = 0;
480487
};
481488

482489
} // End llvm namespace

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,9 @@ bool AArch64FrameLowering::canUseRedZone(const MachineFunction &MF) const {
480480
getSVEStackSize(MF) || LowerQRegCopyThroughMem);
481481
}
482482

483-
/// hasFP - Return true if the specified function should have a dedicated frame
484-
/// pointer register.
485-
bool AArch64FrameLowering::hasFP(const MachineFunction &MF) const {
483+
/// hasFPImpl - Return true if the specified function should have a dedicated
484+
/// frame pointer register.
485+
bool AArch64FrameLowering::hasFPImpl(const MachineFunction &MF) const {
486486
const MachineFrameInfo &MFI = MF.getFrameInfo();
487487
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
488488

llvm/lib/Target/AArch64/AArch64FrameLowering.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class AArch64FrameLowering : public TargetFrameLowering {
6565
/// Can this function use the red zone for local allocations.
6666
bool canUseRedZone(const MachineFunction &MF) const;
6767

68-
bool hasFP(const MachineFunction &MF) const override;
6968
bool hasReservedCallFrame(const MachineFunction &MF) const override;
7069

7170
bool assignCalleeSavedSpillSlots(MachineFunction &MF,
@@ -125,6 +124,9 @@ class AArch64FrameLowering : public TargetFrameLowering {
125124
orderFrameObjects(const MachineFunction &MF,
126125
SmallVectorImpl<int> &ObjectsToAllocate) const override;
127126

127+
protected:
128+
bool hasFPImpl(const MachineFunction &MF) const override;
129+
128130
private:
129131
/// Returns true if a homogeneous prolog or epilog code can be emitted
130132
/// for the size optimization. If so, HOM_Prolog/HOM_Epilog pseudo

llvm/lib/Target/AMDGPU/R600FrameLowering.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ class R600FrameLowering : public AMDGPUFrameLowering {
2727
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
2828
Register &FrameReg) const override;
2929

30-
bool hasFP(const MachineFunction &MF) const override {
31-
return false;
32-
}
30+
protected:
31+
bool hasFPImpl(const MachineFunction &MF) const override { return false; }
3332
};
3433

3534
} // end namespace llvm

llvm/lib/Target/AMDGPU/SIFrameLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ static bool frameTriviallyRequiresSP(const MachineFrameInfo &MFI) {
18051805
// The FP for kernels is always known 0, so we never really need to setup an
18061806
// explicit register for it. However, DisableFramePointerElim will force us to
18071807
// use a register for it.
1808-
bool SIFrameLowering::hasFP(const MachineFunction &MF) const {
1808+
bool SIFrameLowering::hasFPImpl(const MachineFunction &MF) const {
18091809
const MachineFrameInfo &MFI = MF.getFrameInfo();
18101810

18111811
// For entry & chain functions we can use an immediate offset in most cases,

llvm/lib/Target/AMDGPU/SIFrameLowering.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class SIFrameLowering final : public AMDGPUFrameLowering {
6666
MachineBasicBlock &MBB,
6767
MachineBasicBlock::iterator MI) const override;
6868

69+
protected:
70+
bool hasFPImpl(const MachineFunction &MF) const override;
71+
6972
private:
7073
void emitEntryFunctionFlatScratchInit(MachineFunction &MF,
7174
MachineBasicBlock &MBB,
@@ -82,8 +85,6 @@ class SIFrameLowering final : public AMDGPUFrameLowering {
8285
Register ScratchWaveOffsetReg) const;
8386

8487
public:
85-
bool hasFP(const MachineFunction &MF) const override;
86-
8788
bool requiresStackPointerReference(const MachineFunction &MF) const;
8889
};
8990

llvm/lib/Target/ARC/ARCFrameLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ MachineBasicBlock::iterator ARCFrameLowering::eliminateCallFramePseudoInstr(
487487
return MBB.erase(I);
488488
}
489489

490-
bool ARCFrameLowering::hasFP(const MachineFunction &MF) const {
490+
bool ARCFrameLowering::hasFPImpl(const MachineFunction &MF) const {
491491
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
492492
bool HasFP = MF.getTarget().Options.DisableFramePointerElim(MF) ||
493493
MF.getFrameInfo().hasVarSizedObjects() ||

llvm/lib/Target/ARC/ARCFrameLowering.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class ARCFrameLowering : public TargetFrameLowering {
5454
void processFunctionBeforeFrameFinalized(MachineFunction &MF,
5555
RegScavenger *RS) const override;
5656

57-
bool hasFP(const MachineFunction &MF) const override;
58-
5957
MachineBasicBlock::iterator
6058
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
6159
MachineBasicBlock::iterator I) const override;
@@ -64,6 +62,9 @@ class ARCFrameLowering : public TargetFrameLowering {
6462
llvm::MachineFunction &, const llvm::TargetRegisterInfo *,
6563
std::vector<llvm::CalleeSavedInfo> &) const override;
6664

65+
protected:
66+
bool hasFPImpl(const MachineFunction &MF) const override;
67+
6768
private:
6869
void adjustStackToMatchRecords(MachineBasicBlock &MBB,
6970
MachineBasicBlock::iterator MI,

llvm/lib/Target/ARM/ARMFrameLowering.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ bool ARMFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
323323
return true;
324324
}
325325

326-
/// hasFP - Return true if the specified function should have a dedicated frame
327-
/// pointer register. This is true if the function has variable sized allocas
328-
/// or if frame pointer elimination is disabled.
329-
bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
326+
/// hasFPImpl - Return true if the specified function should have a dedicated
327+
/// frame pointer register. This is true if the function has variable sized
328+
/// allocas or if frame pointer elimination is disabled.
329+
bool ARMFrameLowering::hasFPImpl(const MachineFunction &MF) const {
330330
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
331331
const MachineFrameInfo &MFI = MF.getFrameInfo();
332332

llvm/lib/Target/ARM/ARMFrameLowering.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class ARMFrameLowering : public TargetFrameLowering {
4545

4646
bool enableCalleeSaveSkip(const MachineFunction &MF) const override;
4747

48-
bool hasFP(const MachineFunction &MF) const override;
4948
bool isFPReserved(const MachineFunction &MF) const;
5049
bool requiresAAPCSFrameRecord(const MachineFunction &MF) const;
5150
bool hasReservedCallFrame(const MachineFunction &MF) const override;
@@ -87,6 +86,9 @@ class ARMFrameLowering : public TargetFrameLowering {
8786
const SpillSlot *
8887
getCalleeSavedSpillSlots(unsigned &NumEntries) const override;
8988

89+
protected:
90+
bool hasFPImpl(const MachineFunction &MF) const override;
91+
9092
private:
9193
void emitPushInst(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
9294
ArrayRef<CalleeSavedInfo> CSI, unsigned StmOpc,

0 commit comments

Comments
 (0)