Skip to content

Commit 2eb8831

Browse files
authored
Merge pull request #67803 from gottesmm/pr-db469fba5d219b4c1ea5afe325c48c13dd94aacb
[silgen] Eliminate more ManagedValue::forUnmanaged
2 parents 3de765c + cece84f commit 2eb8831

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

lib/SILGen/ManagedValue.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ class ManagedValue {
9292
return ManagedValue(value, false, CleanupHandle::invalid());
9393
}
9494

95+
enum class ScopeKind {
96+
Lexical,
97+
FormalAccess,
98+
};
99+
100+
/// Given a value \p value, create a copy of it and return the relevant
101+
/// ManagedValue.
102+
static ManagedValue forCopyOwnedObjectRValue(SILGenFunction &SGF,
103+
SILLocation loc, SILValue value,
104+
ScopeKind kind) {
105+
assert(value && "No value specified");
106+
assert(value->getType().isObject());
107+
auto mv = ManagedValue::forUnmanaged(value);
108+
if (kind == ScopeKind::Lexical)
109+
return mv.copy(SGF, loc);
110+
return mv.formalAccessCopy(SGF, loc);
111+
}
112+
95113
/// Create a managed value for a SILValue whose ownership is
96114
/// forwarded. Creates a new cleanup for +1 values. Forwarded +0 values
97115
/// require no cleanup.
@@ -178,8 +196,9 @@ class ManagedValue {
178196
return ManagedValue(value, false, CleanupHandle::invalid());
179197
}
180198

