From patchwork Wed Jan 26 23:16:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 50474 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BFB74383F41F for ; Wed, 26 Jan 2022 23:17:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BFB74383F41F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643239031; bh=/O1UqBxv+/yrsQSTGTFSsAY6aZdm/UfT5Plhml8WzHs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VTmQ8OMUUlTwoBRLXsXWDbWnCHg1M9qEQ1OzRmSFMHv9ezzFgkO9FpkbiM3sc1osP 0nFcNzXu16SDguXZ2t/BkpcBymXZYjTYYlVijWiBt6oZ0QsryManRj3CV0LIw+FTCM LtVIMDIhM46uI1ZZeD7kcfhbudTOuQ4xz9AB4P/M= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 37F073870C09 for ; Wed, 26 Jan 2022 23:16:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 37F073870C09 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-42-LojpQzzAMje28fYZZavcWQ-1; Wed, 26 Jan 2022 18:16:39 -0500 X-MC-Unique: LojpQzzAMje28fYZZavcWQ-1 Received: by mail-qv1-f71.google.com with SMTP id kd18-20020a056214401200b0041c3942383aso1460460qvb.9 for ; Wed, 26 Jan 2022 15:16:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=/O1UqBxv+/yrsQSTGTFSsAY6aZdm/UfT5Plhml8WzHs=; b=4iD9nazKoX0TZ20qqIXtdK3yOthek62Ik3e491dCBsHKP3Jp1TzlHpFVXAfcnL8FD5 8vgHVdq+K26WVMVL7ik3fjS3I7mzZM7cZFe0t2DBXYkSi4UzY47F9k7mXLt0NWTCADkG nUOZgEFdoNp8aqABZaI603XX3l0AUfIV6j3sVsSyJmDFqbz8bvN1vWG8BeiSpNVhktkW LDsN5/XiZ0ZX0+68pLL7yIr+8N02ElRyYdqhxWQpS+W2I74Z+wEZ/u7rxSbAC28GuRWd 1Y0pDry1q4GPnae/RiN2fuSNrMXMdxibEkA7wYU6k5PjzIDEmNUZo6tHA8z9M4GIZdKf SdNw== X-Gm-Message-State: AOAM531D8dGFtdIAo56N+IYQmdq/1AxLRgBNOrvj3U39P5vA6SxZ3QX4 q/PY04zph+0OvI6unrDnhzeCzhA5mSZl2zoy+oyP0olEYSpi7WeFwBTJHAs3BbcPAc0v7csVP8w SICn3likPcBrGSK/0dvkfmsYuosOtK9/COufO7cpQLfrSm0Y+1YqmGGDPNDbPBYbs8Q== X-Received: by 2002:a37:ac16:: with SMTP id e22mr844114qkm.684.1643238998220; Wed, 26 Jan 2022 15:16:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJyKIA1c3CSKKwyki71RJr4dmbHnw1bx9vTrR1qe7Dhe5mZaCxwcMBp100y165s7K99JTWrndg== X-Received: by 2002:a37:ac16:: with SMTP id e22mr844099qkm.684.1643238997733; Wed, 26 Jan 2022 15:16:37 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id w9sm363949qtk.89.2022.01.26.15.16.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 15:16:36 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA (tree)] c++: lambda in template default argument [PR103186] Date: Wed, 26 Jan 2022 18:16:34 -0500 Message-Id: <20220126231634.1250371-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The problem with this testcase was that since my patch for PR97900 we weren't preserving DECL_UID identity for parameters of instantiations of templated functions, so using those parameters as the keys for the defarg_inst map broke. I think this was always fragile given the possibility of redeclarations, so instead of reverting that change let's switch to keying off the function. Memory use compiling stdc++.h is not noticeably different. Tested x86_64-pc-linux-gnu. Is the tree.{h,cc} change, just moving the class definition from one to the other, OK for trunk? PR c++/103186 gcc/ChangeLog: * tree.h (struct tree_vec_map_cache_hasher): Move from... * tree.cc (struct tree_vec_map_cache_hasher): ...here. gcc/cp/ChangeLog: * pt.cc (defarg_inst): Use tree_vec_map_cache_hasher. (defarg_inst_for): New. (tsubst_default_argument): Adjust. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-defarg10.C: New test. --- gcc/tree.h | 17 ++++++++ gcc/cp/pt.cc | 43 +++++++++++++++---- .../g++.dg/cpp0x/lambda/lambda-defarg10.C | 21 +++++++++ gcc/tree.cc | 17 -------- 4 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C base-commit: 9bf217920457f2e2d46b601f24721780a20a031b prerequisite-patch-id: c7ab4056fcbd782232c8dc091597602ecd4a7a48 diff --git a/gcc/tree.h b/gcc/tree.h index 30bc53c2996..c5617fbcc61 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5559,6 +5559,23 @@ struct tree_decl_map_cache_hasher : ggc_cache_ptr_hash #define tree_vec_map_hash tree_decl_map_hash #define tree_vec_map_marked_p tree_map_base_marked_p +struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash +{ + static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); } + + static bool + equal (tree_vec_map *a, tree_vec_map *b) + { + return a->base.from == b->base.from; + } + + static int + keep_cache_entry (tree_vec_map *&m) + { + return ggc_marked_p (m->base.from); + } +}; + /* Hasher for tree decls. Pointer equality is enough here, but the DECL_UID is a better hash than the pointer value and gives a predictable traversal order. Additionally it can be used across PCH save/restore. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 6fbda676527..db99b988fc3 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -13666,7 +13666,36 @@ tsubst_aggr_type (tree t, } } -static GTY((cache)) decl_tree_cache_map *defarg_inst; +/* Map from a FUNCTION_DECL to a vec of default argument instantiations. */ + +static GTY((cache)) hash_table *defarg_inst; + +/* Return a reference to the slot for the defarg inst of PARM. */ + +tree & +defarg_inst_for (tree parm) +{ + if (!defarg_inst) + defarg_inst = hash_table::create_ggc (13); + tree fn = DECL_CONTEXT (parm); + tree_vec_map in = { fn, nullptr }; + tree_vec_map **slot + = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT); + if (!*slot) + { + *slot = ggc_alloc (); + **slot = in; + } + + /* Index in reverse order to avoid allocating space for initial parameters + that don't have default arguments. */ + unsigned ridx = list_length (parm); + + vec *&defs = (*slot)->to; + if (vec_safe_length (defs) < ridx) + vec_safe_grow_cleared (defs, ridx); + return (*defs)[ridx - 1]; +} /* Substitute into the default argument ARG (a default argument for FN), which has the indicated TYPE. */ @@ -13696,9 +13725,9 @@ tsubst_default_argument (tree fn, int parmnum, tree type, tree arg, gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype)); - tree *slot; - if (defarg_inst && (slot = defarg_inst->get (parm))) - return *slot; + tree &inst = defarg_inst_for (parm); + if (inst) + return inst; /* This default argument came from a template. Instantiate the default argument here, not in tsubst. In the case of @@ -13743,11 +13772,7 @@ tsubst_default_argument (tree fn, int parmnum, tree type, tree arg, pop_from_top_level (); if (arg != error_mark_node && !cp_unevaluated_operand) - { - if (!defarg_inst) - defarg_inst = decl_tree_cache_map::create_ggc (37); - defarg_inst->put (parm, arg); - } + inst = arg; return arg; } diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C new file mode 100644 index 00000000000..e08eb4f5e56 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C @@ -0,0 +1,21 @@ +// PR c++/103186 +// { dg-do compile { target c++11 } } + +struct f +{ + template + f(const T1&){} +}; + + +template class A { +public: + void foo(A a, const f& fn = [](){}) { } + void bar(A a) { foo(a); } +}; +int main() { + A a; + a.foo(a); + a.bar(a); + return 0; +} diff --git a/gcc/tree.cc b/gcc/tree.cc index c4b36619e6a..6bd3b14c93c 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -242,23 +242,6 @@ static GTY ((cache)) static GTY ((cache)) hash_table *value_expr_for_decl; -struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash -{ - static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); } - - static bool - equal (tree_vec_map *a, tree_vec_map *b) - { - return a->base.from == b->base.from; - } - - static int - keep_cache_entry (tree_vec_map *&m) - { - return ggc_marked_p (m->base.from); - } -}; - static GTY ((cache)) hash_table *debug_args_for_decl;