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

    <tr>
        <th>Summary</th>
        <td>
            clang optimize __attribute__((__const__)) inline function call invalidly
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Ratinale:
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17803

Reproduce:

test.h
```c
#include <stdbool.h>
struct util_cpu_caps_t {
    int a;
    int b;
};
#    define likely(x)   __builtin_expect(!!(x), 1)
#    define unlikely(x) __builtin_expect(!!(x), 0)
static inline __attribute__((__const__)) const struct util_cpu_caps_t *
util_get_cpu_caps(void)
{
   extern void _util_cpu_detect_local(void);
   extern struct util_cpu_caps_t util_cpu_caps;
   extern bool util_cpu_caps_detect_done;

   if (unlikely(!util_cpu_caps_detect_done)) {
      _util_cpu_detect_local();
      util_cpu_caps_detect_done = true;
   }

   return &util_cpu_caps;
}
```

test.c
```c
#include "test.h"

#include <stdio.h>
#include <pthread.h>

struct util_cpu_caps_t util_cpu_caps = {};
bool util_cpu_caps_detect_done = false;
void _util_cpu_detect_local(void);
pthread_once_t once_control = PTHREAD_ONCE_INIT;

void util_cpu_detect_once(void) {
    util_cpu_caps.a = 1;
    printf("The value a is:%d\n", util_cpu_caps.a);
    util_cpu_caps.b = 2;
    printf("The value b is:%d\n", util_cpu_caps.b);
}
void _util_cpu_detect_local(void)
{
    pthread_once(&once_control, util_cpu_detect_once);
}

void func() {
    if (util_get_cpu_caps()->a) {
        printf("A got\n");
    }
    if (util_get_cpu_caps()->b) {
        printf("B got\n");
    }
}

int main() {
    func();
    return 0;
}

```

compile procedure:
```bash
lygstate@lygstate-Standard-PC-Q35-ICH9-2009:~/work/mesa$ clang -g test.c -o test^C
lygstate@lygstate-Standard-PC-Q35-ICH9-2009:~/work/mesa$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64
lygstate@lygstate-Standard-PC-Q35-ICH9-2009:~/work/mesa$ clang -O3 test.c -o test
lygstate@lygstate-Standard-PC-Q35-ICH9-2009:~/work/mesa$ ./test
lygstate@lygstate-Standard-PC-Q35-ICH9-2009:~/work/mesa$ clang test.c -o test
lygstate@lygstate-Standard-PC-Q35-ICH9-2009:~/work/mesa$ ./test
The value a is:1
The value b is:2
A got
B got
lygstate@lygstate-Standard-PC-Q35-ICH9-2009:~/work/mesa$ 
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9V1-T4igQ_zTxhUoqEo3mwYcZnbmdl9293bnnFAGM3GDIJTDn3Ke_hkTzR531qqzTVAI03f3rpruBTLGP1Q-iRUEk96IHL9x44cNO67K2PfwMTy60JFmwrThnvH7TqgxUlQNhz2vSfXzXrHKeVvwvw2tdw8B0sQyjRmjz_sHLSjFDO2XNWwNDsGtH4rB5aNvHkSioNIwjL1rXmmVKSZgcPTX0WleGamS0kCktTUpJWacaeYvHho7gJwqNiBeNRrLTiLfYdG0c2QmMb0XBkRRvXH54eHnwcALDaZoZIcFjKT-UnGqgeHjqHjfFw2s0td8LskwxlHaLrPAkq9awUBSASysrTYnWlciM5mnqGJdpSlVRa9tNrHjXQ9fcg1vXO0LO9YkIkt6VYJ0NPUfyg-ZVgSwdpSeRjGtAn0pFiexxR2d8V7AM-hfY7IKPmFqVTBW8W7gjm9iCecuet8Gl19kbZw3CBV03bmQY_K5KhnDdILCY9xlspA3RVlwbMNLD8WU_dBzHxDjLHPrrzMG4zTGM--xnySVUL7WG1FLvKk5Yn_5ZAg76z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