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

Michael Buch via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 21 04:13:06 PST 2023


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


More information about the cfe-commits mailing list