Skip to content

Commit 1417647

Browse files
committed
SILGen: (Almost) remove "scalar" PreparedArguments
They still exist when an ArgumentShuffleExpr is decomposed, but that's about to go away.
1 parent 50b2442 commit 1417647

File tree

4 files changed

+42
-99
lines changed

4 files changed

+42
-99
lines changed

lib/SILGen/ArgumentSource.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@ RValue &ArgumentSource::peekRValue() & {
2727
return Storage.get<RValueStorage>(StoredKind).Value;
2828
}
2929

30-
bool ArgumentSource::isShuffle() const {
31-
switch (StoredKind) {
32-
case Kind::Invalid:
33-
llvm_unreachable("argument source is invalid");
34-
case Kind::RValue:
35-
case Kind::LValue:
36-
return false;
37-
case Kind::Expr:
38-
return isa<ArgumentShuffleExpr>(asKnownExpr());
39-
}
40-
llvm_unreachable("bad kind");
41-
}
42-
4330
RValue ArgumentSource::getAsRValue(SILGenFunction &SGF, SGFContext C) && {
4431
switch (StoredKind) {
4532
case Kind::Invalid:
@@ -263,10 +250,11 @@ void ArgumentSource::dump(raw_ostream &out, unsigned indent) const {
263250

264251
PreparedArguments::PreparedArguments(
265252
ArrayRef<AnyFunctionType::Param> params,
266-
Expr *arg) : PreparedArguments(params, /*scalar*/ isa<ArgumentShuffleExpr>(arg)) {
267-
if (isa<ArgumentShuffleExpr>(arg))
253+
Expr *arg) : PreparedArguments(params) {
254+
if (isa<ArgumentShuffleExpr>(arg)) {
255+
IsScalar = true;
268256
addArbitrary(arg);
269-
else if (auto *PE = dyn_cast<ParenExpr>(arg))
257+
} else if (auto *PE = dyn_cast<ParenExpr>(arg))
270258
addArbitrary(PE->getSubExpr());
271259
else if (auto *TE = dyn_cast<TupleExpr>(arg)) {
272260
for (auto *elt : TE->getElements())
@@ -277,17 +265,13 @@ PreparedArguments::PreparedArguments(
277265
}
278266
}
279267

280-
void PreparedArguments::emplaceEmptyArgumentList(SILGenFunction &SGF) {
281-
emplace({}, /*scalar*/ false);
282-
assert(isValid());
283-
}
284-
285268
PreparedArguments
286269
PreparedArguments::copy(SILGenFunction &SGF, SILLocation loc) const {
287270
if (isNull()) return PreparedArguments();
288271

289272
assert(isValid());
290-
PreparedArguments result(getParams(), isScalar());
273+
PreparedArguments result(getParams());
274+
result.IsScalar = isScalar();
291275
for (auto &elt : Arguments) {
292276
assert(elt.isRValue());
293277
result.add(elt.getKnownRValueLocation(),
@@ -335,7 +319,8 @@ PreparedArguments PreparedArguments::copyForDiagnostics() const {
335319
return PreparedArguments();
336320

337321
assert(isValid());
338-
PreparedArguments result(getParams(), isScalar());
322+
PreparedArguments result(getParams());
323+
result.IsScalar = isScalar();
339324
for (auto &arg : Arguments) {
340325
result.Arguments.push_back(arg.copyForDiagnostics());
341326
}

lib/SILGen/ArgumentSource.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@ class ArgumentSource {
240240
AbstractionPattern origFormalType,
241241
SILType expectedType = SILType()) &&;
242242

243-
/// Whether this argument source is an ArgumentShuffleExpr.
244-
bool isShuffle() const;
245-
246243
bool isObviouslyEqual(const ArgumentSource &other) const;
247244

248245
ArgumentSource copyForDiagnostics() const;
@@ -268,9 +265,9 @@ class PreparedArguments {
268265
unsigned IsNull : 1;
269266
public:
270267
PreparedArguments() : IsScalar(false), IsNull(true) {}
271-
PreparedArguments(ArrayRef<AnyFunctionType::Param> params, bool isScalar)
272-
: IsNull(true) {
273-
emplace(params, isScalar);
268+
explicit PreparedArguments(ArrayRef<AnyFunctionType::Param> params)
269+
: IsScalar(false), IsNull(true) {
270+
emplace(params);
274271
}
275272

276273
// Decompse an argument list expression.
@@ -323,16 +320,13 @@ class PreparedArguments {
323320
}
324321

325322
/// Emplace a (probably incomplete) argument list.
326-
void emplace(ArrayRef<AnyFunctionType::Param> params, bool isScalar) {
323+
void emplace(ArrayRef<AnyFunctionType::Param> params) {
327324
assert(isNull());
328325
Params.append(params.begin(), params.end());
329-
IsScalar = isScalar;
326+
IsScalar = false;
330327
IsNull = false;
331328
}
332329

333-
/// Emplace an empty argument list.
334-
void emplaceEmptyArgumentList(SILGenFunction &SGF);
335-
336330
/// Add an emitted r-value argument to this argument list.
337331
void add(SILLocation loc, RValue &&arg) {
338332
assert(!isNull());

lib/SILGen/SILGenApply.cpp

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ static PreparedArguments emitStringLiteral(SILGenFunction &SGF, Expr *E,
16681668
unicode::extractFirstUnicodeScalar(Str));
16691669

16701670
AnyFunctionType::Param param(Int32Ty.getASTType());
1671-
PreparedArguments args({param}, /*scalar*/ false);
1671+
PreparedArguments args({param});
16721672
args.add(E, RValue(SGF, E, Int32Ty.getASTType(),
16731673
ManagedValue::forUnmanaged(UnicodeScalarValue)));
16741674
return args;
@@ -1717,7 +1717,7 @@ static PreparedArguments emitStringLiteral(SILGenFunction &SGF, Expr *E,
17171717
llvm_unreachable("these cannot be formed here");
17181718
}
17191719

1720-
PreparedArguments args(TypeElts, /*scalar*/ false);
1720+
PreparedArguments args(TypeElts);
17211721
for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
17221722
args.add(E, RValue(SGF, Elts[i], CanType(TypeElts[i].getPlainType())));
17231723
}
@@ -2719,51 +2719,6 @@ class ArgEmitter {
27192719
maybeEmitForeignErrorArgument();
27202720
}
27212721

2722-
// origFormalType is a function type.
2723-
//
2724-
// FIXME: This is all a bunch of hacks that can be removed once "scalar"
2725-
// PreparedArguments goes away.
2726-
void emitTopLevel(ArgumentSource &&arg, AbstractionPattern origFormalType) {
2727-
SmallVector<AbstractionPattern, 8> origParamTypes;
2728-
for (unsigned i = 0, e = origFormalType.getNumFunctionParams(); i < e; ++i) {
2729-
origParamTypes.push_back(origFormalType.getFunctionParamType(i));
2730-
}
2731-
2732-
auto origParamType = AbstractionPattern::getTuple(origParamTypes);
2733-
2734-
if (arg.isShuffle()) {
2735-
auto *shuffle = cast<ArgumentShuffleExpr>(std::move(arg).asKnownExpr());
2736-
emitShuffle(shuffle, origParamType);
2737-
maybeEmitForeignErrorArgument();
2738-
return;
2739-
}
2740-
2741-
if (arg.isLValue()) {
2742-
assert(origParamTypes.size() == 1);
2743-
emitSingleArg(std::move(arg), origParamTypes[0]);
2744-
return;
2745-
}
2746-
2747-
if (arg.isExpr()) {
2748-
if (origParamTypes.size() == 1) {
2749-
auto *e = std::move(arg).asKnownExpr();
2750-
2751-
origParamType = origParamTypes[0];
2752-
if (auto *paren = dyn_cast<ParenExpr>(e))
2753-
e = paren->getSubExpr();
2754-
else if (auto *tuple = dyn_cast<TupleExpr>(e)) {
2755-
assert(tuple->getNumElements() == 1);
2756-
e = tuple->getElement(0);
2757-
}
2758-
2759-
emitSingleArg(e, origParamType);
2760-
return;
2761-
}
2762-
}
2763-
2764-
emitSingleArg(std::move(arg), origParamType);
2765-
}
2766-
27672722
// origFormalType is a function type.
27682723
void emitPreparedArgs(PreparedArguments &&args,
27692724
AbstractionPattern origFormalType) {
@@ -2772,7 +2727,18 @@ class ArgEmitter {
27722727

27732728
if (args.isScalar()) {
27742729
assert(argSources.size() == 1);
2775-
emitTopLevel(std::move(argSources[0]), origFormalType);
2730+
auto arg = std::move(argSources[0]);
2731+
auto *shuffle = cast<ArgumentShuffleExpr>(std::move(arg).asKnownExpr());
2732+
2733+
SmallVector<AbstractionPattern, 8> origParamTypes;
2734+
for (unsigned i = 0, e = origFormalType.getNumFunctionParams(); i < e; ++i) {
2735+
origParamTypes.push_back(origFormalType.getFunctionParamType(i));
2736+
}
2737+
2738+
auto origParamType = AbstractionPattern::getTuple(origParamTypes);
2739+
2740+
emitShuffle(shuffle, origParamType);
2741+
maybeEmitForeignErrorArgument();
27762742
} else {
27772743
maybeEmitForeignErrorArgument();
27782744
for (auto i : indices(argSources)) {
@@ -4138,7 +4104,7 @@ class CallEmission {
41384104
ArgumentSource &&selfArg,
41394105
AnyFunctionType::Param selfParam,
41404106
CanType methodType) {
4141-
PreparedArguments preparedSelf({selfParam}, /*scalar*/ false);
4107+
PreparedArguments preparedSelf({selfParam});
41424108
preparedSelf.addArbitrary(std::move(selfArg));
41434109

41444110
addCallSite(loc, std::move(preparedSelf), methodType,
@@ -5527,15 +5493,15 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
55275493
builtinInit = stringLiteral->getBuiltinInitializer();
55285494
init = stringLiteral->getInitializer();
55295495
} else if (auto nilLiteral = dyn_cast<NilLiteralExpr>(literal)) {
5530-
builtinLiteralArgs.emplace({}, /*scalar*/ false);
5496+
builtinLiteralArgs.emplace({});
55315497
builtinInit = nilLiteral->getInitializer();
55325498
} else if (auto booleanLiteral = dyn_cast<BooleanLiteralExpr>(literal)) {
55335499
auto i1Ty = SILType::getBuiltinIntegerType(1, getASTContext());
55345500
SILValue boolValue = B.createIntegerLiteral(booleanLiteral, i1Ty,
55355501
booleanLiteral->getValue());
55365502
ManagedValue boolManaged = ManagedValue::forUnmanaged(boolValue);
55375503
CanType ty = boolManaged.getType().getASTType()->getCanonicalType();
5538-
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty), /*scalar*/ false);
5504+
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
55395505
builtinLiteralArgs.add(literal, RValue(*this, {boolManaged}, ty));
55405506
builtinInit = booleanLiteral->getBuiltinInitializer();
55415507
init = booleanLiteral->getInitializer();
@@ -5546,7 +5512,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
55465512
SILType::getBuiltinIntegerLiteralType(getASTContext()),
55475513
integerLiteral->getRawValue()));
55485514
CanType ty = integerManaged.getType().getASTType();
5549-
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty), /*scalar*/ false);
5515+
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
55505516
builtinLiteralArgs.add(literal, RValue(*this, {integerManaged}, ty));
55515517
builtinInit = integerLiteral->getBuiltinInitializer();
55525518
init = integerLiteral->getInitializer();
@@ -5558,7 +5524,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
55585524
floatLiteral->getValue()));
55595525

55605526
CanType ty = floatManaged.getType().getASTType();
5561-
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty), /*scalar*/ false);
5527+
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
55625528
builtinLiteralArgs.add(literal, RValue(*this, {floatManaged}, ty));
55635529
builtinInit = floatLiteral->getBuiltinInitializer();
55645530
init = floatLiteral->getInitializer();
@@ -5604,7 +5570,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
56045570
auto ty = silTy.getASTType();
56055571
SILValue integer = B.createIntegerLiteral(literal, silTy, Value);
56065572
ManagedValue integerManaged = ManagedValue::forUnmanaged(integer);
5607-
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty), /*scalar*/ false);
5573+
builtinLiteralArgs.emplace(AnyFunctionType::Param(ty));
56085574
builtinLiteralArgs.add(literal, RValue(*this, {integerManaged}, ty));
56095575
builtinInit = magicLiteral->getBuiltinInitializer();
56105576
init = magicLiteral->getInitializer();
@@ -5627,7 +5593,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
56275593

56285594
// Otherwise, perform the second initialization step.
56295595
auto ty = builtinLiteral.getType();
5630-
PreparedArguments args(AnyFunctionType::Param(ty), /*scalar*/ false);
5596+
PreparedArguments args((AnyFunctionType::Param(ty)));
56315597
args.add(literal, std::move(builtinLiteral));
56325598

56335599
RValue result = emitApplyAllocatingInitializer(literal, init,
@@ -6074,7 +6040,7 @@ SILGenFunction::prepareSubscriptIndices(SubscriptDecl *subscript,
60746040
// Finally, prepare the evaluated index expression. We might be calling
60756041
// the getter and setter, and it is important to only evaluate the
60766042
// index expression once.
6077-
PreparedArguments result(substParams, /*isScalar=*/false);
6043+
PreparedArguments result(substParams);
60786044

60796045
ArrayRef<ManagedValue> remainingArgs = argValues;
60806046
for (auto substParam : substParams) {
@@ -6121,7 +6087,7 @@ RValue SILGenFunction::emitGetAccessor(SILLocation loc, SILDeclRef get,
61216087
}
61226088
// Index or () if none.
61236089
if (subscriptIndices.isNull())
6124-
subscriptIndices.emplaceEmptyArgumentList(*this);
6090+
subscriptIndices.emplace({});
61256091

61266092
emission.addCallSite(loc, std::move(subscriptIndices),
61276093
accessType.getResult(),
@@ -6157,7 +6123,7 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
61576123
}
61586124

61596125
// (value) or (value, indices...)
6160-
PreparedArguments values(accessType->getParams(), /*scalar*/ false);
6126+
PreparedArguments values(accessType->getParams());
61616127
values.addArbitrary(std::move(setValue));
61626128

61636129
if (!subscriptIndices.isNull()) {
@@ -6202,7 +6168,7 @@ ManagedValue SILGenFunction::emitAddressorAccessor(
62026168
}
62036169
// Index or () if none.
62046170
if (subscriptIndices.isNull())
6205-
subscriptIndices.emplaceEmptyArgumentList(*this);
6171+
subscriptIndices.emplace({});
62066172

62076173
emission.addCallSite(loc, std::move(subscriptIndices),
62086174
accessType.getResult(),
@@ -6267,7 +6233,7 @@ SILGenFunction::emitCoroutineAccessor(SILLocation loc, SILDeclRef accessor,
62676233
}
62686234
// Index or () if none.
62696235
if (subscriptIndices.isNull())
6270-
subscriptIndices.emplaceEmptyArgumentList(*this);
6236+
subscriptIndices.emplace({});
62716237

62726238
emission.addCallSite(loc, std::move(subscriptIndices),
62736239
accessType.getResult(),

lib/SILGen/SILGenExpr.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,8 +1868,7 @@ static RValue emitBoolLiteral(SILGenFunction &SGF, SILLocation loc,
18681868
RValue builtinArg(SGF, ManagedValue::forUnmanaged(builtinBool),
18691869
builtinArgType);
18701870

1871-
PreparedArguments builtinArgs(AnyFunctionType::Param(builtinArgType),
1872-
/*scalar*/ false);
1871+
PreparedArguments builtinArgs((AnyFunctionType::Param(builtinArgType)));
18731872
builtinArgs.add(loc, std::move(builtinArg));
18741873

18751874
auto result =
@@ -2415,7 +2414,7 @@ loadIndexValuesForKeyPathComponent(SILGenFunction &SGF, SILLocation loc,
24152414
indexParams.emplace_back(SGF.F.mapTypeIntoContext(elt.first));
24162415
}
24172416

2418-
PreparedArguments indexValues(indexParams, /*scalar*/ false);
2417+
PreparedArguments indexValues(indexParams);
24192418
if (indexes.empty()) {
24202419
assert(indexValues.isValid());
24212420
return indexValues;
@@ -3649,8 +3648,7 @@ RValue RValueEmitter::visitArrayExpr(ArrayExpr *E, SGFContext C) {
36493648
return array;
36503649

36513650
// Call the builtin initializer.
3652-
PreparedArguments args(AnyFunctionType::Param(E->getType()),
3653-
/*scalar*/ false);
3651+
PreparedArguments args(AnyFunctionType::Param(E->getType()));
36543652
args.add(E, std::move(array));
36553653

36563654
return SGF.emitApplyAllocatingInitializer(

0 commit comments

Comments
 (0)