[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-27 Thread Michael Buch via cfe-commits

Michael137 wrote:

> Would confuse `DwarfDebug` without your changes/this patch? Or only with this 
> patch? (& hopefully there's some way to reach the merge globals 
> functionality/problem/interesting IR from clang itself - but I guess even if 
> it isn't, this is still a demonstration of representational issues)

Would only confuse `DwarfDebug` *with* this patch.

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-27 Thread Michael Buch via cfe-commits

Michael137 wrote:

> @Michael137 is this patch ready to land? We (I have took over the bug from 
> @ilovepi ) have a few builders that are currently blocked by the behavior 
> change introduced in PR #70639 . It would be great if this change can be 
> landed timely. Thx.

Nope this isn't ready yet. We need to figure out how to make global variables 
with locations *and* constants work. I'm about to submit a separate PR that 
will unblock your builds

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-27 Thread David Blaikie via cfe-commits

dwblaikie wrote:

> I.e., two `DIGlobalVariableExpression`s for the same `DIGlobalVariable`, 
> which AFAICT would confuse `DwarfDebug` at the moment, which expects there to 
> be a 1-to-1 mapping.

Would confuse `DwarfDebug` without your changes/this patch? Or only with this 
patch?
(& hopefully there's some way to reach the merge globals 
functionality/problem/interesting IR from clang itself - but I guess even if it 
isn't, this is still a demonstration of representational issues)

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-27 Thread via cfe-commits

zeroomega wrote:

@Michael137 is this patch ready to land? We (I have took over the bug from 
@ilovepi ) have a few builders that are currently blocked by the behavior 
change introduced in PR #70639 . It would be great if this change can be landed 
timely. Thx.

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-22 Thread Michael Buch via cfe-commits

Michael137 wrote:

> Do we end up with two DIGlobalVariableExpressions?

Just checked. With this patch, MergeGlobals would produce following IR:
```
...
!10 = !DIGlobalVariableExpression(var: !11, expr: 
!DIExpression(DW_OP_plus_uconst, 4, DW_OP_constu, 200, DW_OP_stack_value))
!11 = distinct !DIGlobalVariable(name: "b", linkageName: 
"_ZN12_GLOBAL__N_13Foo1bE", scope: !2, file: !3, line: 8, type: !4, isLocal: 
true, isDefinition: true, declaration: !9)
...
!14 = !DIGlobalVariableExpression(var: !11, expr: !DIExpression(DW_OP_constu, 
200, DW_OP_stack_value))
...
```
I.e., two `DIGlobalVariableExpression`s for the same `DIGlobalVariable`, which 
AFAICT would confuse `DwarfDebug` at the moment, which expects there to be a 
1-to-1 mapping.

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-21 Thread Michael Buch via cfe-commits

Michael137 wrote:

> > My understanding was that the DIExpression parameter to 
> > DIGlobalVariableExpression was empty for global variables with locations. 
> > So the patch just encodes the constant into that expression if it's 
> > otherwise empty.
> 
> I think in theory it can be non-empty (see what happens under a merge globals 
> optimization... I'm not sure of an example of when this optimization fires, 
> or what the debug info we emit looks like - in theory it should look like a 
> global with a non-empty expression that describes offsetting the pointer 
> forward to reach the global inside the merged global) & so then you'd have a 
> hard time telling whether the expression is meant to be used in addition to 
> the location, or as part of evaluating the location.
> 
> We don't really have a mechanism for encoding a variable in two locations (or 
> a constant and a location) at the same time, I think. We could invent a 
> special opcode to use in the expression to communicate this, or define some 
> special handling if there's two separate expressions providing a location (or 
> a location and a constant in this case) for the same variable (say that 
> they're both valid, and emit them both if we can).
> 
> @adrian-prantl thoughts?

Thanks for the pointers. The closest I could get to triggering a GlobalMerge of 
const static data-members was for following code:
```
struct Foo {
static const int a;
static const int b;
};

const int Foo::a = 100;
const int Foo::b = 200;
```

But only if I mark the constants as `internal` in the IR as follows (I attached 
the whole file):
```
%struct.Foo = type { i8 }

@a = internal constant i32 100, align 4, !dbg !0
@b = internal constant i32 200, align 4, !dbg !5

define void @use1() {
  %x = load i32, ptr @a, align 4
  %y = load i32, ptr @b, align 4
  ret void
}
...
```

Then run `opt` as:
```
./bin/opt  -global-merge -global-merge-max-offset=100 -global-merge-on-const -S 
merge.ll
```

That changes the IR into:
```
...
@_MergedGlobals = private constant <{ i32, i32 }> <{ i32 100, i32 200 }>, align 
4, !dbg !0, !dbg !13

@a = internal alias i32, ptr @_MergedGlobals
@b = internal alias i32, getelementptr inbounds (<{ i32, i32 }>, ptr 
@_MergedGlobals, i32 0, i32 1)
...
!1 = distinct !DIGlobalVariable(name: "a", linkageName: "_ZN3Foo1aE", scope: 
!2, file: !3, line: 6, type: !7, isLocal: false, isDefinition: true, 
declaration: !12)
...
!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
!6 = distinct !DIGlobalVariable(name: "b", linkageName: "_ZN3Foo1bE", scope: 
!2, file: !3, line: 7, type: !7, isLocal: false, isDefinition: true, 
declaration: !9)
...
!13 = !DIGlobalVariableExpression(var: !6, expr: 
!DIExpression(DW_OP_plus_uconst, 4))
```

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-21 Thread Michael Buch via cfe-commits

Michael137 wrote:

I extracted the parts that are clean-ups/bugfixes into a new PR: 
https://github.com/llvm/llvm-project/pull/72974

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread David Blaikie via cfe-commits

dwblaikie wrote:

https://github.com/llvm/llvm-project/blob/abd85cd473afedf112bf00630a22382fee4a7853/llvm/lib/CodeGen/GlobalMerge.cpp#L531
 - this is around where I think we'd get a global with a location and a 
non-empty expression

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread David Blaikie via cfe-commits

dwblaikie wrote:

> My understanding was that the DIExpression parameter to 
> DIGlobalVariableExpression was empty for global variables with locations. So 
> the patch just encodes the constant into that expression if it's otherwise 
> empty.

I think in theory it can be non-empty (see what happens under a merge globals 
optimization... I'm not sure of an example of when this optimization fires, or 
what the debug info we emit looks like - in theory it should look like a global 
with a non-empty expression that describes offsetting the pointer forward to 
reach the global inside the merged global) & so then you'd have a hard time 
telling whether the expression is meant to be used in addition to the location, 
or as part of evaluating the location.

We don't really have a mechanism for encoding a variable in two locations (or a 
constant and a location) at the same time, I think. We could invent a special 
opcode to use in the expression to communicate this, or define some special 
handling if there's two separate expressions providing a location (or a 
location and a constant in this case) for the same variable (say that they're 
both valid, and emit them both if we can). 

