Skip to content

Commit ffff34b

Browse files
Merge pull request #78279 from cachemeifyoucan/eng/PR-141567785
[Caching] Fix the cache replay bugs from driver C APIs
2 parents 9843de2 + f073d16 commit ffff34b

File tree

7 files changed

+95
-21
lines changed

7 files changed

+95
-21
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class IRGenOptions {
306306

307307
/// Whether we're generating IR for the JIT.
308308
unsigned UseJIT : 1;
309-
309+
310310
/// Whether we should run LLVM optimizations after IRGen.
311311
unsigned DisableLLVMOptzns : 1;
312312

@@ -377,7 +377,7 @@ class IRGenOptions {
377377
/// Force public linkage for private symbols. Used only by the LLDB
378378
/// expression evaluator.
379379
unsigned ForcePublicLinkage : 1;
380-
380+
381381
/// Force lazy initialization of class metadata
382382
/// Used on Windows to avoid cross-module references.
383383
unsigned LazyInitializeClassMetadata : 1;
@@ -439,7 +439,7 @@ class IRGenOptions {
439439
/// Whether to disable shadow copies for local variables on the stack. This is
440440
/// only used for testing.
441441
unsigned DisableDebuggerShadowCopies : 1;
442-
442+
443443
/// Whether to disable using mangled names for accessing concrete type metadata.
444444
unsigned DisableConcreteTypeMetadataMangledNameAccessors : 1;
445445

@@ -524,7 +524,7 @@ class IRGenOptions {
524524
};
525525

526526
TypeInfoDumpFilter TypeInfoFilter;
527-
527+
528528
/// Pull in runtime compatibility shim libraries by autolinking.
529529
std::optional<llvm::VersionTuple> AutolinkRuntimeCompatibilityLibraryVersion;
530530
std::optional<llvm::VersionTuple>
@@ -543,13 +543,13 @@ class IRGenOptions {
543543
llvm::CallingConv::ID PlatformCCallingConvention;
544544

545545
/// Use CAS based object format as the output.
546-
bool UseCASBackend;
546+
bool UseCASBackend = false;
547547

548548
/// The output mode for the CAS Backend.
549549
llvm::CASBackendMode CASObjMode;
550550

551551
/// Emit a .casid file next to the object file if CAS Backend is used.
552-
bool EmitCASIDFile;
552+
bool EmitCASIDFile = false;
553553

554554
/// Paths to the pass plugins registered via -load-pass-plugin.
555555
std::vector<std::string> LLVMPassPlugins;

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ class FrontendOptions {
137137
/// The module for which we should verify all of the generic signatures.
138138
std::string VerifyGenericSignaturesInModule;
139139

140-
/// Emit a .casid file next to the object file if CAS Backend is used.
141-
bool EmitCASIDFile = false;
142-
143140
/// CacheReplay PrefixMap.
144141
std::vector<std::string> CacheReplayPrefixMap;
145142

@@ -389,7 +386,7 @@ class FrontendOptions {
389386
/// are errors. The resulting serialized AST may include errors types and
390387
/// skip nodes entirely, depending on the errors involved.
391388
bool AllowModuleWithCompilerErrors = false;
392-
389+
393390
/// Whether or not the compiler must be strict in ensuring that implicit downstream
394391
/// module dependency build tasks must inherit the parent compiler invocation's context,
395392
/// such as `-Xcc` flags, etc.
@@ -519,7 +516,7 @@ class FrontendOptions {
519516

520517
/// Whether we're configured to track system intermodule dependencies.
521518
bool shouldTrackSystemDependencies() const;
522-
519+
523520
/// Whether we are configured with -typecheck or -typecheck-module-from-interface actuin
524521
bool isTypeCheckAction() const;
525522

@@ -535,7 +532,7 @@ class FrontendOptions {
535532
///
536533
/// \sa SymbolGraphASTWalker
537534
std::string SymbolGraphOutputDir;
538-
535+
539536
/// Whether to emit doc comment information in symbol graphs for symbols
540537
/// which are inherited through classes or default implementations.
541538
bool SkipInheritedDocs = false;

include/swift/Option/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ def emit_symbol_graph: Flag<["-"], "emit-symbol-graph">,
16621662

16631663
def emit_symbol_graph_dir : Separate<["-"], "emit-symbol-graph-dir">,
16641664
Flags<[FrontendOption, NoInteractiveOption, ArgumentIsPath,
1665-
SupplementaryOutput, HelpHidden]>,
1665+
SupplementaryOutput, HelpHidden, CacheInvariant]>,
16661666
HelpText<"Emit a symbol graph to directory <dir>">,
16671667
MetaVarName<"<dir>">;
16681668

lib/Frontend/CachingUtils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,11 @@ bool replayCachedCompilerOutputs(
325325
}
326326
} else if (Output.Kind == file_types::ID::TY_Dependencies) {
327327
if (emitMakeDependenciesFromSerializedBuffer(
328-
Output.Proxy.getData(), *File, Opts, Output.Input, Diag))
328+
Output.Proxy.getData(), *File, Opts, Output.Input, Diag)) {
329+
Diag.diagnose(SourceLoc(), diag::cache_replay_failed,
330+
"failed to emit dependency file");
329331
return false;
332+
}
330333
} else
331334
*File << Output.Proxy.getData();
332335

lib/Tooling/libSwiftScan/SwiftCaching.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
#include "swift/Frontend/CompileJobCacheResult.h"
2929
#include "swift/Frontend/DiagnosticHelper.h"
3030
#include "swift/Frontend/Frontend.h"
31+
#include "swift/Frontend/MakeStyleDependencies.h"
3132
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
3233
#include "swift/Option/Options.h"
3334
#include "clang/CAS/CASOptions.h"
3435
#include "clang/Frontend/CompileJobCacheResult.h"
36+
#include "clang/Frontend/FrontendOptions.h"
3537
#include "llvm/ADT/ArrayRef.h"
3638
#include "llvm/ADT/IntrusiveRefCntPtr.h"
3739
#include "llvm/ADT/SmallVector.h"
@@ -932,17 +934,20 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
932934
Outputs.try_emplace(ID, File);
933935
});
934936
Outputs.try_emplace(file_types::TY_CachedDiagnostics, "<cached-diagnostics>");
937+
Outputs.try_emplace(file_types::ID::TY_SymbolGraphFile, "<symbol-graph>");
935938

936939
// Load all the output buffer.
937940
bool Remarks = Instance.Invocation.getCASOptions().EnableCachingRemarks;
938941
struct OutputEntry {
939942
std::string Path;
940943
llvm::cas::ObjectProxy Proxy;
944+
file_types::ID Kind;
941945
};
942946
SmallVector<OutputEntry> OutputProxies;
943947
std::optional<llvm::cas::ObjectProxy> DiagnosticsOutput;
944948
bool UseCASBackend = Invocation.getIRGenOptions().UseCASBackend;
945-
std::string ObjFile;
949+
const FrontendOptions &FrontendOpts =
950+
Inst.getInvocation().getFrontendOptions();
946951

947952
swift::cas::CachedResultLoader Loader(CAS, Comp.Output);
948953
if (auto Err = Loader.replay(
@@ -956,15 +961,38 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
956961
if (!Proxy)
957962
return Proxy.takeError();
958963

959-
if (Kind == file_types::ID::TY_Object && UseCASBackend)
960-
ObjFile = OutputPath->second;
961-
962964
if (Kind == file_types::ID::TY_CachedDiagnostics) {
963965
assert(!DiagnosticsOutput && "more than 1 diagnostics found");
964966
DiagnosticsOutput = std::move(*Proxy);
967+
} else if (Kind == file_types::ID::TY_SymbolGraphFile &&
968+
!FrontendOpts.SymbolGraphOutputDir.empty()) {
969+
auto Err = Proxy->forEachReference([&](llvm::cas::ObjectRef Ref)
970+
-> llvm::Error {
971+
auto Proxy = CAS.getProxy(Ref);
972+
if (!Proxy)
973+
return Proxy.takeError();
974+
auto PathRef = Proxy->getReference(0);
975+
auto ContentRef = Proxy->getReference(1);
976+
auto Path = CAS.getProxy(PathRef);
977+
auto Content = CAS.getProxy(ContentRef);
978+
if (!Path)
979+
return Path.takeError();
980+
if (!Content)
981+
return Content.takeError();
982+
983+
SmallString<128> OutputPath(FrontendOpts.SymbolGraphOutputDir);
984+
llvm::sys::path::append(OutputPath, Path->getData());
985+
986+
OutputProxies.emplace_back(OutputEntry{
987+
std::string(OutputPath), std::move(*Content), Kind});
988+
989+
return Error::success();
990+
});
991+
if (Err)
992+
return Err;
965993
} else
966994
OutputProxies.emplace_back(
967-
OutputEntry{OutputPath->second, std::move(*Proxy)});
995+
OutputEntry{OutputPath->second, std::move(*Proxy), Kind});
968996
return Error::success();
969997
}))
970998
return Err;
@@ -1006,11 +1034,17 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
10061034
auto File = Backend.createFile(Output.Path);
10071035
if (!File)
10081036
return File.takeError();
1009-
if (UseCASBackend && Output.Path == ObjFile) {
1037+
if (UseCASBackend && Output.Kind == file_types::ID::TY_Object) {
10101038
auto Schema = std::make_unique<llvm::mccasformats::v1::MCSchema>(CAS);
10111039
if (auto E = Schema->serializeObjectFile(Output.Proxy, *File))
10121040
Inst.getDiags().diagnose(SourceLoc(), diag::error_mccas,
10131041
toString(std::move(E)));
1042+
} else if (Output.Kind == file_types::ID::TY_Dependencies) {
1043+
if (emitMakeDependenciesFromSerializedBuffer(Output.Proxy.getData(),
1044+
*File, FrontendOpts, Input,
1045+
Inst.getDiags()))
1046+
Inst.getDiags().diagnose(SourceLoc(), diag::cache_replay_failed,
1047+
"failed to emit dependency file");
10141048
} else
10151049
*File << Output.Proxy.getData();
10161050
if (auto E = File->keep())

test/CAS/dependency_file.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@
5656
// RUN: %FileCheck %s --check-prefix=DEPS3 --input-file=%t/main-3.d -DTMP=%t
5757
// DEPS3: [[TMP]]{{/|\\}}main-3.o : [[TMP]]{{/|\\}}main.swift
5858

59+
/// Test replay from driver interface
60+
// RUN: %swift-scan-test -action compute_cache_key_from_index -cas-path %t/cas -input 0 -- \
61+
// RUN: %target-swift-frontend \
62+
// RUN: -c -o %t/main-4.o -cache-compile-job -cas-path %t/cas -Rcache-compile-job \
63+
// RUN: -swift-version 5 -disable-implicit-swift-modules \
64+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
65+
// RUN: -module-name Test -explicit-swift-module-map-file @%t/map.casid \
66+
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t -cache-replay-prefix-map /^sdk=%t/sdk \
67+
// RUN: /^tmp/main.swift @%t/MyApp-1.cmd -emit-dependencies -emit-dependencies-path %t/main-4.d > %t/key.casid
68+
69+
// RUN: %swift-scan-test -action replay_result -cas-path %t/cas -id @%t/key.casid -- \
70+
// RUN: %target-swift-frontend \
71+
// RUN: -c -o %t/main-4.o -cache-compile-job -cas-path %t/cas -Rcache-compile-job \
72+
// RUN: -swift-version 5 -disable-implicit-swift-modules \
73+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
74+
// RUN: -module-name Test -explicit-swift-module-map-file @%t/map.casid \
75+
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t -cache-replay-prefix-map /^sdk=%t/sdk \
76+
// RUN: /^tmp/main.swift @%t/MyApp-1.cmd -emit-dependencies -emit-dependencies-path %t/main-4.d 2>&1 | %FileCheck %s --check-prefix=CACHE-HIT4
77+
// CACHE-HIT4: remark: replay output file '{{.*}}{{/|\\}}main-4.d': key 'llvmcas://{{.*}}'
78+
// RUN: %FileCheck %s --check-prefix=DEPS4 --input-file=%t/main-4.d -DTMP=%t
79+
// DEPS4: [[TMP]]{{/|\\}}main-4.o : [[TMP]]{{/|\\}}main.swift
80+
81+
5982
//--- main.swift
6083
import A
6184

test/CAS/symbol-graph.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,30 @@
2323
// RUN: -parse-stdlib -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
2424
// RUN: %t/main.swift -o %t/Test.swiftmodule -swift-version 5 -cache-compile-job -cas-path %t/cas -I %t/include \
2525
// RUN: -emit-symbol-graph -emit-symbol-graph-dir %t/symbol-graph2 \
26-
// RUN: -emit-module @%t/Test.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-MISS
26+
// RUN: -emit-module @%t/Test.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-HIT
2727

2828
// CACHE-HIT: remark: replay output file '{{.*}}{{/|\\}}symbol-graph2{{/|\\}}Test.symbols.json': key 'llvmcas://{{.*}}'
2929
// CACHE-HIT: remark: replay output file '{{.*}}{{/|\\}}symbol-graph2{{/|\\}}[email protected]': key 'llvmcas://{{.*}}'
3030

3131
// RUN: diff -r -u %t/symbol-graph1 %t/symbol-graph2
3232

33+
/// Test replay from driver interface
34+
// RUN: %swift-scan-test -action compute_cache_key_from_index -cas-path %t/cas -input 0 -- \
35+
// RUN: %target-swift-frontend -module-name Test -module-cache-path %t/clang-module-cache -O \
36+
// RUN: -parse-stdlib -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
37+
// RUN: %t/main.swift -o %t/Test.swiftmodule -swift-version 5 -cache-compile-job -cas-path %t/cas -I %t/include \
38+
// RUN: -emit-symbol-graph -emit-symbol-graph-dir %t/symbol-graph3 \
39+
// RUN: -emit-module @%t/Test.cmd -Rcache-compile-job > %t/key.casid
40+
41+
// RUN: %swift-scan-test -action replay_result -cas-path %t/cas -id @%t/key.casid -- \
42+
// RUN: %target-swift-frontend -module-name Test -module-cache-path %t/clang-module-cache -O \
43+
// RUN: -parse-stdlib -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
44+
// RUN: %t/main.swift -o %t/Test.swiftmodule -swift-version 5 -cache-compile-job -cas-path %t/cas -I %t/include \
45+
// RUN: -emit-symbol-graph -emit-symbol-graph-dir %t/symbol-graph3 \
46+
// RUN: -emit-module @%t/Test.cmd -Rcache-compile-job
47+
48+
// RUN: diff -r -u %t/symbol-graph1 %t/symbol-graph3
49+
3350
//--- include/A.swiftinterface
3451
// swift-interface-format-version: 1.0
3552
// swift-module-flags: -module-name A -O -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib -user-module-version 1.0

0 commit comments

Comments
 (0)