Skip to content

Commit 2208caa

Browse files
committedAug 7, 2023
[silgen] Convert a bunch of forUnmanaged -> forObjectRValueWithoutOwnership.
NFCI.

11 files changed

+179
-163
lines changed
 

‎lib/SILGen/ManagedValue.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ class ConsumableManagedValue {
423423

424424
/// Create a CMV for a value of trivial type.
425425
static ConsumableManagedValue forUnmanaged(SILValue value) {
426-
return { ManagedValue::forUnmanaged(value),
427-
CastConsumptionKind::TakeAlways };
426+
return {ManagedValue::forObjectRValueWithoutOwnership(value),
427+
CastConsumptionKind::TakeAlways};
428428
}
429429

430430
/// Create a CMV for an owned value.

‎lib/SILGen/ResultPlan.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,8 @@ class ForeignAsyncInitializationPlan final : public ResultPlan {
895895
loc, SILType::getPrimitiveObjectType(continuationTy),
896896
{continuation});
897897

898-
auto continuationMV =
899-
ManagedValue::forUnmanaged(SILValue(wrappedContinuation));
898+
auto continuationMV = ManagedValue::forObjectRValueWithoutOwnership(
899+
SILValue(wrappedContinuation));
900900
SGF.emitApplyOfLibraryIntrinsic(
901901
loc, errorIntrinsic, subs,
902902
{continuationMV,

‎lib/SILGen/SILGenApply.cpp

+34-30
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static ManagedValue borrowedCastToOriginalSelfType(SILGenFunction &SGF,
218218
if (originalSelfType.is<AnyMetatypeType>()) {
219219
assert(originalSelfType.isTrivial(SGF.F) &&
220220
"Metatypes should always be trivial");
221-
return ManagedValue::forUnmanaged(originalSelf);
221+
return ManagedValue::forObjectRValueWithoutOwnership(originalSelf);
222222
}
223223

224224
// Otherwise, we have a non-metatype. Use a borrow+unchecked_ref_cast.
@@ -629,14 +629,14 @@ class Callee {
629629
auto constantInfo =
630630
SGF.getConstantInfo(SGF.getTypeExpansionContext(), *constant);
631631
SILValue ref = SGF.emitGlobalFunctionRef(Loc, *constant, constantInfo);
632-
return ManagedValue::forUnmanaged(ref);
632+
return ManagedValue::forObjectRValueWithoutOwnership(ref);
633633
}
634634
case Kind::StandaloneFunctionDynamicallyReplaceableImpl: {
635635
auto constantInfo =
636636
SGF.getConstantInfo(SGF.getTypeExpansionContext(), *constant);
637637
SILValue ref =
638638
SGF.emitGlobalFunctionRef(Loc, *constant, constantInfo, true);
639-
return ManagedValue::forUnmanaged(ref);
639+
return ManagedValue::forObjectRValueWithoutOwnership(ref);
640640
}
641641
case Kind::ClassMethod: {
642642
auto methodTy = SGF.SGM.Types.getConstantOverrideType(
@@ -655,7 +655,7 @@ class Callee {
655655
SILType::getPrimitiveObjectType(methodTy));
656656
}
657657
S.pop();
658-
return ManagedValue::forUnmanaged(methodVal);
658+
return ManagedValue::forObjectRValueWithoutOwnership(methodVal);
659659
}
660660
case Kind::SuperMethod: {
661661
ArgumentScope S(SGF, Loc);
@@ -709,7 +709,7 @@ class Callee {
709709
*constant, constantInfo.getSILType());
710710
}
711711
S.pop();
712-
return ManagedValue::forUnmanaged(fn);
712+
return ManagedValue::forObjectRValueWithoutOwnership(fn);
713713
}
714714
case Kind::DynamicMethod: {
715715
auto closureType = getDynamicMethodLoweredType(
@@ -720,7 +720,7 @@ class Callee {
720720
Loc, borrowedSelf->getValue(), *constant,
721721
closureType);
722722
S.pop();
723-
return ManagedValue::forUnmanaged(fn);
723+
return ManagedValue::forObjectRValueWithoutOwnership(fn);
724724
}
725725
}
726726
llvm_unreachable("unhandled kind");
@@ -1010,7 +1010,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
10101010
llvm_unreachable("shouldn't happen");
10111011
}
10121012

1013-
auto result = ManagedValue::forUnmanaged(convertedValue);
1013+
auto result = ManagedValue::forObjectRValueWithoutOwnership(convertedValue);
10141014
return {result, SGF.getLoweredType(instanceType)};
10151015
}
10161016

@@ -1550,9 +1550,12 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
15501550
self = SGF.emitUndef(selfFormalType);
15511551
} else if (SGF.AllocatorMetatype) {
15521552
self = emitCorrespondingSelfValue(
1553-
ManagedValue::forUnmanaged(SGF.AllocatorMetatype), arg);
1553+
ManagedValue::forObjectRValueWithoutOwnership(
1554+
SGF.AllocatorMetatype),
1555+
arg);
15541556
} else {
1555-
self = ManagedValue::forUnmanaged(SGF.emitMetatypeOfValue(expr, arg));
1557+
self = ManagedValue::forObjectRValueWithoutOwnership(
1558+
SGF.emitMetatypeOfValue(expr, arg));
15561559
}
15571560
} else {
15581561
// If we haven't allocated "self" yet at this point, do so.
@@ -1565,10 +1568,10 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
15651568
// initializer is @objc.
15661569
usesObjCAllocation = true;
15671570
}
1568-
1569-
self = allocateObject(
1570-
ManagedValue::forUnmanaged(SGF.AllocatorMetatype), arg,
1571-
usesObjCAllocation);
1571+
1572+
self = allocateObject(ManagedValue::forObjectRValueWithoutOwnership(
1573+
SGF.AllocatorMetatype),
1574+
arg, usesObjCAllocation);
15721575

