From patchwork Wed Jan 31 02:21:36 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: 1893191 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=N8FXKCqa; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4TPm5F3fVJz23g7 for ; Wed, 31 Jan 2024 13:22:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7ECF53858420 for ; Wed, 31 Jan 2024 02:22:07 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by sourceware.org (Postfix) with ESMTPS id ECA4C3858413 for ; Wed, 31 Jan 2024 02:21:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ECA4C3858413 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 ECA4C3858413 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::534 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706667702; cv=none; b=b7Cam3lKhRR/n7hHRo4PZgdDWvF2Nn5+DgTbLP9vcF92qGDzV1+VBvfv7gegl1FGoKBPxuJG/I8m8KjzfecHwxIKeT4xJzKceoeofEqP+NRfbkzrIOqdbHCEQ5feoiC3BrrOC/ClDCub1og+ys2Fh6qWh1ah7AWl/kRlVvz2vz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706667702; c=relaxed/simple; bh=MkvS+LwZ4nZazT/UwWAtwFKzBGgdiuyyR1Y0vphq8DE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lylsIE/9ma4K2HWTQjAV1JcpGTyACx9UIeaetcngOa9Coqv+S6rG3n5Ifu1chTpPL42gcPqit7B57vT47+9beQPXo0icttrL7NnNnlWxu1Xn+Mt9n9gFTNlwyR3xaTNEF8dDQAxmCYeD0C3TTi8OSf3fYzGf3gpSxTgtkp5qVnM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-5d4d15ec7c5so3751113a12.1 for ; Tue, 30 Jan 2024 18:21:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706667698; x=1707272498; 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=hKR2GgEa0XwXp3Hht6tYHGsgykpVtUmnNq2sHanXTgY=; b=N8FXKCqauSUCFVv5zwLcPt0CYbxae42hw8Tjq+P1OohV11oQcp3f6vtgIZrYg+W6LD grboKznXFT2/LnCrVU8oy7kQhRXjs2eiJ4Mdracjj1sWIpxLTMxRmwMJwtJ6BcPzl7xv +U/GoOel33h3X5VdbBmi287S0AtZe9S2rBuM/tiZBvDpbkbVqiJEzafyZ/mde9CPVG23 dCKZ2XHAEKhrJVfA8m8RAkJt1Rj26JLDouSWZpNijrwHeiLZinIzp0+YqVKK/c5hkLez Yi1uzP37PJvqgrQ0m4HlGWf4iMDUdXOsiXVA+CZnFoHFLpdWti1gltJsvtrcPO/vSLC+ uSEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706667698; x=1707272498; 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=hKR2GgEa0XwXp3Hht6tYHGsgykpVtUmnNq2sHanXTgY=; b=HiiasyowKrABkr285B8n56N6O//WVoey/TB3gaT9y9R4OlxkpevWwv8tpw+aRg4i7t IbhnWl1LQQC23hrhMqcGUxuNojHr0L50U/lkT0P4+B15J8SQRy7+pcFso8aD8p4SOsKp r9icr3Ch/OTS6fdFaXWQgMfMHg2TeACAWEAKhMQNrU2EEg5XJDTt40gdQnemO0XFXcA5 arxD4fgFaukcn+crvyu86Aq5K+8j2IJMYMHrpdN70o76nuNJdHXwz3oG8I+zYPzbDhJm 3g+e0R4a3tROK6zjf73s+RjszbWropLnqZQYf1XqY6Oc1qaXlhNryyN/qsARRGbhrH6D 8zgA== X-Gm-Message-State: AOJu0Yy/4FKy6K+EHSDZZ6ChU11xtFeDVg2B6HjlFwrZqQ31FOE5rT1S fVL+jcvYoAWW+kOeWAp4mF51NDRZRdd00d5TuAkeb09WSXRVWAyFC86PEvPf X-Google-Smtp-Source: AGHT+IGnb9isa9zsSXP/fqEJQhwHZqxh8pt4fXgY6gHON9EYiw2E5sQMr7phbfuRqlghkdk6gUQMkw== X-Received: by 2002:a05:6a20:85a6:b0:19c:9c2e:7860 with SMTP id s38-20020a056a2085a600b0019c9c2e7860mr293686pzd.13.1706667698250; Tue, 30 Jan 2024 18:21:38 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.169.51]) by smtp.gmail.com with ESMTPSA id jg3-20020a17090326c300b001d8f6f4541csm3297376plb.75.2024.01.30.18.21.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 18:21:37 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 85DD37400A0; Tue, 30 Jan 2024 18:21:36 -0800 (PST) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Cc: jakub@redhat.com Subject: [PATCH v2] Handle private COMDAT function symbol reference in readonly data section Date: Tue, 30 Jan 2024 18:21:36 -0800 Message-ID: <20240131022136.572689-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3023.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 Changes in v2: 1. Check decl non-null before dereferencing it. 2. Update PR rtl-optimization/113617 from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113617#c14 --- For a private COMDAT function symbol reference in readonly data section, instead of putting it in .data.rel.ro or .rodata.cst section, call function_rodata_section to get the read-only or relocated read-only data section associated with the function DECL so that the COMDAT section will be used for the private COMDAT function symbol. gcc/ PR rtl-optimization/113617 * varasm.cc (default_elf_select_rtx_section): Call function_rodata_section to get the read-only or relocated read-only data section for private COMDAT function symbol reference. gcc/testsuite/ PR rtl-optimization/113617 * g++.dg/pr113617-1a.C: New test. * g++.dg/pr113617-1b.C: Likewise. --- gcc/testsuite/g++.dg/pr113617-1a.C | 145 +++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/pr113617-1b.C | 8 ++ gcc/varasm.cc | 30 ++++++ 3 files changed, 183 insertions(+) create mode 100644 gcc/testsuite/g++.dg/pr113617-1a.C create mode 100644 gcc/testsuite/g++.dg/pr113617-1b.C diff --git a/gcc/testsuite/g++.dg/pr113617-1a.C b/gcc/testsuite/g++.dg/pr113617-1a.C new file mode 100644 index 00000000000..c93f08b5068 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr113617-1a.C @@ -0,0 +1,145 @@ +// { dg-do compile { target fpic } } +// { dg-require-visibility "" } +// { dg-options "-O2 -std=c++11 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden" } + +namespace { +template struct J { static constexpr int value = V; }; +template using K = J; +using M = K; +template struct L { template using type = _Tp; }; +template using N = typename L<_Cond>::type<_If, _Else>; +M k; +template struct O { using type = _Tp; }; +template +struct P : N, _Up> {}; +template struct Q { using type = typename P<_Tp>::type; }; +} +namespace R { +struct H; +enum G {}; +template class S; +struct T { using U = bool (*) (H &, const H &, G); U F; }; +template class B; +template +struct B<_R(_A...), _F> { + static bool F(H &, const H &, G) { return false; } + __attribute__((noipa)) static _R bar(const H &) {} +}; +template +struct S<_R(_A...)> : T { + template using AH = B<_R(), _F>; + template S(_F) { + using AG = AH<_F>; + barr = AG::bar; + F = AG::F; + } + using AF = _R (*)(const H &); + AF barr; +}; +template class I; +template +struct I<_F(_B...)> {}; +template using W = decltype(k); +template struct V { + typedef I::type(typename Q<_B>::type...)> type; +}; +template +__attribute__((noipa)) typename V::value, _F, _B...>::type +baz(_F, _B...) { return typename V::value, _F, _B...>::type (); } +template struct AJ { + template struct _Ptr { using type = _Up *; }; + using AI = typename _Ptr<_Tp>::type; +}; +template struct Y { + using AI = typename AJ<_Tp>::AI; + AI operator->(); +}; +} +extern int z; +namespace N1 { +namespace N2 { +namespace N3 { +enum Z { Z1, Z2 }; +template struct X { + template + __attribute__((noipa)) void boo(long long, long long, long long, _F &) {} +}; +struct AC { + AC(int); + void m1(R::S); +}; +template +__attribute__((noipa)) void garply(void *, long long, long long, long long) {} +template <> +template +void X::boo(long long, long long x, long long y, _F &fi) { + AC pool(z); + for (;;) { + auto job = R::baz(garply<_F>, &fi, y, y, x); + pool.m1(job); + } +} +struct AB { + static AB &bleh(); + template + void boo(long first, long x, long y, _F fi) { + switch (ab1) { + case Z1: + ab2->boo(first, x, y, fi); + case Z2: + ab3->boo(first, x, y, fi); + } + } + Z ab1; + R::Y> ab2; + R::Y> ab3; +}; +template struct C; +template struct C<_F, false> { + __attribute__((noipa)) C(_F) {} + void boo(long first, long x, long y) { + auto u = AB::bleh(); + u.boo(first, x, y, *this); + } +}; +template struct AA { typedef C<_F, 0> type; }; +} +} +} +struct AD { + template + static void boo(long first, long x, long y, _F f) { + typename N1::N2::N3::AA<_F>::type fi(f); + fi.boo(first, x, y); + } + template + static void boo(long first, long x, _F f) { + boo(first, x, 0, f); + } +}; +template struct A { + void foo(long long, long long); + int *c; +}; +namespace { +template struct D { __attribute__((noipa)) D(int *) {} }; +} +template +void A::foo(long long x, long long y) +{ + int e; + D d(&e); + AD::boo(0, y, d); + long p; + for (p = 0; p < x; p++) + c[p] = c[p - 1]; +} +int z; +long xx1; +void corge() { + A a; + a.foo(xx1, 0); +} + +// { dg-final { scan-assembler-not ".section\t\.data\.rel\.ro\.local,\"aw\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } +// { dg-final { scan-assembler ".section\t.data\.rel\.ro\.local\..*,\"awG\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } diff --git a/gcc/testsuite/g++.dg/pr113617-1b.C b/gcc/testsuite/g++.dg/pr113617-1b.C new file mode 100644 index 00000000000..4a02bf44e72 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr113617-1b.C @@ -0,0 +1,8 @@ +// { dg-do compile { target fpic } } +// { dg-require-visibility "" } +// { dg-options "-O2 -std=c++11 -fno-pic -fvisibility=hidden -fvisibility-inlines-hidden" } + +#include "pr113617-1a.C" + +// { dg-final { scan-assembler-not ".section\t\.rodata\.cst(4|8),\"aM\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } +// { dg-final { scan-assembler ".section\t\.rodata\..*,\"aG\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } diff --git a/gcc/varasm.cc b/gcc/varasm.cc index fa17eff551e..c26d95b7178 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -7459,16 +7459,46 @@ default_elf_select_rtx_section (machine_mode mode, rtx x, { int reloc = compute_reloc_for_rtx (x); + tree decl = nullptr; + + /* If it is a private COMDAT function symbol reference, call + function_rodata_section for the read-only or relocated read-only + data section associated with function DECL so that the COMDAT + section will be used for the private COMDAT function symbol. */ + if (HAVE_COMDAT_GROUP) + { + if (GET_CODE (x) == CONST + && GET_CODE (XEXP (x, 0)) == PLUS + && CONST_INT_P (XEXP (XEXP (x, 0), 1))) + x = XEXP (XEXP (x, 0), 0); + + if (GET_CODE (x) == SYMBOL_REF) + { + decl = SYMBOL_REF_DECL (x); + if (decl + && (TREE_CODE (decl) != FUNCTION_DECL + || !DECL_COMDAT_GROUP (decl) + || TREE_PUBLIC (decl))) + decl = nullptr; + } + } + /* ??? Handle small data here somehow. */ if (reloc & targetm.asm_out.reloc_rw_mask ()) { + if (decl) + return targetm.asm_out.function_rodata_section (decl, true); + if (reloc == 1) return get_named_section (NULL, ".data.rel.ro.local", 1); else return get_named_section (NULL, ".data.rel.ro", 3); } + if (decl) + return targetm.asm_out.function_rodata_section (decl, false); + return mergeable_constant_section (mode, align, 0); }