From patchwork Thu Jan 6 08:48:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 49615 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB7133857406 for ; Thu, 6 Jan 2022 08:48:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB7133857406 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641458926; bh=8+CQMwpg4ohd7USLplTVYG6lIZdTjXcUllQijElVkzk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=pKQaRvyANIL0D6jhCJefAvxFmRImG2yU9+PBsm6GoEEgLVaWrsy3nmxCQVnQmsUiT Oo5/xLP6REqYG48cDPABCvtH3Xle6oFhr3/eGDqXZmnAChh5gsZE4dj82i9SVY+wlf kX53Ul0nc2xR4B5GUWV06UgUN/6NL3FkPCjHuL9E= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 738563858D39 for ; Thu, 6 Jan 2022 08:48:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 738563858D39 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-668-HOd9yblaMiCcAL4s-vvW5Q-1; Thu, 06 Jan 2022 03:48:13 -0500 X-MC-Unique: HOd9yblaMiCcAL4s-vvW5Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2AE6318B613D; Thu, 6 Jan 2022 08:48:12 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.2.16.169]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B1E1C7314C; Thu, 6 Jan 2022 08:48:11 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 2068m9p5128064 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 6 Jan 2022 09:48:09 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 2068m88E128063; Thu, 6 Jan 2022 09:48:08 +0100 Date: Thu, 6 Jan 2022 09:48:08 +0100 To: Jason Merrill , Jan Hubicka Subject: [PATCH] c++: Ensure some more that immediate functions aren't gimplified [PR103912] Message-ID: <20220106084808.GW2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! 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. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-01-06 Jakub Jelinek 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. Jakub --- gcc/cp/semantics.c.jj 2022-01-03 10:40:48.000000000 +0100 +++ gcc/cp/semantics.c 2022-01-05 12:52:11.484379138 +0100 @@ -4785,6 +4785,18 @@ expand_or_defer_fn (tree fn) emit_associated_thunks (fn); function_depth--; + + if (DECL_IMMEDIATE_FUNCTION_P (fn)) + { + cgraph_node *node = cgraph_node::get (fn); + if (node) + { + node->body_removed = true; + node->analyzed = false; + node->definition = false; + node->force_output = false; + } + } } } --- gcc/cp/decl2.c.jj 2022-01-03 10:40:48.083068010 +0100 +++ gcc/cp/decl2.c 2022-01-05 12:53:34.930204119 +0100 @@ -5272,6 +5272,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 --- gcc/testsuite/g++.dg/cpp2a/consteval26.C.jj 2022-01-05 12:42:07.918878074 +0100 +++ gcc/testsuite/g++.dg/cpp2a/consteval26.C 2022-01-05 12:40:18.853416637 +0100 @@ -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 (); +}