Skip to content

Commit cead6a5

Browse files
committed
Add an OptimizedMandatoryCombine pass variant.
It's against the principles of pass design to check the driver mode within the pass. A pass always needs to do the same thing regardless of where it runs in the pass pipeline. It also needs to be possible to test passes in isolation.
1 parent ab42f75 commit cead6a5

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ PASS(ForEachLoopUnroll, "for-each-loop-unroll",
357357
"Unroll forEach loops over array literals")
358358
PASS(MandatoryCombine, "mandatory-combine",
359359
"Perform mandatory peephole combines")
360+
PASS(OptimizedMandatoryCombine, "optimized-mandatory-combine",
361+
"Perform -O level mandatory peephole combines")
360362
PASS(BugReducerTester, "bug-reducer-tester",
361363
"sil-bug-reducer Tool Testing by Asserting on a Sentinel Function")
362364
PASS(OptRemarkGenerator, "sil-opt-remark-generator",

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ namespace {
114114
class MandatoryCombiner final
115115
: public SILInstructionVisitor<MandatoryCombiner, SILInstruction *> {
116116

117+
bool compilingWithOptimization;
118+
117119
using Worklist = SmallSILInstructionWorklist<256>;
118120

119121
/// The list of instructions remaining to visit, perhaps to combine.
@@ -135,9 +137,11 @@ class MandatoryCombiner final
135137
DeadEndBlocks &deadEndBlocks;
136138

137139
public:
138-
MandatoryCombiner(SmallVectorImpl<SILInstruction *> &createdInstructions,
140+
MandatoryCombiner(bool optimized,
141+
SmallVectorImpl<SILInstruction *> &createdInstructions,
139142
DeadEndBlocks &deadEndBlocks)
140-
: worklist("MC"), madeChange(false), iteration(0),
143+
: compilingWithOptimization(optimized), worklist("MC"), madeChange(false),
144+
iteration(0),
141145
instModCallbacks(
142146
[&](SILInstruction *instruction) {
143147
worklist.erase(instruction);
@@ -210,8 +214,6 @@ void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) {
210214
blockAlreadyAddedToWorklist.insert(firstBlock);
211215
}
212216

213-
bool compilingWithOptimization = function.getEffectiveOptimizationMode() !=
214-
OptimizationMode::NoOptimization;
215217
while (!blockWorklist.empty()) {
216218
auto *block = blockWorklist.pop_back_val();
217219

@@ -249,9 +251,6 @@ bool MandatoryCombiner::doOneIteration(SILFunction &function,
249251
addReachableCodeToWorklist(function);
250252
MandatoryCombineCanonicalize mcCanonicialize(worklist, deadEndBlocks);
251253

252-
bool compilingWithOptimization = function.getEffectiveOptimizationMode() !=
253-
OptimizationMode::NoOptimization;
254-
255254
while (!worklist.isEmpty()) {
256255
auto *instruction = worklist.pop_back_val();
257256
if (instruction == nullptr) {
@@ -383,9 +382,12 @@ SILInstruction *MandatoryCombiner::visitApplyInst(ApplyInst *instruction) {
383382
namespace {
384383

385384
class MandatoryCombine final : public SILFunctionTransform {
386-
385+
bool optimized;
387386
SmallVector<SILInstruction *, 64> createdInstructions;
388387

388+
public:
389+
MandatoryCombine(bool optimized) : optimized(optimized) {}
390+
389391
void run() override {
390392
auto *function = getFunction();
391393

@@ -396,14 +398,15 @@ class MandatoryCombine final : public SILFunctionTransform {
396398
}
397399

398400
DeadEndBlocks deadEndBlocks(function);
399-
MandatoryCombiner combiner(createdInstructions, deadEndBlocks);
401+
MandatoryCombiner combiner(optimized, createdInstructions, deadEndBlocks);
400402
bool madeChange = combiner.runOnFunction(*function);
401403

402404
if (madeChange) {
403405
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
404406
}
405407
}
406408

409+
protected:
407410
void handleDeleteNotification(SILNode *node) override {
408411
// Remove instructions that were both created and deleted from the list of
409412
// created instructions which will eventually be added to the worklist.
@@ -426,4 +429,10 @@ class MandatoryCombine final : public SILFunctionTransform {
426429

427430
} // end anonymous namespace
428431

429-
SILTransform *swift::createMandatoryCombine() { return new MandatoryCombine(); }
432+
SILTransform *swift::createMandatoryCombine() {
433+
return new MandatoryCombine(/*optimized*/ false);
434+
}
435+
436+
SILTransform *swift::createOptimizedMandatoryCombine() {
437+
return new MandatoryCombine(/*optimized*/ true);
438+
}

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ static void addPrepareOptimizationsPipeline(SILPassPipelinePlan &P) {
433433
#endif
434434

435435
P.addForEachLoopUnroll();
436-
P.addMandatoryCombine();
436+
P.addOptimizedMandatoryCombine();
437437
P.addAccessMarkerElimination();
438438
}
439439

test/SILOptimizer/ossa_rauw_tests.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt -enable-sil-verify-all -mandatory-combine -sil-mandatory-combine-enable-canon-and-simple-dce -semantic-arc-opts %s | %FileCheck %s
1+
// RUN: %target-sil-opt -enable-sil-verify-all -optimized-mandatory-combine -sil-mandatory-combine-enable-canon-and-simple-dce -semantic-arc-opts %s | %FileCheck %s
22

33
// Make sure that we can perform all of these RAUW without producing ARC traffic
44
// that semantic arc opts can't eliminate.

0 commit comments

Comments
 (0)