Skip to content

Commit 74e928b

Browse files
committed
Add -enable-ossa-simplifycfg and -enable-ossa-jumpthread-simplifycfg
as temporary flags to gradually stage in OSSA tests.
1 parent c46acc0 commit 74e928b

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ SILLinkage getSpecializedLinkage(SILFunction *f, SILLinkage linkage);
203203
/// function \p Fn.
204204
bool tryCheckedCastBrJumpThreading(
205205
SILFunction *fn, DominanceInfo *dt,
206-
SmallVectorImpl<SILBasicBlock *> &blocksForWorklist);
206+
SmallVectorImpl<SILBasicBlock *> &blocksForWorklist,
207+
bool EnableOSSARewriteTerminator);
207208

208209
/// A utility for deleting one or more instructions belonging to a function, and
209210
/// cleaning up any dead code resulting from deleting those instructions. Use

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343
#include "llvm/Support/Debug.h"
4444
using namespace swift;
4545

46+
// This is temporarily used for testing until Swift 5.5 branches to reduce risk.
47+
llvm::cl::opt<bool> EnableOSSASimplifyCFG(
48+
"enable-ossa-simplify-cfg",
49+
llvm::cl::desc(
50+
"Enable non-trivial OSSA simplify-cfg and simple jump threading "
51+
"(staging)."));
52+
53+
// This requires new OwnershipOptUtilities which aren't well tested yet.
54+
llvm::cl::opt<bool> EnableOSSARewriteTerminator(
55+
"enable-ossa-rewriteterminator",
56+
llvm::cl::desc(
57+
"Enable OSSA simplify-cfg with non-trivial terminator rewriting "
58+
"(staging)."));
59+
4660
STATISTIC(NumBlocksDeleted, "Number of unreachable blocks removed");
4761
STATISTIC(NumBlocksMerged, "Number of blocks merged together");
4862
STATISTIC(NumJumpThreads, "Number of jumps threaded");
@@ -661,23 +675,16 @@ bool SimplifyCFG::dominatorBasedSimplify(DominanceAnalysis *DA) {
661675
// Get the dominator tree.
662676
DT = DA->get(&Fn);
663677

678+
if (!EnableOSSASimplifyCFG && Fn.hasOwnership())
679+
return false;
680+
664681
// Split all critical edges such that we can move code onto edges. This is
665682
// also required for SSA construction in dominatorBasedSimplifications' jump
666683
// threading. It only splits new critical edges it creates by jump threading.
667684
bool Changed = false;
668685
if (!Fn.hasOwnership() && EnableJumpThread) {
669686
Changed = splitAllCriticalEdges(Fn, DT, nullptr);
670687
}
671-
672-
// TODO: OSSA phi support. Even if all block arguments are trivial,
673-
// jump-threading may require creation of guaranteed phis, which may require
674-
// creation of nested borrow scopes.
675-
if (Fn.hasOwnership()) {
676-
for (auto &BB : Fn)
677-
Changed |= simplifyArgs(&BB);
678-
return Changed;
679-
}
680-
681688
unsigned MaxIter = MaxIterationsOfDominatorBasedSimplify;
682689
SmallVector<SILBasicBlock *, 16> BlocksForWorklist;
683690

@@ -688,7 +695,8 @@ bool SimplifyCFG::dominatorBasedSimplify(DominanceAnalysis *DA) {
688695
// Do dominator based simplification of terminator condition. This does not
689696
// and MUST NOT change the CFG without updating the dominator tree to
690697
// reflect such change.
691-
if (tryCheckedCastBrJumpThreading(&Fn, DT, BlocksForWorklist)) {
698+
if (tryCheckedCastBrJumpThreading(&Fn, DT, BlocksForWorklist,
699+
EnableOSSARewriteTerminator)) {
692700
for (auto BB: BlocksForWorklist)
693701
addToWorklist(BB);
694702

@@ -1029,10 +1037,13 @@ static bool hasInjectedEnumAtEndOfBlock(SILBasicBlock *block, SILValue enumAddr)
10291037
/// tryJumpThreading - Check to see if it looks profitable to duplicate the
10301038
/// destination of an unconditional jump into the bottom of this block.
10311039
bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
1040+
if (!EnableOSSASimplifyCFG && Fn.hasOwnership())
1041+
return false;
1042+
10321043
auto *DestBB = BI->getDestBB();
10331044
auto *SrcBB = BI->getParent();
10341045
TermInst *destTerminator = DestBB->getTerminator();
1035-
if (Fn.hasOwnership()) {
1046+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
10361047
if (llvm::any_of(DestBB->getArguments(), [this](SILValue op) {
10371048
return !op->getType().isTrivial(Fn);
10381049
})) {
@@ -1821,8 +1832,7 @@ static bool isOnlyUnreachable(SILBasicBlock *BB) {
18211832
/// switch_enum where all but one block consists of just an
18221833
/// "unreachable" with an unchecked_enum_data and branch.
18231834
bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks(SwitchEnumInst *SEI) {
1824-
if (Fn.hasOwnership()) {
1825-
// TODO: OSSA; cleanup terminator results.
1835+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
18261836
if (!SEI->getOperand()->getType().isTrivial(Fn))
18271837
return false;
18281838
}
@@ -2119,7 +2129,7 @@ static bool hasSameUltimateSuccessor(SILBasicBlock *noneBB, SILBasicBlock *someB
21192129
bool SimplifyCFG::simplifySwitchEnumOnObjcClassOptional(SwitchEnumInst *SEI) {
21202130
// TODO: OSSA; handle non-trivial enum case cleanup
21212131
// (simplify_switch_enum_objc.sil).
2122-
if (Fn.hasOwnership()) {
2132+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
21232133
return false;
21242134
}
21252135

@@ -2183,7 +2193,7 @@ bool SimplifyCFG::simplifySwitchEnumBlock(SwitchEnumInst *SEI) {
21832193
auto *LiveBlock = SEI->getCaseDestination(EnumCase.get());
21842194
auto *ThisBB = SEI->getParent();
21852195

2186-
if (Fn.hasOwnership()) {
2196+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
21872197
// TODO: OSSA; cleanup terminator results.
21882198
if (!SEI->getOperand()->getType().isTrivial(Fn))
21892199
return false;
@@ -2423,7 +2433,7 @@ bool SimplifyCFG::simplifyCheckedCastBranchBlock(CheckedCastBranchInst *CCBI) {
24232433
bool SimplifyCFG::simplifyCheckedCastValueBranchBlock(
24242434
CheckedCastValueBranchInst *CCBI) {
24252435
// TODO: OSSA; handle cleanups for opaque cases (simplify_cfg_opaque.sil).
2426-
if (Fn.hasOwnership()) {
2436+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
24272437
return false;
24282438
}
24292439

@@ -2701,7 +2711,7 @@ bool SimplifyCFG::simplifyTermWithIdenticalDestBlocks(SILBasicBlock *BB) {
27012711
TermInst *Term = BB->getTerminator();
27022712
// TODO: OSSA; cleanup nontrivial terminator operands (if this ever actually
27032713
// happens)
2704-
if (Fn.hasOwnership()) {
2714+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
27052715
if (llvm::any_of(Term->getOperandValues(), [this](SILValue op) {
27062716
return !op->getType().isTrivial(Fn);
27072717
})) {
@@ -2896,7 +2906,7 @@ bool SimplifyCFG::simplifyBlocks() {
28962906
/// Canonicalize all switch_enum and switch_enum_addr instructions.
28972907
/// If possible, replace the default with the corresponding unique case.
28982908
bool SimplifyCFG::canonicalizeSwitchEnums() {
2899-
if (Fn.hasOwnership()) {
2909+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
29002910
// TODO: OSSA. In OSSA, the default switch_enum case passes the
29012911
// original enum as a block argument. This needs to check that the block
29022912
// argument is dead, then replace it with the a new argument for the default
@@ -3004,7 +3014,7 @@ bool SimplifyCFG::tailDuplicateObjCMethodCallSuccessorBlocks() {
30043014
// TODO: OSSA phi support. Even if all block arguments are trivial,
30053015
// jump-threading may require creation of guaranteed phis, which may require
30063016
// creation of nested borrow scopes.
3007-
if (Fn.hasOwnership()) {
3017+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
30083018
return false;
30093019
}
30103020
// Collect blocks to tail duplicate.
@@ -3213,7 +3223,7 @@ RemoveDeadArgsWhenSplitting("sroa-args-remove-dead-args-after",
32133223
llvm::cl::init(true));
32143224

32153225
bool ArgumentSplitter::split() {
3216-
if (Arg->getFunction()->hasOwnership()) {
3226+
if (!EnableOSSARewriteTerminator && Arg->getFunction()->hasOwnership()) {
32173227
// TODO: OSSA phi support
32183228
if (!Arg->getType().isTrivial(*Arg->getFunction()))
32193229
return false;
@@ -4005,7 +4015,7 @@ bool SimplifyCFG::simplifyArgs(SILBasicBlock *BB) {
40054015
if (BB->pred_empty())
40064016
return false;
40074017

4008-
if (Fn.hasOwnership()) {
4018+
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
40094019
// TODO: OSSA phi support
40104020
if (llvm::any_of(BB->getArguments(), [this](SILArgument *arg) {
40114021
return !arg->getType().isTrivial(Fn);

lib/SILOptimizer/Utils/CheckedCastBrJumpThreading.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class CheckedCastBrJumpThreading {
4242
// Dominator information to be used.
4343
DominanceInfo *DT;
4444

45+
// Enable non-trivial terminator rewriting in OSSA.
46+
bool EnableOSSARewriteTerminator;
47+
4548
// List of predecessors.
4649
typedef SmallVector<SILBasicBlock *, 8> PredList;
4750

@@ -119,10 +122,14 @@ class CheckedCastBrJumpThreading {
119122
bool trySimplify(CheckedCastBranchInst *CCBI);
120123

121124
public:
122-
CheckedCastBrJumpThreading(SILFunction *Fn, DominanceInfo *DT,
123-
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist)
124-
: Fn(Fn), DT(DT), BlocksForWorklist(BlocksForWorklist),
125-
BlocksToEdit(Fn), BlocksToClone(Fn) { }
125+
CheckedCastBrJumpThreading(
126+
SILFunction *Fn, DominanceInfo *DT,
127+
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist,
128+
bool EnableOSSARewriteTerminator)
129+
: Fn(Fn), DT(DT),
130+
EnableOSSARewriteTerminator(EnableOSSARewriteTerminator),
131+
BlocksForWorklist(BlocksForWorklist), BlocksToEdit(Fn),
132+
BlocksToClone(Fn) {}
126133

127134
void optimizeFunction();
128135
};
@@ -486,6 +493,11 @@ areEquivalentConditionsAlongPaths(CheckedCastBranchInst *DomCCBI) {
486493
/// Try performing a dominator-based jump-threading for
487494
/// checked_cast_br instructions.
488495
bool CheckedCastBrJumpThreading::trySimplify(CheckedCastBranchInst *CCBI) {
496+
if (!EnableOSSARewriteTerminator && Fn->hasOwnership()
497+
&& !CCBI->getOperand()->getType().isTrivial(*Fn)) {
498+
return false;
499+
}
500+
489501
// Init information about the checked_cast_br we try to
490502
// jump-thread.
491503
BB = CCBI->getParent();
@@ -685,9 +697,13 @@ void CheckedCastBrJumpThreading::optimizeFunction() {
685697

686698
namespace swift {
687699

688-
bool tryCheckedCastBrJumpThreading(SILFunction *Fn, DominanceInfo *DT,
689-
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist) {
690-
CheckedCastBrJumpThreading CCBJumpThreading(Fn, DT, BlocksForWorklist);
700+
bool tryCheckedCastBrJumpThreading(
701+
SILFunction *Fn, DominanceInfo *DT,
702+
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist,
703+
bool EnableOSSARewriteTerminator) {
704+
705+
CheckedCastBrJumpThreading CCBJumpThreading(Fn, DT, BlocksForWorklist,
706+
EnableOSSARewriteTerminator);
691707
CCBJumpThreading.optimizeFunction();
692708
return !BlocksForWorklist.empty();
693709
}

0 commit comments

Comments
 (0)