[llvm] [CMake] Fix __builtin_thread_pointer check with LTO (PR #70968)

Sam James via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 1 11:17:01 PDT 2023


https://github.com/thesamesam created https://github.com/llvm/llvm-project/pull/70968

With LTO, gcc's IPA passes might drop the foo() function and then the test will pass even on platforms where __builtin_thread_pointer is unavailable.

On PPC64, we get this as a result:
```
llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp:361:61: error: ‘__builtin_thread_pointer’ is not supported on this targ
```

Just mark the function in the CMake configure test with the 'used' attribute to avoid it being optimised out. The test then behaves correctly with -flto.

Reported-by: matoro

>From bb5bf261792de2de3b4da2a8a065fe404a16103b Mon Sep 17 00:00:00 2001
From: Sam James <sam at gentoo.org>
Date: Wed, 1 Nov 2023 18:12:16 +0000
Subject: [PATCH] [CMake] Fix __builtin_thread_pointer check with LTO
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

With LTO, gcc's IPA passes might drop the foo() function and then the test
will pass even on platforms where __builtin_thread_pointer is unavailable.

On PPC64, we get this as a result:
```
llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp:361:61: error: ‘__builtin_thread_pointer’ is not supported on this targ
```

Just mark the function in the CMake configure test with the 'used' attribute to
avoid it being optimised out. The test then behaves correctly with -flto.

Reported-by: matoro
---
 llvm/cmake/config-ix.cmake | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/llvm/cmake/config-ix.cmake b/llvm/cmake/config-ix.cmake
index 64c63e199ffa4e4..846879cf55e3cd1 100644
--- a/llvm/cmake/config-ix.cmake
+++ b/llvm/cmake/config-ix.cmake
@@ -65,7 +65,12 @@ check_include_file(fenv.h HAVE_FENV_H)
 check_symbol_exists(FE_ALL_EXCEPT "fenv.h" HAVE_DECL_FE_ALL_EXCEPT)
 check_symbol_exists(FE_INEXACT "fenv.h" HAVE_DECL_FE_INEXACT)
 check_c_source_compiles("
-        void *foo() {
+        #if __has_attribute(unused)
+        #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
+        #else
+        #define LLVM_ATTRIBUTE_UNUSED
+        #endif
+        LLVM_ATTRIBUTE_UNUSED void *foo() {
           return __builtin_thread_pointer();
         }
         int main(void) { return 0; }"



More information about the llvm-commits mailing list