Skip to content

Conversation

@cachemeifyoucan
Copy link
Collaborator

During explicit module build, clear the compilation directory flags when
CWD optimization is on. Now clang CodeGen will not add compilation
directories to DIFile when the corresponding flags are not passed and
make debug info not dependent on CWD.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jul 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 22, 2025

@llvm/pr-subscribers-clang

Author: Steven Wu (cachemeifyoucan)

Changes

During explicit module build, clear the compilation directory flags when
CWD optimization is on. Now clang CodeGen will not add compilation
directories to DIFile when the corresponding flags are not passed and
make debug info not dependent on CWD.


Full diff: https://github.com/llvm/llvm-project/pull/150129.diff

2 Files Affected:

  • (modified) clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp (+2-24)
  • (modified) clang/test/ClangScanDeps/modules-debug-dir.c (+7-3)
diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
index 37f8b945d785e..d67178c153e88 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -144,30 +144,8 @@ static void optimizeDiagnosticOpts(DiagnosticOptions &Opts,
 
 static void optimizeCWD(CowCompilerInvocation &BuildInvocation, StringRef CWD) {
   BuildInvocation.getMutFileSystemOpts().WorkingDir.clear();
-  if (BuildInvocation.getCodeGenOpts().DwarfVersion) {
-    // It is necessary to explicitly set the DebugCompilationDir
-    // to a common directory (e.g. root) if IgnoreCWD is true.
-    // When IgnoreCWD is true, the module's content should not
-    // depend on the current working directory. However, if dwarf
-    // information is needed (when CGOpts.DwarfVersion is
-    // non-zero), then CGOpts.DebugCompilationDir must be
-    // populated, because otherwise the current working directory
-    // will be automatically embedded in the dwarf information in
-    // the pcm, contradicting the assumption that it is safe to
-    // ignore the CWD. Thus in such cases,
-    // CGOpts.DebugCompilationDir is explicitly set to a common
-    // directory.
-    // FIXME: It is still excessive to create a copy of
-    // CodeGenOpts for each module. Since we do not modify the
-    // CodeGenOpts otherwise per module, the following code
-    // ends up generating identical CodeGenOpts for each module
-    // with DebugCompilationDir pointing to the root directory.
-    // We can optimize this away by creating a _single_ copy of
-    // CodeGenOpts whose DebugCompilationDir points to the root
-    // directory and reuse it across modules.
-    BuildInvocation.getMutCodeGenOpts().DebugCompilationDir =
-        llvm::sys::path::root_path(CWD);
-  }
+  BuildInvocation.getMutCodeGenOpts().DebugCompilationDir.clear();
+  BuildInvocation.getMutCodeGenOpts().CoverageCompilationDir.clear();
 }
 
 static std::vector<std::string> splitString(std::string S, char Separator) {
diff --git a/clang/test/ClangScanDeps/modules-debug-dir.c b/clang/test/ClangScanDeps/modules-debug-dir.c
index c4fb4982ed791..e2a0e1b7a5b01 100644
--- a/clang/test/ClangScanDeps/modules-debug-dir.c
+++ b/clang/test/ClangScanDeps/modules-debug-dir.c
@@ -1,5 +1,3 @@
-// REQUIRES: shell
-
 // RUN: rm -rf %t
 // RUN: split-file %s %t
 // RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
@@ -7,6 +5,12 @@
 // RUN:   experimental-full -optimize-args=all > %t/result.json
 // RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s
 
+// RUN: %deps-to-rsp %t/result.json --module-name=mod > %t/mod.rsp
+// RUN: %clang @%t/mod.rsp -o %t/mod.pcm
+// RUN: llvm-dwarfdump --debug-info %t/mod.pcm | FileCheck %s --check-prefix=DWARF
+// DWARF: DW_TAG_compile_unit
+// DWARF-NOT: DW_AT_comp_dir
+
 //--- cdb.json.in
 [{
   "directory": "DIR",
@@ -28,5 +32,5 @@ module mod {
 // directory when current working directory optimization is in effect.
 // CHECK:  "modules": [
 // CHECK: "command-line": [
-// CHECK: "-fdebug-compilation-dir={{\/|.*:(\\)?}}",
+// CHECK-NOT: -fdebug-compilation-dir
 // CHECK:  "translation-units": [

Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
@cachemeifyoucan cachemeifyoucan changed the base branch from users/cachemeifyoucan/spr/main.clangscandeps-clear-compilation-directory-if-needed to main July 31, 2025 23:32
Created using spr 1.3.6
@cachemeifyoucan cachemeifyoucan merged commit bda9272 into main Aug 1, 2025
9 checks passed
@cachemeifyoucan cachemeifyoucan deleted the users/cachemeifyoucan/spr/clangscandeps-clear-compilation-directory-if-needed branch August 1, 2025 18:01
cachemeifyoucan added a commit to cachemeifyoucan/llvm-project that referenced this pull request Aug 5, 2025
During explicit module build, clear the compilation directory flags when
CWD optimization is on. Now clang CodeGen will not add compilation
directories to DIFile when the corresponding flags are not passed and
make debug info not dependent on CWD.
cachemeifyoucan added a commit to swiftlang/llvm-project that referenced this pull request Aug 8, 2025
During explicit module build, clear the compilation directory flags when
CWD optimization is on. Now clang CodeGen will not add compilation
directories to DIFile when the corresponding flags are not passed and
make debug info not dependent on CWD.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants