Skip to content

Commit 1955778

Browse files
committed
SIL: Remove type lowering support for InOutType
There is still some unfortunate hackery around InOutType in SILGenProlog.cpp, but I don't want to clean it up yet.
1 parent 296ce3f commit 1955778

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

lib/SIL/SILFunctionType.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,16 @@ class DestructureInputs {
686686

687687
unsigned origParamIndex = NextOrigParamIndex++;
688688

689+
bool isInout = false;
690+
if (auto inoutType = dyn_cast<InOutType>(substType)) {
691+
isInout = true;
692+
substType = inoutType.getObjectType();
693+
origType = origType.getWithoutSpecifierType();
694+
}
695+
689696
auto &substTL = M.Types.getTypeLowering(origType, substType);
690697
ParameterConvention convention;
691-
if (isa<InOutType>(substType)) {
692-
assert(origType.isTypeParameter() || origType.getAs<InOutType>());
698+
if (isInout) {
693699
convention = ParameterConvention::Indirect_Inout;
694700
} else if (isFormallyPassedIndirectly(origType, substType, substTL)) {
695701
if (forSelf && rep == SILFunctionTypeRepresentation::WitnessMethod)

lib/SIL/TypeLowering.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ TypeConverter::~TypeConverter() {
13011301
CanType srcType = ti.first.OrigType;
13021302
if (!srcType) continue;
13031303
CanType mappedType = ti.second->getLoweredType().getASTType();
1304-
if (srcType == mappedType || isa<InOutType>(srcType))
1304+
if (srcType == mappedType)
13051305
ti.second->~TypeLowering();
13061306
}
13071307
}
@@ -1440,26 +1440,11 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,
14401440

14411441
assert((!key.isDependent() || getCurGenericContext())
14421442
&& "dependent type outside of generic context?!");
1443-
1443+
assert(!substType->is<InOutType>());
1444+
14441445
if (auto existing = find(key))
14451446
return *existing;
14461447

1447-
// inout types are a special case for lowering, because they get
1448-
// completely removed and represented as 'address' SILTypes.
1449-
if (isa<InOutType>(substType)) {
1450-
// Derive SILType for InOutType from the object type.
1451-
CanType substObjectType = substType.getWithoutSpecifierType();
1452-
AbstractionPattern origObjectType = origType.getWithoutSpecifierType();
1453-
1454-
SILType loweredType = getLoweredType(origObjectType, substObjectType)
1455-
.getAddressType();
1456-
1457-
auto *theInfo = new (*this, key.isDependent())
1458-
TrivialTypeLowering(loweredType);
1459-
insert(key, theInfo);
1460-
return *theInfo;
1461-
}
1462-
14631448
// Lower the type.
14641449
CanType loweredSubstType =
14651450
getLoweredRValueType(origType, substType);

lib/SILGen/SILGenApply.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,9 @@ class ArgEmitter {
24172417
// pass the entire tuple indirectly, but if it's not
24182418
// materializable, the convention is actually to break it up
24192419
// into materializable chunks. See the comment in SILType.cpp.
2420+
//
2421+
// FIXME: Once -swift-version 3 code generation goes away, we
2422+
// can simplify this.
24202423
if (isUnmaterializableTupleType(substArgType)) {
24212424
assert(origParamType.isTypeParameter());
24222425
emitExpanded(std::move(arg), origParamType);
@@ -2441,10 +2444,19 @@ class ArgEmitter {
24412444
SILParameterInfo param = claimNextParameter();
24422445
ArgSpecialDest *specialDest = claimNextSpecialDest();
24432446

2447+
// Unwrap InOutType here.
2448+
assert(isa<InOutType>(substArgType) == param.isIndirectInOut());
2449+
if (auto inoutType = dyn_cast<InOutType>(substArgType))
2450+
substArgType = inoutType.getObjectType();
2451+
24442452
// Make sure we use the same value category for these so that we
24452453
// can hereafter just use simple equality checks to test for
24462454
// abstraction.
24472455
SILType loweredSubstArgType = SGF.getLoweredType(substArgType);
2456+
if (param.isIndirectInOut()) {
2457+
loweredSubstArgType =
2458+
SILType::getPrimitiveAddressType(loweredSubstArgType.getASTType());
2459+
}
24482460
SILType loweredSubstParamType =
24492461
SILType::getPrimitiveType(param.getType(),
24502462
loweredSubstArgType.getCategory());
@@ -2453,7 +2465,6 @@ class ArgEmitter {
24532465
// inout type.
24542466
if (param.isIndirectInOut()) {
24552467
assert(!specialDest);
2456-
assert(isa<InOutType>(substArgType));
24572468
emitInOut(std::move(arg), loweredSubstArgType, loweredSubstParamType,
24582469
origParamType, substArgType);
24592470
return;
@@ -2641,10 +2652,9 @@ class ArgEmitter {
26412652
auto address = std::move(arg).asKnownRValue(SGF).getAsSingleValue(
26422653
SGF, arg.getKnownRValueLocation());
26432654
assert(address.isLValue());
2644-
auto substObjectType = cast<InOutType>(substType).getObjectType();
26452655
return LValue::forAddress(address, None,
2646-
AbstractionPattern(substObjectType),
2647-
substObjectType);
2656+
AbstractionPattern(substType),
2657+
substType);
26482658
} else {
26492659
auto *e = cast<InOutExpr>(std::move(arg).asKnownExpr()->
26502660
getSemanticsProvidingExpr());

lib/SILGen/SILGenProlog.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
9393
parameters(parameters) {}
9494

9595
ManagedValue visitType(CanType t) {
96-
auto argType = SGF.getLoweredType(t);
96+
auto argType = SGF.getLoweredType(t->getInOutObjectType());
97+
if (t->is<InOutType>())
98+
argType = SILType::getPrimitiveAddressType(argType.getASTType());
99+
97100
// Pop the next parameter info.
98101
auto parameterInfo = parameters.front();
99102
parameters = parameters.slice(1);
@@ -225,11 +228,9 @@ struct ArgumentInitHelper {
225228
if (vd->isInOut()) {
226229
SILValue address = argrv.getUnmanagedValue();
227230

228-
CanType objectType = vd->getType()->getCanonicalType();
229-
230231
// As a special case, don't introduce a local variable for
231232
// Builtin.UnsafeValueBuffer, which is not copyable.
232-
if (isa<BuiltinUnsafeValueBufferType>(objectType)) {
233+
if (isa<BuiltinUnsafeValueBufferType>(ty->getCanonicalType())) {
233234
// FIXME: mark a debug location?
234235
SGF.VarLocs[vd] = SILGenFunction::VarLoc::get(address);
235236
SGF.B.createDebugValueAddr(loc, address,
@@ -279,6 +280,8 @@ struct ArgumentInitHelper {
279280

280281
void emitAnonymousParam(Type type, SILLocation paramLoc, ParamDecl *PD) {
281282
// Allow non-materializable tuples to be bound to anonymous parameters.
283+
//
284+
// FIXME: Remove this once -swift-version 3 code generation goes away.
282285
if (!type->isMaterializable()) {
283286
if (auto tupleType = type->getAs<TupleType>()) {
284287
for (auto eltType : tupleType->getElementTypes()) {
@@ -320,8 +323,10 @@ static void makeArgument(Type ty, ParamDecl *decl,
320323
for (auto fieldType : tupleTy->getElementTypes())
321324
makeArgument(fieldType, decl, args, SGF);
322325
} else {
323-
auto arg =
324-
SGF.F.begin()->createFunctionArgument(SGF.getLoweredType(ty), decl);
326+
auto loweredTy = SGF.getLoweredType(ty);
327+
if (decl->isInOut())
328+
loweredTy = SILType::getPrimitiveAddressType(loweredTy.getASTType());
329+
auto arg = SGF.F.begin()->createFunctionArgument(loweredTy, decl);
325330
args.push_back(arg);
326331
}
327332
}
@@ -333,8 +338,6 @@ void SILGenFunction::bindParametersForForwarding(const ParameterList *params,
333338
Type type = (param->hasType()
334339
? param->getType()
335340
: F.mapTypeIntoContext(param->getInterfaceType()));
336-
if (param->isInOut())
337-
type = InOutType::get(type); // FIXME remove InOutType
338341
makeArgument(type->eraseDynamicSelfType(), param, parameters, *this);
339342
}
340343
}

0 commit comments

Comments
 (0)