Skip to content

Commit a0cb311

Browse files
authored
Merge pull request #34644 from gottesmm/pr-e398af60dfa740b58a743d92dc2fdc617e9c9bf0
[ownership] Change ReturnInst to have its ValueOwnershipKind stored within it rather than always recomputing from the function type.
2 parents bb79e9c + c7051d2 commit a0cb311

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ class SILBuilder {
19691969

19701970
ReturnInst *createReturn(SILLocation Loc, SILValue ReturnValue) {
19711971
return insertTerminator(new (getModule()) ReturnInst(
1972-
getSILDebugLocation(Loc), ReturnValue));
1972+
getFunction(), getSILDebugLocation(Loc), ReturnValue));
19731973
}
19741974

19751975
ThrowInst *createThrow(SILLocation Loc, SILValue errorValue) {

include/swift/SIL/SILInstruction.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7366,16 +7366,25 @@ class ReturnInst
73667366
{
73677367
friend SILBuilder;
73687368

7369+
/// We store the ownership kind in the return inst, but we do not consider the
7370+
/// underlying return inst to be forwarding. This is because its ownership is
7371+
/// tied to the function signature and thus should be static.
7372+
ValueOwnershipKind ownershipKind;
7373+
73697374
/// Constructs a ReturnInst representing a return.
73707375
///
7371-
/// \param DebugLoc The backing AST location.
7372-
///
7373-
/// \param ReturnValue The value to be returned.
7374-
///
7375-
ReturnInst(SILDebugLocation DebugLoc, SILValue ReturnValue)
7376-
: UnaryInstructionBase(DebugLoc, ReturnValue) {}
7376+
/// \param func The function we are returning from. Used to compute the
7377+
/// preferred ownership kind.
7378+
/// \param debugLoc The backing AST location.
7379+
/// \param returnValue The value to be returned.
7380+
ReturnInst(SILFunction &func, SILDebugLocation debugLoc,
7381+
SILValue returnValue);
73777382

73787383
public:
7384+
/// Return the ownership kind for this instruction if we had any direct
7385+
/// results.
7386+
ValueOwnershipKind getOwnershipKind() const { return ownershipKind; }
7387+
73797388
SuccessorListTy getSuccessors() {
73807389
// No Successors.
73817390
return SuccessorListTy();

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -433,36 +433,11 @@ OperandOwnershipKindClassifier::visitCheckedCastBranchInst(
433433
return Map::compatibilityMap(kind, lifetimeConstraint);
434434
}
435435

436-
//// FIX THIS HERE
437436
OperandOwnershipKindMap
438437
OperandOwnershipKindClassifier::visitReturnInst(ReturnInst *ri) {
439-
auto *f =ri->getFunction();
440-
441-
// If we have a trivial value, return allLive().
442-
bool isTrivial = ri->getOperand()->getType().isTrivial(*f);
443-
if (isTrivial) {
444-
return Map::allLive();
445-
}
446-
447-
SILFunctionConventions fnConv = f->getConventions();
448-
449-
auto results = fnConv.getDirectSILResults();
450-
if (results.empty())
451-
return Map();
452-
453-
auto ownershipKindRange = makeTransformRange(results,
454-
[&](const SILResultInfo &info) {
455-
return info.getOwnershipKind(*f, f->getLoweredFunctionType());
456-
});
457-
458-
// Then merge all of our ownership kinds. If we fail to merge, return an empty
459-
// map so we fail on all operands.
460-
auto mergedBase = ValueOwnershipKind::merge(ownershipKindRange);
461-
if (!mergedBase)
462-
return Map();
463-
464-
auto base = *mergedBase;
465-
return Map::compatibilityMap(base, base.getForwardingLifetimeConstraint());
438+
auto kind = ri->getOwnershipKind();
439+
auto lifetimeConstraint = kind.getForwardingLifetimeConstraint();
440+
return Map::compatibilityMap(kind, lifetimeConstraint);
466441
}
467442

468443
OperandOwnershipKindMap

lib/SIL/IR/SILInstructions.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,3 +2863,28 @@ bool GetAsyncContinuationInstBase::throws() const {
28632863
return getType().castTo<BoundGenericType>()->getDecl()
28642864
== getFunction()->getASTContext().getUnsafeThrowingContinuationDecl();
28652865
}
2866+
2867+
ReturnInst::ReturnInst(SILFunction &func, SILDebugLocation debugLoc,
2868+
SILValue returnValue)
2869+
: UnaryInstructionBase(debugLoc, returnValue),
2870+
ownershipKind(ValueOwnershipKind::None) {
2871+
// If we have a trivial value, leave our ownership kind as none.
2872+
if (returnValue->getType().isTrivial(func))
2873+
return;
2874+
2875+
SILFunctionConventions fnConv = func.getConventions();
2876+
2877+
// If we do not have any direct SIL results, we should accept a tuple
2878+
// argument, meaning that we should have a none ownership kind.
2879+
auto results = fnConv.getDirectSILResults();
2880+
if (results.empty())
2881+
return;
2882+
2883+
auto ownershipKindRange =
2884+
makeTransformRange(results, [&](const SILResultInfo &info) {
2885+
return info.getOwnershipKind(func, func.getLoweredFunctionType());
2886+
});
2887+
2888+
// Then merge all of our ownership kinds. Assert if we fail to merge.
2889+
ownershipKind = *ValueOwnershipKind::merge(ownershipKindRange);
2890+
}

0 commit comments

Comments
 (0)