Skip to content

SimplifyCFG: Fix a missing borrowed-from when doing jump threading #74933

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
Jul 3, 2024
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
4 changes: 4 additions & 0 deletions include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ class BasicBlockCloner : public SILCloner<BasicBlockCloner> {
BasicBlockCloner(SILBasicBlock *origBB, SILPassManager *pm, DeadEndBlocks *deBlocks = nullptr)
: SILCloner(*origBB->getParent()), origBB(origBB), deBlocks(deBlocks), pm(pm) {}

void registerBlockWithNewPhiArg(SILBasicBlock *b) {
blocksWithNewPhiArgs.push_back(b);
}

bool canCloneBlock() {
for (auto &inst : *origBB) {
if (!canCloneInstruction(&inst))
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Transforms/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
(*ThreadedSuccessorBlock->args_begin())->getType() &&
"Argument types must match");
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock, {UED});
Cloner.registerBlockWithNewPhiArg(ThreadedSuccessorBlock);
} else {
assert(SEI->getDefaultBB() == ThreadedSuccessorBlock);
auto *OldBlockArg = ThreadedSuccessorBlock->getArgument(0);
Expand Down
46 changes: 46 additions & 0 deletions test/SILOptimizer/simplify_cfg_ossa_dom_jumpthread.sil
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ struct FakeBool {

class Klass {}

struct F {
let a: Int?
let e: String
}

sil @external_f : $@convention(thin) () -> ()
// func testThread3(a : Int32) -> Int32 {
// (enum b, val) = (a ? (_true, 16) : (_false, 17))
Expand Down Expand Up @@ -886,3 +891,44 @@ bb10:
return %21 : $()
}

// CHECK-LABEL: sil [ossa] @jump_thread_new_phi_arg
// CHECK: switch_enum %0 : $Optional<F>
// CHECK: switch_enum {{%[0-9]+}} : $Optional<Int>
// CHECK-NOT: switch_enum
// CHECK: } // end sil function 'jump_thread_new_phi_arg'
sil [ossa] @jump_thread_new_phi_arg : $@convention(thin) (@guaranteed Optional<F>) -> Optional<Int> {
bb0(%0 : @guaranteed $Optional<F>):
switch_enum %0 : $Optional<F>, case #Optional.some!enumelt: bb2, case #Optional.none!enumelt: bb1

bb1:
%4 = enum $Optional<Int>, #Optional.none!enumelt
br bb3(%4 : $Optional<Int>)


bb2(%6 : @guaranteed $F):
%7 = struct_extract %6 : $F, #F.a
br bb3(%7 : $Optional<Int>)


bb3(%9 : $Optional<Int>):
switch_enum %9 : $Optional<Int>, case #Optional.some!enumelt: bb4, case #Optional.none!enumelt: bb5

bb4(%11 : $Int):
br bb8(%9 : $Optional<Int>)

bb5:
switch_enum %0 : $Optional<F>, case #Optional.some!enumelt: bb6, case #Optional.none!enumelt: bb7


bb6(%14 : @guaranteed $F):
%15 = struct_extract %14 : $F, #F.a
br bb8(%15 : $Optional<Int>)

bb7:
%17 = enum $Optional<Int>, #Optional.none!enumelt
br bb8(%17 : $Optional<Int>)


bb8(%19 : $Optional<Int>):
return %19 : $Optional<Int>
}