[lld] [llvm] [DebugInfo][DwarfDebug] Move emission of globals from beginModule() to endModule() (5/7) (PR #184219)

Vladislav Dzhidzhoev via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 06:42:16 PST 2026


================
@@ -1513,9 +1487,47 @@ void DwarfDebug::endModule() {
   assert(CurFn == nullptr);
   assert(CurMI == nullptr);
 
-  for (const auto &P : CUMap) {
-    const auto *CUNode = cast<DICompileUnit>(P.first);
-    DwarfCompileUnit *CU = &*P.second;
+  const Module *M = MMI->getModule();
+
+  // Collect global variables info.
+  DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
+      GVMap;
+  for (const GlobalVariable &Global : M->globals()) {
+    SmallVector<DIGlobalVariableExpression *, 1> GVs;
+    Global.getDebugInfo(GVs);
+    for (auto *GVE : GVs)
+      GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
+  }
+
+  for (DICompileUnit *CUNode : M->debug_compile_units()) {
+    DwarfCompileUnit *CU = CUMap.lookup(CUNode);
+
+    // If the CU hasn't been emitted yet, create it here unless it is empty.
+    if (!CU) {
+      if (CUNode->getImportedEntities().empty() &&
+          CUNode->getEnumTypes().empty() &&
+          CUNode->getRetainedTypes().empty() &&
+          CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
+        continue;
+      CU = &getOrCreateDwarfCompileUnit(CUNode);
+    }
----------------
dzhidzhoev wrote:

Ah, I agree that we can skip duplicate emptiness check. However, `CUMap.lookup()` only is not enough here to support split dwarf mode.
I've separated `DwarfDebug::getDwarfCompileUnit()` from  `DwarfDebug::getOrCreateDwarfCompileUnit()`, used it here, and removed the check for the empties.

https://github.com/llvm/llvm-project/pull/184219


More information about the llvm-commits mailing list