Skip to content

Commit 42c0375

Browse files
committed
[clang][cas] Move some caching env variables from clang-cache to driver
Allows us to use CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS, CLANG_CACHE_REDACT_TIME_MACROS, and CLANG_CACHE_CHECK_REPRODUCIBLE_CACHING_ISSUES without going through clang-cache. For deterministic outputs this calls cc1_main twice. rdar://108161760 (cherry picked from commit a71d588)
1 parent 880cf58 commit 42c0375

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4576,6 +4576,24 @@ void Clang::AddPrefixMappingOptions(const ArgList &Args, ArgStringList &CmdArgs,
45764576
}
45774577
}
45784578

4579+
static void addCachingOptions(ArgStringList &CmdArgs) {
4580+
if (std::getenv("CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS"))
4581+
CmdArgs.push_back("-fcache-disable-replay");
4582+
4583+
if (std::getenv("CLANG_CACHE_REDACT_TIME_MACROS")) {
4584+
// Remove use of these macros to get reproducible outputs. This can
4585+
// accompany CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS to avoid fatal errors
4586+
// when the source uses these macros.
4587+
CmdArgs.push_back("-Wno-builtin-macro-redefined");
4588+
CmdArgs.push_back("-D__DATE__=\"redacted\"");
4589+
CmdArgs.push_back("-D__TIMESTAMP__=\"redacted\"");
4590+
CmdArgs.push_back("-D__TIME__=\"redacted\"");
4591+
}
4592+
4593+
if (std::getenv("CLANG_CACHE_CHECK_REPRODUCIBLE_CACHING_ISSUES"))
4594+
CmdArgs.push_back("-Werror=reproducible-caching");
4595+
}
4596+
45794597
void Clang::ConstructJob(Compilation &C, const JobAction &Job,
45804598
const InputInfo &Output, const InputInfoList &Inputs,
45814599
const ArgList &Args, const char *LinkingOutput) const {
@@ -5945,6 +5963,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
59455963
// Handle -fdepscan-prefix-map-* options
59465964
AddPrefixMappingOptions(Args, CmdArgs, D, Sysroot);
59475965

5966+
// Handle compile caching options.
5967+
addCachingOptions(CmdArgs);
5968+
59485969
// Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
59495970
// that "The compiler can only warn and ignore the option if not recognized".
59505971
// When building with ccache, it will pass -D options to clang even on

clang/test/CAS/test-for-deterministic-outputs.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22

33
// This compiles twice with replay disabled, ensuring that we get the same outputs for the same key.
44

5+
// Under clang-cache
6+
57
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS=1 CLANG_CACHE_REDACT_TIME_MACROS=1 %clang-cache \
68
// RUN: %clang -target x86_64-apple-macos11 -c %s -o %t/t.o -Rcompile-job-cache 2> %t/out.txt
79
// RUN: FileCheck %s --check-prefix=CACHE-SKIPPED --input-file=%t/out.txt
810

11+
// Under clang driver
12+
13+
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS=1 CLANG_CACHE_REDACT_TIME_MACROS=1 \
14+
// RUN: %clang -target x86_64-apple-macos11 -c %s -o %t/t.o -Rcompile-job-cache \
15+
// RUN: -fdepscan=inline -Xclang -fcas-path -Xclang %t/cas 2> %t/out_driver.txt
16+
// RUN: FileCheck %s --check-prefix=CACHE-SKIPPED --input-file=%t/out_driver.txt
17+
918
// CACHE-SKIPPED: remark: compile job cache skipped
1019
// CACHE-SKIPPED: remark: compile job cache skipped
1120

clang/tools/driver/CacheLauncherMode.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,6 @@ static void addLauncherArgs(SmallVectorImpl<const char *> &Args,
163163
ServicePath});
164164
}
165165
Args.append({"-greproducible"});
166-
167-
if (llvm::sys::Process::GetEnv("CLANG_CACHE_REDACT_TIME_MACROS")) {
168-
// Remove use of these macros to get reproducible outputs. This can
169-
// accompany CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS to avoid fatal errors
170-
// when the source uses these macros.
171-
Args.append({"-Wno-builtin-macro-redefined", "-D__DATE__=\"redacted\"",
172-
"-D__TIMESTAMP__=\"redacted\"", "-D__TIME__=\"redacted\""});
173-
}
174-
if (llvm::sys::Process::GetEnv(
175-
"CLANG_CACHE_CHECK_REPRODUCIBLE_CACHING_ISSUES")) {
176-
Args.append({"-Werror=reproducible-caching"});
177-
}
178166
}
179167

180168
static void addScanServerArgs(const char *SocketPath,
@@ -251,15 +239,6 @@ clang::handleClangCacheInvocation(SmallVectorImpl<const char *> &Args,
251239
return None;
252240
}
253241
addLauncherArgs(Args, Saver);
254-
if (llvm::sys::Process::GetEnv("CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS")) {
255-
// Run the compilation twice, without replaying, to check that we get the
256-
// same compilation artifacts for the same key. If they are not the same
257-
// the action cache will trigger a fatal error.
258-
Args.append({"-Xclang", "-fcache-disable-replay"});
259-
int Result = executeAsProcess(Args, Diags);
260-
if (Result != 0)
261-
return Result;
262-
}
263242
return None;
264243
}
265244

clang/tools/driver/driver.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,17 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
366366
ECtx.expandResponseFiles(ArgV);
367367
StringRef Tool = ArgV[1];
368368
void *GetExecutablePathVP = (void *)(intptr_t)GetExecutablePath;
369-
if (Tool == "-cc1")
369+
if (Tool == "-cc1") {
370+
if (std::getenv("CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS")) {
371+
// Perform the compile twice in order to catch differences in the output.
372+
int RC = cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
373+
if (RC != 0)
374+
return RC;
375+
// FIXME: cc1_main should possibly clean up global state itself.
376+
llvm::remove_fatal_error_handler();
377+
}
370378
return cc1_main(makeArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
379+
}
371380
#if LLVM_ON_UNIX
372381
if (Tool == "-cc1depscand")
373382
return cc1depscand_main(makeArrayRef(ArgV).slice(2), ArgV[0],

0 commit comments

Comments
 (0)