Skip to content

[sil] Teach the SILCloner how to clone from ossa -> non-ossa. #22217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 116 additions & 1 deletion include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,28 @@ SILCloner<ImplClass>::visitStringLiteralInst(StringLiteralInst *Inst) {
template <typename ImplClass>
void SILCloner<ImplClass>::visitLoadInst(LoadInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(
if (!getBuilder().hasOwnership()) {
switch (Inst->getOwnershipQualifier()) {
case LoadOwnershipQualifier::Copy: {
auto *li = getBuilder().createLoad(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand()),
LoadOwnershipQualifier::Unqualified);
// This will emit a retain_value/strong_retain as appropriate.
getBuilder().emitCopyValueOperation(getOpLocation(Inst->getLoc()), li);
return recordClonedInstruction(Inst, li);
}
case LoadOwnershipQualifier::Take:
case LoadOwnershipQualifier::Trivial:
case LoadOwnershipQualifier::Unqualified:
break;
}
return recordClonedInstruction(
Inst, getBuilder().createLoad(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand()),
LoadOwnershipQualifier::Unqualified));
}

return recordClonedInstruction(
Inst, getBuilder().createLoad(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand()),
Inst->getOwnershipQualifier()));
Expand All @@ -1008,6 +1029,14 @@ void SILCloner<ImplClass>::visitLoadInst(LoadInst *Inst) {
template <typename ImplClass>
void SILCloner<ImplClass>::visitLoadBorrowInst(LoadBorrowInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
// If we are not inlining into an ownership function, just use a load.
if (!getBuilder().hasOwnership()) {
return recordClonedInstruction(
Inst, getBuilder().createLoad(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand()),
LoadOwnershipQualifier::Unqualified));
}

recordClonedInstruction(
Inst, getBuilder().createLoadBorrow(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand())));
Expand All @@ -1016,6 +1045,10 @@ void SILCloner<ImplClass>::visitLoadBorrowInst(LoadBorrowInst *Inst) {
template <typename ImplClass>
void SILCloner<ImplClass>::visitBeginBorrowInst(BeginBorrowInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
if (!getBuilder().hasOwnership()) {
return recordFoldedValue(Inst, getOpValue(Inst->getOperand()));
}

recordClonedInstruction(
Inst, getBuilder().createBeginBorrow(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand())));
Expand All @@ -1024,6 +1057,31 @@ void SILCloner<ImplClass>::visitBeginBorrowInst(BeginBorrowInst *Inst) {
template <typename ImplClass>
void SILCloner<ImplClass>::visitStoreInst(StoreInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
if (!getBuilder().hasOwnership()) {
switch (Inst->getOwnershipQualifier()) {
case StoreOwnershipQualifier::Assign: {
auto *li = getBuilder().createLoad(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getDest()),
LoadOwnershipQualifier::Unqualified);
auto *si = getBuilder().createStore(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getSrc()),
getOpValue(Inst->getDest()), StoreOwnershipQualifier::Unqualified);
getBuilder().emitDestroyValueOperation(getOpLocation(Inst->getLoc()), li);
return recordClonedInstruction(Inst, si);
}
case StoreOwnershipQualifier::Init:
case StoreOwnershipQualifier::Trivial:
case StoreOwnershipQualifier::Unqualified:
break;
}

return recordClonedInstruction(
Inst, getBuilder().createStore(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getSrc()),
getOpValue(Inst->getDest()),
StoreOwnershipQualifier::Unqualified));
}

