Skip to content

Commit b144527

Browse files
authored
Merge pull request swiftlang#36143 from meg-gupta/fixcloneforwardingownership
2 parents a274ebf + 5660643 commit b144527

File tree

8 files changed

+621
-296
lines changed

8 files changed

+621
-296
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 247 additions & 45 deletions
Large diffs are not rendered by default.

include/swift/SIL/SILCloner.h

Lines changed: 159 additions & 78 deletions
Large diffs are not rendered by default.

include/swift/SIL/SILInstruction.h

Lines changed: 88 additions & 74 deletions
Large diffs are not rendered by default.

lib/SIL/IR/SILBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,9 @@ CheckedCastBranchInst *SILBuilder::createCheckedCastBranch(
660660
failureBB->getArgument(0)->getType() == op->getType()) &&
661661
"failureBB's argument doesn't match incoming argument type");
662662
return insertTerminator(CheckedCastBranchInst::create(
663-
getSILDebugLocation(Loc), isExact, op,
664-
destLoweredTy, destFormalTy, successBB, failureBB,
665-
getFunction(), C.OpenedArchetypes, target1Count, target2Count));
663+
getSILDebugLocation(Loc), isExact, op, destLoweredTy, destFormalTy,
664+
successBB, failureBB, getFunction(), C.OpenedArchetypes, target1Count,
665+
target2Count, op.getOwnershipKind()));
666666
}
667667

668668
void SILBuilderWithScope::insertAfter(SILInstruction *inst,

lib/SIL/IR/SILInstructions.cpp

Lines changed: 84 additions & 94 deletions
Large diffs are not rendered by default.

lib/SIL/IR/SILPrinter.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,13 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
14201420
}
14211421
}
14221422

1423+
void printForwardingOwnershipKind(OwnershipForwardingMixin *inst,
1424+
SILValue op) {
1425+
if (inst->getForwardingOwnershipKind() != op.getOwnershipKind()) {
1426+
*this << ", forwarding: @" << inst->getForwardingOwnershipKind();
1427+
}
1428+
}
1429+
14231430
void visitStoreInst(StoreInst *SI) {
14241431
*this << Ctx.getID(SI->getSrc()) << " to ";
14251432
printStoreOwnershipQualifier(SI->getOwnershipQualifier());
@@ -1465,8 +1472,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
14651472
*this << "[delegatingselfallocated] ";
14661473
break;
14671474
}
1468-
14691475
*this << getIDAndType(MU->getOperand());
1476+
printForwardingOwnershipKind(MU, MU->getOperand());
14701477
}
14711478

