@@ -267,13 +267,35 @@ extension ModuleDependencyGraph {
267
267
// MARK: - Serialization
268
268
269
269
extension ModuleDependencyGraph {
270
+ /// The leading signature of this file format.
270
271
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 )
271
277
278
+ /// The IDs of the records used by the module dependency graph.
272
279
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
+ }
277
299
}
278
300
279
301
fileprivate enum ReadError : Error {
@@ -351,7 +373,10 @@ extension ModuleDependencyGraph {
351
373
}
352
374
353
375
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
+
355
380
switch kind {
356
381
case . metadata:
357
382
// If we've already read metadata, this is an unexpected duplicate.
@@ -371,10 +396,13 @@ extension ModuleDependencyGraph {
371
396
self . finalize ( node: node)
372
397
}
373
398
let kindCode = record. fields [ 0 ]
374
- guard record. fields. count == 6 ,
399
+ guard record. fields. count == 7 ,
375
400
let declAspect = DependencyKey . DeclAspect ( record. fields [ 1 ] ) ,
376
401
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 {
378
406
throw ReadError . malformedModuleDepGraphNodeRecord
379
407
}
380
408
let context = identifiers [ Int ( record. fields [ 2 ] ) ]
@@ -384,21 +412,15 @@ extension ModuleDependencyGraph {
384
412
let key = DependencyKey ( aspect: declAspect, designator: designator)
385
413
let hasSwiftDeps = Int ( record. fields [ 4 ] ) != 0
386
414
let swiftDepsStr = hasSwiftDeps ? identifiers [ Int ( record. fields [ 5 ] ) ] : nil
415
+ let hasFingerprint = Int ( record. fields [ 6 ] ) != 0
416
+ let fingerprint = hasFingerprint ? fingerprintStr : nil
387
417
let swiftDeps = try swiftDepsStr
388
418
. map ( { try VirtualPath ( path: $0) } )
389
419
. map ( ModuleDependencyGraph . SwiftDeps. init)
390
420
node = Node ( key: key,
391
- fingerprint: nil ,
421
+ fingerprint: fingerprint ,
392
422
swiftDeps: swiftDeps)
393
423
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
402
424
case . identifierNode:
403
425
guard record. fields. count == 0 ,
404
426
case . blob( let identifierBlob) = record. payload,
@@ -422,7 +444,7 @@ extension ModuleDependencyGraph {
422
444
guard let major = visitor. majorVersion,
423
445
let minor = visitor. minorVersion,
424
446
visitor. compilerVersionString != nil ,
425
- ( major, minor) == ( 1 , 0 )
447
+ Version ( Int ( major) , Int ( minor) , 0 ) == Self . version
426
448
else {
427
449
throw ReadError . malformedMetadataRecord
428
450
}
@@ -495,20 +517,19 @@ extension ModuleDependencyGraph {
495
517
}
496
518
}
497
519
498
- private func emitRecordID( _ id: RecordID , named name : String ) {
520
+ private func emitRecordID( _ id: RecordID ) {
499
521
self . stream. writeRecord ( Bitstream . BlockInfoCode. setRecordName) {
500
522
$0. append ( id)
501
- $0. append ( name )
523
+ $0. append ( id . humanReadableName )
502
524
}
503
525
}
504
526
505
527
private func writeBlockInfoBlock( ) {
506
528
self . stream. writeBlockInfoBlock {
507
529
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)
512
533
}
513
534
}
514
535
@@ -601,11 +622,10 @@ extension ModuleDependencyGraph {
601
622
. fixed( bitWidth: 1 ) ,
602
623
// swiftdeps path
603
624
. 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,
609
629
] )
610
630
serializer. abbreviate ( . identifierNode, [
611
631
. literal( RecordID . identifierNode. rawValue) ,
@@ -618,7 +638,7 @@ extension ModuleDependencyGraph {
618
638
serializer. writeStrings ( in: graph)
619
639
620
640
graph. nodeFinder. forEachNode { node in
621
- serializer. stream. writeRecord ( serializer. abbreviations [ . moduleDepGraphNode] !) {
641
+ serializer. stream. writeRecord ( serializer. abbreviations [ . moduleDepGraphNode] !, {
622
642
$0. append ( RecordID . moduleDepGraphNode)
623
643
$0. append ( node. dependencyKey. designator. code)
624
644
$0. append ( node. dependencyKey. aspect. code)
@@ -629,13 +649,8 @@ extension ModuleDependencyGraph {
629
649
$0. append ( ( node. swiftDeps != nil ) ? UInt32 ( 1 ) : UInt32 ( 0 ) )
630
650
$0. append ( serializer. lookupIdentifierCode (
631
651
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 ?? " " )
639
654
}
640
655
}
641
656
return ByteString ( serializer. stream. data)
0 commit comments