From patchwork Wed Feb 9 07:15:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Rozenfeld X-Patchwork-Id: 50943 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 3394A385841B for ; Wed, 9 Feb 2022 07:16:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3394A385841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644390977; bh=ywrRVSTlctJq7S19GxInoMwrgtOo/IfRJmsIptXp3lA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=gcu/WttaCXOpnX4Yyn4DQo5oiPZhhQ9QgMnCAAiM61ti8FBBylJUyDSouoNHtgpTY QCBfYJKNbuccdlNGmNBZAoZfxavX3QC/RaBP+r+brEu7N21kFi7Hu+2DDTGAVlpFOO h09xAK9AXRmaDjoR1/IXFfztv15kFzGVBumfFAQo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from na01-obe.outbound.protection.outlook.com (mail-eus2azon11020019.outbound.protection.outlook.com [52.101.56.19]) by sourceware.org (Postfix) with ESMTPS id 3D4C83858D20 for ; Wed, 9 Feb 2022 07:15:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3D4C83858D20 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RrqWc+in/BWf9B8qDEzD9Q2L9/WESg+nJp1WVol8hcfFTFKBoGl276k7+IDvcvlhXm34vLVAPvMCGfWx1HnB8gl/m+L8tC3hmCnDuMWNLf0oI0mpjPXYZlkSfmk6Qkj/ylLFzltTCdd0Qkn1WFJJjMxGN5lOwXXkIg9VgxQxYMk4gWgh0JPGTGRljoWsGFXMvtZfoYtfsMgsmfFR0CPq406bDGZXFBZ92HysyPYEjf8s3ZFu8I+kScM0OJdQh7NQEM7nyKz/j7N3Ri7tmfmfPS4iYNVIOrEhMzTo3PRaD2eDmRNBzf16HZ2ZP2ODA/u1xG0fH2W3WRVAEA5Cnw93EQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ywrRVSTlctJq7S19GxInoMwrgtOo/IfRJmsIptXp3lA=; b=ZGO34WpBJLp2GuMI5zWR+D/DqXPxnsT2QQqr/eUtbT6xIR7swufMqYAmkHmJRCe+tnPwV+71Bx+4hd0df6oMSF/hkQ4HpixxOI5JO5/i59qESwMMWQRaMXfCs+27F0SVJBZ+SRYRmWKLZVaNGatkz56Saq1MKelv9lUKH+B6RXv8PJfH8wWwUHG9VubH+qq3aLQmjW06xpOvLiimHGQ/z3EVzzUhnunBfxUHUf6PfQZGXMMQafCjlE4qDz+VpT276CcMTVO3rrEor7vxkjkbhAxJtR+0te88DQGAg0RiC6iLxDSACKZbLoEwVRkey+h/hII2IT0v8L5pAnBsqnvXLA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=microsoft.com; dmarc=pass action=none header.from=microsoft.com; dkim=pass header.d=microsoft.com; arc=none Received: from MW2PR2101MB1100.namprd21.prod.outlook.com (2603:10b6:302:a::29) by BN7PR21MB1604.namprd21.prod.outlook.com (2603:10b6:406:b3::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4995.6; Wed, 9 Feb 2022 07:15:45 +0000 Received: from MW2PR2101MB1100.namprd21.prod.outlook.com ([fe80::655f:1341:775d:738a]) by MW2PR2101MB1100.namprd21.prod.outlook.com ([fe80::655f:1341:775d:738a%4]) with mapi id 15.20.4995.004; Wed, 9 Feb 2022 07:15:44 +0000 To: "gcc-patches@gcc.gnu.org" Subject: [PATCH] AutoFDO: Don't try to promote indirect calls that result in recursive direct calls Thread-Topic: [PATCH] AutoFDO: Don't try to promote indirect calls that result in recursive direct calls Thread-Index: Adgdg+Tyen5AfnrbR4KVG45wcdyYsQ== Date: Wed, 9 Feb 2022 07:15:44 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_ActionId=596c5515-334c-4760-8e34-28f2d693c632; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_ContentBits=0; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Enabled=true; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Method=Standard; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Name=Internal; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_SetDate=2022-02-09T07:06:55Z; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_SiteId=72f988bf-86f1-41af-91ab-2d7cd011db47; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ddb4b177-3396-4504-5b8f-08d9eb9bfd11 x-ms-traffictypediagnostic: BN7PR21MB1604:EE_ x-ms-exchange-atpmessageproperties: SA|SL x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:7219; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: igTewaILDii7v+Ur3GpSbnZNOftEahP0J0uUhCPL9/ANJqDq1zLuKukEXKdGSZNeVdzOBs9ab0q/5jzhzv3XC1OazyVKZcPmZPs8UrhBAM8Eqkdq5NmIKba678Z5jsC9pHRmFsqDBxm8OR8rpjFVcR7qjUfkd5S6qRq2huEPG3ukuTosNeCcie6ttDo0Dhqp7nJIp9SIQTR4fQS4BYmrAt5tCfDU+Qbapa78AJjXLmYL209Cno9fK9tFy+V+hZusgyYHrvaD+au+af817c7MKMRMiKGQb6/ICw+Pkgp6hFkOmPHcM3d52p9u9CrhWWuJUhrGfcdNBHQlCUO1PJ2UFkWT+oSNodbQsJvPUQb8FtSFDqpwz+DStVVGcnZmvmcIQbyiZ0rQxwbRk4JdnzUAEGnZSgt8JoYQztSIboDvRdITD2YDFtwnb5H3O8g6OE61Ya4VwHhgbDBLwz81fXkiAp3dHvQrOL0lQMOnQkOhbLk3kXVbkwo/56ULxDpmYrp9Y+Ik7/72SB/7GF87B6zolXhimCPdA3Kh0IZbcwnol5ygoUntECAcnBq8e80SMDI29qrBYoMhpGckpHjYYebAsGYlDgsiRLeduRBGKsRfZSjQmzCgy63gXKgc3xPqNaHoJAuZ3RxSBnn3X6MZa4D92wBmanDveyrTQ00j6X0spNG3bW2sKzBM7+Q/JKbo9umjyVHmh6ykqeBleyNQZyUncxTQ3nUuapGOk136pjP5ODVjgR69XSQBPrly60LecsoK x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MW2PR2101MB1100.namprd21.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(38100700002)(66946007)(66476007)(66446008)(8676002)(122000001)(316002)(8990500004)(76116006)(8936002)(52536014)(38070700005)(64756008)(5660300002)(82960400001)(86362001)(82950400001)(66556008)(6916009)(10290500003)(55016003)(508600001)(83380400001)(186003)(6506007)(7696005)(2906002)(71200400001)(9686003)(33656002); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: a/wBpqVybhRJRE+B6byiVFDfkVNo9NYcxzVcrpX+YM8Sv0iLnnpY27p36tbL1/RhCJ7dqQz7KmiGbpkiry7PbAvQa7L8oYIQ7f9YjjcoMFJ4r9BkdKx9Z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W6w5z+egVv0LRcSIdRQVl9KdBuWSzaLCEW9bSpvDi77TqEeWPZWzTfhKlzlp+PryNOOfoNwOu4WaOH9QEqEoBNWc4etPf9KPI3nBDt5Cc9b6jfOeGwWF0PJ2Bd0AoKTaM7kmEow6IaF0FlOLIO7NTQhjxFKVYq3apfZmz+rSHwewxPLBXi5UkS3LzSqf5iOr1KCR0FhKrWcW0hRNB6W5wlOoq9BcHhcsk8xXvlh8fSL2DNvJpc0KEW6IztBVOAPRjgSYA2LuQChFdrjFazxoNQKowIJG+6WhSF436gTsFZkwvA4zOfArwExgrGp84ma0iTIHCHgnBDpioVUJff658Bm/WTWM+AZaUVKJ/W/flYFGOOE2/k+e084ARstc+OjCyvveL4UZvrKEj47WGoXJV4FACBGGC3DrUBUxfYcBGSaBXUwm9QiUvu+mcGNsOIFneUSa08H5kKqC1fN6pQFVrCTFlRkrDc2WT9mW+1Ikw0ESQ== MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MW2PR2101MB1100.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ddb4b177-3396-4504-5b8f-08d9eb9bfd11 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Feb 2022 07:15:44.6967 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: NKL0dkBXMnuamI95ZUCWxfLizkTOEiJ5+t5GxZBvoDA2myXtFJJMidybwas9pjnLRxz8gkh+22mNYGKJNdfqyA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7PR21MB1604 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FORGED_SPF_HELO, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: Eugene Rozenfeld via Gcc-patches From: Eugene Rozenfeld Reply-To: Eugene Rozenfeld Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" AutoFDO tries to promote and inline all indirect calls that were promoted and inlined in the original binary and that are still hot. In the included test case, the promotion results in a direct call that is a recursive call. inline_call and optimize_inline_calls can't handle recursive calls at this stage. Currently, inline_call fails with a segmentation fault. This change leaves the indirect call alone if promotion will result in a recursive call. Tested on x86_64-pc-linux-gnu. gcc/ChangeLog: * auto-profile.cc (afdo_indirect_call): Don't attempt to promote indirect calls that will result in direct recursive calls. gcc/testsuite/g++.dg/tree-prof/ChangeLog: * indir-call-recursive-inlining.C : New test. --- gcc/auto-profile.cc | 40 ++++++++------ .../tree-prof/indir-call-recursive-inlining.C | 54 +++++++++++++++++++ 2 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/g++.dg/tree-prof/indir-call-recursive-inlining.C -- 2.25.1 diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc index c7cee639c85..2b34b80b82d 100644 --- a/gcc/auto-profile.cc +++ b/gcc/auto-profile.cc @@ -975,7 +975,7 @@ read_profile (void) * after annotation, we just need to mark, and let follow-up logic to decide if it needs to promote and inline. */ -static void +static bool afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map, bool transform) { @@ -983,12 +983,12 @@ afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map, tree callee; if (map.size () == 0) - return; + return false; gcall *stmt = dyn_cast (gs); if (!stmt || gimple_call_internal_p (stmt) || gimple_call_fndecl (stmt) != NULL_TREE) - return; + return false; gcov_type total = 0; icall_target_map::const_iterator max_iter = map.end (); @@ -1003,7 +1003,7 @@ afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map, struct cgraph_node *direct_call = cgraph_node::get_for_asmname ( get_identifier (afdo_string_table->get_name (max_iter->first))); if (direct_call == NULL || !direct_call->profile_id) - return; + return false; callee = gimple_call_fn (stmt); @@ -1013,20 +1013,27 @@ afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map, hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters); gimple_add_histogram_value (cfun, stmt, hist); - // Total counter + /* Total counter */ hist->hvalue.counters[0] = total; - // Number of value/counter pairs + /* Number of value/counter pairs */ hist->hvalue.counters[1] = 1; - // Value + /* Value */ hist->hvalue.counters[2] = direct_call->profile_id; - // Counter + /* Counter */ hist->hvalue.counters[3] = max_iter->second; if (!transform) - return; + return false; + + cgraph_node* current_function_node = cgraph_node::get (current_function_decl); + + /* If the direct call is a recursive call, don't promote it since + we are not set up to inline recursive calls at this stage. */ + if (direct_call == current_function_node) + return false; struct cgraph_edge *indirect_edge - = cgraph_node::get (current_function_decl)->get_edge (stmt); + = current_function_node->get_edge (stmt); if (dump_file) { @@ -1040,13 +1047,13 @@ afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map, { if (dump_file) fprintf (dump_file, " not transforming\n"); - return; + return false; } if (DECL_STRUCT_FUNCTION (direct_call->decl) == NULL) { if (dump_file) fprintf (dump_file, " no declaration\n"); - return; + return false; } if (dump_file) @@ -1063,16 +1070,17 @@ afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map, cgraph_edge::redirect_call_stmt_to_callee (new_edge); gimple_remove_histogram_value (cfun, stmt, hist); inline_call (new_edge, true, NULL, NULL, false); + return true; } /* From AutoFDO profiles, find values inside STMT for that we want to measure histograms and adds them to list VALUES. */ -static void +static bool afdo_vpt (gimple_stmt_iterator *gsi, const icall_target_map &map, bool transform) { - afdo_indirect_call (gsi, map, transform); + return afdo_indirect_call (gsi, map, transform); } typedef std::set bb_set; @@ -1498,8 +1506,8 @@ afdo_vpt_for_early_inline (stmt_set *promoted_stmts) { /* Promote the indirect call and update the promoted_stmts. */ promoted_stmts->insert (stmt); - afdo_vpt (&gsi, info.targets, true); - has_vpt = true; + if (afdo_vpt (&gsi, info.targets, true)) + has_vpt = true; } } } diff --git a/gcc/testsuite/g++.dg/tree-prof/indir-call-recursive-inlining.C b/gcc/testsuite/g++.dg/tree-prof/indir-call-recursive-inlining.C new file mode 100644 index 00000000000..11f690063ef --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-prof/indir-call-recursive-inlining.C @@ -0,0 +1,54 @@ +/* { dg-options "-O2 " } */ + +class Parent +{ +public: + Parent *object; + + Parent() + { + object = this; + } + + virtual void recurse (int t) = 0; +}; + +class Child : public Parent +{ + + Parent * + get_object () + { + return this; + } + +public: + virtual void + recurse (int t) + { + if (t != 10) + for (int i = 0; i < 5; ++i) + get_object()->recurse(t + 1); + }; +}; + +Parent * +create_object () +{ + Child *mod = new Child; + return mod; +} + +int +main (int argc, char **argv) +{ + Parent *parent = create_object (); + + for (int i = 0; i < 5; ++i) + { + parent->recurse (0); + } + + return 0; +} +