Skip to content

Commit 81d455d

Browse files
authored
Merge pull request swiftlang#33107 from hamishknight/tbd-two
2 parents d693ef0 + 3ae136c commit 81d455d

File tree

6 files changed

+170
-98
lines changed

6 files changed

+170
-98
lines changed

include/swift/AST/TBDGenRequests.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "swift/AST/SimpleRequest.h"
2222

2323
namespace llvm {
24+
25+
class DataLayout;
26+
class Triple;
27+
2428
namespace MachO {
2529
class InterfaceFile;
2630
} // end namespace MachO
@@ -59,6 +63,9 @@ class TBDGenDescriptor final {
5963
/// Returns the TBDGen options.
6064
const TBDGenOptions &getOptions() const { return Opts; }
6165

66+
const llvm::DataLayout &getDataLayout() const;
67+
const llvm::Triple &getTarget() const;
68+
6269
bool operator==(const TBDGenDescriptor &other) const;
6370
bool operator!=(const TBDGenDescriptor &other) const {
6471
return !(*this == other);
@@ -77,13 +84,27 @@ llvm::hash_code hash_value(const TBDGenDescriptor &desc);
7784
void simple_display(llvm::raw_ostream &out, const TBDGenDescriptor &desc);
7885
SourceLoc extractNearestSourceLoc(const TBDGenDescriptor &desc);
7986

80-
using TBDFileAndSymbols =
81-
std::pair<llvm::MachO::InterfaceFile, std::vector<std::string>>;
87+
using TBDFile = llvm::MachO::InterfaceFile;
8288

83-
/// Computes the TBD file and public symbols for a given module or file.
89+
/// Computes the TBD file for a given Swift module or file.
8490
class GenerateTBDRequest
8591
: public SimpleRequest<GenerateTBDRequest,
86-
TBDFileAndSymbols(TBDGenDescriptor),
92+
TBDFile(TBDGenDescriptor),
93+
RequestFlags::Uncached> {
94+
public:
95+
using SimpleRequest::SimpleRequest;
96+
97+
private:
98+
friend SimpleRequest;
99+
100+
// Evaluation.
101+
TBDFile evaluate(Evaluator &evaluator, TBDGenDescriptor desc) const;
102+
};
103+
104+
/// Retrieve the public symbols for a file or module.
105+
class PublicSymbolsRequest
106+
: public SimpleRequest<PublicSymbolsRequest,
107+
std::vector<std::string>(TBDGenDescriptor),
87108
RequestFlags::Uncached> {
88109
public:
89110
using SimpleRequest::SimpleRequest;
@@ -92,7 +113,8 @@ class GenerateTBDRequest
92113
friend SimpleRequest;
93114

94115
// Evaluation.
95-
TBDFileAndSymbols evaluate(Evaluator &evaluator, TBDGenDescriptor desc) const;
116+
std::vector<std::string>
117+
evaluate(Evaluator &evaluator, TBDGenDescriptor desc) const;
96118
};
97119

98120
/// Report that a request of the given kind is being evaluated, so it

include/swift/AST/TBDGenTypeIDZone.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
SWIFT_REQUEST(TBDGen, GenerateTBDRequest, TBDFileAndSymbols(TBDGenDescriptor),
17+
SWIFT_REQUEST(TBDGen, GenerateTBDRequest, TBDFile(TBDGenDescriptor),
18+
Uncached, NoLocationInfo)
19+
SWIFT_REQUEST(TBDGen, PublicSymbolsRequest,
20+
std::vector<std::string>(TBDGenDescriptor),
1821
Uncached, NoLocationInfo)

lib/FrontendTool/FrontendTool.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,10 @@ static bool generateCode(CompilerInstance &Instance, StringRef OutputFilename,
19761976
// Free up some compiler resources now that we have an IRModule.
19771977
freeASTContextIfPossible(Instance);
19781978

1979+
// If we emitted any errors while perfoming the end-of-pipeline actions, bail.
1980+
if (Instance.getDiags().hadAnyError())
1981+
return true;
1982+
19791983
// Now that we have a single IR Module, hand it over to performLLVM.
19801984
return performLLVM(opts, Instance.getDiags(), nullptr, HashGlobal, IRModule,
19811985
TargetMachine.get(), OutputFilename,
@@ -2097,21 +2101,17 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
20972101
IRGenOpts, Invocation.getTBDGenOptions(), std::move(SM), PSPs,
20982102
OutputFilename, MSF, HashGlobal, ParallelOutputFilenames);
20992103

2100-
// Just because we had an AST error it doesn't mean we can't performLLVM.
2101-
bool HadError = Instance.getASTContext().hadError();
2102-
2103-
// If the AST Context has no errors but no IRModule is available,
2104-
// parallelIRGen happened correctly, since parallel IRGen produces multiple
2105-
// modules.
2104+
// If no IRModule is available, bail. This can either happen if IR generation
2105+
// fails, or if parallelIRGen happened correctly (in which case it would have
2106+
// already performed LLVM).
21062107
if (!IRModule)
2107-
return HadError;
2108+
return Instance.getDiags().hadAnyError();
21082109

21092110
if (validateTBDIfNeeded(Invocation, MSF, *IRModule.getModule()))
21102111
return true;
21112112

21122113
return generateCode(Instance, OutputFilename, IRModule.getModule(),
2113-
HashGlobal) ||
2114-
HadError;
2114+
HashGlobal);
21152115
}
21162116

21172117
static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,

lib/TBDGen/TBDGen.cpp

Lines changed: 91 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,27 @@ static bool isGlobalOrStaticVar(VarDecl *VD) {
6161
return VD->isStatic() || VD->getDeclContext()->isModuleScopeContext();
6262
}
6363

64+
TBDGenVisitor::TBDGenVisitor(TBDGenDescriptor desc,
65+
SymbolCallbackFn symbolCallback)
66+
: TBDGenVisitor(desc.getTarget(), desc.getDataLayout(),
67+
desc.getParentModule(), desc.getOptions(),
68+
symbolCallback) {}
69+
6470
void TBDGenVisitor::addSymbolInternal(StringRef name,
6571
llvm::MachO::SymbolKind kind,
6672
bool isLinkerDirective) {
6773
if (!isLinkerDirective && Opts.LinkerDirectivesOnly)
6874
return;
69-
Symbols.addSymbol(kind, name, Targets);
70-
if (kind == SymbolKind::GlobalSymbol) {
71-
StringSymbols.push_back(name);
75+
7276
#ifndef NDEBUG
77+
if (kind == SymbolKind::GlobalSymbol) {
7378
if (!DuplicateSymbolChecker.insert(name).second) {
7479
llvm::dbgs() << "TBDGen duplicate symbol: " << name << '\n';
7580
assert(false && "TBDGen symbol appears twice");
7681
}
77-
#endif
7882
}
83+
#endif
84+
SymbolCallback(name, kind);
7985
}
8086

8187
static std::vector<OriginallyDefinedInAttr::ActiveVersion>
@@ -1053,6 +1059,63 @@ void TBDGenVisitor::visit(Decl *D) {
10531059
ASTVisitor::visit(D);
10541060
}
10551061

1062+
static bool hasLinkerDirective(Decl *D) {
1063+
return !getAllMovedPlatformVersions(D).empty();
1064+
}
1065+
1066+
void TBDGenVisitor::visitFile(FileUnit *file) {
1067+
if (file == SwiftModule->getFiles()[0])
1068+
addFirstFileSymbols();
1069+
1070+
SmallVector<Decl *, 16> decls;
1071+
file->getTopLevelDecls(decls);
1072+
1073+
addMainIfNecessary(file);
1074+
1075+
for (auto d : decls) {
1076+
if (Opts.LinkerDirectivesOnly && !hasLinkerDirective(d))
1077+
continue;
1078+
visit(d);
1079+
}
1080+
}
1081+
1082+
void TBDGenVisitor::visit(const TBDGenDescriptor &desc) {
1083+
if (auto *singleFile = desc.getSingleFile()) {
1084+
assert(SwiftModule == singleFile->getParentModule() &&
1085+
"mismatched file and module");
1086+
visitFile(singleFile);
1087+
1088+
// Visit synthesized file, if it exists.
1089+
if (auto *SF = dyn_cast<SourceFile>(singleFile)) {
1090+
if (auto *synthesizedFile = SF->getSynthesizedFile())
1091+
visitFile(synthesizedFile);
1092+
}
1093+
return;
1094+
}
1095+
1096+
llvm::SmallVector<ModuleDecl*, 4> Modules;
1097+
Modules.push_back(SwiftModule);
1098+
1099+
auto &ctx = SwiftModule->getASTContext();
1100+
for (auto Name: Opts.embedSymbolsFromModules) {
1101+
if (auto *MD = ctx.getModuleByName(Name)) {
1102+
// If it is a clang module, the symbols should be collected by TAPI.
1103+
if (!MD->isNonSwiftModule()) {
1104+
Modules.push_back(MD);
1105+
continue;
1106+
}
1107+
}
1108+
// Diagnose module name that cannot be found
1109+
ctx.Diags.diagnose(SourceLoc(), diag::unknown_swift_module_name, Name);
1110+
}
1111+
// Collect symbols in each module.
1112+
llvm::for_each(Modules, [&](ModuleDecl *M) {
1113+
for (auto *file : M->getFiles()) {
1114+
visitFile(file);
1115+
}
1116+
});
1117+
}
1118+
10561119
/// The kind of version being parsed, used for diagnostics.
10571120
/// Note: Must match the order in DiagnosticsFrontend.def
10581121
enum DylibVersionKind_t: unsigned {
@@ -1100,25 +1163,15 @@ static bool isApplicationExtensionSafe(const LangOptions &LangOpts) {
11001163
llvm::sys::Process::GetEnv("LD_APPLICATION_EXTENSION_SAFE");
11011164
}
11021165

1103-
static bool hasLinkerDirective(Decl *D) {
1104-
return !getAllMovedPlatformVersions(D).empty();
1105-
}
1106-
1107-
TBDFileAndSymbols
1108-
GenerateTBDRequest::evaluate(Evaluator &evaluator,
1109-
TBDGenDescriptor desc) const {
1166+
TBDFile GenerateTBDRequest::evaluate(Evaluator &evaluator,
1167+
TBDGenDescriptor desc) const {
11101168
auto *M = desc.getParentModule();
11111169
auto &opts = desc.getOptions();
1112-
11131170
auto &ctx = M->getASTContext();
1114-
const auto &triple = ctx.LangOpts.Target;
1115-
UniversalLinkageInfo linkInfo(triple, opts.HasMultipleIGMs,
1116-
/*forcePublicDecls*/ false);
11171171

11181172
llvm::MachO::InterfaceFile file;
11191173
file.setFileType(llvm::MachO::FileType::TBD_V4);
1120-
file.setApplicationExtensionSafe(
1121-
isApplicationExtensionSafe(M->getASTContext().LangOpts));
1174+
file.setApplicationExtensionSafe(isApplicationExtensionSafe(ctx.LangOpts));
11221175
file.setInstallName(opts.InstallName);
11231176
file.setTwoLevelNamespace();
11241177
file.setSwiftABIVersion(irgen::getSwiftABIVersion());
@@ -1134,75 +1187,47 @@ GenerateTBDRequest::evaluate(Evaluator &evaluator,
11341187
file.setCompatibilityVersion(*packed);
11351188
}
11361189

1137-
llvm::MachO::Target target(triple);
1190+
llvm::MachO::Target target(ctx.LangOpts.Target);
11381191
file.addTarget(target);
11391192
// Add target variant
11401193
if (ctx.LangOpts.TargetVariant.hasValue()) {
11411194
llvm::MachO::Target targetVar(*ctx.LangOpts.TargetVariant);
11421195
file.addTarget(targetVar);
11431196
}
1144-
auto *clang = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
1145-
TBDGenVisitor visitor(file, {target}, clang->getTargetInfo().getDataLayout(),
1146-
linkInfo, M, opts);
11471197

1148-
auto visitFile = [&](FileUnit *file) {
1149-
if (file == M->getFiles()[0]) {
1150-
visitor.addFirstFileSymbols();
1151-
}
1152-
1153-
SmallVector<Decl *, 16> decls;
1154-
file->getTopLevelDecls(decls);
1198+
llvm::MachO::TargetList targets{target};
1199+
auto addSymbol = [&](StringRef symbol, SymbolKind kind) {
1200+
file.addSymbol(kind, symbol, targets);
1201+
};
11551202

1156-
visitor.addMainIfNecessary(file);
1203+
TBDGenVisitor visitor(desc, addSymbol);
1204+
visitor.visit(desc);
1205+
return file;
1206+
}
11571207

1158-
for (auto d : decls) {
1159-
if (opts.LinkerDirectivesOnly && !hasLinkerDirective(d))
1160-
continue;
1161-
visitor.visit(d);
1162-
}
1208+
std::vector<std::string>
1209+
PublicSymbolsRequest::evaluate(Evaluator &evaluator,
1210+
TBDGenDescriptor desc) const {
1211+
std::vector<std::string> symbols;
1212+
auto addSymbol = [&](StringRef symbol, SymbolKind kind) {
1213+
if (kind == SymbolKind::GlobalSymbol)
1214+
symbols.push_back(symbol.str());
11631215
};
11641216

1165-
if (auto *singleFile = desc.getSingleFile()) {
1166-
assert(M == singleFile->getParentModule() && "mismatched file and module");
1167-
visitFile(singleFile);
1168-
// Visit synthesized file, if it exists.
1169-
if (auto *SF = dyn_cast<SourceFile>(singleFile))
1170-
if (auto *synthesizedFile = SF->getSynthesizedFile())
1171-
visitFile(synthesizedFile);
1172-
} else {
1173-
llvm::SmallVector<ModuleDecl*, 4> Modules;
1174-
Modules.push_back(M);
1175-
for (auto Name: opts.embedSymbolsFromModules) {
1176-
if (auto *MD = ctx.getModuleByName(Name)) {
1177-
// If it is a clang module, the symbols should be collected by TAPI.
1178-
if (!MD->isNonSwiftModule()) {
1179-
Modules.push_back(MD);
1180-
continue;
1181-
}
1182-
}
1183-
// Diagnose module name that cannot be found
1184-
ctx.Diags.diagnose(SourceLoc(), diag::unknown_swift_module_name, Name);
1185-
}
1186-
// Collect symbols in each module.
1187-
llvm::for_each(Modules, [&](ModuleDecl *M) {
1188-
for (auto *file : M->getFiles()) {
1189-
visitFile(file);
1190-
}
1191-
});
1192-
}
1193-
1194-
return std::make_pair(std::move(file), std::move(visitor.StringSymbols));
1217+
TBDGenVisitor visitor(desc, addSymbol);
1218+
visitor.visit(desc);
1219+
return symbols;
11951220
}
11961221

11971222
std::vector<std::string> swift::getPublicSymbols(TBDGenDescriptor desc) {
11981223
auto &evaluator = desc.getParentModule()->getASTContext().evaluator;
1199-
return llvm::cantFail(evaluator(GenerateTBDRequest{desc})).second;
1224+
return llvm::cantFail(evaluator(PublicSymbolsRequest{desc}));
12001225
}
12011226
void swift::writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
12021227
const TBDGenOptions &opts) {
12031228
auto &evaluator = M->getASTContext().evaluator;
12041229
auto desc = TBDGenDescriptor::forModule(M, opts);
1205-
auto file = llvm::cantFail(evaluator(GenerateTBDRequest{desc})).first;
1230+
auto file = llvm::cantFail(evaluator(GenerateTBDRequest{desc}));
12061231
llvm::cantFail(llvm::MachO::TextAPIWriter::writeToStream(os, file),
12071232
"YAML writing should be error-free");
12081233
}

lib/TBDGen/TBDGenRequests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/AST/TBDGenRequests.h"
14+
#include "swift/AST/ASTContext.h"
1415
#include "swift/AST/Evaluator.h"
1516
#include "swift/AST/FileUnit.h"
1617
#include "swift/AST/Module.h"
18+
#include "swift/ClangImporter/ClangImporter.h"
1719
#include "swift/Subsystems.h"
1820
#include "swift/TBDGen/TBDGen.h"
21+
#include "clang/Basic/TargetInfo.h"
1922
#include "llvm/TextAPI/MachO/InterfaceFile.h"
2023

2124
using namespace swift;
@@ -43,6 +46,16 @@ ModuleDecl *TBDGenDescriptor::getParentModule() const {
4346
return Input.get<FileUnit *>()->getParentModule();
4447
}
4548

49+
const llvm::DataLayout &TBDGenDescriptor::getDataLayout() const {
50+
auto &ctx = getParentModule()->getASTContext();
51+
auto *clang = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
52+
return clang->getTargetInfo().getDataLayout();
53+
}
54+
55+
const llvm::Triple &TBDGenDescriptor::getTarget() const {
56+
return getParentModule()->getASTContext().LangOpts.Target;
57+
}
58+
4659
bool TBDGenDescriptor::operator==(const TBDGenDescriptor &other) const {
4760
return Input == other.Input && Opts == other.Opts;
4861
}

0 commit comments

Comments
 (0)