Skip to content

Commit 245ace3

Browse files
authored
Merge pull request #34196 from CodaFi/crossbeam
Turn On Cross-Module Incremental Builds!
2 parents 8ebfd9a + 00d8e3c commit 245ace3

25 files changed

+294
-78
lines changed

include/swift/AST/FineGrainedDependencyFormat.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,32 @@ bool writeFineGrainedDependencyGraphToPath(DiagnosticEngine &diags,
135135
llvm::StringRef path,
136136
const SourceFileDepGraph &g);
137137

138+
/// Enumerates the supported set of purposes for writing out or reading in
139+
/// swift dependency information into a file. These can be used to influence
140+
/// the structure of the resulting data that is produced by the serialization
141+
/// machinery defined here.
142+
enum class Purpose : bool {
143+
/// Write out fine grained dependency metadata suitable for embedding in
144+
/// \c .swiftmodule file.
145+
///
146+
/// The resulting metadata does not contain the usual block descriptor header
147+
/// nor does it contain a leading magic signature, which would otherwise
148+
/// disrupt clients and tools that do not expect them to be present such as
149+
/// llvm-bcanalyzer.
150+
ForSwiftModule = false,
151+
/// Write out fine grained dependency metadata suitable for a standalone
152+
/// \c .swiftdeps file.
153+
///
154+
/// The resulting metadata will contain a leading magic signature and block
155+
/// descriptor header.
156+
ForSwiftDeps = true,
157+
};
158+
159+
/// Tries to write out the given dependency graph with the given
160+
/// bitstream writer.
138161
void writeFineGrainedDependencyGraph(llvm::BitstreamWriter &Out,
139-
const SourceFileDepGraph &g);
162+
const SourceFileDepGraph &g,
163+
Purpose purpose);
140164

141165
} // namespace fine_grained_dependencies
142166
} // namespace swift

include/swift/Driver/FineGrainedDependencyDriverGraph.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ class ModuleDepGraph {
278278
assert(swiftDeps.hasValue() && "Don't call me for expats.");
279279
auto iter = jobsBySwiftDeps.find(swiftDeps.getValue());
280280
assert(iter != jobsBySwiftDeps.end() && "All jobs should be tracked.");
281-
assert(getSwiftDeps(iter->second) == swiftDeps.getValue() &&
282-
"jobsBySwiftDeps should be inverse of getSwiftDeps.");
283281
return iter->second;
284282
}
285283

include/swift/Option/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ def experimental_cxx_stdlib :
573573

574574
def enable_experimental_cross_module_incremental_build :
575575
Flag<["-"], "enable-experimental-cross-module-incremental-build">,
576+
Flags<[FrontendOption]>,
576577
HelpText<"(experimental) Enable cross-module incremental build metadata and "
577578
"driver scheduling">;
578579

lib/AST/FineGrainedDependencyFormat.cpp

Lines changed: 50 additions & 25 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, Purpose purpose);
4848
bool readFineGrainedDependencyGraphFromSwiftModule(SourceFileDepGraph &g);
4949
};
5050

