Skip to content

Commit b873fe2

Browse files
committed
Code review feedback from Dave Ungar
1 parent 53931c0 commit b873fe2

File tree

4 files changed

+50
-40
lines changed

4 files changed

+50
-40
lines changed

include/swift/AST/FineGrainedDependencyFormat.h

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ using llvm::BCVBR;
3333
using llvm::BCBlob;
3434
using llvm::BCRecordLayout;
3535

36+
/// Every .swiftdeps file begins with these 4 bytes, for easy identification when
37+
/// debugging.
3638
const unsigned char FINE_GRAINED_DEPDENENCY_FORMAT_SIGNATURE[] = {'D', 'E', 'P', 'S'};
3739

3840
const unsigned FINE_GRAINED_DEPENDENCY_FORMAT_VERSION_MAJOR = 1;
@@ -47,6 +49,13 @@ using DeclAspectField = BCFixed<1>;
4749

4850
const unsigned RECORD_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID;
4951

52+
/// The swiftdeps file format consists of a METADATA record, followed by zero or more
53+
/// IDENTIFIER_NODE records.
54+
///
55+
/// Then, there is one SOURCE_FILE_DEP_GRAPH_NODE for each serialized
56+
/// SourceFileDepGraphNode. These are followed by FINGERPRINT_NODE and
57+
/// DEPENDS_ON_DEFINITION_NODE, if the node has a fingerprint and depends-on
58+
/// definitions, respectively.
5059
namespace record_block {
5160
enum {
5261
METADATA = 1,
@@ -56,47 +65,65 @@ namespace record_block {
5665
IDENTIFIER_NODE,
5766
};
5867

68+
// Always the first record in the file.
5969
using MetadataLayout = BCRecordLayout<
6070
METADATA, // ID
6171
BCFixed<16>, // Dependency graph format major version
6272
BCFixed<16>, // Dependency graph format minor version
6373
BCBlob // Compiler version string
6474
>;
6575

76+
// After the metadata record, we have zero or more identifier records,
77+
// for each unique string that is referenced from a SourceFileDepGraphNode.
78+
//
79+
// Identifiers are referenced by their sequence number, starting from 1.
80+
// The identifier value 0 is special; it always represents the empty string.
81+
// There is no IDENTIFIER_NODE serialized that corresponds to it, instead
82+
// the first IDENTIFIER_NODE always has a sequence number of 1.
83+
using IdentifierNodeLayout = BCRecordLayout<
84+
IDENTIFIER_NODE,
85+
BCBlob
86+
>;
87+
6688
using SourceFileDepGraphNodeLayout = BCRecordLayout<
6789
SOURCE_FILE_DEP_GRAPH_NODE, // ID
68-
NodeKindField, // Dependency key node kind
69-
DeclAspectField, // Dependency key declaration aspect
70-
IdentifierIDField, // Dependency key mangled context type name
71-
IdentifierIDField, // Dependency key basic name
90+
// The next four fields correspond to the fields of the DependencyKey
91+
// structure.
92+
NodeKindField, // DependencyKey::kind
93+
DeclAspectField, // DependencyKey::aspect
94+
IdentifierIDField, // DependencyKey::context
95+
IdentifierIDField, // DependencyKey::name
7296
BCFixed<1> // Is this a "provides" node?
7397
>;
7498

75-
// Optionally follows DEPENDS_ON_DEFINITION_NODE.
99+
// Follows DEPENDS_ON_DEFINITION_NODE when the SourceFileDepGraphNode has a
100+
// fingerprint set.
76101
using FingerprintNodeLayout = BCRecordLayout<
77102
FINGERPRINT_NODE,
78103
BCBlob
79104
>;
80105

81-
// Optionally follows SOURCE_FILE_DEP_GRAPH_NODE and FINGERPRINT_NODE.
106+
// Follows SOURCE_FILE_DEP_GRAPH_NODE and FINGERPRINT_NODE when the
107+
// SourceFileDepGraphNode has one or more depends-on entries.
82108
using DependsOnDefNodeLayout = BCRecordLayout<
83109
DEPENDS_ON_DEFINITION_NODE,
84-
BCVBR<16>
85-
>;
86-
87-
// Optionally follows all other nodes.
88-
using IdentifierNodeLayout = BCRecordLayout<
89-
IDENTIFIER_NODE,
90-
BCBlob
110+
BCVBR<16> // The sequence number (starting from 0) of the referenced
111+
// SOURCE_FILE_DEP_GRAPH_NODE
91112
>;
92113
}
93114

115+
/// Tries to read the dependency graph from the given buffer.
116+
/// Returns true if there was an error.
94117
bool readFineGrainedDependencyGraph(llvm::MemoryBuffer &buffer,
95118
SourceFileDepGraph &g);
96119

120+
/// Tries to read the dependency graph from the given path name.
121+
/// Returns true if there was an error.
97122
bool readFineGrainedDependencyGraph(llvm::StringRef path,
98123
SourceFileDepGraph &g);
99124

125+
/// Tries to write the dependency graph to the given path name.
126+
/// Returns true if there was an error.
100127
bool writeFineGrainedDependencyGraph(DiagnosticEngine &diags, llvm::StringRef path,
101128
const SourceFileDepGraph &g);
102129

lib/AST/FineGrainedDependencies.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,11 @@ Optional<SourceFileDepGraph> SourceFileDepGraph::loadFromPath(StringRef path) {
5252

5353
Optional<SourceFileDepGraph>
5454
SourceFileDepGraph::loadFromBuffer(llvm::MemoryBuffer &buffer) {
55-
if (false) {
56-
SourceFileDepGraph fg;
57-
llvm::yaml::Input yamlReader(llvm::MemoryBufferRef(buffer), nullptr);
58-
yamlReader >> fg;
59-
if (yamlReader.error())
60-
return None;
61-
// return fg; compiles for Mac but not Linux, because it cannot be copied.
62-
return Optional<SourceFileDepGraph>(std::move(fg));
63-
} else {
64-
SourceFileDepGraph fg;
65-
if (swift::fine_grained_dependencies::readFineGrainedDependencyGraph(
66-
buffer, fg))
67-
return None;
68-
return Optional<SourceFileDepGraph>(std::move(fg));
69-
}
55+
SourceFileDepGraph fg;
56+
if (swift::fine_grained_dependencies::readFineGrainedDependencyGraph(
57+
buffer, fg))
58+
return None;
59+
return Optional<SourceFileDepGraph>(std::move(fg));
7060
}
7161

7262
//==============================================================================

lib/AST/FineGrainedDependencyFormat.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Deserializer {
3535
SmallVector<uint64_t, 64> Scratch;
3636
StringRef BlobData;
3737

38+
// These all return true if there was an error.
3839
bool readSignature();
3940
bool enterTopLevelBlock();
4041
bool readMetadata();
@@ -63,6 +64,8 @@ bool Deserializer::readSignature() {
6364
}
6465

6566
bool Deserializer::enterTopLevelBlock() {
67+
// Read the BLOCKINFO_BLOCK, which contains metadata used when dumping
68+
// the binary data with llvm-bcanalyzer.
6669
{
6770
auto next = Cursor.advance();
6871
if (!next) {
@@ -80,6 +83,7 @@ bool Deserializer::enterTopLevelBlock() {
8083
return true;
8184
}
8285

86+
// Enters our subblock, which contains the actual dependency information.
8387
{
8488
auto next = Cursor.advance();
8589
if (!next) {

lib/AST/FrontendSourceFileDepGraphFactory.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,7 @@ bool fine_grained_dependencies::emitReferenceDependencies(
292292
SF, outputPath, depTracker, alsoEmitDotFile)
293293
.construct();
294294

295-
bool hadError = false;
296-
if (false) {
297-
hadError =
298-
withOutputFile(diags, outputPath, [&](llvm::raw_pwrite_stream &out) {
299-
out << g.yamlProlog(SF->getASTContext().hadError());
300-
llvm::yaml::Output yamlWriter(out);
301-
yamlWriter << g;
302-
return false;
303-
});
304-
} else {
305-
hadError = writeFineGrainedDependencyGraph(diags, outputPath, g);
306-
}
295+
bool hadError = writeFineGrainedDependencyGraph(diags, outputPath, g);
307296

308297
// If path is stdout, cannot read it back, so check for "-"
309298
assert(outputPath == "-" || g.verifyReadsWhatIsWritten(outputPath));

0 commit comments

Comments
 (0)