Skip to content

Commit eed6a4e

Browse files
asuhancompnerd
authored andcommitted
Expose function sections option (#28088)
* Expose separate function sections option to compiler * Add function sections test
1 parent d8cc616 commit eed6a4e

File tree

8 files changed

+41
-1
lines changed

8 files changed

+41
-1
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ class IRGenOptions {
146146
/// objects.
147147
unsigned EmitStackPromotionChecks : 1;
148148

149+
/// Emit functions to separate sections.
150+
unsigned FunctionSections : 1;
151+
149152
/// The maximum number of bytes used on a stack frame for stack promotion
150153
/// (includes alloc_stack allocations).
151154
unsigned StackPromotionSizeLimit = 1024;
@@ -253,7 +256,7 @@ class IRGenOptions {
253256
IntegratedREPL(false), DisableLLVMOptzns(false),
254257
DisableSwiftSpecificLLVMOptzns(false), DisableLLVMSLPVectorizer(false),
255258
DisableFPElim(true), Playground(false), EmitStackPromotionChecks(false),
256-
PrintInlineTree(false), EmbedMode(IRGenEmbedMode::None),
259+
FunctionSections(false), PrintInlineTree(false), EmbedMode(IRGenEmbedMode::None),
257260
HasValueNamesSetting(false), ValueNames(false),
258261
EnableReflectionMetadata(true), EnableReflectionNames(true),
259262
EnableAnonymousContextMangledNames(false), ForcePublicLinkage(false),

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ def disable_reflection_names : Flag<["-"], "disable-reflection-names">,
334334
HelpText<"Disable emission of names of stored properties and enum cases in"
335335
"reflection metadata">;
336336

337+
def function_sections: Flag<["-"], "function-sections">,
338+
Flags<[FrontendOption, NoInteractiveOption]>,
339+
HelpText<"Emit functions to separate sections.">;
340+
337341
def stack_promotion_checks : Flag<["-"], "emit-stack-promotion-checks">,
338342
HelpText<"Emit runtime checks for correct stack promotion of objects.">;
339343

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,8 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
11361136
Opts.StackPromotionSizeLimit = limit;
11371137
}
11381138

1139+
Opts.FunctionSections = Args.hasArg(OPT_function_sections);
1140+
11391141
if (Args.hasArg(OPT_autolink_force_load))
11401142
Opts.ForceLoadSymbolName = Args.getLastArgValue(OPT_module_link_name);
11411143

lib/IRGen/IRGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ swift::getIRTargetOptions(IRGenOptions &Opts, ASTContext &Ctx) {
162162
// Explicitly request debugger tuning for LLDB which is the default
163163
// on Darwin platforms but not on others.
164164
TargetOpts.DebuggerTuning = llvm::DebuggerKind::LLDB;
165+
TargetOpts.FunctionSections = Opts.FunctionSections;
165166

166167
auto *Clang = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
167168
clang::TargetOptions &ClangOpts = Clang->getTargetInfo().getTargetOpts();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public typealias Void = ()
2+
3+
public func func1() -> Void {}
4+
5+
public func func2() -> Void {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import FunctionSections
2+
3+
func1()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: OS=linux-gnu || OS=linux-androideabi || OS=linux-android || OS=freebsd
2+
// RUN: %empty-directory(%t)
3+
// RUN: %target-build-swift -Xfrontend -function-sections -emit-module -emit-library -static -parse-stdlib %S/Inputs/FunctionSections.swift
4+
// RUN: %target-build-swift -Xlinker --gc-sections -Xlinker -Map=%t/../../FunctionSections.map -I%t/../.. -L%t/../.. -lFunctionSections %S/Inputs/FunctionSectionsUse.swift
5+
// RUN: %FileCheck %s < %t/../../FunctionSections.map
6+
7+
// CHECK: Discarded input sections
8+
// CHECK: .text.$s16FunctionSections5func2yyF
9+
// CHECK: Memory map
10+
// CHECK: .text.$s16FunctionSections5func1yyF
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -Xfrontend -function-sections -emit-library -emit-ir -static -parse-stdlib %S/Inputs/FunctionSections.swift | %FileCheck %s
3+
4+
// CHECK: define {{(dllexport |protected )?}}swiftcc void @"$s16FunctionSections5func1yyF"() #0 {
5+
// CHECK: entry:
6+
// CHECK: ret void
7+
// CHECK: }
8+
9+
// CHECK: define {{(dllexport |protected )?}}swiftcc void @"$s16FunctionSections5func2yyF"() #0 {
10+
// CHECK: entry:
11+
// CHECK: ret void
12+
// CHECK: }

0 commit comments

Comments
 (0)