15731576
// Perform any adjustments needed to 'self'.
15741577
self = emitCorrespondingSelfValue(self, arg);
@@ -1819,7 +1822,8 @@ static PreparedArguments emitStringLiteralArgs(SILGenFunction &SGF, SILLocation
18191822
AnyFunctionType::Param param(Int32Ty.getASTType());
18201823
PreparedArguments args(llvm::ArrayRef<AnyFunctionType::Param>{param});
18211824
args.add(E, RValue(SGF, E, Int32Ty.getASTType(),
1822-
ManagedValue::forUnmanaged(UnicodeScalarValue)));
1825+
ManagedValue::forObjectRValueWithoutOwnership(
1826+
UnicodeScalarValue)));
18231827
return args;
18241828
}
18251829
}
@@ -1836,10 +1840,9 @@ static PreparedArguments emitStringLiteralArgs(SILGenFunction &SGF, SILLocation
18361840
auto *isASCIIInst = SGF.B.createIntegerLiteral(E, Int1Ty, isASCII);
18371841

18381842
ManagedValue EltsArray[] = {
1839-
ManagedValue::forUnmanaged(string),
1840-
ManagedValue::forUnmanaged(lengthInst),
1841-
ManagedValue::forUnmanaged(isASCIIInst)
1842-
};
1843+
ManagedValue::forObjectRValueWithoutOwnership(string),
1844+
ManagedValue::forObjectRValueWithoutOwnership(lengthInst),
1845+
ManagedValue::forObjectRValueWithoutOwnership(isASCIIInst)};
18431846

