Skip to content

Commit 4636d45

Browse files
committed
[clang][cas] Don't include PGO-related files in the include-tree when they are not used
PGO options are ignored for PCH invocations and may not be treated as dependencies from the build system, so don't add them in the include-tree. Otherwise we may get in a situation where such a file gets updated and an incremental build fails because of mismatched files in the include-tree of the PCH and the translation unit that uses it. rdar://125629656 (cherry picked from commit 4ccf152)
1 parent c35339a commit 4636d45

File tree

7 files changed

+80
-11
lines changed

7 files changed

+80
-11
lines changed

clang/include/clang/Basic/DiagnosticCASKinds.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def err_cas_cannot_be_initialized : Error<
1414
def err_cas_cannot_parse_root_id : Error<
1515
"CAS cannot parse root-id '%0' specified by -fcas-fs">, DefaultFatal;
1616
def err_cas_filesystem_cannot_be_initialized : Error<
17-
"CAS filesystem cannot be initialized from root-id '%0' specified by"
18-
" -fcas-fs">, DefaultFatal;
17+
"CAS filesystem cannot be initialized from root-id '%0': %1">, DefaultFatal;
1918
def err_cas_filesystem_cannot_set_working_directory : Error<
2019
"CAS filesystem cannot set working directory to '%0' specified by"
2120
" -fcas-fs-working-directory">, DefaultFatal;

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,9 +1552,8 @@ createBaseFS(const FileSystemOptions &FSOpts, const FrontendOptions &FEOpts,
15521552
auto ExpectedFS = IsIncludeTreeFS ? makeIncludeTreeFS(std::move(CAS), *RootID)
15531553
: makeCASFS(std::move(CAS), *RootID);
15541554
if (!ExpectedFS) {
1555-
llvm::consumeError(ExpectedFS.takeError());
15561555
Diags.Report(diag::err_cas_filesystem_cannot_be_initialized)
1557-
<< RootIDString;
1556+
<< RootIDString << llvm::toString(ExpectedFS.takeError());
15581557
return makeEmptyCASFS();
15591558
}
15601559
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = std::move(*ExpectedFS);

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,6 @@ void DependencyScanningWorker::computeDependenciesFromCompilerInvocation(
926926

927927
// Adjust the invocation.
928928
auto &Frontend = Invocation->getFrontendOpts();
929-
Frontend.ProgramAction = frontend::RunPreprocessorOnly;
930929
Frontend.OutputFile = "/dev/null";
931930
Frontend.DisableFree = false;
932931

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ Error IncludeTreeActionController::finalizeModuleBuild(
381381
CompilerInstance &ModuleScanInstance) {
382382
// FIXME: the scan invocation is incorrect here; we need the `NewInvocation`
383383
// from `finalizeModuleInvocation` to finish the tree.
384+
resetBenignCodeGenOptions(
385+
frontend::GenerateModule,
386+
ModuleScanInstance.getInvocation().getLangOpts(),
387+
ModuleScanInstance.getInvocation().getCodeGenOpts());
384388
auto Builder = BuilderStack.pop_back_val();
385389
auto Tree = Builder->finishIncludeTree(ModuleScanInstance,
386390
ModuleScanInstance.getInvocation());
@@ -589,6 +593,8 @@ IncludeTreeBuilder::finishIncludeTree(CompilerInstance &ScanInstance,
589593

590594
auto addFile = [&](StringRef FilePath,
591595
bool IgnoreFileError = false) -> Error {
596+
if (FilePath.empty())
597+
return Error::success();
592598
llvm::ErrorOr<const FileEntry *> FE = FM.getFile(FilePath);
593599
if (!FE) {
594600
if (IgnoreFileError)

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ void tooling::dependencies::configureInvocationForCaching(
2929
// Clear this otherwise it defeats the purpose of making the compilation key
3030
// independent of certain arguments.
3131
CI.getCodeGenOpts().DwarfDebugFlags.clear();
32-
if (FrontendOpts.ProgramAction == frontend::GeneratePCH) {
33-
// Clear paths that are emitted into binaries; they do not affect PCH.
34-
// For modules this is handled in ModuleDepCollector.
35-
CI.getCodeGenOpts().CoverageDataFile.clear();
36-
CI.getCodeGenOpts().CoverageNotesFile.clear();
37-
}
32+
resetBenignCodeGenOptions(FrontendOpts.ProgramAction, CI.getLangOpts(),
33+
CI.getCodeGenOpts());
3834

3935
HeaderSearchOptions &HSOpts = CI.getHeaderSearchOpts();
4036
// Avoid writing potentially volatile diagnostic options into pcms.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// REQUIRES: ondisk_cas
2+
3+
// RUN: rm -rf %t
4+
// RUN: split-file %s %t
5+
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
6+
7+
// RUN: llvm-profdata merge -o %t/instrumentation.profdata %S/Inputs/pgo.profraw
8+
// RUN: clang-scan-deps -compilation-database %t/cdb.json \
9+
// RUN: -cas-path %t/cas -module-files-dir %t/outputs \
10+
// RUN: -format experimental-include-tree-full -mode preprocess-dependency-directives \
11+
// RUN: > %t/deps.json
12+
13+
// RUN: %deps-to-rsp %t/deps.json --module-name Top > %t/Top.rsp
14+
// RUN: FileCheck %s --input-file=%t/Top.rsp
15+
16+
// RUN: cat %t/Top.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/Top.casid
17+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/Top.casid | FileCheck %s
18+
// CHECK-NOT: instrumentation.profdata
19+
20+
//--- cdb.json.template
21+
[{
22+
"file": "DIR/tu.m",
23+
"directory": "DIR",
24+
"command": "clang -fsyntax-only DIR/tu.m -I DIR -fmodules -fimplicit-modules -fimplicit-module-maps -fmodules-cache-path=DIR/module-cache -Rcompile-job-cache -fprofile-instr-use=DIR/instrumentation.profdata"
25+
}]
26+
27+
//--- module.modulemap
28+
module Top { header "Top.h" export *}
29+
30+
//--- Top.h
31+
#pragma once
32+
struct Top {
33+
int x;
34+
};
35+
void top(void);
36+
37+
//--- tu.m
38+
#import "Top.h"
39+
40+
void tu(void) {
41+
top();
42+
}

clang/test/CAS/pgo-profile-with-pch.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: rm -rf %t.dir && mkdir -p %t.dir
2+
3+
// Check that use of profile data for PCH is ignored
4+
5+
// RUN: llvm-profdata merge -o %t.profdata %S/Inputs/pgo.profraw
6+
// RUN: %clang -cc1depscan -fdepscan=inline -fdepscan-include-tree -o %t-pch.rsp -cc1-args -cc1 -triple x86_64-apple-macosx12.0.0 -emit-pch -O3 -Rcompile-job-cache \
7+
// RUN: -x c-header %s -o %t.h.pch -fcas-path %t.dir/cas -fprofile-instrument-use-path=%t.profdata
8+
// RUN: %clang @%t-pch.rsp 2>&1 | FileCheck %s --check-prefix=CACHE-MISS
9+
// RUN: FileCheck %s -check-prefix=PCHPROF -input-file %t-pch.rsp
10+
// PCHPROF-NOT: -fprofile-instrument-use-path
11+
12+
// Update profdata file contents
13+
// RUN: llvm-profdata merge -o %t.profdata %S/Inputs/pgo2.profraw
14+
15+
// Use the modified profdata file for the main file along with the PCH.
16+
// RUN: %clang -cc1depscan -fdepscan=inline -fdepscan-include-tree -o %t.rsp -cc1-args -cc1 -triple x86_64-apple-macosx12.0.0 -emit-obj -O3 -Rcompile-job-cache \
17+
// RUN: -x c %s -o %t.o -fcas-path %t.dir/cas -fprofile-instrument-use-path=%t.profdata -include-pch %t.h.pch
18+
// RUN: %clang @%t.rsp 2>&1 | FileCheck %s --check-prefix=CACHE-MISS
19+
// RUN: FileCheck %s -check-prefix=TUPROF -input-file %t.rsp
20+
// TUPROF: -fprofile-instrument-use-path
21+
22+
// Check that the modified profdata is ignored when re-scanning for the PCH.
23+
// RUN: %clang -cc1depscan -fdepscan=inline -fdepscan-include-tree -o %t-pch2.rsp -cc1-args -cc1 -triple x86_64-apple-macosx12.0.0 -emit-pch -O3 -Rcompile-job-cache \
24+
// RUN: -x c-header %s -o %t.h.pch -fcas-path %t.dir/cas -fprofile-instrument-use-path=%t.profdata
25+
// RUN: diff -u %t-pch.rsp %t-pch2.rsp
26+
27+
// CACHE-MISS: remark: compile job cache miss
28+
// CACHE-HIT: remark: compile job cache hit

0 commit comments

Comments
 (0)