Skip to content

Commit 8b569c4

Browse files
committed
IRGen: -enable-split-cold-code frontend flag
1 parent c2df9b5 commit 8b569c4

File tree

5 files changed

+53
-0
lines changed

5 files changed

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

0 commit comments

Comments
 (0)