@adrian-prantl thoughts?

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Michael Buch via cfe-commits

Michael137 wrote:

> Hmm, I can't quite tell from the test case updates in the patch, at least at 
> a glance: How does this get encoded at the IR level? Do we end up with two 
> DIGlobalVariableExpressions? One with the constant value expresison, and one 
> that references the actual global variable? Or somehow encode both in one?

My understanding was that the DIExpression parameter to 
DIGlobalVariableExpression was empty for global variables with locations. So 
the patch just encodes the constant into that expression if it's otherwise 
empty.

My plan was to make 
[DwarfCompileUnit](https://llvm.org/doxygen/DwarfCompileUnit_8cpp_source.html#l00203)
 support global variables with locations *and* constants, which it currently 
assumes doesn't happen.

Let me know if I missed something here

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread David Blaikie via cfe-commits

dwblaikie wrote:

Hmm, I can't quite tell from the test case updates in the patch, at least at a 
glance: How does this get encoded at the IR level? Do we end up with two 
DIGlobalVariableExpressions? One with the constant value expresison, and one 
that references the actual global variable? Or somehow encode both in one?

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Michael Buch via cfe-commits

Michael137 wrote:

This will need a slight tweak in DwarfCompileUnit because it currently doesn't 
attach a DW_AT_location to global variable DIEs that already have a 
DW_AT_const_value

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Michael Buch via cfe-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/72730

>From 6dcb09dcc50a9b9e92640412242927b3e226929e Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:20:05 +
Subject: [PATCH 1/8] [clang][DebugInfo][NFC] Create
 evaluateConstantInitializer helper function

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..4840581b5d03f89 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
+  assert (VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5596,14 +5609,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const * InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From fcc6e19d108798fb18c1973e4d4cc3800da07f9f Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:52:24 +
Subject: [PATCH 2/8] fixup! clang-format

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 4840581b5d03f89..9bba6e6b13e9318 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,8 +69,8 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
-APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
-  assert (VD != nullptr);
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
 
   VD = VD->getCanonicalDecl();
   if (!VD)
@@ -5613,7 +5613,7 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const * InitVal = evaluateConstantInitializer(VD);
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From 148ab1793a866111060f77807ff065040fad92d8 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:22:06 +
Subject: [PATCH 3/8] [clang][DebugInfo] Attach DW_AT_const_value to static
 data-member definitions if available

---
 clang/lib/CodeGen/CGDebugInfo.cpp  | 10 --
 .../CodeGenCXX/debug-info-static-inline-member.cpp |  2 +-
 clang/test/CodeGenCXX/inline-dllexport-member.cpp  |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 9bba6e6b13e9318..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5516,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(), true, E,
 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
 Align, Annotations);
 Var->addDebugInfo(GVE);
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp 
b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a8297a..950ea9b302b290c 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -43,7 +43,7 @@ int main() {
 // CHECK:  @{{.*}}cexpr_struct_with_addr{{.*}} = 
 // CHECK-SAME!dbg ![[EMPTY_GLOBAL:[0-9]+]]
 
-// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression())
+// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], 

[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Paul T Robinson via cfe-commits


@@ -69,6 +69,29 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+/// Given a VarDecl corresponding to either the definition or
+/// declaration of a C++ static data member, if it has a constant
+/// initializer and is evaluatable, return the evaluated value.
+/// Returns std::nullopt on failure.

pogo59 wrote:

Maybe instead of "on failure" say "otherwise." It's not really a _failure_ 
condition.

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 approved this pull request.

One comment, otherwise LGTM.

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 edited https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Michael Buch via cfe-commits

https://github.com/Michael137 edited 
https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Michael Buch via cfe-commits

https://github.com/Michael137 edited 
https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Michael Buch via cfe-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/72730

>From 6dcb09dcc50a9b9e92640412242927b3e226929e Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:20:05 +
Subject: [PATCH 1/7] [clang][DebugInfo][NFC] Create
 evaluateConstantInitializer helper function

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..4840581b5d03f89 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
+  assert (VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5596,14 +5609,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const * InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From fcc6e19d108798fb18c1973e4d4cc3800da07f9f Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:52:24 +
Subject: [PATCH 2/7] fixup! clang-format

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 4840581b5d03f89..9bba6e6b13e9318 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,8 +69,8 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
-APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
-  assert (VD != nullptr);
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
 
   VD = VD->getCanonicalDecl();
   if (!VD)
@@ -5613,7 +5613,7 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const * InitVal = evaluateConstantInitializer(VD);
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From 148ab1793a866111060f77807ff065040fad92d8 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:22:06 +
Subject: [PATCH 3/7] [clang][DebugInfo] Attach DW_AT_const_value to static
 data-member definitions if available

---
 clang/lib/CodeGen/CGDebugInfo.cpp  | 10 --
 .../CodeGenCXX/debug-info-static-inline-member.cpp |  2 +-
 clang/test/CodeGenCXX/inline-dllexport-member.cpp  |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 9bba6e6b13e9318..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5516,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(), true, E,
 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
 Align, Annotations);
 Var->addDebugInfo(GVE);
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp 
b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a8297a..950ea9b302b290c 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -43,7 +43,7 @@ int main() {
 // CHECK:  @{{.*}}cexpr_struct_with_addr{{.*}} = 
 // CHECK-SAME!dbg ![[EMPTY_GLOBAL:[0-9]+]]
 
-// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression())
+// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], 

[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-20 Thread Michael Buch via cfe-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/72730

>From 6dcb09dcc50a9b9e92640412242927b3e226929e Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:20:05 +
Subject: [PATCH 1/6] [clang][DebugInfo][NFC] Create
 evaluateConstantInitializer helper function

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..4840581b5d03f89 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
+  assert (VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5596,14 +5609,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const * InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From fcc6e19d108798fb18c1973e4d4cc3800da07f9f Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:52:24 +
Subject: [PATCH 2/6] fixup! clang-format

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 4840581b5d03f89..9bba6e6b13e9318 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,8 +69,8 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
-APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
-  assert (VD != nullptr);
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
 
   VD = VD->getCanonicalDecl();
   if (!VD)
@@ -5613,7 +5613,7 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const * InitVal = evaluateConstantInitializer(VD);
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From 148ab1793a866111060f77807ff065040fad92d8 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:22:06 +
Subject: [PATCH 3/6] [clang][DebugInfo] Attach DW_AT_const_value to static
 data-member definitions if available

---
 clang/lib/CodeGen/CGDebugInfo.cpp  | 10 --
 .../CodeGenCXX/debug-info-static-inline-member.cpp |  2 +-
 clang/test/CodeGenCXX/inline-dllexport-member.cpp  |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 9bba6e6b13e9318..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5516,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(), true, E,
 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
 Align, Annotations);
 Var->addDebugInfo(GVE);
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp 
b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a8297a..950ea9b302b290c 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -43,7 +43,7 @@ int main() {
 // CHECK:  @{{.*}}cexpr_struct_with_addr{{.*}} = 
 // CHECK-SAME!dbg ![[EMPTY_GLOBAL:[0-9]+]]
 
-// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression())
+// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], 

[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-18 Thread J. Ryan Stinnett via cfe-commits

https://github.com/jryans approved this pull request.

> This patch makes sure we encode the constant in a `DIExpression` on 
> definitions, which is tested. And we have tests for ensuring that 
> `DW_OP_constu` expressions get turned into `DW_AT_const_value`s (I know of at 
> least `llvm/test/DebugInfo/Generic/dwarf5-debug-info-static-member.ll`)

Ah okay, then with that in mind, I think the test changes here are likely 
enough to merge.  

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-18 Thread Michael Buch via cfe-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/72730

>From 6dcb09dcc50a9b9e92640412242927b3e226929e Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:20:05 +
Subject: [PATCH 1/5] [clang][DebugInfo][NFC] Create
 evaluateConstantInitializer helper function

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..4840581b5d03f89 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
+  assert (VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5596,14 +5609,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const * InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From fcc6e19d108798fb18c1973e4d4cc3800da07f9f Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:52:24 +
Subject: [PATCH 2/5] fixup! clang-format

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 4840581b5d03f89..9bba6e6b13e9318 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,8 +69,8 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
-APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
-  assert (VD != nullptr);
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
 
   VD = VD->getCanonicalDecl();
   if (!VD)
@@ -5613,7 +5613,7 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const * InitVal = evaluateConstantInitializer(VD);
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From 148ab1793a866111060f77807ff065040fad92d8 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:22:06 +
Subject: [PATCH 3/5] [clang][DebugInfo] Attach DW_AT_const_value to static
 data-member definitions if available

---
 clang/lib/CodeGen/CGDebugInfo.cpp  | 10 --
 .../CodeGenCXX/debug-info-static-inline-member.cpp |  2 +-
 clang/test/CodeGenCXX/inline-dllexport-member.cpp  |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 9bba6e6b13e9318..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5516,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(), true, E,
 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
 Align, Annotations);
 Var->addDebugInfo(GVE);
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp 
b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a8297a..950ea9b302b290c 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -43,7 +43,7 @@ int main() {
 // CHECK:  @{{.*}}cexpr_struct_with_addr{{.*}} = 
 // CHECK-SAME!dbg ![[EMPTY_GLOBAL:[0-9]+]]
 
-// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression())
+// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], 

[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-18 Thread Michael Buch via cfe-commits

Michael137 wrote:

> Overall this looks good, thanks!
> 
> I think we may want to add a test to check that the output DWARF has a const 
> value in the expected place, since consumers are relying on it.

Any suggestions where to put such end-to-end test?

This patch makes sure we encode the constant in a `DIExpression` on 
definitions, which is tested. And we have tests for ensuring that 
`DW_OP_constu` expressions get turned into `DW_AT_const_value`s (I know of at 
least `llvm/test/DebugInfo/Generic/dwarf5-debug-info-static-member.ll`)

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-18 Thread J. Ryan Stinnett via cfe-commits


@@ -5503,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else

jryans wrote:

I believe code style says this else block should use braces because the if 
block has them.

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-18 Thread J. Ryan Stinnett via cfe-commits

https://github.com/jryans edited https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-18 Thread J. Ryan Stinnett via cfe-commits

https://github.com/jryans commented:

Overall this looks good, thanks!

I think we may want to add a test to check that the output DWARF has a const 
value in the expected place, since consumers are relying on it.

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-17 Thread Paul Kirth via cfe-commits

ilovepi wrote:

This looks great, thanks for keeping me in the loop. :)

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-17 Thread Michael Buch via cfe-commits

Michael137 wrote:

@ilovepi 

https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-17 Thread Michael Buch via cfe-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/72730

>From 6dcb09dcc50a9b9e92640412242927b3e226929e Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:20:05 +
Subject: [PATCH 1/4] [clang][DebugInfo][NFC] Create
 evaluateConstantInitializer helper function

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..4840581b5d03f89 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
+  assert (VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5596,14 +5609,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const * InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From fcc6e19d108798fb18c1973e4d4cc3800da07f9f Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:52:24 +
Subject: [PATCH 2/4] fixup! clang-format

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 4840581b5d03f89..9bba6e6b13e9318 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,8 +69,8 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
-APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
-  assert (VD != nullptr);
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
 
   VD = VD->getCanonicalDecl();
   if (!VD)
@@ -5613,7 +5613,7 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const * InitVal = evaluateConstantInitializer(VD);
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From 148ab1793a866111060f77807ff065040fad92d8 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:22:06 +
Subject: [PATCH 3/4] [clang][DebugInfo] Attach DW_AT_const_value to static
 data-member definitions if available

---
 clang/lib/CodeGen/CGDebugInfo.cpp  | 10 --
 .../CodeGenCXX/debug-info-static-inline-member.cpp |  2 +-
 clang/test/CodeGenCXX/inline-dllexport-member.cpp  |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 9bba6e6b13e9318..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5516,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(), true, E,
 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
 Align, Annotations);
 Var->addDebugInfo(GVE);
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp 
b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a8297a..950ea9b302b290c 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -43,7 +43,7 @@ int main() {
 // CHECK:  @{{.*}}cexpr_struct_with_addr{{.*}} = 
 // CHECK-SAME!dbg ![[EMPTY_GLOBAL:[0-9]+]]
 
-// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression())
+// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], 

[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-17 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Michael Buch (Michael137)


Changes

In https://github.com/llvm/llvm-project/pull/71780 we started emitting 
definitions for all static data-members with constant initialisers, even if 
they were constants (i.e., didn't have a location). We also dropped the 
`DW_AT_const_value` from the declaration to help resolve [inconsistencies 
during type merging in the 
DWARFParallelLinker](https://github.com/llvm/llvm-project/pull/68721). However, 
for static data members that do have locations, we wouldn't emit a 
`DW_AT_const_value` on it, assuming that the consumer knows how to read the 
value using the location. This broke some consumers that really wanted to find 
a `DW_AT_const_value`.

This patch makes sure we also attach the `DW_AT_const_value` to definitions 
that have a location.

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


3 Files Affected:

- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+22-6) 
- (modified) clang/test/CodeGenCXX/debug-info-static-inline-member.cpp (+1-1) 
- (modified) clang/test/CodeGenCXX/inline-dllexport-member.cpp (+1-1) 


``diff
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5503,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(), true, E,
 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
 Align, Annotations);
 Var->addDebugInfo(GVE);
@@ -5596,14 +5615,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp 
b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a8297a..950ea9b302b290c 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -43,7 +43,7 @@ int main() {
 // CHECK:  @{{.*}}cexpr_struct_with_addr{{.*}} = 
 // CHECK-SAME!dbg ![[EMPTY_GLOBAL:[0-9]+]]
 
-// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression())
+// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression(DW_OP_constu, 25, DW_OP_stack_value))
 // CHECK:  ![[INT_VAR]] = distinct !DIGlobalVariable(name: 
"cexpr_int_with_addr", linkageName:
 // CHECK-SAME:isLocal: false, isDefinition: true, declaration: 
![[INT_DECL:[0-9]+]])
 
diff --git a/clang/test/CodeGenCXX/inline-dllexport-member.cpp 
b/clang/test/CodeGenCXX/inline-dllexport-member.cpp
index d6b004d66dc6cbd..6bc01599c466780 100644
--- a/clang/test/CodeGenCXX/inline-dllexport-member.cpp
+++ b/clang/test/CodeGenCXX/inline-dllexport-member.cpp
@@ -7,7 +7,7 @@ struct __declspec(dllexport) s {
   static const unsigned int ui = 0;
 };
 
-// CHECK: [[UI]] = !DIGlobalVariableExpression(var: [[UIV:.*]], expr: 
!DIExpression())
+// CHECK: [[UI]] = !DIGlobalVariableExpression(var: [[UIV:.*]], expr: 
!DIExpression(DW_OP_constu, 0, DW_OP_stack_value))
 // CHECK: [[UIV]] = distinct !DIGlobalVariable(name: "ui", linkageName: 
"?ui@s@@2IB", scope: ![[SCOPE:[0-9]+]],
 // CHECK: ![[SCOPE]] = distinct !DICompileUnit(
 

``




https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits mailing 

[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-17 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Michael Buch (Michael137)


Changes

In https://github.com/llvm/llvm-project/pull/71780 we started emitting 
definitions for all static data-members with constant initialisers, even if 
they were constants (i.e., didn't have a location). We also dropped the 
`DW_AT_const_value` from the declaration to help resolve [inconsistencies 
during type merging in the 
DWARFParallelLinker](https://github.com/llvm/llvm-project/pull/68721). However, 
for static data members that do have locations, we wouldn't emit a 
`DW_AT_const_value` on it, assuming that the consumer knows how to read the 
value using the location. This broke some consumers that really wanted to find 
a `DW_AT_const_value`.

This patch makes sure we also attach the `DW_AT_const_value` to definitions 
that have a location.

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


3 Files Affected:

- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+22-6) 
- (modified) clang/test/CodeGenCXX/debug-info-static-inline-member.cpp (+1-1) 
- (modified) clang/test/CodeGenCXX/inline-dllexport-member.cpp (+1-1) 


``diff
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5503,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(), true, E,
 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
 Align, Annotations);
 Var->addDebugInfo(GVE);
@@ -5596,14 +5615,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp 
b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a8297a..950ea9b302b290c 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -43,7 +43,7 @@ int main() {
 // CHECK:  @{{.*}}cexpr_struct_with_addr{{.*}} = 
 // CHECK-SAME!dbg ![[EMPTY_GLOBAL:[0-9]+]]
 
-// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression())
+// CHECK:  !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: 
!DIExpression(DW_OP_constu, 25, DW_OP_stack_value))
 // CHECK:  ![[INT_VAR]] = distinct !DIGlobalVariable(name: 
"cexpr_int_with_addr", linkageName:
 // CHECK-SAME:isLocal: false, isDefinition: true, declaration: 
![[INT_DECL:[0-9]+]])
 
diff --git a/clang/test/CodeGenCXX/inline-dllexport-member.cpp 
b/clang/test/CodeGenCXX/inline-dllexport-member.cpp
index d6b004d66dc6cbd..6bc01599c466780 100644
--- a/clang/test/CodeGenCXX/inline-dllexport-member.cpp
+++ b/clang/test/CodeGenCXX/inline-dllexport-member.cpp
@@ -7,7 +7,7 @@ struct __declspec(dllexport) s {
   static const unsigned int ui = 0;
 };
 
-// CHECK: [[UI]] = !DIGlobalVariableExpression(var: [[UIV:.*]], expr: 
!DIExpression())
+// CHECK: [[UI]] = !DIGlobalVariableExpression(var: [[UIV:.*]], expr: 
!DIExpression(DW_OP_constu, 0, DW_OP_stack_value))
 // CHECK: [[UIV]] = distinct !DIGlobalVariable(name: "ui", linkageName: 
"?ui@s@@2IB", scope: ![[SCOPE:[0-9]+]],
 // CHECK: ![[SCOPE]] = distinct !DICompileUnit(
 

``




https://github.com/llvm/llvm-project/pull/72730
___
cfe-commits 

[clang] [clang][DebugInfo] Attach DW_AT_const_value to static data-member definitions if available (PR #72730)

2023-11-17 Thread Michael Buch via cfe-commits

https://github.com/Michael137 created 
https://github.com/llvm/llvm-project/pull/72730

In https://github.com/llvm/llvm-project/pull/71780 we started emitting 
definitions for all static data-members with constant initialisers, even if 
they were constants (i.e., didn't have a location). We also dropped the 
`DW_AT_const_value` from the declaration to help resolve [inconsistencies 
during type merging in the 
DWARFParallelLinker](https://github.com/llvm/llvm-project/pull/68721). However, 
for static data members that do have locations, we wouldn't emit a 
`DW_AT_const_value` on it, assuming that the consumer knows how to read the 
value using the location. This broke some consumers that really wanted to find 
a `DW_AT_const_value`.

This patch makes sure we also attach the `DW_AT_const_value` to definitions 
that have a location.

>From 6dcb09dcc50a9b9e92640412242927b3e226929e Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:20:05 +
Subject: [PATCH 1/3] [clang][DebugInfo][NFC] Create
 evaluateConstantInitializer helper function

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0b52d99ad07f164..4840581b5d03f89 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,6 +69,19 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
+APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
+  assert (VD != nullptr);
+
+  VD = VD->getCanonicalDecl();
+  if (!VD)
+return nullptr;
+
+  if (!VD->hasConstantInitialization() || !VD->hasInit())
+return nullptr;
+
+  return VD->evaluateValue();
+}
+
 CGDebugInfo::CGDebugInfo(CodeGenModule )
 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
   DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5596,14 +5609,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) 
{
   if (VD->hasAttr())
 return;
 
-  if (!VD->hasInit())
-return;
-
   const auto CacheIt = DeclCache.find(VD);
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const *InitVal = VD->evaluateValue();
+  auto const * InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From fcc6e19d108798fb18c1973e4d4cc3800da07f9f Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:52:24 +
Subject: [PATCH 2/3] fixup! clang-format

---
 clang/lib/CodeGen/CGDebugInfo.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 4840581b5d03f89..9bba6e6b13e9318 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -69,8 +69,8 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const 
ASTContext ) {
   return D->hasAttr() ? D->getMaxAlignment() : 0;
 }
 
-APValue const * evaluateConstantInitializer(clang::VarDecl const * VD) {
-  assert (VD != nullptr);
+APValue const *evaluateConstantInitializer(clang::VarDecl const *VD) {
+  assert(VD != nullptr);
 
   VD = VD->getCanonicalDecl();
   if (!VD)
@@ -5613,7 +5613,7 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
   if (CacheIt != DeclCache.end())
 return;
 
-  auto const * InitVal = evaluateConstantInitializer(VD);
+  auto const *InitVal = evaluateConstantInitializer(VD);
   if (!InitVal)
 return;
 

>From 148ab1793a866111060f77807ff065040fad92d8 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Sat, 18 Nov 2023 00:22:06 +
Subject: [PATCH 3/3] [clang][DebugInfo] Attach DW_AT_const_value to static
 data-member definitions if available

---
 clang/lib/CodeGen/CGDebugInfo.cpp  | 10 --
 .../CodeGenCXX/debug-info-static-inline-member.cpp |  2 +-
 clang/test/CodeGenCXX/inline-dllexport-member.cpp  |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 9bba6e6b13e9318..e01c57baef19931 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5516,11 +5516,17 @@ void 
CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
 }
 AppendAddressSpaceXDeref(AddressSpace, Expr);
 
+llvm::DIExpression *E = nullptr;
+if (Expr.empty()) {
+  if (auto const *InitVal = evaluateConstantInitializer(D))
+E = createConstantValueExpression(D, *InitVal);
+} else
+  E = DBuilder.createExpression(Expr);
+
 llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
 GVE = DBuilder.createGlobalVariableExpression(
 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, 
Unit),
-Var->hasLocalLinkage(), true,
-Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
+Var->hasLocalLinkage(),