Skip to content

Commit 4a8a3ac

Browse files
committed
[NFC] Use a generator in prolog emission to generate parameters.
This adds an assertion that we're using all of the parameters, so pass prolog emission the number of lowered parameters to ignore. That's easy for the callers to provide, since they do actually still need to add function arguments for those parameters.
1 parent 0b3b0d8 commit 4a8a3ac

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

lib/SILGen/SILGenConstructor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
414414
/*selfParam=*/nullptr,
415415
ctor->getResultInterfaceType(), ctor,
416416
ctor->hasThrows(),
417-
ctor->getThrowsLoc());
417+
ctor->getThrowsLoc(),
418+
/*ignored parameters*/ 1);
418419
emitConstructorMetatypeArg(*this, ctor);
419420

420421
// Make sure we've hopped to the right global actor, if any.
@@ -816,7 +817,8 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
816817
// FIXME: Handle self along with the other body patterns.
817818
uint16_t ArgNo = emitBasicProlog(ctor->getParameters(), /*selfParam=*/nullptr,
818819
TupleType::getEmpty(F.getASTContext()), ctor,
819-
ctor->hasThrows(), ctor->getThrowsLoc());
820+
ctor->hasThrows(), ctor->getThrowsLoc(),
821+
/*ignored parameters*/ 1);
820822

821823
SILType selfTy = getLoweredLoadableType(selfDecl->getType());
822824
ManagedValue selfArg = B.createInputFunctionArgument(selfTy, selfDecl);

lib/SILGen/SILGenFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,8 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, VarDecl *var) {
12311231
}
12321232

12331233
emitBasicProlog(/*paramList*/ nullptr, /*selfParam*/ nullptr,
1234-
interfaceType, dc, /*throws=*/ false,SourceLoc());
1234+
interfaceType, dc, /*throws=*/ false,SourceLoc(),
1235+
/*ignored parameters*/ 0);
12351236
prepareEpilog(interfaceType, false, CleanupLocation(loc));
12361237

12371238
auto pbd = var->getParentPatternBinding();

lib/SILGen/SILGenFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
11011101
uint16_t emitBasicProlog(ParameterList *paramList, ParamDecl *selfParam,
11021102
Type resultType, DeclContext *DC,
11031103
bool throws, SourceLoc throwsLoc,
1104+
unsigned numIgnoredTrailingParameters,
11041105
Optional<AbstractionPattern> origClosureType = None);
11051106

11061107
/// Create SILArguments in the entry block that bind a single value

lib/SILGen/SILGenProlog.cpp

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/ParameterList.h"
2424
#include "swift/AST/PropertyWrappers.h"
2525
#include "swift/Basic/Defer.h"
26+
#include "swift/Basic/Generators.h"
2627
#include "swift/SIL/SILArgument.h"
2728
#include "swift/SIL/SILArgumentConvention.h"
2829
#include "swift/SIL/SILInstruction.h"
@@ -76,22 +77,22 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
7677
SILBasicBlock *parent;
7778
SILLocation loc;
7879
CanSILFunctionType fnTy;
79-
ArrayRef<SILParameterInfo> &parameters;
80+
ArrayRefGenerator<ArrayRef<SILParameterInfo>> &parameters;
8081
bool isNoImplicitCopy;
8182
LifetimeAnnotation lifetimeAnnotation;
8283

8384
EmitBBArguments(SILGenFunction &sgf, SILBasicBlock *parent, SILLocation l,
8485
CanSILFunctionType fnTy,
85-
ArrayRef<SILParameterInfo> &parameters, bool isNoImplicitCopy,
86+
ArrayRefGenerator<ArrayRef<SILParameterInfo>> &parameters,
87+
bool isNoImplicitCopy,
8688
LifetimeAnnotation lifetimeAnnotation)
8789
: SGF(sgf), parent(parent), loc(l), fnTy(fnTy), parameters(parameters),
8890
isNoImplicitCopy(isNoImplicitCopy),
8991
lifetimeAnnotation(lifetimeAnnotation) {}
9092

