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

    <tr>
        <th>Summary</th>
        <td>
            How to add "add __attribute__((annotate("XXX")))" to a function declaration and keep @llvm.global.annotations in LLVM IR?
        </td>
    </tr>

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

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

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

<pre>
    I want to `add __attribute__((annotate("XXX")))` to a `function declaration` to make some modifications in its behavior during static analysis. However, I've noticed that if `__attribute__((annotate("XXX")))` is only added to a `function declaration`, the information "`XXX`" is not retained in the compiled LLVM IR. For example:

```
__attribute__((annotate("XXX")))
char *swap(char *a, char *b);
```
Compiled with the command` clang -c -emit-llvm -S test.c -o test.ll`, the resulting `test.ll` file is:

```
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-macosx14.0.0"

!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7}
!llvm.ident = !{!8}
```

However, for a `function definition that includes a function body` with `__attribute__((annotate("XXX")))`, like:
```
__attribute__((annotate("XXX")))
char *swap(char *a, char *b)
{
    // Function body
}
```
The generated `test.ll `file is:

```
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-macosx14.0.0"

@.str = private unnamed_addr constant [4 x i8] c"XXX\00", section "llvm.metadata"
@.str.1 = private unnamed_addr constant [7 x i8] c"test.c\00", section "llvm.metadata"
@llvm.global.annotations = appending global [1 x { i8*, i8*, i8*, i32, i8* }] [{ i8*, i8*, i8*, i32, i8* } { i8* bitcast (i8* (i8*, i8*)* @swap to i8*), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0), i32 2, i8* null }], section "llvm.metadata"

; Function Attrs: noinline nounwind optnone ssp uwtable
define i8* @swap(i8* noundef %0, i8* noundef %1) #0 {
  ...
}
...
```

Here, the relationship between "`XXX`" and `swap()` is preserved in the `@llvm.global.annotations` global variable.

I'm  want to know if it's possible to only add `__attribute__((annotate("XXX")))` to a `function declaration` and still have the relationship between the function and "XXX" (`@llvm.global.annotations`) preserved in the .ll file?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkV0tv4z4O_zTKhYhhy4mdHHJoGwRTYOays1jMraAtJtFWlgxJTtpvv6DiPDrTefWwlz8QWE_-KP4oUgyGoHeWaCXm92K-nuAQ986vwn5Au3vRaHfPaCeNU6-rRziijRAdiCpHpeDpCWP0uhkiPT0JuRBygda6iJHSSH779k1IKeRy_FU5SyPLbwfbRu0sKGoNeuT-uN7hM0FwHUHnlN7qNi0G0BZ0DNDQHg_aeVCD13YHIWLULaBF8xp0yOCTO9KBvJAP8ChkfSCwLuqWFMQ9RtBb1v-Rs-sAzppXQKUY7NeWsPq4J9B263yXZoEBq5yReV0yoHURPEXUlhRbyCKt63ptSMHnz__5Ao__ymDjPNALdr0hUd6JfC3y87fKx18a_rVZSardowch78IReyEX5yGyDedBw7vL-3e1PpwPfNRxfzahQ6uYtdag3cG0hSl1Ok6NOXQw_QqRQsxamLpTz5gbzjyFwUR2rqjy6zJstSHQ4dcUiPIevjg1GHpcgyjXIGR9UiZkfdoS3OBbemI4ix2Nu-R5lzztiuh3FEFhRIOvbojnfTTtRHnnprqaifKumk11IReivCvkYmpLeZr7WiS-30BFr3tzUYe-q2ZT7HtD0w5bF16KWZZn-UVq_MqCOcu6ZFO2NbgLI0Qh6nshi8SbkEUxtnJsy7Gdje18bKuxrUW9fqtDK7LxLfjiuuk7ntP3Jti2zv8QEVttdeqeQs-2ZlAUAOGyh1ML-zZdnY8FJis3-vkmNP6vQXFSWY-hAQAg5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