Skip to content

Commit c36ddc2

Browse files
authored
Merge pull request #61685 from tshortli/refactor-tbdgen-visitor
TBDGen: Refactor symbol enumeration into SIL and IRGen utilities
2 parents 5a00b78 + 290dd6b commit c36ddc2

File tree

9 files changed

+1349
-888
lines changed

9 files changed

+1349
-888
lines changed

include/swift/IRGen/IRSymbolVisitor.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===--- IRSymbolVisitor.h - Symbol Visitor for IR --------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_IRGEN_IRSYMBOLVISITOR_H
14+
#define SWIFT_IRGEN_IRSYMBOLVISITOR_H
15+
16+
#include "swift/IRGen/Linking.h"
17+
18+
namespace swift {
19+
20+
class SILSymbolVisitorContext;
21+
22+
namespace irgen {
23+
24+
/// Context for symbol enumeration using `IRSymbolVisitor`.
25+
class IRSymbolVisitorContext {
26+
const UniversalLinkageInfo &LinkInfo;
27+
const SILSymbolVisitorContext &SILCtx;
28+
29+
public:
30+
IRSymbolVisitorContext(const UniversalLinkageInfo &LinkInfo,
31+
const SILSymbolVisitorContext &SILCtx)
32+
: LinkInfo{LinkInfo}, SILCtx{SILCtx} {}
33+
34+
const UniversalLinkageInfo &getLinkInfo() const { return LinkInfo; }
35+
const SILSymbolVisitorContext &getSILCtx() const { return SILCtx; }
36+
};
37+
38+
/// A visitor class which may be used to enumerate the entities representing
39+
/// linker symbols associated with a Swift declaration, file, or module. This
40+
/// class is a refinement of `SILSymbolVisitor` for the IRGen layer. Most of the
41+
/// enumerated linker symbols can be represented as either a `SILDeclRef` or a
42+
/// `LinkEntity`, but a few aren't supported by those abstractions and are
43+
/// handled ad-hoc. Additionally, there are some Obj-C entities (like methods)
44+
/// that don't actually have associated linker symbols but are enumerated for
45+
/// the convenience of clients using this utility for API surface discovery.
46+
class IRSymbolVisitor {
47+
public:
48+
virtual ~IRSymbolVisitor() {}
49+
50+
/// Enumerate the symbols associated with the given decl.
51+
void visit(Decl *D, const IRSymbolVisitorContext &ctx);
52+
53+
/// Enumerate the symbols associated with the given file.
54+
void visitFile(FileUnit *file, const IRSymbolVisitorContext &ctx);
55+
56+
/// Enumerate the symbols associated with the given modules.
57+
void visitModules(llvm::SmallVector<ModuleDecl *, 4> &modules,
58+
const IRSymbolVisitorContext &ctx);
59+
60+
/// Override to prepare for enumeration of the symbols for a specific decl.
61+
virtual void willVisitDecl(Decl *D) {}
62+
63+
/// Override to clean up after enumeration of the symbols for a specific decl.
64+
virtual void didVisitDecl(Decl *D) {}
65+
66+
virtual void addFunction(SILDeclRef declRef) {}
67+
virtual void addFunction(StringRef name, SILDeclRef declRef) {}
68+
virtual void addGlobalVar(VarDecl *VD) {}
69+
virtual void addLinkEntity(LinkEntity entity) {}
70+
virtual void addObjCInterface(ClassDecl *CD) {}
71+
virtual void addObjCMethod(AbstractFunctionDecl *AFD) {}
72+
virtual void addProtocolWitnessThunk(RootProtocolConformance *C,
73+
ValueDecl *requirementDecl) {}
74+
};
75+
76+
} // end namespace irgen
77+
} // end namespace swift
78+
79+
#endif

