public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9499] c++: Ensure some more that immediate functions aren't gimplified [PR103912]
@ 2022-01-24  9:21 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-01-24  9:21 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:18abe529d092ca00903fe6a9ec5210c91d45030f

commit r11-9499-g18abe529d092ca00903fe6a9ec5210c91d45030f
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jan 10 20:49:11 2022 +0100

    c++: Ensure some more that immediate functions aren't gimplified [PR103912]
    
    Immediate functions should never be emitted into assembly, the FE doesn't
    genericize them and does various things to ensure they aren't gimplified.
    But the following testcase ICEs anyway due to that, because the consteval
    function returns a lambda, and operator() of the lambda has
    decl_function_context of the consteval function.  cgraphunit.c then
    does:
                  /* Preserve a functions function context node.  It will
                     later be needed to output debug info.  */
                  if (tree fn = decl_function_context (decl))
                    {
                      cgraph_node *origin_node = cgraph_node::get_create (fn);
                      enqueue_node (origin_node);
                    }
    which enqueues the immediate function and then tries to gimplify it,
    which results in ICE because it hasn't been genericized.
    
    When I try similar testcase with constexpr instead of consteval and
    static constinit auto instead of auto in main, what happens is that
    the functions are gimplified, later ipa.c discovers they aren't reachable
    and sets body_removed to true for them (and clears other flags) and we end
    up with a debug info which has the foo and bar functions without
    DW_AT_low_pc and other code specific attributes, just stuff from its BLOCK
    structure and in there the lambda with DW_AT_low_pc etc.
    
    The following patch attempts to emulate that behavior early, so that cgraph
    doesn't try to gimplify those and pretends they were already gimplified
    and found unused and optimized away.
    
    2022-01-10  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/103912
            * semantics.c (expand_or_defer_fn): For immediate functions, set
            node->body_removed to true and clear analyzed, definition and
            force_output.
            * decl2.c (c_parse_final_cleanups): Ignore immediate functions for
            expand_or_defer_fn.
    
            * g++.dg/cpp2a/consteval26.C: New test.
    
    (cherry picked from commit 54fa7daefe35cacf4a933947d1802318da193c01)

Diff:
---
 gcc/cp/decl2.c                           |  1 +
 gcc/cp/semantics.c                       | 11 +++++++++
 gcc/testsuite/g++.dg/cpp2a/consteval26.C | 39 ++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)

diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index a82960fb39c..aaeda8220f7 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -5137,6 +5137,7 @@ c_parse_final_cleanups (void)
 	  if (!DECL_EXTERNAL (decl)
 	      && decl_needed_p (decl)
 	      && !TREE_ASM_WRITTEN (decl)
+	      && !DECL_IMMEDIATE_FUNCTION_P (decl)
 	      && !node->definition)
 	    {
 	      /* We will output the function; no longer consider it in this
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 9dc10e7d759..7069e196d16 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -4681,6 +4681,17 @@ expand_or_defer_fn (tree fn)
       emit_associated_thunks (fn);
 
       function_depth--;
+
+      if (DECL_IMMEDIATE_FUNCTION_P (fn))
+	{
+	  if (cgraph_node *node = cgraph_node::get (fn))
+	    {
+	      node->body_removed = true;
+	      node->analyzed = false;
+	      node->definition = false;
+	      node->force_output = false;
+	    }
+	}
     }
 }
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval26.C b/gcc/testsuite/g++.dg/cpp2a/consteval26.C
new file mode 100644
index 00000000000..01acb80ff9e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/consteval26.C
@@ -0,0 +1,39 @@
+// PR c++/103912
+// { dg-do run { target c++20 } }
+// { dg-additional-options "-O2 -g -fkeep-inline-functions" }
+
+extern "C" void abort ();
+
+struct A { A () {} };
+
+consteval auto
+foo ()
+{
+  if (1)
+    ;
+  return [] (A x) { return 1; };
+}
+
+consteval auto
+bar (int a)
+{
+  int b = a + 4;
+  if (1)
+    ;
+  return [=] (A x) { return a + b; };
+}
+
+int
+main ()
+{
+  A x;
+  auto h = foo ();
+  if (h (x) != 1)
+    abort ();
+  auto i = bar (5);
+  if (i (x) != 14)
+    abort ();
+  auto j = bar (42);
+  if (j (x) != 88)
+    abort ();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-24  9:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-24  9:21 [gcc r11-9499] c++: Ensure some more that immediate functions aren't gimplified [PR103912] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).