Skip to content

Commit db96d63

Browse files
committed
SimplifyCFG: correctly handle borrowed-from values when removing a redundant phi argument
Fixes a verifier crash
1 parent df62235 commit db96d63

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -3850,11 +3850,21 @@ static void tryToReplaceArgWithIncomingValue(SILBasicBlock *BB, unsigned i,
38503850
// An argument has one result value. We need to replace this with the *value*
38513851
// of the incoming block(s).
38523852
LLVM_DEBUG(llvm::dbgs() << "replace arg with incoming value:" << *A);
3853-
if (auto *bfi = getBorrowedFromUser(A)) {
3854-
bfi->replaceAllUsesWith(A);
3855-
bfi->eraseFromParent();
3853+
while (!A->use_empty()) {
3854+
Operand *op = *A->use_begin();
3855+
if (auto *bfi = dyn_cast<BorrowedFromInst>(op->getUser())) {
3856+
if (op->getOperandNumber() == 0) {
3857+
bfi->replaceAllUsesWith(V);
3858+
bfi->eraseFromParent();
3859+
continue;
3860+
}
3861+
if (auto *prevBfi = dyn_cast<BorrowedFromInst>(V)) {
3862+
op->set(prevBfi->getBorrowedValue());
3863+
continue;
3864+
}
3865+
}
3866+
op->set(V);
38563867
}
3857-
A->replaceAllUsesWith(V);
38583868
}
38593869

38603870
bool SimplifyCFG::simplifyArgs(SILBasicBlock *BB) {

test/SILOptimizer/simplify_cfg_ossa.sil

+32
Original file line numberDiff line numberDiff line change
@@ -1939,3 +1939,35 @@ bb3:
19391939
return %t : $()
19401940
}
19411941

1942+
// CHECK-LABEL: sil [ossa] @replace_phi_arg_with_borrowed_from_use :
1943+
// CHECK: bb3([[R:%.*]] : @reborrow $B):
1944+
// CHECK: bb6([[G:%.*]] : @guaranteed $E):
1945+
// CHECK-NEXT: [[X:%.*]] = borrowed [[G]] : $E from ([[R]] : $B)
1946+
// CHECK-NEXT: fix_lifetime [[X]]
1947+
// CHECK: } // end sil function 'replace_phi_arg_with_borrowed_from_use'
1948+
sil [ossa] @replace_phi_arg_with_borrowed_from_use : $@convention(thin) (@in_guaranteed B) -> () {
1949+
bb0(%0 : $*B):
1950+
cond_br undef, bb1, bb2
1951+
bb1:
1952+
%4 = load_borrow %0
1953+
br bb3(%4)
1954+
bb2:
1955+
%6 = load_borrow %0
1956+
br bb3(%6)
1957+
bb3(%8 : @reborrow $B):
1958+
%9 = borrowed %8 from ()
1959+
cond_br undef, bb4, bb5
1960+
bb4:
1961+
%11 = unchecked_ref_cast %9 to $E
1962+
br bb6(%11, %9)
1963+
bb5:
1964+
%13 = unchecked_ref_cast %9 to $E
1965+
br bb6(%13, %9)
1966+
bb6(%15: @guaranteed $E, %16 : @reborrow $B):
1967+
%17 = borrowed %15 from (%16)
1968+
%18 = borrowed %16 from ()
1969+
fix_lifetime %17
1970+
end_borrow %18
1971+
%r = tuple()
1972+
return %r
1973+
}

0 commit comments

Comments
 (0)