<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/103486>103486</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect debug information for a union variable when compiled with "-g -O3"
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
edumoot
</td>
</tr>
</table>
<pre>
The debug information for a union variable at line 33 is incorrect when debugging a binary compiled with '-g -O3', and the output of the binary is also incorrect, but the variable's information in memory is correct.
```
$ ./bug_06_O3.out
50792199
```
```
clang -g -O3 bug_06.c -o bug_06_O3.out
(lldb) file bug_06_O3.out
(lldb) b 33
(lldb) r
[...]
* thread #1, name = 'bug_06_O3.out', stop reason = breakpoint 1.1
frame #0: 0x0000555555555154 bug_06_O3.out`main at bug_06.c:33:5
30 {
31 union U0 a = fun_b();
32 printf("%d\n", a.f5);
-> 33 return 0;
34 }
(lldb) fr v
(U0) a = {
f0 = 7
f1 = -1
f2 = 117901063
f3 = 460551
f4 = 117901063
f5 = 50792199
f6 = (f0 = 7, f1 = -2, 230275, f2 = 0, 0, f3 = 0, f4 = 0, f5 = 0)
}
(lldb) memory read &a
0x7ffff7fba030: 07 07 07 07 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x7ffff7fba040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
(lldb)
```
`cat bug_06.c`
```
#include <stdio.h>
struct S0 {
unsigned f0 :7;
signed f1 :2;
signed f2 :29;
unsigned f3 :20;
signed f4;
unsigned f5:26 ;
};
union U0 {
unsigned f0 : 7;
signed f1 : 2;
signed f2 : 29;
unsigned f3 : 20;
signed f4;
unsigned f5 : 26;
struct S0 f6;
};
static union U0 fun_b(void)
{
union U0 d = {7};
return d;
}
int main(void)
{
union U0 a = fun_b();
printf("%d\n", a.f5);
return 0;
}
```
We can reproduce this bug in LLVM versions 18.1.8, 17.0.6, and 16.0.3.
Godbolt [https://godbolt.org/z/r5hfcqY59](url)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVkuP27YT_zT0ZWCBIkU9DjrY6_iPP5AihzYtegooibTYyqRLUk7ST1-Ielje9W42hqCl5vGbB2dmhzunTlqIErE9YocN731rbCma_myM31Sm-V7-1gpoRNWfQGlp7Jl7ZTRIY4FDr4fzlVvFq04A99ApLYBSUA6Uro21ovbwtRV6xDgpfQIOldLcfofanC-qEw18Vb4FRLLtCbafKCIZIk_AdQO-FWB6f-k9GBm-JlXlgHfO3IwMGlXvg8zsECKZu_NaaTiLsxn1J8UIED4gvEMpnp7xkyQQIXKs-tMXnH75RCPT-5HFcFaQuCgmwYfaj4h1x_UJxhhhxI1q2Bp4YAORvOuaCpECpOrED0QqoPQF0U4Uto-iCLHDLLAD31rBG0CExkPaND8LQPQw3MC9nfEinDcXsII7o4NYZQX_-2KU9hBH8QgLACBtACIUI7oD_A1jjNn8i1nyLIgUn7nSQ9HMuUB0RymiO7ZgUgyAcIGy_Y0UB9JgcCy_zxh48Ev2-kuFSI5IgehKgczyF6u0l0GCIMIaxJ50OD4BjyRb620R_TDU8axqhe-tBozoHm7Iyejc4eWdWbguxM94II0-riKROFCy5TsO39slo5IEQhxnBY5xShc6DfQkxYzdhJNXhFmg39csgEynG88XN8jT4kJICaGYZCyQR0fwcA6vyYPxnKzObD7PzfEgN1MHTiWY8lEAf8uklDKTFcdTAWWrB__4gejZ7yVwMgK_A-wdwKuQ3jMJ6nWhL9znQ4cqXXd9M_Tjk_ONMlGL6Ic1lPO2rz38ite1BL0Ok7wZq2qXrRtg5gyXuyOPOCRw7rrmBkgDE9-YEwNk8liBDfJDfe1vRbCcw3tp3DdCgNdjgNeDgDejgJ8KY9RI72wtyZfpG_E5z72qb_NpnkxXo5pVb-zvCuduojXztMhGdLhNnXkWNXcOrICGwTyM1gcGH1h6Y3b-xLx8PCNvjj1qij8E1FyDFRdrmr4W4FvlYNw04OPH33-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