Skip to content

[silgen] Create a function level scope when open coding implicit value initializers. #32089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/SILGen/SILGenBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,13 @@ ReturnInst *SILGenBuilder::createReturn(SILLocation loc,
return createReturn(loc, returnValue.forward(SGF));
}

ReturnInst *
SILGenBuilder::createReturn(SILLocation loc, SILValue returnValue,
AssertingManualScope &&functionLevelScope) {
std::move(functionLevelScope).pop();
return createReturn(loc, returnValue);
}

ManagedValue SILGenBuilder::createTuple(SILLocation loc, SILType type,
ArrayRef<ManagedValue> elements) {
// Handle the empty tuple case.
Expand Down
4 changes: 4 additions & 0 deletions lib/SILGen/SILGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Lowering {

class SILGenFunction;
class SGFContext;
class AssertingManualScope;

/// A subclass of SILBuilder that wraps APIs to vend ManagedValues.
/// APIs only vend ManagedValues.
Expand Down Expand Up @@ -362,6 +363,9 @@ class SILGenBuilder : public SILBuilder {
using SILBuilder::createReturn;
ReturnInst *createReturn(SILLocation Loc, ManagedValue ReturnValue);

ReturnInst *createReturn(SILLocation Loc, SILValue ReturnValue,
AssertingManualScope &&functionLevelScope);

using SILBuilder::emitDestructureValueOperation;
/// Perform either a tuple or struct destructure and then pass its components
/// as managed value one by one with an index to the closure.
Expand Down
9 changes: 7 additions & 2 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
ConstructorDecl *ctor) {
RegularLocation Loc(ctor);
Loc.markAutoGenerated();

AssertingManualScope functionLevelScope(SGF.Cleanups,
CleanupLocation::get(Loc));

// FIXME: Handle 'self' along with the other arguments.
auto *paramList = ctor->getParameters();
auto *selfDecl = ctor->getImplicitSelfDecl();
Expand Down Expand Up @@ -238,8 +242,9 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
SGF.emitExprInto(field->getParentInitializer(), init.get());
}
}

SGF.B.createReturn(ImplicitReturnLocation::getImplicitReturnLoc(Loc),
SGF.emitEmptyTuple(Loc));
SGF.emitEmptyTuple(Loc), std::move(functionLevelScope));
return;
}

Expand Down Expand Up @@ -287,7 +292,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,

SILValue selfValue = SGF.B.createStruct(Loc, selfTy, eltValues);
SGF.B.createReturn(ImplicitReturnLocation::getImplicitReturnLoc(Loc),
selfValue);
selfValue, std::move(functionLevelScope));
return;
}

Expand Down
43 changes: 41 additions & 2 deletions lib/SILGen/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ class LLVM_LIBRARY_VISIBILITY Scope {
Scope(const Scope &other) = delete;
Scope &operator=(const Scope &other) = delete;

Scope(Scope &&other) = delete;
Scope &operator=(Scope &&other) = delete; // implementable if needed
Scope(Scope &&other)
: cleanups(other.cleanups), depth(other.depth),
savedInnermostScope(other.savedInnermostScope), loc(other.loc) {
// Invalidate other.
other.depth = CleanupsDepth::invalid();
}

Scope &operator=(Scope &&other) {
depth = other.depth;
savedInnermostScope = other.savedInnermostScope;
loc = other.loc;

// Invalidate other.
other.depth = CleanupsDepth::invalid();

return *this;
}

explicit Scope(SILGenFunction &SGF, SILLocation loc)
: Scope(SGF.Cleanups, CleanupLocation::get(loc)) {}
Expand Down Expand Up @@ -83,6 +98,30 @@ class LLVM_LIBRARY_VISIBILITY Scope {
void popImpl();
};

/// A scope that must be manually popped by the using code. If not
/// popped, the destructor asserts.
class LLVM_LIBRARY_VISIBILITY AssertingManualScope {
Scope scope;

public:
explicit AssertingManualScope(CleanupManager &cleanups, CleanupLocation loc)
: scope(cleanups, loc) {}

AssertingManualScope(AssertingManualScope &&other)
: scope(std::move(other.scope)) {}

AssertingManualScope &operator=(AssertingManualScope &&other) {
scope = std::move(other.scope);
return *this;
}

~AssertingManualScope() {
assert(!scope.isValid() && "Unpopped manual scope?!");
}

void pop() && { scope.pop(); }
};

/// A FullExpr is a RAII object recording that a full-expression has
/// been entered. A full-expression is essentially a very small scope
/// for the temporaries in an expression, with the added complexity
Expand Down