Skip to content

Commit 9f6e4bf

Browse files
author
Zak Kent
committed
[SILGen] Move all top-level emission code to SILGenTopLevel.cpp
1 parent 57bc16c commit 9f6e4bf

File tree

5 files changed

+192
-192
lines changed

5 files changed

+192
-192
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 0 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,171 +1928,6 @@ void SILGenModule::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
19281928
// Nothing to do for #error/#warning; they've already been emitted.
19291929
}
19301930

1931-
void SILGenModule::emitEntryPoint(SourceFile *SF, SILFunction *TopLevel) {
1932-
1933-
auto EntryRef = SILDeclRef::getMainFileEntryPoint(SF);
1934-
bool isAsyncTopLevel = false;
1935-
if (SF->isAsyncContext()) {
1936-
isAsyncTopLevel = true;
1937-
auto asyncEntryRef = SILDeclRef::getAsyncMainFileEntryPoint(SF);
1938-
auto *asyncTopLevel = getFunction(asyncEntryRef, ForDefinition);
1939-
SILGenFunction(*this, *TopLevel, SF)
1940-
.emitAsyncMainThreadStart(asyncEntryRef);
1941-
TopLevel = asyncTopLevel;
1942-
EntryRef = asyncEntryRef;
1943-
}
1944-
1945-
TopLevel->createProfiler(EntryRef);
1946-
1947-
SILGenFunction TopLevelSGF(*this, *TopLevel, SF, true);
1948-
TopLevelSGF.MagicFunctionName = SwiftModule->getName();
1949-
auto moduleCleanupLoc = CleanupLocation::getModuleCleanupLocation();
1950-
1951-
TopLevelSGF.prepareEpilog(llvm::None, true, moduleCleanupLoc);
1952-
1953-
auto prologueLoc = RegularLocation::getModuleLocation();
1954-
prologueLoc.markAsPrologue();
1955-
if (SF->isAsyncContext()) {
1956-
// emitAsyncMainThreadStart will create argc and argv.
1957-
// Just set the main actor as the expected executor; we should
1958-
// already be running on it.
1959-
SILValue executor = TopLevelSGF.emitMainExecutor(prologueLoc);
1960-
TopLevelSGF.ExpectedExecutor = TopLevelSGF.B.createOptionalSome(
1961-
prologueLoc, executor, SILType::getOptionalType(executor->getType()));
1962-
} else {
1963-
// Create the argc and argv arguments.
1964-
auto entry = TopLevelSGF.B.getInsertionBB();
1965-
auto context = TopLevelSGF.getTypeExpansionContext();
1966-
auto paramTypeIter =
1967-
TopLevelSGF.F.getConventions().getParameterSILTypes(context).begin();
1968-
1969-
entry->createFunctionArgument(*paramTypeIter);
1970-
entry->createFunctionArgument(*std::next(paramTypeIter));
1971-
}
1972-
1973-
{
1974-
Scope S(TopLevelSGF.Cleanups, moduleCleanupLoc);
1975-
SILGenTopLevel(TopLevelSGF).visitSourceFile(SF);
1976-
}
1977-
1978-
// Unregister the top-level function emitter.
1979-
TopLevelSGF.stopEmittingTopLevelCode();
1980-
1981-
// Write out the epilog.
1982-
auto moduleLoc = RegularLocation::getModuleLocation();
1983-
moduleLoc.markAutoGenerated();
1984-
auto returnInfo = TopLevelSGF.emitEpilogBB(moduleLoc);
1985-
auto returnLoc = returnInfo.second;
1986-
returnLoc.markAutoGenerated();
1987-
1988-
SILFunction *exitFunc = nullptr;
1989-
1990-
SILType returnType;
1991-
if (isAsyncTopLevel) {
1992-
FuncDecl *exitFuncDecl = getExit();
1993-
assert(exitFuncDecl && "Failed to find exit function declaration");
1994-
exitFunc = getFunction(
1995-
SILDeclRef(exitFuncDecl, SILDeclRef::Kind::Func, /*isForeign*/ true),
1996-
NotForDefinition);
1997-
SILFunctionType &funcType =
1998-
*exitFunc->getLoweredType().getAs<SILFunctionType>();
1999-
returnType = SILType::getPrimitiveObjectType(
2000-
funcType.getParameters().front().getInterfaceType());
2001-
} else {
2002-
returnType = TopLevelSGF.F.getConventions().getSingleSILResultType(
2003-
TopLevelSGF.getTypeExpansionContext());
2004-
}
2005-
2006-
auto emitTopLevelReturnValue = [&](unsigned value) -> SILValue {
2007-
// Create an integer literal for the value.
2008-
auto litType = SILType::getBuiltinIntegerType(32, getASTContext());
2009-
SILValue retValue =
2010-
TopLevelSGF.B.createIntegerLiteral(moduleLoc, litType, value);
2011-
2012-
// Wrap that in a struct if necessary.
2013-
if (litType != returnType) {
2014-
retValue = TopLevelSGF.B.createStruct(moduleLoc, returnType, retValue);
2015-
}
2016-
return retValue;
2017-
};
2018-
2019-
// Fallthrough should signal a normal exit by returning 0.
2020-
SILValue returnValue;
2021-
if (TopLevelSGF.B.hasValidInsertionPoint())
2022-
returnValue = emitTopLevelReturnValue(0);
2023-
2024-
// Handle the implicit rethrow block.
2025-
auto rethrowBB = TopLevelSGF.ThrowDest.getBlock();
2026-
TopLevelSGF.ThrowDest = JumpDest::invalid();
2027-
2028-
// If the rethrow block wasn't actually used, just remove it.
2029-
if (rethrowBB->pred_empty()) {
2030-
TopLevelSGF.eraseBasicBlock(rethrowBB);
2031-
2032-
// Otherwise, we need to produce a unified return block.
2033-
} else {
2034-
auto returnBB = TopLevelSGF.createBasicBlock();
2035-
if (TopLevelSGF.B.hasValidInsertionPoint())
2036-
TopLevelSGF.B.createBranch(returnLoc, returnBB, returnValue);
2037-
returnValue = returnBB->createPhiArgument(returnType, OwnershipKind::Owned);
2038-
TopLevelSGF.B.emitBlock(returnBB);
2039-
2040-
// Emit the rethrow block.
2041-
SILGenSavedInsertionPoint savedIP(TopLevelSGF, rethrowBB,
2042-
FunctionSection::Postmatter);
2043-
2044-
// Log the error.
2045-
SILValue error = rethrowBB->getArgument(0);
2046-
TopLevelSGF.B.createBuiltin(moduleLoc,
2047-
getASTContext().getIdentifier("errorInMain"),
2048-
Types.getEmptyTupleType(), {}, {error});
2049-
2050-
// Then end the lifetime of the error.
2051-
//
2052-
// We do this to appease the ownership verifier. We do not care about
2053-
// actually destroying the value since we are going to immediately exit,
2054-
// so this saves us a slight bit of code-size since end_lifetime is
2055-
// stripped out after ownership is removed.
2056-
TopLevelSGF.B.createEndLifetime(moduleLoc, error);
2057-
2058-
// Signal an abnormal exit by returning 1.
2059-
TopLevelSGF.Cleanups.emitCleanupsForReturn(CleanupLocation(moduleLoc),
2060-
IsForUnwind);
2061-
TopLevelSGF.B.createBranch(returnLoc, returnBB, emitTopLevelReturnValue(1));
2062-
}
2063-
2064-
// Return.
2065-
if (TopLevelSGF.B.hasValidInsertionPoint()) {
2066-
2067-
if (isAsyncTopLevel) {
2068-
SILValue exitCall = TopLevelSGF.B.createFunctionRef(moduleLoc, exitFunc);
2069-
TopLevelSGF.B.createApply(moduleLoc, exitCall, {}, {returnValue});
2070-
TopLevelSGF.B.createUnreachable(moduleLoc);
2071-
} else {
2072-
TopLevelSGF.B.createReturn(returnLoc, returnValue);
2073-
}
2074-
}
2075-
2076-
// Okay, we're done emitting the top-level function; destroy the
2077-
// emitter and verify the result.
2078-
SILFunction &toplevel = TopLevelSGF.getFunction();
2079-
2080-
LLVM_DEBUG(llvm::dbgs() << "lowered toplevel sil:\n";
2081-
toplevel.print(llvm::dbgs()));
2082-
toplevel.verifyIncompleteOSSA();
2083-
emitLazyConformancesForFunction(&toplevel);
2084-
}
2085-
2086-
void SILGenModule::emitEntryPoint(SourceFile *SF) {
2087-
assert(!M.lookUpFunction(getASTContext().getEntryPointFunctionName()) &&
2088-
"already emitted toplevel?!");
2089-
2090-
auto mainEntryRef = SILDeclRef::getMainFileEntryPoint(SF);
2091-
SILFunction *TopLevel = getFunction(mainEntryRef, ForDefinition);
2092-
TopLevel->setBare(IsBare);
2093-
emitEntryPoint(SF, TopLevel);
2094-
}
2095-
20961931
namespace {
20971932

20981933
// An RAII object that constructs a \c SILGenModule instance.

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,6 @@ SILGenFunction::emitGlobalVariableRef(SILLocation loc, VarDecl *var,
133133
return ManagedValue::forLValue(addr);
134134
}
135135

136-
void SILGenFunction::emitMarkFunctionEscapeForTopLevelCodeGlobals(
137-
SILLocation Loc, CaptureInfo CaptureInfo) {
138-
139-
llvm::SmallVector<SILValue, 4> Captures;
140-
141-
for (auto Capture : CaptureInfo.getCaptures()) {
142-
// Decls captured by value don't escape.
143-
auto It = VarLocs.find(Capture.getDecl());
144-
if (It == VarLocs.end() || !It->getSecond().value->getType().isAddress())
145-
continue;
146-
147-
Captures.push_back(It->second.value);
148-
}
149-
150-
if (!Captures.empty())
151-
B.createMarkFunctionEscape(Loc, Captures);
152-
}
153-
154136
//===----------------------------------------------------------------------===//
155137
// Global initialization
156138
//===----------------------------------------------------------------------===//

lib/SILGen/SILGenStmt.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -907,11 +907,10 @@ void StmtEmitter::visitDeferStmt(DeferStmt *S) {
907907
// If the defer is at the top-level code, insert 'mark_escape_inst'
908908
// to the top-level code to check initialization of any captured globals.
909909
FuncDecl *deferDecl = S->getTempDecl();
910-
auto declCtxKind = deferDecl->getDeclContext()->getContextKind();
911-
if (declCtxKind == DeclContextKind::TopLevelCodeDecl &&
912-
SGF.isEmittingTopLevelCode()) {
913-
SGF.emitMarkFunctionEscapeForTopLevelCodeGlobals(
914-
S, deferDecl->getCaptureInfo());
910+
auto *Ctx = deferDecl->getDeclContext();
911+
if (isa<TopLevelCodeDecl>(Ctx) && SGF.isEmittingTopLevelCode()) {
912+
auto Captures = deferDecl->getCaptureInfo();
913+
SGF.emitMarkFunctionEscapeForTopLevelCodeGlobals(S, std::move(Captures));
915914
}
916915
SGF.visitFuncDecl(deferDecl);
917916

0 commit comments

Comments
 (0)