Skip to content

Commit 965c735

Browse files
authored
Merge pull request #32131 from slavapestov/binary-swiftdeps-format
New binary swiftdeps format
2 parents 42d2f9d + b873fe2 commit 965c735

File tree

547 files changed

+1380
-2017
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

547 files changed

+1380
-2017
lines changed

include/swift/AST/FileSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/Basic/FileSystem.h"
1717
#include "swift/AST/DiagnosticEngine.h"
18+
#include "swift/AST/DiagnosticsCommon.h"
1819

1920
namespace swift {
2021

include/swift/AST/FineGrainedDependencies.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===----- FineGrainedependencies.h -----------------------------*- C++ -*-===//
1+
//===----- FineGrainedDependencies.h ----------------------------*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -632,6 +632,11 @@ class DepGraphNode {
632632

633633
const DependencyKey &getKey() const { return key; }
634634

635+
/// Only used when the driver is reading a SourceFileDepGraphNode.
636+
void setKey(const DependencyKey &key) {
637+
this->key = key;
638+
}
639+
635640
const Optional<StringRef> getFingerprint() const {
636641
if (fingerprint) {
637642
return StringRef(fingerprint.getValue());
@@ -684,15 +689,15 @@ class SourceFileDepGraphNode : public DepGraphNode {
684689
/// True iff a Decl exists for this node.
685690
/// If a provides and a depends in the existing system both have the same key,
686691
/// only one SourceFileDepGraphNode is emitted.
687-
bool isProvides;
692+
bool isProvides = false;
688693

689694
friend ::llvm::yaml::MappingContextTraits<SourceFileDepGraphNode,
690695
SourceFileDepGraph>;
691696

692697
public:
693698
/// When the driver imports a node create an uninitialized instance for
694699
/// deserializing.
695-
SourceFileDepGraphNode() : DepGraphNode(), sequenceNumber(~0) {}
700+
SourceFileDepGraphNode() : DepGraphNode() {}
696701

697702
/// Used by the frontend to build nodes.
698703
SourceFileDepGraphNode(DependencyKey key, Optional<StringRef> fingerprint,
@@ -736,6 +741,9 @@ class SourceFileDepGraphNode : public DepGraphNode {
736741
: "somewhere else");
737742
}
738743

744+
SWIFT_DEBUG_DUMP;
745+
void dump(llvm::raw_ostream &os) const;
746+
739747
bool verify() const {
740748
DepGraphNode::verify();
741749
assert(getIsProvides() || isDepends());
@@ -872,7 +880,6 @@ class SourceFileDepGraph {
872880

873881
void emitDotFile(StringRef outputPath, DiagnosticEngine &diags);
874882

875-
private:
876883
void addNode(SourceFileDepGraphNode *n) {
877884
n->setSequenceNumber(allNodes.size());
878885
allNodes.push_back(n);
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//===---- FineGrainedDependencyFormat.h - swiftdeps format ---*- C++ -*-======//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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_AST_FINEGRAINEDDEPENDENCYFORMAT_H
14+
#define SWIFT_AST_FINEGRAINEDDEPENDENCYFORMAT_H
15+
16+
#include "llvm/Bitcode/RecordLayout.h"
17+
#include "llvm/Bitstream/BitCodes.h"
18+
19+
namespace llvm {
20+
class MemoryBuffer;
21+
}
22+
23+
namespace swift {
24+
25+
class DiagnosticEngine;
26+
27+
namespace fine_grained_dependencies {
28+
29+
class SourceFileDepGraph;
30+
31+
using llvm::BCFixed;
32+
using llvm::BCVBR;
33+
using llvm::BCBlob;
34+
using llvm::BCRecordLayout;
35+
36+
/// Every .swiftdeps file begins with these 4 bytes, for easy identification when
37+
/// debugging.
38+
const unsigned char FINE_GRAINED_DEPDENENCY_FORMAT_SIGNATURE[] = {'D', 'E', 'P', 'S'};
39+
40+
const unsigned FINE_GRAINED_DEPENDENCY_FORMAT_VERSION_MAJOR = 1;
41+
42+
/// Increment this on every change.
43+
const unsigned FINE_GRAINED_DEPENDENCY_FORMAT_VERSION_MINOR = 0;
44+
45+
using IdentifierIDField = BCVBR<13>;
46+
47+
using NodeKindField = BCFixed<3>;
48+
using DeclAspectField = BCFixed<1>;
49+
50+
const unsigned RECORD_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID;
51+
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.
59+
namespace record_block {
60+
enum {
61+
METADATA = 1,
62+
SOURCE_FILE_DEP_GRAPH_NODE,
63+
FINGERPRINT_NODE,
64+
DEPENDS_ON_DEFINITION_NODE,
65+
IDENTIFIER_NODE,
66+
};
67+
68+
// Always the first record in the file.
69+
using MetadataLayout = BCRecordLayout<
70+
METADATA, // ID
71+
BCFixed<16>, // Dependency graph format major version
72+
BCFixed<16>, // Dependency graph format minor version
73+
BCBlob // Compiler version string
74+
>;
75+
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+
88+
using SourceFileDepGraphNodeLayout = BCRecordLayout<
89+
SOURCE_FILE_DEP_GRAPH_NODE, // ID
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
96+
BCFixed<1> // Is this a "provides" node?
97+
>;
98+
99+
// Follows DEPENDS_ON_DEFINITION_NODE when the SourceFileDepGraphNode has a
100+
// fingerprint set.
101+
using FingerprintNodeLayout = BCRecordLayout<
102+
FINGERPRINT_NODE,
103+
BCBlob
104+
>;
105+
106+
// Follows SOURCE_FILE_DEP_GRAPH_NODE and FINGERPRINT_NODE when the
107+
// SourceFileDepGraphNode has one or more depends-on entries.
108+
using DependsOnDefNodeLayout = BCRecordLayout<
109+
DEPENDS_ON_DEFINITION_NODE,
110+
BCVBR<16> // The sequence number (starting from 0) of the referenced
111+
// SOURCE_FILE_DEP_GRAPH_NODE
112+
>;
113+
}
114+
115+
/// Tries to read the dependency graph from the given buffer.
116+
/// Returns true if there was an error.
117+
bool readFineGrainedDependencyGraph(llvm::MemoryBuffer &buffer,
118+
SourceFileDepGraph &g);
119+
120+
/// Tries to read the dependency graph from the given path name.
121+
/// Returns true if there was an error.
122+
bool readFineGrainedDependencyGraph(llvm::StringRef path,
123+
SourceFileDepGraph &g);
124+
125+
/// Tries to write the dependency graph to the given path name.
126+
/// Returns true if there was an error.
127+
bool writeFineGrainedDependencyGraph(DiagnosticEngine &diags, llvm::StringRef path,
128+
const SourceFileDepGraph &g);
129+
130+
} // namespace fine_grained_dependencies
131+
} // namespace swift
132+
133+
#endif

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ add_swift_host_library(swiftAST STATIC
4545
Evaluator.cpp
4646
Expr.cpp
4747
FineGrainedDependencies.cpp
48+
FineGrainedDependencyFormat.cpp
4849
FrontendSourceFileDepGraphFactory.cpp
4950
GenericEnvironment.cpp
5051
GenericSignature.cpp

lib/AST/FineGrainedDependencies.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include <stdio.h>
14-
1513
#include "swift/AST/FineGrainedDependencies.h"
1614

1715
// may not all be needed
1816
#include "swift/AST/DiagnosticEngine.h"
1917
#include "swift/AST/DiagnosticsCommon.h"
2018
#include "swift/AST/DiagnosticsFrontend.h"
2119
#include "swift/AST/FileSystem.h"
20+
#include "swift/AST/FineGrainedDependencyFormat.h"
2221
#include "swift/Basic/FileSystem.h"
2322
#include "swift/Basic/LLVM.h"
2423
#include "swift/Demangling/Demangle.h"
@@ -31,6 +30,7 @@
3130
#include "llvm/Support/Path.h"
3231
#include "llvm/Support/YAMLParser.h"
3332

33+
3434
// This file holds the definitions for the fine-grained dependency system
3535
// that are likely to be stable as it moves away from the status quo.
3636
// These include the graph structures common to both programs and also
@@ -53,11 +53,9 @@ Optional<SourceFileDepGraph> SourceFileDepGraph::loadFromPath(StringRef path) {
5353
Optional<SourceFileDepGraph>
5454
SourceFileDepGraph::loadFromBuffer(llvm::MemoryBuffer &buffer) {
5555
SourceFileDepGraph fg;
56-
llvm::yaml::Input yamlReader(llvm::MemoryBufferRef(buffer), nullptr);
57-
yamlReader >> fg;
58-
if (yamlReader.error())
56+
if (swift::fine_grained_dependencies::readFineGrainedDependencyGraph(
57+
buffer, fg))
5958
return None;
60-
// return fg; compiles for Mac but not Linux, because it cannot be copied.
6159
return Optional<SourceFileDepGraph>(std::move(fg));
6260
}
6361

@@ -333,6 +331,19 @@ void DepGraphNode::dump(raw_ostream &os) const {
333331
llvm::errs() << "no fingerprint";
334332
}
335333

334+
void SourceFileDepGraphNode::dump() const {
335+
dump(llvm::errs());
336+
}
337+
338+
void SourceFileDepGraphNode::dump(raw_ostream &os) const {
339+
DepGraphNode::dump(os);
340+
os << " sequence number: " << sequenceNumber;
341+
os << " is provides: " << isProvides;
342+
os << " depends on:";
343+
for (auto def : defsIDependUpon)
344+
os << " " << def;
345+
}
346+
336347
std::string DepGraphNode::humanReadableName(StringRef where) const {
337348

338349
return getKey().humanReadableName() +

0 commit comments

Comments
 (0)