9193
ManagedValue claimNextParameter() {
9294
// Pop the next parameter info.
93-
auto parameterInfo = parameters.front();
94-
parameters = parameters.slice(1);
95+
auto parameterInfo = parameters.claimNext();
9596

9697
auto paramType =
9798
SGF.F.mapTypeIntoContext(SGF.getSILType(parameterInfo, fnTy));
@@ -408,28 +409,48 @@ namespace {
408409

409410
/// A helper for creating SILArguments and binding variables to the argument
410411
/// names.
411-
struct ArgumentInitHelper {
412+
class ArgumentInitHelper {
412413
SILGenFunction &SGF;
413414
SILFunction &f;
414-
SILGenBuilder &initB;
415415

416-
/// An ArrayRef that we use in our SILParameterList queue. Parameters are
417-
/// sliced off of the front as they're emitted.
418-
ArrayRef<SILParameterInfo> parameters;
416+
ArrayRefGenerator<ArrayRef<SILParameterInfo>> parameters;
419417
uint16_t ArgNo = 0;
420418

421419
Optional<AbstractionPattern> OrigFnType;
422420

421+
public:
423422
ArgumentInitHelper(SILGenFunction &SGF, SILFunction &f,
424-
Optional<AbstractionPattern> origFnType)
425-
: SGF(SGF), f(f), initB(SGF.B),
423+
Optional<AbstractionPattern> origFnType,
424+
unsigned numIgnoredTrailingParameters)
425+
: SGF(SGF), f(f),
426426
parameters(
427427
f.getLoweredFunctionTypeInContext(SGF.B.getTypeExpansionContext())
428-
->getParameters()),
428+
->getParameters().drop_back(numIgnoredTrailingParameters)),
429429
OrigFnType(origFnType)
430430
{}
431431

432-
unsigned getNumArgs() const { return ArgNo; }
432+
/// Emit the given list of parameters.
433+
unsigned emitParams(ParameterList *paramList, ParamDecl *selfParam) {
434+
if (paramList) {
435+
for (auto *param : *paramList)
436+
emitParam(param);
437+
}
438+
439+
// The self parameter follows the formal parameters in all of
440+
// our representations.
441+
if (selfParam) {
442+
emitParam(selfParam);
443+
}
444+
445+
finish();
446+
447+
return ArgNo;
448+
}
449+
450+
private:
451+
void finish() {
452+
parameters.finish();
453+
}
433454

434455
ManagedValue makeArgument(Type ty, bool isInOut, bool isNoImplicitCopy,
435456
LifetimeAnnotation lifetime, SILBasicBlock *parent,
@@ -845,13 +866,17 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
845866
bool throws,
846867
SourceLoc throwsLoc,
847868
Optional<AbstractionPattern> origClosureType) {
848-
uint16_t ArgNo = emitBasicProlog(paramList, selfParam, resultType,
849-
DC, throws, throwsLoc, origClosureType);
850-
851869
// Emit the capture argument variables. These are placed last because they
852870
// become the first curry level of the SIL function.
853871
assert(captureInfo.hasBeenComputed() &&
854872
"can't emit prolog of function with uncomputed captures");
873+
874+
uint16_t ArgNo = emitBasicProlog(paramList, selfParam, resultType,
875+
DC, throws, throwsLoc,
876+
/*ignored parameters*/
877+
captureInfo.getCaptures().size(),
878+
origClosureType);
879+
855880
for (auto capture : captureInfo.getCaptures()) {
856881
if (capture.isDynamicSelfMetadata()) {
857882
auto selfMetatype = MetatypeType::get(
@@ -1335,6 +1360,7 @@ uint16_t SILGenFunction::emitBasicProlog(ParameterList *paramList,
13351360
DeclContext *DC,
13361361
bool throws,
13371362
SourceLoc throwsLoc,
1363+
unsigned numIgnoredTrailingParameters,
13381364
Optional<AbstractionPattern> origClosureType) {
13391365
// Create the indirect result parameters.
13401366
auto genericSig = DC->getGenericSignatureOfContext();
@@ -1348,18 +1374,12 @@ uint16_t SILGenFunction::emitBasicProlog(ParameterList *paramList,
13481374
emitIndirectResultParameters(*this, resultType, origResultType, DC);
13491375

13501376
// Emit the argument variables in calling convention order.
1351-
ArgumentInitHelper emitter(*this, F, origClosureType);
1352-
1353-
// Add the SILArguments and use them to initialize the local argument
1354-
// values.
1355-
if (paramList)
1356-
for (auto *param : *paramList)
1357-
emitter.emitParam(param);
1358-
if (selfParam)
1359-
emitter.emitParam(selfParam);
1377+
unsigned ArgNo =
1378+
ArgumentInitHelper(*this, F, origClosureType,
1379+
numIgnoredTrailingParameters)
1380+
.emitParams(paramList, selfParam);
13601381

13611382
// Record the ArgNo of the artificial $error inout argument.
1362-
unsigned ArgNo = emitter.getNumArgs();
13631383
if (throws) {
13641384
auto NativeErrorTy = SILType::getExceptionType(getASTContext());
13651385
ManagedValue Undef = emitUndef(NativeErrorTy);

0 commit comments

Comments
 (0)