Skip to content

Commit 611e676

Browse files
committed
[borrowing/consuming] Add a new instruction called copyable_to_moveonlywrapper_addr.
Just the $*T -> $*@moveOnly T variant for addresses. Unlike the object version this acts like a cast rather than something that provides semantics from the frontend to the optimizer.
1 parent 43f42e2 commit 611e676

20 files changed

+139
-4
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,12 @@ class SILBuilder {
14191419
getSILDebugLocation(loc), src));
14201420
}
14211421

1422+
CopyableToMoveOnlyWrapperAddrInst *
1423+
createCopyableToMoveOnlyWrapperAddr(SILLocation loc, SILValue src) {
1424+
return insert(new (getModule()) CopyableToMoveOnlyWrapperAddrInst(
1425+
getSILDebugLocation(loc), src));
1426+
}
1427+
14221428
MoveOnlyWrapperToCopyableValueInst *
14231429
createOwnedMoveOnlyWrapperToCopyableValue(SILLocation loc, SILValue src) {
14241430
return insert(new (getModule()) MoveOnlyWrapperToCopyableValueInst(

include/swift/SIL/SILCloner.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,15 @@ void SILCloner<ImplClass>::visitMoveOnlyWrapperToCopyableAddrInst(
19501950
getOpLocation(inst->getLoc()), getOpValue(inst->getOperand())));
19511951
}
19521952

1953+
template <typename ImplClass>
1954+
void SILCloner<ImplClass>::visitCopyableToMoveOnlyWrapperAddrInst(
1955+
CopyableToMoveOnlyWrapperAddrInst *inst) {
1956+
getBuilder().setCurrentDebugScope(getOpScope(inst->getDebugScope()));
1957+
recordClonedInstruction(
1958+
inst, getBuilder().createCopyableToMoveOnlyWrapperAddr(
1959+
getOpLocation(inst->getLoc()), getOpValue(inst->getOperand())));
1960+
}
1961+
19531962
template <typename ImplClass>
19541963
void SILCloner<ImplClass>::visitCopyableToMoveOnlyWrapperValueInst(
19551964
CopyableToMoveOnlyWrapperValueInst *inst) {

include/swift/SIL/SILInstruction.h

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

8577+
class CopyableToMoveOnlyWrapperAddrInst
8578+
: public UnaryInstructionBase<
8579+
SILInstructionKind::CopyableToMoveOnlyWrapperAddrInst,
8580+
SingleValueInstruction> {
8581+
friend class SILBuilder;
8582+
8583+
CopyableToMoveOnlyWrapperAddrInst(SILDebugLocation DebugLoc, SILValue operand)
8584+
: UnaryInstructionBase(DebugLoc, operand,
8585+
operand->getType().addingMoveOnlyWrapper()) {}
8586+
};
8587+
85778588
class MoveOnlyWrapperToCopyableAddrInst
85788589
: public UnaryInstructionBase<
85798590
SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst,
85808591
SingleValueInstruction> {
85818592
friend class SILBuilder;
85828593

8583-
MoveOnlyWrapperToCopyableAddrInst(const SILFunction &fn,
8584-
SILDebugLocation DebugLoc, SILValue operand)
8594+
MoveOnlyWrapperToCopyableAddrInst(SILDebugLocation DebugLoc, SILValue operand)
85858595
: UnaryInstructionBase(DebugLoc, operand,
8586-
operand->getType().removingMoveOnlyWrapper()) {
8587-
}
8596+
operand->getType().removingMoveOnlyWrapper()) {}
85888597
};
85898598

85908599
/// Given an object reference, return true iff it is non-nil and refers

include/swift/SIL/SILNodes.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
500500
SINGLE_VALUE_INST(MoveOnlyWrapperToCopyableAddrInst,
501501
moveonlywrapper_to_copyable_addr, SingleValueInstruction,
502502
None, DoesNotRelease)
503+
// Convert a $*T to $*@moveOnly T. Acts just as a cast.
504+
SINGLE_VALUE_INST(CopyableToMoveOnlyWrapperAddrInst,
505+
copyable_to_moveonlywrapper_addr, SingleValueInstruction,
506+
None, DoesNotRelease)
503507
// A move_addr is a Raw SIL only instruction that is equivalent to a copy_addr
504508
// [init]. It is lowered during the diagnostic passes to a copy_addr [init] if
505509
// 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
@@ -1274,6 +1274,11 @@ class IRGenSILFunction :
12741274
auto e = getLoweredExplosion(i->getOperand());
12751275
setLoweredExplosion(i, e);
12761276
}
1277+
void
1278+
visitCopyableToMoveOnlyWrapperAddrInst(CopyableToMoveOnlyWrapperAddrInst *i) {
1279+
auto e = getLoweredExplosion(i->getOperand());
1280+
setLoweredExplosion(i, e);
1281+
}
12771282
void visitReleaseValueInst(ReleaseValueInst *i);
12781283
void visitReleaseValueAddrInst(ReleaseValueAddrInst *i);
12791284
void visitDestroyValueInst(DestroyValueInst *i);

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ OPERAND_OWNERSHIP(TrivialUse, AllocRef) // with tail operand
149149
OPERAND_OWNERSHIP(TrivialUse, AllocRefDynamic) // with tail operand
150150
OPERAND_OWNERSHIP(TrivialUse, BeginAccess)
151151
OPERAND_OWNERSHIP(TrivialUse, MoveOnlyWrapperToCopyableAddr)
152+
OPERAND_OWNERSHIP(TrivialUse, CopyableToMoveOnlyWrapperAddr)
152153
OPERAND_OWNERSHIP(TrivialUse, BeginUnpairedAccess)
153154
OPERAND_OWNERSHIP(TrivialUse, BindMemory)
154155
OPERAND_OWNERSHIP(TrivialUse, RebindMemory)

