From patchwork Fri Mar 11 22:37:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1604538 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=WG3No53V; 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 (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KFgms4Kdhz9sG5 for ; Sat, 12 Mar 2022 09:37:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E4AD03857825 for ; Fri, 11 Mar 2022 22:37:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4AD03857825 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1647038264; bh=pJNB0mr5GbFqPxLG3BzHRw2FNrBAPhR5rp9Pse1htrE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=WG3No53VeV+diriSvz2e+3nw7qgXe4QDYdJ6gJkXKpUGwXJABMwdSQNFvhgwlk8Ih E97zl+vWcEHiCW0mPMISiXn6rqC5MpNMD3MCdWXUqNI5ZfG52gOIB+5Czl0x8RPs1X uyVM+hRNYoRlpja9ytf4DE1zsAjlCZgF5VAIPUD4= 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 [IPv6:2001:67c:2050::465:201]) by sourceware.org (Postfix) with ESMTPS id 05E793858D3C for ; Fri, 11 Mar 2022 22:37:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 05E793858D3C Received: from smtp202.mailbox.org (smtp202.mailbox.org [80.241.60.245]) (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 4KFgmL1j56z9sZr; Fri, 11 Mar 2022 23:37:22 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Cache generated import declarations in a hash_map Date: Fri, 11 Mar 2022 23:37:10 +0100 Message-Id: <20220311223710.499645-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch refactors the ImportVisitor to cache the generated result decl in a hash_map. Originally, these were cached in the front-end AST node field `isym'. However, this field is soon to be removed. Bootstrapped and regression tested on x86_64-linux-gnu/m32/mx32, and committed to mainline. Regards, Iain. --- gcc/d/ChangeLog: * imports.cc (imported_decls): Define. (class ImportVisitor): Add result_ field. (ImportVisitor::result): New method. (ImportVisitor::visit (Module *)): Store decl to result_. (ImportVisitor::visit (Import *)): Likewise. (ImportVisitor::visit (AliasDeclaration *)): Don't cache decl in front-end AST node. (ImportVisitor::visit (OverDeclaration *)): Likewise. (ImportVisitor::visit (FuncDeclaration *)): Likewise. (ImportVisitor::visit (Declaration *)): Likewise. (build_import_decl): Use imported_decls to cache and lookup built declarations. --- gcc/d/imports.cc | 77 ++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/gcc/d/imports.cc b/gcc/d/imports.cc index d3a3099ce76..29c0fbfe6d2 100644 --- a/gcc/d/imports.cc +++ b/gcc/d/imports.cc @@ -31,14 +31,17 @@ along with GCC; see the file COPYING3. If not see #include "d-tree.h" +static hash_map *imported_decls; /* Implements the visitor interface to build debug trees for all - module and import declarations, where ISYM holds the cached - back-end representation to be returned. */ + module and import declarations, where RESULT_ holds the back-end + representation to be cached and returned from the caller. */ class ImportVisitor : public Visitor { using Visitor::visit; + tree result_; + /* Build the declaration DECL as an imported symbol. */ tree make_import (tree decl) { @@ -55,6 +58,12 @@ class ImportVisitor : public Visitor public: ImportVisitor (void) { + this->result_ = NULL_TREE; + } + + tree result (void) + { + return this->result_; } /* This should be overridden by each symbol class. */ @@ -70,16 +79,16 @@ public: Loc loc = (m->md != NULL) ? m->md->loc : Loc (m->srcfile.toChars (), 1, 0); - m->isym = build_decl (make_location_t (loc), NAMESPACE_DECL, - get_identifier (m->toPrettyChars ()), - void_type_node); - d_keep (m->isym); + this->result_ = build_decl (make_location_t (loc), NAMESPACE_DECL, + get_identifier (m->toPrettyChars ()), + void_type_node); + d_keep (this->result_); if (!m->isRoot ()) - DECL_EXTERNAL (m->isym) = 1; + DECL_EXTERNAL (this->result_) = 1; - TREE_PUBLIC (m->isym) = 1; - DECL_CONTEXT (m->isym) = NULL_TREE; + TREE_PUBLIC (this->result_) = 1; + DECL_CONTEXT (this->result_) = NULL_TREE; } /* Build an import of another module symbol. */ @@ -87,7 +96,7 @@ public: void visit (Import *m) { tree module = build_import_decl (m->mod); - m->isym = this->make_import (module); + this->result_ = this->make_import (module); } /* Build an import for any kind of user defined type. @@ -141,20 +150,14 @@ public: /* This symbol is really an alias for another, visit the other. */ if (dsym != d) - { - dsym->accept (this); - d->isym = dsym->isym; - } + dsym->accept (this); } /* Visit the underlying alias symbol of overloadable aliases. */ void visit (OverDeclaration *d) { if (d->aliassym != NULL) - { - d->aliassym->accept (this); - d->isym = d->aliassym->isym; - } + d->aliassym->accept (this); } /* Function aliases are the same as alias symbols. */ @@ -163,10 +166,7 @@ public: FuncDeclaration *fd = d->toAliasFunc (); if (fd != NULL) - { - fd->accept (this); - d->isym = fd->isym; - } + fd->accept (this); } /* Skip over importing templates and tuples. */ @@ -182,7 +182,7 @@ public: symbol generation routines, the compiler will throw an error. */ void visit (Declaration *d) { - d->isym = this->make_import (get_symbol_decl (d)); + this->result_ = this->make_import (get_symbol_decl (d)); } }; @@ -192,17 +192,22 @@ public: tree build_import_decl (Dsymbol *d) { - if (!d->isym) - { - location_t saved_location = input_location; - ImportVisitor v; - - input_location = make_location_t (d->loc); - d->accept (&v); - input_location = saved_location; - } - - /* Not all visitors set `isym'. */ - return d->isym ? d->isym : NULL_TREE; -} + hash_map_maybe_create (imported_decls); + + if (tree *decl = imported_decls->get (d)) + return *decl; + location_t saved_location = input_location; + ImportVisitor v = ImportVisitor (); + + input_location = make_location_t (d->loc); + d->accept (&v); + input_location = saved_location; + + /* Not all visitors set `result'. */ + tree isym = v.result (); + if (isym != NULL_TREE) + imported_decls->put (d, isym); + + return isym; +}