From patchwork Mon Aug 15 20:42:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 538 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:38f:b0:2d5:3c95:9e21 with SMTP id 15csp1582851pxh; Mon, 15 Aug 2022 13:43:25 -0700 (PDT) X-Google-Smtp-Source: AA6agR48nzJsDZ+Y3sdiU7vcdb05rUT4bUeji1Ni3X0J5TDruqroOvFR7aYHjJpsLf6ObJqdifMf X-Received: by 2002:aa7:c946:0:b0:43d:3038:1381 with SMTP id h6-20020aa7c946000000b0043d30381381mr16306515edt.354.1660596205577; Mon, 15 Aug 2022 13:43:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660596205; cv=none; d=google.com; s=arc-20160816; b=nfO29GFmxR375Gd6ID+FPKQIua5CBglMa/p9Wgg4IiLDbaTV418Avywhmer1kv91JJ 1SZdoil2iS+yhtH4lYAMkpT6+wdpGmFJrgx9E0m3lxPSfClgA9Su4yWXOIUlpTAUV+DJ mh6wJN6A64CvvcpkNjHtmoAB07RBs8hOZpn/aqgh2W7BSvqP59iomd/iX+rtSMZ9pnej J6RZb8WktT0Q4tTG2WbMf2IfoyzrH21lNaC9eGw2v0nUKXZgenout7UxLJI7UBaMUcuK m/uu+MJOb+CT7lunqg9xH+kKqKoGTxqfeHouFM9rgb/cjgkHtpTRi+AjvBvf2PkbqdXs a+ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=8T8yv/J/vYL7HOHSBtUA3NZigQS0479nZepgvrCfbr0=; b=buixsyvI44/ZH/f9HgZv6ePl0XOGDhVeR86t0PFhA1yfMaCgOZMYe/7fyi6oOmNEC0 WZHZU7yEA0/CSZZKB8V2CJjsdiczpxPJdlkFxHHfNzsHjEu1/kNW11qyGqjmnqnTnLdQ 0x6BWncGyJ2qv2yqMRZxx3WRUrBON8NEfsstIKrBQQ3+1oaLpvY5MW6d2dLXtbORzjvG p3I6BpXJGvmkqwu29jr4dSt3Jg2slaF6iN9/5ORFrFLT4v2q54ULTX5ZIl3AYJt0iBUk aQxr6AIlWeaqx/k2mZkaunlVZIwSU+77P+QHhAQdGZSheer17xC+rZH9LityZgQSAH6R iftA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HiSGTXuR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id p5-20020a17090653c500b00726efdeaf13si8192531ejo.164.2022.08.15.13.43.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Aug 2022 13:43:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HiSGTXuR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C641C38582BB for ; Mon, 15 Aug 2022 20:43:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C641C38582BB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660596201; bh=8T8yv/J/vYL7HOHSBtUA3NZigQS0479nZepgvrCfbr0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=HiSGTXuRUKVU8YwVUyOAHU+qsrjVseXJuP3gXI16jnEApMUwJQJDfTziX07SwKMt1 xToYP/CNw8enWiqs6FG+L04vKKUNekWtiz0DP036Volbx4QYLqn06JMct90ygneZqH J+aJ0v/U0EXy23Ghonk3KKYdaKIhfKdr0B7TCXcA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [IPv6:2001:67c:2050:0:465::101]) by sourceware.org (Postfix) with ESMTPS id 7F34F3858C2F for ; Mon, 15 Aug 2022 20:42:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7F34F3858C2F Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (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-101.mailbox.org (Postfix) with ESMTPS id 4M65nR62rmz9sT1; Mon, 15 Aug 2022 22:42:35 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Defer compiling inline definitions until after the module has finished. Date: Mon, 15 Aug 2022 22:42:33 +0200 Message-Id: <20220815204233.2831073-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4M65nR62rmz9sT1 X-Spam-Status: No, score=-13.8 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, 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: , X-Patchwork-Original-From: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1741261326970394559?= X-GMAIL-MSGID: =?utf-8?q?1741261326970394559?= Hi, This patch is a follow-up to r13-2002 and r12-8673 (PR d/106563), that instead pushes inline definitions onto a deferred list to be compiled after the current module has finished. This is to prevent the case of when generating the methods of a struct type, we don't accidentally emit an inline function that references it, as the outer struct itself would still be incomplete. Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-mx32, committed to mainline, and backported to the releases/gcc-12 branch. Regards, Iain. --- gcc/d/ChangeLog: * d-tree.h (d_defer_declaration): Declare. * decl.cc (function_needs_inline_definition_p): Defer checking DECL_UNINLINABLE and DECL_DECLARED_INLINE_P. (maybe_build_decl_tree): Call d_defer_declaration instead of build_decl_tree. * modules.cc (deferred_inline_declarations): New variable. (build_module_tree): Set deferred_inline_declarations and a handle declarations pushed to it. (d_defer_declaration): New function. --- gcc/d/d-tree.h | 1 + gcc/d/decl.cc | 22 +++++++++++----------- gcc/d/modules.cc | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/gcc/d/d-tree.h b/gcc/d/d-tree.h index 809a242ea93..4885cfe2b15 100644 --- a/gcc/d/d-tree.h +++ b/gcc/d/d-tree.h @@ -673,6 +673,7 @@ extern tree maybe_expand_intrinsic (tree); extern void build_module_tree (Module *); extern tree d_module_context (void); extern void register_module_decl (Declaration *); +extern void d_defer_declaration (Declaration *); extern void d_finish_compilation (tree *, int); /* In runtime.cc. */ diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index 0131b01dcc9..e91aee30845 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -1046,18 +1046,10 @@ function_needs_inline_definition_p (FuncDeclaration *fd) if (!DECL_EXTERNAL (fd->csym)) return false; - /* Non-inlineable functions are always external. */ - if (DECL_UNINLINABLE (fd->csym)) - return false; - /* No function body available for inlining. */ if (!fd->fbody) return false; - /* Ignore functions that aren't decorated with `pragma(inline)'. */ - if (fd->inlining != PINLINE::always) - return false; - /* These functions are tied to the module they are defined in. */ if (fd->isFuncLiteralDeclaration () || fd->isUnitTestDeclaration () @@ -1070,6 +1062,14 @@ function_needs_inline_definition_p (FuncDeclaration *fd) if (function_defined_in_root_p (fd)) return false; + /* Non-inlineable functions are always external. */ + if (DECL_UNINLINABLE (fd->csym)) + return false; + + /* Ignore functions that aren't decorated with `pragma(inline)'. */ + if (!DECL_DECLARED_INLINE_P (fd->csym)) + return false; + /* Weak functions cannot be inlined. */ if (lookup_attribute ("weak", DECL_ATTRIBUTES (fd->csym))) return false; @@ -1081,8 +1081,8 @@ function_needs_inline_definition_p (FuncDeclaration *fd) return true; } -/* If the variable or function declaration in DECL needs to be defined, call - build_decl_tree on it now before returning its back-end symbol. */ +/* If the variable or function declaration in DECL needs to be defined, add it + to the list of deferred declarations to build later. */ static tree maybe_build_decl_tree (Declaration *decl) @@ -1103,7 +1103,7 @@ maybe_build_decl_tree (Declaration *decl) if (function_needs_inline_definition_p (fd)) { DECL_EXTERNAL (fd->csym) = 0; - build_decl_tree (fd); + d_defer_declaration (fd); } } diff --git a/gcc/d/modules.cc b/gcc/d/modules.cc index edc79122365..0aac8fe3545 100644 --- a/gcc/d/modules.cc +++ b/gcc/d/modules.cc @@ -121,6 +121,9 @@ static module_info *current_testing_module; static Module *current_module_decl; +/* Any inline symbols that were deferred during codegen. */ +vec *deferred_inline_declarations; + /* Returns an internal function identified by IDENT. This is used by both module initialization and dso handlers. */ @@ -724,6 +727,9 @@ build_module_tree (Module *decl) current_testing_module = &mitest; current_module_decl = decl; + vec deferred_decls = vNULL; + deferred_inline_declarations = &deferred_decls; + /* Layout module members. */ if (decl->members) { @@ -811,9 +817,14 @@ build_module_tree (Module *decl) layout_moduleinfo (decl); } + /* Process all deferred functions after finishing module. */ + for (size_t i = 0; i < deferred_decls.length (); ++i) + build_decl_tree (deferred_decls[i]); + current_moduleinfo = NULL; current_testing_module = NULL; current_module_decl = NULL; + deferred_inline_declarations = NULL; } /* Returns the current function or module context for the purpose @@ -888,6 +899,15 @@ register_module_decl (Declaration *d) } } +/* Add DECL as a declaration to emit at the end of the current module. */ + +void +d_defer_declaration (Declaration *decl) +{ + gcc_assert (deferred_inline_declarations != NULL); + deferred_inline_declarations->safe_push (decl); +} + /* Wrapup all global declarations and start the final compilation. */ void