Skip to content

Commit bd80342

Browse files
committed
[SILGen] Record local auxiliary decls when a parameter is emitted, and
emit those auxiliary decls inside the function body brace statement. This generalizes the old code to work for parameters to any kind of function (e.g. initializers).
1 parent fb0121e commit bd80342

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -521,27 +521,7 @@ void SILGenFunction::emitFunction(FuncDecl *fd) {
521521
emitDistributedActorFactory(fd);
522522
} else {
523523
// Emit the actual function body as usual
524-
if (llvm::any_of(
525-
*fd->getParameters(),
526-
[](ParamDecl *p){ return p->hasAttachedPropertyWrapper(); })) {
527-
// If any parameters have property wrappers, emit the local auxiliary
528-
// variables before emitting the function body.
529-
LexicalScope BraceScope(*this, CleanupLocation(fd));
530-
for (auto *param : *fd->getParameters()) {
531-
param->visitAuxiliaryDecls([&](VarDecl *auxiliaryVar) {
532-
SILLocation WrapperLoc(auxiliaryVar);
533-
WrapperLoc.markAsPrologue();
534-
if (auto *patternBinding = auxiliaryVar->getParentPatternBinding())
535-
visitPatternBindingDecl(patternBinding);
536-
537-
visit(auxiliaryVar);
538-
});
539-
}
540-
541-
emitStmt(fd->getTypecheckedBody());
542-
} else {
543-
emitStmt(fd->getTypecheckedBody());
544-
}
524+
emitStmt(fd->getTypecheckedBody());
545525
}
546526

547527
emitEpilog(fd);
@@ -559,11 +539,6 @@ void SILGenFunction::emitClosure(AbstractClosureExpr *ace) {
559539
emitProlog(captureInfo, ace->getParameters(), /*selfParam=*/nullptr,
560540
ace, resultIfaceTy, ace->isBodyThrowing(), ace->getLoc());
561541
prepareEpilog(true, ace->isBodyThrowing(), CleanupLocation(ace));
562-
for (auto *param : *ace->getParameters()) {
563-
param->visitAuxiliaryDecls([&](VarDecl *auxiliaryVar) {
564-
visit(auxiliaryVar);
565-
});
566-
}
567542

568543
if (auto *ce = dyn_cast<ClosureExpr>(ace)) {
569544
emitStmt(ce->getBody());

lib/SILGen/SILGenFunction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
376376
/// a local variable.
377377
llvm::DenseMap<ValueDecl*, VarLoc> VarLocs;
378378

379+
/// The local auxiliary declarations for the parameters of this function that
380+
/// need to be emitted inside the next brace statement.
381+
llvm::SmallVector<VarDecl *, 2> LocalAuxiliaryDecls;
382+
379383
// Context information for tracking an `async let` child task.
380384
struct AsyncLetChildTask {
381385
SILValue asyncLet; // RawPointer to the async let state

lib/SILGen/SILGenProlog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ struct ArgumentInitHelper {
244244
}
245245

246246
void emitParam(ParamDecl *PD) {
247+
PD->visitAuxiliaryDecls([&](VarDecl *localVar) {
248+
SGF.LocalAuxiliaryDecls.push_back(localVar);
249+
});
250+
247251
if (PD->hasExternalPropertyWrapper()) {
248252
PD = cast<ParamDecl>(PD->getPropertyWrapperBackingProperty());
249253
}

lib/SILGen/SILGenStmt.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,19 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
282282
const unsigned ThrowStmtType = 3;
283283
const unsigned UnknownStmtType = 4;
284284
unsigned StmtType = UnknownStmtType;
285-
285+
286+
// Emit local auxiliary declarations.
287+
if (!SGF.LocalAuxiliaryDecls.empty()) {
288+
for (auto *var : SGF.LocalAuxiliaryDecls) {
289+
if (auto *patternBinding = var->getParentPatternBinding())
290+
SGF.visit(patternBinding);
291+
292+
SGF.visit(var);
293+
}
294+
295+
SGF.LocalAuxiliaryDecls.clear();
296+
}
297+
286298
for (auto &ESD : S->getElements()) {
287299

288300
if (auto D = ESD.dyn_cast<Decl*>())

0 commit comments

Comments
 (0)