[clang] [clang][DebugInfo] Don't assume static locals are emitted inside their function (PR #236525)
Paulius Velesko via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 22:46:13 PST 2026
https://github.com/pvelesko updated https://github.com/llvm/llvm-project/pull/236525
>From c506be9a7171e424265bfa2c38b4449d2972cbd7 Mon Sep 17 00:00:00 2001
From: Paulius Velesko <pvelesko at pglc.io>
Date: Wed, 28 Oct 2026 19:15:13 +0200
Subject: [PATCH 1/4] [clang][DebugInfo] Don't assume static locals are emitted
inside their function
collectVarDeclProps scopes a static local in the innermost lexical block
on LexicalBlockStack, asserting the stack is non-empty. CUDA/HIP device
compilation also emits static __device__ locals that host code ODR-uses
(ASTContext::CUDADeviceVarODRUsedByHost) from EmitDeferred at the end of
the translation unit, outside any function, so the stack is empty there
and clang asserts (or reads past an empty vector without assertions).
Fall back to the declaration context, as before #213153, when no
lexical block is open.
---
clang/lib/CodeGen/CGDebugInfo.cpp | 3 +--
.../debug-info-host-used-static-local.cu | 13 +++++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 96cda4555e49db..51371aea2d2650 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4759,8 +4759,7 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
// Get context for static locals (that are technically globals) the same way
// we do for "local" locals -- by using current lexical block.
- if (VD->isStaticLocal()) {
- assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
+ if (VD->isStaticLocal() && !LexicalBlockStack.empty()) {
VDContext = LexicalBlockStack.back();
return;
}
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..68b016b31aba23
--- /dev/null
+++ b/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
@@ -0,0 +1,13 @@
+// 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"
>From ef244e302d4afdc9e3613dd959f8f79fb991536b Mon Sep 17 00:00:00 2001
From: Paulius Velesko <pvelesko at pglc.io>
Date: Fri, 30 Oct 2026 01:30:08 +0200
Subject: [PATCH 2/4] Address review: describe the out-of-function case and
check the scope
---
clang/lib/CodeGen/CGDebugInfo.cpp | 3 +++
clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu | 3 ++-
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 51371aea2d2650..2455fdabb444dd 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4759,6 +4759,9 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
// Get context for static locals (that are technically globals) the same way
// we do for "local" locals -- by using current lexical block.
+ // A static local can also be emitted outside its function (e.g. a CUDA/HIP
+ // __device__ static local ODR-used by host code); then no lexical block is
+ // open and it falls through to the declaration context below.
if (VD->isStaticLocal() && !LexicalBlockStack.empty()) {
VDContext = LexicalBlockStack.back();
return;
diff --git a/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu b/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
index 68b016b31aba23..4d873c7f977671 100644
--- a/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
+++ b/clang/test/CodeGenCUDA/debug-info-host-used-static-local.cu
@@ -10,4 +10,5 @@ void fun1() {
}
// CHECK: @_ZZ4fun1vE11static_var1 = addrspace(1) externally_initialized global i32 3
-// CHECK: !DIGlobalVariable(name: "static_var1"
+// CHECK: !DIGlobalVariable(name: "static_var1", scope: ![[CU:[0-9]+]]
+// CHECK: ![[CU]] = distinct !DICompileUnit(
>From 09bdd6c8496b1406cb1d00a52f995d5d81952001 Mon Sep 17 00:00:00 2001
From: Paulius Velesko <pvelesko at pglc.io>
Date: Sat, 31 Oct 2026 07:59:28 +0200
Subject: [PATCH 3/4] Address review: keep the assert outside CUDA/HIP
Michael137 asked whether the fallback works around a CUDA bug and
suggested relaxing the assert only for CUDA. It is not a CUDA bug:
static __device__ locals that host code ODR-uses
(ASTContext::CUDADeviceVarODRUsedByHost) are emitted from
CodeGenModule::EmitDeferred at the end of the translation unit by
design, so no lexical block is open. Keep the assert for everything
else, where an empty LexicalBlockStack for a static local still points
at a scoping bug, and skip it only when compiling CUDA/HIP.
---
clang/lib/CodeGen/CGDebugInfo.cpp | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 2455fdabb444dd..848dd2cf3ffb94 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4759,12 +4759,13 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
// Get context for static locals (that are technically globals) the same way
// we do for "local" locals -- by using current lexical block.
- // A static local can also be emitted outside its function (e.g. a CUDA/HIP
- // __device__ static local ODR-used by host code); then no lexical block is
- // open and it falls through to the declaration context below.
- if (VD->isStaticLocal() && !LexicalBlockStack.empty()) {
- VDContext = LexicalBlockStack.back();
- return;
+ if (VD->isStaticLocal()) {
+ if (!LexicalBlockStack.empty()) {
+ VDContext = LexicalBlockStack.back();
+ return;
+ }
+ // CUDA/HIP emit host-used __device__ static locals at the end of the TU.
+ assert(CGM.getLangOpts().CUDA && "Region stack mismatch, stack empty!");
}
// Since we emit declarations (DW_AT_members) for static members, place the
>From c95d9aaad660dc6b174f730e9021561cc0808409 Mon Sep 17 00:00:00 2001
From: Paulius Velesko <pvelesko at pglc.io>
Date: Fri, 20 Nov 2026 13:24:41 +0200
Subject: [PATCH 4/4] Fall back to the DeclContext for every language
dzhidzhoev showed (PR 243405) that plain C++ also reaches
collectVarDeclProps with an empty LexicalBlockStack: an alias to a
static local is emitted by EmitAliasDefinition -> EmitGlobalAlias ->
getGlobalVariableForwardDeclaration at file scope when the static has
no debug info yet, either because its function or the static is
nodebug or because the function (e.g. constexpr) is emitted after the
alias.
So the empty stack is not CUDA-specific: drop the CUDA-only assert and
use the declaration context whenever no lexical block is open, as
before #213153. In the deferred case the forward declaration is
replaced by the real variable, scoped in its function, at finalize().
Fixes #243381.
---
clang/lib/CodeGen/CGDebugInfo.cpp | 11 ++++-----
.../test/DebugInfo/CXX/static-local-alias.cpp | 23 +++++++++++++++++++
2 files changed, 27 insertions(+), 7 deletions(-)
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 848dd2cf3ffb94..bab3afc405b8ad 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4759,13 +4759,10 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
// Get context for static locals (that are technically globals) the same way
// we do for "local" locals -- by using current lexical block.
- if (VD->isStaticLocal()) {
- if (!LexicalBlockStack.empty()) {
- VDContext = LexicalBlockStack.back();
- return;
- }
- // CUDA/HIP emit host-used __device__ static locals at the end of the TU.
- assert(CGM.getLangOpts().CUDA && "Region stack mismatch, stack empty!");
+ // Outside a function (aliases, CUDA/HIP host-used statics): DeclContext.
+ if (VD->isStaticLocal() && !LexicalBlockStack.empty()) {
+ VDContext = LexicalBlockStack.back();
+ return;
}
// Since we emit declarations (DW_AT_members) for static members, place the
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..ac867fb1e9ff0d
--- /dev/null
+++ b/clang/test/DebugInfo/CXX/static-local-alias.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -debug-info-kind=constructor -std=c++23 %s -o - | FileCheck %s
+
+// Aliases to static locals are emitted at file scope, 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(
+
+void g() { __attribute__((nodebug)) static int s; }
+extern int b __attribute__((alias("_ZZ1gvE1s")));
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "b", {{.*}}entity: ![[B:[0-9]+]]
+// CHECK-DAG: ![[B]] = !DIGlobalVariable(name: "s", scope: ![[G:[0-9]+]], {{.*}}isDefinition: false)
+// 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