From patchwork Tue Jan 30 12:56:44 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: 1892945 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=e3m1KprZ; 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 4TPQDT2rS4z23gZ for ; Tue, 30 Jan 2024 23:57:13 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6994E385E45A for ; Tue, 30 Jan 2024 12:57:11 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id 579D738618AE for ; Tue, 30 Jan 2024 12:56:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 579D738618AE 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 579D738618AE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::633 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706619409; cv=none; b=BVr96NMjSyaGP5zF9zDDVI1p2kWmSN3nesPIPp7DjLUEyVyVKhLmi+C7YEvvj4ue1cFzGuzQRfHTp15ASenXNunV+RHX1H5JHGqZvn2FdKNabjdXecy9bLxMErFb/lJeyF48lU6FIgcG0CF82ldYBPgPSJKjJtbBDQ0XVfLzVew= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706619409; c=relaxed/simple; bh=n8TyhoQpSHGxmGsYnIOh6roJ8aNuNDCSfg3tqMoLZxM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=s3me94wDNsDw8PqlM4B4UVbYNiRWz6rzB2mDss+scUuTvy4J86EXOoQcxbe91JZsKRVE/TdR5fcWnZUamTG2RfRYJogz4lF8tQ7FmUTspIHI8C5ew7CdlGJId0kdQngMAWVtq1OhiwzHJlzBxH0WKay1BtSljzf3wbqXjEydvU0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1d911dac3dcso2786385ad.0 for ; Tue, 30 Jan 2024 04:56:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706619406; x=1707224206; 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=sOpTVCzEoOalQGlNfrGuDdKsvaBq0VC5XiG7v3ZdY1c=; b=e3m1KprZrgRrNMf5haE+MkmTNhlUZmDY8BgFkaLN2SuzhctmUBLdP7cIbR/0XqO2b+ D8qbxa1n324GFKZZqrQuk8WZlqGCG1NMYHq85mj0QCcWF295PF1YY/O+9vOe72o0/cu4 xrsjvDCyj5a6yef+Tlk0yJy5rwjoLFtKn5Wk2JbqF947gHqRlFD6T9C6+ucOuxeR2Wiq ySGcE+YeOjx5t1eIt+iP74pkdoOnMQ52XgdFCTxn2hXHNhgJwZr7Qj5TRCdMBIPJl+Ss td014kjEidw9f8e32xngJ9KrdkTFYqZx1KgcWj7YxxnjtqNzepyc2rBsjqlq0kTu2ye7 /ANg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706619406; x=1707224206; 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=sOpTVCzEoOalQGlNfrGuDdKsvaBq0VC5XiG7v3ZdY1c=; b=dUEbsi0BQb3EmCDuL2+eI9if2g4Xme2xLX/V3tDR0aSTlpUx3Wzks1SLbas6aC80pV xgrgmTNUimMHWKRYZkU9MET9WDH0jZzEFZtNh5SG0dpM1jDt8xmTEATHgYEKyusLAc3O a0GlnwGK4xkLS3F6A3mv6AGBfqjdK4tQsZwqMt/6eJ2CwTcQTDUuLyutS0K8ljTA4Rx4 cxABXtEwOJfar0fVuQJ5blImiMtF0li51NF+aOZw5GlOenuzj+OeM/yzCsNX6S/YSlBL k6IsE1skununAnhVQQOAl+cPVtWvAezsznui9G9hHDFnaKSv9bOxLBLnioBFhm5jzuuT EWkA== X-Gm-Message-State: AOJu0YxLcebRWjNPkefjsKbbYk1Xb3+Jy/EX1zHyC30jTEUXPPxKF0PF D7UfDhhigHe9O+w8uVo7ciCqn+2Nr3hNtOhCXTUq+uY3h2DR7fKJ8fKlW8id X-Google-Smtp-Source: AGHT+IFNPOdy8xCjLX9V5Z4Gcs2kPHADFxP1GzXd1DyTppNBnJravYSoEsrV6dNmFfuN8T96yHyZTQ== X-Received: by 2002:a17:902:fe18:b0:1d5:e583:628f with SMTP id g24-20020a170902fe1800b001d5e583628fmr1364185plj.47.1706619405832; Tue, 30 Jan 2024 04:56:45 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.169.51]) by smtp.gmail.com with ESMTPSA id j17-20020a170902c3d100b001d7273e380fsm3126827plj.153.2024.01.30.04.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:56:45 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 386537400A0; Tue, 30 Jan 2024 04:56:44 -0800 (PST) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Cc: jakub@redhat.com Subject: [PATCH] Handle private COMDAT function symbol reference in readonly data section Date: Tue, 30 Jan 2024 04:56:44 -0800 Message-ID: <20240130125644.75373-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_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 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 | 170 +++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/pr113617-1b.C | 8 ++ gcc/varasm.cc | 29 +++++ 3 files changed, 207 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..effd50841c0 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr113617-1a.C @@ -0,0 +1,170 @@ +// { dg-do compile { target fpic } } +// { dg-require-visibility "" } +// { dg-options "-O2 -std=c++11 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden" } + +namespace { +template struct integral_constant { + static constexpr int value = __v; +}; +template using __bool_constant = integral_constant<__v>; +using true_type = __bool_constant; +template struct __conditional { + template using type = _Tp; +}; +template +using __conditional_t = typename __conditional<_Cond>::type<_If, _Else>; +true_type __trans_tmp_1; +template struct remove_cv { using type = _Tp; }; +template +struct __decay_selector + : __conditional_t, _Up> {}; +template struct decay { + using type = typename __decay_selector<_Tp>::type; +}; +} +struct vtkCellArray {}; +namespace blah { +struct _Any_data; +enum _Manager_operation {}; +template class function; +struct _Function_base { + using _Manager_type = bool (*)(_Any_data &, const _Any_data &, + _Manager_operation); + _Manager_type _M_manager; +}; +template class _Function_handler; +template +struct _Function_handler<_Res(_ArgTypes...), _Functor> { + static bool _M_manager(_Any_data &, const _Any_data &, _Manager_operation) { + return false; + } + __attribute__((noipa)) static _Res _M_invoke(const _Any_data &) {} +}; +template +struct function<_Res(_ArgTypes...)> : _Function_base { + template + using _Handler = _Function_handler<_Res(), _Functor>; + template function(_Functor) { + using _My_handler = _Handler<_Functor>; + _M_invoker = _My_handler::_M_invoke; + _M_manager = _My_handler::_M_manager; + } + using _Invoker_type = _Res (*)(const _Any_data &); + _Invoker_type _M_invoker; +}; +template class _Bind; +template +struct _Bind<_Functor(_Bound_args...)> {}; +template using __is_socketlike = decltype(__trans_tmp_1); +template struct _Bind_helper { + typedef _Bind::type( + typename decay<_BoundArgs>::type...)> + type; +}; +template +__attribute__((noipa)) typename _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type +bind(_Func, _BoundArgs...) { return typename _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type (); } +template struct __uniq_ptr_impl { + template struct _Ptr { using type = _Up *; }; + using pointer = typename _Ptr<_Tp>::type; +}; +template struct unique_ptr { + using pointer = typename __uniq_ptr_impl<_Tp>::pointer; + pointer operator->(); +}; +} +extern int For_threadNumber; +namespace vtk { +namespace detail { +namespace smp { +enum BackendType { Sequential, STDThread }; +template struct vtkSMPToolsImpl { + template + __attribute__((noipa)) void For(long long, long long, long long, FunctorInternal &) {} +}; +struct vtkSMPThreadPool { + vtkSMPThreadPool(int); + void DoJob(blah::function); +}; +template +__attribute__((noipa)) void ExecuteFunctorSTDThread(void *, long long, long long, long long) {} +template <> +template +void vtkSMPToolsImpl::For(long long, long long last, long long grain, + FunctorInternal &fi) { + vtkSMPThreadPool pool(For_threadNumber); + for (;;) { + auto job = blah::bind(ExecuteFunctorSTDThread, &fi, grain, + grain, last); + pool.DoJob(job); + } +} +struct vtkSMPToolsAPI { + static vtkSMPToolsAPI &GetInstance(); + template + void For(long first, long last, long grain, FunctorInternal fi) { + switch (ActivatedBackend) { + case Sequential: + SequentialBackend->For(first, last, grain, fi); + case STDThread: + STDThreadBackend->For(first, last, grain, fi); + } + } + BackendType ActivatedBackend; + blah::unique_ptr> SequentialBackend; + blah::unique_ptr> STDThreadBackend; +}; +template struct vtkSMPTools_FunctorInternal; +template struct vtkSMPTools_FunctorInternal { + __attribute__((noipa)) vtkSMPTools_FunctorInternal(Functor) {} + void For(long first, long last, long grain) { + auto SMPToolsAPI = vtkSMPToolsAPI::GetInstance(); + SMPToolsAPI.For(first, last, grain, *this); + } +}; +template struct vtkSMPTools_Lookup_For { + typedef vtkSMPTools_FunctorInternal type; +}; +} +} +} +struct vtkSMPTools { + template + static void For(long first, long last, long grain, Functor f) { + typename vtk::detail::smp::vtkSMPTools_Lookup_For::type fi(f); + fi.For(first, last, grain); + } + template + static void For(long first, long last, Functor f) { + For(first, last, 0, f); + } +}; +template struct vtkStaticCellLinksTemplate { + void ThreadedBuildLinks(long long, long long, vtkCellArray *); + int *Offsets; +}; +namespace { +template struct CountUses { __attribute__((noipa)) CountUses(vtkCellArray *, int *) {} }; +} +template +void vtkStaticCellLinksTemplate::ThreadedBuildLinks( + long long numPts, long long numCells, vtkCellArray *cellArray) { + int counts; + CountUses count(cellArray, &counts); + vtkSMPTools::For(0, numCells, count); + long ptId, npts; + for (ptId = 0; ptId < numPts; ptId++) + Offsets[ptId] = Offsets[ptId - 1] + npts; +} + +int For_threadNumber; +long vtkConstrainedSmoothingFilterRequestData_numPts; +void vtkConstrainedSmoothingFilterRequestData() { + vtkCellArray lines; + vtkStaticCellLinksTemplate links; + links.ThreadedBuildLinks(vtkConstrainedSmoothingFilterRequestData_numPts, 0, + &lines); +} + +// { 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..3237aa29c48 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -7459,16 +7459,45 @@ 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 (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); }