Skip to content

Commit b0403cd

Browse files
authored
Merge pull request #8599 from akyrtzi/akyrtzi/pr/ignore-profile-flags-for-pch-release
[6.0][clang][cas] Don't include PGO-related files in the include-tree when they are not used
2 parents b76411d + 4636d45 commit b0403cd

File tree

11 files changed

+142
-21
lines changed

11 files changed

+142
-21
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/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ class ModuleDepCollector final : public DependencyCollector {
320320
ModuleDeps &Deps);
321321
};
322322

323+
/// Resets codegen options that don't affect modules/PCH.
324+
void resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
325+
const LangOptions &LangOpts,
326+
CodeGenOptions &CGOpts);
327+
323328
} // end namespace dependencies
324329
} // end namespace tooling
325330
} // end namespace clang

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/ModuleDepCollector.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ void ModuleDepCollector::addOutputPaths(CowCompilerInvocation &CI,
160160
}
161161
}
162162

163+
void dependencies::resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
164+
const LangOptions &LangOpts,
165+
CodeGenOptions &CGOpts) {
166+
// TODO: Figure out better way to set options to their default value.
167+
if (ProgramAction == frontend::GenerateModule) {
168+
CGOpts.MainFileName.clear();
169+
CGOpts.DwarfDebugFlags.clear();
170+
}
171+
if (ProgramAction == frontend::GeneratePCH ||
172+
(ProgramAction == frontend::GenerateModule && !LangOpts.ModulesCodegen)) {
173+
CGOpts.DebugCompilationDir.clear();
174+
CGOpts.CoverageCompilationDir.clear();
175+
CGOpts.CoverageDataFile.clear();
176+
CGOpts.CoverageNotesFile.clear();
177+
CGOpts.ProfileInstrumentUsePath.clear();
178+
CGOpts.SampleProfileFile.clear();
179+
CGOpts.ProfileRemappingFile.clear();
180+
}
181+
}
182+
163183
static CowCompilerInvocation
164184
makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
165185
CI.resetNonModularOptions();
@@ -176,15 +196,8 @@ makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
176196
// LLVM options are not going to affect the AST
177197
CI.getFrontendOpts().LLVMArgs.clear();
178198

179-
// TODO: Figure out better way to set options to their default value.
180-
CI.getCodeGenOpts().MainFileName.clear();
181-
CI.getCodeGenOpts().DwarfDebugFlags.clear();
182-
if (!CI.getLangOpts().ModulesCodegen) {
183-
CI.getCodeGenOpts().DebugCompilationDir.clear();
184-
CI.getCodeGenOpts().CoverageCompilationDir.clear();
185-
CI.getCodeGenOpts().CoverageDataFile.clear();
186-
CI.getCodeGenOpts().CoverageNotesFile.clear();
187-
}
199+
resetBenignCodeGenOptions(frontend::GenerateModule, CI.getLangOpts(),
200+
CI.getCodeGenOpts());
188201

