Skip to content

Commit 20f4ef9

Browse files
committed
IRGen: Default to clang's frame pointer elimination settings
Clang provides options to override that default value. These options are accessible via the -Xcc flag. Some Swift functions explicitly disable the frame pointer. The clang options will not override those.
1 parent 456775f commit 20f4ef9

File tree

10 files changed

+57
-77
lines changed

10 files changed

+57
-77
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ class IRGenOptions {
190190
/// Whether we should run LLVM SLP vectorizer.
191191
unsigned DisableLLVMSLPVectorizer : 1;
192192

193-
/// Disable frame pointer elimination?
194-
unsigned DisableFPElimLeaf : 1;
195-
unsigned DisableFPElim : 1;
196-
197193
/// Special codegen for playgrounds.
198194
unsigned Playground : 1;
199195

@@ -320,8 +316,7 @@ class IRGenOptions {
320316
DisableClangModuleSkeletonCUs(false), UseJIT(false),
321317
DisableLLVMOptzns(false),
322318
DisableSwiftSpecificLLVMOptzns(false), DisableLLVMSLPVectorizer(false),
323-
DisableFPElimLeaf(false),
324-
DisableFPElim(true), Playground(false), EmitStackPromotionChecks(false),
319+
Playground(false), EmitStackPromotionChecks(false),
325320
FunctionSections(false), PrintInlineTree(false), EmbedMode(IRGenEmbedMode::None),
326321
HasValueNamesSetting(false), ValueNames(false),
327322
EnableReflectionMetadata(true), EnableReflectionNames(true),

include/swift/Option/Options.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,10 +1010,6 @@ def enable_private_imports : Flag<["-"], "enable-private-imports">,
10101010
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
10111011
HelpText<"Allows this module's internal and private API to be accessed">;
10121012

1013-
def disable_leaf_frame_pointer_elim : Flag<["-"], "no-omit-leaf-frame-pointer">,
1014-
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
1015-
HelpText<"Don't omit the frame pointer for leaf functions">;
1016-
10171013
def sanitize_EQ : CommaJoined<["-"], "sanitize=">,
10181014
Flags<[FrontendOption, NoInteractiveOption]>, MetaVarName<"<check>">,
10191015
HelpText<"Turn on runtime checks for erroneous behavior.">;

lib/Driver/ToolChains.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,6 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
288288
arguments.push_back("-enable-anonymous-context-mangled-names");
289289
}
290290

291-
inputArgs.AddLastArg(arguments, options::OPT_disable_leaf_frame_pointer_elim);
292-
293291
// Pass through any subsystem flags.
294292
inputArgs.AddAllArgs(arguments, options::OPT_Xllvm);
295293
inputArgs.AddAllArgs(arguments, options::OPT_Xcc);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,9 +1514,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
15141514
getRuntimeCompatVersion();
15151515
}
15161516

1517-
if (Args.hasArg(OPT_disable_leaf_frame_pointer_elim))
1518-
Opts.DisableFPElimLeaf = true;
1519-
15201517
return false;
15211518
}
15221519