recordClonedInstruction(
Inst, getBuilder().createStore(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getSrc()),
Expand All @@ -1033,6 +1091,16 @@ void SILCloner<ImplClass>::visitStoreInst(StoreInst *Inst) {
template <typename ImplClass>
void SILCloner<ImplClass>::visitStoreBorrowInst(StoreBorrowInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
if (!getBuilder().hasOwnership()) {
// TODO: Eliminate store_borrow result so we can use
// recordClonedInstruction. It is not "technically" necessary, but it is
// better from an invariant perspective.
getBuilder().createStore(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getSrc()),
getOpValue(Inst->getDest()), StoreOwnershipQualifier::Unqualified);
return;
}

recordClonedInstruction(
Inst, getBuilder().createStoreBorrow(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getSrc()),
Expand All @@ -1042,6 +1110,11 @@ void SILCloner<ImplClass>::visitStoreBorrowInst(StoreBorrowInst *Inst) {
template <typename ImplClass>
void SILCloner<ImplClass>::visitEndBorrowInst(EndBorrowInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

// Do not clone any end_borrow.
if (!getBuilder().hasOwnership())
return;

recordClonedInstruction(
Inst,
getBuilder().createEndBorrow(getOpLocation(Inst->getLoc()),
Expand Down Expand Up @@ -1546,6 +1619,12 @@ void SILCloner<ImplClass>::visitUnmanagedRetainValueInst(
template <typename ImplClass>
void SILCloner<ImplClass>::visitCopyValueInst(CopyValueInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
if (!getBuilder().hasOwnership()) {
SILValue newValue = getBuilder().emitCopyValueOperation(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()));
return recordFoldedValue(Inst, newValue);
}

recordClonedInstruction(
Inst, getBuilder().createCopyValue(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand())));
Expand Down Expand Up @@ -1583,6 +1662,13 @@ void SILCloner<ImplClass>::visitUnmanagedReleaseValueInst(
template <typename ImplClass>
void SILCloner<ImplClass>::visitDestroyValueInst(DestroyValueInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
if (!getBuilder().hasOwnership()) {
return recordClonedInstruction(
Inst, getBuilder().createReleaseValue(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
RefCountingInst::Atomicity::Atomic));
}

recordClonedInstruction(
Inst, getBuilder().createDestroyValue(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand())));
Expand Down Expand Up @@ -1795,6 +1881,16 @@ template <typename ImplClass>
void SILCloner<ImplClass>::visitDestructureStructInst(
DestructureStructInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

if (!getBuilder().hasOwnership()) {
getBuilder().emitDestructureValueOperation(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
[&](unsigned index, SILValue value) {
recordFoldedValue(Inst->getResults()[index], value);
});
return;
}

recordClonedInstruction(
Inst, getBuilder().createDestructureStruct(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
Expand All @@ -1804,6 +1900,15 @@ template <typename ImplClass>
void SILCloner<ImplClass>::visitDestructureTupleInst(
DestructureTupleInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
if (!getBuilder().hasOwnership()) {
getBuilder().emitDestructureValueOperation(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
[&](unsigned index, SILValue value) {
recordFoldedValue(Inst->getResults()[index], value);
});
return;
}

recordClonedInstruction(
Inst, getBuilder().createDestructureTuple(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
Expand Down Expand Up @@ -2104,6 +2209,11 @@ SILCloner<ImplClass>::visitFixLifetimeInst(FixLifetimeInst *Inst) {
template <typename ImplClass>
void SILCloner<ImplClass>::visitEndLifetimeInst(EndLifetimeInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

// These are only needed in OSSA.
if (!getBuilder().hasOwnership())
return;

recordClonedInstruction(
Inst, getBuilder().createEndLifetime(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand())));
Expand All @@ -2113,6 +2223,11 @@ template <typename ImplClass>
void SILCloner<ImplClass>::visitUncheckedOwnershipConversionInst(
UncheckedOwnershipConversionInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

if (!getBuilder().hasOwnership()) {
return recordFoldedValue(Inst, getOpValue(Inst->getOperand()));
}

ValueOwnershipKind Kind = SILValue(Inst).getOwnershipKind();
if (getOpValue(Inst->getOperand()).getOwnershipKind() ==
ValueOwnershipKind::Any) {
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void SILBuilder::emitDestructureValueOperation(
function_ref<void(unsigned, SILValue)> func) {
// Do a quick check to see if we have a tuple without elements. In that
// case, bail early since we are not going to ever invoke Func.
if (auto tupleType = operand->getType().castTo<TupleType>())
if (auto tupleType = operand->getType().getAs<TupleType>())
if (0 == tupleType->getNumElements())
return;

Expand Down
Loading