Skip to content

Fix condition forwarding for switch_enum with default in ossa #78714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/SILOptimizer/Transforms/ConditionForwarding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,13 @@ bool ConditionForwarding::tryOptimize(SwitchEnumInst *SEI) {
llvm::SmallVector<SILValue, 2> BranchArgs;
unsigned HasEnumArg = NeedEnumArg.contains(SEDest);
if (SEDest->getNumArguments() == 1 + HasEnumArg) {
// The successor block has an original argument, which is the Enum's
// payload.
BranchArgs.push_back(EI->getOperand());
if (SEI->hasDefault() && SEDest == SEI->getDefaultBB()) {
BranchArgs.push_back(EI);
} else {
// The successor block has an original argument, which is the Enum's
// payload.
BranchArgs.push_back(EI->getOperand());
}
}
if (HasEnumArg) {
// The successor block has a new argument (which we created above) where
Expand Down
35 changes: 35 additions & 0 deletions test/SILOptimizer/conditionforwarding_nontrivial_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ enum FakeOptional<T> {
case some(T)
}

enum PrimaryColor : Int {
case red = 0
case blue = 1
case green = 2
}

sil [ossa] @use_color : $@convention(thin) (PrimaryColor) -> ()
sil [ossa] @callee : $@convention(thin) () -> ()
sil [ossa] @use_enum : $@convention(thin) (@guaranteed E) -> ()
sil [ossa] @use_int : $@convention(thin) (Builtin.Int64) -> ()
Expand Down Expand Up @@ -134,6 +141,34 @@ bb6:
return %r : $()
}

sil [ossa] @simple_forwarding4 : $@convention(thin) (Builtin.Int1) -> () {
bb0(%0 : $Builtin.Int1):
cond_br %0, bb1, bb2

bb1:
%2 = enum $PrimaryColor, #PrimaryColor.red!enumelt
br bb3(%2 : $PrimaryColor)

bb2:
%3 = enum $PrimaryColor, #PrimaryColor.blue!enumelt
br bb3(%3 : $PrimaryColor)

bb3(%14 : $PrimaryColor):
switch_enum %14 : $PrimaryColor, case #PrimaryColor.red!enumelt: bb4, default bb5

bb4:
br bb6

bb5(%18 : $PrimaryColor):
%15 = function_ref @use_color : $@convention(thin) (PrimaryColor) -> ()
%16 = apply %15(%18) : $@convention(thin) (PrimaryColor) -> ()
br bb6

bb6:
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: sil [ossa] @simple_switch_enum_forwarding1 :
// CHECK: bb0({{.*}}):
// CHECK-NEXT: br bb3
Expand Down