18441847
AnyFunctionType::Param TypeEltsArray[] = {
18451848
AnyFunctionType::Param(EltsArray[0].getType().getASTType()),
@@ -2043,7 +2046,8 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
20432046
auto i1Ty = SILType::getBuiltinIntegerType(1, SGF.getASTContext());
20442047
SILValue boolValue = SGF.B.createIntegerLiteral(booleanLiteral, i1Ty,
20452048
booleanLiteral->getValue());
2046-
ManagedValue boolManaged = ManagedValue::forUnmanaged(boolValue);
2049+
ManagedValue boolManaged =
2050+
ManagedValue::forObjectRValueWithoutOwnership(boolValue);
20472051
CanType ty = boolManaged.getType().getASTType()->getCanonicalType();
20482052
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
20492053
builtinLiteralArgs.add(booleanLiteral, RValue(SGF, {boolManaged}, ty));
@@ -2055,7 +2059,7 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
20552059
IntegerLiteralExpr *integerLiteral) {
20562060
PreparedArguments builtinLiteralArgs;
20572061
ManagedValue integerManaged =
2058-
ManagedValue::forUnmanaged(SGF.B.createIntegerLiteral(
2062+
ManagedValue::forObjectRValueWithoutOwnership(SGF.B.createIntegerLiteral(
20592063
integerLiteral,
20602064
SILType::getBuiltinIntegerLiteralType(SGF.getASTContext()),
20612065
integerLiteral->getRawValue()));
@@ -2071,7 +2075,7 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
20712075
PreparedArguments builtinLiteralArgs;
20722076
auto *litTy = floatLiteral->getBuiltinType()->castTo<BuiltinFloatType>();
20732077
ManagedValue floatManaged =
2074-
ManagedValue::forUnmanaged(SGF.B.createFloatLiteral(
2078+
ManagedValue::forObjectRValueWithoutOwnership(SGF.B.createFloatLiteral(
20752079
floatLiteral,
20762080
SILType::getBuiltinFloatType(litTy->getFPKind(), SGF.getASTContext()),
20772081
floatLiteral->getValue()));
@@ -2100,7 +2104,7 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
21002104
// The version of the regex string.
21012105
// %3 = integer_literal $Builtin.IntLiteral <version>
21022106
auto versionIntLiteral =
2103-
ManagedValue::forUnmanaged(SGF.B.createIntegerLiteral(
2107+
ManagedValue::forObjectRValueWithoutOwnership(SGF.B.createIntegerLiteral(
21042108
expr, SILType::getBuiltinIntegerLiteralType(SGF.getASTContext()),
21052109
expr->getVersion()));
21062110

@@ -2187,7 +2191,8 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
21872191
auto silTy = SILType::getBuiltinIntegerLiteralType(ctx);
21882192
auto ty = silTy.getASTType();
21892193
SILValue integer = SGF.B.createIntegerLiteral(magicLiteral, silTy, Value);
2190-
ManagedValue integerManaged = ManagedValue::forUnmanaged(integer);
2194+
ManagedValue integerManaged =
2195+
ManagedValue::forObjectRValueWithoutOwnership(integer);
21912196
PreparedArguments builtinLiteralArgs;
21922197
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
21932198
builtinLiteralArgs.add(magicLiteral, RValue(SGF, {integerManaged}, ty));
@@ -6090,7 +6095,7 @@ RValue SILGenFunction::emitApplyAllocatingInitializer(SILLocation loc,
60906095
if (selfMetaTy != selfParamMetaTy)
60916096
selfMeta = B.createUpcast(loc, selfMeta, selfParamMetaTy);
60926097

6093-
selfMetaVal = ManagedValue::forUnmanaged(selfMeta);
6098+
selfMetaVal = ManagedValue::forObjectRValueWithoutOwnership(selfMeta);
60946099
}
60956100

60966101
// Form the callee.
@@ -6221,10 +6226,9 @@ SILGenFunction::emitUninitializedArrayAllocation(Type ArrayTy,
62216226
// Invoke the intrinsic, which returns a tuple.
62226227
auto subMap = ArrayTy->getContextSubstitutionMap(SGM.M.getSwiftModule(),
62236228
Ctx.getArrayDecl());
6224-
auto result = emitApplyOfLibraryIntrinsic(Loc, allocate,
6225-
subMap,
6226-
ManagedValue::forUnmanaged(Length),
6227-
SGFContext());
6229+
auto result = emitApplyOfLibraryIntrinsic(
6230+
Loc, allocate, subMap,
6231+
ManagedValue::forObjectRValueWithoutOwnership(Length), SGFContext());
62286232

62296233
// Explode the tuple.
62306234
SmallVector<ManagedValue, 2> resultElts;
@@ -6935,7 +6939,7 @@ ManagedValue SILGenFunction::emitAsyncLetStart(
69356939
getLoweredType(ctx.TheRawPointerType), subs,
69366940
{taskOptions, taskFunction.getValue(), resultBuf});
69376941

6938-
return ManagedValue::forUnmanaged(apply);
6942+
return ManagedValue::forObjectRValueWithoutOwnership(apply);
69396943
}
69406944

69416945
ManagedValue SILGenFunction::emitCancelAsyncTask(
@@ -6946,7 +6950,7 @@ ManagedValue SILGenFunction::emitCancelAsyncTask(
69466950
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::CancelAsyncTask)),
69476951
getLoweredType(ctx.TheEmptyTupleType), SubstitutionMap(),
69486952
{ task });
6949-
return ManagedValue::forUnmanaged(apply);
6953+
return ManagedValue::forObjectRValueWithoutOwnership(apply);
69506954
}
69516955

69526956
ManagedValue SILGenFunction::emitReadAsyncLetBinding(SILLocation loc,

‎lib/SILGen/SILGenBridging.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ emitBridgeObjectiveCToNative(SILGenFunction &SGF, SILLocation loc,
243243
ResultPlanPtr resultPlan =
244244
ResultPlanBuilder::computeResultPlan(SGF, calleeTypeInfo, loc, context);
245245
ArgumentScope argScope(SGF, loc);
246-
RValue result =
247-
SGF.emitApply(std::move(resultPlan), std::move(argScope), loc,
248-
ManagedValue::forUnmanaged(witnessRef), subs,
249-
{objcValue, ManagedValue::forUnmanaged(metatypeValue)},
250-
calleeTypeInfo, ApplyOptions(), context, llvm::None);
246+
RValue result = SGF.emitApply(
247+
std::move(resultPlan), std::move(argScope), loc,
248+
ManagedValue::forObjectRValueWithoutOwnership(witnessRef), subs,
249+
{objcValue, ManagedValue::forObjectRValueWithoutOwnership(metatypeValue)},
250+
calleeTypeInfo, ApplyOptions(), context, llvm::None);
251251
return std::move(result).getAsSingleValue(SGF, loc);
252252
}
253253

@@ -2297,8 +2297,8 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
22972297
ArgumentScope argScope(*this, fd);
22982298
ManagedValue resultMV =
22992299
emitApply(std::move(resultPlan), std::move(argScope), fd,
2300-
ManagedValue::forUnmanaged(fn), subs, args, calleeTypeInfo,
2301-
ApplyOptions(), context, llvm::None)
2300+
ManagedValue::forObjectRValueWithoutOwnership(fn), subs, args,
2301+
calleeTypeInfo, ApplyOptions(), context, llvm::None)
23022302
.getAsSingleValue(*this, fd);
23032303

23042304
if (indirectResult) {

‎lib/SILGen/SILGenBuilder.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ ManagedValue SILGenBuilder::createPhi(SILType type,
233233
return SGF.emitManagedBorrowedArgumentWithCleanup(arg);
234234

235235
case OwnershipKind::None:
236+
return ManagedValue::forObjectRValueWithoutOwnership(arg);
237+
236238
case OwnershipKind::Unowned:
237239
return ManagedValue::forUnmanaged(arg);
238240
}
@@ -387,7 +389,7 @@ SILGenBuilder::bufferForExpr(SILLocation loc, SILType ty,
387389

388390
// Add a cleanup for the temporary we allocated.
389391
if (lowering.isTrivial())
390-
return ManagedValue::forUnmanaged(address);
392+
return ManagedValue::forTrivialAddressRValue(address);
391393

392394
return SGF.emitManagedBufferWithCleanup(address);
393395
}
@@ -415,7 +417,7 @@ ManagedValue SILGenBuilder::formalAccessBufferForExpr(
415417

416418
// Add a cleanup for the temporary we allocated.
417419
if (lowering.isTrivial())
418-
return ManagedValue::forUnmanaged(address);
420+
return ManagedValue::forTrivialAddressRValue(address);
419421

420422
return SGF.emitFormalAccessManagedBufferWithCleanup(loc, address);
421423
}
@@ -465,7 +467,7 @@ ManagedValue SILGenBuilder::createLoadTake(SILLocation loc, ManagedValue v,
465467
SILValue result =
466468
lowering.emitLoadOfCopy(*this, loc, v.forward(SGF), IsTake);
467469
if (lowering.isTrivial())
468-
return ManagedValue::forUnmanaged(result);
470+
return ManagedValue::forObjectRValueWithoutOwnership(result);
469471
assert((!lowering.isAddressOnly() || !SGF.silConv.useLoweredAddresses()) &&
470472
"cannot retain an unloadable type");
471473
return SGF.emitManagedRValueWithCleanup(result, lowering);
@@ -482,7 +484,7 @@ ManagedValue SILGenBuilder::createLoadCopy(SILLocation loc, ManagedValue v,
482484
SILValue result =
483485
lowering.emitLoadOfCopy(*this, loc, v.getValue(), IsNotTake);
484486
if (lowering.isTrivial())
485-
return ManagedValue::forUnmanaged(result);
487+
return ManagedValue::forObjectRValueWithoutOwnership(result);
486488
assert((!lowering.isAddressOnly()
487489
|| !SGF.silConv.useLoweredAddresses()) &&
488490
"cannot retain an unloadable type");
@@ -649,7 +651,7 @@ ManagedValue SILGenBuilder::createManagedOptionalNone(SILLocation loc,
649651
if (!type.isAddressOnly(getFunction()) ||
650652
!SGF.silConv.useLoweredAddresses()) {
651653
SILValue noneValue = createOptionalNone(loc, type);
652-
return ManagedValue::forUnmanaged(noneValue);
654+
return ManagedValue::forObjectRValueWithoutOwnership(noneValue);
653655
}
654656

655657
SILValue tempResult = SGF.emitTemporaryAllocation(loc, type);

0 commit comments

Comments
 (0)
Please sign in to comment.