181-
/// Create a managed value for a +0 trivial rvalue.
182-
static ManagedValue forTrivialRValue(SILValue value) {
199+
/// Create a managed value for a trivial address rvalue or an object rvalue
200+
/// that has .none ownership.
201+
static ManagedValue forRValueWithoutOwnership(SILValue value) {
183202
if (value->getType().isObject())
184203
return ManagedValue::forObjectRValueWithoutOwnership(value);
185204
return ManagedValue::forTrivialAddressRValue(value);

lib/SILGen/ResultPlan.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,9 @@ class ForeignAsyncInitializationPlan final : public ResultPlan {
899899
SILValue(wrappedContinuation));
900900
SGF.emitApplyOfLibraryIntrinsic(
901901
loc, errorIntrinsic, subs,
902-
{continuationMV,
903-
ManagedValue::forUnmanaged(bridgedForeignError).copy(SGF, loc)},
902+
{continuationMV, ManagedValue::forCopyOwnedObjectRValue(
903+
SGF, loc, bridgedForeignError,
904+
ManagedValue::ScopeKind::Lexical)},
904905
SGFContext());
905906

906907
// Second, emit a branch from the end of the foreign error block to the

lib/SILGen/SILGenApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4915,14 +4915,14 @@ SILGenFunction::emitBeginApply(SILLocation loc, ManagedValue fn,
49154915
yields.push_back(ManagedValue::forLValue(value));
49164916
} else if (info.isConsumed()) {
49174917
!useLoweredAddresses && value->getType().isTrivial(getFunction())
4918-
? yields.push_back(ManagedValue::forTrivialRValue(value))
4918+
? yields.push_back(ManagedValue::forRValueWithoutOwnership(value))
49194919
: yields.push_back(emitManagedRValueWithCleanup(value));
49204920
} else if (info.isGuaranteed()) {
49214921
!useLoweredAddresses && value->getType().isTrivial(getFunction())
4922-
? yields.push_back(ManagedValue::forTrivialRValue(value))
4922+
? yields.push_back(ManagedValue::forRValueWithoutOwnership(value))
49234923
: yields.push_back(ManagedValue::forBorrowedRValue(value));
49244924
} else {
4925-
yields.push_back(ManagedValue::forTrivialRValue(value));
4925+
yields.push_back(ManagedValue::forRValueWithoutOwnership(value));
49264926
}
49274927
}
49284928

lib/SILGen/SILGenBuilder.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ static ManagedValue createInputFunctionArgument(
525525
//
526526
// NOTE: If we have a trivial value, the copy will do nothing, so this is
527527
// just a convenient way to avoid writing conditional code.
528-
return ManagedValue::forUnmanaged(arg).copy(SGF, loc);
528+
return ManagedValue::forCopyOwnedObjectRValue(
529+
SGF, loc, arg, ManagedValue::ScopeKind::Lexical);
529530

530531
case SILArgumentConvention::Direct_Owned:
531532
return SGF.emitManagedRValueWithCleanup(arg);
@@ -706,13 +707,14 @@ ManagedValue SILGenBuilder::createUncheckedBitCast(SILLocation loc,
706707

707708
// If we have a trivial inst, just return early.
708709
if (isa<UncheckedTrivialBitCastInst>(cast))
709-
return ManagedValue::forUnmanaged(cast);
710+
return ManagedValue::forObjectRValueWithoutOwnership(cast);
710711

711712
// If we perform an unchecked bitwise case, then we are producing a new RC
712713
// identity implying that we need a copy of the casted value to be returned so
713714
// that the inputs/outputs of the case have separate ownership.
714715
if (isa<UncheckedBitwiseCastInst>(cast)) {
715-
return ManagedValue::forUnmanaged(cast).copy(SGF, loc);
716+
return ManagedValue::forCopyOwnedObjectRValue(
717+
SGF, loc, cast, ManagedValue::ScopeKind::Lexical);
716718
}
717719

718720
// Otherwise, we forward the cleanup of the input value and place the cleanup
@@ -762,7 +764,7 @@ ManagedValue SILGenBuilder::createOpenExistentialMetatype(SILLocation loc,
762764
SILType openedType) {
763765
SILValue result = SILGenBuilder::createOpenExistentialMetatype(
764766
loc, value.getValue(), openedType);
765-
return ManagedValue::forTrivialRValue(result);
767+
return ManagedValue::forRValueWithoutOwnership(result);
766768
}
767769

768770
ManagedValue SILGenBuilder::createStore(SILLocation loc, ManagedValue value,

lib/SILGen/SILGenExpr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ SILGenFunction::emitManagedBeginBorrow(SILLocation loc, SILValue v,
152152
assert(lowering.getLoweredType().getObjectType() ==
153153
v->getType().getObjectType());
154154
if (lowering.isTrivial())
155-
return ManagedValue::forUnmanaged(v);
155+
return ManagedValue::forRValueWithoutOwnership(v);
156156

157157
if (v->getOwnershipKind() == OwnershipKind::None)
158-
return ManagedValue::forUnmanaged(v);
158+
return ManagedValue::forRValueWithoutOwnership(v);
159159

160160
if (v->getOwnershipKind() == OwnershipKind::Guaranteed)
161161
return ManagedValue::forUnmanaged(v);
@@ -2867,7 +2867,7 @@ emitKeyPathRValueBase(SILGenFunction &subSGF,
28672867
return ManagedValue();
28682868

28692869
auto paramOrigValue = paramArg->getType().isTrivial(subSGF.F)
2870-
? ManagedValue::forTrivialRValue(paramArg)
2870+
? ManagedValue::forRValueWithoutOwnership(paramArg)
28712871
: ManagedValue::forBorrowedRValue(paramArg);
28722872
paramOrigValue = paramOrigValue.copy(subSGF, loc);
28732873
auto paramSubstValue = subSGF.emitOrigToSubstValue(loc, paramOrigValue,
@@ -3273,7 +3273,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32733273
indexes, indexPtrArg);
32743274

32753275
auto valueOrig = valueArgTy.isTrivial(subSGF.F)
3276-
? ManagedValue::forTrivialRValue(valueArg)
3276+
? ManagedValue::forRValueWithoutOwnership(valueArg)
32773277
: ManagedValue::forBorrowedRValue(valueArg);
32783278
valueOrig = valueOrig.copy(subSGF, loc);
32793279
auto valueSubst = subSGF.emitOrigToSubstValue(loc, valueOrig,

0 commit comments

Comments
 (0)