Skip to content

Commit 82e9935

Browse files
committed
Correct the Serialization of Embedded Swift Dependencies
llvm-bcanalyzer does *not* like it when there are multiple block info metadata blocks in the same bitstream file. This patch will skip the emission of that and just jump straight to the metadata block when we're not reading a "standalone" incremental dependency file. While I'm here, also add the right block abbreviation info so we can get a decent dump from llvm-bcanalyzer
1 parent 161899d commit 82e9935

File tree

7 files changed

+51
-59
lines changed

7 files changed

+51
-59
lines changed

include/swift/AST/FineGrainedDependencyFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ bool writeFineGrainedDependencyGraphToPath(DiagnosticEngine &diags,
136136
const SourceFileDepGraph &g);
137137

138138
void writeFineGrainedDependencyGraph(llvm::BitstreamWriter &Out,
139-
const SourceFileDepGraph &g);
139+
const SourceFileDepGraph &g,
140+
bool standalone);
140141

141142
} // namespace fine_grained_dependencies
142143
} // namespace swift

lib/AST/FineGrainedDependencies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ SourceFileDepGraph::loadFromBuffer(llvm::MemoryBuffer &buffer) {
6161
Optional<SourceFileDepGraph>
6262
SourceFileDepGraph::loadFromSwiftModuleBuffer(llvm::MemoryBuffer &buffer) {
6363
SourceFileDepGraph fg;
64-
if (swift::fine_grained_dependencies::
64+
if (!swift::fine_grained_dependencies::
6565
readFineGrainedDependencyGraphFromSwiftModule(buffer, fg))
6666
return None;
6767
return Optional<SourceFileDepGraph>(std::move(fg));

lib/AST/FineGrainedDependencyFormat.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Deserializer {
4444

4545
public:
4646
Deserializer(llvm::MemoryBufferRef Data) : Cursor(Data) {}
47-
bool readFineGrainedDependencyGraph(SourceFileDepGraph &g);
47+
bool readFineGrainedDependencyGraph(SourceFileDepGraph &g, bool standalone);
4848
bool readFineGrainedDependencyGraphFromSwiftModule(SourceFileDepGraph &g);
4949
};
5050

@@ -151,13 +151,14 @@ static llvm::Optional<DeclAspect> getDeclAspect(unsigned declAspect) {
151151
return None;
152152
}
153153

154-
bool Deserializer::readFineGrainedDependencyGraph(SourceFileDepGraph &g) {
154+
bool Deserializer::readFineGrainedDependencyGraph(SourceFileDepGraph &g,
155+
bool standalone) {
155156
using namespace record_block;
156157

157-
if (readSignature())
158+
if (standalone && readSignature())
158159
return true;
159160

160-
if (enterTopLevelBlock())
161+
if (standalone && enterTopLevelBlock())
161162
return true;
162163

163164
if (readMetadata())
@@ -260,7 +261,7 @@ bool Deserializer::readFineGrainedDependencyGraph(SourceFileDepGraph &g) {
260261
bool swift::fine_grained_dependencies::readFineGrainedDependencyGraph(
261262
llvm::MemoryBuffer &buffer, SourceFileDepGraph &g) {
262263
Deserializer deserializer(buffer.getMemBufferRef());
263-
return deserializer.readFineGrainedDependencyGraph(g);
264+
return deserializer.readFineGrainedDependencyGraph(g, /*standalone*/true);
264265
}
265266

266267
bool swift::fine_grained_dependencies::readFineGrainedDependencyGraph(
@@ -322,7 +323,8 @@ class Serializer {
322323
Serializer(llvm::BitstreamWriter &ExistingOut) : Out(ExistingOut) {}
323324

324325
public:
325-
void writeFineGrainedDependencyGraph(const SourceFileDepGraph &g);
326+
void writeFineGrainedDependencyGraph(const SourceFileDepGraph &g,
327+
bool standalone);
326328
};
327329

328330
} // end namespace
@@ -382,11 +384,15 @@ void Serializer::writeMetadata() {
382384
}
383385

384386
void
385-
Serializer::writeFineGrainedDependencyGraph(const SourceFileDepGraph &g) {
386-
writeSignature();
387-
writeBlockInfoBlock();
388-
389-
llvm::BCBlockRAII restoreBlock(Out, RECORD_BLOCK_ID, 8);
387+
Serializer::writeFineGrainedDependencyGraph(const SourceFileDepGraph &g,
388+
bool standalone) {
389+
auto blockID = INCREMENTAL_INFORMATION_BLOCK_ID;
390+
if (standalone) {
391+
writeSignature();
392+
writeBlockInfoBlock();
393+
blockID = RECORD_BLOCK_ID;
394+
}
395+
llvm::BCBlockRAII restoreBlock(Out, blockID, 8);
390396

391397
using namespace record_block;
392398

@@ -468,9 +474,9 @@ unsigned Serializer::getIdentifier(StringRef str) {
468474
}
469475

470476
void swift::fine_grained_dependencies::writeFineGrainedDependencyGraph(
471-
llvm::BitstreamWriter &Out, const SourceFileDepGraph &g) {
477+
llvm::BitstreamWriter &Out, const SourceFileDepGraph &g, bool standalone) {
472478
Serializer serializer{Out};
473-
serializer.writeFineGrainedDependencyGraph(g);
479+
serializer.writeFineGrainedDependencyGraph(g, standalone);
474480
}
475481

476482
bool swift::fine_grained_dependencies::writeFineGrainedDependencyGraphToPath(
@@ -480,7 +486,7 @@ bool swift::fine_grained_dependencies::writeFineGrainedDependencyGraphToPath(
480486
return withOutputFile(diags, path, [&](llvm::raw_ostream &out) {
481487
SmallVector<char, 0> Buffer;
482488
llvm::BitstreamWriter Writer{Buffer};
483-
writeFineGrainedDependencyGraph(Writer, g);
489+
writeFineGrainedDependencyGraph(Writer, g, /*standalone*/ true);
484490
out.write(Buffer.data(), Buffer.size());
485491
out.flush();
486492
return false;
@@ -516,7 +522,7 @@ static bool enterTopLevelModuleBlock(llvm::BitstreamCursor &cursor, unsigned ID,
516522
if (next.Kind != llvm::BitstreamEntry::SubBlock)
517523
return false;
518524

519-
if (next.ID == RECORD_BLOCK_ID) {
525+
if (next.ID == llvm::bitc::BLOCKINFO_BLOCK_ID) {
520526
if (shouldReadBlockInfo) {
521527
if (!cursor.ReadBlockInfoBlock())
522528
return false;
@@ -531,7 +537,6 @@ static bool enterTopLevelModuleBlock(llvm::BitstreamCursor &cursor, unsigned ID,
531537
return false;
532538

533539
if (llvm::Error Err = cursor.EnterSubBlock(ID)) {
534-
// FIXME this drops the error on the floor.
535540
consumeError(std::move(Err));
536541
return false;
537542
}
@@ -549,12 +554,12 @@ bool swift::fine_grained_dependencies::
549554
bool Deserializer::readFineGrainedDependencyGraphFromSwiftModule(
550555
SourceFileDepGraph &g) {
551556
if (!checkModuleSignature(Cursor, {0xE2, 0x9C, 0xA8, 0x0E}) ||
552-
!enterTopLevelModuleBlock(Cursor, RECORD_BLOCK_ID, false)) {
557+
!enterTopLevelModuleBlock(Cursor, llvm::bitc::FIRST_APPLICATION_BLOCKID, false)) {
553558
return false;
554559
}
555560

556561
llvm::BitstreamEntry topLevelEntry;
557-
562+
bool ReadFineGrainedDependencies = false;
558563
while (!Cursor.AtEndOfStream()) {
559564
llvm::Expected<llvm::BitstreamEntry> maybeEntry =
560565
Cursor.advance(llvm::BitstreamCursor::AF_DontPopBlockAtEnd);
@@ -573,7 +578,11 @@ bool Deserializer::readFineGrainedDependencyGraphFromSwiftModule(
573578
consumeError(std::move(Err));
574579
return false;
575580
}
576-
readFineGrainedDependencyGraph(g);
581+
if (readFineGrainedDependencyGraph(g, /*standalone*/ false)) {
582+
break;
583+
}
584+
585+
ReadFineGrainedDependencies = true;
577586
break;
578587
}
579588

@@ -587,5 +596,5 @@ bool Deserializer::readFineGrainedDependencyGraphFromSwiftModule(
587596
}
588597
}
589598

590-
return false;
599+
return ReadFineGrainedDependencies;
591600
}

lib/Serialization/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ add_swift_host_library(swiftSerialization STATIC
99
SerializedModuleLoader.cpp
1010
SerializedSILLoader.cpp
1111
SerializeDoc.cpp
12-
SerializeIncremental.cpp
1312
SerializeSIL.cpp
1413

1514
LLVM_LINK_COMPONENTS

lib/Serialization/Serialization.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,13 @@ void Serializer::writeBlockInfoBlock() {
861861
BLOCK_RECORD(sil_index_block, SIL_DIFFERENTIABILITY_WITNESS_NAMES);
862862
BLOCK_RECORD(sil_index_block, SIL_DIFFERENTIABILITY_WITNESS_OFFSETS);
863863

864+
BLOCK(INCREMENTAL_INFORMATION_BLOCK);
865+
BLOCK_RECORD(fine_grained_dependencies::record_block, METADATA);
866+
BLOCK_RECORD(fine_grained_dependencies::record_block, SOURCE_FILE_DEP_GRAPH_NODE);
867+
BLOCK_RECORD(fine_grained_dependencies::record_block, FINGERPRINT_NODE);
868+
BLOCK_RECORD(fine_grained_dependencies::record_block, DEPENDS_ON_DEFINITION_NODE);
869+
BLOCK_RECORD(fine_grained_dependencies::record_block, IDENTIFIER_NODE);
870+
864871
#undef BLOCK
865872
#undef BLOCK_RECORD
866873
}
@@ -5231,8 +5238,9 @@ void Serializer::writeToStream(
52315238
S.writeInputBlock(options);
52325239
S.writeSIL(SILMod, options.SerializeAllSIL);
52335240
S.writeAST(DC);
5234-
if (options.ExperimentalCrossModuleIncrementalInfo) {
5235-
S.writeIncrementalInfo(DepGraph);
5241+
if (options.ExperimentalCrossModuleIncrementalInfo && DepGraph) {
5242+
fine_grained_dependencies::writeFineGrainedDependencyGraph(
5243+
S.Out, *DepGraph, /*standalone*/ false);
52365244
}
52375245
}
52385246

lib/Serialization/SerializeIncremental.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -module-name EmbeddedIncremental -o %t/EmbeddedIncremental~partial.swiftmodule -primary-file %s
3+
// RUN: %target-swift-frontend -enable-experimental-cross-module-incremental-build -emit-module -module-name EmbeddedIncremental -o %t/EmbeddedIncremental.swiftmodule %t/EmbeddedIncremental~partial.swiftmodule
4+
// RUN: llvm-bcanalyzer -dump %t/EmbeddedIncremental.swiftmodule | %FileCheck %s --dump-input=always
5+
6+
public struct AnyWindows {}
7+
8+
// CHECK: <INCREMENTAL_INFORMATION_BLOCK
9+
// CHECK: </INCREMENTAL_INFORMATION_BLOCK>

0 commit comments

Comments
 (0)