Skip to content

Commit f073d16

Browse files
[Caching] Fix symbol graph output from driver APIs
Add the logics to replay symbol graph output from driver APIs.
1 parent 2fd7687 commit f073d16

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

lib/Tooling/libSwiftScan/SwiftCaching.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/Option/Options.h"
3434
#include "clang/CAS/CASOptions.h"
3535
#include "clang/Frontend/CompileJobCacheResult.h"
36+
#include "clang/Frontend/FrontendOptions.h"
3637
#include "llvm/ADT/ArrayRef.h"
3738
#include "llvm/ADT/IntrusiveRefCntPtr.h"
3839
#include "llvm/ADT/SmallVector.h"
@@ -933,6 +934,7 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
933934
Outputs.try_emplace(ID, File);
934935
});
935936
Outputs.try_emplace(file_types::TY_CachedDiagnostics, "<cached-diagnostics>");
937+
Outputs.try_emplace(file_types::ID::TY_SymbolGraphFile, "<symbol-graph>");
936938

937939
// Load all the output buffer.
938940
bool Remarks = Instance.Invocation.getCASOptions().EnableCachingRemarks;
@@ -944,6 +946,8 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
944946
SmallVector<OutputEntry> OutputProxies;
945947
std::optional<llvm::cas::ObjectProxy> DiagnosticsOutput;
946948
bool UseCASBackend = Invocation.getIRGenOptions().UseCASBackend;
949+
const FrontendOptions &FrontendOpts =
950+
Inst.getInvocation().getFrontendOptions();
947951

948952
swift::cas::CachedResultLoader Loader(CAS, Comp.Output);
949953
if (auto Err = Loader.replay(
@@ -960,6 +964,32 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
960964
if (Kind == file_types::ID::TY_CachedDiagnostics) {
961965
assert(!DiagnosticsOutput && "more than 1 diagnostics found");
962966
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;
963993
} else
964994
OutputProxies.emplace_back(
965995
OutputEntry{OutputPath->second, std::move(*Proxy), Kind});
@@ -1010,10 +1040,9 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
10101040
Inst.getDiags().diagnose(SourceLoc(), diag::error_mccas,
10111041
toString(std::move(E)));
10121042
} else if (Output.Kind == file_types::ID::TY_Dependencies) {
1013-
if (emitMakeDependenciesFromSerializedBuffer(
1014-
Output.Proxy.getData(), *File,
1015-
Inst.getInvocation().getFrontendOptions(), Input,
1016-
Inst.getDiags()))
1043+
if (emitMakeDependenciesFromSerializedBuffer(Output.Proxy.getData(),
1044+
*File, FrontendOpts, Input,
1045+
Inst.getDiags()))
10171046
Inst.getDiags().diagnose(SourceLoc(), diag::cache_replay_failed,
10181047
"failed to emit dependency file");
10191048
} else

test/CAS/symbol-graph.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@
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)