<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55190>55190</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang's annotate attribute / llvm.ptr.annotation leads to suboptimal codegen
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
zmodem
</td>
</tr>
</table>
<pre>
Consider:
```
$ cat /tmp/a.cc
struct AutoReset {
AutoReset(int *ptr, int val) : ptr_(ptr), orig_val_(*ptr) { *ptr_ = val; }
~AutoReset() { *ptr_ = orig_val_; }
#ifdef ANNOTATE
__attribute__((annotate("nanana")))
#endif
int *ptr_;
int orig_val_;
};
void bar(int *p);
void foo(int* p) {
AutoReset ar(p, 42);
bar(p);
}
$ clang -c -O3 -fno-exceptions /tmp/a.cc && objdump -dr a.o
a.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z3fooPi>:
0: 55 push %rbp
1: 53 push %rbx
2: 50 push %rax
3: 48 89 fb mov %rdi,%rbx
6: 8b 2f mov (%rdi),%ebp
8: c7 07 2a 00 00 00 movl $0x2a,(%rdi)
e: e8 00 00 00 00 call 13 <_Z3fooPi+0x13>
f: R_X86_64_PLT32 _Z3barPi-0x4
13: 89 2b mov %ebp,(%rbx)
15: 48 83 c4 08 add $0x8,%rsp
19: 5b pop %rbx
1a: 5d pop %rbp
1b: c3 ret
$ clang -c -O3 -fno-exceptions -DANNOTATE /tmp/a.cc && objdump -dr a.o
a.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z3fooPi>:
0: 48 83 ec 18 sub $0x18,%rsp
4: 48 89 7c 24 08 mov %rdi,0x8(%rsp)
9: 8b 07 mov (%rdi),%eax
b: 89 44 24 10 mov %eax,0x10(%rsp)
f: c7 07 2a 00 00 00 movl $0x2a,(%rdi)
15: e8 00 00 00 00 call 1a <_Z3fooPi+0x1a>
16: R_X86_64_PLT32 _Z3barPi-0x4
1a: 8b 44 24 10 mov 0x10(%rsp),%eax
1e: 48 8b 4c 24 08 mov 0x8(%rsp),%rcx
23: 89 01 mov %eax,(%rcx)
25: 48 83 c4 18 add $0x18,%rsp
29: c3 ret
```
Note that the second version, which includes the "annotate" attribute, generates worse code.
While the "annotate" attribute isn't documented anywhere, users seem to use it both for providing info to custom backends, as well as for consumers which just look at the AST such as the clang static analyzer or custom plugins and tooling. The difference in generated code seems unfortunate for the second use case.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzdVstyozgU_Rq8uWUXCGzDwgsnTlZT6a6eVM1Ub1xCCKM0IAoJx-mvnyvx9CPxLGY1mMIW0jn36tyHFcvkY_MoSyUSXjv-1nF3jts_V253t0MSAKMaHPKsiwqfdMFYO6V03TAN20bLH1xxXLN-aGdgfOmQUJQGvq107ZBHMKMjzR0SAVoGfLvHNXYyMvOyFoc9LjBve1RkqDuOPcJ2lsHHV-tdb9FZP02N3gKN1FNov09fpAlPYfvy8u11-_rU8-73VOtaxI3m-9ankJal1FRzOyIlNR_8YTdg756Rl4lIeyJzjVIYH_oZ83bqW4dHB4ff9nmUIoGY1hNNjbUbi1Ip20W4BqpOi6vYgKWqjOoBmTJBZ-Wc_lIwTIyclgeYM5h_82GelnLOT4xXWmBqnWcMjlZ4g4zfkqaoYJ7UQBdySmiGfqtTKnKOe6gLTDyep6tgfgpX81VwZt8-d0JRpXgR5x8gU1CcGeOw0PykLxLbvbgwJR73P32U6rtw_KdhNdp3W0eWS7h5VY3K8MshyzquBpDXgfz7oNMAIh3IvQuiI8hvQUEIYQRpfAkq5BE6UCIwuBcmVy06jIGkN0wO6LAniFoOPtlr2HKwNbhrIBRQzfYeOHLLEbgnQi18wtaT8JaEhyPcnejAaG5IPP8sUOTBPXm-idekrK6v1HD_2P8drvarYP_9j1efdDPIhMn9XczdU9BzeJ2iKCe5kvNMUSPCsB0UddyOtxyj4gMLwA3POGiSQK9J2EVFD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