Skip to content

Commit 9fa5aa2

Browse files
[Caching] Re-associate diagnostics cache key with InputFile
Change how cached diagnostics are stored inside the CAS. It used to be stored as a standalone entry for a frontend invocation in the cache and now it is switched to be associated with input files, stored together with other outputs like object files, etc. This enables cleaner Cache Replay APIs and future cached diagnostics that can be splitted up by file contribution.
1 parent aa6a588 commit 9fa5aa2

File tree

5 files changed

+47
-40
lines changed

5 files changed

+47
-40
lines changed

lib/Frontend/CASOutputBackends.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ void SwiftCASOutputBackend::Implementation::initBackend(
157157
});
158158
};
159159
llvm::for_each(InputsAndOutputs.getAllInputs(), addInput);
160+
161+
// FIXME: The current diagnostics is associated with the first input file.
162+
OutputToInputMap.insert(
163+
{"<cached-diagnostics>",
164+
{InputsAndOutputs.getAllInputs()[0], file_types::TY_CachedDiagnostics}});
160165
}
161166

162167
Error SwiftCASOutputBackend::Implementation::storeImpl(

lib/Frontend/CachedDiagnostics.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,13 @@ CachingDiagnosticsProcessor::CachingDiagnosticsProcessor(
772772
}
773773

774774
StringRef Content = Compression.empty() ? Output : toStringRef(Compression);
775-
// Store CachedDiagnostics in the CAS/Cache. There is no real associated
776-
// inputs.
775+
// Store CachedDiagnostics in the CAS/Cache.
776+
// FIXME: Currently, it is associated with first input file.
777777
auto Err = Instance.getCASOutputBackend().storeCachedDiagnostics(
778-
"<cached-diagnostics>", Content);
778+
Instance.getInvocation()
779+
.getFrontendOptions()
780+
.InputsAndOutputs.getFilenameOfFirstInput(),
781+
Content);
779782

780783
if (Err) {
781784
Instance.getDiags().diagnose(SourceLoc(), diag::error_cas,

lib/Frontend/CachingUtils.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,15 @@ bool replayCachedCompilerOutputs(
209209
Outputs.try_emplace(ID, File);
210210
});
211211

212-
// Nothing to replay.
213-
if (Outputs.empty())
214-
return;
212+
// Add cached diagnostic entry for lookup. Output path doesn't matter here.
213+
Outputs.try_emplace(file_types::ID::TY_CachedDiagnostics,
214+
"<cached-diagnostics>");
215215

216216
return replayOutputsForInputFile(InputPath, Outputs);
217217
};
218218

219219
llvm::for_each(InputsAndOutputs.getAllInputs(), replayOutputFromInput);
220220

221-
replayOutputsForInputFile(
222-
"<cached-diagnostics>",
223-
{{file_types::ID::TY_CachedDiagnostics, "<cached-diagnostics>"}});
224-
225221
if (!CanReplayAllOutput)
226222
return false;
227223

test/CAS/Inputs/ExtractOutputKey.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111

1212
with open(input_json, 'r') as file:
13-
outputs = json.load(file)
14-
for output in outputs:
15-
if output['OutputPath'] != output_path:
16-
continue
17-
print(output['CacheKey'])
13+
entries = json.load(file)
14+
for entry in entries:
15+
for output in entry["Outputs"]:
16+
if output['Path'] != output_path:
17+
continue
18+
print(entry['CacheKey'])

test/CAS/cache_key_compute.swift

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,34 @@
5050
// RUN: %target-swift-frontend -cache-compile-job %s -emit-module -c -emit-dependencies \
5151
// RUN: -emit-tbd -emit-tbd-path %t/test.tbd -o %t/test.o -allow-unstable-cache-key-for-testing | %FileCheck %s --check-prefix=CHECK --check-prefix=PLUGIN
5252

53-
// CHECK: test.o
54-
// CHECK-NEXT: "OutputKind": "object"
55-
// CHECK-NEXT: "Input"
53+
// CHECK: "Input": "{{.*}}{{/|\\}}cache_key_compute.swift"
5654
// CHECK-NEXT: "CacheKey"
5755
// PLUGIN-SAME: myfirst-llvmcas://
5856

59-
// CHECK: test.swiftmodule
60-
// CHECK-NEXT: "OutputKind": "swiftmodule"
61-
// CHECK-NEXT: "Input"
62-
// CHECK-NEXT: "CacheKey"
63-
// PLUGIN-SAME: myfirst-llvmcas://
64-
65-
// CHECK: test.d
66-
// CHECK-NEXT: "OutputKind": "dependencies"
67-
// CHECK-NEXT: "Input"
68-
// CHECK-NEXT: "CacheKey"
69-
// PLUGIN-SAME: myfirst-llvmcas://
57+
// CHECK-NEXT: "Outputs": [
58+
// CHECK-NEXT: {
59+
// CHECK-NEXT: "Kind": "object",
60+
// CHECK-NEXT: "Path":
61+
// CHECK-SAME: test.o
62+
// CHECK-NEXT: },
63+
// CHECK-NEXT: {
64+
// CHECK-NEXT: "Kind": "swiftmodule",
65+
// CHECK-NEXT: "Path":
66+
// CHECK-SAME: test.swiftmodule
67+
// CHECK-NEXT: },
68+
// CHECK-NEXT: {
69+
// CHECK-NEXT: "Kind": "dependencies",
70+
// CHECK-NEXT: "Path":
71+
// CHECK-SAME: test.d
72+
// CHECK-NEXT: },
73+
// CHECK-NEXT: {
74+
// CHECK-NEXT: "Kind": "tbd",
75+
// CHECK-NEXT: "Path":
76+
// CHECK-SAME: test.tbd
77+
// CHECK-NEXT: },
78+
// CHECK-NEXT: {
79+
// CHECK-NEXT: "Kind": "cached-diagnostics",
80+
// CHECK-NEXT: "Path": "<cached-diagnostics>"
81+
// CHECK-NEXT: }
82+
// CHECK-NEXT: ]
7083

71-
// CHECK: test.tbd
72-
// CHECK-NEXT: "OutputKind": "tbd"
73-
// CHECK-NEXT: "Input"
74-
// CHECK-NEXT: "CacheKey"
75-
// PLUGIN-SAME: myfirst-llvmcas://
76-
77-
// CHECK: <cached-diagnostics>
78-
// CHECK-NEXT: "OutputKind": "cached-diagnostics"
79-
// CHECK-NEXT: "Input": "<cached-diagnostics>"
80-
// CHECK-NEXT: "CacheKey"
81-
// PLUGIN-SAME: myfirst-llvmcas://

0 commit comments

Comments
 (0)