Skip to content

Commit ca8341a

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. (cherry picked from commit 611e676)
1 parent fb8078b commit ca8341a

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
@@ -1416,6 +1416,12 @@ class SILBuilder {
14161416
getSILDebugLocation(loc), src));
14171417
}
14181418

1419+
CopyableToMoveOnlyWrapperAddrInst *
1420+
createCopyableToMoveOnlyWrapperAddr(SILLocation loc, SILValue src) {
1421+
return insert(new (getModule()) CopyableToMoveOnlyWrapperAddrInst(
1422+
getSILDebugLocation(loc), src));
1423+
}
1424+
14191425
MoveOnlyWrapperToCopyableValueInst *
14201426
createOwnedMoveOnlyWrapperToCopyableValue(SILLocation loc, SILValue src) {
14211427
return insert(new (getModule()) MoveOnlyWrapperToCopyableValueInst(

include/swift/SIL/SILCloner.h

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

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

include/swift/SIL/SILInstruction.h

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

8543+
class CopyableToMoveOnlyWrapperAddrInst
8544+
: public UnaryInstructionBase<
8545+
SILInstructionKind::CopyableToMoveOnlyWrapperAddrInst,
8546+
SingleValueInstruction> {
8547+
friend class SILBuilder;
8548+
8549+
CopyableToMoveOnlyWrapperAddrInst(SILDebugLocation DebugLoc, SILValue operand)
8550+
: UnaryInstructionBase(DebugLoc, operand,
8551+
operand->getType().addingMoveOnlyWrapper()) {}
8552+
};
8553+
85438554
class MoveOnlyWrapperToCopyableAddrInst
85448555
: public UnaryInstructionBase<
85458556
SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst,
85468557
SingleValueInstruction> {
85478558
friend class SILBuilder;
85488559

8549-
MoveOnlyWrapperToCopyableAddrInst(const SILFunction &fn,
8550-
SILDebugLocation DebugLoc, SILValue operand)
8560+
MoveOnlyWrapperToCopyableAddrInst(SILDebugLocation DebugLoc, SILValue operand)
85518561
: UnaryInstructionBase(DebugLoc, operand,
8552-
operand->getType().removingMoveOnlyWrapper()) {
8553-
}
8562+
operand->getType().removingMoveOnlyWrapper()) {}
85548563
};
85558564

85568565
/// 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
@@ -1273,6 +1273,11 @@ class IRGenSILFunction :
12731273
auto e = getLoweredExplosion(i->getOperand());
12741274
setLoweredExplosion(i, e);
12751275
}
1276+
void
1277+
visitCopyableToMoveOnlyWrapperAddrInst(CopyableToMoveOnlyWrapperAddrInst *i) {
1278+
auto e = getLoweredExplosion(i->getOperand());
1279+
setLoweredExplosion(i, e);
1280+
}
12761281
void visitReleaseValueInst(ReleaseValueInst *i);
12771282
void visitReleaseValueAddrInst(ReleaseValueAddrInst *i);
12781283
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
@@ -2463,6 +2463,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24632463
MoveOnlyWrapperToCopyableAddrInst *BAI) {
24642464
*this << getIDAndType(BAI->getOperand());
24652465
}
2466+
void visitCopyableToMoveOnlyWrapperAddrInst(
2467+
CopyableToMoveOnlyWrapperAddrInst *BAI) {
2468+
*this << getIDAndType(BAI->getOperand());
2469+
}
24662470
void visitEndAccessInst(EndAccessInst *EAI) {
24672471
*this << (EAI->isAborting() ? "[abort] " : "")
24682472
<< 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
@@ -4565,6 +4565,29 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
45654565
ResultVal = B.createMoveOnlyWrapperToCopyableAddr(InstLoc, addrVal);
45664566
break;
45674567
}
4568+
case SILInstructionKind::CopyableToMoveOnlyWrapperAddrInst: {
4569+
SILValue addrVal;
4570+
SourceLoc addrLoc;
4571+
if (parseTypedValueRef(addrVal, addrLoc, B))
4572+
return true;
4573+
4574+
if (parseSILDebugLocation(InstLoc, B))
4575+
return true;
4576+
4577+
if (!addrVal->getType().isAddress()) {
4578+
P.diagnose(addrLoc, diag::sil_operand_not_address, "operand", OpcodeName);
4579+
return true;
4580+
}
4581+
4582+
if (addrVal->getType().isMoveOnlyWrapped()) {
4583+
P.diagnose(addrLoc, diag::sil_operand_has_incorrect_moveonlywrapped,
4584+
"operand", OpcodeName, 1);
4585+
return true;
4586+
}
4587+
4588+
ResultVal = B.createCopyableToMoveOnlyWrapperAddr(InstLoc, addrVal);
4589+
break;
4590+
}
45684591
case SILInstructionKind::BeginAccessInst:
45694592
case SILInstructionKind::BeginUnpairedAccessInst:
45704593
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)