Skip to content

Commit f67df31

Browse files
committed
[NFC] Used SILOption field for copy propagation.
Replaced the quad-state (of state which one was illegal) of two booleans (EnableCopyPropagation and DisableCopyPropagation) with an enum.
1 parent 0c1b56a commit f67df31

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

include/swift/AST/SILOptions.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ enum class LexicalLifetimesOption : uint8_t {
4444
On,
4545
};
4646

47+
enum class CopyPropagationOption : uint8_t {
48+
// Do not add any copy propagation passes.
49+
Off = 0,
50+
51+
// Only add the copy propagation passes requested by other flags, currently
52+
// just -enable-ossa-modules.
53+
RequestedPassesOnly,
54+
55+
// Add all relevant copy propagation passes. If a setting, e.g.
56+
// -enable-ossa-modules, requests to add copy propagation to the pipeline, do
57+
// so.
58+
On
59+
};
60+
4761
class SILModule;
4862

4963
class SILOptions {
@@ -64,15 +78,12 @@ class SILOptions {
6478
LexicalLifetimesOption LexicalLifetimes =
6579
LexicalLifetimesOption::DiagnosticMarkersOnly;
6680

67-
/// Force-run SIL copy propagation to shorten object lifetime in whatever
68-
/// optimization pipeline is currently used.
69-
/// When this is 'false' the pipeline has default behavior.
70-
bool EnableCopyPropagation = false;
71-
72-
/// Disable SIL copy propagation to preserve object lifetime in whatever
81+
/// Whether to run SIL copy propagation to shorten object lifetime in whatever
7382
/// optimization pipeline is currently used.
74-
/// When this is 'false' the pipeline has default behavior.
75-
bool DisableCopyPropagation = false;
83+
///
84+
/// When this is 'RequestedPassesOnly' the pipeline has default behavior.
85+
CopyPropagationOption CopyPropagation =
86+
CopyPropagationOption::RequestedPassesOnly;
7687

7788
/// Controls whether the SIL ARC optimizations are run.
7889
bool EnableARCOptimizations = true;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,10 +1549,18 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
15491549
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_combination,
15501550
"enable-copy-propagation", "disable-copy-propagation");
15511551
return true;
1552+
} else if (Args.hasArg(OPT_enable_copy_propagation) &&
1553+
!Args.hasArg(OPT_disable_copy_propagation)) {
1554+
Opts.CopyPropagation = CopyPropagationOption::On;
1555+
} else if (!Args.hasArg(OPT_enable_copy_propagation) &&
1556+
Args.hasArg(OPT_disable_copy_propagation)) {
1557+
Opts.CopyPropagation = CopyPropagationOption::Off;
1558+
} else /*if (!Args.hasArg(OPT_enable_copy_propagation) &&
1559+
!Args.hasArg(OPT_disable_copy_propagation))*/
1560+
{
1561+
Opts.CopyPropagation = CopyPropagationOption::RequestedPassesOnly;
15521562
}
15531563

