Skip to content

Commit fb8078b

Browse files
committed
[borrowing/consuming] Add new instruction: moveonlywrapper_to_copyable_addr.
The reason why I am using a different instruction for addresses and objects here is that the object checker doesnt have to deal with things like initialization. (cherry picked from commit 43f42e2) Conflicts: lib/SIL/Verifier/SILVerifier.cpp lib/Serialization/ModuleFormat.h test/SIL/Parser/basic2.sil
1 parent e4d0f31 commit fb8078b

23 files changed

+276
-8
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,11 @@ ERROR(sil_ref_inst_wrong_field,none,
602602
ERROR(sil_invalid_instr_operands,none,
603603
"invalid instruction operands", ())
604604
ERROR(sil_operand_not_address,none,
605-
"%0 operand of '%1' must have address type", (StringRef, StringRef))
605+
"%0 of '%1' must have address type", (StringRef, StringRef))
606+
ERROR(sil_operand_not_object,none,
607+
"%0 of '%1' must have object type", (StringRef, StringRef))
608+
ERROR(sil_operand_has_incorrect_moveonlywrapped,none,
609+
"%0 of '%1' %select{must|must not}2 be of moveonlywrapped type", (StringRef, StringRef, unsigned))
606610
ERROR(sil_operand_not_ref_storage_address,none,
607611
"%0 operand of '%1' must have address of %2 type",
608612
(StringRef, StringRef, ReferenceOwnership))

include/swift/SIL/SILBuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,12 @@ class SILBuilder {
14101410
CopyableToMoveOnlyWrapperValueInst::Guaranteed));
14111411
}
14121412