@@ -151,14 +151,24 @@ 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+
Purpose purpose) {
155156
using namespace record_block;
156157

157-
if (readSignature())
158-
return true;
158+
switch (purpose) {
159+
case Purpose::ForSwiftDeps:
160+
if (readSignature())
161+
return true;
159162

160-
if (enterTopLevelBlock())
161-
return true;
163+
if (enterTopLevelBlock())
164+
return true;
165+
LLVM_FALLTHROUGH;
166+
case Purpose::ForSwiftModule:
167+
// N.B. Incremental metadata embedded in swiftmodule files does not have
168+
// a leading signature, and its top-level block has already been
169+
// consumed by the time we get here.
170+
break;
171+
}
162172

163173
if (readMetadata())
164174
return true;
@@ -260,7 +270,7 @@ bool Deserializer::readFineGrainedDependencyGraph(SourceFileDepGraph &g) {
260270
bool swift::fine_grained_dependencies::readFineGrainedDependencyGraph(
261271
llvm::MemoryBuffer &buffer, SourceFileDepGraph &g) {
262272
Deserializer deserializer(buffer.getMemBufferRef());
263-
return deserializer.readFineGrainedDependencyGraph(g);
273+
return deserializer.readFineGrainedDependencyGraph(g, Purpose::ForSwiftDeps);
264274
}
265275

266276
bool swift::fine_grained_dependencies::readFineGrainedDependencyGraph(
@@ -322,7 +332,8 @@ class Serializer {
322332
Serializer(llvm::BitstreamWriter &ExistingOut) : Out(ExistingOut) {}
323333

324334
public:
325-
void writeFineGrainedDependencyGraph(const SourceFileDepGraph &g);
335+
void writeFineGrainedDependencyGraph(const SourceFileDepGraph &g,
336+
Purpose purpose);
326337
};
327338

328339
} // end namespace
@@ -382,11 +393,21 @@ void Serializer::writeMetadata() {
382393
}
383394

384395
void
385-
Serializer::writeFineGrainedDependencyGraph(const SourceFileDepGraph &g) {
386-
writeSignature();
387-
writeBlockInfoBlock();
396+
Serializer::writeFineGrainedDependencyGraph(const SourceFileDepGraph &g,
397+
Purpose purpose) {
398+
unsigned blockID = 0;
399+
switch (purpose) {
400+
case Purpose::ForSwiftDeps:
401+
writeSignature();
402+
writeBlockInfoBlock();
403+
blockID = RECORD_BLOCK_ID;
404+
break;
405+
case Purpose::ForSwiftModule:
406+
blockID = INCREMENTAL_INFORMATION_BLOCK_ID;
407+
break;
408+
}
388409

389-
llvm::BCBlockRAII restoreBlock(Out, RECORD_BLOCK_ID, 8);
410+
llvm::BCBlockRAII restoreBlock(Out, blockID, 8);
390411

391412
using namespace record_block;
392413

@@ -468,9 +489,10 @@ unsigned Serializer::getIdentifier(StringRef str) {
468489
}
469490

470491
void swift::fine_grained_dependencies::writeFineGrainedDependencyGraph(
471-
llvm::BitstreamWriter &Out, const SourceFileDepGraph &g) {
492+
llvm::BitstreamWriter &Out, const SourceFileDepGraph &g,
493+
Purpose purpose) {
472494
Serializer serializer{Out};
473-
serializer.writeFineGrainedDependencyGraph(g);
495+
serializer.writeFineGrainedDependencyGraph(g, purpose);
474496
}
475497

476498
bool swift::fine_grained_dependencies::writeFineGrainedDependencyGraphToPath(
@@ -480,7 +502,7 @@ bool swift::fine_grained_dependencies::writeFineGrainedDependencyGraphToPath(
480502
return withOutputFile(diags, path, [&](llvm::raw_ostream &out) {
481503
SmallVector<char, 0> Buffer;
482504
llvm::BitstreamWriter Writer{Buffer};
483-
writeFineGrainedDependencyGraph(Writer, g);
505+
writeFineGrainedDependencyGraph(Writer, g, Purpose::ForSwiftDeps);
484506
out.write(Buffer.data(), Buffer.size());
485507
out.flush();
486508
return false;
@@ -516,7 +538,7 @@ static bool enterTopLevelModuleBlock(llvm::BitstreamCursor &cursor, unsigned ID,
516538
if (next.Kind != llvm::BitstreamEntry::SubBlock)
517539
return false;
518540

519-
if (next.ID == RECORD_BLOCK_ID) {
541+
if (next.ID == llvm::bitc::BLOCKINFO_BLOCK_ID) {
520542
if (shouldReadBlockInfo) {
521543
if (!cursor.ReadBlockInfoBlock())
522544
return false;
@@ -531,7 +553,6 @@ static bool enterTopLevelModuleBlock(llvm::BitstreamCursor &cursor, unsigned ID,
531553
return false;
532554

533555
if (llvm::Error Err = cursor.EnterSubBlock(ID)) {
534-
// FIXME this drops the error on the floor.
535556
consumeError(std::move(Err));
536557
return false;
537558
}
@@ -549,18 +570,18 @@ bool swift::fine_grained_dependencies::
549570
bool Deserializer::readFineGrainedDependencyGraphFromSwiftModule(
550571
SourceFileDepGraph &g) {
551572
if (!checkModuleSignature(Cursor, {0xE2, 0x9C, 0xA8, 0x0E}) ||
552-
!enterTopLevelModuleBlock(Cursor, RECORD_BLOCK_ID, false)) {
553-
return false;
573+
!enterTopLevelModuleBlock(Cursor, llvm::bitc::FIRST_APPLICATION_BLOCKID, false)) {
574+
return true;
554575
}
555576

556577
llvm::BitstreamEntry topLevelEntry;
557-
578+
bool DidNotReadFineGrainedDependencies = true;
558579
while (!Cursor.AtEndOfStream()) {
559580
llvm::Expected<llvm::BitstreamEntry> maybeEntry =
560581
Cursor.advance(llvm::BitstreamCursor::AF_DontPopBlockAtEnd);
561582
if (!maybeEntry) {
562583
consumeError(maybeEntry.takeError());
563-
return false;
584+
return true;
564585
}
565586
topLevelEntry = maybeEntry.get();
566587
if (topLevelEntry.Kind != llvm::BitstreamEntry::SubBlock)
@@ -571,21 +592,25 @@ bool Deserializer::readFineGrainedDependencyGraphFromSwiftModule(
571592
if (llvm::Error Err =
572593
Cursor.EnterSubBlock(INCREMENTAL_INFORMATION_BLOCK_ID)) {
573594
consumeError(std::move(Err));
574-
return false;
595+
return true;
596+
}
597+
if (readFineGrainedDependencyGraph(g, Purpose::ForSwiftModule)) {
598+
break;
575599
}
576-
readFineGrainedDependencyGraph(g);
600+
601+
DidNotReadFineGrainedDependencies = false;
577602
break;
578603
}
579604

580605
default:
581606
// Unknown top-level block, possibly for use by a future version of the
582607
// module format.
583608
if (Cursor.SkipBlock()) {
584-
return false;
609+
return true;
585610
}
586611
break;
587612
}
588613
}
589614

590-
return false;
615+
return DidNotReadFineGrainedDependencies;
591616
}

lib/Driver/Compilation.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,10 +1161,6 @@ namespace driver {
11611161
continue;
11621162
}
11631163

1164-
// Is this module out of date? If not, just keep searching.
1165-
if (Comp.getLastBuildTime() >= depStatus.getLastModificationTime())
1166-
continue;
1167-
11681164
// Can we run a cross-module incremental build at all? If not, fallback.
11691165
if (!Comp.getEnableCrossModuleIncrementalBuild()) {
11701166
fallbackToExternalBehavior(external);

lib/Driver/FineGrainedDependencyDriverGraph.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ ModuleDepGraph::Changes ModuleDepGraph::loadFromSwiftModuleBuffer(
120120
SourceFileDepGraph::loadFromSwiftModuleBuffer(buffer);
121121
if (!sourceFileDepGraph)
122122
return None;
123-
registerJob(Cmd);
123+
jobsBySwiftDeps[buffer.getBufferIdentifier().str()] = Cmd;
124124
auto changes = integrate(*sourceFileDepGraph, buffer.getBufferIdentifier());
125125
if (verifyFineGrainedDependencyGraphAfterEveryImport)
126126
verify();
@@ -445,11 +445,11 @@ bool ModuleDepGraph::recordWhatUseDependsUpon(
445445
StringRef externalSwiftDeps = def->getKey().getName();
446446
if (def->getKey().getKind() == NodeKind::externalDepend) {
447447
externalDependencies.insert(externalSwiftDeps.str());
448+
useHasNewExternalDependency = true;
448449
} else if (def->getKey().getKind() ==
449450
NodeKind::incrementalExternalDepend) {
450451
incrementalExternalDependencies.insert(externalSwiftDeps.str());
451452
}
452-
useHasNewExternalDependency = true;
453453
}
454454
});
455455
return useHasNewExternalDependency;

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, fine_grained_dependencies::Purpose::ForSwiftModule);
52365244
}
52375245
}
52385246

lib/Serialization/SerializeIncremental.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -973,13 +973,6 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
973973
isFramework, isSystemModule)) {
974974
return nullptr;
975975
}
976-
if (dependencyTracker) {
977-
// Don't record cached artifacts as dependencies.
978-
StringRef DepPath = moduleInputBuffer->getBufferIdentifier();
979-
if (!isCached(DepPath)) {
980-
dependencyTracker->addDependency(DepPath, /*isSystem=*/false);
981-
}
982-
}
983976

984977
assert(moduleInputBuffer);
985978

@@ -997,6 +990,18 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
997990
} else {
998991
M->setFailedToLoad();
999992
}
993+
994+
if (dependencyTracker && file) {
995+
auto DepPath = file->getFilename();
996+
// Don't record cached artifacts as dependencies.
997+
if (!isCached(DepPath)) {
998+
if (M->hasIncrementalInfo()) {
999+
dependencyTracker->addIncrementalDependency(DepPath);
1000+
} else {
1001+
dependencyTracker->addDependency(DepPath, /*isSystem=*/false);
1002+
}
1003+
}
1004+
}
10001005
return M;
10011006
}
10021007

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"A.swift": {
3+
"object": "./A.o",
4+
"swift-dependencies": "./A.swiftdeps",
5+
"swiftmodule": "./A~partial.swiftmodule",
6+
"swiftdoc": "./A.swiftdoc",
7+
},
8+
"": {
9+
"swift-dependencies": "./A~buildrecord.swiftdeps"
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import B
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"B.swift": {
3+
"object": "./B.o",
4+
"swift-dependencies": "./B.swiftdeps",
5+
"swiftmodule": "./B~partial.swiftmodule",
6+
"swiftdoc": "./B.swiftdoc",
7+
},
8+
"": {
9+
"swift-dependencies": "./B~buildrecord.swiftdeps"
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import C
2+
3+
public func fromB() {
4+
return fromC()
5+
}

0 commit comments

Comments
 (0)