From patchwork Thu Jun 9 13:23:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 1641244 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=gEiiS/S4; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LJlDY6y7Nz9sFk for ; Thu, 9 Jun 2022 23:24:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C5003831EFB for ; Thu, 9 Jun 2022 13:24:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by sourceware.org (Postfix) with ESMTPS id 2D79538344FB for ; Thu, 9 Jun 2022 13:23:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2D79538344FB Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=acm.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qv1-xf30.google.com with SMTP id q104so299771qvq.8 for ; Thu, 09 Jun 2022 06:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:message-id:date:mime-version:user-agent:content-language:to :from:subject; bh=TDXUeKjIBPRLyD9JzVi6M+tSEe1bIfLHyxxu7i5IUIo=; b=gEiiS/S4XuRqmFXeXJ00SIcJ6FS99Q6YDiwxu0JZii5kBTe4W/Mje91PUWizDFhpZb z8wzdj9Aw188n9Yd02+KNGmwzvwG6CjEge8ev0IDRmz/PvPkkvRB9cl3/gr7hztOdI4m bCxUgM4mNNq2YeugITytQE6OvDN95FJuQYuC2CmNifX5r7BYFh18Wm+2LFk5oGObBtBW 0LlBzlv6rA0b2kg04lQIbj7upp443PKYHOl0zBSLqF0jxCsdMJ4VnZXZ94ncH/HQrtPy ZWWZQDGYSB2W4Ov1jyI6k5+e/g2W68/vQd6gzgRaIGrHaEfYMh07fokf4bA+4rQQhEum +bRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:message-id:date:mime-version:user-agent :content-language:to:from:subject; bh=TDXUeKjIBPRLyD9JzVi6M+tSEe1bIfLHyxxu7i5IUIo=; b=ug2d54JwziwZN1rh0tdYnserj41PXCW5hyS6rxiQeN9K3qXf20OgCnqsyiLntLYCkT bz/Ll5TP48fiH3tf93Tgsn5UanF3wWqh7ZPEBJkYAStjT7GuLgTpkKBvohe5oAm4V4W9 2XC08mVX/aWCJd/WIoWo2tFJL0GEhnXjGYz7IROQAoxu7EQyeCoriopehoz6jUprJ0EY IVdDcGLxXVZAbxg4jersre95RRpVHeKRZaC8aLdvvgOcQHZPqOM1CXlVhMiU2a2O5ZRl tjFbLDe+nfJ6Gd6e6fzR8I+mtJ3FJZXB9l6tMQ34aK4F/8OoKjCQkQDU9V643R9P4xJt aepg== X-Gm-Message-State: AOAM5305tH7ueiw/YG94rWKf2cfOF05egqRzqAF5etnA0HFhrKFCIdSK oODK7lUCvH5feEoybkXmM2ilcTKMyzs= X-Google-Smtp-Source: ABdhPJyZvfyrW/S6xHt8l0ZQW1PgiT2nkeob9Yi3a/eBC/BkBGtMEqt1OpmE+pvuzZJMGs69rQrWUA== X-Received: by 2002:a05:6214:1ccb:b0:467:d4e1:e764 with SMTP id g11-20020a0562141ccb00b00467d4e1e764mr27235224qvd.72.1654781003351; Thu, 09 Jun 2022 06:23:23 -0700 (PDT) Received: from ?IPV6:2620:10d:c0a3:1407:97f7:8d5b:1cec:9cbf? ([2620:10d:c091:500::a8b5]) by smtp.googlemail.com with ESMTPSA id i10-20020ac813ca000000b00304ed1cf3f1sm7869488qtj.80.2022.06.09.06.23.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Jun 2022 06:23:22 -0700 (PDT) Message-ID: Date: Thu, 9 Jun 2022 09:23:21 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Content-Language: en-US To: GCC Patches From: Nathan Sidwell Subject: c++: Better module initializer code X-Spam-Status: No, score=-3038.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, 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.29 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 Sender: "Gcc-patches" Every module interface needs to emit a global initializer, but it might have nothing to init. In those cases, there's no need for any idempotency boolean to be emitted. nathan From 227ffed7dbbdffeeb5bc013852d61a97aa468c62 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Wed, 8 Jun 2022 11:25:14 -0700 Subject: [PATCH] c++: Better module initializer code Every module interface needs to emit a global initializer, but it might have nothing to init. In those cases, there's no need for any idempotency boolean to be emitted. gcc/cp * cp-tree.h (module_initializer_kind): Replace with ... (module_global_init_needed, module_has_import_inits): ... these. * decl2.cc (start_objects): Add has_body parm. Reorganize module initializer creation. (generate_ctor_or_dtor_function): Adjust. (c_parse_final_cleanups): Adjust. (vtv_start_verification_constructor_init_function): Adjust. * module.cc (module_initializer_kind): Replace with ... (module_global_init_needed, module_has_import_inits): ... these. gcc/testsuite/ * g++.dg/modules/init-2_a.C: Check no idempotency. * g++.dg/modules/init-2_b.C: Check idempotency. --- gcc/cp/cp-tree.h | 3 ++- gcc/cp/decl2.cc | 32 +++++++++++++------------ gcc/cp/module.cc | 23 +++++++++--------- gcc/testsuite/g++.dg/modules/init-2_a.C | 2 ++ gcc/testsuite/g++.dg/modules/init-2_b.C | 2 ++ 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 3d8a08b8dd7..a5d93282167 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7179,7 +7179,8 @@ extern module_state *get_module (tree name, module_state *parent = NULL, bool partition = false); extern bool module_may_redeclare (tree decl); -extern int module_initializer_kind (); +extern bool module_global_init_needed (); +extern bool module_has_import_inits (); extern void module_add_import_initializers (); /* Where the namespace-scope decl was originally declared. */ diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index bfb6a32e3b6..9de9a7a4f8a 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -55,7 +55,7 @@ int raw_dump_id; extern cpp_reader *parse_in; -static tree start_objects (bool, unsigned); +static tree start_objects (bool, unsigned, bool); static tree finish_objects (bool, unsigned, tree); static tree start_partial_init_fini_fn (bool, unsigned, unsigned); static void finish_partial_init_fini_fn (tree); @@ -3848,15 +3848,13 @@ generate_tls_wrapper (tree fn) /* Start a global constructor or destructor function. */ static tree -start_objects (bool initp, unsigned priority) +start_objects (bool initp, unsigned priority, bool has_body) { - int module_init = 0; - - if (priority == DEFAULT_INIT_PRIORITY && initp) - module_init = module_initializer_kind (); - + bool default_init = initp && priority == DEFAULT_INIT_PRIORITY; + bool is_module_init = default_init && module_global_init_needed (); tree name = NULL_TREE; - if (module_init > 0) + + if (is_module_init) name = mangle_module_global_init (0); else { @@ -3880,7 +3878,7 @@ start_objects (bool initp, unsigned priority) tree fntype = build_function_type (void_type_node, void_list_node); tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype); DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace); - if (module_init > 0) + if (is_module_init) { SET_DECL_ASSEMBLER_NAME (fndecl, name); TREE_PUBLIC (fndecl) = true; @@ -3905,8 +3903,10 @@ start_objects (bool initp, unsigned priority) tree body = begin_compound_stmt (BCS_FN_BODY); - if (module_init > 0) + bool has_import_inits = default_init && module_has_import_inits (); + if (is_module_init && (has_import_inits || has_body)) { + // If the function is going to be empty, don't emit idempotency. // 'static bool __in_chrg = false; // if (__inchrg) return; // __inchrg = true @@ -3930,7 +3930,7 @@ start_objects (bool initp, unsigned priority) finish_expr_stmt (assign); } - if (module_init) + if (has_import_inits) module_add_import_initializers (); return body; @@ -4321,7 +4321,7 @@ generate_ctor_or_dtor_function (bool initp, unsigned priority, { input_location = locus; - tree body = start_objects (initp, priority); + tree body = start_objects (initp, priority, bool (fns)); /* To make sure dynamic construction doesn't access globals from other compilation units where they might not be yet constructed, for @@ -4359,7 +4359,9 @@ generate_ctor_or_dtor_function (bool initp, unsigned priority, if (initp && (flag_sanitize & SANITIZE_ADDRESS)) finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true)); - /* Close out the function. */ + /* Close out the function, and arrange for it to be called at init + or fini time. (Even module initializer functions need this, as + we cannot guarantee the module is imported somewhere in the programq.) */ expand_or_defer_fn (finish_objects (initp, priority, body)); } @@ -5205,7 +5207,7 @@ c_parse_final_cleanups (void) push_lang_context (lang_name_c); if ((c_dialect_objc () && objc_static_init_needed_p ()) - || module_initializer_kind ()) + || module_global_init_needed () || module_has_import_inits ()) { // Make sure there's a default priority entry. if (!static_init_fini_fns[true]) @@ -5871,7 +5873,7 @@ mark_used (tree decl) tree vtv_start_verification_constructor_init_function (void) { - return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1); + return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true); } tree diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index d1dc73724d1..7e36996c0fc 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -19026,22 +19026,21 @@ declare_module (module_state *module, location_t from_loc, bool exporting_p, } } -/* +1, we're the primary or a partition. Therefore emitting a - globally-callable idemportent initializer function. - -1, we have direct imports. Therefore emitting calls to their - initializers. */ +/* Return true IFF we must emit a module global initializer function + (which will be called by importers' init code). */ -int -module_initializer_kind () +bool +module_global_init_needed () { - int result = 0; + return module_has_cmi_p () && !header_module_p (); +} - if (module_has_cmi_p () && !header_module_p ()) - result = +1; - else if (num_init_calls_needed) - result = -1; +/* Return true IFF we have import global inits to call. */ - return result; +bool +module_has_import_inits () +{ + return bool (num_init_calls_needed); } /* Emit calls to each direct import's global initializer. Including diff --git a/gcc/testsuite/g++.dg/modules/init-2_a.C b/gcc/testsuite/g++.dg/modules/init-2_a.C index c0390a1b56e..4174cf53911 100644 --- a/gcc/testsuite/g++.dg/modules/init-2_a.C +++ b/gcc/testsuite/g++.dg/modules/init-2_a.C @@ -3,3 +3,5 @@ export module Foo; // { dg-module-cmi Foo } // { dg-final { scan-assembler {_ZGIW3Foo:} } } +// But it is empty, and so no idempotency bool +// { dg-final { scan-assembler-not {_ZZ9_ZGIW3FooE9__in_chrg} } } diff --git a/gcc/testsuite/g++.dg/modules/init-2_b.C b/gcc/testsuite/g++.dg/modules/init-2_b.C index 912ee406931..4350944139f 100644 --- a/gcc/testsuite/g++.dg/modules/init-2_b.C +++ b/gcc/testsuite/g++.dg/modules/init-2_b.C @@ -5,4 +5,6 @@ export module Bar; import Foo; // { dg-final { scan-assembler {_?_ZGIW3Bar:} } } +// There should be an idempotency check +// { dg-final { scan-assembler {_ZZ9_ZGIW3BarE9__in_chrg} } } // { dg-final { scan-assembler {call[ \t]+_?_ZGIW3Foo} { target i?86-*-* x86_64-*-* } } } -- 2.30.2