[compiler-rt] [compiler-rt] Make sure __clzdi2 doesn't call itself recursively on sparc64 (PR #136737)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 22 11:03:54 PDT 2025


https://github.com/koachan created https://github.com/llvm/llvm-project/pull/136737

On 64-bit platforms, libgcc doesn't ship with __clzsi2, so __builtin_clz gets lowered to __clzdi2. A check already exists for GCC, but as of commit 8210ca019839fc5430b3a95d7caf5c829df3232a clang also lowers __builtin_clz to __clzdi2 on sparc64.

Update the check so that building __clzdi2 with clang/sparc64 also works.

>From 504f08f60b8ef2008960b88d5d0a39ee788976a0 Mon Sep 17 00:00:00 2001
From: Koakuma <koachan at protonmail.com>
Date: Wed, 23 Apr 2025 00:42:43 +0700
Subject: [PATCH] [compiler-rt] Make sure __clzdi2 doesn't call itself
 recursively on sparc64

On 64-bit platforms, libgcc doesn't ship with __clzsi2, so __builtin_clz
gets lowered to __clzdi2. A check already exists for GCC, but as of
commit 8210ca019839fc5430b3a95d7caf5c829df3232a clang also lowers __builtin_clz
to __clzdi2 on sparc64.

Update the check so that building __clzdi2 with clang/sparc64 also works.
---
 compiler-rt/lib/builtins/clzdi2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/builtins/clzdi2.c b/compiler-rt/lib/builtins/clzdi2.c
index 12c17982a5cb1..41fd12533b360 100644
--- a/compiler-rt/lib/builtins/clzdi2.c
+++ b/compiler-rt/lib/builtins/clzdi2.c
@@ -14,12 +14,12 @@
 
 // Returns: the number of leading 0-bits
 
-#if !defined(__clang__) &&                                                     \
-    ((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) ||       \
+#if ((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) ||       \
      (defined(__riscv) && __SIZEOF_POINTER__ >= 8))
 // On 64-bit architectures with neither a native clz instruction nor a native
-// ctz instruction, gcc resolves __builtin_clz to __clzdi2 rather than
+// ctz instruction, resolves __builtin_clz resolves to __clzdi2 rather than
 // __clzsi2, leading to infinite recursion.
+// This is because on those platforms, libgcc doesn't ship with __clzsi2.
 #define __builtin_clz(a) __clzsi2(a)
 extern int __clzsi2(si_int);
 #endif



More information about the llvm-commits mailing list