]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: compiler: add a function attribute to disable tail calls
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Aug 2026 13:14:31 +0000 (15:14 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 13 Aug 2026 15:43:30 +0000 (17:43 +0200)
In some cases we want a tail call to be turned into a jump, which is
sometimes referred to as "tail call elimination". Gcc and clang have
options for this, so let's add an attribute "eliminate_tail_calls" that
works with both.

include/haproxy/compiler.h

index b74e636bbe1bc72bfe978de94e95cdfa534f7b55..72792476cc53b76f3bdd2ca6990909acb9a00e93 100644 (file)
 #  define __fallthrough do { } while (0)
 #endif
 
+/* tail-call elimination (turn the final call of a function into a jump). This
+ * applies to the calling function. Clang doesn't support -f type optimizations
+ * but has a dedicated attribute that we can use. Modern versions have a
+ * "musttail" attribute at the statement level (per-return), but that's harder
+ * to turn into something portable.
+ */
+#if defined(__clang__) && __has_attribute(disable_tail_calls)
+#  define eliminate_tail_calls __attribute__((disable_tail_calls))
+#elif !defined(__clang__) && __has_attribute(optimize)
+#  define eliminate_tail_calls __attribute__((optimize("optimize-sibling-calls")))
+#else
+#  define eliminate_tail_calls
+#endif
+
 #if !defined(__GNUC__)
 /* Some versions of glibc irresponsibly redefine __attribute__() to empty for
  * non-gcc compilers, and as such, silently break all constructors with other