Skip to content

Commit ff4ab7d

Browse files
committed
Enable SimplifyCFG::canonicalizeSwitchEnums and add unit tests
1 parent 2a908e3 commit ff4ab7d

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,9 +2748,6 @@ bool SimplifyCFG::simplifyBlocks() {
27482748
/// Canonicalize all switch_enum and switch_enum_addr instructions.
27492749
/// If possible, replace the default with the corresponding unique case.
27502750
bool SimplifyCFG::canonicalizeSwitchEnums() {
2751-
if (!EnableOSSASimplifyCFG && Fn.hasOwnership()) {
2752-
return false;
2753-
}
27542751
bool Changed = false;
27552752
for (auto &BB : Fn) {
27562753
TermInst *TI = BB.getTerminator();
@@ -2768,13 +2765,7 @@ bool SimplifyCFG::canonicalizeSwitchEnums() {
27682765
NullablePtr<EnumElementDecl> defaultDecl = SWI.getUniqueCaseForDefault();
27692766
if (!defaultDecl)
27702767
continue;
2771-
2772-
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
2773-
if (!SWI.getOperand()->getType().isTrivial(Fn)) {
2774-
// TODO: Test and enable this case.
2775-
continue;
2776-
}
2777-
}
2768+
27782769
LLVM_DEBUG(llvm::dbgs() << "simplify canonical switch_enum\n");
27792770

27802771
// Construct a new instruction by copying all the case entries.

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
7676
#include "swift/SILOptimizer/PassManager/Passes.h"
7777
#include "swift/SILOptimizer/PassManager/Transforms.h"
78+
#include "swift/SILOptimizer/Transforms/SimplifyCFG.h"
7879
#include "swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h"
7980
#include "swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h"
8081
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
@@ -303,6 +304,18 @@ struct DumpFunction : UnitTest {
303304
void invoke(Arguments &arguments) override { getFunction()->dump(); }
304305
};
305306

307+
struct SimplifyCFGCanonicalizeSwitchEnum : UnitTest {
308+
SimplifyCFGCanonicalizeSwitchEnum(UnitTestRunner *pass) : UnitTest(pass) {}
309+
void invoke(Arguments &arguments) override {
310+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
311+
passToRun->injectPassManager(getPass()->getPassManager());
312+
passToRun->injectFunction(getFunction());
313+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
314+
/*EnableJumpThread=*/false)
315+
.canonicalizeSwitchEnums();
316+
}
317+
};
318+
306319
// Arguments: NONE
307320
// Dumps: the index of the self argument of the current function
308321
struct FunctionGetSelfArgumentIndex : UnitTest {
@@ -386,6 +399,8 @@ void UnitTestRunner::withTest(StringRef name, Doit doit) {
386399
PrunedLivenessBoundaryWithListOfLastUsersInsertionPointsTest)
387400
ADD_UNIT_TEST_SUBCLASS("shrink-borrow-scope", ShrinkBorrowScopeTest)
388401
ADD_UNIT_TEST_SUBCLASS("is-deinit-barrier", IsDeinitBarrierTest)
402+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-canonicalize-switch-enum",
403+
SimplifyCFGCanonicalizeSwitchEnum)
389404
/// [new_tests] Add the new mapping from string to subclass above this line.
390405

391406
#undef ADD_UNIT_TEST_SUBCLASS
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// RUN: %target-sil-opt -unit-test-runner %s 2>&1 | %FileCheck %s
2+
3+
class Klass {
4+
}
5+
6+
enum FakeOptional<T> {
7+
case some(T)
8+
case none
9+
}
10+
11+
enum E1 {
12+
case A(Klass)
13+
case B(Klass)
14+
case C(Klass)
15+
}
16+
17+
enum E2 {
18+
case A(Klass)
19+
case B(Klass)
20+
case C
21+
}
22+
23+
// CHECK-LABEL: sil [ossa] @test_canonicalize_switch_enum1 :
24+
// CHECK-NOT: default
25+
// CHECK-LABEL: } // end sil function 'test_canonicalize_switch_enum1'
26+
sil [ossa] @test_canonicalize_switch_enum1 : $@convention(thin) (@owned FakeOptional<Klass>) -> () {
27+
bb0(%0 : @owned $FakeOptional<Klass>):
28+
test_specification "simplify-cfg-canonicalize-switch-enum"
29+
switch_enum %0 : $FakeOptional<Klass>, case #FakeOptional.none!enumelt: bb1, default bb2
30+
31+
bb1:
32+
br bb3
33+
34+
bb2(%5 : @owned $Klass):
35+
destroy_value %5 : $Klass
36+
br bb3
37+
38+
bb3:
39+
%t = tuple ()
40+
return %t : $()
41+
}
42+
43+
// CHECK-LABEL: sil [ossa] @test_canonicalize_switch_enum2 :
44+
// CHECK: default
45+
// CHECK-LABEL: } // end sil function 'test_canonicalize_switch_enum2'
46+
sil [ossa] @test_canonicalize_switch_enum2 : $@convention(thin) (@owned E1) -> () {
47+
bb0(%0 : @owned $E1):
48+
test_specification "simplify-cfg-canonicalize-switch-enum"
49+
switch_enum %0 : $E1, case #E1.A!enumelt: bb1, default bb2
50+
51+
bb1(%2 : @owned $Klass):
52+
destroy_value %2 : $Klass
53+
br bb3
54+
55+
bb2(%5 : @owned $E1):
56+
destroy_value %5 : $E1
57+
br bb3
58+
59+
bb3:
60+
%t = tuple ()
61+
return %t : $()
62+
}
63+
64+
// CHECK-LABEL: sil [ossa] @test_canonicalize_switch_enum3 :
65+
// CHECK: default
66+
// CHECK-LABEL: } // end sil function 'test_canonicalize_switch_enum3'
67+
sil [ossa] @test_canonicalize_switch_enum3 : $@convention(thin) (@owned E2) -> () {
68+
bb0(%0 : @owned $E2):
69+
test_specification "simplify-cfg-canonicalize-switch-enum"
70+
switch_enum %0 : $E2, case #E2.A!enumelt: bb1, default bb2
71+
72+
bb1(%2 : @owned $Klass):
73+
destroy_value %2 : $Klass
74+
br bb3
75+
76+
bb2(%5 : @owned $E2):
77+
destroy_value %5 : $E2
78+
br bb3
79+
80+
bb3:
81+
%t = tuple ()
82+
return %t : $()
83+
}
84+

0 commit comments

Comments
 (0)