Skip to content

Commit 11e8bb8

Browse files
committed
IRGen: -enable-split-cold-code frontend feature
1 parent 885b758 commit 11e8bb8

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ class IRGenOptions {
475475

476476
unsigned UseFragileResilientProtocolWitnesses : 1;
477477

478+
// Whether to run the HotColdSplitting pass when optimizing.
479+
unsigned EnableHotColdSplit : 1;
480+
478481
/// The number of threads for multi-threaded code generation.
479482
unsigned NumThreads = 0;
480483

@@ -564,6 +567,7 @@ class IRGenOptions {
564567
DisableReadonlyStaticObjects(false), CollocatedMetadataFunctions(false),
565568
ColocateTypeDescriptors(true), UseRelativeProtocolWitnessTables(false),
566569
UseFragileResilientProtocolWitnesses(false),
570+
EnableHotColdSplit(false),
567571
CmdArgs(), SanitizeCoverage(llvm::SanitizerCoverageOptions()),
568572
TypeInfoFilter(TypeInfoDumpFilter::All),
569573
PlatformCCallingConvention(llvm::CallingConv::C), UseCASBackend(false),

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,13 @@ def disable_fragile_resilient_protocol_witnesses :
13101310
Flag<["-"], "disable-fragile-relative-protocol-tables">,
13111311
HelpText<"Disable relative protocol witness tables">;
13121312

1313+
def enable_split_cold_code :
1314+
Flag<["-"], "enable-split-cold-code">,
1315+
HelpText<"Enable splitting of cold code when optimizing">;
1316+
def disable_split_cold_code :
1317+
Flag<["-"], "disable-split-cold-code">,
1318+
HelpText<"Disable splitting of cold code when optimizing">;
1319+
13131320
def enable_new_llvm_pass_manager :
13141321
Flag<["-"], "enable-new-llvm-pass-manager">,
13151322
HelpText<"Enable the new llvm pass manager">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,6 +3362,10 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
33623362
Args.hasFlag(OPT_enable_fragile_resilient_protocol_witnesses,
33633363
OPT_disable_fragile_resilient_protocol_witnesses,
33643364
Opts.UseFragileResilientProtocolWitnesses);
3365+
Opts.EnableHotColdSplit =
3366+
Args.hasFlag(OPT_enable_split_cold_code,
3367+
OPT_disable_split_cold_code,
3368+
Opts.EnableHotColdSplit);
33653369
Opts.EnableLargeLoadableTypesReg2Mem =
33663370
Args.hasFlag(OPT_enable_large_loadable_types_reg2mem,
33673371
OPT_disable_large_loadable_types_reg2mem,

lib/IRGen/IRGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
211211
bool RunSwiftSpecificLLVMOptzns =
212212
!Opts.DisableSwiftSpecificLLVMOptzns && !Opts.DisableLLVMOptzns;
213213

214+
bool DoHotColdSplit = false;
214215
PTO.CallGraphProfile = false;
215216

216217
llvm::OptimizationLevel level = llvm::OptimizationLevel::O0;
@@ -221,6 +222,7 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
221222
PTO.LoopVectorization = true;
222223
PTO.SLPVectorization = true;
223224
PTO.MergeFunctions = true;
225+
DoHotColdSplit = Opts.EnableHotColdSplit;
224226
level = llvm::OptimizationLevel::Os;
225227
} else {
226228
level = llvm::OptimizationLevel::O0;
@@ -259,6 +261,8 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
259261
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
260262
ModulePassManager MPM;
261263

264+
PB.setEnableHotColdSplitting(DoHotColdSplit);
265+
262266
if (RunSwiftSpecificLLVMOptzns) {
263267
PB.registerScalarOptimizerLateEPCallback(
264268
[](FunctionPassManager &FPM, OptimizationLevel Level) {

test/IRGen/cold_split.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-swift-frontend %s -module-name=test -emit-assembly \
2+
// RUN: -enable-throws-prediction -O -enable-split-cold-code \
3+
// RUN: | %FileCheck --check-prefix CHECK-ENABLED %s
4+
5+
//// Test disabling just the pass doesn't yield a split.
6+
// RUN: %target-swift-frontend %s -module-name=test -emit-assembly \
7+
// RUN: -enable-throws-prediction -O -disable-split-cold-code \
8+
// RUN: | %FileCheck --check-prefix CHECK-DISABLED %s
9+
10+
//// Test disabling optimization entirely doesn't yield a split.
11+
// RUN: %target-swift-frontend %s -module-name=test -emit-assembly \
12+
// RUN: -enable-throws-prediction -enable-split-cold-code \
13+
// RUN: | %FileCheck --check-prefix CHECK-DISABLED %s
14+
15+
16+
// CHECK-ENABLED: cold
17+
18+
// CHECK-DISABLED-NOT: cold
19+
20+
enum MyError: Error { case err }
21+
22+
func getRandom(_ b: Bool) throws -> Int {
23+
if b {
24+
return Int.random(in: 0..<1024)
25+
} else {
26+
throw MyError.err
27+
}
28+
}
29+
30+
public func numberWithLogging(_ b: Bool) -> Int {
31+
do {
32+
return try getRandom(b)
33+
} catch {
34+
print("Log: random number generator failed with b=\(b)")
35+
return 1337
36+
}
37+
}

0 commit comments

Comments
 (0)