From patchwork Sun Apr 14 19:57:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1923519 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=fhRX5iCJ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VHh1h2mcHz1yYM for ; Mon, 15 Apr 2024 05:58:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9895D3858C62 for ; Sun, 14 Apr 2024 19:58:07 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 6D14E3858D34 for ; Sun, 14 Apr 2024 19:57:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6D14E3858D34 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6D14E3858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::431 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713124665; cv=none; b=KefOviRkBFyt0HjyznGGxiiGXqfPjlXykftxFbS+wtoal+9hO500/pml4Q4Ge+weNGUEfbN8LX6FiuWYd6uffqz6SJTWLLKCSaFVcTC8UUujx1a9PCSQGs2z51mFHNl6v+5HNbUjZJriD17EnKTFDW0ptzWHtkPqWyX1xDXN2fw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713124665; c=relaxed/simple; bh=OrEMIGPqcmgNq1+MgDvhWy0IQL6rrfuS72OLEt9/PFY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Iyfqme2r/Rh7VHAS/9E+2GJCyK5wLUQGml3wT6rgCVjAYDQLKYP/G2WFZgRqESMTyprqhchqAw8ZZPM9qH7iRsCw39YAxjqd+IzN5/LjfXvbsAlA2UBIChyTEHpWsX54Mu0BmR56fzUZ5SxpEgAKXqAnI8onK8m2nqHFQOE6ZAE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6eff2be3b33so157394b3a.2 for ; Sun, 14 Apr 2024 12:57:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713124662; x=1713729462; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=uVoWq2RaCbSMpZjRDw2DxSX0n+BZmJUGV7UuS+esQfw=; b=fhRX5iCJF125SRTzK7yMf1wjYHfw/8Ch72iANvLE8DHwq4qliw+PPg0w2La9Hr348U T8hJLtPtz/7zaADWJQv2/gbIiZY+TqMfUZGJPbHyqpOH5Fa5rlznQCT8aP8Bmb6IhNw9 98JYmzRiXzpZuejHA3lxGGsOMBUgjyLTWId+ORbUoQhABXBb/V7fq2b61DBFckKG0pDq 3fzVBESzZ74Mrm1Xp0qS0cR8whfa/RzMN07eKn2SIrAJ8rN/TYPPK/2w67uqM8DlrDIg i4Xvjj+/Gl6RgRs9fVT+czvpoyZ8QyuwsejjdqLLh1UoLshpbJ1KO3gPwvHVWvEBy1VA o5gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713124662; x=1713729462; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uVoWq2RaCbSMpZjRDw2DxSX0n+BZmJUGV7UuS+esQfw=; b=Bx7y4IlgJzwD11S7pY5BbiniHq8zBy6Ei1pC5SrqzpUuy8QPPeFc+M2CaiULg90FaF ambKZ6mB8JjjK9QTTzGRPFTuy/VxKujBxaNTPQn7bveLHPzmRIdPZmGv5PNnVivMKQ4p Qbv7zAXRZSppDJTvfy1kAiyHH4eIIYQp2FWBjw0iy48jfvsUmvsTJ86qFCI3TBP1fO2Q DLuf49z38Op7D1WM4g0lP7EZPm0zD6E812sEhVNH9v45RilXm+sHPNN1WZ2C9cvIxqQN 0yw+TXf2EPdmDY8+65rNM+aLX+2Vgm5nV0nJxdIDSvyX5sQANDLbEIvHspPwNAk+yL2T WN6Q== X-Gm-Message-State: AOJu0YzxesLhBZhifbCzDWlKmWHHozaKSZIcpeXw3v3DovPdVSWp2Dy6 hCbXSPN7hNsPC6MIa3qrvn+qYoBrXOQzeQM4L3RzOOSKhddDewBiHw8tTw== X-Google-Smtp-Source: AGHT+IF52AaZtMvy4GucerhmH4uPWtGamv7X1P7sN8CYd0WyRBrzqOiIpBpaSheXBvwPIwlCM0GiaA== X-Received: by 2002:a05:6a00:3d01:b0:6ea:c156:f8dd with SMTP id lo1-20020a056a003d0100b006eac156f8ddmr7866977pfb.11.1713124661883; Sun, 14 Apr 2024 12:57:41 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.56.168.158]) by smtp.gmail.com with ESMTPSA id gx13-20020a056a001e0d00b006ecf25d0b8dsm5935472pfb.184.2024.04.14.12.57.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Apr 2024 12:57:41 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 2174274032A; Sun, 14 Apr 2024 12:57:40 -0700 (PDT) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Cc: hubicka@ucw.cz, rguenther@suse.de Subject: [Backport 1/2] tree-profile: Disable indirect call profiling for IFUNC resolvers Date: Sun, 14 Apr 2024 12:57:39 -0700 Message-ID: <20240414195740.237329-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3022.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org We can't profile indirect calls to IFUNC resolvers nor their callees as it requires TLS which hasn't been set up yet when the dynamic linker is resolving IFUNC symbols. Add an IFUNC resolver caller marker to cgraph_node and set it if the function is called by an IFUNC resolver. Disable indirect call profiling for IFUNC resolvers and their callees. Tested with profiledbootstrap on Fedora 39/x86-64. gcc/ChangeLog: PR tree-optimization/114115 * cgraph.h (symtab_node): Add check_ifunc_callee_symtab_nodes. (cgraph_node): Add called_by_ifunc_resolver. * cgraphunit.cc (symbol_table::compile): Call symtab_node::check_ifunc_callee_symtab_nodes. * symtab.cc (check_ifunc_resolver): New. (ifunc_ref_map): Likewise. (is_caller_ifunc_resolver): Likewise. (symtab_node::check_ifunc_callee_symtab_nodes): Likewise. * tree-profile.cc (gimple_gen_ic_func_profiler): Disable indirect call profiling for IFUNC resolvers and their callees. gcc/testsuite/ChangeLog: PR tree-optimization/114115 * gcc.dg/pr114115.c: New test. (cherry picked from commit cab32bacaea268ec062b1fb4fc662d90c9d1cfce) --- gcc/cgraph.h | 6 +++ gcc/cgraphunit.cc | 2 + gcc/symtab.cc | 89 +++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr114115.c | 24 +++++++++ gcc/tree-profile.cc | 8 ++- 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/pr114115.c diff --git a/gcc/cgraph.h b/gcc/cgraph.h index c1a3691b6f5..430c87d8bb7 100644 --- a/gcc/cgraph.h +++ b/gcc/cgraph.h @@ -479,6 +479,9 @@ public: Return NULL if there's no such node. */ static symtab_node *get_for_asmname (const_tree asmname); + /* Check symbol table for callees of IFUNC resolvers. */ + static void check_ifunc_callee_symtab_nodes (void); + /* Verify symbol table for internal consistency. */ static DEBUG_FUNCTION void verify_symtab_nodes (void); @@ -896,6 +899,7 @@ struct GTY((tag ("SYMTAB_FUNCTION"))) cgraph_node : public symtab_node redefined_extern_inline (false), tm_may_enter_irr (false), ipcp_clone (false), declare_variant_alt (false), calls_declare_variant_alt (false), gc_candidate (false), + called_by_ifunc_resolver (false), m_uid (uid), m_summary_id (-1) {} @@ -1491,6 +1495,8 @@ struct GTY((tag ("SYMTAB_FUNCTION"))) cgraph_node : public symtab_node is set for local SIMD clones when they are created and cleared if the vectorizer uses them. */ unsigned gc_candidate : 1; + /* Set if the function is called by an IFUNC resolver. */ + unsigned called_by_ifunc_resolver : 1; private: /* Unique id of the node. */ diff --git a/gcc/cgraphunit.cc b/gcc/cgraphunit.cc index bccd2f2abb5..40dcceccca5 100644 --- a/gcc/cgraphunit.cc +++ b/gcc/cgraphunit.cc @@ -2313,6 +2313,8 @@ symbol_table::compile (void) symtab_node::checking_verify_symtab_nodes (); + symtab_node::check_ifunc_callee_symtab_nodes (); + timevar_push (TV_CGRAPHOPT); if (pre_ipa_mem_report) dump_memory_report ("Memory consumption before IPA"); diff --git a/gcc/symtab.cc b/gcc/symtab.cc index 0470509a98d..df09def81e9 100644 --- a/gcc/symtab.cc +++ b/gcc/symtab.cc @@ -1369,6 +1369,95 @@ symtab_node::verify (void) timevar_pop (TV_CGRAPH_VERIFY); } +/* Return true and set *DATA to true if NODE is an ifunc resolver. */ + +static bool +check_ifunc_resolver (cgraph_node *node, void *data) +{ + if (node->ifunc_resolver) + { + bool *is_ifunc_resolver = (bool *) data; + *is_ifunc_resolver = true; + return true; + } + return false; +} + +static auto_bitmap ifunc_ref_map; + +/* Return true if any caller of NODE is an ifunc resolver. */ + +static bool +is_caller_ifunc_resolver (cgraph_node *node) +{ + bool is_ifunc_resolver = false; + + for (cgraph_edge *e = node->callers; e; e = e->next_caller) + { + /* Return true if caller is known to be an IFUNC resolver. */ + if (e->caller->called_by_ifunc_resolver) + return true; + + /* Check for recursive call. */ + if (e->caller == node) + continue; + + /* Skip if it has been visited. */ + unsigned int uid = e->caller->get_uid (); + if (bitmap_bit_p (ifunc_ref_map, uid)) + continue; + bitmap_set_bit (ifunc_ref_map, uid); + + if (is_caller_ifunc_resolver (e->caller)) + { + /* Return true if caller is an IFUNC resolver. */ + e->caller->called_by_ifunc_resolver = true; + return true; + } + + /* Check if caller's alias is an IFUNC resolver. */ + e->caller->call_for_symbol_and_aliases (check_ifunc_resolver, + &is_ifunc_resolver, + true); + if (is_ifunc_resolver) + { + /* Return true if caller's alias is an IFUNC resolver. */ + e->caller->called_by_ifunc_resolver = true; + return true; + } + } + + return false; +} + +/* Check symbol table for callees of IFUNC resolvers. */ + +void +symtab_node::check_ifunc_callee_symtab_nodes (void) +{ + symtab_node *node; + + FOR_EACH_SYMBOL (node) + { + cgraph_node *cnode = dyn_cast (node); + if (!cnode) + continue; + + unsigned int uid = cnode->get_uid (); + if (bitmap_bit_p (ifunc_ref_map, uid)) + continue; + bitmap_set_bit (ifunc_ref_map, uid); + + bool is_ifunc_resolver = false; + cnode->call_for_symbol_and_aliases (check_ifunc_resolver, + &is_ifunc_resolver, true); + if (is_ifunc_resolver || is_caller_ifunc_resolver (cnode)) + cnode->called_by_ifunc_resolver = true; + } + + bitmap_clear (ifunc_ref_map); +} + /* Verify symbol table for internal consistency. */ DEBUG_FUNCTION void diff --git a/gcc/testsuite/gcc.dg/pr114115.c b/gcc/testsuite/gcc.dg/pr114115.c new file mode 100644 index 00000000000..2629f591877 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr114115.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-O0 -fprofile-generate -fdump-tree-optimized" } */ +/* { dg-require-profiling "-fprofile-generate" } */ +/* { dg-require-ifunc "" } */ + +void *foo_ifunc2() __attribute__((ifunc("foo_resolver"))); + +void bar(void) +{ +} + +static int f3() +{ + bar (); + return 5; +} + +void (*foo_resolver(void))(void) +{ + f3(); + return bar; +} + +/* { dg-final { scan-tree-dump-not "__gcov_indirect_call_profiler_v" "optimized" } } */ diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc index da300d5f9e8..b5de0fb914f 100644 --- a/gcc/tree-profile.cc +++ b/gcc/tree-profile.cc @@ -418,7 +418,13 @@ gimple_gen_ic_func_profiler (void) gcall *stmt1; tree tree_uid, cur_func, void0; - if (c_node->only_called_directly_p ()) + /* Disable indirect call profiling for an IFUNC resolver and its + callees since it requires TLS which hasn't been set up yet when + the dynamic linker is resolving IFUNC symbols. See + https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114115 + */ + if (c_node->only_called_directly_p () + || c_node->called_by_ifunc_resolver) return; gimple_init_gcov_profiler ();