Skip to content

Commit fccd75e

Browse files
committed
[sil-passmanager] Stash a SILOptions reference in SILPassPipeline.
This normalizes the creation of pass pipelines by ensuring that all pass pipelines take a SILOption instead of only some. It also makes it so that we do not need to propagate options through various pipeline creation helpers.
1 parent c12e556 commit fccd75e

File tree

8 files changed

+57
-58
lines changed

8 files changed

+57
-58
lines changed

include/swift/SILOptimizer/PassManager/PassPipeline.def

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,19 @@
1717
/// PASSPIPELINE(NAME, DESCRIPTION)
1818
///
1919
/// A pipeline with Name NAME and description DESCRIPTION. PassPipelinePlan
20-
/// constructor is assumed to not take a SILOptions value.
20+
/// constructor is assumed to take a SILOptions value.
2121
#ifndef PASSPIPELINE
2222
#define PASSPIPELINE(NAME, DESCRIPTION)
2323
#endif
2424

25-
/// PASSPIPELINE_WITH_OPTIONS(NAME, DESCRIPTION)
26-
///
27-
/// A pipeline with Name NAME and description DESCRIPTION. PassPipelinePlan
28-
/// constructor is assumed to take a SILOptions value.
29-
#ifndef PASSPIPELINE_WITH_OPTIONS
30-
#define PASSPIPELINE_WITH_OPTIONS(NAME, DESCRIPTION) PASSPIPELINE(NAME, DESCRIPTION)
31-
#endif
32-
33-
PASSPIPELINE_WITH_OPTIONS(Diagnostic, "Guaranteed Passes")
25+
PASSPIPELINE(Diagnostic, "Guaranteed Passes")
3426
PASSPIPELINE(OwnershipEliminator, "Utility pass to just run the ownership eliminator pass")
35-
PASSPIPELINE_WITH_OPTIONS(SILOptPrepare, "Passes that prepare SIL for -O")
36-
PASSPIPELINE_WITH_OPTIONS(Performance, "Passes run at -O")
27+
PASSPIPELINE(SILOptPrepare, "Passes that prepare SIL for -O")
28+
PASSPIPELINE(Performance, "Passes run at -O")
3729
PASSPIPELINE(Onone, "Passes run at -Onone")
3830
PASSPIPELINE(InstCount, "Utility pipeline to just run the inst count pass")
3931
PASSPIPELINE(Lowering, "SIL Address Lowering")
40-
PASSPIPELINE_WITH_OPTIONS(IRGenPrepare, "Pipeline to run during IRGen")
32+
PASSPIPELINE(IRGenPrepare, "Pipeline to run during IRGen")
4133

4234
#undef PASSPIPELINE_WITH_OPTIONS
4335
#undef PASSPIPELINE

include/swift/SILOptimizer/PassManager/PassPipeline.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@ enum class PassPipelineKind {
4141
};
4242

