[PATCH] D42902: [compiler-rt] Workaround for endless recursion in ctzdi2

Jonas Devlieghere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 5 03:18:27 PST 2018


JDevlieghere created this revision.
JDevlieghere added reviewers: howard.hinnant, compnerd, joerg.
Herald added subscribers: Sanitizers, fedor.sergeev, krytarowski, arichardson, dberris, sdardis, emaste, jyknight.

Workaround for LLVM bug 11663. Prevent endless recursion in
__c?zdi2(), where calls to __builtin_c?z() are expanded to
__c?zdi2() instead of __c?zsi2().

Instead of placing this workaround in c?zdi2.c, put it in this global
header to prevent other C files from making the detour through
__c?zdi2() as well.

This problem has been observed on FreeBSD for sparc64 and mips64 with
GCC 4.2.1, and for riscv with GCC 5.2.0. Presumably it's any version of
GCC, and targeting an arch that does not have dedicated bit counting
instructions.

PR: https://bugs.llvm.org/show_bug.cgi?id=11663

This problem was brought to my attention by Adrian Glaubitz yesterday at
FOSDEM. He asked me if I could help him get this upstream. The workaround is
already present in Rust's version of compiler-rt, but obviously they'd like to have 
this upstream: https://github.com/rust-lang/compiler-rt/pull/35/files


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D42902

Files:
  compiler-rt/lib/builtins/int_lib.h


Index: compiler-rt/lib/builtins/int_lib.h
===================================================================
--- compiler-rt/lib/builtins/int_lib.h
+++ compiler-rt/lib/builtins/int_lib.h
@@ -77,6 +77,29 @@
 /* Include internal utility function declarations. */
 #include "int_util.h"
 
+/*
+ * Workaround for LLVM bug 11663.  Prevent endless recursion in
+ * __c?zdi2(), where calls to __builtin_c?z() are expanded to
+ * __c?zdi2() instead of __c?zsi2().
+ *
+ * Instead of placing this workaround in c?zdi2.c, put it in this
+ * global header to prevent other C files from making the detour
+ * through __c?zdi2() as well.
+ *
+ * This problem has been observed on FreeBSD for sparc64 and
+ * mips64 with GCC 4.2.1, and for riscv with GCC 5.2.0.
+ * Presumably it's any version of GCC, and targeting an arch that
+ * does not have dedicated bit counting instructions.
+ */
+#if ((defined(__sparc__) && defined(__arch64__)) || defined(__mips_n64) \
+    || defined(__mips_o64) || defined(__riscv__)                        \
+    || (defined(_MIPS_SIM) && ((_MIPS_SIM == _ABI64) || (_MIPS_SIM == _ABIO64))))
+si_int __clzsi2(si_int);
+si_int __ctzsi2(si_int);
+#define __builtin_clz __clzsi2
+#define __builtin_ctz __ctzsi2
+#endif /* sparc64 || mips_n64 || mips_o64 || riscv */
+
 COMPILER_RT_ABI si_int __paritysi2(si_int a);
 COMPILER_RT_ABI si_int __paritydi2(di_int a);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42902.132800.patch
Type: text/x-patch
Size: 1378 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180205/83eb2c99/attachment.bin>


More information about the llvm-commits mailing list