[llvm] [win/arm64] Enable tail call with inreg arguments when possible (PR #134671)

Hans Wennborg via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 00:49:59 PDT 2025


================
@@ -8703,13 +8703,19 @@ bool AArch64TargetLowering::isEligibleForTailCallOptimization(
       return false;
 
     // On Windows, "inreg" attributes signify non-aggregate indirect returns.
-    // In this case, it is necessary to save/restore X0 in the callee. Tail
-    // call opt interferes with this. So we disable tail call opt when the
-    // caller has an argument with "inreg" attribute.
-
-    // FIXME: Check whether the callee also has an "inreg" argument.
-    if (i->hasInRegAttr())
-      return false;
+    // In this case, it is necessary to save X0/X1 in the callee and return it
+    // in X0. Tail call opt may interfere with this, so we disable tail call
+    // opt when the caller has an "inreg" attribute -- except if the callee
+    // also has that attribute on the same argument, and the same value is
+    // passed.
+    if (i->hasInRegAttr()) {
+      unsigned ArgNum = i - CallerF.arg_begin();
+      if (!CLI.CB || CLI.CB->arg_size() <= ArgNum ||
+          !CLI.CB->getParamAttr(ArgNum, Attribute::InReg).isValid() ||
----------------
zmodem wrote:

> I think this should check that both the caller and callee argument are also marked "sret"

Done.

> Can we skip this whole loop if `CLI.CB && CLI.CB->isMustTailCall()` is true?

As Eli said, I think some of the checks are redundant for musttail, but not all. And I want regular "no-must" tail calls to work too, so I'm not sure that it would make the code simpler.

https://github.com/llvm/llvm-project/pull/134671


More information about the llvm-commits mailing list