Skip to content

Commit e4e77fb

Browse files
committed
Serialization: Break the cycle between Serialization and SymbolGraphGen.
Push the top level logic for writing out swiftmodules and associated files into the frontend library which has access to all the necessary dependencies.
1 parent a252dae commit e4e77fb

File tree

8 files changed

+215
-151
lines changed

8 files changed

+215
-151
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- Serialization.h - Swiftmodule emission -----------------*- C++ -*-===//
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#ifndef SWIFT_SERIALIZATION_H
13+
#define SWIFT_SERIALIZATION_H
14+
15+
#include "swift/Subsystems.h"
16+
17+
namespace swift {
18+
19+
class SILModule;
20+
21+
namespace serialization {
22+
23+
/// Serialize a module to the given stream.
24+
void writeToStream(
25+
raw_ostream &os, ModuleOrSourceFile DC, const SILModule *M,
26+
const SerializationOptions &options,
27+
const fine_grained_dependencies::SourceFileDepGraph *DepGraph);
28+
29+
/// Serialize module documentation to the given stream.
30+
void writeDocToStream(raw_ostream &os, ModuleOrSourceFile DC,
31+
StringRef GroupInfoPath);
32+
33+
/// Serialize module source info to the given stream.
34+
void writeSourceInfoToStream(raw_ostream &os, ModuleOrSourceFile DC);
35+
36+
} // end namespace serialization
37+
} // end namespace swift
38+
39+
#endif // SWIFT_SERIALIZATION_H

lib/Frontend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_swift_host_library(swiftFrontend STATIC
1313
ModuleInterfaceLoader.cpp
1414
ModuleInterfaceSupport.cpp
1515
PrintingDiagnosticConsumer.cpp
16+
Serialization.cpp
1617
SerializedDiagnosticConsumer.cpp)
1718
add_dependencies(swiftFrontend
1819
SwiftOptions)