1413+
MoveOnlyWrapperToCopyableAddrInst *
1414+
createMoveOnlyWrapperToCopyableAddr(SILLocation loc, SILValue src) {
1415+
return insert(new (getModule()) MoveOnlyWrapperToCopyableAddrInst(
1416+
getSILDebugLocation(loc), src));
1417+
}
1418+
14131419
MoveOnlyWrapperToCopyableValueInst *
14141420
createOwnedMoveOnlyWrapperToCopyableValue(SILLocation loc, SILValue src) {
14151421
return insert(new (getModule()) MoveOnlyWrapperToCopyableValueInst(

include/swift/SIL/SILCloner.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,15 @@ void SILCloner<ImplClass>::visitMoveOnlyWrapperToCopyableValueInst(
19421942
recordClonedInstruction(inst, cvt);
19431943
}
19441944

1945+
template <typename ImplClass>
1946+
void SILCloner<ImplClass>::visitMoveOnlyWrapperToCopyableAddrInst(
1947+
MoveOnlyWrapperToCopyableAddrInst *inst) {
1948+
getBuilder().setCurrentDebugScope(getOpScope(inst->getDebugScope()));
1949+
recordClonedInstruction(
1950+
inst, getBuilder().createMoveOnlyWrapperToCopyableAddr(
1951+
getOpLocation(inst->getLoc()), getOpValue(inst->getOperand())));
1952+
}
1953+
19451954
template <typename ImplClass>
19461955
void SILCloner<ImplClass>::visitCopyableToMoveOnlyWrapperValueInst(
19471956
CopyableToMoveOnlyWrapperValueInst *inst) {

include/swift/SIL/SILInstruction.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8540,6 +8540,19 @@ class MoveOnlyWrapperToCopyableValueInst
85408540
InitialKind getInitialKind() const { return initialKind; }
85418541
};
85428542

8543+
class MoveOnlyWrapperToCopyableAddrInst
8544+
: public UnaryInstructionBase<
8545+
SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst,
8546+
SingleValueInstruction> {
8547+
friend class SILBuilder;
8548+
8549+
MoveOnlyWrapperToCopyableAddrInst(const SILFunction &fn,
8550+
SILDebugLocation DebugLoc, SILValue operand)
8551+
: UnaryInstructionBase(DebugLoc, operand,
8552+
operand->getType().removingMoveOnlyWrapper()) {
8553+
}
8554+
};
8555+
85438556
/// Given an object reference, return true iff it is non-nil and refers
85448557
/// to a native swift object with strong reference count of 1.
85458558
class IsUniqueInst

include/swift/SIL/SILNodes.def

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,16 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
490490
// and owned for assignment/owned function arguments.
491491
SINGLE_VALUE_INST(CopyableToMoveOnlyWrapperValueInst, copyable_to_moveonlywrapper,
492492
SingleValueInstruction, None, DoesNotRelease)
493-
// Convert a $@moveOnly T to $T. Ownership is fixed at construction by
493+
// Convert a $@moveOnly T object to $T. Ownership is fixed at construction by
494494
// frontend to express specific semantics: guaranteed for function arguments
495495
// and owned for assignment/return values.
496-
SINGLE_VALUE_INST(MoveOnlyWrapperToCopyableValueInst, moveonlywrapper_to_copyable,
497-
SingleValueInstruction, None, DoesNotRelease)
496+
SINGLE_VALUE_INST(MoveOnlyWrapperToCopyableValueInst,
497+
moveonlywrapper_to_copyable, SingleValueInstruction, None,
498+
DoesNotRelease)
499+
// Convert a $*@moveOnly T to $*T. Acts just as a cast.
500+
SINGLE_VALUE_INST(MoveOnlyWrapperToCopyableAddrInst,
501+
moveonlywrapper_to_copyable_addr, SingleValueInstruction,
502+
None, DoesNotRelease)
498503
// A move_addr is a Raw SIL only instruction that is equivalent to a copy_addr
499504
// [init]. It is lowered during the diagnostic passes to a copy_addr [init] if
500505
// the move checker found uses that prevented us from converting this to a

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ class IRGenSILFunction :
12681268
auto e = getLoweredExplosion(i->getOperand());
12691269
setLoweredExplosion(i, e);
12701270
}
1271+
void
1272+
visitMoveOnlyWrapperToCopyableAddrInst(MoveOnlyWrapperToCopyableAddrInst *i) {
1273+
auto e = getLoweredExplosion(i->getOperand());
1274+
setLoweredExplosion(i, e);
1275+
}
12711276
void visitReleaseValueInst(ReleaseValueInst *i);
12721277
void visitReleaseValueAddrInst(ReleaseValueAddrInst *i);
12731278
void visitDestroyValueInst(DestroyValueInst *i);

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ OPERAND_OWNERSHIP(TrivialUse, AddressToPointer)
148148
OPERAND_OWNERSHIP(TrivialUse, AllocRef) // with tail operand
149149
OPERAND_OWNERSHIP(TrivialUse, AllocRefDynamic) // with tail operand
150150
OPERAND_OWNERSHIP(TrivialUse, BeginAccess)
151+
OPERAND_OWNERSHIP(TrivialUse, MoveOnlyWrapperToCopyableAddr)
151152
OPERAND_OWNERSHIP(TrivialUse, BeginUnpairedAccess)
152153
OPERAND_OWNERSHIP(TrivialUse, BindMemory)
153154
OPERAND_OWNERSHIP(TrivialUse, RebindMemory)

lib/SIL/IR/SILPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24592459
<< (BAI->isFromBuiltin() ? "[builtin] " : "")
24602460
<< getIDAndType(BAI->getOperand());
24612461
}
2462+
void visitMoveOnlyWrapperToCopyableAddrInst(
2463+
MoveOnlyWrapperToCopyableAddrInst *BAI) {
2464+
*this << getIDAndType(BAI->getOperand());
2465+
}
24622466
void visitEndAccessInst(EndAccessInst *EAI) {
24632467
*this << (EAI->isAborting() ? "[abort] " : "")
24642468
<< getIDAndType(EAI->getOperand());

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ CONSTANT_OWNERSHIP_INST(None, AllocPack)
9797
CONSTANT_OWNERSHIP_INST(None, AllocPackMetadata)
9898
CONSTANT_OWNERSHIP_INST(None, PackLength)
9999
CONSTANT_OWNERSHIP_INST(None, BeginAccess)
100+
CONSTANT_OWNERSHIP_INST(None, MoveOnlyWrapperToCopyableAddr)
100101
CONSTANT_OWNERSHIP_INST(None, BindMemory)
101102
CONSTANT_OWNERSHIP_INST(None, RebindMemory)
102103
CONSTANT_OWNERSHIP_INST(None, BridgeObjectToWord)

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3800,6 +3800,20 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
38003800
return true;
38013801
if (parseSILDebugLocation(InstLoc, B))
38023802
return true;
3803+
3804+
if (!Val->getType().isObject()) {
3805+
P.diagnose(InstLoc.getSourceLoc(),
3806+
diag::sil_operand_not_object, "operand", OpcodeName);
3807+
return true;
3808+
}
3809+
3810+
if (Val->getType().isMoveOnlyWrapped()) {
3811+
P.diagnose(InstLoc.getSourceLoc(),
3812+
diag::sil_operand_has_incorrect_moveonlywrapped,
3813+
"operand", OpcodeName, 1);
3814+
return true;
3815+
}
3816+
38033817
if (OwnershipKind == OwnershipKind::Owned)
38043818
ResultVal = B.createOwnedCopyableToMoveOnlyWrapperValue(InstLoc, Val);
38053819
else
@@ -3832,6 +3846,20 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
38323846
return true;
38333847
if (parseSILDebugLocation(InstLoc, B))
38343848
return true;
3849+
3850+
if (!Val->getType().isObject()) {
3851+
P.diagnose(InstLoc.getSourceLoc(),
3852+
diag::sil_operand_not_object, "operand", OpcodeName);
3853+
return true;
3854+
}
3855+
3856+
if (!Val->getType().isMoveOnlyWrapped()) {
3857+
P.diagnose(InstLoc.getSourceLoc(),
3858+
diag::sil_operand_has_incorrect_moveonlywrapped,
3859+
"operand", OpcodeName, 0);
3860+
return true;
3861+
}
3862+
38353863
if (OwnershipKind == OwnershipKind::Owned)
38363864
ResultVal = B.createOwnedMoveOnlyWrapperToCopyableValue(InstLoc, Val);
38373865
else
@@ -4514,6 +4542,29 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
45144542
break;
45154543
}
45164544

4545+
case SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst: {
4546+
SILValue addrVal;
4547+
SourceLoc addrLoc;
4548+
if (parseTypedValueRef(addrVal, addrLoc, B))
4549+
return true;
4550+
4551+
if (parseSILDebugLocation(InstLoc, B))
4552+
return true;
4553+
4554+
if (!addrVal->getType().isAddress()) {
4555+
P.diagnose(addrLoc, diag::sil_operand_not_address, "operand", OpcodeName);
4556+
return true;
4557+
}
4558+
4559+
if (!addrVal->getType().isMoveOnlyWrapped()) {
4560+
P.diagnose(addrLoc, diag::sil_operand_has_incorrect_moveonlywrapped,
4561+
"operand", OpcodeName, 0);
4562+
return true;
4563+
}
4564+
4565+
ResultVal = B.createMoveOnlyWrapperToCopyableAddr(InstLoc, addrVal);
4566+
break;
4567+
}
45174568
case SILInstructionKind::BeginAccessInst:
45184569
case SILInstructionKind::BeginUnpairedAccessInst:
45194570
case SILInstructionKind::EndAccessInst:

0 commit comments

Comments
 (0)