-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang][Dependency Scanning] Canonicalize Defines of a Compiler Invocation As Early As Possible #159620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang Author: Qiongsi Wu (qiongsiwu) ChangesBefore this patch, we only perform However, in the presence of CAS, the content of the Part of work for rdar://136303612. Full diff: https://github.com/llvm/llvm-project/pull/159620.diff 1 Files Affected:
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index 0855e6dec6158..8c4a3684a10a7 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -393,8 +393,6 @@ class DependencyScanningAction {
DiagnosticConsumer *DiagConsumer) {
// Make a deep copy of the original Clang invocation.
CompilerInvocation OriginalInvocation(*Invocation);
- if (any(Service.getOptimizeArgs() & ScanningOptimizations::Macros))
- canonicalizeDefines(OriginalInvocation.getPreprocessorOpts());
if (Scanned) {
// Scanning runs once for the first -cc1 invocation in a chain of driver
@@ -708,7 +706,8 @@ static bool createAndRunToolInvocation(
std::vector<std::string> CommandLine, DependencyScanningAction &Action,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
std::shared_ptr<clang::PCHContainerOperations> &PCHContainerOps,
- DiagnosticsEngine &Diags, DependencyConsumer &Consumer) {
+ DiagnosticsEngine &Diags, DependencyConsumer &Consumer,
+ bool CanonicalizeDefs) {
// Save executable path before providing CommandLine to ToolInvocation
std::string Executable = CommandLine[0];
@@ -723,6 +722,9 @@ static bool createAndRunToolInvocation(
return false;
}
+ if (CanonicalizeDefs)
+ canonicalizeDefines(Invocation->getPreprocessorOpts());
+
if (!Action.runInvocation(std::move(Invocation), std::move(FS),
PCHContainerOps, Diags.getClient()))
return false;
@@ -744,14 +746,17 @@ bool DependencyScanningWorker::scanDependencies(
sanitizeDiagOpts(*DiagOpts);
auto Diags = CompilerInstance::createDiagnostics(*FS, *DiagOpts, &DC,
/*ShouldOwnClient=*/false);
+ bool canonicalizeDefs =
+ any(Service.getOptimizeArgs() & ScanningOptimizations::Macros);
DependencyScanningAction Action(Service, WorkingDirectory, Consumer,
Controller, DepFS, ModuleName);
bool Success = false;
if (CommandLine[1] == "-cc1") {
- Success = createAndRunToolInvocation(CommandLine, Action, FS,
- PCHContainerOps, *Diags, Consumer);
+ Success =
+ createAndRunToolInvocation(CommandLine, Action, FS, PCHContainerOps,
+ *Diags, Consumer, canonicalizeDefs);
} else {
Success = forEachDriverJob(
CommandLine, *Diags, FS, [&](const driver::Command &Cmd) {
@@ -774,7 +779,8 @@ bool DependencyScanningWorker::scanDependencies(
// are made by the driver do not go through the
// dependency scanning filesystem.
return createAndRunToolInvocation(std::move(Argv), Action, FS,
- PCHContainerOps, *Diags, Consumer);
+ PCHContainerOps, *Diags, Consumer,
+ canonicalizeDefs);
});
}
|
Note to reviewers: I did not add any tests here since I don't think there are visible behavior change observable without CAS. I am testing this work using https://github.com/swiftlang/swift and its llvm fork. Please let me know if there are things I can test against and I will add a test. Thanks! |
Do we have test downstream? Can you link a PR with test added? |
clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
Outdated
Show resolved
Hide resolved
clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
Outdated
Show resolved
Hide resolved
Here's a downstream test swiftlang#11454. |
d13bc74
to
72ca739
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
72ca739
to
250d52d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! FWIW I think this is easy to test even upstream by scanning two TUs with different defines that canonicalize to the same thing, and then checking the number of entries in the module cache. Might be worth doing here.
clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
Outdated
Show resolved
Hide resolved
Modifying the comment to make it more explicit. Co-authored-by: Jan Svoboda <[email protected]>
Ah great suggestion! Thanks! A test is now added here as well. |
This is a test accompanying llvm#159620, which fixes config macro canonicalization for scanning. Specifically, without llvm#159620, when CAS is on, there are three variants of A. With llvm#159620, there should be only two variants. Part of work for rdar://136303612
Before this patch, we only perform
-D
canonicalization on the deep copy of theCompilerInvocation
instance, since the canonicalization should have no impact on scanning.However, in the presence of CAS, the content of the
builtin
macros are included in the context hash. This patch makes sure that we canonicalize the scanningCompilerInvocation
's-D
s.Part of work for rdar://136303612.