Skip to content

[silgen] Rename forTrivialObjectRValue -> forObjectRValueWithoutOwnership and change a bunch of forUnmanaged to use this API #67780

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
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
8 changes: 4 additions & 4 deletions lib/SILGen/ManagedValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class ManagedValue {
}

/// Create a managed value for a +0 trivial object rvalue.
static ManagedValue forTrivialObjectRValue(SILValue value) {
static ManagedValue forObjectRValueWithoutOwnership(SILValue value) {
assert(value->getType().isObject() && "Expected an object");
assert(value->getOwnershipKind() == OwnershipKind::None);
return ManagedValue(value, false, CleanupHandle::invalid());
Expand All @@ -181,7 +181,7 @@ class ManagedValue {
/// Create a managed value for a +0 trivial rvalue.
static ManagedValue forTrivialRValue(SILValue value) {
if (value->getType().isObject())
return ManagedValue::forTrivialObjectRValue(value);
return ManagedValue::forObjectRValueWithoutOwnership(value);
return ManagedValue::forTrivialAddressRValue(value);
}

Expand Down Expand Up @@ -423,8 +423,8 @@ class ConsumableManagedValue {

/// Create a CMV for a value of trivial type.
static ConsumableManagedValue forUnmanaged(SILValue value) {
return { ManagedValue::forUnmanaged(value),
CastConsumptionKind::TakeAlways };
return {ManagedValue::forObjectRValueWithoutOwnership(value),
CastConsumptionKind::TakeAlways};
}

/// Create a CMV for an owned value.
Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/ResultPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ class ForeignAsyncInitializationPlan final : public ResultPlan {
loc, SILType::getPrimitiveObjectType(continuationTy),
{continuation});

auto continuationMV =
ManagedValue::forUnmanaged(SILValue(wrappedContinuation));
auto continuationMV = ManagedValue::forObjectRValueWithoutOwnership(
SILValue(wrappedContinuation));
SGF.emitApplyOfLibraryIntrinsic(
loc, errorIntrinsic, subs,
{continuationMV,
Expand Down
84 changes: 45 additions & 39 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ static ManagedValue borrowedCastToOriginalSelfType(SILGenFunction &SGF,
if (originalSelfType.is<AnyMetatypeType>()) {
assert(originalSelfType.isTrivial(SGF.F) &&
"Metatypes should always be trivial");
return ManagedValue::forUnmanaged(originalSelf);
return ManagedValue::forObjectRValueWithoutOwnership(originalSelf);
}

// Otherwise, we have a non-metatype. Use a borrow+unchecked_ref_cast.
Expand Down Expand Up @@ -629,14 +629,14 @@ class Callee {
auto constantInfo =
SGF.getConstantInfo(SGF.getTypeExpansionContext(), *constant);
SILValue ref = SGF.emitGlobalFunctionRef(Loc, *constant, constantInfo);
return ManagedValue::forUnmanaged(ref);
return ManagedValue::forObjectRValueWithoutOwnership(ref);
}
case Kind::StandaloneFunctionDynamicallyReplaceableImpl: {
auto constantInfo =
SGF.getConstantInfo(SGF.getTypeExpansionContext(), *constant);
SILValue ref =
SGF.emitGlobalFunctionRef(Loc, *constant, constantInfo, true);
return ManagedValue::forUnmanaged(ref);
return ManagedValue::forObjectRValueWithoutOwnership(ref);
}
case Kind::ClassMethod: {
auto methodTy = SGF.SGM.Types.getConstantOverrideType(
Expand All @@ -655,7 +655,7 @@ class Callee {
SILType::getPrimitiveObjectType(methodTy));
}
S.pop();
return ManagedValue::forUnmanaged(methodVal);
return ManagedValue::forObjectRValueWithoutOwnership(methodVal);
}
case Kind::SuperMethod: {
ArgumentScope S(SGF, Loc);
Expand Down Expand Up @@ -709,7 +709,7 @@ class Callee {
*constant, constantInfo.getSILType());
}
S.pop();
return ManagedValue::forUnmanaged(fn);
return ManagedValue::forObjectRValueWithoutOwnership(fn);
}
case Kind::DynamicMethod: {
auto closureType = getDynamicMethodLoweredType(
Expand All @@ -720,7 +720,7 @@ class Callee {
Loc, borrowedSelf->getValue(), *constant,
closureType);
S.pop();
return ManagedValue::forUnmanaged(fn);
return ManagedValue::forObjectRValueWithoutOwnership(fn);
}
}
llvm_unreachable("unhandled kind");
Expand Down Expand Up @@ -1010,7 +1010,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
llvm_unreachable("shouldn't happen");
}

auto result = ManagedValue::forUnmanaged(convertedValue);
auto result = ManagedValue::forObjectRValueWithoutOwnership(convertedValue);
return {result, SGF.getLoweredType(instanceType)};
}

Expand Down Expand Up @@ -1550,9 +1550,12 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
self = SGF.emitUndef(selfFormalType);
} else if (SGF.AllocatorMetatype) {
self = emitCorrespondingSelfValue(
ManagedValue::forUnmanaged(SGF.AllocatorMetatype), arg);
ManagedValue::forObjectRValueWithoutOwnership(
SGF.AllocatorMetatype),
arg);
} else {
self = ManagedValue::forUnmanaged(SGF.emitMetatypeOfValue(expr, arg));
self = ManagedValue::forObjectRValueWithoutOwnership(
SGF.emitMetatypeOfValue(expr, arg));
}
} else {
// If we haven't allocated "self" yet at this point, do so.
Expand All @@ -1565,10 +1568,10 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
// initializer is @objc.
usesObjCAllocation = true;
}
self = allocateObject(
ManagedValue::forUnmanaged(SGF.AllocatorMetatype), arg,
usesObjCAllocation);

self = allocateObject(ManagedValue::forObjectRValueWithoutOwnership(
SGF.AllocatorMetatype),
arg, usesObjCAllocation);

// Perform any adjustments needed to 'self'.
self = emitCorrespondingSelfValue(self, arg);
Expand Down Expand Up @@ -1819,7 +1822,8 @@ static PreparedArguments emitStringLiteralArgs(SILGenFunction &SGF, SILLocation
AnyFunctionType::Param param(Int32Ty.getASTType());
PreparedArguments args(llvm::ArrayRef<AnyFunctionType::Param>{param});
args.add(E, RValue(SGF, E, Int32Ty.getASTType(),
ManagedValue::forUnmanaged(UnicodeScalarValue)));
ManagedValue::forObjectRValueWithoutOwnership(
UnicodeScalarValue)));
return args;
}
}
Expand All @@ -1836,10 +1840,9 @@ static PreparedArguments emitStringLiteralArgs(SILGenFunction &SGF, SILLocation
auto *isASCIIInst = SGF.B.createIntegerLiteral(E, Int1Ty, isASCII);

ManagedValue EltsArray[] = {
ManagedValue::forUnmanaged(string),
ManagedValue::forUnmanaged(lengthInst),
ManagedValue::forUnmanaged(isASCIIInst)
};
ManagedValue::forObjectRValueWithoutOwnership(string),
ManagedValue::forObjectRValueWithoutOwnership(lengthInst),
ManagedValue::forObjectRValueWithoutOwnership(isASCIIInst)};

