llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.19k stars 11.13k forks source link

Clang doesn't emit `DW_TAG_const_expr` for `constexpr` variables. #96965

Open mvanotti opened 1 month ago

mvanotti commented 1 month ago

When declaring a C++ variable as constexpr, clang doesn't emit DW_AT_const_expr inside the DW_TAG_variable.

See the following program for example:

#include <stddef.h>
constexpr size_t kPageSize = 0x1000;

int main(void) {
  __asm__ volatile("" ::"r"(kPageSize));
  return 0;
}

Compiled with:

$ clang++ -Wall -Wextra -pedantic -std=c++17 -gdwarf-5 -O0    program_with_constexpr.cc   -o program_with_constexpr

Using clang version 19.0.0git (https://llvm.googlesource.com/llvm-project 3809e20afc68d7d03821f0ec59b928dcf9befbf4)

Using $ llvm-dwarfdump ./program_with_constexpr

0x00000023:   DW_TAG_variable
                DW_AT_name      ("kPageSize")
                DW_AT_type      (0x0000002d "const size_t")
                DW_AT_decl_file ("/home/user/test/program_with_constexpr.cc")
                DW_AT_decl_line (2)
                DW_AT_const_value       (4096)

It's missing DW_AT_const_expr

When I compile this with gcc:

$ g++ -Wall -Wextra -pedantic -std=c++17 -gdwarf-5 -O0    program_with_constexpr.cc   -o program_with_constexpr
0x0000005b:   DW_TAG_variable
                DW_AT_name      ("kPageSize")
                DW_AT_decl_file ("/home/user/test/program_with_constexpr.cc")
                DW_AT_decl_line (2)
                DW_AT_decl_column       (0x12)
                DW_AT_type      (0x00000041 "const size_t")
                DW_AT_const_expr        (true)
                DW_AT_location  (DW_OP_addr 0x2008)
Michael137 commented 1 month ago

Think it was just not implemented because no consumer asked for it (is my assumption). Out of curiosity, what would you use it for?

llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-codegen

Author: Marco Vanotti (mvanotti)

When declaring a C++ variable as `constexpr`, clang doesn't emit `DW_AT_const_expr` inside the `DW_TAG_variable`. See the following program for example: ```c++ #include <stddef.h> constexpr size_t kPageSize = 0x1000; int main(void) { __asm__ volatile("" ::"r"(kPageSize)); return 0; } ``` Compiled with: ```shell $ clang++ -Wall -Wextra -pedantic -std=c++17 -gdwarf-5 -O0 program_with_constexpr.cc -o program_with_constexpr ``` Using `clang version 19.0.0git (https://llvm.googlesource.com/llvm-project 3809e20afc68d7d03821f0ec59b928dcf9befbf4)` Using `$ llvm-dwarfdump ./program_with_constexpr` ``` 0x00000023: DW_TAG_variable DW_AT_name ("kPageSize") DW_AT_type (0x0000002d "const size_t") DW_AT_decl_file ("/home/user/test/program_with_constexpr.cc") DW_AT_decl_line (2) DW_AT_const_value (4096) ``` It's missing `DW_AT_const_expr` When I compile this with gcc: ```shell $ g++ -Wall -Wextra -pedantic -std=c++17 -gdwarf-5 -O0 program_with_constexpr.cc -o program_with_constexpr ``` ``` 0x0000005b: DW_TAG_variable DW_AT_name ("kPageSize") DW_AT_decl_file ("/home/user/test/program_with_constexpr.cc") DW_AT_decl_line (2) DW_AT_decl_column (0x12) DW_AT_type (0x00000041 "const size_t") DW_AT_const_expr (true) DW_AT_location (DW_OP_addr 0x2008) ```
mvanotti commented 1 month ago

Think it was just not implemented because no consumer asked for it (is my assumption). Out of curiosity, what would you use it for?

I don't particularly need it. I was looking at an issue in lldb0 and was wondering if there was a way to differentiate between const and constexpr variables, and a coworker pointed out that it was possible but was unsure that clang emitted that info.

llvmbot commented 1 month ago

@llvm/issue-subscribers-debuginfo

Author: Marco Vanotti (mvanotti)

When declaring a C++ variable as `constexpr`, clang doesn't emit `DW_AT_const_expr` inside the `DW_TAG_variable`. See the following program for example: ```c++ #include <stddef.h> constexpr size_t kPageSize = 0x1000; int main(void) { __asm__ volatile("" ::"r"(kPageSize)); return 0; } ``` Compiled with: ```shell $ clang++ -Wall -Wextra -pedantic -std=c++17 -gdwarf-5 -O0 program_with_constexpr.cc -o program_with_constexpr ``` Using `clang version 19.0.0git (https://llvm.googlesource.com/llvm-project 3809e20afc68d7d03821f0ec59b928dcf9befbf4)` Using `$ llvm-dwarfdump ./program_with_constexpr` ``` 0x00000023: DW_TAG_variable DW_AT_name ("kPageSize") DW_AT_type (0x0000002d "const size_t") DW_AT_decl_file ("/home/user/test/program_with_constexpr.cc") DW_AT_decl_line (2) DW_AT_const_value (4096) ``` It's missing `DW_AT_const_expr` When I compile this with gcc: ```shell $ g++ -Wall -Wextra -pedantic -std=c++17 -gdwarf-5 -O0 program_with_constexpr.cc -o program_with_constexpr ``` ``` 0x0000005b: DW_TAG_variable DW_AT_name ("kPageSize") DW_AT_decl_file ("/home/user/test/program_with_constexpr.cc") DW_AT_decl_line (2) DW_AT_decl_column (0x12) DW_AT_type (0x00000041 "const size_t") DW_AT_const_expr (true) DW_AT_location (DW_OP_addr 0x2008) ```
dwblaikie commented 1 month ago

Any particular DWARF-consumer feature you're planning to build with this functionality? (it's not immediately apparent to me what value this DWARF would provide to a consumer)

mvanotti commented 1 month ago

Any particular DWARF-consumer feature you're planning to build with this functionality? (it's not immediately apparent to me what value this DWARF would provide to a consumer)

I'm probably not the main consumer of this functionality 😅. I was playing with lldb's python api building a program to keep track of variable values during a debugging session, and thought that knowing whether a variable was constexpr would help me filter out some uninteresting variables. lldb doesn't expose this information currently, so even if it's available in clang, it will take some time until this becomes useful to me, and even then, it's just a nice-to-have and not something that I desperately need.

dwblaikie commented 3 weeks ago

even if it's available in clang, it will take some time until this becomes useful to me, and even then, it's just a nice-to-have and not something that I desperately need.

Fair enough - good to have the context - thanks!