include/swift/SIL/SILSymbolVisitor.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//===--- SILSymbolVisitor.h - Symbol Visitor for SIL ------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SIL_SILSYMBOLVISITOR_H
14+
#define SWIFT_SIL_SILSYMBOLVISITOR_H
15+
16+
#include "swift/AST/Decl.h"
17+
#include "swift/AST/ProtocolAssociations.h"
18+
#include "swift/AST/ProtocolConformance.h"
19+
#include "swift/SIL/SILDeclRef.h"
20+
21+
namespace swift {
22+
23+
/// Options dictating which symbols are enumerated by `SILSymbolVisitor`.
24+
struct SILSymbolVisitorOptions {
25+
/// Whether to visit the members of declarations like structs, classes, and
26+
/// protocols.
27+
bool VisitMembers = true;
28+
29+
/// Whether to only visit declarations for which linker directive symbols
30+
/// are needed (e.g. decls with `@_originallyDefinedIn`.
31+
bool LinkerDirectivesOnly = false;
32+
33+
/// Whether to only visit symbols with public linkage.
34+
bool PublicSymbolsOnly = true;
35+
36+
/// Whether LLVM IR Virtual Function Elimination is enabled.
37+
bool VirtualFunctionElimination = false;
38+
39+
/// Whether LLVM IR Witness Method Elimination is enabled.
40+
bool WitnessMethodElimination = false;
41+
};
42+
43+
/// Context for `SILSymbolVisitor` symbol enumeration.
44+
class SILSymbolVisitorContext {
45+
ModuleDecl *Module;
46+
const SILSymbolVisitorOptions Opts;
47+
48+
public:
49+
SILSymbolVisitorContext(ModuleDecl *M, const SILSymbolVisitorOptions Opts)
50+
: Module{M}, Opts{Opts} {
51+
assert(M);
52+
};
53+
54+
ModuleDecl *getModule() const { return Module; }
55+
const SILSymbolVisitorOptions &getOpts() const { return Opts; }
56+
};
57+
58+
/// A visitor class which may be used to enumerate the entities representing
59+
/// linker symbols associated with a Swift declaration, file, or module. Some
60+
/// enumerated linker symbols can be represented as a `SILDeclRef`. The rest are
61+
/// enumerated ad-hoc. Additionally, there a some Obj-C entities (like methods)
62+
/// that don't actually have associated linker symbols but are enumerated for
63+
/// the convenience of clients using this utility for API surface discovery.
64+
class SILSymbolVisitor {
65+
public:
66+
virtual ~SILSymbolVisitor() {}
67+
68+
/// Enumerate the symbols associated with the given decl.
69+
void visitDecl(Decl *D, const SILSymbolVisitorContext &ctx);
70+
71+
/// Enumerate the symbols associated with the given file.
72+
void visitFile(FileUnit *file, const SILSymbolVisitorContext &ctx);
73+
74+
/// Enumerate the symbols associated with the given modules.
75+
void visitModules(llvm::SmallVector<ModuleDecl *, 4> &modules,
76+
const SILSymbolVisitorContext &ctx);
77+
78+
/// Override to prepare for enumeration of the symbols for a specific decl.
79+
virtual void willVisitDecl(Decl *D) {}
80+
81+
/// Override to clean up after enumeration of the symbols for a specific decl.
82+
virtual void didVisitDecl(Decl *D) {}
83+
84+
/// A classification for the dynamic dispatch metadata of a decl.
85+
enum class DynamicKind {
86+
/// May be replaced at runtime (e.g.`dynamic`).
87+
Replaceable,
88+
89+
/// May replace another decl at runtime (e.g. `@_dynamicReplacement(for:)`).
90+
Replacement,
91+
};
92+
93+
virtual void addAssociatedConformanceDescriptor(AssociatedConformance AC) {}
94+
virtual void addAssociatedTypeDescriptor(AssociatedTypeDecl *ATD) {}
95+
virtual void addAsyncFunctionPointer(SILDeclRef declRef) {}
96+
virtual void addBaseConformanceDescriptor(BaseConformance BC) {}
97+
virtual void addClassMetadataBaseOffset(ClassDecl *CD) {}
98+
virtual void addDispatchThunk(SILDeclRef declRef) {}
99+
virtual void addDynamicFunction(AbstractFunctionDecl *AFD,
100+
DynamicKind dynKind) {}
101+
virtual void addEnumCase(EnumElementDecl *EED) {}
102+
virtual void addFieldOffset(VarDecl *VD) {}
103+
virtual void addFunction(SILDeclRef declRef) {}
104+
virtual void addFunction(StringRef name, SILDeclRef declRef) {}
105+
virtual void addGlobalVar(VarDecl *VD) {}
106+
virtual void addMethodDescriptor(SILDeclRef declRef) {}
107+
virtual void addMethodLookupFunction(ClassDecl *CD) {}
108+
virtual void addNominalTypeDescriptor(NominalTypeDecl *NTD) {}
109+
virtual void addObjCClass(ClassDecl *CD) {}
110+
virtual void addObjCInterface(ClassDecl *CD) {}
111+
virtual void addObjCMetaclass(ClassDecl *CD) {}
112+
virtual void addObjCMethod(AbstractFunctionDecl *AFD) {}
113+
virtual void addObjCResilientClassStub(ClassDecl *CD) {}
114+
virtual void addOpaqueTypeDescriptor(OpaqueTypeDecl *OTD) {}
115+
virtual void addOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *OTD,
116+
DynamicKind dynKind) {}
117+
virtual void addPropertyDescriptor(AbstractStorageDecl *ASD) {}
118+
virtual void addProtocolConformanceDescriptor(RootProtocolConformance *C) {}
119+
virtual void addProtocolDescriptor(ProtocolDecl *PD) {}
120+
virtual void addProtocolRequirementsBaseDescriptor(ProtocolDecl *PD) {}
121+
virtual void addProtocolWitnessTable(RootProtocolConformance *C) {}
122+
virtual void addProtocolWitnessThunk(RootProtocolConformance *C,
123+
ValueDecl *requirementDecl) {}
124+
virtual void addSwiftMetaclassStub(ClassDecl *CD) {}
125+
virtual void addTypeMetadataAccessFunction(CanType T) {}
126+
virtual void addTypeMetadataAddress(CanType T) {}
127+
};
128+
129+
} // end namespace swift
130+
131+
#endif

lib/IRGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ add_swift_host_library(swiftIRGen STATIC
5050
IRGenSILPasses.cpp
5151
IRGenRequests.cpp
5252
IRGenSIL.cpp
53+
IRSymbolVisitor.cpp
5354
Linking.cpp
5455
LoadableByAddress.cpp
5556
LocalTypeData.cpp

0 commit comments

Comments
 (0)