Skip to content

Commit a71d588

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
1 parent 6170b73 commit a71d588

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
@@ -4552,6 +4552,24 @@ void Clang::AddPrefixMappingOptions(const ArgList &Args, ArgStringList &CmdArgs,
45524552
}
45534553
}
45544554

4555+
static void addCachingOptions(ArgStringList &CmdArgs) {
4556+
if (std::getenv("CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS"))
4557+
CmdArgs.push_back("-fcache-disable-replay");
4558+
4559+
if (std::getenv("CLANG_CACHE_REDACT_TIME_MACROS")) {
4560+
// Remove use of these macros to get reproducible outputs. This can
4561+
// accompany CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS to avoid fatal errors
4562+
// when the source uses these macros.
4563+
CmdArgs.push_back("-Wno-builtin-macro-redefined");
4564+
CmdArgs.push_back("-D__DATE__=\"redacted\"");
4565+
CmdArgs.push_back("-D__TIMESTAMP__=\"redacted\"");
4566+
CmdArgs.push_back("-D__TIME__=\"redacted\"");
4567+
}
4568+
4569+
if (std::getenv("CLANG_CACHE_CHECK_REPRODUCIBLE_CACHING_ISSUES"))
4570+
CmdArgs.push_back("-Werror=reproducible-caching");
4571+
}
4572+
45554573
void Clang::ConstructJob(Compilation &C, const JobAction &Job,
45564574
const InputInfo &Output, const InputInfoList &Inputs,
45574575
const ArgList &Args, const char *LinkingOutput) const {
@@ -5901,6 +5919,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
59015919
// Handle -fdepscan-prefix-map-* options
59025920
AddPrefixMappingOptions(Args, CmdArgs, D, Sysroot);
59035921

5922+
// Handle compile caching options.
5923+
addCachingOptions(CmdArgs);
5924+
59045925
// Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
59055926
// that "The compiler can only warn and ignore the option if not recognized".
59065927
// 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
@@ -164,18 +164,6 @@ static void addLauncherArgs(SmallVectorImpl<const char *> &Args,
164164
ServicePath});
165165
}
166166
Args.append({"-greproducible", "-Xclang", "-fcas-token-cache"});
167-
168-
if (llvm::sys::Process::GetEnv("CLANG_CACHE_REDACT_TIME_MACROS")) {
169-
// Remove use of these macros to get reproducible outputs. This can
170-
// accompany CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS to avoid fatal errors
171-
// when the source uses these macros.
172-
Args.append({"-Wno-builtin-macro-redefined", "-D__DATE__=\"redacted\"",
173-
"-D__TIMESTAMP__=\"redacted\"", "-D__TIME__=\"redacted\""});
174-
}
175-
if (llvm::sys::Process::GetEnv(
176-
"CLANG_CACHE_CHECK_REPRODUCIBLE_CACHING_ISSUES")) {
177-
Args.append({"-Werror=reproducible-caching"});
178-
}
179167
}
180168

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

clang/tools/driver/driver.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,17 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
376376
}
377377
StringRef Tool = ArgV[1];
378378
void *GetExecutablePathVP = (void *)(intptr_t)GetExecutablePath;
379-
if (Tool == "-cc1")
379+
if (Tool == "-cc1") {
380+
if (std::getenv("CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS")) {
381+
// Perform the compile twice in order to catch differences in the output.
382+
int RC = cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
383+
if (RC != 0)
384+
return RC;
385+
// FIXME: cc1_main should possibly clean up global state itself.
386+
llvm::remove_fatal_error_handler();
387+
}
380388
return cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
389+
}
381390
#if LLVM_ON_UNIX
382391
if (Tool == "-cc1depscand")
383392
return cc1depscand_main(ArrayRef(ArgV).slice(2), ArgV[0],

0 commit comments

Comments
 (0)