Skip to content

Commit d7938b1

Browse files
committed
MachineModuleInfo: Move HasSplitStack handling to AsmPrinter
This is used to emit one field in doFinalization for the module. We can accumulate this when emitting all individual functions directly in the AsmPrinter, rather than accumulating additional state in MachineModuleInfo. Move the special case behavior predicate into MachineFrameInfo to share it. This now promotes it to generic behavior. I'm assuming this is fine because no other target implements adjustForSegmentedStacks, or has tests using the split-stack attribute.
1 parent 8544523 commit d7938b1

File tree

9 files changed

+44
-58
lines changed

9 files changed

+44
-58
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ class AsmPrinter : public MachineFunctionPass {
210210
/// CFISection type the module needs i.e. either .eh_frame or .debug_frame.
211211
CFISection ModuleCFISection = CFISection::None;
212212

213+
/// True if the module contains split-stack functions. This is used to
214+
/// emit .note.GNU-split-stack section as required by the linker for
215+
/// special handling split-stack function calling no-split-stack function.
216+
bool HasSplitStack = false;
217+
218+
/// True if the module contains no-split-stack functions. This is used to emit
219+
/// .note.GNU-no-split-stack section when it also contains functions without a
220+
/// split stack prologue.
221+
bool HasNoSplitStack = false;
222+
213223
protected:
214224
explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
215225

llvm/include/llvm/CodeGen/MachineFrameInfo.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,20 @@ class MachineFrameInfo {
385385
bool hasPatchPoint() const { return HasPatchPoint; }
386386
void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
387387

388+
/// Return true if this function requires a split stack prolog, even if it
389+
/// uses no stack space. This is only meaningful for functions where
390+
/// MachineFunction::shouldSplitStack() returns true.
391+
//
392+
// For non-leaf functions we have to allow for the possibility that the call
393+
// is to a non-split function, as in PR37807. This function could also take
394+
// the address of a non-split function. When the linker tries to adjust its
395+
// non-existent prologue, it would fail with an error. Mark the object file so
396+
// that such failures are not errors. See this Go language bug-report
397+
// https://go-review.googlesource.com/c/go/+/148819/
398+
bool needsSplitStackProlog() const {
399+
return getStackSize() != 0 || hasTailCall();
400+
}
401+
388402
/// Return the minimum frame object index.
389403
int getObjectIndexBegin() const { return -NumFixedObjects; }
390404

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,6 @@ class MachineModuleInfo {
129129
/// comments in lib/Target/X86/X86FrameLowering.cpp for more details.
130130
bool UsesMorestackAddr;
131131

132-
/// True if the module contains split-stack functions. This is used to
133-
/// emit .note.GNU-split-stack section as required by the linker for
134-
/// special handling split-stack function calling no-split-stack function.
135-
bool HasSplitStack;
136-
137-
/// True if the module contains no-split-stack functions. This is used to
138-
/// emit .note.GNU-no-split-stack section when it also contains split-stack
139-
/// functions.
140-
bool HasNosplitStack;
141-
142132
/// Maps IR Functions to their corresponding MachineFunctions.
143133
DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
144134
/// Next unique number available for a MachineFunction.
@@ -213,22 +203,6 @@ class MachineModuleInfo {
213203
UsesMorestackAddr = b;
214204
}
215205

216-
bool hasSplitStack() const {
217-
return HasSplitStack;
218-
}
219-
220-
void setHasSplitStack(bool b) {
221-
HasSplitStack = b;
222-
}
223-
224-
bool hasNosplitStack() const {
225-
return HasNosplitStack;
226-
}
227-
228-
void setHasNosplitStack(bool b) {
229-
HasNosplitStack = b;
230-
}
231-
232206
/// Return the symbol to be used for the specified basic block when its
233207
/// address is taken. This cannot be its normal LBB label because the block
234208
/// may be accessed outside its containing function.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
257257
bool AsmPrinter::doInitialization(Module &M) {
258258
auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
259259
MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
260+
HasSplitStack = false;
261+
HasNoSplitStack = false;
260262

261263
// Initialize TargetLoweringObjectFile.
262264
const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
@@ -1921,10 +1923,10 @@ bool AsmPrinter::doFinalization(Module &M) {
19211923

19221924
// Emit .note.GNU-split-stack and .note.GNU-no-split-stack sections if
19231925
// split-stack is used.
1924-
if (TM.getTargetTriple().isOSBinFormatELF() && MMI->hasSplitStack()) {
1926+
if (TM.getTargetTriple().isOSBinFormatELF() && HasSplitStack) {
19251927
OutStreamer->SwitchSection(
19261928
OutContext.getELFSection(".note.GNU-split-stack", ELF::SHT_PROGBITS, 0));
1927-
if (MMI->hasNosplitStack())
1929+
if (HasNoSplitStack)
19281930
OutStreamer->SwitchSection(
19291931
OutContext.getELFSection(".note.GNU-no-split-stack", ELF::SHT_PROGBITS, 0));
19301932
}
@@ -1991,6 +1993,16 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
19911993
this->MF = &MF;
19921994
const Function &F = MF.getFunction();
19931995

1996+
// Record that there are split-stack functions, so we will emit a special
1997+
// section to tell the linker.
1998+
if (MF.shouldSplitStack()) {
1999+
HasSplitStack = true;
2000+
2001+
if (!MF.getFrameInfo().needsSplitStackProlog())
2002+
HasNoSplitStack = true;
2003+
} else
2004+
HasNoSplitStack = true;
2005+
19942006
// Get the function symbol.
19952007
if (!MAI->needsFunctionDescriptors()) {
19962008
CurrentFnSym = getSymbol(&MF.getFunction());

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ void MachineModuleInfo::initialize() {
203203
CurCallSite = 0;
204204
NextFnNum = 0;
205205
UsesMSVCFloatingPoint = UsesMorestackAddr = false;
206-
HasSplitStack = HasNosplitStack = false;
207206
AddrLabelSymbols = nullptr;
208207
DbgInfoAvailable = false;
209208
}
@@ -232,8 +231,6 @@ MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
232231
CurCallSite = MMI.CurCallSite;
233232
UsesMSVCFloatingPoint = MMI.UsesMSVCFloatingPoint;
234233
UsesMorestackAddr = MMI.UsesMorestackAddr;
235-
HasSplitStack = MMI.HasSplitStack;
236-
HasNosplitStack = MMI.HasNosplitStack;
237234
AddrLabelSymbols = MMI.AddrLabelSymbols;
238235
ExternalContext = MMI.ExternalContext;
239236
TheModule = MMI.TheModule;

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,11 +1144,7 @@ void PEI::insertPrologEpilogCode(MachineFunction &MF) {
11441144
if (MF.shouldSplitStack()) {
11451145
for (MachineBasicBlock *SaveBlock : SaveBlocks)
11461146
TFI.adjustForSegmentedStacks(MF, *SaveBlock);
1147-
// Record that there are split-stack functions, so we will emit a
1148-
// special section to tell the linker.
1149-
MF.getMMI().setHasSplitStack(true);
1150-
} else
1151-
MF.getMMI().setHasNosplitStack(true);
1147+
}
11521148

11531149
// Emit additional code that is required to explicitly handle the stack in
11541150
// HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The

llvm/lib/Target/ARM/ARMAsmPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/SetVector.h"
2525
#include "llvm/ADT/SmallString.h"
2626
#include "llvm/BinaryFormat/COFF.h"
27+
#include "llvm/CodeGen/MachineFrameInfo.h"
2728
#include "llvm/CodeGen/MachineFunctionPass.h"
2829
#include "llvm/CodeGen/MachineJumpTableInfo.h"
2930
#include "llvm/CodeGen/MachineModuleInfoImpls.h"

llvm/lib/Target/ARM/ARMFrameLowering.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,19 +2506,10 @@ void ARMFrameLowering::adjustForSegmentedStacks(
25062506
ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
25072507
DebugLoc DL;
25082508

2509-
uint64_t StackSize = MFI.getStackSize();
2510-
2511-
// Do not generate a prologue for leaf functions with a stack of size zero.
2512-
// For non-leaf functions we have to allow for the possibility that the
2513-
// callis to a non-split function, as in PR37807. This function could also
2514-
// take the address of a non-split function. When the linker tries to adjust
2515-
// its non-existent prologue, it would fail with an error. Mark the object
2516-
// file so that such failures are not errors. See this Go language bug-report
2517-
// https://go-review.googlesource.com/c/go/+/148819/
2518-
if (StackSize == 0 && !MFI.hasTailCall()) {
2519-
MF.getMMI().setHasNosplitStack(true);
2509+
if (!MFI.needsSplitStackProlog())
25202510
return;
2521-
}
2511+
2512+
uint64_t StackSize = MFI.getStackSize();
25222513

25232514
// Use R4 and R5 as scratch registers.
25242515
// We save R4 and R5 before use and restore them before leaving the function.

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,17 +2944,8 @@ void X86FrameLowering::adjustForSegmentedStacks(
29442944
// prologue.
29452945
StackSize = MFI.getStackSize();
29462946

2947-
// Do not generate a prologue for leaf functions with a stack of size zero.
2948-
// For non-leaf functions we have to allow for the possibility that the
2949-
// callis to a non-split function, as in PR37807. This function could also
2950-
// take the address of a non-split function. When the linker tries to adjust
2951-
// its non-existent prologue, it would fail with an error. Mark the object
2952-
// file so that such failures are not errors. See this Go language bug-report
2953-
// https://go-review.googlesource.com/c/go/+/148819/
2954-
if (StackSize == 0 && !MFI.hasTailCall()) {
2955-
MF.getMMI().setHasNosplitStack(true);
2947+
if (!MFI.needsSplitStackProlog())
29562948
return;
2957-
}
29582949

29592950
MachineBasicBlock *allocMBB = MF.CreateMachineBasicBlock();
29602951
MachineBasicBlock *checkMBB = MF.CreateMachineBasicBlock();

0 commit comments

Comments
 (0)