Skip to content

Commit 8e7019e

Browse files
committed
Enable SimplifyCFG::simplifyArgs in OSSA
1 parent cc6004e commit 8e7019e

File tree

3 files changed

+247
-11
lines changed

3 files changed

+247
-11
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,16 +3811,21 @@ bool SimplifyCFG::simplifyArgument(SILBasicBlock *BB, unsigned i) {
38113811
// the uses in this block, and then rewrite the branch operands.
38123812
LLVM_DEBUG(llvm::dbgs() << "unwrap argument:" << *A);
38133813
A->replaceAllUsesWith(SILUndef::get(A->getType(), *BB->getParent()));
3814-
auto *NewArg =
3815-
BB->replacePhiArgument(i, proj->getType(), OwnershipKind::Owned);
3814+
auto *NewArg = BB->replacePhiArgument(i, proj->getType(),
3815+
BB->getArgument(i)->getOwnershipKind());
38163816
proj->replaceAllUsesWith(NewArg);
38173817

38183818
// Rewrite the branch operand for each incoming branch.
38193819
for (auto *Pred : BB->getPredecessorBlocks()) {
38203820
if (auto *Branch = cast<BranchInst>(Pred->getTerminator())) {
3821+
auto *BranchOpValue = cast<SingleValueInstruction>(Branch->getOperand(i));
38213822
auto V = getInsertedValue(cast<SingleValueInstruction>(Branch->getArg(i)),
38223823
proj);
38233824
Branch->setOperand(i, V);
3825+
if (isInstructionTriviallyDead(BranchOpValue)) {
3826+
BranchOpValue->replaceAllUsesWithUndef();
3827+
BranchOpValue->eraseFromParent();
3828+
}
38243829
addToWorklist(Pred);
38253830
}
38263831
}
@@ -3866,15 +3871,6 @@ bool SimplifyCFG::simplifyArgs(SILBasicBlock *BB) {
38663871
if (BB->pred_empty())
38673872
return false;
38683873

3869-
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
3870-
// TODO: OSSA phi support
3871-
if (llvm::any_of(BB->getArguments(), [this](SILArgument *arg) {
3872-
return !arg->getType().isTrivial(Fn);
3873-
})) {
3874-
return false;
3875-
}
3876-
}
3877-
38783874
// Ignore blocks that are successors of terminators with mandatory args.
38793875
for (SILBasicBlock *pred : BB->getPredecessorBlocks()) {
38803876
if (hasMandatoryArgument(pred->getTerminator()))

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,32 @@ struct ShrinkBorrowScopeTest : UnitTest {
270270
}
271271
};
272272

273+
struct SimplifyCFGSimplifyArgument: UnitTest {
274+
SimplifyCFGSimplifyArgument(UnitTestRunner *pass)
275+
: UnitTest(pass) {}
276+
void invoke(Arguments &arguments) override {
277+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
278+
passToRun->injectPassManager(getPass()->getPassManager());
279+
passToRun->injectFunction(getFunction());
280+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
281+
/*EnableJumpThread=*/false)
282+
.simplifyArgument(arguments.takeBlock(), arguments.takeUInt());
283+
}
284+
};
285+
286+
struct SimplifyCFGSimplifyBlockArgs : UnitTest {
287+
SimplifyCFGSimplifyBlockArgs(UnitTestRunner *pass)
288+
: UnitTest(pass) {}
289+
void invoke(Arguments &arguments) override {
290+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
291+
passToRun->injectPassManager(getPass()->getPassManager());
292+
passToRun->injectFunction(getFunction());
293+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
294+
/*EnableJumpThread=*/false)
295+
.simplifyBlockArgs();
296+
}
297+
};
298+
273299
struct SimplifyCFGCanonicalizeSwitchEnum : UnitTest {
274300
SimplifyCFGCanonicalizeSwitchEnum(UnitTestRunner *pass) : UnitTest(pass) {}
275301
void invoke(Arguments &arguments) override {
@@ -444,6 +470,10 @@ void UnitTestRunner::withTest(StringRef name, Doit doit) {
444470
ADD_UNIT_TEST_SUBCLASS("shrink-borrow-scope", ShrinkBorrowScopeTest)
445471

446472
// SimplifyCFG unit tests
473+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-argument",
474+
SimplifyCFGSimplifyArgument)
475+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-block-args",
476+
SimplifyCFGSimplifyBlockArgs)
447477
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-canonicalize-switch-enum",
448478
SimplifyCFGCanonicalizeSwitchEnum)
449479
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-switch-enum-block",
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
// RUN: %target-sil-opt -unit-test-runner %s 2>&1 | %FileCheck %s
2+
3+
class Klass {}
4+
5+
enum FakeOptional<T> {
6+
case some(T)
7+
case none
8+
}
9+
10+
sil @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
11+
12+
// CHECK-LABEL: sil [ossa] @test_simplify_args1 :
13+
// CHECK: bb2:
14+
// CHECK-LABEL: } // end sil function 'test_simplify_args1'
15+
sil [ossa] @test_simplify_args1 : $@convention(thin) (@owned Klass) -> () {
16+
bb0(%0 : @owned $Klass):
17+
test_specification "simplify-cfg-simplify-block-args"
18+
%1 = begin_borrow %0 : $Klass
19+
br bb1(%1 : $Klass)
20+
21+
bb1(%3 : @guaranteed $Klass):
22+
br bb2(%3 : $Klass)
23+
24+
bb2(%5 : @guaranteed $Klass):
25+
end_borrow %5 : $Klass
26+
destroy_value %0 : $Klass
27+
%t = tuple ()
28+
return %t : $()
29+
}
30+
31+
// CHECK-LABEL: sil [ossa] @test_simplify_args2 :
32+
// CHECK: bb3({{.*}}):
33+
// CHECK-LABEL: } // end sil function 'test_simplify_args2'
34+
sil [ossa] @test_simplify_args2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
35+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
36+
test_specification "simplify-cfg-simplify-block-args"
37+
cond_br undef, bb1, bb2
38+
39+
bb1:
40+
%2 = begin_borrow %0 : $Klass
41+
br bb3(%2 : $Klass)
42+
43+
bb2:
44+
%3 = begin_borrow %1 : $Klass
45+
br bb3(%3 : $Klass)
46+
47+
bb3(%4 : @guaranteed $Klass):
48+
end_borrow %4 : $Klass
49+
destroy_value %0 : $Klass
50+
destroy_value %1 : $Klass
51+
%t = tuple ()
52+
return %t : $()
53+
}
54+
55+
// CHECK-LABEL: sil [ossa] @test_simplify_args3 :
56+
// CHECK: bb3({{.*}}):
57+
// CHECK-LABEL: } // end sil function 'test_simplify_args3'
58+
sil [ossa] @test_simplify_args3 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
59+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
60+
test_specification "simplify-cfg-simplify-block-args"
61+
cond_br undef, bb1, bb2
62+
63+
bb1:
64+
destroy_value %0 : $Klass
65+
br bb3(%1 : $Klass)
66+
67+
bb2:
68+
destroy_value %1 : $Klass
69+
br bb3(%0 : $Klass)
70+
71+
bb3(%4 : @owned $Klass):
72+
destroy_value %4 : $Klass
73+
%t = tuple ()
74+
return %t : $()
75+
}
76+
77+
// CHECK-LABEL: sil [ossa] @test_simplify_args4 :
78+
// CHECK: bb3:
79+
// CHECK-LABEL: } // end sil function 'test_simplify_args4'
80+
sil [ossa] @test_simplify_args4 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
81+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
82+
test_specification "simplify-cfg-simplify-block-args"
83+
cond_br undef, bb1, bb2
84+
85+
bb1:
86+
destroy_value %0 : $Klass
87+
br bb3(%1 : $Klass)
88+
89+
bb2:
90+
destroy_value %0 : $Klass
91+
br bb3(%1 : $Klass)
92+
93+
bb3(%4 : @owned $Klass):
94+
destroy_value %4 : $Klass
95+
%t = tuple ()
96+
return %t : $()
97+
}
98+
99+
// CHECK-LABEL: sil [ossa] @test_simplify_args5 :
100+
// CHECK: bb3:
101+
// CHECK-LABEL: } // end sil function 'test_simplify_args5'
102+
sil [ossa] @test_simplify_args5 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
103+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
104+
test_specification "simplify-cfg-simplify-block-args"
105+
%2 = begin_borrow %0 : $Klass
106+
cond_br undef, bb1, bb2
107+
108+
bb1:
109+
br bb3(%2 : $Klass)
110+
111+
bb2:
112+
br bb3(%2 : $Klass)
113+
114+
bb3(%4 : @guaranteed $Klass):
115+
end_borrow %4 : $Klass
116+
destroy_value %0 : $Klass
117+
destroy_value %1 : $Klass
118+
%t = tuple ()
119+
return %t : $()
120+
}
121+
122+
// CHECK-LABEL: sil [ossa] @test_simplify_args6 :
123+
// CHECK: bb1:
124+
// CHECK-LABEL: } // end sil function 'test_simplify_args6'
125+
sil [ossa] @test_simplify_args6 : $@convention(thin) (@owned Klass) -> () {
126+
bb0(%0 : @owned $Klass):
127+
test_specification "simplify-cfg-simplify-block-args"
128+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
129+
br bb1(%1 : $FakeOptional<Klass>)
130+
131+
bb1(%3 : @owned $FakeOptional<Klass>):
132+
switch_enum %3 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb3, case #FakeOptional.none!enumelt: bb2
133+
134+
bb2:
135+
br bb4
136+
137+
bb3(%6 : @owned $Klass):
138+
destroy_value %6 : $Klass
139+
br bb4
140+
141+
bb4:
142+
%t = tuple ()
143+
return %t : $()
144+
}
145+
146+
// CHECK-LABEL: sil [ossa] @test_simplify_args7 :
147+
// CHECK: bb1:
148+
// CHECK-LABEL: } // end sil function 'test_simplify_args7'
149+
sil [ossa] @test_simplify_args7 : $@convention(thin) (@owned Klass) -> () {
150+
bb0(%0 : @owned $Klass):
151+
test_specification "simplify-cfg-simplify-block-args"
152+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
153+
br bb1(%1 : $FakeOptional<Klass>)
154+
155+
bb1(%3 : @owned $FakeOptional<Klass>):
156+
switch_enum %3 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb3, case #FakeOptional.none!enumelt: bb2
157+
158+
bb2:
159+
br bb4
160+
161+
bb3(%6 : @owned $Klass):
162+
destroy_value %6 : $Klass
163+
br bb4
164+
165+
bb4:
166+
%t = tuple ()
167+
return %t : $()
168+
}
169+
170+
// CHECK-LABEL: sil [ossa] @test_simplify_args8 :
171+
// CHECK-NOT: unchecked_enum_date
172+
// CHECK-LABEL: } // end sil function 'test_simplify_args9'
173+
sil [ossa] @test_simplify_args8 : $@convention(thin) (@guaranteed Klass) -> () {
174+
bb0(%0 : @guaranteed $Klass):
175+
test_specification "simplify-cfg-simplify-argument @block[1] 0"
176+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
177+
br bb1(%1 : $FakeOptional<Klass>)
178+
179+
bb1(%3 : @guaranteed $FakeOptional<Klass>):
180+
%4 = unchecked_enum_data %3 : $FakeOptional<Klass>, #FakeOptional.some!enumelt
181+
%f = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
182+
%c = apply %f(%4) : $@convention(thin) (@guaranteed Klass) -> ()
183+
br bb3
184+
185+
bb3:
186+
%7 = tuple ()
187+
return %7 : $()
188+
}
189+
190+
// CHECK-LABEL: sil [ossa] @test_simplify_args9 :
191+
// CHECK-NOT: unchecked_enum_date
192+
// CHECK-LABEL: } // end sil function 'test_simplify_args9'
193+
sil [ossa] @test_simplify_args9 : $@convention(thin) (@owned Klass) -> () {
194+
bb0(%0 : @owned $Klass):
195+
test_specification "simplify-cfg-simplify-argument @block[1] 0"
196+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
197+
br bb1(%1 : $FakeOptional<Klass>)
198+
199+
bb1(%3 : @owned $FakeOptional<Klass>):
200+
%4 = unchecked_enum_data %3 : $FakeOptional<Klass>, #FakeOptional.some!enumelt
201+
%f = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
202+
%c = apply %f(%4) : $@convention(thin) (@guaranteed Klass) -> ()
203+
destroy_value %4 : $Klass
204+
br bb3
205+
206+
bb3:
207+
%7 = tuple ()
208+
return %7 : $()
209+
}
210+

0 commit comments

Comments
 (0)