<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54675>54675</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DWARF] Type definition appears in nameless and empty DW_TAG_subprogram
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
dwblaikie
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jmorse
</td>
</tr>
</table>
<pre>
Using clang 12a2f7494e745eb4 from ~2 weeks ago, the type definition in the contrived code below ends up in a DW_TAG_subprogram with no name. This is an obstacle to parsing, and is arguably incorrect as information about the scope of the type is lost. Compiling with `-O2 -g -c`, or any scenario where "foo" is inlined into "bar":
```
static int foo(int wat) {
struct beans {
int a;
};
struct beans baz;
baz.a = 0;
baz.a += wat;
baz.a /= 2;
return baz.a;
}
int bar(int input) {
return foo(input);
}
```
The DWARF definition for `struct beans` ends up being embedded in a nameless subprogram at the unit level:
```
0x00000045: DW_TAG_subprogram
0x00000046: DW_TAG_structure_type
DW_AT_calling_convention (DW_CC_pass_by_value)
DW_AT_name ("beans")
DW_AT_byte_size (0x04)
DW_AT_decl_file ("/tmp/nou.cpp")
DW_AT_decl_line (2)
0x0000004c: DW_TAG_member
DW_AT_name ("a")
DW_AT_type (0x00000041 "int")
DW_AT_decl_file ("/tmp/nou.cpp")
DW_AT_decl_line (3)
DW_AT_data_member_location (0x00)
0x00000055: NULL
0x00000056: NULL
```
Elsewhere in the file are the definition for `bar`, the abstract origin of `foo` and it's DW_TAG_inlined_subroutine. I would have expected the type definition to end up in the abstract origin for `foo`, rather than an empty subprogram. Note that if you remove `static` from the definition of`foo`, then a definition for `foo` appears in the output and has the type definition for `struct beans` inside it's subprogram. Again, in that case I would have expected the type definition to be in the abstract origin. That's where GCC puts it, at least.
I had a step through, and the cause seems to be recursion -- when constructing the abstract scope for the inlined copy of "foo", the abstract origin DIE hasn't been inserted with DwarfUnit::insertDIE yet. When looking up the context to put `struct beans` into, that causes the call to `getOrCreateContextDIE` [0] to return an empty subprogram, rather than the abstract origins. I'm not that familiar with the DWARF emitter, so a fix hasn't immediately occurred to me.
[0] https://github.com/llvm/llvm-project/blob/a5032b26337bc7d877c3ab4e7f18265a3c044df9/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp#L624
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVl1v4joQ_TXwYhWlCTTlgQcWlmql1e7VVVf7iJxkAt46cWQ7peyvv2ccPkJL9-MiBIk9njlzZjwzmSn2s29O1RuRa4nf21jGZTqejikdTygbi9KaSgzSj7HYET05ITdmEC-E35Lw-4ZEQaWqlVemFqoOy7mpvVXPVOCpIJGRNjtBdeFE27CMFMvv68f5w9q1WWPNxspK7JTfitqIWlY0Eo9b5QS-shYmc17mGsaMaKRlpGxe1kUQsJtWZnoPtbmxlnIvJE7WpbGVDJhkZlofYLncAK4pz9ChQBvnR2JhqkZpJiHgGNxFN19jcbMRNzme2Z6xMLmHDqqlVUbstmRJDOK4NKAjZlWqhgY4rWpAxVomLX4HyXwQLQfRnBV13_AKr7zKWVgEFff8tJN-EE_B9odOSAjnbQunMpK166-LcFIOktPKIF323i7OZfJnbwtvIykGyVJEb1fjD7zBON5urXgr7m1Y8q2tu_3TMuPoHsIvwwxMBAdV3bSvXTxoObLQCVxRd8nfI4K4_D7_d9VPQISdg9d3Hq-n3MuIQ0xVRkURAoVU5ITT5JzoJaPsEqaFVqHpmfR7QYxeovAZTyCBmLzJ6z4TJ-G7TvgsHuC2ltaclecIX34gPH9c51Jznq5xx56pDk6HD6iDwGKxbqRz62y_fpa6JSbyl-rY-8NxzthAGHL2d8eyvae1Uz9JnKzDu_FvzxWU63WpdO9cMLfyVYPf2rSjvGn-BEHQxPftrOl86BXf-ZHvE-MV54B9z8IlOR1G-WtQxyOhrBzo6IzfcilA6v_Z-UuC_g891wmCpuRPDkkvD-Sstcm7Cnp05x16J5MzvV--ff58VeaU8j2Jy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