Skip to content

Commit 2b1c9f3

Browse files
author
Zak Kent
committed
[SILGen] Move all top-level emission code to SILGenTopLevel.cpp
1 parent 90120e2 commit 2b1c9f3

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
@@ -1927,171 +1927,6 @@ void SILGenModule::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
19271927
// Nothing to do for #error/#warning; they've already been emitted.
19281928
}
19291929

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

20971932
// 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)