Skip to content

Commit 3645bec

Browse files
committed
PassManager: infrastructure to disable or enable a specific instruction simplification
* for testing: add the option `-simplify-instruction=<instruction-name>` to only run simplification passes for that instruction type * on the swift side, add `Options.enableSimplification`
1 parent 72d3f8e commit 3645bec

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Options.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SIL
1314
import OptimizerBridging
1415

1516
struct Options {
@@ -22,4 +23,8 @@ struct Options {
2223
var enableMoveInoutStackProtection: Bool {
2324
SILOptions_enableMoveInoutStackProtection(_bridged) != 0
2425
}
26+
27+
func enableSimplification(for inst: Instruction) -> Bool {
28+
SILOptions_enableSimplificationFor(inst.bridged)
29+
}
2530
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ PassContext_loadFunction(BridgedPassContext context, llvm::StringRef name);
204204

205205
SwiftInt SILOptions_enableStackProtection(BridgedPassContext context);
206206
SwiftInt SILOptions_enableMoveInoutStackProtection(BridgedPassContext context);
207+
bool SILOptions_enableSimplificationFor(BridgedInstruction inst);
207208

208209
BridgedValue SILUndef_get(BridgedType type, BridgedPassContext context);
209210

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ class SILPassManager {
379379
SILTransform *trans);
380380

381381
static bool isPassDisabled(StringRef passName);
382+
static bool isInstructionPassDisabled(StringRef instName);
382383
static bool disablePassesForFunction(SILFunction *function);
383384

384385
private:

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ llvm::cl::opt<bool> SILForceVerifyAll(
140140
llvm::cl::desc("For all passes, precompute analyses before the pass and "
141141
"verify analyses after the pass"));
142142

143+
llvm::cl::list<std::string>
144+
SimplifyInstructionTest("simplify-instruction", llvm::cl::CommaSeparated,
145+
llvm::cl::desc("Simplify instruction of specified kind(s)"));
146+
143147
static llvm::ManagedStatic<std::vector<unsigned>> DebugPassNumbers;
144148

145149
namespace {
@@ -487,6 +491,18 @@ bool SILPassManager::isPassDisabled(StringRef passName) {
487491
return false;
488492
}
489493

494+
bool SILPassManager::isInstructionPassDisabled(StringRef instName) {
495+
StringRef prefix("simplify-");
496+
for (const std::string &namePattern : SILDisablePass) {
497+
StringRef pattern(namePattern);
498+
if (pattern.startswith(prefix) && pattern.endswith(instName) &&
499+
pattern.size() == prefix.size() + instName.size()) {
500+
return true;
501+
}
502+
}
503+
return false;
504+
}
505+
490506
bool SILPassManager::disablePassesForFunction(SILFunction *function) {
491507
if (SILDisablePassOnlyFun.empty())
492508
return true;
@@ -1706,6 +1722,26 @@ SwiftInt SILOptions_enableMoveInoutStackProtection(BridgedPassContext context) {
17061722
return mod->getOptions().EnableMoveInoutStackProtection;
17071723
}
17081724

1725+
bool SILOptions_enableSimplificationFor(BridgedInstruction inst) {
1726+
// Fast-path check.
1727+
if (SimplifyInstructionTest.empty() && SILDisablePass.empty())
1728+
return true;
1729+
1730+
StringRef instName = getSILInstructionName(castToInst(inst)->getKind());
1731+
1732+
if (SILPassManager::isInstructionPassDisabled(instName))
1733+
return false;
1734+
1735+
if (SimplifyInstructionTest.empty())
1736+
return true;
1737+
1738+
for (const std::string &testName : SimplifyInstructionTest) {
1739+
if (testName == instName)
1740+
return true;
1741+
}
1742+
return false;
1743+
}
1744+
17091745
BridgedValue SILUndef_get(BridgedType type, BridgedPassContext context) {
17101746
SILUndef *undef = SILUndef::get(castToSILType(type),
17111747
*castToPassInvocation(context)->getFunction());

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ SILInstruction *SILCombiner::visit##INST(INST *inst) { \
558558
return nullptr; \
559559
} \
560560
} \
561-
passDisabled = SILPassManager::isPassDisabled(#INST) || \
562-
SILPassManager::isPassDisabled("sil-combine-" #INST); \
561+
StringRef instName = getSILInstructionName(SILInstructionKind::INST); \
562+
passDisabled = SILPassManager::isInstructionPassDisabled(instName); \
563563
} \
564564
if (passDisabled && \
565565
SILPassManager::disablePassesForFunction(inst->getFunction())) { \

0 commit comments

Comments
 (0)