Skip to content

Commit 229398c

Browse files
authored
Merge pull request #69141 from tshortli/silgen-lazy-typecheck
SILGen: Introduce option to skip non-exportable declarations
2 parents 0ada2e2 + b8bddb5 commit 229398c

File tree

14 files changed

+129
-27
lines changed

14 files changed

+129
-27
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
957957
unsigned getAttachedMacroDiscriminator(DeclBaseName macroName, MacroRole role,
958958
const CustomAttr *attr) const;
959959

960+
/// Determines if this declaration is exposed to clients of the module it is
961+
/// defined in. For example, `public` declarations are exposed to clients.
962+
bool isExposedToClients() const;
963+
960964
/// Returns the innermost enclosing decl with an availability annotation.
961965
const Decl *getInnermostDeclWithAvailability() const;
962966

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,15 +877,6 @@ class IterableDeclContext {
877877
/// The resulting list of members will be stable across translation units.
878878
ArrayRef<Decl *> getABIMembers() const;
879879

880-
using DeclsForLowering =
881-
OptionalTransformRange<ArrayRef<Decl *>,
882-
AvailableDuringLoweringDeclFilter<Decl>>;
883-
884-
/// Get all of the members within this context that should be included when
885-
/// lowering to SIL/IR, including any implicitly-synthesized members. The
886-
/// decls returned by \c getABIMembers() are a superset of these decls.
887-
DeclsForLowering getMembersForLowering() const;
888-
889880
/// Get all of the members within this context, including any
890881
/// implicitly-synthesized members.
891882
///

include/swift/SIL/SILVTableVisitor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ template <class T> class SILVTableVisitor {
105105
}
106106

107107
void maybeAddMember(Decl *member) {
108+
if (!member->isAvailableDuringLowering())
109+
return;
110+
108111
if (isa<AccessorDecl>(member))
109112
/* handled as part of its storage */;
110113
else if (auto *fd = dyn_cast<FuncDecl>(member))
@@ -141,7 +144,7 @@ template <class T> class SILVTableVisitor {
141144
if (!theClass->hasKnownSwiftImplementation())
142145
return;
143146

144-
for (Decl *member : theClass->getMembersForLowering()) {
147+
for (Decl *member : theClass->getABIMembers()) {
145148
maybeAddMember(member);
146149
}
147150
}

lib/AST/Decl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/AccessScope.h"
2424
#include "swift/AST/Attr.h"
2525
#include "swift/AST/CaptureInfo.h"
26+
#include "swift/AST/DeclExportabilityVisitor.h"
2627
#include "swift/AST/DiagnosticEngine.h"
2728
#include "swift/AST/DiagnosticsSema.h"
2829
#include "swift/AST/ExistentialLayout.h"
@@ -492,6 +493,10 @@ unsigned Decl::getAttachedMacroDiscriminator(DeclBaseName macroName,
492493
macroName);
493494
}
494495

496+
bool Decl::isExposedToClients() const {
497+
return DeclExportabilityVisitor().visit(this);
498+
}
499+
495500
const Decl *Decl::getInnermostDeclWithAvailability() const {
496501
if (getAttrs().hasAttribute<AvailableAttr>())
497502
return this;

lib/AST/DeclContext.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -914,12 +914,6 @@ ArrayRef<Decl *> IterableDeclContext::getABIMembers() const {
914914
ArrayRef<Decl *>());
915915
}
916916

917-
IterableDeclContext::DeclsForLowering
918-
IterableDeclContext::getMembersForLowering() const {
919-
return DeclsForLowering(getABIMembers(),
920-
AvailableDuringLoweringDeclFilter<Decl>());
921-
}
922-
923917
ArrayRef<Decl *> IterableDeclContext::getAllMembers() const {
924918
ASTContext &ctx = getASTContext();
925919
return evaluateOrDefault(

lib/Frontend/Frontend.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,11 +1425,6 @@ bool CompilerInstance::performParseAndResolveImportsOnly() {
14251425
void CompilerInstance::performSema() {
14261426
performParseAndResolveImportsOnly();
14271427

1428-
// Skip eager type checking. Instead, let later stages of compilation drive
1429-
// type checking as needed through request evaluation.
1430-
if (getASTContext().TypeCheckerOpts.EnableLazyTypecheck)
1431-
return;
1432-
14331428
FrontendStatsTracer tracer(getStatsReporter(), "perform-sema");
14341429

14351430
forEachFileToTypeCheck([&](SourceFile &SF) {

lib/SILGen/SILGen.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,19 @@ bool SILGenModule::hasFunction(SILDeclRef constant) {
797797
return emittedFunctions.count(constant);
798798
}
799799

800-
void SILGenModule::visit(Decl *D) {
800+
bool SILGenModule::shouldSkipDecl(Decl *D) {
801801
if (!D->isAvailableDuringLowering())
802+
return true;
803+
804+
if (getASTContext().SILOpts.SkipNonExportableDecls &&
805+
!D->isExposedToClients())
806+
return true;
807+
808+
return false;
809+
}
810+
811+
void SILGenModule::visit(Decl *D) {
812+
if (shouldSkipDecl(D))
802813
return;
803814

804815
ASTVisitor::visit(D);
@@ -1412,6 +1423,8 @@ void SILGenModule::emitAbstractFuncDecl(AbstractFunctionDecl *AFD) {
14121423
}
14131424

14141425
void SILGenModule::emitFunction(FuncDecl *fd) {
1426+
assert(!shouldSkipDecl(fd));
1427+
14151428
Types.setCaptureTypeExpansionContext(SILDeclRef(fd), M);
14161429

14171430
SILDeclRef::Loc decl = fd;

lib/SILGen/SILGen.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
264264
// Visitors for top-level forms
265265
//===--------------------------------------------------------------------===//
266266

267+
/// Returns true if SILGen should be skipped for the given decl.
268+
bool shouldSkipDecl(Decl *d);
269+
267270
void visit(Decl *D);
268271

269272
// These are either not allowed at global scope or don't require

lib/SILGen/SILGenTopLevel.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,18 @@ void SILGenTopLevel::visitTopLevelCodeDecl(TopLevelCodeDecl *TD) {
294294
SILGenTopLevel::TypeVisitor::TypeVisitor(SILGenFunction &SGF) : SGF(SGF) {}
295295

296296
void SILGenTopLevel::TypeVisitor::emit(IterableDeclContext *Ctx) {
297-
for (auto *Member : Ctx->getMembersForLowering()) {
297+
for (auto *Member : Ctx->getABIMembers()) {
298298
visit(Member);
299299
}
300300
}
301301

302+
void SILGenTopLevel::TypeVisitor::visit(Decl *D) {
303+
if (SGF.SGM.shouldSkipDecl(D))
304+
return;
305+
306+
TypeMemberVisitor::visit(D);
307+
}
308+
302309
void SILGenTopLevel::TypeVisitor::visitPatternBindingDecl(
303310
PatternBindingDecl *PD) {
304311
for (auto i : range(PD->getNumPatternEntries())) {

lib/SILGen/SILGenTopLevel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SILGenTopLevel : public ASTVisitor<SILGenTopLevel> {
4949
/// Emit `mark_function_escape` SIL instructions into `SGF` for encountered
5050
/// escape points.
5151
TypeVisitor(SILGenFunction &SGF);
52+
void visit(Decl *D);
5253
void visitDecl(Decl *D) {}
5354
void emit(IterableDeclContext *Ctx);
5455
virtual void visitPatternBindingDecl(PatternBindingDecl *PD);

0 commit comments

Comments
 (0)