AnyFunctionType::Param TypeEltsArray[] = {
AnyFunctionType::Param(EltsArray[0].getType().getASTType()),
Expand Down Expand Up @@ -2043,7 +2046,8 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
auto i1Ty = SILType::getBuiltinIntegerType(1, SGF.getASTContext());
SILValue boolValue = SGF.B.createIntegerLiteral(booleanLiteral, i1Ty,
booleanLiteral->getValue());
ManagedValue boolManaged = ManagedValue::forUnmanaged(boolValue);
ManagedValue boolManaged =
ManagedValue::forObjectRValueWithoutOwnership(boolValue);
CanType ty = boolManaged.getType().getASTType()->getCanonicalType();
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
builtinLiteralArgs.add(booleanLiteral, RValue(SGF, {boolManaged}, ty));
Expand All @@ -2055,7 +2059,7 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
IntegerLiteralExpr *integerLiteral) {
PreparedArguments builtinLiteralArgs;
ManagedValue integerManaged =
ManagedValue::forUnmanaged(SGF.B.createIntegerLiteral(
ManagedValue::forObjectRValueWithoutOwnership(SGF.B.createIntegerLiteral(
integerLiteral,
SILType::getBuiltinIntegerLiteralType(SGF.getASTContext()),
integerLiteral->getRawValue()));
Expand All @@ -2071,7 +2075,7 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
PreparedArguments builtinLiteralArgs;
auto *litTy = floatLiteral->getBuiltinType()->castTo<BuiltinFloatType>();
ManagedValue floatManaged =
ManagedValue::forUnmanaged(SGF.B.createFloatLiteral(
ManagedValue::forObjectRValueWithoutOwnership(SGF.B.createFloatLiteral(
floatLiteral,
SILType::getBuiltinFloatType(litTy->getFPKind(), SGF.getASTContext()),
floatLiteral->getValue()));
Expand Down Expand Up @@ -2100,7 +2104,7 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
// The version of the regex string.
// %3 = integer_literal $Builtin.IntLiteral <version>
auto versionIntLiteral =
ManagedValue::forUnmanaged(SGF.B.createIntegerLiteral(
ManagedValue::forObjectRValueWithoutOwnership(SGF.B.createIntegerLiteral(
expr, SILType::getBuiltinIntegerLiteralType(SGF.getASTContext()),
expr->getVersion()));

Expand Down Expand Up @@ -2187,7 +2191,8 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
auto silTy = SILType::getBuiltinIntegerLiteralType(ctx);
auto ty = silTy.getASTType();
SILValue integer = SGF.B.createIntegerLiteral(magicLiteral, silTy, Value);
ManagedValue integerManaged = ManagedValue::forUnmanaged(integer);
ManagedValue integerManaged =
ManagedValue::forObjectRValueWithoutOwnership(integer);
PreparedArguments builtinLiteralArgs;
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
builtinLiteralArgs.add(magicLiteral, RValue(SGF, {integerManaged}, ty));
Expand Down Expand Up @@ -6090,7 +6095,7 @@ RValue SILGenFunction::emitApplyAllocatingInitializer(SILLocation loc,
if (selfMetaTy != selfParamMetaTy)
selfMeta = B.createUpcast(loc, selfMeta, selfParamMetaTy);

selfMetaVal = ManagedValue::forUnmanaged(selfMeta);
selfMetaVal = ManagedValue::forObjectRValueWithoutOwnership(selfMeta);
}

// Form the callee.
Expand Down Expand Up @@ -6221,10 +6226,9 @@ SILGenFunction::emitUninitializedArrayAllocation(Type ArrayTy,
// Invoke the intrinsic, which returns a tuple.
auto subMap = ArrayTy->getContextSubstitutionMap(SGM.M.getSwiftModule(),
Ctx.getArrayDecl());
auto result = emitApplyOfLibraryIntrinsic(Loc, allocate,
subMap,
ManagedValue::forUnmanaged(Length),
SGFContext());
auto result = emitApplyOfLibraryIntrinsic(
Loc, allocate, subMap,
ManagedValue::forObjectRValueWithoutOwnership(Length), SGFContext());

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

return ManagedValue::forUnmanaged(apply);
return ManagedValue::forObjectRValueWithoutOwnership(apply);
}

ManagedValue SILGenFunction::emitCancelAsyncTask(
Expand All @@ -6946,7 +6950,7 @@ ManagedValue SILGenFunction::emitCancelAsyncTask(
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::CancelAsyncTask)),
getLoweredType(ctx.TheEmptyTupleType), SubstitutionMap(),
{ task });
return ManagedValue::forUnmanaged(apply);
return ManagedValue::forObjectRValueWithoutOwnership(apply);
}

ManagedValue SILGenFunction::emitReadAsyncLetBinding(SILLocation loc,
Expand All @@ -6967,11 +6971,12 @@ ManagedValue SILGenFunction::emitReadAsyncLetBinding(SILLocation loc,

// The intrinsic returns a pointer to the address of the result value inside
// the async let task context.
emitApplyOfLibraryIntrinsic(loc, asyncLetGet, {},
{ManagedValue::forTrivialObjectRValue(childTask.asyncLet),
ManagedValue::forTrivialObjectRValue(childTask.resultBuf)},
SGFContext());

emitApplyOfLibraryIntrinsic(
loc, asyncLetGet, {},
{ManagedValue::forObjectRValueWithoutOwnership(childTask.asyncLet),
ManagedValue::forObjectRValueWithoutOwnership(childTask.resultBuf)},
SGFContext());

auto resultAddr = B.createPointerToAddress(loc, childTask.resultBuf,
loweredOpaquePatternType.getAddressType(),
/*strict*/ true,
Expand Down Expand Up @@ -7054,10 +7059,11 @@ void SILGenFunction::emitFinishAsyncLet(
SILLocation loc, SILValue asyncLet, SILValue resultPtr) {
// This runtime function cancels the task, awaits its completion, and
// destroys the value in the result buffer if necessary.
emitApplyOfLibraryIntrinsic(loc, SGM.getFinishAsyncLet(), {},
{ManagedValue::forTrivialObjectRValue(asyncLet),
ManagedValue::forTrivialObjectRValue(resultPtr)},
SGFContext());
emitApplyOfLibraryIntrinsic(
loc, SGM.getFinishAsyncLet(), {},
{ManagedValue::forObjectRValueWithoutOwnership(asyncLet),
ManagedValue::forObjectRValueWithoutOwnership(resultPtr)},
SGFContext());
// This builtin ends the lifetime of the allocation for the async let.
auto &ctx = getASTContext();
B.createBuiltin(loc,
Expand Down
14 changes: 7 additions & 7 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ emitBridgeObjectiveCToNative(SILGenFunction &SGF, SILLocation loc,
ResultPlanPtr resultPlan =
ResultPlanBuilder::computeResultPlan(SGF, calleeTypeInfo, loc, context);
ArgumentScope argScope(SGF, loc);
RValue result =
SGF.emitApply(std::move(resultPlan), std::move(argScope), loc,
ManagedValue::forUnmanaged(witnessRef), subs,
{objcValue, ManagedValue::forUnmanaged(metatypeValue)},
calleeTypeInfo, ApplyOptions(), context, llvm::None);
RValue result = SGF.emitApply(
std::move(resultPlan), std::move(argScope), loc,
ManagedValue::forObjectRValueWithoutOwnership(witnessRef), subs,
{objcValue, ManagedValue::forObjectRValueWithoutOwnership(metatypeValue)},
calleeTypeInfo, ApplyOptions(), context, llvm::None);
return std::move(result).getAsSingleValue(SGF, loc);
}

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

if (indirectResult) {
Expand Down
12 changes: 7 additions & 5 deletions lib/SILGen/SILGenBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ ManagedValue SILGenBuilder::createPhi(SILType type,
return SGF.emitManagedBorrowedArgumentWithCleanup(arg);

case OwnershipKind::None:
return ManagedValue::forObjectRValueWithoutOwnership(arg);

case OwnershipKind::Unowned:
return ManagedValue::forUnmanaged(arg);
}
Expand Down Expand Up @@ -387,7 +389,7 @@ SILGenBuilder::bufferForExpr(SILLocation loc, SILType ty,

// Add a cleanup for the temporary we allocated.
if (lowering.isTrivial())
return ManagedValue::forUnmanaged(address);
return ManagedValue::forTrivialAddressRValue(address);

return SGF.emitManagedBufferWithCleanup(address);
}
Expand Down Expand Up @@ -415,7 +417,7 @@ ManagedValue SILGenBuilder::formalAccessBufferForExpr(

// Add a cleanup for the temporary we allocated.
if (lowering.isTrivial())
return ManagedValue::forUnmanaged(address);
return ManagedValue::forTrivialAddressRValue(address);

return SGF.emitFormalAccessManagedBufferWithCleanup(loc, address);
}
Expand Down Expand Up @@ -465,7 +467,7 @@ ManagedValue SILGenBuilder::createLoadTake(SILLocation loc, ManagedValue v,
SILValue result =
lowering.emitLoadOfCopy(*this, loc, v.forward(SGF), IsTake);
if (lowering.isTrivial())
return ManagedValue::forUnmanaged(result);
return ManagedValue::forObjectRValueWithoutOwnership(result);
assert((!lowering.isAddressOnly() || !SGF.silConv.useLoweredAddresses()) &&
"cannot retain an unloadable type");
return SGF.emitManagedRValueWithCleanup(result, lowering);
Expand All @@ -482,7 +484,7 @@ ManagedValue SILGenBuilder::createLoadCopy(SILLocation loc, ManagedValue v,
SILValue result =
lowering.emitLoadOfCopy(*this, loc, v.getValue(), IsNotTake);
if (lowering.isTrivial())
return ManagedValue::forUnmanaged(result);
return ManagedValue::forObjectRValueWithoutOwnership(result);
assert((!lowering.isAddressOnly()
|| !SGF.silConv.useLoweredAddresses()) &&
"cannot retain an unloadable type");
Expand Down Expand Up @@ -649,7 +651,7 @@ ManagedValue SILGenBuilder::createManagedOptionalNone(SILLocation loc,
if (!type.isAddressOnly(getFunction()) ||
!SGF.silConv.useLoweredAddresses()) {
SILValue noneValue = createOptionalNone(loc, type);
return ManagedValue::forUnmanaged(noneValue);
return ManagedValue::forObjectRValueWithoutOwnership(noneValue);
}

SILValue tempResult = SGF.emitTemporaryAllocation(loc, type);
Expand Down
Loading