From 619901e8163c9f7e9691d1215ca263ece7afdb22 Mon Sep 17 00:00:00 2001 From: Matthias Braun <matze@braunis.de> Date: Tue, 25 Mar 2025 18:16:27 -0700 Subject: [PATCH 1/2] Add __attribute__((__retain__)) to LLVM_ATTRIBUTE_USED Without the retain attribute the dump functions will be stripped when LLVM is compiled with `-ffunction-section -Wl,--gc-sections` on ELF-based systems. --- llvm/include/llvm/Support/Compiler.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index dc8b5389069eb..b6d06238f8d83 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -224,7 +224,9 @@ #define LLVM_PREFETCH(addr, rw, locality) #endif -#if __has_attribute(used) +#if __has_attribute(used) && __has_attribute(retain) +#define LLVM_ATTRIBUTE_USED __attribute__((__used__, __retain__)) +#elif __has_attribute(used) #define LLVM_ATTRIBUTE_USED __attribute__((__used__)) #else #define LLVM_ATTRIBUTE_USED From 584a2db08aaf66dffd9be66a47257107e16acc20 Mon Sep 17 00:00:00 2001 From: Matthias Braun <matze@braunis.de> Date: Wed, 26 Mar 2025 09:58:08 -0700 Subject: [PATCH 2/2] Add separate LLVM_ATTRIBUTE_RETAIN macro --- llvm/include/llvm/Support/Compiler.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index b6d06238f8d83..ff6f5e44ae2f1 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -224,14 +224,18 @@ #define LLVM_PREFETCH(addr, rw, locality) #endif -#if __has_attribute(used) && __has_attribute(retain) -#define LLVM_ATTRIBUTE_USED __attribute__((__used__, __retain__)) -#elif __has_attribute(used) +#if __has_attribute(used) #define LLVM_ATTRIBUTE_USED __attribute__((__used__)) #else #define LLVM_ATTRIBUTE_USED #endif +#if __has_attribute(retain) +#define LLVM_ATTRIBUTE_RETAIN __attribute__((__retain__)) +#else +#define LLVM_ATTRIBUTE_RETAIN +#endif + #if defined(__clang__) #define LLVM_DEPRECATED(MSG, FIX) __attribute__((deprecated(MSG, FIX))) #else @@ -621,7 +625,8 @@ void AnnotateIgnoreWritesEnd(const char *file, int line); /// get stripped in release builds. // FIXME: Move this to a private config.h as it's not usable in public headers. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) -#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED +#define LLVM_DUMP_METHOD \ + LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED LLVM_ATTRIBUTE_RETAIN #else #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE #endif