Skip to content

Commit 3f903b4

Browse files
Merge pull request swiftlang#31986 from aschwaighofer/irgen_inherit_clangs_fp_elim
IRGen: Default to clang's frame pointer elimination settings
2 parents f7473a7 + 20f4ef9 commit 3f903b4

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
@@ -1017,10 +1017,6 @@ def enable_private_imports : Flag<["-"], "enable-private-imports">,
10171017
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
10181018
HelpText<"Allows this module's internal and private API to be accessed">;
10191019

1020-
def disable_leaf_frame_pointer_elim : Flag<["-"], "no-omit-leaf-frame-pointer">,
1021-
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
1022-
HelpText<"Don't omit the frame pointer for leaf functions">;
1023-
10241020
def sanitize_EQ : CommaJoined<["-"], "sanitize=">,
10251021
Flags<[FrontendOption, NoInteractiveOption]>, MetaVarName<"<check>">,
10261022
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
@@ -290,8 +290,6 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
290290
arguments.push_back("-enable-anonymous-context-mangled-names");
291291
}
292292

293-
inputArgs.AddLastArg(arguments, options::OPT_disable_leaf_frame_pointer_elim);
294-
295293
// Pass through any subsystem flags.
296294
inputArgs.AddAllArgs(arguments, options::OPT_Xllvm);
297295
inputArgs.AddAllArgs(arguments, options::OPT_Xcc);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,9 +1533,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
15331533
getRuntimeCompatVersion();
15341534
}
15351535

1536-
if (Args.hasArg(OPT_disable_leaf_frame_pointer_elim))
1537-
Opts.DisableFPElimLeaf = true;
1538-
15391536
return false;
15401537
}
15411538

lib/IRGen/GenMeta.cpp

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

185185
IRGenFunction IGF(IGM, f);
186186

@@ -2270,7 +2270,7 @@ namespace {
22702270
IGM.getAddrOfTypeMetadataInstantiationFunction(Target, ForDefinition);
22712271
f->setAttributes(IGM.constructInitialAttributes());
22722272
f->setDoesNotThrow();
2273-
IGM.setHasFramePointer(f, false);
2273+
IGM.setHasNoFramePointer(f);
22742274

22752275
IRGenFunction IGF(IGM, f);
22762276

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
@@ -1313,8 +1313,8 @@ private: \
13131313
void constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
13141314
OptimizationMode FuncOptMode =
13151315
OptimizationMode::NotSet);
1316-
void setHasFramePointer(llvm::AttrBuilder &Attrs, bool HasFP);
1317-
void setHasFramePointer(llvm::Function *F, bool HasFP);
1316+
void setHasNoFramePointer(llvm::AttrBuilder &Attrs);
1317+
void setHasNoFramePointer(llvm::Function *F);
13181318
llvm::AttributeList constructInitialAttributes();
13191319

13201320
void emitProtocolDecl(ProtocolDecl *D);

lib/IRGen/MetadataRequest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ void irgen::emitCacheAccessFunction(IRGenModule &IGM,
16841684
accessor->addAttribute(llvm::AttributeList::FunctionIndex,
16851685
llvm::Attribute::NoInline);
16861686
// Accessor functions don't need frame pointers.
1687-
IGM.setHasFramePointer(accessor, false);
1687+
IGM.setHasNoFramePointer(accessor);
16881688

16891689
// This function is logically 'readnone': the caller does not need
16901690
// to reason about any side effects or stores it might perform.
@@ -2094,8 +2094,8 @@ MetadataResponse irgen::emitGenericTypeMetadataAccessFunction(
20942094
thunkFn->setCallingConv(IGM.SwiftCC);
20952095
thunkFn->addAttribute(llvm::AttributeList::FunctionIndex,
20962096
llvm::Attribute::NoInline);
2097-
IGM.setHasFramePointer(thunkFn, false);
2098-
2097+
IGM.setHasNoFramePointer(thunkFn);
2098+
20992099
[&IGM, thunkFn]{
21002100
IRGenFunction subIGF(IGM, thunkFn);
21012101

@@ -2631,7 +2631,7 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type,
26312631
instantiationFn->setDoesNotThrow();
26322632
instantiationFn->addAttribute(llvm::AttributeList::FunctionIndex,
26332633
llvm::Attribute::NoInline);
2634-
IGM.setHasFramePointer(instantiationFn, false);
2634+
IGM.setHasNoFramePointer(instantiationFn);
26352635

26362636
[&IGM, instantiationFn, request]{
26372637
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)