From patchwork Sat Apr 17 12:38:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1467501 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=dOx0q4GL; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FMt17458Qz9vG3 for ; Sat, 17 Apr 2021 22:38:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17B68394FC31; Sat, 17 Apr 2021 12:38:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17B68394FC31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1618663127; bh=HV5xosoD3gAOttacTRZ0BcEYpZmEGYAD98BiCByCVnY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=dOx0q4GLlozUEQq+ZqUaMKDddpuGbeqxuclNUCPbU5C35UwvcwhveOXSq9aWbtH7W pfCbXs1x4FR6Nr5aa3Zlh6QOc7SYBF32NExOM6wqImdBqLXbQaji32ja0FMZT1O2Kl VJT1ZiEhLTV6D6t0JDSDyWe7DHUJssVZIpaQxuUk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by sourceware.org (Postfix) with ESMTPS id 08FB2394FC31 for ; Sat, 17 Apr 2021 12:38:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 08FB2394FC31 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4FMt0v3ZlBzQjYt; Sat, 17 Apr 2021 14:38:39 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id 77-Shs1_V0wU; Sat, 17 Apr 2021 14:38:35 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Add TARGET_D_TEMPLATES_ALWAYS_COMDAT Date: Sat, 17 Apr 2021 14:38:34 +0200 Message-Id: <20210417123834.3103758-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: * X-Rspamd-Score: 1.30 / 15.00 / 15.00 X-Rspamd-Queue-Id: 6FBB017EF X-Rspamd-UID: e28bfa X-Spam-Status: No, score=-15.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi, Following up on the fix for PR99914, when testing on MinGW, it was found not to support weak in the same way as on ELF or Mach-O targets. So the linkage has been reverted back to COMDAT for that target, however in order to properly support overriding functions and variables, all declarations with external linkage must be put on COMDAT. For this a new target hook has been added to control the behavior. Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-mx32, as well as on a preliminary x86_64-w64-mingw32 port, committed to mainline. Regards, Iain --- gcc/ChangeLog: PR d/99914 * config/i386/winnt-d.c (TARGET_D_TEMPLATES_ALWAYS_COMDAT): Define. * doc/tm.texi: Regenerate. * doc/tm.texi.in (D language and ABI): Add @hook for TARGET_D_TEMPLATES_ALWAYS_COMDAT. gcc/d/ChangeLog: PR d/99914 * d-target.def (d_templates_always_comdat): New hook. * d-tree.h (mark_needed): Remove prototype. * decl.cc: Include d-target.h. (mark_needed): Rename to... (d_mark_needed): ...this. Make static. (set_linkage_for_decl): Put variables in comdat if d_templates_always_comdat. --- gcc/config/i386/winnt-d.c | 5 +++++ gcc/d/d-target.def | 8 ++++++++ gcc/d/d-tree.h | 1 - gcc/d/decl.cc | 17 +++++++++++++++-- gcc/doc/tm.texi | 6 ++++++ gcc/doc/tm.texi.in | 2 ++ 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/gcc/config/i386/winnt-d.c b/gcc/config/i386/winnt-d.c index b9780258549..ea4cd13d0bf 100644 --- a/gcc/config/i386/winnt-d.c +++ b/gcc/config/i386/winnt-d.c @@ -78,4 +78,9 @@ winnt_d_register_target_info (void) #undef TARGET_D_MINFO_END_NAME #define TARGET_D_MINFO_END_NAME "__stop_minfo" +/* Define TARGET_D_TEMPLATES_ALWAYS_COMDAT for Windows targets. */ + +#undef TARGET_D_TEMPLATES_ALWAYS_COMDAT +#define TARGET_D_TEMPLATES_ALWAYS_COMDAT true + struct gcc_targetdm targetdm = TARGETDM_INITIALIZER; diff --git a/gcc/d/d-target.def b/gcc/d/d-target.def index aa6bf55e6e6..67647515cf2 100644 --- a/gcc/d/d-target.def +++ b/gcc/d/d-target.def @@ -104,5 +104,13 @@ and @var{link_windows} to @code{1} to apply @code{stdcall} to functions with\n\ bool, (unsigned int *link_system, unsigned int *link_windows), hook_bool_uintp_uintp_false) +/* True if instantiations are always COMDAT if they have external linkage. */ +DEFHOOKPOD +(d_templates_always_comdat, + "This flag is true if instantiated functions and variables are always COMDAT\n\ +if they have external linkage. If this flag is false, then instantiated\n\ +decls will be emitted as weak symbols. The default is @code{false}.", + bool, false) + /* Close the 'struct gcc_targetdm' definition. */ HOOK_VECTOR_END (C90_EMPTY_HACK) diff --git a/gcc/d/d-tree.h b/gcc/d/d-tree.h index c1b6f275149..bb731a60541 100644 --- a/gcc/d/d-tree.h +++ b/gcc/d/d-tree.h @@ -631,7 +631,6 @@ extern void d_finish_decl (tree); extern tree make_thunk (FuncDeclaration *, int); extern tree start_function (FuncDeclaration *); extern void finish_function (tree); -extern void mark_needed (tree); extern tree get_vtable_decl (ClassDeclaration *); extern tree build_new_class_expr (ClassReferenceExp *); extern tree aggregate_initializer_decl (AggregateDeclaration *); diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index 8948e40e902..7d1378255bd 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -59,6 +59,7 @@ along with GCC; see the file COPYING3. If not see #include "symtab-thunks.h" #include "d-tree.h" +#include "d-target.h" /* Return identifier for the external mangled name of DECL. */ @@ -1960,8 +1961,8 @@ finish_function (tree old_context) /* Mark DECL, which is a VAR_DECL or FUNCTION_DECL as a symbol that must be emitted in this, output module. */ -void -mark_needed (tree decl) +static void +d_mark_needed (tree decl) { TREE_USED (decl) = 1; @@ -2380,6 +2381,18 @@ set_linkage_for_decl (tree decl) if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)) return d_comdat_linkage (decl); + /* If all instantiations must go in COMDAT, give them that linkage. + This also applies to other extern declarations, so that it is possible + for them to override template declarations. */ + if (targetdm.d_templates_always_comdat) + { + /* Make sure that instantiations are not removed. */ + if (flag_weak_templates && DECL_INSTANTIATED (decl)) + d_mark_needed (decl); + + return d_comdat_linkage (decl); + } + /* Instantiated variables and functions need to be overridable by any other symbol with the same name, so give them weak linkage. */ if (DECL_INSTANTIATED (decl)) diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 97c8eebcd6f..823f85ba9ab 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -10850,6 +10850,12 @@ and @var{link_windows} to @code{1} to apply @code{stdcall} to functions with @code{extern(Windows)} linkage. @end deftypefn +@deftypevr {D Target Hook} bool TARGET_D_TEMPLATES_ALWAYS_COMDAT +This flag is true if instantiated functions and variables are always COMDAT +if they have external linkage. If this flag is false, then instantiated +decls will be emitted as weak symbols. The default is @code{false}. +@end deftypevr + @node Named Address Spaces @section Adding support for named address spaces @cindex named address spaces diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index e2d49ee9f57..2321a5fc4e0 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -7369,6 +7369,8 @@ floating-point support; they are not included in this mechanism. @hook TARGET_D_HAS_STDCALL_CONVENTION +@hook TARGET_D_TEMPLATES_ALWAYS_COMDAT + @node Named Address Spaces @section Adding support for named address spaces @cindex named address spaces