Skip to content

Commit e91f6ca

Browse files
akyrtzibenlangmuir
authored andcommitted
[clang][cas] Move CompileJobCache into Frontend library
(cherry picked from commit b36bc6f)
1 parent c0d24c6 commit e91f6ca

File tree

7 files changed

+1022
-979
lines changed

7 files changed

+1022
-979
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//===- CompileJobCache.h ----------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_FRONTEND_COMPILEJOBCACHE_H
10+
#define LLVM_CLANG_FRONTEND_COMPILEJOBCACHE_H
11+
12+
#include "clang/Frontend/CompileJobCacheResult.h"
13+
14+
namespace clang {
15+
16+
class CompilerInstance;
17+
class DiagnosticsEngine;
18+
19+
// Manage caching and replay of compile jobs.
20+
//
21+
// The high-level model is:
22+
//
23+
// 1. Extract options from the CompilerInvocation:
24+
// - that can be simulated and
25+
// - that don't affect the compile job's result.
26+
// 2. Canonicalize the options extracted in (1).
27+
// 3. Compute the result of the compile job using the canonicalized
28+
// CompilerInvocation, with hooks installed to redirect outputs and
29+
// enable live-streaming of a running compile job to stdout or stderr.
30+
// - Compute a cache key.
31+
// - Check the cache, and run the compile job if there's a cache miss.
32+
// - Store the result of the compile job in the cache.
33+
// 4. Replay the compile job, using the options extracted in (1).
34+
//
35+
// An example (albeit not yet implemented) is handling options controlling
36+
// output of diagnostics. The CompilerInvocation can be canonicalized to
37+
// serialize the diagnostics to a virtual path (<output>.diag or something).
38+
//
39+
// - On a cache miss, the compile job runs, and the diagnostics are
40+
// serialized and stored in the cache per the canonicalized options
41+
// from (2).
42+
// - Either way, the diagnostics are replayed according to the options
43+
// extracted from (1) during (4).
44+
//
45+
// The above will produce the correct output for diagnostics, but the experience
46+
// will be degraded in the common command-line case (emitting to stderr)
47+
// because the diagnostics will not be streamed live. This can be improved:
48+
//
49+
// - Change (3) to accept a hook: a DiagnosticsConsumer that diagnostics
50+
// are mirrored to (in addition to canonicalized options from (2)).
51+
// - If diagnostics would be live-streamed, send in a diagnostics consumer
52+
// that matches (1). Otherwise, send in an IgnoringDiagnosticsConsumer.
53+
// - In step (4), only skip replaying the diagnostics if they were already
54+
// handled.
55+
class CompileJobCache {
56+
public:
57+
CompileJobCache();
58+
~CompileJobCache();
59+
60+
using OutputKind = clang::cas::CompileJobCacheResult::OutputKind;
61+
62+
StringRef getPathForOutputKind(OutputKind Kind);
63+
64+
/// Canonicalize \p Clang.
65+
///
66+
/// \returns status if should exit immediately, otherwise None.
67+
///
68+
/// TODO: Refactor \a cc1_main() so that instead this canonicalizes the
69+
/// CompilerInvocation before Clang gets access to command-line arguments, to
70+
/// control what might leak.
71+
std::optional<int> initialize(CompilerInstance &Clang);
72+
73+
/// Try looking up a cached result and replaying it.
74+
///
75+
/// \returns status if should exit immediately, otherwise None.
76+
std::optional<int> tryReplayCachedResult(CompilerInstance &Clang);
77+
78+
/// Finish writing outputs from a computed result, after a cache miss.
79+
///
80+
/// \returns true if finished successfully.
81+
bool finishComputedResult(CompilerInstance &Clang, bool Success);
82+
83+
class CachingOutputs;
84+
85+
private:
86+
int reportCachingBackendError(DiagnosticsEngine &Diag, llvm::Error &&E);
87+
88+
bool CacheCompileJob = false;
89+
bool DisableCachedCompileJobReplay = false;
90+
91+
std::shared_ptr<llvm::cas::ObjectStore> CAS;
92+
std::shared_ptr<llvm::cas::ActionCache> Cache;
93+
llvm::Optional<llvm::cas::CASID> ResultCacheKey;
94+
95+
std::unique_ptr<CachingOutputs> CacheBackend;
96+
};
97+
98+
} // namespace clang
99+
100+
#endif // LLVM_CLANG_FRONTEND_COMPILEJOBCACHE_H

clang/lib/Frontend/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ set(LLVM_LINK_COMPONENTS
66
CAS
77
Option
88
ProfileData
9+
RemoteCachingService
910
Support
1011
)
1112

1213
add_clang_library(clangFrontend
1314
ASTConsumers.cpp
1415
ASTMerge.cpp
1516
ASTUnit.cpp
17+
CachedDiagnostics.cpp
1618
CASDependencyCollector.cpp
1719
ChainedDiagnosticConsumer.cpp
1820
ChainedIncludesSource.cpp
21+
CompileJobCache.cpp
1922
CompileJobCacheKey.cpp
2023
CompileJobCacheResult.cpp
2124
CompilerInstance.cpp

0 commit comments

Comments
 (0)