lib/SIL/IR/SILPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,6 +2494,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24942494
MoveOnlyWrapperToCopyableAddrInst *BAI) {
24952495
*this << getIDAndType(BAI->getOperand());
24962496
}
2497+
void visitCopyableToMoveOnlyWrapperAddrInst(
2498+
CopyableToMoveOnlyWrapperAddrInst *BAI) {
2499+
*this << getIDAndType(BAI->getOperand());
2500+
}
24972501
void visitEndAccessInst(EndAccessInst *EAI) {
24982502
*this << (EAI->isAborting() ? "[abort] " : "")
24992503
<< getIDAndType(EAI->getOperand());

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ CONSTANT_OWNERSHIP_INST(None, AllocPackMetadata)
9898
CONSTANT_OWNERSHIP_INST(None, PackLength)
9999
CONSTANT_OWNERSHIP_INST(None, BeginAccess)
100100
CONSTANT_OWNERSHIP_INST(None, MoveOnlyWrapperToCopyableAddr)
101+
CONSTANT_OWNERSHIP_INST(None, CopyableToMoveOnlyWrapperAddr)
101102
CONSTANT_OWNERSHIP_INST(None, BindMemory)
102103
CONSTANT_OWNERSHIP_INST(None, RebindMemory)
103104
CONSTANT_OWNERSHIP_INST(None, BridgeObjectToWord)

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4626,6 +4626,29 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
46264626
ResultVal = B.createMoveOnlyWrapperToCopyableAddr(InstLoc, addrVal);
46274627
break;
46284628
}
4629+
case SILInstructionKind::CopyableToMoveOnlyWrapperAddrInst: {
4630+
SILValue addrVal;
4631+
SourceLoc addrLoc;
4632+
if (parseTypedValueRef(addrVal, addrLoc, B))
4633+
return true;
4634+
4635+
if (parseSILDebugLocation(InstLoc, B))
4636+
return true;
4637+
4638+
if (!addrVal->getType().isAddress()) {
4639+
P.diagnose(addrLoc, diag::sil_operand_not_address, "operand", OpcodeName);
4640+
return true;
4641+
}
4642+
4643+
if (addrVal->getType().isMoveOnlyWrapped()) {
4644+
P.diagnose(addrLoc, diag::sil_operand_has_incorrect_moveonlywrapped,
4645+
"operand", OpcodeName, 1);
4646+
return true;
4647+
}
4648+
4649+
ResultVal = B.createCopyableToMoveOnlyWrapperAddr(InstLoc, addrVal);
4650+
break;
4651+
}
46294652
case SILInstructionKind::BeginAccessInst:
46304653
case SILInstructionKind::BeginUnpairedAccessInst:
46314654
case SILInstructionKind::EndAccessInst:

lib/SIL/Utils/AddressWalker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ AddressUseKind TransitiveAddressWalker::walk(SILValue projectedAddress) && {
160160
isa<MarkUninitializedInst>(user) || isa<DropDeinitInst>(user) ||
161161
isa<ProjectBlockStorageInst>(user) || isa<UpcastInst>(user) ||
162162
isa<TuplePackElementAddrInst>(user) ||
163+
isa<CopyableToMoveOnlyWrapperAddrInst>(user) ||
163164
isa<MoveOnlyWrapperToCopyableAddrInst>(user)) {
164165
transitiveResultUses(op);
165166
continue;

0 commit comments

Comments
 (0)