Skip to content

Commit b170ad7

Browse files
authored
Merge pull request #24953 from gottesmm/pr-71990c28f613fe44d9bf454eeb0b89a4356c8f91
2 parents 2e12ac8 + 748bf88 commit b170ad7

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,20 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
232232
const ValueDecl *D = nullptr);
233233

234234
/// Replace the \p{i}th BB arg with a new BBArg with SILType \p Ty and
235-
/// ValueDecl
236-
/// \p D.
237-
SILPhiArgument *replacePhiArgument(unsigned i, SILType Ty,
238-
ValueOwnershipKind Kind,
239-
const ValueDecl *D = nullptr);
235+
/// ValueDecl \p D.
236+
///
237+
/// NOTE: This assumes that the current argument in position \p i has had its
238+
/// uses eliminated. To replace/replace all uses with, use
239+
/// replacePhiArgumentAndRAUW.
240+
SILPhiArgument *replacePhiArgument(unsigned i, SILType type,
241+
ValueOwnershipKind kind,
242+
const ValueDecl *decl = nullptr);
243+
244+
/// Replace phi argument \p i and RAUW all uses.
245+
SILPhiArgument *
246+
replacePhiArgumentAndReplaceAllUses(unsigned i, SILType type,
247+
ValueOwnershipKind kind,
248+
const ValueDecl *decl = nullptr);
240249

241250
/// Allocate a new argument of type \p Ty and append it to the argument
242251
/// list. Optionally you can pass in a value decl parameter.

include/swift/SIL/SILBuilder.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,11 +1938,7 @@ class SILBuilder {
19381938
SILType destTy, SILBasicBlock *successBB,
19391939
SILBasicBlock *failureBB,
19401940
ProfileCounter Target1Count = ProfileCounter(),
1941-
ProfileCounter Target2Count = ProfileCounter()) {
1942-
return insertTerminator(CheckedCastBranchInst::create(
1943-
getSILDebugLocation(Loc), isExact, op, destTy, successBB, failureBB,
1944-
getFunction(), C.OpenedArchetypes, Target1Count, Target2Count));
1945-
}
1941+
ProfileCounter Target2Count = ProfileCounter());
19461942

19471943
CheckedCastValueBranchInst *
19481944
createCheckedCastValueBranch(SILLocation Loc, SILValue op, SILType destTy,

lib/SIL/SILBasicBlock.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,29 @@ SILPhiArgument *SILBasicBlock::replacePhiArgument(unsigned i, SILType Ty,
210210
return NewArg;
211211
}
212212

213+
SILPhiArgument *SILBasicBlock::replacePhiArgumentAndReplaceAllUses(
214+
unsigned i, SILType ty, ValueOwnershipKind kind, const ValueDecl *d) {
215+
// Put in an undef placeholder before we do the replacement since
216+
// replacePhiArgument() expects the replaced argument to not have
217+
// any uses.
218+
SmallVector<Operand *, 16> operands;
219+
SILValue undef = SILUndef::get(ty, *getParent());
220+
for (auto *use : getArgument(i)->getUses()) {
221+
use->set(undef);
222+
operands.push_back(use);
223+
}
224+
225+
// Perform the replacement.
226+
auto *newArg = replacePhiArgument(i, ty, kind, d);
227+
228+
// Wire back up the uses.
229+
while (!operands.empty()) {
230+
operands.pop_back_val()->set(newArg);
231+
}
232+
233+
return newArg;
234+
}
235+
213236
SILPhiArgument *SILBasicBlock::createPhiArgument(SILType Ty,
214237
ValueOwnershipKind Kind,
215238
const ValueDecl *D) {

lib/SIL/SILBuilder.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,15 @@ void SILBuilder::emitScopedBorrowOperation(SILLocation loc, SILValue original,
600600

601601
createEndBorrow(loc, original);
602602
}
603+
604+
CheckedCastBranchInst *SILBuilder::createCheckedCastBranch(
605+
SILLocation Loc, bool isExact, SILValue op, SILType destTy,
606+
SILBasicBlock *successBB, SILBasicBlock *failureBB,
607+
ProfileCounter target1Count, ProfileCounter target2Count) {
608+
assert((!hasOwnership() || !failureBB->getNumArguments() ||
609+
failureBB->getArgument(0)->getType() == op->getType()) &&
610+
"failureBB's argument doesn't match incoming argument type");
611+
return insertTerminator(CheckedCastBranchInst::create(
612+
getSILDebugLocation(Loc), isExact, op, destTy, successBB, failureBB,
613+
getFunction(), C.OpenedArchetypes, target1Count, target2Count));
614+
}

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,10 +1198,19 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
11981198
auto replaceCastHelper = [](SILBuilderWithScope &B,
11991199
SILDynamicCastInst dynamicCast,
12001200
MetatypeInst *mi) -> SILInstruction * {
1201+
// Make sure that the failure block has the new metatype type for
1202+
// its default argument as required when we are in ossa
1203+
// mode. Without ossa, failure blocks do not have args, so we do
1204+
// not need to do anything.
1205+
auto *fBlock = dynamicCast.getFailureBlock();
1206+
if (B.hasOwnership()) {
1207+
fBlock->replacePhiArgumentAndReplaceAllUses(0, mi->getType(),
1208+
ValueOwnershipKind::Any);
1209+
}
12011210
return B.createCheckedCastBranch(
12021211
dynamicCast.getLocation(), false /*isExact*/, mi,
12031212
dynamicCast.getLoweredTargetType(), dynamicCast.getSuccessBlock(),
1204-
dynamicCast.getFailureBlock(), *dynamicCast.getSuccessBlockCount(),
1213+
fBlock, *dynamicCast.getSuccessBlockCount(),
12051214
*dynamicCast.getFailureBlockCount());
12061215
};
12071216

test/SILOptimizer/cast_folding.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: %target-swift-frontend -disable-availability-checking -O -emit-sil %s | %FileCheck %s
22
// RUN: %target-swift-frontend -disable-availability-checking -Onone -emit-sil %s | %FileCheck %s --check-prefix=MANDATORY
3+
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=FunctionSignatureOpts -Xllvm -sil-disable-pass=PerfInliner -enable-ownership-stripping-after-serialization -disable-availability-checking -O -emit-sil %s | %FileCheck %s
4+
// RUN: %target-swift-frontend -enable-ownership-stripping-after-serialization -disable-availability-checking -Onone -emit-sil %s | %FileCheck %s --check-prefix=MANDATORY
5+
36
// We want to check two things here:
47
// - Correctness
58
// - That certain "is" checks are eliminated based on static analysis at compile-time

0 commit comments

Comments
 (0)