4343
class SILPassPipelinePlan final {
44+
const SILOptions &Options;
4445
std::vector<PassKind> Kinds;
4546
std::vector<SILPassPipeline> PipelineStages;
4647

4748
public:
48-
SILPassPipelinePlan() = default;
49+
SILPassPipelinePlan(const SILOptions &Options)
50+
: Options(Options), Kinds(), PipelineStages() {}
51+
4952
~SILPassPipelinePlan() = default;
5053
SILPassPipelinePlan(const SILPassPipelinePlan &) = default;
5154

55+
const SILOptions &getOptions() const { return Options; }
56+
5257
// Each pass gets its own add-function.
5358
#define PASS(ID, TAG, NAME) \
5459
void add##ID() { \
@@ -60,13 +65,13 @@ class SILPassPipelinePlan final {
6065
void addPasses(ArrayRef<PassKind> PassKinds);
6166

6267
#define PASSPIPELINE(NAME, DESCRIPTION) \
63-
static SILPassPipelinePlan get##NAME##PassPipeline();
64-
#define PASSPIPELINE_WITH_OPTIONS(NAME, DESCRIPTION) \
6568
static SILPassPipelinePlan get##NAME##PassPipeline(const SILOptions &Options);
6669
#include "swift/SILOptimizer/PassManager/PassPipeline.def"
6770

68-
static SILPassPipelinePlan getPassPipelineForKinds(ArrayRef<PassKind> Kinds);
69-
static SILPassPipelinePlan getPassPipelineFromFile(StringRef Filename);
71+
static SILPassPipelinePlan getPassPipelineForKinds(const SILOptions &Options,
72+
ArrayRef<PassKind> Kinds);
73+
static SILPassPipelinePlan getPassPipelineFromFile(const SILOptions &Options,
74+
StringRef Filename);
7075

7176
/// Our general format is as follows:
7277
///

lib/SILOptimizer/IPO/DeadFunctionElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,5 +753,5 @@ void swift::performSILDeadFunctionElimination(SILModule *M) {
753753
SILPassManager PM(M);
754754
llvm::SmallVector<PassKind, 1> Pass = {PassKind::DeadFunctionElimination};
755755
PM.executePassPipelinePlan(
756-
SILPassPipelinePlan::getPassPipelineForKinds(Pass));
756+
SILPassPipelinePlan::getPassPipelineForKinds(M->getOptions(), Pass));
757757
}

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ static void addDefiniteInitialization(SILPassPipelinePlan &P) {
7979
P.addRawSILInstLowering();
8080
}
8181

82-
static void addMandatoryOptPipeline(SILPassPipelinePlan &P,
83-
const SILOptions &Options) {
82+
static void addMandatoryOptPipeline(SILPassPipelinePlan &P) {
8483
P.startPipeline("Guaranteed Passes");
8584
P.addDiagnoseStaticExclusivity();
8685
P.addCapturePromotion();
@@ -99,6 +98,7 @@ static void addMandatoryOptPipeline(SILPassPipelinePlan &P,
9998
// pass. This will happen when OSSA is no longer eliminated before the
10099
// optimizer pipeline is run implying we can put a pass that requires OSSA
101100
// there.
101+
const auto &Options = P.getOptions();
102102
if (Options.EnableMandatorySemanticARCOpts && Options.shouldOptimize()) {
103103
P.addSemanticARCOpts();
104104
}
@@ -130,7 +130,7 @@ static void addMandatoryOptPipeline(SILPassPipelinePlan &P,
130130

131131
SILPassPipelinePlan
132132
SILPassPipelinePlan::getDiagnosticPassPipeline(const SILOptions &Options) {
133-
SILPassPipelinePlan P;
133+
SILPassPipelinePlan P(Options);
134134

135135
if (SILViewSILGenCFG) {
136136
addCFGPrinterPipeline(P, "SIL View SILGen CFG");
@@ -145,7 +145,7 @@ SILPassPipelinePlan::getDiagnosticPassPipeline(const SILOptions &Options) {
145145
}
146146

147147
// Otherwise run the rest of diagnostics.
148-
addMandatoryOptPipeline(P, Options);
148+
addMandatoryOptPipeline(P);
149149

150150
if (SILViewGuaranteedCFG) {
151151
addCFGPrinterPipeline(P, "SIL View Guaranteed CFG");
@@ -157,8 +157,9 @@ SILPassPipelinePlan::getDiagnosticPassPipeline(const SILOptions &Options) {
157157
// Ownership Eliminator Pipeline
158158
//===----------------------------------------------------------------------===//
159159

160-
SILPassPipelinePlan SILPassPipelinePlan::getOwnershipEliminatorPassPipeline() {
161-
SILPassPipelinePlan P;
160+
SILPassPipelinePlan SILPassPipelinePlan::getOwnershipEliminatorPassPipeline(
161+
const SILOptions &Options) {
162+
SILPassPipelinePlan P(Options);
162163
addOwnershipModelEliminatorPipeline(P);
163164
return P;
164165
}
@@ -240,8 +241,7 @@ void addHighLevelLoopOptPasses(SILPassPipelinePlan &P) {
240241
}
241242

242243
// Perform classic SSA optimizations.
243-
void addSSAPasses(SILPassPipelinePlan &P, OptimizationLevelKind OpLevel,
244-
bool stopAfterSerialization = false) {
244+
void addSSAPasses(SILPassPipelinePlan &P, OptimizationLevelKind OpLevel) {
245245
// Promote box allocations to stack allocations.
246246
P.addAllocBoxToStack();
247247

@@ -294,7 +294,7 @@ void addSSAPasses(SILPassPipelinePlan &P, OptimizationLevelKind OpLevel,
294294
// which reduces the ability of the compiler to optimize clients
295295
// importing this module.
296296
P.addSerializeSILPass();
297-
if (stopAfterSerialization)
297+
if (P.getOptions().StopOptimizationAfterSerialization)
298298
return;
299299

300300
// Does inline semantics-functions (except "availability"), but not
@@ -370,6 +370,7 @@ static void addPerfEarlyModulePassPipeline(SILPassPipelinePlan &P) {
370370
// Get rid of apparently dead functions as soon as possible so that
371371
// we do not spend time optimizing them.
372372
P.addDeadFunctionElimination();
373+
373374
// Start by cloning functions from stdlib.
374375
P.addPerformanceSILLinker();
375376

@@ -399,11 +400,10 @@ static void addMidModulePassesStackPromotePassPipeline(SILPassPipelinePlan &P) {
399400
P.addStackPromotion();
400401
}
401402

402-
static bool addMidLevelPassPipeline(SILPassPipelinePlan &P,
403-
bool stopAfterSerialization) {
403+
static bool addMidLevelPassPipeline(SILPassPipelinePlan &P) {
404404
P.startPipeline("MidLevel");
405-
addSSAPasses(P, OptimizationLevelKind::MidLevel, stopAfterSerialization);
406-
if (stopAfterSerialization)
405+
addSSAPasses(P, OptimizationLevelKind::MidLevel);
406+
if (P.getOptions().StopOptimizationAfterSerialization)
407407
return true;
408408

409409
// Specialize partially applied functions with dead arguments as a preparation
@@ -538,8 +538,8 @@ static void addSILDebugInfoGeneratorPipeline(SILPassPipelinePlan &P) {
538538
/// Mandatory IRGen preparation. It is the caller's job to set the set stage to
539539
/// "lowered" after running this pipeline.
540540
SILPassPipelinePlan
541-
SILPassPipelinePlan::getLoweringPassPipeline() {
542-
SILPassPipelinePlan P;
541+
SILPassPipelinePlan::getLoweringPassPipeline(const SILOptions &Options) {
542+
SILPassPipelinePlan P(Options);
543543
P.startPipeline("Address Lowering");
544544
P.addIRGenPrepare();
545545
P.addAddressLowering();
@@ -549,7 +549,7 @@ SILPassPipelinePlan::getLoweringPassPipeline() {
549549

550550
SILPassPipelinePlan
551551
SILPassPipelinePlan::getIRGenPreparePassPipeline(const SILOptions &Options) {
552-
SILPassPipelinePlan P;
552+
SILPassPipelinePlan P(Options);
553553
P.startPipeline("IRGen Preparation");
554554
// Insert SIL passes to run during IRGen.
555555
// Hoist generic alloc_stack instructions to the entry block to enable better
@@ -563,7 +563,7 @@ SILPassPipelinePlan::getIRGenPreparePassPipeline(const SILOptions &Options) {
563563

564564
SILPassPipelinePlan
565565
SILPassPipelinePlan::getSILOptPreparePassPipeline(const SILOptions &Options) {
566-
SILPassPipelinePlan P;
566+
SILPassPipelinePlan P(Options);
567567

568568
if (Options.DebugSerialization) {
569569
addPerfDebugSerializationPipeline(P);
@@ -578,7 +578,7 @@ SILPassPipelinePlan::getSILOptPreparePassPipeline(const SILOptions &Options) {
578578

579579
SILPassPipelinePlan
580580
SILPassPipelinePlan::getPerformancePassPipeline(const SILOptions &Options) {
581-
SILPassPipelinePlan P;
581+
SILPassPipelinePlan P(Options);
582582

583583
if (Options.DebugSerialization) {
584584
addPerfDebugSerializationPipeline(P);
@@ -594,7 +594,7 @@ SILPassPipelinePlan::getPerformancePassPipeline(const SILOptions &Options) {
594594
addMidModulePassesStackPromotePassPipeline(P);
595595

596596
// Run an iteration of the mid-level SSA passes.
597-
if (addMidLevelPassPipeline(P, Options.StopOptimizationAfterSerialization))
597+
if (addMidLevelPassPipeline(P))
598598
return P;
599599

600600
// Perform optimizations that specialize.
@@ -624,8 +624,9 @@ SILPassPipelinePlan::getPerformancePassPipeline(const SILOptions &Options) {
624624
// Onone Pass Pipeline
625625
//===----------------------------------------------------------------------===//
626626

627-
SILPassPipelinePlan SILPassPipelinePlan::getOnonePassPipeline() {
628-
SILPassPipelinePlan P;
627+
SILPassPipelinePlan
628+
SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
629+
SILPassPipelinePlan P(Options);
629630

630631
// First specialize user-code.
631632
P.startPipeline("Prespecialization");
@@ -649,8 +650,9 @@ SILPassPipelinePlan SILPassPipelinePlan::getOnonePassPipeline() {
649650
// Inst Count Pass Pipeline
650651
//===----------------------------------------------------------------------===//
651652

652-
SILPassPipelinePlan SILPassPipelinePlan::getInstCountPassPipeline() {
653-
SILPassPipelinePlan P;
653+
SILPassPipelinePlan
654+
SILPassPipelinePlan::getInstCountPassPipeline(const SILOptions &Options) {
655+
SILPassPipelinePlan P(Options);
654656
P.startPipeline("Inst Count");
655657
P.addInstCount();
656658
return P;
@@ -680,8 +682,9 @@ void SILPassPipelinePlan::addPasses(ArrayRef<PassKind> PassKinds) {
680682
}
681683

682684
SILPassPipelinePlan
683-
SILPassPipelinePlan::getPassPipelineForKinds(ArrayRef<PassKind> PassKinds) {
684-
SILPassPipelinePlan P;
685+
SILPassPipelinePlan::getPassPipelineForKinds(const SILOptions &Options,
686+
ArrayRef<PassKind> PassKinds) {
687+
SILPassPipelinePlan P(Options);
685688
P.startPipeline("Pass List Pipeline");
686689
P.addPasses(PassKinds);
687690
return P;
@@ -717,7 +720,8 @@ void SILPassPipelinePlan::print(llvm::raw_ostream &os) {
717720
}
718721

719722
SILPassPipelinePlan
720-
SILPassPipelinePlan::getPassPipelineFromFile(StringRef Filename) {
723+
SILPassPipelinePlan::getPassPipelineFromFile(const SILOptions &Options,
724+
StringRef Filename) {
721725
namespace yaml = llvm::yaml;
722726
LLVM_DEBUG(llvm::dbgs() << "Parsing Pass Pipeline from " << Filename << "\n");
723727

@@ -736,7 +740,7 @@ SILPassPipelinePlan::getPassPipelineFromFile(StringRef Filename) {
736740
yaml::Node *N = DI->getRoot();
737741
assert(N && "Failed to find a root");
738742

739-
SILPassPipelinePlan P;
743+
SILPassPipelinePlan P(Options);
740744

741745
auto *RootList = cast<yaml::SequenceNode>(N);
742746
llvm::SmallVector<PassKind, 32> Passes;

lib/SILOptimizer/PassManager/Passes.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ bool swift::runSILOwnershipEliminatorPass(SILModule &Module) {
7878

7979
SILPassManager PM(&Module);
8080
PM.executePassPipelinePlan(
81-
SILPassPipelinePlan::getOwnershipEliminatorPassPipeline());
81+
SILPassPipelinePlan::getOwnershipEliminatorPassPipeline(
82+
Module.getOptions()));
8283

8384
return Ctx.hadError();
8485
}
@@ -122,7 +123,8 @@ void swift::runSILPassesForOnone(SILModule &Module) {
122123
// We want to run the Onone passes also for function which have an explicit
123124
// Onone attribute.
124125
SILPassManager PM(&Module, "Onone", /*isMandatoryPipeline=*/ true);
125-
PM.executePassPipelinePlan(SILPassPipelinePlan::getOnonePassPipeline());
126+
PM.executePassPipelinePlan(
127+
SILPassPipelinePlan::getOnonePassPipeline(Module.getOptions()));
126128

127129
// Verify the module, if required.
128130
if (Module.getOptions().VerifyAll)
@@ -136,7 +138,7 @@ void swift::runSILOptimizationPassesWithFileSpecification(SILModule &M,
136138
StringRef Filename) {
137139
SILPassManager PM(&M);
138140
PM.executePassPipelinePlan(
139-
SILPassPipelinePlan::getPassPipelineFromFile(Filename));
141+
SILPassPipelinePlan::getPassPipelineFromFile(M.getOptions(), Filename));
140142
}
141143

142144
/// Get the Pass ID enum value from an ID string.
@@ -187,7 +189,8 @@ StringRef swift::PassKindTag(PassKind Kind) {
187189
// same stage of lowering.
188190
void swift::runSILLoweringPasses(SILModule &Module) {
189191
SILPassManager PM(&Module, "LoweringPasses", /*isMandatoryPipeline=*/ true);
190-
PM.executePassPipelinePlan(SILPassPipelinePlan::getLoweringPassPipeline());
192+
PM.executePassPipelinePlan(
193+
SILPassPipelinePlan::getLoweringPassPipeline(Module.getOptions()));
191194

192195
assert(Module.getStage() == SILStage::Lowered);
193196
}

lib/SILOptimizer/UtilityPasses/InstCount.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,5 @@ void swift::performSILInstCountIfNeeded(SILModule *M) {
157157
return;
158158
SILPassManager PrinterPM(M);
159159
PrinterPM.executePassPipelinePlan(
160-
SILPassPipelinePlan::getInstCountPassPipeline());
160+
SILPassPipelinePlan::getInstCountPassPipeline(M->getOptions()));
161161
}

tools/sil-opt/SILOpt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ static void runCommandLineSelectedPasses(SILModule *Module,
257257
#include "swift/SILOptimizer/PassManager/Passes.def"
258258
}
259259

260-
PM.executePassPipelinePlan(
261-
SILPassPipelinePlan::getPassPipelineForKinds(Passes));
260+
PM.executePassPipelinePlan(SILPassPipelinePlan::getPassPipelineForKinds(
261+
Module->getOptions(), Passes));
262262

263263
if (Module->getOptions().VerifyAll)
264264
Module->verify();

tools/sil-passpipeline-dumper/SILPassPipelineDumper.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ int main(int argc, char **argv) {
5555

5656
switch (PipelineKind) {
5757
#define PASSPIPELINE(NAME, DESCRIPTION) \
58-
case PassPipelineKind::NAME: { \
59-
SILPassPipelinePlan::get##NAME##PassPipeline().print(llvm::outs()); \
60-
break; \
61-
}
62-
#define PASSPIPELINE_WITH_OPTIONS(NAME, DESCRIPTION) \
6358
case PassPipelineKind::NAME: { \
6459
SILPassPipelinePlan::get##NAME##PassPipeline(Opt).print(llvm::outs()); \
6560
break; \

0 commit comments

Comments
 (0)