<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/68345>68345</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang generates output for both libc function calls and builtin functions
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang,
libc
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rilysh
</td>
</tr>
</table>
<pre>
For the following C code
```c
#include <stdio.h>
#include <time.h>
static void sin_builtin_fn(int n)
{
clock_t prev, now;
prev = clock();
while (n--) {
__builtin_sinf(198811.0);
__builtin_sinf(598484.0);
__builtin_sinf(151545.0);
__builtin_sinf(205111.0);
__builtin_sinf(326215.0);
__builtin_sinf(165165.0);
__builtin_sinf(185465.0);
__builtin_sinf(185465.0);
}
now = clock();
fprintf(stdout, "took: %.2fs\n", ((double)(now - prev) / (double)(CLOCKS_PER_SEC)));
}
int main(void)
{
sin_builtin_fn(99999999);
}
```
Clang (17.0.1) generates the following assembly output (truncated):
```asm
sin_builtin_fn:
.LBB1_1:
movl -4(%rbp), %eax
movl %eax, %ecx
addl $-1, %ecx
movl %ecx, -4(%rbp)
cmpl $0, %eax
je .LBB1_3
movss .LCPI1_7(%rip), %xmm0
callq sinf@PLT
movss .LCPI1_6(%rip), %xmm0
callq sinf@PLT
movss .LCPI1_5(%rip), %xmm0
callq sinf@PLT
movss .LCPI1_4(%rip), %xmm0
callq sinf@PLT
movss .LCPI1_3(%rip), %xmm0
callq sinf@PLT
movss .LCPI1_2(%rip), %xmm0
callq sinf@PLT
movss .LCPI1_1(%rip), %xmm0
callq sinf@PLT
movss .LCPI1_1(%rip), %xmm0
callq sinf@PLT
jmp .LBB1_1
.LBB1_3:
callq clock@PLT
movq %rax, -24(%rbp)
movq stdout@GOTPCREL(%rip), %rax
movq (%rax), %rdi
movq -24(%rbp), %rax
subq -16(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd .LCPI1_0(%rip), %xmm1
divsd %xmm1, %xmm0
leaq .L.str(%rip), %rsi
movb $1, %al
callq fprintf@PLT
```
And here the output is calling libc's `sinf` function despite the builtin function is already provided. Somehow, it seems like in code-generation, for some reason, it tries to fall back to libc's `sinf` but also generates output for compiler builtin`sinf` implementation.
When I feed the same example C code to GCC, it generates the correct output.
GCC (13.2) generated (truncated):
```asm
movl %edi, -20(%rbp)
call clock@PLT
movq %rax, -8(%rbp)
nop
.L2:
movl -20(%rbp), %eax
leal -1(%rax), %edx
movl %edx, -20(%rbp)
testl %eax, %eax
jne .L2
call clock@PLT
movq %rax, -16(%rbp)
movq -16(%rbp), %rax
subq -8(%rbp), %rax
pxor %xmm0, %xmm0
cvtsi2sdq %rax, %xmm0
movsd .LC0(%rip), %xmm1
divsd %xmm1, %xmm0
movq %xmm0, %rdx
movq stdout(%rip), %rax
movq %rdx, %xmm0
leaq .LC1(%rip), %rdx
movq %rdx, %rsi
movq %rax, %rdi
movl $1, %eax
call fprintf@PLT
```
Tested results: https://godbolt.org/z/sr4Ghcxze
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV19vo74S_TTOixUE5k_gIQ8tbarVrbTV7kr3MTLYSdwam9omTffTX9lAAoTd9upX6bdapQHPnDkez5xxsNZsLyhdg_gWxHcL3JiDVGvF-Ls-LApJ3tcbqaA5ULiTnMs3JvYwh6UkFPh3wL_pPhO__V92zyhkouQNoRCEuTaESe8Awvu5VcMqOlx0n9pgw0p4lIxAzcS2aBg3TGx3AqCUCQMFQFnnsLrtPbOSy_Jla2Ct6BGgHAr5BsLL8vmLXYcgvIPOAaDUol0MO6u3A-MUApSK5RKgDA4jAT_bnllpJnYApUGWpkHg-WOwWdM4S6M0-pRpEAdxFH_KFPlx8EkCIUpQ8DnUIImD5JOmaRz9Q9PV3eQUhHz7-Kh2tWLCWFxtiGyMPXyAkJHyBYQ3EKDYQzsN4lwAhNrFFKCUyKbg1CKi1MZZdpWTQYA2cGKQP37P__Nz-3T_Y_v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