Skip to content

Commit 8f7978b

Browse files
committed
Fix SILParsing for forwarding ownership kind
1 parent 9bf4fe8 commit 8f7978b

File tree

6 files changed

+312
-68
lines changed

6 files changed

+312
-68
lines changed

docs/SIL.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,15 @@ and as a result:
20362036

20372037
* Textual SIL does not represent the ownership of forwarding instructions
20382038
explicitly. Instead, the instruction's ownership is inferred normally from the
2039-
parsed operand. Since the SILVerifier runs on Textual SIL after parsing, you
2039+
parsed operand.
2040+
In some cases the forwarding ownership kind is different from the ownership kind
2041+
of its operand. In such cases, textual SIL represents the forwarding ownership kind
2042+
explicity.
2043+
Eg: ::
2044+
2045+
%cast = unchecked_ref_cast %val : $Klass to $Optional<Klass>, forwarding: @unowned
2046+
2047+
Since the SILVerifier runs on Textual SIL after parsing, you
20402048
can feel confident that ownership constraints were inferred correctly.
20412049

20422050
Forwarding has slightly different ownership semantics depending on the value

include/swift/SIL/SILBuilder.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1845,10 +1845,20 @@ class SILBuilder {
18451845
createInitExistentialRef(SILLocation Loc, SILType ExistentialType,
18461846
CanType FormalConcreteType, SILValue Concrete,
18471847
ArrayRef<ProtocolConformanceRef> Conformances) {
1848+
return createInitExistentialRef(Loc, ExistentialType, FormalConcreteType,
1849+
Concrete, Conformances,
1850+
Concrete.getOwnershipKind());
1851+
}
1852+
1853+
InitExistentialRefInst *
1854+
createInitExistentialRef(SILLocation Loc, SILType ExistentialType,
1855+
CanType FormalConcreteType, SILValue Concrete,
1856+
ArrayRef<ProtocolConformanceRef> Conformances,
1857+
ValueOwnershipKind forwardingOwnershipKind) {
18481858
return insert(InitExistentialRefInst::create(
18491859
getSILDebugLocation(Loc), ExistentialType, FormalConcreteType, Concrete,
18501860
Conformances, &getFunction(), C.OpenedArchetypes,
1851-
Concrete.getOwnershipKind()));
1861+
forwardingOwnershipKind));
18521862
}
18531863

18541864
DeinitExistentialAddrInst *createDeinitExistentialAddr(SILLocation Loc,
@@ -2298,6 +2308,14 @@ class SILBuilder {
22982308
ProfileCounter Target1Count = ProfileCounter(),
22992309
ProfileCounter Target2Count = ProfileCounter());
23002310

2311+
CheckedCastBranchInst *
2312+
createCheckedCastBranch(SILLocation Loc, bool isExact, SILValue op,
2313+
SILType destLoweredTy, CanType destFormalTy,
2314+
SILBasicBlock *successBB, SILBasicBlock *failureBB,
2315+
ValueOwnershipKind forwardingOwnershipKind,
2316+
ProfileCounter Target1Count = ProfileCounter(),
2317+
ProfileCounter Target2Count = ProfileCounter());
2318+
23012319
CheckedCastValueBranchInst *
23022320
createCheckedCastValueBranch(SILLocation Loc,
23032321
SILValue op, CanType srcFormalTy,

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,8 +2697,8 @@ SILCloner<ImplClass>::visitCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
26972697
getOpLocation(Inst->getLoc()), Inst->isExact(),
26982698
getOpValue(Inst->getOperand()),
26992699
getOpType(Inst->getTargetLoweredType()),
2700-
getOpASTType(Inst->getTargetFormalType()),
2701-
OpSuccBB, OpFailBB, TrueCount, FalseCount));
2700+
getOpASTType(Inst->getTargetFormalType()), OpSuccBB, OpFailBB,
2701+
Inst->getForwardingOwnershipKind(), TrueCount, FalseCount));
27022702
}
27032703

27042704
template <typename ImplClass>

lib/SIL/IR/SILBuilder.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,23 @@ CheckedCastBranchInst *SILBuilder::createCheckedCastBranch(
656656
SILType destLoweredTy, CanType destFormalTy,
657657
SILBasicBlock *successBB, SILBasicBlock *failureBB,
658658
ProfileCounter target1Count, ProfileCounter target2Count) {
659+
return createCheckedCastBranch(Loc, isExact, op, destLoweredTy, destFormalTy,
660+
successBB, failureBB, op.getOwnershipKind(),
661+
target1Count, target2Count);
662+
}
663+
664+
CheckedCastBranchInst *SILBuilder::createCheckedCastBranch(
665+
SILLocation Loc, bool isExact, SILValue op, SILType destLoweredTy,
666+
CanType destFormalTy, SILBasicBlock *successBB, SILBasicBlock *failureBB,
667+
ValueOwnershipKind forwardingOwnershipKind, ProfileCounter target1Count,
668+
ProfileCounter target2Count) {
659669
assert((!hasOwnership() || !failureBB->getNumArguments() ||
660670
failureBB->getArgument(0)->getType() == op->getType()) &&
661671
"failureBB's argument doesn't match incoming argument type");
662672
return insertTerminator(CheckedCastBranchInst::create(
663673
getSILDebugLocation(Loc), isExact, op, destLoweredTy, destFormalTy,
664674
successBB, failureBB, getFunction(), C.OpenedArchetypes, target1Count,
665-
target2Count, op.getOwnershipKind()));
675+
target2Count, forwardingOwnershipKind));
666676
}
667677

668678
void SILBuilderWithScope::insertAfter(SILInstruction *inst,

0 commit comments

Comments
 (0)