Skip to content

Commit 64a9154

Browse files
committed
Frontend: Add an Osize optimization option
This adds an size optimization mode ("Osize") which intends to enable some optimization but targets mainly reduced code size compared to the regular optimized mode ("O"). rdar://33075751
1 parent 148b57b commit 64a9154

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class IRGenOptions {
9696
/// Whether or not to run optimization passes.
9797
unsigned Optimize : 1;
9898

99+
/// Whether or not to optimize for code size.
100+
unsigned OptimizeForSize : 1;
101+
99102
/// Which sanitizer is turned on.
100103
SanitizerKind Sanitize : 2;
101104

@@ -172,7 +175,8 @@ class IRGenOptions {
172175

173176
IRGenOptions()
174177
: DWARFVersion(2), OutputKind(IRGenOutputKind::LLVMAssembly),
175-
Verify(true), Optimize(false), Sanitize(SanitizerKind::None),
178+
Verify(true), Optimize(false), OptimizeForSize(false),
179+
Sanitize(SanitizerKind::None),
176180
DebugInfoKind(IRGenDebugInfoKind::None), UseJIT(false),
177181
DisableLLVMOptzns(false), DisableLLVMARCOpts(false),
178182
DisableLLVMSLPVectorizer(false), DisableFPElim(true), Playground(false),
@@ -196,6 +200,7 @@ class IRGenOptions {
196200
unsigned getLLVMCodeGenOptionsHash() {
197201
unsigned Hash = 0;
198202
Hash = (Hash << 1) | Optimize;
203+
Hash = (Hash << 1) | OptimizeForSize;
199204
Hash = (Hash << 1) | DisableLLVMOptzns;
200205
Hash = (Hash << 1) | DisableLLVMARCOpts;
201206
return Hash;

include/swift/AST/SILOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class SILOptions {
5151
None,
5252
Debug,
5353
Optimize,
54+
OptimizeForSize,
5455
OptimizeUnchecked
5556
};
5657

include/swift/Option/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ def Onone : Flag<["-"], "Onone">, Group<O_Group>, Flags<[FrontendOption]>,
374374
HelpText<"Compile without any optimization">;
375375
def O : Flag<["-"], "O">, Group<O_Group>, Flags<[FrontendOption]>,
376376
HelpText<"Compile with optimizations">;
377+
def Osize : Flag<["-"], "Osize">, Group<O_Group>, Flags<[FrontendOption]>,
378+
HelpText<"Compile with optimizations and target small code size">;
377379
def Ounchecked : Flag<["-"], "Ounchecked">, Group<O_Group>,
378380
Flags<[FrontendOption]>,
379381
HelpText<"Compile with optimizations and remove runtime safety checks">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,23 +1373,34 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
13731373
Args.hasArg(OPT_sil_serialize_witness_tables);
13741374

13751375
// Parse the optimization level.
1376+
// Default to Onone settings if no option is passed.
1377+
IRGenOpts.Optimize = false;
1378+
IRGenOpts.OptimizeForSize = false;
1379+
Opts.Optimization = SILOptions::SILOptMode::None;
13761380
if (const Arg *A = Args.getLastArg(OPT_O_Group)) {
13771381
if (A->getOption().matches(OPT_Onone)) {
13781382
IRGenOpts.Optimize = false;
13791383
Opts.Optimization = SILOptions::SILOptMode::None;
13801384
} else if (A->getOption().matches(OPT_Ounchecked)) {
13811385
// Turn on optimizations and remove all runtime checks.
13821386
IRGenOpts.Optimize = true;
1387+
IRGenOpts.OptimizeForSize = false;
13831388
Opts.Optimization = SILOptions::SILOptMode::OptimizeUnchecked;
13841389
// Removal of cond_fail (overflow on binary operations).
13851390
Opts.RemoveRuntimeAsserts = true;
13861391
Opts.AssertConfig = SILOptions::Unchecked;
13871392
} else if (A->getOption().matches(OPT_Oplayground)) {
13881393
// For now -Oplayground is equivalent to -Onone.
13891394
IRGenOpts.Optimize = false;
1395+
IRGenOpts.OptimizeForSize = false;
13901396
Opts.Optimization = SILOptions::SILOptMode::None;
1397+
} else if (A->getOption().matches(OPT_Osize)) {
1398+
IRGenOpts.Optimize = true;
1399+
IRGenOpts.OptimizeForSize = true;
1400+
Opts.Optimization = SILOptions::SILOptMode::OptimizeForSize;
13911401
} else {
13921402
assert(A->getOption().matches(OPT_O));
1403+
IRGenOpts.OptimizeForSize = false;
13931404
IRGenOpts.Optimize = true;
13941405
Opts.Optimization = SILOptions::SILOptMode::Optimize;
13951406
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ static bool emitIndexData(SourceFile *PrimarySourceFile,
10241024
isDebugCompilation = true;
10251025
break;
10261026
case SILOptions::SILOptMode::Optimize:
1027+
case SILOptions::SILOptMode::OptimizeForSize:
10271028
case SILOptions::SILOptMode::OptimizeUnchecked:
10281029
isDebugCompilation = false;
10291030
break;
@@ -1123,6 +1124,8 @@ silOptModeArgStr(SILOptions::SILOptMode mode) {
11231124
return "O";
11241125
case SILOptions::SILOptMode::OptimizeUnchecked:
11251126
return "Ounchecked";
1127+
case SILOptions::SILOptMode::OptimizeForSize:
1128+
return "Osize";
11261129
default:
11271130
return "Onone";
11281131
}

0 commit comments

Comments
 (0)