lib/IRGen/GenMeta.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static void emitMetadataCompletionFunction(IRGenModule &IGM,
177177
IGM.getAddrOfTypeMetadataCompletionFunction(typeDecl, ForDefinition);
178178
f->setAttributes(IGM.constructInitialAttributes());
179179
f->setDoesNotThrow();
180-
IGM.setHasFramePointer(f, false);
180+
IGM.setHasNoFramePointer(f);
181181

182182
IRGenFunction IGF(IGM, f);
183183

@@ -2234,7 +2234,7 @@ namespace {
22342234
IGM.getAddrOfTypeMetadataInstantiationFunction(Target, ForDefinition);
22352235
f->setAttributes(IGM.constructInitialAttributes());
22362236
f->setDoesNotThrow();
2237-
IGM.setHasFramePointer(f, false);
2237+
IGM.setHasNoFramePointer(f);
22382238

22392239
IRGenFunction IGF(IGM, f);
22402240

lib/IRGen/IRGenModule.cpp

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -90,34 +90,19 @@ static llvm::PointerType *createStructPointerType(IRGenModule &IGM,
9090
return createStructType(IGM, name, types)->getPointerTo(DefaultAS);
9191
};
9292

93-
static clang::CodeGenOptions::FramePointerKind
94-
shouldUseFramePointer(const IRGenOptions &Opts, const llvm::Triple &triple) {
95-
if (Opts.DisableFPElim) {
96-
// General frame pointer elimination is disabled.
97-
// Should we at least eliminate in leaf functions?
98-
// Currently we only do that on arm64 (this matches the behavior of clang).
99-
return Opts.DisableFPElimLeaf
100-
? clang::CodeGenOptions::FramePointerKind::All
101-
: triple.isAArch64()
102-
? clang::CodeGenOptions::FramePointerKind::NonLeaf
103-
: clang::CodeGenOptions::FramePointerKind::All;
104-
}
105-
106-
return clang::CodeGenOptions::FramePointerKind::None;
107-
}
108-
109-
static clang::CodeGenerator *
110-
createClangCodeGenerator(ASTContext &Context, llvm::LLVMContext &LLVMContext,
111-
const IRGenOptions &Opts, StringRef ModuleName,
112-
StringRef PD, const llvm::Triple &triple) {
93+
static clang::CodeGenerator *createClangCodeGenerator(ASTContext &Context,
94+
llvm::LLVMContext &LLVMContext,
95+
const IRGenOptions &Opts,
96+
StringRef ModuleName,
97+
StringRef PD) {
11398
auto Loader = Context.getClangModuleLoader();
11499
auto *Importer = static_cast<ClangImporter*>(&*Loader);
115100
assert(Importer && "No clang module loader!");
116101
auto &ClangContext = Importer->getClangASTContext();
117102

118103
auto &CGO = Importer->getClangCodeGenOpts();
119104
CGO.OptimizationLevel = Opts.shouldOptimize() ? 3 : 0;
120-
CGO.setFramePointer(shouldUseFramePointer(Opts, triple));
105+
121106
CGO.DiscardValueNames = !Opts.shouldProvideValueNames();
122107
switch (Opts.DebugInfoLevel) {
123108
case IRGenDebugInfoLevel::None:
@@ -207,17 +192,17 @@ static void sanityCheckStdlib(IRGenModule &IGM) {
207192

208193
IRGenModule::IRGenModule(IRGenerator &irgen,
209194
std::unique_ptr<llvm::TargetMachine> &&target,
210-
SourceFile *SF, StringRef ModuleName,
211-
StringRef OutputFilename,
195+
SourceFile *SF,
196+
StringRef ModuleName, StringRef OutputFilename,
212197
StringRef MainInputFilenameForDebugInfo,
213198
StringRef PrivateDiscriminator)
214-
: LLVMContext(new llvm::LLVMContext()), IRGen(irgen),
215-
Context(irgen.SIL.getASTContext()),
199+
: LLVMContext(new llvm::LLVMContext()),
200+
IRGen(irgen), Context(irgen.SIL.getASTContext()),
216201
// The LLVMContext (and the IGM itself) will get deleted by the IGMDeleter
217202
// as long as the IGM is registered with the IRGenerator.
218-
ClangCodeGen(createClangCodeGenerator(Context, *LLVMContext, irgen.Opts,
219-
ModuleName, PrivateDiscriminator,
220-
irgen.getEffectiveClangTriple())),
203+
ClangCodeGen(createClangCodeGenerator(Context, *LLVMContext,
204+
irgen.Opts,
205+
ModuleName, PrivateDiscriminator)),
221206
Module(*ClangCodeGen->GetModule()),
222207
DataLayout(irgen.getClangDataLayout()),
223208
Triple(irgen.getEffectiveClangTriple()), TargetMachine(std::move(target)),
@@ -999,28 +984,13 @@ bool swift::irgen::shouldRemoveTargetFeature(StringRef feature) {
999984
return feature == "+thumb-mode";
1000985
}
1001986

1002-
void IRGenModule::setHasFramePointer(llvm::AttrBuilder &Attrs,
1003-
bool HasFramePointer) {
1004-
if (!HasFramePointer) {
1005-
Attrs.addAttribute("frame-pointer", "none");
1006-
return;
1007-
}
1008-
if (IRGen.Opts.DisableFPElimLeaf) {
1009-
Attrs.addAttribute("frame-pointer", "all");
1010-
return;
1011-
}
1012-
1013-
// We omit frame pointers for leaf functions only for arm64 for now (matching
1014-
// clang's behavior).
1015-
auto framePointer =
1016-
IRGen.getEffectiveClangTriple().isAArch64() ? "non-leaf" : "all";
1017-
Attrs.addAttribute("frame-pointer", framePointer);
987+
void IRGenModule::setHasNoFramePointer(llvm::AttrBuilder &Attrs) {
988+
Attrs.addAttribute("frame-pointer", "none");
1018989
}
1019990

1020-
void IRGenModule::setHasFramePointer(llvm::Function *F,
1021-
bool HasFramePointer) {
991+
void IRGenModule::setHasNoFramePointer(llvm::Function *F) {
1022992
llvm::AttrBuilder b;
1023-
setHasFramePointer(b, HasFramePointer);
993+
setHasNoFramePointer(b);
1024994
F->addAttributes(llvm::AttributeList::FunctionIndex, b);
1025995
}
1026996

@@ -1030,10 +1000,6 @@ void IRGenModule::constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
10301000
// Add the default attributes for the Clang configuration.
10311001
clang::CodeGen::addDefaultFunctionDefinitionAttributes(getClangCGM(), Attrs);
10321002

1033-
// Add frame pointer attributes.
1034-
// FIXME: why are we doing this?
1035-
setHasFramePointer(Attrs, IRGen.Opts.DisableFPElim);
1036-
10371003
// Add/remove MinSize based on the appropriate setting.
10381004
if (FuncOptMode == OptimizationMode::NotSet)
10391005
FuncOptMode = IRGen.Opts.OptMode;

lib/IRGen/IRGenModule.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,8 +1305,8 @@ private: \
13051305
void constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
13061306
OptimizationMode FuncOptMode =
13071307
OptimizationMode::NotSet);
1308-
void setHasFramePointer(llvm::AttrBuilder &Attrs, bool HasFP);
1309-
void setHasFramePointer(llvm::Function *F, bool HasFP);
1308+
void setHasNoFramePointer(llvm::AttrBuilder &Attrs);
1309+
void setHasNoFramePointer(llvm::Function *F);
13101310
llvm::AttributeList constructInitialAttributes();
13111311

13121312
void emitProtocolDecl(ProtocolDecl *D);

lib/IRGen/MetadataRequest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ void irgen::emitCacheAccessFunction(IRGenModule &IGM,
16151615
accessor->addAttribute(llvm::AttributeList::FunctionIndex,
16161616
llvm::Attribute::NoInline);
16171617
// Accessor functions don't need frame pointers.
1618-
IGM.setHasFramePointer(accessor, false);
1618+
IGM.setHasNoFramePointer(accessor);
16191619

16201620
// This function is logically 'readnone': the caller does not need
16211621
// to reason about any side effects or stores it might perform.
@@ -1997,8 +1997,8 @@ MetadataResponse irgen::emitGenericTypeMetadataAccessFunction(
19971997
thunkFn->setCallingConv(IGM.SwiftCC);
19981998
thunkFn->addAttribute(llvm::AttributeList::FunctionIndex,
19991999
llvm::Attribute::NoInline);
2000-
IGM.setHasFramePointer(thunkFn, false);
2001-
2000+
IGM.setHasNoFramePointer(thunkFn);
2001+
20022002
[&IGM, thunkFn]{
20032003
IRGenFunction subIGF(IGM, thunkFn);
20042004

@@ -2469,7 +2469,7 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type,
24692469
instantiationFn->setDoesNotThrow();
24702470
instantiationFn->addAttribute(llvm::AttributeList::FunctionIndex,
24712471
llvm::Attribute::NoInline);
2472-
IGM.setHasFramePointer(instantiationFn, false);
2472+
IGM.setHasNoFramePointer(instantiationFn);
24732473

24742474
[&IGM, instantiationFn, request]{
24752475
IRGenFunction subIGF(IGM, instantiationFn);

test/IRGen/framepointer.sil

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %target-swift-frontend -primary-file %s -emit-ir | %FileCheck %s --check-prefix=CHECK
2-
// RUN: %target-swift-frontend -primary-file %s -emit-ir -no-omit-leaf-frame-pointer| %FileCheck %s --check-prefix=CHECK-ALL
2+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -Xcc -mno-omit-leaf-frame-pointer | %FileCheck %s --check-prefix=CHECK-ALL
33
// RUN: %target-swift-frontend -primary-file %s -S | %FileCheck %s --check-prefix=CHECKASM --check-prefix=CHECKASM-%target-os-%target-cpu
4+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -Xcc -momit-leaf-frame-pointer | %FileCheck %s --check-prefix=LEAF
5+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -Xcc -fomit-frame-pointer | %FileCheck %s --check-prefix=NOFP
46

57
// REQUIRES: CPU=x86_64
68

@@ -46,6 +48,32 @@ entry(%i : $Int32):
4648

4749
// CHECK-ALL: attributes [[ATTR]] = {{{.*}}"frame-pointer"="all"
4850

51+
// LEAF: define{{.*}} swiftcc i32 @leaf_function_no_frame_pointer(i32 %0) [[ATTR:#.*]] {
52+
// LEAF: entry:
53+
// LEAF: ret i32 %0
54+
// LEAF: }
55+
56+
// LEAF: define{{.*}} swiftcc i32 @non_leaf_function_with_frame_pointer(i32 %0) [[ATTR]] {
57+
// LEAF: entry:
58+
// LEAF: %1 = call swiftcc i32 @leaf_function_no_frame_pointer(i32 %0)
59+
// LEAF: ret i32 %1
60+
// LEAF: }
61+
62+
// LEAF: attributes [[ATTR]] = {{{.*}}"frame-pointer"="non-leaf"
63+
64+
// NOFP: define{{.*}} swiftcc i32 @leaf_function_no_frame_pointer(i32 %0) [[ATTR:#.*]] {
65+
// NOFP: entry:
66+
// NOFP: ret i32 %0
67+
// NOFP: }
68+
69+
// NOFP: define{{.*}} swiftcc i32 @non_leaf_function_with_frame_pointer(i32 %0) [[ATTR]] {
70+
// NOFP: entry:
71+
// NOFP: %1 = call swiftcc i32 @leaf_function_no_frame_pointer(i32 %0)
72+
// NOFP: ret i32 %1
73+
// NOFP: }
74+
75+
// NOFP: attributes [[ATTR]] = {{{.*}}"frame-pointer"="none"
76+
4977
// Silence other os-archs.
5078
// CHECKASM: {{.*}}
5179

test/IRGen/framepointer_arm64.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-swift-frontend -target arm64-apple-ios8.0 -primary-file %s -emit-ir | %FileCheck %s --check-prefix=CHECK
2-
// RUN: %target-swift-frontend -target arm64-apple-ios8.0 -primary-file %s -emit-ir -no-omit-leaf-frame-pointer| %FileCheck %s --check-prefix=CHECK-ALL
2+
// RUN: %target-swift-frontend -target arm64-apple-ios8.0 -primary-file %s -emit-ir -Xcc -mno-omit-leaf-frame-pointer| %FileCheck %s --check-prefix=CHECK-ALL
33
// RUN: %target-swift-frontend -target arm64-apple-ios8.0 -primary-file %s -S | %FileCheck %s --check-prefix=CHECKASM
4-
// RUN: %target-swift-frontend -target arm64-apple-ios8.0 -primary-file %s -S -no-omit-leaf-frame-pointer | %FileCheck %s --check-prefix=CHECKASM-ALL
4+
// RUN: %target-swift-frontend -target arm64-apple-ios8.0 -primary-file %s -S -Xcc -mno-omit-leaf-frame-pointer | %FileCheck %s --check-prefix=CHECKASM-ALL
55

66
// REQUIRES: CODEGENERATOR=AArch64
77
// REQUIRES: OS=ios

0 commit comments

Comments
 (0)