lib/Frontend/Serialization.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
//===--- Serialization.cpp - Write Swift modules --------------------------===//
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+
#include "swift/Serialization/Serialization.h"
14+
#include "swift/APIDigester/ModuleAnalyzerNodes.h"
15+
#include "swift/AST/FileSystem.h"
16+
#include "swift/Subsystems.h"
17+
#include "swift/SymbolGraphGen/SymbolGraphGen.h"
18+
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
19+
#include "llvm/Support/SmallVectorMemoryBuffer.h"
20+
21+
using namespace swift;
22+
23+
static ModuleDecl *getModule(ModuleOrSourceFile DC) {
24+
if (auto M = DC.dyn_cast<ModuleDecl *>())
25+
return M;
26+
return DC.get<SourceFile *>()->getParentModule();
27+
}
28+
29+
static ASTContext &getContext(ModuleOrSourceFile DC) {
30+
return getModule(DC)->getASTContext();
31+
}
32+
33+
static void emitABIDescriptor(ModuleOrSourceFile DC,
34+
const SerializationOptions &options) {
35+
using namespace swift::ide::api;
36+
if (!options.ABIDescriptorPath.empty()) {
37+
if (DC.is<ModuleDecl *>()) {
38+
dumpModuleContent(DC.get<ModuleDecl *>(), options.ABIDescriptorPath, true,
39+
options.emptyABIDescriptor);
40+
}
41+
}
42+
}
43+
44+
void swift::serializeToBuffers(
45+
ModuleOrSourceFile DC, const SerializationOptions &options,
46+
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
47+
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
48+
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
49+
const SILModule *M) {
50+
51+
assert(!options.OutputPath.empty());
52+
{
53+
FrontendStatsTracer tracer(getContext(DC).Stats,
54+
"Serialization, swiftmodule, to buffer");
55+
llvm::SmallString<1024> buf;
56+
llvm::raw_svector_ostream stream(buf);
57+
serialization::writeToStream(stream, DC, M, options,
58+
/*dependency info*/ nullptr);
59+
bool hadError = withOutputFile(getContext(DC).Diags, options.OutputPath,
60+
[&](raw_ostream &out) {
61+
out << stream.str();
62+
return false;
63+
});
64+
if (hadError)
65+
return;
66+
67+
emitABIDescriptor(DC, options);
68+
if (moduleBuffer)
69+
*moduleBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>(
70+
std::move(buf), options.OutputPath,
71+
/*RequiresNullTerminator=*/false);
72+
}
73+
74+
if (!options.DocOutputPath.empty()) {
75+
FrontendStatsTracer tracer(getContext(DC).Stats,
76+
"Serialization, swiftdoc, to buffer");
77+
llvm::SmallString<1024> buf;
78+
llvm::raw_svector_ostream stream(buf);
79+
serialization::writeDocToStream(stream, DC, options.GroupInfoPath);
80+
(void)withOutputFile(getContext(DC).Diags, options.DocOutputPath,
81+
[&](raw_ostream &out) {
82+
out << stream.str();
83+
return false;
84+
});
85+
if (moduleDocBuffer)
86+
*moduleDocBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>(
87+
std::move(buf), options.DocOutputPath,
88+
/*RequiresNullTerminator=*/false);
89+
}
90+
91+
if (!options.SourceInfoOutputPath.empty()) {
92+
FrontendStatsTracer tracer(getContext(DC).Stats,
93+
"Serialization, swiftsourceinfo, to buffer");
94+
llvm::SmallString<1024> buf;
95+
llvm::raw_svector_ostream stream(buf);
96+
serialization::writeSourceInfoToStream(stream, DC);
97+
(void)withOutputFile(getContext(DC).Diags, options.SourceInfoOutputPath,
98+
[&](raw_ostream &out) {
99+
out << stream.str();
100+
return false;
101+
});
102+
if (moduleSourceInfoBuffer)
103+
*moduleSourceInfoBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>(
104+
std::move(buf), options.SourceInfoOutputPath,
105+
/*RequiresNullTerminator=*/false);
106+
}
107+
}
108+
109+
void swift::serialize(
110+
ModuleOrSourceFile DC, const SerializationOptions &options,
111+
const symbolgraphgen::SymbolGraphOptions &symbolGraphOptions,
112+
const SILModule *M,
113+
const fine_grained_dependencies::SourceFileDepGraph *DG) {
114+
assert(!options.OutputPath.empty());
115+
116+
if (options.OutputPath == "-") {
117+
// Special-case writing to stdout.
118+
serialization::writeToStream(llvm::outs(), DC, M, options, DG);
119+
assert(options.DocOutputPath.empty());
120+
return;
121+
}
122+
123+
bool hadError = withOutputFile(getContext(DC).Diags,
124+
options.OutputPath,
125+
[&](raw_ostream &out) {
126+
FrontendStatsTracer tracer(getContext(DC).Stats,
127+
"Serialization, swiftmodule");
128+
serialization::writeToStream(out, DC, M, options, DG);
129+
return false;
130+
});
131+
if (hadError)
132+
return;
133+
134+
if (!options.DocOutputPath.empty()) {
135+
(void)withOutputFile(getContext(DC).Diags,
136+
options.DocOutputPath,
137+
[&](raw_ostream &out) {
138+
FrontendStatsTracer tracer(getContext(DC).Stats,
139+
"Serialization, swiftdoc");
140+
serialization::writeDocToStream(out, DC, options.GroupInfoPath);
141+
return false;
142+
});
143+
}
144+
145+
if (!options.SourceInfoOutputPath.empty()) {
146+
(void)withOutputFile(getContext(DC).Diags,
147+
options.SourceInfoOutputPath,
148+
[&](raw_ostream &out) {
149+
FrontendStatsTracer tracer(getContext(DC).Stats,
150+
"Serialization, swiftsourceinfo");
151+
serialization::writeSourceInfoToStream(out, DC);
152+
return false;
153+
});
154+
}
155+
156+
if (!symbolGraphOptions.OutputDir.empty()) {
157+
if (DC.is<ModuleDecl *>()) {
158+
auto *M = DC.get<ModuleDecl *>();
159+
FrontendStatsTracer tracer(getContext(DC).Stats,
160+
"Serialization, symbolgraph");
161+
symbolgraphgen::emitSymbolGraphForModule(M, symbolGraphOptions);
162+
}
163+
}
164+
emitABIDescriptor(DC, options);
165+
}

lib/Serialization/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_swift_host_library(swiftSerialization STATIC
1616
)
1717
target_link_libraries(swiftSerialization PRIVATE
1818
swiftClangImporter
19-
swiftSIL
20-
swiftSymbolGraphGen)
19+
swiftOption
20+
swiftSIL)
2121

2222
set_swift_llvm_is_available(swiftSerialization)

0 commit comments

Comments
 (0)