14721479
void visitMarkFunctionEscapeInst(MarkFunctionEscapeInst *MFE) {
@@ -1516,6 +1523,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
15161523

15171524
void visitUnconditionalCheckedCastInst(UnconditionalCheckedCastInst *CI) {
15181525
*this << getIDAndType(CI->getOperand()) << " to " << CI->getTargetFormalType();
1526+
printForwardingOwnershipKind(CI, CI->getOperand());
15191527
}
15201528

15211529
void visitCheckedCastBranchInst(CheckedCastBranchInst *CI) {
@@ -1564,6 +1572,9 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
15641572

15651573
void printUncheckedConversionInst(ConversionInst *CI, SILValue operand) {
15661574
*this << getIDAndType(operand) << " to " << CI->getType();
1575+
if (auto *ofci = dyn_cast<OwnershipForwardingConversionInst>(CI)) {
1576+
printForwardingOwnershipKind(ofci, ofci->getOperand(0));
1577+
}
15671578
}
15681579

15691580
void visitUncheckedOwnershipConversionInst(
@@ -1578,6 +1589,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
15781589
if (CI->withoutActuallyEscaping())
15791590
*this << "[without_actually_escaping] ";
15801591
*this << CI->getType();
1592+
printForwardingOwnershipKind(CI, CI->getOperand());
15811593
}
15821594
void visitConvertEscapeToNoEscapeInst(ConvertEscapeToNoEscapeInst *CI) {
15831595
*this << (CI->isLifetimeGuaranteed() ? "" : "[not_guaranteed] ")
@@ -1661,10 +1673,12 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
16611673
void visitRefToBridgeObjectInst(RefToBridgeObjectInst *I) {
16621674
*this << getIDAndType(I->getConverted()) << ", "
16631675
<< getIDAndType(I->getBitsOperand());
1676+
printForwardingOwnershipKind(I, I->getConverted());
16641677
}
16651678

16661679
void visitBridgeObjectToRefInst(BridgeObjectToRefInst *I) {
16671680
printUncheckedConversionInst(I, I->getOperand());
1681+
printForwardingOwnershipKind(I, I->getOperand());
16681682
}
16691683
void visitBridgeObjectToWordInst(BridgeObjectToWordInst *I) {
16701684
printUncheckedConversionInst(I, I->getOperand());
@@ -1748,6 +1762,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
17481762
<< SILDeclRef(UI->getElement(), SILDeclRef::Kind::EnumElement);
17491763
if (UI->hasOperand()) {
17501764
*this << ", " << getIDAndType(UI->getOperand());
1765+
printForwardingOwnershipKind(UI, UI->getOperand());
17511766
}
17521767
}
17531768

@@ -1759,6 +1774,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
17591774
void visitUncheckedEnumDataInst(UncheckedEnumDataInst *UDAI) {
17601775
*this << getIDAndType(UDAI->getOperand()) << ", "
17611776
<< SILDeclRef(UDAI->getElement(), SILDeclRef::Kind::EnumElement);
1777+
printForwardingOwnershipKind(UDAI, UDAI->getOperand());
17621778
}
17631779

17641780
void visitUncheckedTakeEnumDataAddrInst(UncheckedTakeEnumDataAddrInst *UDAI) {
@@ -1773,6 +1789,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
17731789

17741790
void visitTupleExtractInst(TupleExtractInst *EI) {
17751791
*this << getIDAndType(EI->getOperand()) << ", " << EI->getFieldIndex();
1792+
printForwardingOwnershipKind(EI, EI->getOperand());
17761793
}
17771794

17781795
void visitTupleElementAddrInst(TupleElementAddrInst *EI) {
@@ -1782,6 +1799,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
17821799
*this << getIDAndType(EI->getOperand()) << ", #";
17831800
printFullContext(EI->getField()->getDeclContext(), PrintState.OS);
17841801
*this << EI->getField()->getName().get();
1802+
printForwardingOwnershipKind(EI, EI->getOperand());
17851803
}
17861804
void visitStructElementAddrInst(StructElementAddrInst *EI) {
17871805
*this << getIDAndType(EI->getOperand()) << ", #";
@@ -1802,10 +1820,12 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
18021820

18031821
void visitDestructureStructInst(DestructureStructInst *DSI) {
18041822
*this << getIDAndType(DSI->getOperand());
1823+
printForwardingOwnershipKind(DSI, DSI->getOperand());
18051824
}
18061825

18071826
void visitDestructureTupleInst(DestructureTupleInst *DTI) {
18081827
*this << getIDAndType(DTI->getOperand());
1828+
printForwardingOwnershipKind(DTI, DTI->getOperand());
18091829
}
18101830

18111831
void printMethodInst(MethodInst *I, SILValue Operand) {
@@ -1859,6 +1879,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
18591879
}
18601880
void visitOpenExistentialRefInst(OpenExistentialRefInst *OI) {
18611881
*this << getIDAndType(OI->getOperand()) << " to " << OI->getType();
1882+
printForwardingOwnershipKind(OI, OI->getOperand());
18621883
}
18631884
void visitOpenExistentialMetatypeInst(OpenExistentialMetatypeInst *OI) {
18641885
*this << getIDAndType(OI->getOperand()) << " to " << OI->getType();
@@ -1868,9 +1889,11 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
18681889
}
18691890
void visitOpenExistentialBoxValueInst(OpenExistentialBoxValueInst *OI) {
18701891
*this << getIDAndType(OI->getOperand()) << " to " << OI->getType();
1892+
printForwardingOwnershipKind(OI, OI->getOperand());
18711893
}
18721894
void visitOpenExistentialValueInst(OpenExistentialValueInst *OI) {
18731895
*this << getIDAndType(OI->getOperand()) << " to " << OI->getType();
1896+
printForwardingOwnershipKind(OI, OI->getOperand());
18741897
}
18751898
void visitInitExistentialAddrInst(InitExistentialAddrInst *AEI) {
18761899
*this << getIDAndType(AEI->getOperand()) << ", $"
@@ -1886,6 +1909,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
18861909
*this << getIDAndType(AEI->getOperand()) << " : $"
18871910
<< AEI->getFormalConcreteType() << ", " << AEI->getType();
18881911
printConformances(AEI->getConformances());
1912+
printForwardingOwnershipKind(AEI, AEI->getOperand());
18891913
}
18901914
void visitInitExistentialMetatypeInst(InitExistentialMetatypeInst *EMI) {
18911915
*this << getIDAndType(EMI->getOperand()) << ", " << EMI->getType();
@@ -1939,6 +1963,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
19391963
void visitMarkDependenceInst(MarkDependenceInst *MDI) {
19401964
*this << getIDAndType(MDI->getValue()) << " on "
19411965
<< getIDAndType(MDI->getBase());
1966+
printForwardingOwnershipKind(MDI, MDI->getValue());
19421967
}
19431968
void visitCopyBlockInst(CopyBlockInst *RI) {
19441969
*this << getIDAndType(RI->getOperand());
@@ -2133,6 +2158,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
21332158

21342159
void visitSwitchEnumInst(SwitchEnumInst *SOI) {
21352160
printSwitchEnumInst(SOI);
2161+
printForwardingOwnershipKind(SOI, SOI->getOperand());
21362162
}
21372163
void visitSwitchEnumAddrInst(SwitchEnumAddrInst *SOI) {
21382164
printSwitchEnumInst(SOI);
@@ -2156,6 +2182,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
21562182

21572183
void visitSelectEnumInst(SelectEnumInst *SEI) {
21582184
printSelectEnumInst(SEI);
2185+
printForwardingOwnershipKind(SEI, SEI->getOperand());
21592186
}
21602187
void visitSelectEnumAddrInst(SelectEnumAddrInst *SEI) {
21612188
printSelectEnumInst(SEI);
@@ -2174,6 +2201,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
21742201
*this << ", default " << Ctx.getID(SVI->getDefaultResult());
21752202

21762203
*this << " : " << SVI->getType();
2204+
printForwardingOwnershipKind(SVI, SVI->getOperand());
21772205
}
21782206

21792207
void visitDynamicMethodBranchInst(DynamicMethodBranchInst *DMBI) {
@@ -2411,6 +2439,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24112439
*this << " as ";
24122440
*this << dfei->getType();
24132441
}
2442+
printForwardingOwnershipKind(dfei, dfei->getOperand());
24142443
}
24152444

24162445
void visitLinearFunctionExtractInst(LinearFunctionExtractInst *lfei) {
@@ -2425,6 +2454,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24252454
}
24262455
*this << "] ";
24272456
*this << getIDAndType(lfei->getOperand());
2457+
printForwardingOwnershipKind(lfei, lfei->getOperand());
24282458
}
24292459

24302460
void visitDifferentiabilityWitnessFunctionInst(

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,10 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
28932893

28942894
require(EI->getFieldIndex() < operandTy->getNumElements(),
28952895
"invalid field index for tuple_extract instruction");
2896+
2897+
require(EI->getForwardingOwnershipKind() == OwnershipKind::None ||
2898+
EI->getForwardingOwnershipKind() == OwnershipKind::Guaranteed,
2899+
"invalid forwarding ownership kind on tuple_extract instruction");
28962900
if (EI->getModule().getStage() != SILStage::Lowered) {
28972901
requireSameType(EI->getType().getASTType(),
28982902
operandTy.getElementType(EI->getFieldIndex()),
@@ -2919,6 +2923,10 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
29192923
require(EI->getField()->getDeclContext() == sd,
29202924
"struct_extract field is not a member of the struct");
29212925

2926+
require(EI->getForwardingOwnershipKind() == OwnershipKind::None ||
2927+
EI->getForwardingOwnershipKind() == OwnershipKind::Guaranteed,
2928+
"invalid forwarding ownership kind on tuple_extract instruction");
2929+
29222930
if (EI->getModule().getStage() != SILStage::Lowered) {
29232931
SILType loweredFieldTy = operandTy.getFieldType(
29242932
EI->getField(), F.getModule(), F.getTypeExpansionContext());

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4931,7 +4931,7 @@ bb2(%error : @owned $Error):
49314931
sil [ossa] @takeKlass : $@convention(thin) (@owned Optional<Klass>) -> ()
49324932

49334933
// CHECK_FORWARDING_OWNERSHIP_KIND-LABEL: sil shared [ossa] @$s8refcast24main5KlassC_Tg5 :
4934-
// CHECK_FORWARDING_OWNERSHIP_KIND: unchecked_ref_cast
4934+
// CHECK_FORWARDING_OWNERSHIP_KIND: unchecked_ref_cast %1 : $Klass to $Optional<Klass>, forwarding: @unowned
49354935
// CHECK_FORWARDING_OWNERSHIP_KIND-LABEL: } // end sil function '$s8refcast24main5KlassC_Tg5'
49364936
sil [ossa] @refcast2 : $@convention(thin) <T> (@in T, @owned Klass) -> () {
49374937
bb0(%0 : $*T, %1 : @owned $Klass):

0 commit comments

Comments
 (0)