From patchwork Thu Feb 2 16:19:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 52074 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp333285wrn; Thu, 2 Feb 2023 08:20:29 -0800 (PST) X-Google-Smtp-Source: AK7set9wXefa85O/OCZxh/hcPufwnKaPDnn1G9Ajd3T0UNNzNYUoaXRDyeDJQlLV4NZjeTcIWK/b X-Received: by 2002:a17:907:7094:b0:88a:72bf:9670 with SMTP id yj20-20020a170907709400b0088a72bf9670mr6893700ejb.50.1675354829457; Thu, 02 Feb 2023 08:20:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675354829; cv=none; d=google.com; s=arc-20160816; b=cusOp2B8y/AgdqySlyQ2AWOXGsFX1QGojXERCEb0zRR+CK3LP8Lb6HS0hAj7KQXBXS mP/4kWA9qbkJ3t7wiyCZIQjz4CLl5ZMmotBY8uH6nZL3w1OutwstLEBwPY4R1xQHuMWj UqZxd9Wa1XYXdZM2A03gCUWYk4M0abzEFW/j5BQCBVOQqUrS+T6zt4Hs2r3ie15DkvMY 4YuapvCrNRMSzMau2ea4u/KtqGRRO7pMysvoW6WoswxXMsbd/QxN9C81TkR6ruA07D8F neqmSgVEjxlhJgLUOeeXypX7ga4qoLn/dFg33OakaT7WbxB9wW6lQlSi4A0YjTIEKMIH K+zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:message-id:date :user-agent:subject:cc:to:from:dkim-signature:dkim-signature :dmarc-filter:delivered-to; bh=wLJD89gRQhNRcYSrKYEBNpcuFYUexuqOstibYmGSowQ=; b=Fk59iYKoawvSp/G95VOexAg3wtaHC3cxWsmCc8yWSPppO1AxDoJ/IyglrgzGh7j5Eh H/gp2nIe0sYunqCLusElqJAO4/vMYrQNYZyGXmpfOZ5eW8oqgJ6udIggmson6RJ6SdWX tZiPcDuRnZn1dwW8q8GPoSWhinv+VIUaRELyr2DhkntZwHbtKJ5jgfOotT1iivl2K6ig hPsq1LVDkBaJyyjQGf2xoPOnnBxHH3/44Xe/N3cHEfgXYiHtN9SKbjcigRxMQnM6MFbm x6YE2bhr62pSllIDKoQnPO2BWfkzYjbqgG52rAcya8LRts8QNMkiptJulPZL8/IkgNb5 PiNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=zOF+dTIH; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s5-20020a170906220500b00878547e8b66si2997393ejs.904.2023.02.02.08.20.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 08:20:29 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=zOF+dTIH; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8EB70385B507 for ; Thu, 2 Feb 2023 16:20:19 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 8614A385842A for ; Thu, 2 Feb 2023 16:19:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8614A385842A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4E95A33A4B; Thu, 2 Feb 2023 16:19:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1675354792; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=wLJD89gRQhNRcYSrKYEBNpcuFYUexuqOstibYmGSowQ=; b=zOF+dTIHMlfbUqkAEAEbPmLs0x9nQTW9kr55BvU+0SchvcHN/UEYVyD5ML+akvs74L+nOH z7nn8dYzYKjzEB3hG0W4A+4p+KVQ3cgv4UEHQ0oQtMb/s1Jb6wwetPS+8d1SyHKK3DwMXg q9nbAnF4cmPfmMO2sT5xn84HoYDNArg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1675354792; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=wLJD89gRQhNRcYSrKYEBNpcuFYUexuqOstibYmGSowQ=; b=bCiuswRgol3e6XwEfKbvx/xmvWnL1p+UA48XLiA3taLKJXqaWEWn3T/Z24QpogasquHUaq 8eHJT6TSyFE09CDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 42BB5138E8; Thu, 2 Feb 2023 16:19:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id kbBCEKji22O4BAAAMHmgww (envelope-from ); Thu, 02 Feb 2023 16:19:52 +0000 From: Martin Jambor To: GCC Patches Cc: Jan Hubicka Subject: [PATCH] ipa: Avoid invalid gimple when IPA-CP and IPA-SRA disagree on types (108384) User-Agent: Notmuch/0.37 (https://notmuchmail.org) Emacs/28.2 (x86_64-suse-linux-gnu) Date: Thu, 02 Feb 2023 17:19:51 +0100 Message-ID: MIME-Version: 1.0 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756736865656949877?= X-GMAIL-MSGID: =?utf-8?q?1756736865656949877?= Hi, when the compiled program contains type mismatches between callers and callees when it comes to a parameter, IPA-CP can try to propagate one constant from callers while IPA-SRA may try to split a parameter expecting a value of a different size on the same offset. This then currently leads to creation of a VIEW_CONVERT_EXPR with mismatching type sizes of LHS and RHS which is correctly flagged by the GIMPLE verifier as invalid. It seems that the best course of action is to try and avoid the situation altogether and so this patch adds a check to IPA-SRA that peeks into the result of IPA-CP and when it sees a value on the same offset but with a mismatching size, it just decides to leave that particular parameter be. Bootstrapped and tested on x86_64-linux, OK for master? Thanks, Martin gcc/ChangeLog: 2023-02-02 Martin Jambor PR ipa/108384 * ipa-sra.cc (push_param_adjustments_for_index): Remove a size check when comparing to an IPA-CP value. (dump_list_of_param_indices): New function. (adjust_parameter_descriptions): Check for mismatching IPA-CP values. Dump removed candidates using dump_list_of_param_indices. * ipa-param-manipulation.cc (ipa_param_body_adjustments::modify_expression): Add assert checking sizes of a VIEW_CONVERT_EXPR will match. (ipa_param_body_adjustments::modify_assignment): Likewise. gcc/testsuite/ChangeLog: 2023-02-02 Martin Jambor PR ipa/108384 * gcc.dg/ipa/pr108384.c: New test. --- gcc/ipa-param-manipulation.cc | 4 ++ gcc/ipa-sra.cc | 66 ++++++++++++++++++++--------- gcc/testsuite/gcc.dg/ipa/pr108384.c | 25 +++++++++++ 3 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/ipa/pr108384.c diff --git a/gcc/ipa-param-manipulation.cc b/gcc/ipa-param-manipulation.cc index 1de9ca2ceb8..42488ee09c3 100644 --- a/gcc/ipa-param-manipulation.cc +++ b/gcc/ipa-param-manipulation.cc @@ -1857,6 +1857,8 @@ ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert) if (convert && !useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (repl))) { + gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (expr))) + == tree_to_shwi (TYPE_SIZE (TREE_TYPE (repl)))); tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl); *expr_p = vce; } @@ -1900,6 +1902,8 @@ ipa_param_body_adjustments::modify_assignment (gimple *stmt, } else { + gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (*lhs_p))) + == tree_to_shwi (TYPE_SIZE (TREE_TYPE (*rhs_p)))); tree new_rhs = fold_build1_loc (gimple_location (stmt), VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p), *rhs_p); diff --git a/gcc/ipa-sra.cc b/gcc/ipa-sra.cc index 81b75910db1..7a2b4dc8608 100644 --- a/gcc/ipa-sra.cc +++ b/gcc/ipa-sra.cc @@ -3989,9 +3989,7 @@ push_param_adjustments_for_index (isra_func_summary *ifs, unsigned base_index, { ipa_argagg_value_list avl (ipcp_ts); tree value = avl.get_value (base_index, pa->unit_offset); - if (value - && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value))) / BITS_PER_UNIT - == pa->unit_size)) + if (value) { if (dump_file) fprintf (dump_file, " - omitting component at byte " @@ -4130,6 +4128,22 @@ process_isra_node_results (cgraph_node *node, callers.release (); } +/* If INDICES is not empty, dump a combination of NODE's dump_name and MSG + followed by the list of numbers in INDICES. */ + +static void +dump_list_of_param_indices (const cgraph_node *node, const char* msg, + const vec &indices) +{ + if (indices.is_empty ()) + return; + fprintf (dump_file, "The following parameters of %s %s:", node->dump_name (), + msg); + for (unsigned i : indices) + fprintf (dump_file, " %u", i); + fprintf (dump_file, "\n"); +} + /* Check which parameters of NODE described by IFS have survived until IPA-SRA and disable transformations for those which have not or which should not transformed because the associated debug counter reached its limit. Return @@ -4153,6 +4167,7 @@ adjust_parameter_descriptions (cgraph_node *node, isra_func_summary *ifs) check_surviving = true; cinfo->param_adjustments->get_surviving_params (&surviving_params); } + ipcp_transformation *ipcp_ts = ipcp_get_transformation_summary (node); auto_vec dump_dead_indices; auto_vec dump_bad_cond_indices; for (unsigned i = 0; i < len; i++) @@ -4202,27 +4217,40 @@ adjust_parameter_descriptions (cgraph_node *node, isra_func_summary *ifs) if (size_would_violate_limit_p (desc, desc->size_reached)) desc->split_candidate = false; } + + /* Avoid ICEs on size-mismatched VIEW_CONVERT_EXPRs when callers and + callees don't agree on types in aggregates and we try to do both + IPA-CP and IPA-SRA. */ + if (ipcp_ts && desc->split_candidate) + { + ipa_argagg_value_list avl (ipcp_ts); + for (const param_access *pa : desc->accesses) + { + if (!pa->certain) + continue; + tree value = avl.get_value (i, pa->unit_offset); + if (value + && ((tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value))) + / BITS_PER_UNIT) + != pa->unit_size)) + { + desc->split_candidate = false; + if (dump_file && (dump_flags & TDF_DETAILS)) + dump_dead_indices.safe_push (i); + break; + } + } + } + if (desc->locally_unused || desc->split_candidate) ret = false; } } - if (!dump_dead_indices.is_empty ()) - { - fprintf (dump_file, "The following parameters of %s are dead on arrival:", - node->dump_name ()); - for (unsigned i : dump_dead_indices) - fprintf (dump_file, " %u", i); - fprintf (dump_file, "\n"); - } - if (!dump_bad_cond_indices.is_empty ()) - { - fprintf (dump_file, "The following parameters of %s are not safe to " - "derefernce in all callers:", node->dump_name ()); - for (unsigned i : dump_bad_cond_indices) - fprintf (dump_file, " %u", i); - fprintf (dump_file, "\n"); - } + dump_list_of_param_indices (node, "are dead on arrival or have a type " + "mismatch with IPA-CP", dump_dead_indices); + dump_list_of_param_indices (node, "are not safe to derefernce in all callers", + dump_bad_cond_indices); return ret; } diff --git a/gcc/testsuite/gcc.dg/ipa/pr108384.c b/gcc/testsuite/gcc.dg/ipa/pr108384.c new file mode 100644 index 00000000000..2b714aa78b1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/pr108384.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-options "-O3" } */ + +struct S0 { + int f0; + short f1; + unsigned f2 : 7; + short f3; +} func_2_l_27; +int *g_389; +int safe_sub_func_int16_t_s_s(void); +void safe_lshift_func_uint8_t_u_s(int); +void func_23(struct S0 p_24, struct S0 p_25) { + int *l_1051 = g_389; + if (safe_sub_func_int16_t_s_s()) + for (;;) + safe_lshift_func_uint8_t_u_s(p_24.f1); + *l_1051 = p_25.f0; +} +void func_2(void) { + struct S0 l_26[2]; + l_26[1].f0 = 4; + ((long long*)&l_26)[2] = 25770065925; + func_23(l_26[1], func_2_l_27); +}