[Git][ghc/ghc][master] CmmToC: emit explicit tail calls when the C compiler supports it

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Tue Oct 11 16:50:15 UTC 2022



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
66af1399 by Cheng Shao at 2022-10-11T12:49:59-04:00
CmmToC: emit explicit tail calls when the C compiler supports it

Clang 13+ supports annotating a return statement using the musttail
attribute, which guarantees that it lowers to a tail call if compilation
succeeds.

This patch takes advantage of that feature for the unregisterised code
generator. The configure script tests availability of the musttail
attribute, if it's available, the Cmm tail calls will become C tail
calls that avoids the mini interpreter trampoline overhead. Nothing is
affected if the musttail attribute is not supported.

Clang documentation:
https://clang.llvm.org/docs/AttributeReference.html#musttail

- - - - -


3 changed files:

- configure.ac
- + m4/fp_musttail.m4
- rts/include/Stg.h


Changes:

=====================================
configure.ac
=====================================
@@ -996,6 +996,8 @@ fi
 
 FP_VISIBILITY_HIDDEN
 
+FP_MUSTTAIL
+
 dnl ** check for librt
 AC_CHECK_LIB([rt], [clock_gettime])
 AC_CHECK_LIB([rt], [clock_gettime], [AC_SUBST([CabalHaveLibrt], [True])], [AC_SUBST([CabalHaveLibrt], [False])])


=====================================
m4/fp_musttail.m4
=====================================
@@ -0,0 +1,16 @@
+# FP_MUSTTAIL
+# ----------------------------------
+# Is the musttail attribute supported?
+AC_DEFUN([FP_MUSTTAIL],
+[
+    AC_MSG_CHECKING([whether __attribute__((musttail)) is supported])
+    echo 'extern int foo(void); int bar(void) { __attribute__((musttail)) return foo(); }' > conftest.c
+    if $CC -c conftest.c -o conftest.o
+    then
+        AC_MSG_RESULT([yes])
+        AC_DEFINE(HAS_MUSTTAIL, 1, [Has musttail])
+    else
+        AC_MSG_RESULT([no])
+    fi
+    rm -f conftest.c conftest.o
+])


=====================================
rts/include/Stg.h
=====================================
@@ -325,7 +325,11 @@ external prototype return neither of these types to workaround #11395.
    Tail calls
    -------------------------------------------------------------------------- */
 
-#define JMP_(cont) return((StgFunPtr)(cont))
+#if defined(HAS_MUSTTAIL)
+#define JMP_(cont) { StgFunPtr (*_f)(void) = (StgFunPtr (*)(void))(cont); __attribute__((musttail)) return _f(); }
+#else
+#define JMP_(cont) return (StgFunPtr)(cont)
+#endif
 
 /* -----------------------------------------------------------------------------
    Other Stg stuff...
@@ -591,4 +595,3 @@ typedef union {
   c;                                            \
 })
 #endif
-



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/66af1399963a1872e520d1dbd1c94fd43e65082d

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/66af1399963a1872e520d1dbd1c94fd43e65082d
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20221011/815660a3/attachment-0001.html>


More information about the ghc-commits mailing list