Skip to content

Commit 860e677

Browse files
authored
Merge pull request #452 from CodaFi/graphing-calculator
Clean Up The Module Dependency Graph Format A Bit
2 parents de31343 + 34d05d0 commit 860e677

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,35 @@ extension ModuleDependencyGraph {
267267
// MARK: - Serialization
268268

269269
extension ModuleDependencyGraph {
270+
/// The leading signature of this file format.
270271
fileprivate static let signature = "DDEP"
272+
/// The expected version number of the serialized dependency graph.
273+
///
274+
/// - WARNING: You *must* increment the minor version number when making any
275+
/// changes to the underlying serialization format.
276+
fileprivate static let version = Version(1, 0, 0)
271277

278+
/// The IDs of the records used by the module dependency graph.
272279
fileprivate enum RecordID: UInt64 {
273-
case metadata = 1
274-
case moduleDepGraphNode
275-
case fingerprintNode
276-
case identifierNode
280+
case metadata = 1
281+
case moduleDepGraphNode = 2
282+
case identifierNode = 3
283+
284+
/// The human-readable name of this record.
285+
///
286+
/// This data is emitted into the block info field for each record so tools
287+
/// like llvm-bcanalyzer can be used to more effectively debug serialized
288+
/// dependency graphs.
289+
var humanReadableName: String {
290+
switch self {
291+
case .metadata:
292+
return "METADATA"
293+
case .moduleDepGraphNode:
294+
return "MODULE_DEP_GRAPH_NODE"
295+
case .identifierNode:
296+
return "IDENTIFIER_NODE"
297+
}
298+
}
277299
}
278300

279301
fileprivate enum ReadError: Error {
@@ -351,7 +373,10 @@ extension ModuleDependencyGraph {
351373
}
352374

353375
mutating func visit(record: BitcodeElement.Record) throws {
354-
guard let kind = RecordID(rawValue: record.id) else { throw ReadError.unknownRecord }
376+
guard let kind = RecordID(rawValue: record.id) else {
377+
throw ReadError.unknownRecord
378+
}
379+
355380
switch kind {
356381
case .metadata:
357382
// If we've already read metadata, this is an unexpected duplicate.
@@ -371,10 +396,13 @@ extension ModuleDependencyGraph {
371396
self.finalize(node: node)
372397
}
373398
let kindCode = record.fields[0]
374-
guard record.fields.count == 6,
399+
guard record.fields.count == 7,
375400
let declAspect = DependencyKey.DeclAspect(record.fields[1]),
376401
record.fields[2] < identifiers.count,
377-
record.fields[3] < identifiers.count else {
402+
record.fields[3] < identifiers.count,
403+
case .blob(let fingerprintBlob) = record.payload,
404+
let fingerprintStr = String(data: fingerprintBlob, encoding: .utf8)
405+
else {
378406
throw ReadError.malformedModuleDepGraphNodeRecord
379407
}
380408
let context = identifiers[Int(record.fields[2])]
@@ -384,21 +412,15 @@ extension ModuleDependencyGraph {
384412
let key = DependencyKey(aspect: declAspect, designator: designator)
385413
let hasSwiftDeps = Int(record.fields[4]) != 0
386414
let swiftDepsStr = hasSwiftDeps ? identifiers[Int(record.fields[5])] : nil
415+
let hasFingerprint = Int(record.fields[6]) != 0
416+
let fingerprint = hasFingerprint ? fingerprintStr : nil
387417
let swiftDeps = try swiftDepsStr
388418
.map({ try VirtualPath(path: $0) })
389419
.map(ModuleDependencyGraph.SwiftDeps.init)
390420
node = Node(key: key,
391-
fingerprint: nil,
421+
fingerprint: fingerprint,
392422
swiftDeps: swiftDeps)
393423
sequenceNumber += 1
394-
case .fingerprintNode:
395-
guard node != nil,
396-
record.fields.count == 0,
397-
case .blob(let fingerprintBlob) = record.payload,
398-
let fingerprint = String(data: fingerprintBlob, encoding: .utf8) else {
399-
throw ReadError.malformedFingerprintRecord
400-
}
401-
node?.fingerprint = fingerprint
402424
case .identifierNode:
403425
guard record.fields.count == 0,
404426
case .blob(let identifierBlob) = record.payload,
@@ -422,7 +444,7 @@ extension ModuleDependencyGraph {
422444
guard let major = visitor.majorVersion,
423445
let minor = visitor.minorVersion,
424446
visitor.compilerVersionString != nil,
425-
(major, minor) == (1, 0)
447+
Version(Int(major), Int(minor), 0) == Self.version
426448
else {
427449
throw ReadError.malformedMetadataRecord
428450
}
@@ -495,20 +517,19 @@ extension ModuleDependencyGraph {
495517
}
496518
}
497519

498-
private func emitRecordID(_ id: RecordID, named name: String) {
520+
private func emitRecordID(_ id: RecordID) {
499521
self.stream.writeRecord(Bitstream.BlockInfoCode.setRecordName) {
500522
$0.append(id)
501-
$0.append(name)
523+
$0.append(id.humanReadableName)
502524
}
503525
}
504526

505527
private func writeBlockInfoBlock() {
506528
self.stream.writeBlockInfoBlock {
507529
self.emitBlockID(.firstApplicationID, "RECORD_BLOCK")
508-
self.emitRecordID(.metadata, named: "METADATA")
509-
self.emitRecordID(.moduleDepGraphNode, named: "MODULE_DEP_GRAPH_NODE")
510-
self.emitRecordID(.fingerprintNode, named: "FINGERPRINT_NODE")
511-
self.emitRecordID(.identifierNode, named: "IDENTIFIER_NODE")
530+
self.emitRecordID(.metadata)
531+
self.emitRecordID(.moduleDepGraphNode)
532+
self.emitRecordID(.identifierNode)
512533
}
513534
}
514535

@@ -601,11 +622,10 @@ extension ModuleDependencyGraph {
601622
.fixed(bitWidth: 1),
602623
// swiftdeps path
603624
.vbr(chunkBitWidth: 13),
604-
])
605-
serializer.abbreviate(.fingerprintNode, [
606-
.literal(RecordID.fingerprintNode.rawValue),
607-
// fingerprint data
608-
.blob
625+
// fingerprint?
626+
.fixed(bitWidth: 1),
627+
// fingerprint bytes
628+
.blob,
609629
])
610630
serializer.abbreviate(.identifierNode, [
611631
.literal(RecordID.identifierNode.rawValue),
@@ -618,7 +638,7 @@ extension ModuleDependencyGraph {
618638
serializer.writeStrings(in: graph)
619639

620640
graph.nodeFinder.forEachNode { node in
621-
serializer.stream.writeRecord(serializer.abbreviations[.moduleDepGraphNode]!) {
641+
serializer.stream.writeRecord(serializer.abbreviations[.moduleDepGraphNode]!, {
622642
$0.append(RecordID.moduleDepGraphNode)
623643
$0.append(node.dependencyKey.designator.code)
624644
$0.append(node.dependencyKey.aspect.code)
@@ -629,13 +649,8 @@ extension ModuleDependencyGraph {
629649
$0.append((node.swiftDeps != nil) ? UInt32(1) : UInt32(0))
630650
$0.append(serializer.lookupIdentifierCode(
631651
for: node.swiftDeps?.file.name ?? ""))
632-
}
633-
634-
if let fingerprint = node.fingerprint {
635-
serializer.stream.writeRecord(serializer.abbreviations[.fingerprintNode]!, {
636-
$0.append(RecordID.fingerprintNode)
637-
}, blob: fingerprint)
638-
}
652+
$0.append((node.fingerprint != nil) ? UInt32(1) : UInt32(0))
653+
}, blob: node.fingerprint ?? "")
639654
}
640655
}
641656
return ByteString(serializer.stream.data)

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraphParts/Node.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension ModuleDependencyGraph {
4545
/// frontend creates an interface node,
4646
/// it adds a dependency to it from the implementation source file node (which
4747
/// has the intefaceHash as its fingerprint).
48-
var fingerprint: String?
48+
let fingerprint: String?
4949

5050

5151
/// The swiftDeps file that holds this entity iff the entities .swiftdeps is known.

0 commit comments

Comments
 (0)