When a split has no return value or does not return, the outlined
function becomes `void`-returning.  However, if the enclosing function
is non-`void` and has a `musttail` call, the call to the outlined split
is also annotated with `musttail`, leading to a return type mismatch.

Reject splits containing `musttail` calls when outlining would change
their return type.  Similarly, only apply `musttail` to the call to the
outlined split when it would match the return type.

`musttail_fnsplit1.C` is the reduced example I extracted from my code
that evoked this error, and the LLM extended it to cases 2--5.  This
issue affects versions 15+16 as well.  I could not find an active IPA
bug with this exact failure mode; should I open one?

Marked as RFC since if bugfixes like this count as legally significant,
I need to get approval from "${EMPLOYER}".

Disclosure: I used LLM assistance for creating tests, discovering what
utilities were available to probe the CFG, and assembling the changelog
below in the style of existing patches.

Thank you,
-T

gcc/ChangeLog:

        * ipa-split.cc (split_point): Add split_part_return_p.
        (split_part_has_musttail_p): New function.
        (consider_split): Determine whether the split part returns.  Check
        skip_return and the return type before checking for musttail calls.
        Reject incompatible musttail splits.
        (split_function): Use the recorded split_part_return_p.  Preserve
        musttail propagation when the return type matches, and omit it
        when outlining changes a non-void return type to void.

gcc/testsuite/ChangeLog:

        * g++.dg/opt/musttail-fnsplit1.C: New test.
        * g++.dg/opt/musttail-fnsplit2.C: New test.
        * g++.dg/opt/musttail-fnsplit3.C: New test.
        * g++.dg/opt/musttail-fnsplit4.C: New test.
        * g++.dg/opt/musttail-fnsplit5.C: New test.

Assisted-by: GPT-6.1 Sol

---

diff --git a/gcc/ipa-split.cc b/gcc/ipa-split.cc
index 84a51fd..98ae62e 100644
--- a/gcc/ipa-split.cc
+++ b/gcc/ipa-split.cc
@@ -144,6 +144,9 @@ public:
   /* True when return value is computed on split part and thus it needs
      to be returned.  */
   bool split_part_set_retval;
+
+  /* True when the split part reaches the return block.  */
+  bool split_part_return_p;
 };

 /* Best split point found.  */
@@ -420,6 +423,28 @@ split_part_set_ssa_name_p (tree val, class split_point 
*current,
              || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
 }

+/* Return true if SPLIT_POINT contains a musttail call.  */
+
+static bool
+split_part_has_musttail_p (const split_point *split_point)
+{
+  unsigned int i;
+  bitmap_iterator bi;
+
+  EXECUTE_IF_SET_IN_BITMAP (split_point->split_bbs, 0, i, bi)
+    {
+      basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
+
+      for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
+           !gsi_end_p (gsi); gsi_next (&gsi))
+        if (gcall *call = dyn_cast <gcall *> (gsi_stmt (gsi)))
+          if (gimple_call_must_tail_p (call))
+            return true;
+    }
+
+  return false;
+}
+
 /* We found an split_point CURRENT.  NON_SSA_VARS is bitmap of all non ssa
    variables used and RETURN_BB is return basic block.
    See if we can split function here.  */
@@ -681,6 +706,25 @@ consider_split (class split_point *current, bitmap 
non_ssa_vars,
   else
     current->split_part_set_retval = true;

+  /* See if the split function will return.  */
+  current->split_part_return_p = false;
+  FOR_EACH_EDGE (e, ei, return_bb->preds)
+    if (bitmap_bit_p (current->split_bbs, e->src->index))
+      current->split_part_return_p = true;
+
+  /* Reject splits that would change the return type of a musttail chain.  */
+  bool skip_return = (!current->split_part_return_p
+                      || !current->split_part_set_retval);
+  if (skip_return
+      && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
+      && cfun->has_musttail
+      && split_part_has_musttail_p (current))
+    {
+      if (dump_file && (dump_flags & TDF_DETAILS))
+        fprintf (dump_file, "  Refused: incompatible musttail return type\n");
+      return;
+    }
+
   /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
      for the return value.  If there are other PHIs, give up.  */
   if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
@@ -1253,13 +1297,7 @@ split_function (basic_block return_bb, class split_point 
*split_point,
        args_to_pass.safe_push (arg);
       }

-  /* See if the split function will return.  */
-  bool split_part_return_p = false;
-  FOR_EACH_EDGE (e, ei, return_bb->preds)
-    {
-      if (bitmap_bit_p (split_point->split_bbs, e->src->index))
-       split_part_return_p = true;
-    }
+  bool split_part_return_p = split_point->split_part_return_p;

   /* Add return block to what will become the split function.
      We do not return; no return block is needed.  */
@@ -1507,7 +1545,9 @@ split_function (basic_block return_bb, class split_point 
*split_point,
        args_to_pass[i] = arg;
       }
   call = gimple_build_call_vec (node->decl, args_to_pass);
-  if (cur_node->get_fun ()->has_musttail && !add_tsan_func_exit)
+  if (cur_node->get_fun ()->has_musttail && !add_tsan_func_exit
+      && (!skip_return
+          || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))))
     gimple_call_set_must_tail (call, true);
   gimple_set_block (call, DECL_INITIAL (current_function_decl));
   args_to_pass.release ();
