<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62976>62976</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            atomic_fence's memory barrier greater than a function call, lead to auto inline missing optimization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          youngbe
      </td>
    </tr>
</table>

<pre>
    For the code:
test.c:
```c
#include <stddef.h>
#include <stdatomic.h>

void test(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long);
void test1(unsigned long *);
void test2(void);
void kkk2(void);

int kkk()
{
 unsigned long k[512];
    unsigned long k2[512];
    test1(k2);
 for (size_t i=0; i<512; ++i)
        k[i] = k2[i];

    k[1] = 3;
    k[2] = 3;
    k[3] = 3;
    k[4] = 3;
    k[5] = 3;
 kkk2();
    test(k[1], k[2], k[3], k[4], k[5]);

    for (size_t i=0; i<512; ++i)
        k[i] = k2[i];
    test1(k);
    return 0;
}
```
test2.c:
```c
#include <stdatomic.h>

void kkk2(void)
{
 atomic_signal_fence(memory_order_acq_rel);
}
```

After compile by clang 16.0.0 with -O3:
test.s:
```assembly
 kkk: # @kkk
        pushq   %r14
        pushq   %rbx
 subq    $8200, %rsp                     # imm = 0x2008
        movq %rsp, %rbx
        movq    %rbx, %rdi
        callq   test1@PLT
 callq   kkk2@PLT
        movl    $3, %edi
        movl    $3, %esi
        movl    $3, %edx
        movl    $3, %ecx
        movl $3, %r8d
        callq   test@PLT
        leaq    4096(%rsp), %r14
        movl    $4096, %edx                     # imm = 0x1000
 movq    %r14, %rdi
        movq    %rbx, %rsi
        callq memcpy@PLT
        movq    %r14, %rdi
        callq   test1@PLT
 xorl    %eax, %eax
        addq    $8200, %rsp                     # imm = 0x2008
        popq    %rbx
        popq    %r14
 retq
```
test2.s:
```assembly
kkk2: # @kkk2
        #MEMBARRIER
        retq
```


However, if I put test2.c and test.c together, the kkk2 will be automatically inline to kkk, this make kkk function even more complex.

For code:
test.c:
```c
#include <stddef.h>
#include <stdatomic.h>

void test(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long);
void test1(unsigned long *);
void test2(void);
void kkk2(void);

int kkk()
{
    unsigned long k[512];
    unsigned long k2[512];
    test1(k2);
    for (size_t i=0; i<512; ++i)
        k[i] = k2[i];

    k[1] = 3;
    k[2] = 3;
    k[3] = 3;
    k[4] = 3;
    k[5] = 3;
    kkk2();

 test(k[1], k[2], k[3], k[4], k[5]);

    for (size_t i=0; i<512; ++i)
        k[i] = k2[i];
    test1(k);
    return 0;
}
void kkk2()
{
 atomic_signal_fence(memory_order_acq_rel);
}
```
After compile:
test.s
```assembly
kkk: # @kkk
        pushq   %r14
        pushq   %rbx
        subq $8200, %rsp                     # imm = 0x2008
        leaq    4096(%rsp), %rbx
        movq    %rbx, %rdi
        callq   test1@PLT
 movq    %rsp, %r14
        movl    $4096, %edx                     # imm = 0x1000
        movq    %r14, %rdi
        movq    %rbx, %rsi
 callq   memcpy@PLT
        movq    $3, 8(%rsp)
        movq    $3, 16(%rsp)
        movq    $3, 24(%rsp)
        movq    $3, 32(%rsp)
 movq    $3, 40(%rsp)
        #MEMBARRIER
        movq    8(%rsp), %rdi
        movq    16(%rsp), %rsi
        movq    24(%rsp), %rdx
 movq    32(%rsp), %rcx
        movq    40(%rsp), %r8
 callq   test@PLT
        movl    $4096, %edx                     # imm = 0x1000
        movq    %r14, %rdi
        movq    %rbx, %rsi
 callq   memcpy@PLT
        movq    %r14, %rdi
        callq test1@PLT
        xorl    %eax, %eax
        addq    $8200, %rsp # imm = 0x2008
        popq    %rbx
        popq %r14
        retq

kkk2:                                   # @kkk2
        #MEMBARRIER
        retq
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWF1vozgX_jXOzdFExgZCLnKRTCZ6R3pHu6r2PjLgJN4anBrTNvPrVwaTAIE2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