Skip to content

Commit a77ced8

Browse files
committed
Add frontend flags for developers to easily control copy propagation:
-enable-copy-propagation: enables whatever form of copy propagation the current pipeline runs (mandatory-copy-propagation at -Onone, regular copy-propation at -O). -disable-copy-propagation: similarly disables any form of copy propagation in the current pipelien.
1 parent b689b1d commit a77ced8

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

include/swift/AST/SILOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class SILOptions {
4444
/// Remove all runtime assertions during optimizations.
4545
bool RemoveRuntimeAsserts = false;
4646

47+
/// Force-run SIL copy propagation to shorten object lifetime in whatever
48+
/// optimization pipeline is currently used.
49+
/// When this is 'false' the pipeline has default behavior.
50+
bool EnableCopyPropagation = false;
51+
52+
/// Disable SIL copy propagation to preserve object lifetime in whatever
53+
/// optimization pipeline is currently used.
54+
/// When this is 'false' the pipeline has default behavior.
55+
bool DisableCopyPropagation = false;
56+
4757
/// Controls whether the SIL ARC optimizations are run.
4858
bool EnableARCOptimizations = true;
4959

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ def batch_scan_input_file
188188
def import_prescan : Flag<["-"], "import-prescan">,
189189
HelpText<"When performing a dependency scan, only dentify all imports of the main Swift module sources">;
190190

191+
def enable_copy_propagation : Flag<["-"], "enable-copy-propagation">,
192+
HelpText<"Run SIL copy propagation to shorten object lifetime.">;
193+
def disable_copy_propagation : Flag<["-"], "disable-copy-propagation">,
194+
HelpText<"Don't run SIL copy propagation to preserve object lifetime.">;
195+
191196
} // end let Flags = [FrontendOption, NoDriverOption]
192197

193198
def debug_crash_Group : OptionGroup<"<automatic crashing options>">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,8 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
11851185
// -Ounchecked might also set removal of runtime asserts (cond_fail).
11861186
Opts.RemoveRuntimeAsserts |= Args.hasArg(OPT_RemoveRuntimeAsserts);
11871187

1188+
Opts.EnableCopyPropagation |= Args.hasArg(OPT_enable_copy_propagation);
1189+
Opts.DisableCopyPropagation |= Args.hasArg(OPT_disable_copy_propagation);
11881190
Opts.EnableARCOptimizations &= !Args.hasArg(OPT_disable_arc_opts);
11891191
Opts.EnableOSSAModules |= Args.hasArg(OPT_enable_ossa_modules);
11901192
Opts.EnableOSSAOptimizations &= !Args.hasArg(OPT_disable_ossa_opts);

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ void addFunctionPasses(SILPassPipelinePlan &P,
350350
if (P.getOptions().EnableOSSAModules) {
351351
// We earlier eliminated ownership if we are not compiling the stdlib. Now
352352
// handle the stdlib functions, re-simplifying, eliminating ARC as we do.
353-
P.addCopyPropagation();
353+
if (!P.getOptions().DisableCopyPropagation) {
354+
P.addCopyPropagation();
355+
}
354356
P.addSemanticARCOpts();
355357
}
356358

@@ -372,7 +374,9 @@ void addFunctionPasses(SILPassPipelinePlan &P,
372374

373375
// Clean up Semantic ARC before we perform additional post-inliner opts.
374376
if (P.getOptions().EnableOSSAModules) {
375-
P.addCopyPropagation();
377+
if (!P.getOptions().DisableCopyPropagation) {
378+
P.addCopyPropagation();
379+
}
376380
P.addSemanticARCOpts();
377381
}
378382

@@ -437,7 +441,9 @@ void addFunctionPasses(SILPassPipelinePlan &P,
437441

438442
// Run a final round of ARC opts when ownership is enabled.
439443
if (P.getOptions().EnableOSSAModules) {
440-
P.addCopyPropagation();
444+
if (!P.getOptions().DisableCopyPropagation) {
445+
P.addCopyPropagation();
446+
}
441447
P.addSemanticARCOpts();
442448
}
443449
}
@@ -471,7 +477,9 @@ static void addPerfEarlyModulePassPipeline(SILPassPipelinePlan &P) {
471477
// Cleanup after SILGen: remove trivial copies to temporaries.
472478
P.addTempRValueOpt();
473479
// Cleanup after SILGen: remove unneeded borrows/copies.
474-
P.addCopyPropagation();
480+
if (!P.getOptions().DisableCopyPropagation) {
481+
P.addCopyPropagation();
482+
}
475483
P.addSemanticARCOpts();
476484

477485
// Devirtualizes differentiability witnesses into functions that reference them.
@@ -785,7 +793,9 @@ SILPassPipelinePlan::getPerformancePassPipeline(const SILOptions &Options) {
785793
// Run one last copy propagation/semantic arc opts run before serialization/us
786794
// lowering ownership.
787795
if (P.getOptions().EnableOSSAModules) {
788-
P.addCopyPropagation();
796+
if (!P.getOptions().DisableCopyPropagation) {
797+
P.addCopyPropagation();
798+
}
789799
P.addSemanticARCOpts();
790800
}
791801

@@ -835,8 +845,10 @@ SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
835845
P.startPipeline("non-Diagnostic Enabling Mandatory Optimizations");
836846
P.addForEachLoopUnroll();
837847
P.addMandatoryCombine();
838-
// MandatoryCopyPropagation should only be run at -Onone, not -O.
839-
P.addMandatoryCopyPropagation();
848+
if (P.getOptions().EnableCopyPropagation) {
849+
// MandatoryCopyPropagation should only be run at -Onone, not -O.
850+
P.addMandatoryCopyPropagation();
851+
}
840852
// TODO: MandatoryARCOpts should be subsumed by CopyPropagation. There should
841853
// be no need to run another analysis of copies at -Onone.
842854
P.addMandatoryARCOpts();

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,9 @@ void CopyPropagation::run() {
136136
}
137137

138138
SILTransform *swift::createMandatoryCopyPropagation() {
139-
return new CopyPropagation(/*pruneDebug*/ true, /*unownedRemnant*/ true,
140-
/*canonicalizeAll*/ true);
139+
return new CopyPropagation(/*pruneDebug*/ true, /*canonicalizeAll*/ true);
141140
}
142141

143142
SILTransform *swift::createCopyPropagation() {
144-
return new CopyPropagation(/*pruneDebug*/ true, /*unownedRemnant*/ false,
145-
/*canonicalizeAll*/ false);
143+
return new CopyPropagation(/*pruneDebug*/ true, /*canonicalizeAll*/ false);
146144
}

0 commit comments

Comments
 (0)