diff --git a/gcc/testsuite/g++.dg/opt/musttail-fnsplit1.C 
b/gcc/testsuite/g++.dg/opt/musttail-fnsplit1.C
new file mode 100644
index 0000000..d4075bf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/musttail-fnsplit1.C
@@ -0,0 +1,28 @@
+// { dg-do compile { target musttail } }
+// { dg-options "-O3 -fdump-tree-fnsplit" }
+// { dg-final { scan-tree-dump "Splitting function at:" "fnsplit" } }
+// { dg-final { scan-tree-dump-not {part\.[0-9]+[^\n]*\[must tail call\]} 
"fnsplit" } }
+
+// Splitting the error path must not introduce a void-returning musttail
+// call in a function returning int.
+
+template <int N>
+static int f (int *);
+[[noreturn]] void fail (int *, int *, int *);
+
+int (*fs[]) (int *) = { f<1>, f<2>, f<3> };
+
+template <int N>
+static int
+f (int *p)
+{
+  if (*p < 0)
+    fail (p, p, p);
+  [[gnu::musttail]] return fs[*p] (p + N);
+}
+
+int
+go (int *p)
+{
+  return f<1> (p);
+}
diff --git a/gcc/testsuite/g++.dg/opt/musttail-fnsplit2.C 
b/gcc/testsuite/g++.dg/opt/musttail-fnsplit2.C
new file mode 100644
index 0000000..ca1ba8f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/musttail-fnsplit2.C
@@ -0,0 +1,30 @@
+// { dg-do compile { target musttail } }
+// { dg-options "-O3" }
+
+// Also cover an error path whose noreturn property is inferred.
+
+template <int N>
+static int f (int *);
+__attribute__((noinline)) void
+fail (int *a, int *b, int *c)
+{
+  asm volatile ("" : : "r" (a), "r" (b), "r" (c));
+  __builtin_abort ();
+}
+
+int (*fs[]) (int *) = { f<1>, f<2>, f<3> };
+
+template <int N>
+static int
+f (int *p)
+{
+  if (*p < 0)
+    fail (p, p, p);
+  [[gnu::musttail]] return fs[*p] (p + N);
+}
+
+int
+go (int *p)
+{
+  return f<1> (p);
+}
diff --git a/gcc/testsuite/g++.dg/opt/musttail-fnsplit3.C 
b/gcc/testsuite/g++.dg/opt/musttail-fnsplit3.C
new file mode 100644
index 0000000..1df7954
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/musttail-fnsplit3.C
@@ -0,0 +1,28 @@
+// { dg-do compile { target { musttail && external_musttail } } }
+// { dg-options "-O3 -fdump-tree-fnsplit-details" }
+// { dg-final { scan-tree-dump "Refused: incompatible musttail return type" 
"fnsplit" } }
+// { dg-final { scan-tree-dump-not "Splitting function at:" "fnsplit" } }
+
+// A musttail call inside the outlined region requires propagation to the
+// helper call.  Reject a split that would make that call incompatible.
+
+template <int N>
+static int f (int *, int *, int *);
+[[noreturn]] int fail (int *, int *, int *);
+
+int (*fs[]) (int *, int *, int *) = { f<1>, f<2>, f<3> };
+
+template <int N>
+static int
+f (int *p, int *q, int *r)
+{
+  if (*p < 0)
+    [[gnu::musttail]] return fail (p + p[1], q + q[1], r + r[1]);
+  [[gnu::musttail]] return fs[*p] (p + N, q, r);
+}
+
+int
+go (int *p, int *q, int *r)
+{
+  return f<1> (p, q, r);
+}
diff --git a/gcc/testsuite/g++.dg/opt/musttail-fnsplit4.C 
b/gcc/testsuite/g++.dg/opt/musttail-fnsplit4.C
new file mode 100644
index 0000000..4a5d229
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/musttail-fnsplit4.C
@@ -0,0 +1,28 @@
+// { dg-do compile { target { musttail && external_musttail } } }
+// { dg-options "-O3 -fdump-tree-fnsplit" }
+// { dg-final { scan-tree-dump "Splitting function at:" "fnsplit" } }
+// { dg-final { scan-tree-dump {part\.[0-9]+[^\n]*\[must tail call\]} 
"fnsplit" } }
+
+// A void-returning split can preserve the musttail chain even when it
+// does not return.  Keep the helper call marked musttail.
+
+template <int N>
+static void f (int *, int *, int *);
+[[noreturn]] void fail (int *, int *, int *);
+
+void (*fs[]) (int *, int *, int *) = { f<1>, f<2>, f<3> };
+
+template <int N>
+static void
+f (int *p, int *q, int *r)
+{
+  if (*p < 0)
+    [[gnu::musttail]] return fail (p + p[1], q + q[1], r + r[1]);
+  [[gnu::musttail]] return fs[*p] (p + N, q, r);
+}
+
+void
+go (int *p, int *q, int *r)
+{
+  return f<1> (p, q, r);
+}
diff --git a/gcc/testsuite/g++.dg/opt/musttail-fnsplit5.C 
b/gcc/testsuite/g++.dg/opt/musttail-fnsplit5.C
new file mode 100644
index 0000000..b6d1980
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/musttail-fnsplit5.C
@@ -0,0 +1,27 @@
+// { dg-do compile { target { musttail && external_musttail } } }
+// { dg-options "-O3 -fdump-tree-fnsplit" }
+// { dg-final { scan-tree-dump "Splitting function at:" "fnsplit" } }
+// { dg-final { scan-tree-dump {part\.[0-9]+[^\n]*\[must tail call\]} 
"fnsplit" } }
+// { dg-final { scan-tree-dump-not {work [^\n]*\[must tail call\]} "fnsplit" } 
}
+
+// Keep propagation into compatible splits even if the outlined region
+// contains no musttail call itself.
+
+int target (int);
+int work (int, int, int);
+
+inline int
+f (int x)
+{
+  if (__builtin_expect (x >= 0, 1))
+    [[gnu::musttail]] return target (x);
+  return work (x * x + x, x * x - x, x | 42);
+}
+
+int (*address) (int) = f;
+
+int
+go (int x)
+{
+  return f (x);
+}

Reply via email to