189202
// Map output paths that affect behaviour to "-" so their existence is in the
190203
// context hash. The final path will be computed in addOutputPaths.
@@ -368,6 +381,8 @@ static bool needsModules(FrontendInputFile FIF) {
368381

369382
void ModuleDepCollector::applyDiscoveredDependencies(CompilerInvocation &CI) {
370383
CI.clearImplicitModuleBuildOptions();
384+
resetBenignCodeGenOptions(CI.getFrontendOpts().ProgramAction,
385+
CI.getLangOpts(), CI.getCodeGenOpts());
371386

372387
if (llvm::any_of(CI.getFrontendOpts().Inputs, needsModules)) {
373388
Preprocessor &PP = ScanInstance.getPreprocessor();

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
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"directory": "DIR",
4-
"command": "clang -fsyntax-only DIR/tu.c -fmodules -fimplicit-module-maps -fmodules-validate-once-per-build-session -fbuild-session-file=DIR/build-session -fmodules-prune-interval=123 -fmodules-prune-after=123 -fmodules-cache-path=DIR/cache -include DIR/header.h -grecord-command-line -fdebug-compilation-dir=DIR/debug -fcoverage-compilation-dir=DIR/coverage -ftest-coverage -o DIR/tu.o -serialize-diagnostics DIR/tu.diag -MT tu -MD -MF DIR/tu.d",
4+
"command": "clang -fsyntax-only DIR/tu.c -fmodules -fimplicit-module-maps -fmodules-validate-once-per-build-session -fbuild-session-file=DIR/build-session -fmodules-prune-interval=123 -fmodules-prune-after=123 -fmodules-cache-path=DIR/cache -include DIR/header.h -grecord-command-line -fdebug-compilation-dir=DIR/debug -fcoverage-compilation-dir=DIR/coverage -ftest-coverage -fprofile-instr-use=DIR/tu.profdata -o DIR/tu.o -serialize-diagnostics DIR/tu.diag -MT tu -MD -MF DIR/tu.d",
55
"file": "DIR/tu.c"
66
}
77
]

clang/test/ClangScanDeps/removed-args.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// RUN: rm -rf %t && mkdir %t
1010
// RUN: cp %S/Inputs/removed-args/* %t
1111
// RUN: touch %t/build-session
12+
// RUN: touch %t/tu.proftext
13+
// RUN: llvm-profdata merge %t/tu.proftext -o %t/tu.profdata
1214

1315
// RUN: sed "s|DIR|%/t|g" %S/Inputs/removed-args/cdb.json.template > %t/cdb.json
1416
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full > %t/result.json
@@ -25,6 +27,7 @@
2527
// CHECK-NOT: "-fcoverage-compilation-dir="
2628
// CHECK-NOT: "-coverage-notes-file
2729
// CHECK-NOT: "-coverage-data-file
30+
// CHECK-NOT: "-fprofile-instrument-use-path
2831
// CHECK-NOT: "-dwarf-debug-flags"
2932
// CHECK-NOT: "-main-file-name"
3033
// CHECK-NOT: "-include"
@@ -50,6 +53,7 @@
5053
// CHECK-NOT: "-fcoverage-compilation-dir=
5154
// CHECK-NOT: "-coverage-notes-file
5255
// CHECK-NOT: "-coverage-data-file
56+
// CHECK-NOT: "-fprofile-instrument-use-path
5357
// CHECK-NOT: "-dwarf-debug-flags"
5458
// CHECK-NOT: "-main-file-name"
5559
// CHECK-NOT: "-include"
@@ -89,3 +93,31 @@
8993
// CHECK-NOT: "-fmodules-prune-interval=
9094
// CHECK-NOT: "-fmodules-prune-after=
9195
// CHECK: ],
96+
97+
// Check for removed args for PCH invocations.
98+
99+
// RUN: split-file %s %t
100+
// RUN: sed "s|DIR|%/t|g" %t/cdb-pch.json.template > %t/cdb-pch.json
101+
// RUN: clang-scan-deps -compilation-database %t/cdb-pch.json -format experimental-full > %t/result-pch.json
102+
// RUN: cat %t/result-pch.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t -check-prefix=PCH
103+
//
104+
// PCH-NOT: "-fdebug-compilation-dir="
105+
// PCH-NOT: "-fcoverage-compilation-dir="
106+
// PCH-NOT: "-coverage-notes-file
107+
// PCH-NOT: "-coverage-data-file
108+
// PCH-NOT: "-fprofile-instrument-use-path
109+
// PCH-NOT: "-include"
110+
// PCH-NOT: "-fmodules-cache-path=
111+
// PCH-NOT: "-fmodules-validate-once-per-build-session"
112+
// PCH-NOT: "-fbuild-session-timestamp=
113+
// PCH-NOT: "-fmodules-prune-interval=
114+
// PCH-NOT: "-fmodules-prune-after=
115+
116+
//--- cdb-pch.json.template
117+
[
118+
{
119+
"directory": "DIR",
120+
"command": "clang -x c-header DIR/header.h -fmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -fdebug-compilation-dir=DIR/debug -fcoverage-compilation-dir=DIR/coverage -ftest-coverage -fprofile-instr-use=DIR/tu.profdata -o DIR/header.h.pch -serialize-diagnostics DIR/header.h.pch.diag ",
121+
"file": "DIR/header.h.pch"
122+
}
123+
]

0 commit comments

Comments
 (0)