Skip to content

Commit 79e9b9a

Browse files
committed
SIL optimizer: add the ability to disable swift instruction passes with the -sil-disable-pass option.
1 parent 97eb38e commit 79e9b9a

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ class SILPassManager {
352352

353353
void executePassPipelinePlan(const SILPassPipelinePlan &Plan);
354354

355+
static bool isPassDisabled(StringRef passName);
356+
static bool disablePassesForFunction(SILFunction *function);
357+
355358
private:
356359
void execute();
357360

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,6 @@ static bool doPrintAfter(SILTransform *T, SILFunction *F, bool PassChangedSIL) {
224224
return PassChangedSIL && (SILPrintAll || !functionSelectionEmpty());
225225
}
226226

227-
static bool isDisabled(SILTransform *T, SILFunction *F = nullptr) {
228-
if (!SILDisablePassOnlyFun.empty() && F &&
229-
SILDisablePassOnlyFun.end() == std::find(SILDisablePassOnlyFun.begin(),
230-
SILDisablePassOnlyFun.end(),
231-
F->getName()))
232-
return false;
233-
234-
for (const std::string &NamePattern : SILDisablePass) {
235-
if (T->getTag().contains(NamePattern) || T->getID().contains(NamePattern)) {
236-
return true;
237-
}
238-
}
239-
return false;
240-
}
241-
242227
static void printModule(SILModule *Mod, bool EmitVerboseSIL) {
243228
if (functionSelectionEmpty()) {
244229
Mod->dump();
@@ -401,6 +386,35 @@ bool SILPassManager::isMandatoryFunctionPass(SILFunctionTransform *sft) {
401386
sft->getPassKind() == PassKind::OwnershipModelEliminator;
402387
}
403388

389+
static bool isDisabled(SILTransform *T, SILFunction *F = nullptr) {
390+
if (SILDisablePass.empty())
391+
return false;
392+
393+
if (SILPassManager::isPassDisabled(T->getTag()) ||
394+
SILPassManager::isPassDisabled(T->getID())) {
395+
if (F && !SILPassManager::disablePassesForFunction(F))
396+
return false;
397+
return true;
398+
}
399+
return false;
400+
}
401+
402+
bool SILPassManager::isPassDisabled(StringRef passName) {
403+
for (const std::string &namePattern : SILDisablePass) {
404+
if (passName.contains(namePattern))
405+
return true;
406+
}
407+
return false;
408+
}
409+
410+
bool SILPassManager::disablePassesForFunction(SILFunction *function) {
411+
if (SILDisablePassOnlyFun.empty())
412+
return true;
413+
414+
return std::find(SILDisablePassOnlyFun.begin(), SILDisablePassOnlyFun.end(),
415+
function->getName()) != SILDisablePassOnlyFun.end();
416+
}
417+
404418
void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
405419

406420
assert(analysesUnlocked() && "Expected all analyses to be unlocked!");

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,17 +491,23 @@ void SILCombine_registerInstructionPass(BridgedStringRef name,
491491
SILInstruction *SILCombiner::visit##INST(INST *inst) { \
492492
static BridgedInstructionPassRunFn runFunction = nullptr; \
493493
static bool runFunctionSet = false; \
494+
static bool passDisabled = false; \
494495
if (!runFunctionSet) { \
495496
runFunction = swiftInstPasses[TAG]; \
496497
if (!runFunction && passesRegistered) { \
497498
llvm::errs() << "Swift pass " << TAG << " is not registered\n"; \
498499
abort(); \
499500
} \
501+
passDisabled = SILPassManager::isPassDisabled(TAG); \
500502
runFunctionSet = true; \
501503
} \
502504
if (!runFunction) { \
503505
LEGACY_RUN; \
504506
} \
507+
if (passDisabled && \
508+
SILPassManager::disablePassesForFunction(inst->getFunction())) { \
509+
return nullptr; \
510+
} \
505511
runSwiftInstructionPass(inst, runFunction); \
506512
return nullptr; \
507513
} \
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-sil-opt -sil-combine %s | %FileCheck %s --check-prefix=CHECK_ENABLED
2+
// RUN: %target-sil-opt -sil-combine %s -sil-disable-pass=SILCombine | %FileCheck %s --check-prefix=CHECK_DISABLED
3+
// RUN: %target-sil-opt -sil-combine %s -sil-disable-pass=SILCombine -sil-disable-pass-only-function=test | %FileCheck %s --check-prefix=CHECK_DISABLED
4+
// RUN: %target-sil-opt -sil-combine %s -sil-disable-pass=SILCombine -sil-disable-pass-only-function=other | %FileCheck %s --check-prefix=CHECK_ENABLED
5+
// RUN: %target-sil-opt -sil-combine %s -sil-disable-pass=simplify-begin_cow_mutation | %FileCheck %s --check-prefix=CHECK_DISABLED
6+
// RUN: %target-sil-opt -sil-combine %s -sil-disable-pass=simplify-begin_cow_mutation -sil-disable-pass-only-function=test | %FileCheck %s --check-prefix=CHECK_DISABLED
7+
// RUN: %target-sil-opt -sil-combine %s -sil-disable-pass=simplify-begin_cow_mutation -sil-disable-pass-only-function=other | %FileCheck %s --check-prefix=CHECK_ENABLED
8+
9+
// REQUIRES: swift_in_compiler
10+
11+
import Builtin
12+
13+
class Buffer {}
14+
15+
// CHECK_ENABLED-NOT: begin_cow_mutation
16+
// CHECK_ENABLED-NOT: end_cow_mutation
17+
// CHECK_DISABLED: begin_cow_mutation
18+
// CHECK_DISABLED: end_cow_mutation
19+
sil [ossa] @test : $@convention(thin) (@owned Buffer) -> @owned Buffer {
20+
bb0(%0 : @owned $Buffer):
21+
(%u, %b) = begin_cow_mutation %0 : $Buffer
22+
%e = end_cow_mutation %b : $Buffer
23+
return %e : $Buffer
24+
}

0 commit comments

Comments
 (0)