1554-
Opts.EnableCopyPropagation |= Args.hasArg(OPT_enable_copy_propagation);
1555-
Opts.DisableCopyPropagation |= Args.hasArg(OPT_disable_copy_propagation);
15561564
Opts.EnableARCOptimizations &= !Args.hasArg(OPT_disable_arc_opts);
15571565
Opts.EnableOSSAModules |= Args.hasArg(OPT_enable_ossa_modules);
15581566
Opts.EnableOSSAOptimizations &= !Args.hasArg(OPT_disable_ossa_opts);

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ static void addMandatoryDiagnosticOptPipeline(SILPassPipelinePlan &P) {
190190

191191
// Only issue weak lifetime warnings for users who select object lifetime
192192
// optimization. The risk of spurious warnings outweighs the benefits.
193-
if (P.getOptions().EnableCopyPropagation) {
193+
if (P.getOptions().CopyPropagation == CopyPropagationOption::On) {
194194
P.addDiagnoseLifetimeIssues();
195195
}
196-
196+
197197
P.addGlobalOpt();
198198
P.addPerformanceDiagnostics();
199199

@@ -408,7 +408,7 @@ void addFunctionPasses(SILPassPipelinePlan &P,
408408
if (P.getOptions().EnableOSSAModules) {
409409
// We earlier eliminated ownership if we are not compiling the stdlib. Now
410410
// handle the stdlib functions, re-simplifying, eliminating ARC as we do.
411-
if (!P.getOptions().DisableCopyPropagation) {
411+
if (P.getOptions().CopyPropagation != CopyPropagationOption::Off) {
412412
P.addCopyPropagation();
413413
}
414414
P.addSemanticARCOpts();
@@ -432,7 +432,7 @@ void addFunctionPasses(SILPassPipelinePlan &P,
432432

433433
// Clean up Semantic ARC before we perform additional post-inliner opts.
434434
if (P.getOptions().EnableOSSAModules) {
435-
if (!P.getOptions().DisableCopyPropagation) {
435+
if (P.getOptions().CopyPropagation != CopyPropagationOption::Off) {
436436
P.addCopyPropagation();
437437
}
438438
P.addSemanticARCOpts();
@@ -499,7 +499,7 @@ void addFunctionPasses(SILPassPipelinePlan &P,
499499

500500
// Run a final round of ARC opts when ownership is enabled.
501501
if (P.getOptions().EnableOSSAModules) {
502-
if (!P.getOptions().DisableCopyPropagation) {
502+
if (P.getOptions().CopyPropagation != CopyPropagationOption::Off) {
503503
P.addCopyPropagation();
504504
}
505505
P.addSemanticARCOpts();
@@ -535,7 +535,7 @@ static void addPerfEarlyModulePassPipeline(SILPassPipelinePlan &P) {
535535
// Cleanup after SILGen: remove trivial copies to temporaries.
536536
P.addTempRValueOpt();
537537
// Cleanup after SILGen: remove unneeded borrows/copies.
538-
if (P.getOptions().EnableCopyPropagation) {
538+
if (P.getOptions().CopyPropagation == CopyPropagationOption::On) {
539539
P.addCopyPropagation();
540540
}
541541
P.addSemanticARCOpts();
@@ -853,7 +853,7 @@ SILPassPipelinePlan::getPerformancePassPipeline(const SILOptions &Options) {
853853
// Run one last copy propagation/semantic arc opts run before serialization/us
854854
// lowering ownership.
855855
if (P.getOptions().EnableOSSAModules) {
856-
if (!P.getOptions().DisableCopyPropagation) {
856+
if (P.getOptions().CopyPropagation != CopyPropagationOption::Off) {
857857
P.addCopyPropagation();
858858
}
859859
P.addSemanticARCOpts();
@@ -905,7 +905,7 @@ SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
905905
P.startPipeline("non-Diagnostic Enabling Mandatory Optimizations");
906906
P.addForEachLoopUnroll();
907907
P.addMandatoryCombine();
908-
if (P.getOptions().EnableCopyPropagation) {
908+
if (P.getOptions().CopyPropagation == CopyPropagationOption::On) {
909909
// MandatoryCopyPropagation should only be run at -Onone, not -O.
910910
P.addMandatoryCopyPropagation();
911911
}

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,11 @@ class SILCombine : public SILFunctionTransform {
537537
auto *CHA = PM->getAnalysis<ClassHierarchyAnalysis>();
538538
auto *NLABA = PM->getAnalysis<NonLocalAccessBlockAnalysis>();
539539

540-
bool enableCopyPropagation = getOptions().EnableCopyPropagation;
540+
bool enableCopyPropagation =
541+
getOptions().CopyPropagation == CopyPropagationOption::On;
541542
if (getOptions().EnableOSSAModules) {
542-
enableCopyPropagation = !getOptions().DisableCopyPropagation;
543+
enableCopyPropagation =
544+
getOptions().CopyPropagation != CopyPropagationOption::Off;
543545
}
544546

545547
SILOptFunctionBuilder FuncBuilder(*this);

tools/sil-opt/SILOpt.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,14 @@ int main(int argc, char **argv) {
529529
fprintf(stderr, "Error! Cannot specify both -enable-copy-propagation "
530530
"and -disable-copy-propagation.");
531531
exit(-1);
532+
} else if (EnableCopyPropagation && !DisableCopyPropagation) {
533+
SILOpts.CopyPropagation = CopyPropagationOption::On;
534+
} else if (!EnableCopyPropagation && DisableCopyPropagation) {
535+
SILOpts.CopyPropagation = CopyPropagationOption::Off;
536+
} else /*if (!EnableCopyPropagation && !DisableCopyPropagation)*/ {
537+
SILOpts.CopyPropagation = CopyPropagationOption::RequestedPassesOnly;
532538
}
533539

534-
SILOpts.EnableCopyPropagation = EnableCopyPropagation;
535-
SILOpts.DisableCopyPropagation = DisableCopyPropagation;
536-
537540
if (EnableCopyPropagation)
538541
SILOpts.LexicalLifetimes = LexicalLifetimesOption::On;
539542
if (DisableCopyPropagation)

0 commit comments

Comments
 (0)