Skip to content

Commit d502357

Browse files
authored
Merge pull request #15089 from gottesmm/pr-215a0ace32f79bcd4c1910c06c19468d27720cfe
2 parents 3ffbc41 + 8af8e4f commit d502357

File tree

6 files changed

+67
-86
lines changed

6 files changed

+67
-86
lines changed

lib/SILGen/SILGenBuilder.cpp

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,56 @@ ManagedValue SILGenBuilder::createLoadCopy(SILLocation loc, ManagedValue v,
519519
return SGF.emitManagedRValueWithCleanup(result, lowering);
520520
}
521521

522-
ManagedValue SILGenBuilder::createFunctionArgument(SILType type,
523-
ValueDecl *decl) {
524-
SILFunction &F = getFunction();
525-
526-
SILFunctionArgument *arg = F.begin()->createFunctionArgument(type, decl);
527-
if (arg->getType().isObject()) {
528-
if (arg->getOwnershipKind().isTrivialOr(ValueOwnershipKind::Owned))
529-
return SGF.emitManagedRValueWithCleanup(arg);
530-
return ManagedValue::forBorrowedRValue(arg);
522+
static ManagedValue createInputFunctionArgument(SILGenBuilder &B, SILType type,
523+
SILLocation loc,
524+
ValueDecl *decl = nullptr) {
525+
auto &SGF = B.getSILGenFunction();
526+
SILFunction &F = B.getFunction();
527+
assert((F.isBare() || decl) &&
528+
"Function arguments of non-bare functions must have a decl");
529+
auto *arg = F.begin()->createFunctionArgument(type, decl);
530+
switch (arg->getArgumentConvention()) {
531+
case SILArgumentConvention::Indirect_In_Guaranteed:
532+
case SILArgumentConvention::Direct_Guaranteed:
533+
// Guaranteed parameters are passed at +0.
534+
return ManagedValue::forUnmanaged(arg);
535+
case SILArgumentConvention::Direct_Unowned:
536+
// Unowned parameters are only guaranteed at the instant of the call, so we
537+
// must retain them even if we're in a context that can accept a +0 value.
538+
return ManagedValue::forUnmanaged(arg).copy(SGF, loc);
539+
540+
case SILArgumentConvention::Direct_Owned:
541+
return SGF.emitManagedRValueWithCleanup(arg);
542+
543+
case SILArgumentConvention::Indirect_In:
544+
if (SGF.silConv.useLoweredAddresses())
545+
return SGF.emitManagedBufferWithCleanup(arg);
546+
return SGF.emitManagedRValueWithCleanup(arg);
547+
548+
case SILArgumentConvention::Indirect_Inout:
549+
case SILArgumentConvention::Indirect_InoutAliasable:
550+
// An inout parameter is +0 and guaranteed, but represents an lvalue.
551+
return ManagedValue::forLValue(arg);
552+
case SILArgumentConvention::Indirect_In_Constant:
553+
llvm_unreachable("Convention not produced by SILGen");
554+
case SILArgumentConvention::Direct_Deallocating:
555+
case SILArgumentConvention::Indirect_Out:
556+
llvm_unreachable("unsupported convention for API");
531557
}
558+
llvm_unreachable("bad parameter convention");
559+
}
560+
561+
ManagedValue SILGenBuilder::createInputFunctionArgument(SILType type,
562+
ValueDecl *decl) {
563+
return ::createInputFunctionArgument(*this, type, SILLocation(decl), decl);
564+
}
532565

533-
return SGF.emitManagedBufferWithCleanup(arg);
566+
ManagedValue
567+
SILGenBuilder::createInputFunctionArgument(SILType type,
568+
Optional<SILLocation> inputLoc) {
569+
assert(inputLoc.hasValue() && "This optional is only for overload resolution "
570+
"purposes! Do not pass in None here!");
571+
return ::createInputFunctionArgument(*this, type, *inputLoc);
534572
}
535573

536574
ManagedValue

lib/SILGen/SILGenBuilder.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,20 @@ class SILGenBuilder : public SILBuilder {
255255
ManagedValue createLoadCopy(SILLocation loc, ManagedValue addr,
256256
const TypeLowering &lowering);
257257

258-
ManagedValue createFunctionArgument(SILType type, ValueDecl *decl);
258+
/// Create a SILArgument for an input parameter. Asserts if used to create a
259+
/// function argument for an out parameter.
260+
ManagedValue createInputFunctionArgument(SILType type, ValueDecl *decl);
261+
262+
/// Create a SILArgument for an input parameter. Uses \p loc to create any
263+
/// copies necessary. Asserts if used to create a function argument for an out
264+
/// parameter.
265+
///
266+
/// *NOTE* This API purposely used an Optional<SILLocation> to distinguish
267+
/// this API from the ValueDecl * API in C++. This is necessary since
268+
/// ValueDecl * can implicitly convert to SILLocation. The optional forces the
269+
/// user to be explicit that they want to use this API.
270+
ManagedValue createInputFunctionArgument(SILType type,
271+
Optional<SILLocation> loc);
259272

260273
using SILBuilder::createEnum;
261274
ManagedValue createEnum(SILLocation loc, ManagedValue payload,

lib/SILGen/SILGenConstructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
598598
ctor->hasThrows());
599599

600600
SILType selfTy = getLoweredLoadableType(selfDecl->getType());
601-
ManagedValue selfArg = B.createFunctionArgument(selfTy, selfDecl);
601+
ManagedValue selfArg = B.createInputFunctionArgument(selfTy, selfDecl);
602602

603603
if (!NeedsBoxForSelf) {
604604
SILLocation PrologueLoc(selfDecl);

lib/SILGen/SILGenPoly.cpp

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -785,40 +785,6 @@ ManagedValue Transform::transformTuple(ManagedValue inputTuple,
785785
return SGF.emitManagedRValueWithCleanup(outputTuple, outputTL);
786786
}
787787

788-
static ManagedValue manageParam(SILGenFunction &SGF,
789-
SILLocation loc,
790-
SILValue paramValue,
791-
SILParameterInfo info) {
792-
switch (info.getConvention()) {
793-
case ParameterConvention::Indirect_In_Guaranteed:
794-
if (SGF.silConv.useLoweredAddresses())
795-
return ManagedValue::forUnmanaged(paramValue);
796-
LLVM_FALLTHROUGH;
797-
case ParameterConvention::Direct_Guaranteed:
798-
return SGF.emitManagedBeginBorrow(loc, paramValue);
799-
// Unowned parameters are only guaranteed at the instant of the call, so we
800-
// must retain them even if we're in a context that can accept a +0 value.
801-
case ParameterConvention::Direct_Unowned:
802-
paramValue = SGF.getTypeLowering(paramValue->getType())
803-
.emitCopyValue(SGF.B, loc, paramValue);
804-
LLVM_FALLTHROUGH;
805-
case ParameterConvention::Direct_Owned:
806-
return SGF.emitManagedRValueWithCleanup(paramValue);
807-
808-
case ParameterConvention::Indirect_In:
809-
if (SGF.silConv.useLoweredAddresses())
810-
return SGF.emitManagedBufferWithCleanup(paramValue);
811-
return SGF.emitManagedRValueWithCleanup(paramValue);
812-
813-
case ParameterConvention::Indirect_Inout:
814-
case ParameterConvention::Indirect_InoutAliasable:
815-
return ManagedValue::forLValue(paramValue);
816-
case ParameterConvention::Indirect_In_Constant:
817-
break;
818-
}
819-
llvm_unreachable("bad parameter convention");
820-
}
821-
822788
void SILGenFunction::collectThunkParams(
823789
SILLocation loc, SmallVectorImpl<ManagedValue> &params,
824790
SmallVectorImpl<SILArgument *> *indirectResults) {
@@ -834,9 +800,7 @@ void SILGenFunction::collectThunkParams(
834800
auto paramTypes = F.getLoweredFunctionType()->getParameters();
835801
for (auto param : paramTypes) {
836802
auto paramTy = F.mapTypeIntoContext(F.getConventions().getSILType(param));
837-
auto paramValue = F.begin()->createFunctionArgument(paramTy);
838-
auto paramMV = manageParam(*this, loc, paramValue, param);
839-
params.push_back(paramMV);
803+
params.push_back(B.createInputFunctionArgument(paramTy, loc));
840804
}
841805
}
842806

lib/SILGen/SILGenProlog.cpp

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -92,39 +92,6 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
9292
: SGF(sgf), parent(parent), loc(l), functionArgs(functionArgs),
9393
parameters(parameters) {}
9494

95-
ManagedValue getManagedValue(SILValue arg, CanType t,
96-
SILParameterInfo parameterInfo) const {
97-
switch (parameterInfo.getConvention()) {
98-
case ParameterConvention::Direct_Guaranteed:
99-
case ParameterConvention::Indirect_In_Guaranteed:
100-
// If we have a guaranteed parameter, it is passed in at +0, and its
101-
// lifetime is guaranteed. We can potentially use the argument as-is
102-
// if the parameter is bound as a 'let' without cleaning up.
103-
return ManagedValue::forUnmanaged(arg);
104-
105-
case ParameterConvention::Direct_Unowned:
106-
// An unowned parameter is passed at +0, like guaranteed, but it isn't
107-
// kept alive by the caller, so we need to retain and manage it
108-
// regardless.
109-
return SGF.emitManagedRetain(loc, arg);
110-
111-
case ParameterConvention::Indirect_Inout:
112-
case ParameterConvention::Indirect_InoutAliasable:
113-
// An inout parameter is +0 and guaranteed, but represents an lvalue.
114-
return ManagedValue::forLValue(arg);
115-
116-
case ParameterConvention::Direct_Owned:
117-
case ParameterConvention::Indirect_In:
118-
// An owned or 'in' parameter is passed in at +1. We can claim ownership
119-
// of the parameter and clean it up when it goes out of scope.
120-
return SGF.emitManagedRValueWithCleanup(arg);
121-
122-
case ParameterConvention::Indirect_In_Constant:
123-
break;
124-
}
125-
llvm_unreachable("bad parameter convention");
126-
}
127-
12895
ManagedValue visitType(CanType t) {
12996
auto argType = SGF.getLoweredType(t);
13097
// Pop the next parameter info.
@@ -136,9 +103,8 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
136103
SGF.getSILType(parameterInfo))
137104
&& "argument does not have same type as specified by parameter info");
138105

139-
SILValue arg =
140-
parent->createFunctionArgument(argType, loc.getAsASTNode<ValueDecl>());
141-
ManagedValue mv = getManagedValue(arg, t, parameterInfo);
106+
ManagedValue mv = SGF.B.createInputFunctionArgument(
107+
argType, loc.getAsASTNode<ValueDecl>());
142108

143109
// If the value is a (possibly optional) ObjC block passed into the entry
144110
// point of the function, then copy it so we can treat the value reliably

lib/SILGen/SILGenThunk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void SILGenFunction::emitCurryThunk(SILDeclRef thunk) {
152152
->getInput();
153153
selfTy = vd->getInnermostDeclContext()->mapTypeIntoContext(selfTy);
154154
ManagedValue selfArg =
155-
B.createFunctionArgument(getLoweredType(selfTy), nullptr);
155+
B.createInputFunctionArgument(getLoweredType(selfTy), SILLocation(vd));
156156

157157
// Forward substitutions.
158158
auto subs = F.getForwardingSubstitutions();

0 commit comments

Comments
 (0)