Skip to content

Commit

Permalink
Merge pull request swiftlang#79030 from meg-gupta/fixcfcp
Browse files Browse the repository at this point in the history
[6.1] Fix condition forwarding for switch_enum with default in ossa
  • Loading branch information
meg-gupta authored Jan 30, 2025
2 parents 447e13b + ffd3e5a commit 48c0689
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
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

0 comments on commit 48c0689

Please sign in to comment.