[clang] [clang][DebugInfo] Record lexical scopes of static locals (PR #248298)

Vladislav Dzhidzhoev via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 2 16:54:58 PST 2026


https://github.com/dzhidzhoev created https://github.com/llvm/llvm-project/pull/248298

Follows the discussion in https://github.com/llvm/llvm-project/pull/236525.

https://github.com/llvm/llvm-project/pull/213153 introduced emission of static locals inside DILexicalBlock scopes. It relied on the state of `CGDebugInfo::LexicalBlockStack` at a point of static local variable emission. When emission of a static local is requested out of its parent function emission context, `LexicalBlockStack` is not guaranteed to represent the actual variable scope (see https://github.com/llvm/llvm-project/issues/243381).

The fix is similar to the patch for local types emission https://github.com/llvm/llvm-project/pull/242447. It records the lexical scope of a static local when it is available, and reuses it when the variable emission is requested.

Tests from https://github.com/llvm/llvm-project/pull/236525 are used. Fixes https://github.com/llvm/llvm-project/issues/243381.

>From 18ef8ca0db5719db23f8192995354cf9b40aec8d Mon Sep 17 00:00:00 2001
From: Vladislav Dzhidzhoev <vdzhidzhoev at accesssoftek.com>
Date: Tue, 1 Dec 2026 19:02:04 +0100
Subject: [PATCH] [clang][DebugInfo] Record lexical scopes of static locals

Follows the discussion in
https://github.com/llvm/llvm-project/pull/236525.

https://github.com/llvm/llvm-project/pull/213153 introduced emission of
static locals inside DILexicalBlock scopes. It relied on the state of
`CGDebugInfo::LexicalBlockStack` at the moment of static local variable
emission. When emission of a static local is requested out of its parent
function emission context, `LexicalBlockStack` is not guaranteed to represent
the actual variable scope
(see https://github.com/llvm/llvm-project/issues/243381).

The fix is similar to the patch for local types emission
https://github.com/llvm/llvm-project/pull/242447. It records the lexical scope
of a static local when it is available, and reuses it when the variable
emission is requested.

Tests from https://github.com/llvm/llvm-project/pull/236525 are used.
Fixes https://github.com/llvm/llvm-project/issues/243381.
---
 clang/lib/CodeGen/CGDebugInfo.cpp             | 11 +++---
 clang/lib/CodeGen/CGDecl.cpp                  |  8 ++++
 .../debug-info-host-used-static-local.cu      | 14 +++++++
 .../test/DebugInfo/CXX/static-local-alias.cpp | 37 +++++++++++++++++++
 4 files changed, 65 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
 create mode 100644 clang/test/DebugInfo/CXX/static-local-alias.cpp

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 50f523c3318d77..3004f7f8719d6c 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4843,12 +4843,13 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
     TemplateParameters = nullptr;
   }
 
-  // Get context for static locals (that are technically globals) the same way
-  // we do for "local" locals -- by using current lexical block.
+  // Get context for static locals (that are technically globals) similar way
+  // we do for local types -- by using recorded lexical block.
   if (VD->isStaticLocal()) {
-    assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
-    VDContext = LexicalBlockStack.back();
-    return;
+    if (llvm::DIScope *S = LexicalBlockMap.lookup(VD)) {
+      VDContext = S;
+      return;
+    }
   }
 
   // Since we emit declarations (DW_AT_members) for static members, place the
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 76e374db976796..9c8fb749af2b30 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -180,6 +180,14 @@ void CodeGenFunction::EmitDecl(const Decl &D, bool EvaluateConditionDecl) {
     const VarDecl &VD = cast<VarDecl>(D);
     assert(VD.isLocalVarDecl() &&
            "Should not see file-scope variables inside a function!");
+    // Record the current debug info scope for (possibly deferred) static local
+    // variable emission.
+    // Only record the scope when emitting the variable in its own declaration
+    // context.
+    if (CGDebugInfo *DI = getDebugInfo();
+        DI && VD.isStaticLocal() &&
+        CurCodeDecl == dyn_cast<Decl>(VD.getDeclContext()))
+      DI->recordDeclarationLexicalScope(VD);
     EmitVarDecl(VD);
     if (EvaluateConditionDecl)
       MaybeEmitDeferredVarDeclInit(&VD);
diff --git a/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu b/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
new file mode 100644
index 00000000000000..4d873c7f977671
--- /dev/null
+++ b/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device -x hip %s -emit-llvm -debug-info-kind=limited -o - | FileCheck %s
+
+#include "Inputs/cuda.h"
+
+// A static __device__ local ODR-used by host code is emitted outside any function.
+void use(int *);
+void fun1() {
+  static __device__ int static_var1 = 3;
+  use(&static_var1);
+}
+
+// CHECK: @_ZZ4fun1vE11static_var1 = addrspace(1) externally_initialized global i32 3
+// CHECK: !DIGlobalVariable(name: "static_var1", scope: ![[CU:[0-9]+]]
+// CHECK: ![[CU]] = distinct !DICompileUnit(
diff --git a/clang/test/DebugInfo/CXX/static-local-alias.cpp b/clang/test/DebugInfo/CXX/static-local-alias.cpp
new file mode 100644
index 00000000000000..2565152404af94
--- /dev/null
+++ b/clang/test/DebugInfo/CXX/static-local-alias.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -debug-info-kind=constructor -std=c++23 %s -O0 -o - | FileCheck %s
+
+// Aliases to static locals may be emitted in compile unit, outside any lexical block.
+
+__attribute__((nodebug)) void f() {
+  static int s;
+}
+extern int a __attribute__((alias("_ZZ1fvE1s")));
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "a", {{.*}}entity: ![[A:[0-9]+]]
+// CHECK-DAG: ![[A]] = !DIGlobalVariable(name: "s", scope: ![[CU:[0-9]+]], {{.*}}isDefinition: false)
+// CHECK-DAG: ![[CU]] = distinct !DICompileUnit(
+
+int g(bool flag) {
+  if (flag) {
+    __attribute__((nodebug)) static int sg;
+    int x = flag;
+    sg += flag;
+  }
+  return 0;
+}
+extern int b __attribute__((alias("_ZZ1gbE2sg")));
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "b", {{.*}}entity: ![[B:[0-9]+]]
+// CHECK-DAG: ![[B]] = !DIGlobalVariable(name: "sg", scope: ![[LB1:[0-9]+]], {{.*}}isDefinition: false)
+// CHECK-DAG: ![[LB1]] = distinct !DILexicalBlock(scope: ![[LB2:[0-9]+]]
+// CHECK-DAG: ![[LB2]] = distinct !DILexicalBlock(scope: ![[G:[0-9]+]]
+// CHECK-DAG: ![[G]] = distinct !DISubprogram(name: "g"
+
+// The function is emitted after the alias.
+constexpr const int *h() {
+  static constexpr int s = 1;
+  return &s;
+}
+const int *p = h();
+extern const int c __attribute__((alias("_ZZ1hvE1s")));
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "c", {{.*}}entity: ![[C:[0-9]+]]
+// CHECK-DAG: ![[C]]  = distinct !DIGlobalVariable(name: "s", scope: ![[H:[0-9]+]], {{.*}}isDefinition: true)
+// CHECK-DAG: ![[H]]  = distinct !DISubprogram(name: "h"



More information about the cfe-commits mailing list