Issue 75766
Summary How to add "add __attribute__((annotate("XXX")))" to a function declaration and keep @llvm.global.annotations in LLVM IR?
Labels new issue
Assignees
Reporter shuangxiangkan
    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?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to