Skip to content

Commit 01d4f11

Browse files
committed
Fix condition forwarding for switch_enum with default in ossa
In ossa, switch_enum's destination accepts an argument. Fixup condition forwarding to correctly forward the enum in this case. Fixes rdar://143042093
1 parent 4a94fc9 commit 01d4f11

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/SILOptimizer/Transforms/ConditionForwarding.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,13 @@ bool ConditionForwarding::tryOptimize(SwitchEnumInst *SEI) {
290290
llvm::SmallVector<SILValue, 2> BranchArgs;
291291
unsigned HasEnumArg = NeedEnumArg.contains(SEDest);
292292
if (SEDest->getNumArguments() == 1 + HasEnumArg) {
293-
// The successor block has an original argument, which is the Enum's
294-
// payload.
295-
BranchArgs.push_back(EI->getOperand());
293+
if (SEI->hasDefault() && SEDest == SEI->getDefaultBB()) {
294+
BranchArgs.push_back(EI);
295+
} else {
296+
// The successor block has an original argument, which is the Enum's
297+
// payload.
298+
BranchArgs.push_back(EI->getOperand());
299+
}
296300
}
297301
if (HasEnumArg) {
298302
// The successor block has a new argument (which we created above) where

test/SILOptimizer/conditionforwarding_nontrivial_ossa.sil

+35
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ enum FakeOptional<T> {
2121
case some(T)
2222
}
2323

24+
enum PrimaryColor : Int {
25+
case red = 0
26+
case blue = 1
27+
case green = 2
28+
}
29+
30+
sil [ossa] @use_color : $@convention(thin) (PrimaryColor) -> ()
2431
sil [ossa] @callee : $@convention(thin) () -> ()
2532
sil [ossa] @use_enum : $@convention(thin) (@guaranteed E) -> ()
2633
sil [ossa] @use_int : $@convention(thin) (Builtin.Int64) -> ()
@@ -134,6 +141,34 @@ bb6:
134141
return %r : $()
135142
}
136143

144+
sil [ossa] @simple_forwarding4 : $@convention(thin) (Builtin.Int1) -> () {
145+
bb0(%0 : $Builtin.Int1):
146+
cond_br %0, bb1, bb2
147+
148+
bb1:
149+
%2 = enum $PrimaryColor, #PrimaryColor.red!enumelt
150+
br bb3(%2 : $PrimaryColor)
151+
152+
bb2:
153+
%3 = enum $PrimaryColor, #PrimaryColor.blue!enumelt
154+
br bb3(%3 : $PrimaryColor)
155+
156+
bb3(%14 : $PrimaryColor):
157+
switch_enum %14 : $PrimaryColor, case #PrimaryColor.red!enumelt: bb4, default bb5
158+
159+
bb4:
160+
br bb6
161+
162+
bb5(%18 : $PrimaryColor):
163+
%15 = function_ref @use_color : $@convention(thin) (PrimaryColor) -> ()
164+
%16 = apply %15(%18) : $@convention(thin) (PrimaryColor) -> ()
165+
br bb6
166+
167+
bb6:
168+
%r = tuple ()
169+
return %r : $()
170+
}
171+
137172
// CHECK-LABEL: sil [ossa] @simple_switch_enum_forwarding1 :
138173
// CHECK: bb0({{.*}}):
139174
// CHECK-NEXT: br bb3

0 commit comments

Comments
 (0)