Skip to content

Commit 43f42e2

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.
1 parent 76374f8 commit 43f42e2

23 files changed

+274
-7
lines changed

include/swift/AST/DiagnosticsParse.def

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

include/swift/SIL/SILBuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,12 @@ class SILBuilder {
14131413
CopyableToMoveOnlyWrapperValueInst::Guaranteed));
14141414
}
14151415

1416+
MoveOnlyWrapperToCopyableAddrInst *
1417+
createMoveOnlyWrapperToCopyableAddr(SILLocation loc, SILValue src) {
1418+
return insert(new (getModule()) MoveOnlyWrapperToCopyableAddrInst(
1419+
getSILDebugLocation(loc), src));
1420+
}
1421+
14161422
MoveOnlyWrapperToCopyableValueInst *
14171423
createOwnedMoveOnlyWrapperToCopyableValue(SILLocation loc, SILValue src) {
14181424
return insert(new (getModule()) MoveOnlyWrapperToCopyableValueInst(

include/swift/SIL/SILCloner.h

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

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

include/swift/SIL/SILInstruction.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8574,6 +8574,19 @@ class MoveOnlyWrapperToCopyableValueInst
85748574
InitialKind getInitialKind() const { return initialKind; }
85758575
};
85768576

8577+
class MoveOnlyWrapperToCopyableAddrInst
8578+
: public UnaryInstructionBase<
8579+
SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst,
8580+
SingleValueInstruction> {
8581+
friend class SILBuilder;
8582+
8583+
MoveOnlyWrapperToCopyableAddrInst(const SILFunction &fn,
8584+
SILDebugLocation DebugLoc, SILValue operand)
8585+
: UnaryInstructionBase(DebugLoc, operand,
8586+
operand->getType().removingMoveOnlyWrapper()) {
8587+
}
8588+
};
8589+
85778590
/// Given an object reference, return true iff it is non-nil and refers
85788591
/// to a native swift object with strong reference count of 1.
85798592
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
@@ -1269,6 +1269,11 @@ class IRGenSILFunction :
12691269
auto e = getLoweredExplosion(i->getOperand());
12701270
setLoweredExplosion(i, e);
12711271
}
1272+
void
1273+
visitMoveOnlyWrapperToCopyableAddrInst(MoveOnlyWrapperToCopyableAddrInst *i) {
1274+
auto e = getLoweredExplosion(i->getOperand());
1275+
setLoweredExplosion(i, e);
1276+
}
12721277
void visitReleaseValueInst(ReleaseValueInst *i);
12731278
void visitReleaseValueAddrInst(ReleaseValueAddrInst *i);
12741279
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
@@ -2490,6 +2490,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24902490
<< (BAI->isFromBuiltin() ? "[builtin] " : "")
24912491
<< getIDAndType(BAI->getOperand());
24922492
}
2493+
void visitMoveOnlyWrapperToCopyableAddrInst(
2494+
MoveOnlyWrapperToCopyableAddrInst *BAI) {
2495+
*this << getIDAndType(BAI->getOperand());
2496+
}
24932497
void visitEndAccessInst(EndAccessInst *EAI) {
24942498
*this << (EAI->isAborting() ? "[abort] " : "")
24952499
<< 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
@@ -3847,6 +3847,20 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
38473847
return true;
38483848
if (parseSILDebugLocation(InstLoc, B))
38493849
return true;
3850+
3851+
if (!Val->getType().isObject()) {
3852+
P.diagnose(InstLoc.getSourceLoc(),
3853+
diag::sil_operand_not_object, "operand", OpcodeName);
3854+
return true;
3855+
}
3856+
3857+
if (Val->getType().isMoveOnlyWrapped()) {
3858+
P.diagnose(InstLoc.getSourceLoc(),
3859+
diag::sil_operand_has_incorrect_moveonlywrapped,
3860+
"operand", OpcodeName, 1);
3861+
return true;
3862+
}
3863+
38503864
if (OwnershipKind == OwnershipKind::Owned)
38513865
ResultVal = B.createOwnedCopyableToMoveOnlyWrapperValue(InstLoc, Val);
38523866
else
@@ -3879,6 +3893,20 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
38793893
return true;
38803894
if (parseSILDebugLocation(InstLoc, B))
38813895
return true;
3896+
3897+
if (!Val->getType().isObject()) {
3898+
P.diagnose(InstLoc.getSourceLoc(),
3899+
diag::sil_operand_not_object, "operand", OpcodeName);
3900+
return true;
3901+
}
3902+
3903+
if (!Val->getType().isMoveOnlyWrapped()) {
3904+
P.diagnose(InstLoc.getSourceLoc(),
3905+
diag::sil_operand_has_incorrect_moveonlywrapped,
3906+
"operand", OpcodeName, 0);
3907+
return true;
3908+
}
3909+
38823910
if (OwnershipKind == OwnershipKind::Owned)
38833911
ResultVal = B.createOwnedMoveOnlyWrapperToCopyableValue(InstLoc, Val);
38843912
else
@@ -4575,6 +4603,29 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
45754603
break;
45764604
}
45774605

4606+
case SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst: {
4607+
SILValue addrVal;
4608+
SourceLoc addrLoc;
4609+
if (parseTypedValueRef(addrVal, addrLoc, B))
4610+
return true;
4611+
4612+
if (parseSILDebugLocation(InstLoc, B))
4613+
return true;
4614+
4615+
if (!addrVal->getType().isAddress()) {
4616+
P.diagnose(addrLoc, diag::sil_operand_not_address, "operand", OpcodeName);
4617+
return true;
4618+
}
4619+
4620+
if (!addrVal->getType().isMoveOnlyWrapped()) {
4621+
P.diagnose(addrLoc, diag::sil_operand_has_incorrect_moveonlywrapped,
4622+
"operand", OpcodeName, 0);
4623+
return true;
4624+
}
4625+
4626+
ResultVal = B.createMoveOnlyWrapperToCopyableAddr(InstLoc, addrVal);
4627+
break;
4628+
}
45784629
case SILInstructionKind::BeginAccessInst:
45794630
case SILInstructionKind::BeginUnpairedAccessInst:
45804631
case SILInstructionKind::EndAccessInst:

0 commit comments

Comments
 (0)