From patchwork Fri Dec 11 19:05:55 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 555884 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 7C54E140297 for ; Sat, 12 Dec 2015 06:06:10 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=htBgRXwh; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=mAbUm1RN+vUEOO/sR OieQ3ITEqcaYNLTyVt8HHCckFmtq6YlmZnXu377P+I4C8wU/NOqlRkUVvRlJUz3W D4SNm/YrWKZjcD8tUPwjLfqvT1Jfie7ugWmGn+omFtE6mmKiaJA7p0WDmZOfIk7c 9/1XMAnMsoi4s1S4BogQOxX+zQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=TWh0USS58vzJyQg2NywDPo0 TzbQ=; b=htBgRXwhq6bN1hHI1ExCBvOcEUqjEyg0lTXGjNxBRmkEmc8jo3gDrtw tvSAaDh/qkF14sDVmVlOguLGm3FCwrlDidxUU3JJDGzl/WbFgila/jntE6KutsEM HUoB5v0g8d0OML8BPaDSGP1M1tvLFIS++YMegYLm82gY8YUJ3T78= Received: (qmail 87955 invoked by alias); 11 Dec 2015 19:06:02 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 87293 invoked by uid 89); 11 Dec 2015 19:06:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 11 Dec 2015 19:06:01 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id BD03E347A73; Fri, 11 Dec 2015 19:05:59 +0000 (UTC) Received: from [10.10.116.46] (ovpn-116-46.rdu2.redhat.com [10.10.116.46]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tBBJ5ttK032635; Fri, 11 Dec 2015 14:05:58 -0500 Subject: Re: RFA (hash-*): PATCH for c++/68309 To: Richard Biener References: <5669F6AA.2000606@redhat.com> Cc: gcc-patches List , Jeffrey A Law , Richard Sandiford , Trevor Saunders From: Jason Merrill Message-ID: <566B1E93.3010802@redhat.com> Date: Fri, 11 Dec 2015 14:05:55 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: On 12/11/2015 05:10 AM, Richard Biener wrote: > On Thu, Dec 10, 2015 at 11:03 PM, Jason Merrill wrote: >> The C++ front end uses a temporary hash table to remember specializations of >> local variables during template instantiations. In a nested function such >> as a lambda or local class member function, we need to retain the elements >> from the enclosing function's local_specializations table; otherwise the >> testcase crashes because we don't find a local specialization for the >> non-captured use of 'args' in the decltype. >> >> This patch addresses that by making a copy of the enclosing >> local_specializations table if it exists; to enable that I've added copy >> constructors to hash_table and hash_map. >> >> Tested x86_64-pc-linux-gnu. OK for trunk? > > I don't think you can copy the elements with memcpy they may be C++ classes > that are not copyable. True. Fixed thus: > + for (size_t i = 0; i < size; ++i) > + { > + value_type &entry = h.m_entries[i]; > + if (is_deleted (entry)) > + mark_deleted (m_entries[i]); > + else if (!is_empty (entry)) > + m_entries[i] = entry; > + } > Also watch out for the bool gather_mem_stats = true > to bool gather_mem_stats = GATHER_STATISTICS change if that crosses your > change. OK. > I also think copying hash tables should be discouraged ;) I wonder if you > can get around the copying by adding a generation count (to easily "backtrack") > instead. I considered having a chain of tables to check, to handle generations, but I figured that the tables in question were small enough (only containing local variables for a single function) that a simple copy was reasonable. Jason commit ae8c23201f9a057516f081d4d9fde1fb3281153f Author: Jason Merrill Date: Thu Dec 10 15:37:50 2015 -0500 PR c++/68309 gcc/ * hash-table.h: Add copy constructor. * hash-map.h: Add copy constructor. gcc/cp/ * pt.c (instantiate_decl): Copy local_specializations for nested function. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 60cc94c..a45e6df 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -21725,8 +21725,13 @@ instantiate_decl (tree d, int defer_ok, template from within the body of another. */ saved_local_specializations = local_specializations; - /* Set up the list of local specializations. */ - local_specializations = new hash_map; + /* Set up the list of local specializations, copying the current + list if there is one. */ + if (local_specializations) + local_specializations + = new hash_map (*local_specializations); + else + local_specializations = new hash_map; /* Set up context. */ if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern) diff --git a/gcc/hash-map.h b/gcc/hash-map.h index b83708c..9b3d1e9 100644 --- a/gcc/hash-map.h +++ b/gcc/hash-map.h @@ -110,6 +110,11 @@ public: bool gather_mem_stats = true CXX_MEM_STAT_INFO) : m_table (n, ggc, gather_mem_stats, HASH_MAP_ORIGIN PASS_MEM_STAT) {} + hash_map (const hash_map &h, bool ggc = false, + bool gather_mem_stats = true CXX_MEM_STAT_INFO) + : m_table (h.m_table, ggc, gather_mem_stats, + HASH_MAP_ORIGIN PASS_MEM_STAT) {} + /* Create a hash_map in ggc memory. */ static hash_map *create_ggc (size_t size, bool gather_mem_stats = true CXX_MEM_STAT_INFO) diff --git a/gcc/hash-table.h b/gcc/hash-table.h index 192be30..e3f9dbd 100644 --- a/gcc/hash-table.h +++ b/gcc/hash-table.h @@ -364,6 +364,10 @@ public: explicit hash_table (size_t, bool ggc = false, bool gather_mem_stats = true, mem_alloc_origin origin = HASH_TABLE_ORIGIN CXX_MEM_STAT_INFO); + hash_table (const hash_table &, bool ggc = false, + bool gather_mem_stats = true, + mem_alloc_origin origin = HASH_TABLE_ORIGIN + CXX_MEM_STAT_INFO); ~hash_table (); /* Create a hash_table in gc memory. */ @@ -580,6 +584,34 @@ hash_table::hash_table (size_t size, bool ggc, bool } template class Allocator> +hash_table::hash_table (const hash_table &h, bool ggc, + bool gather_mem_stats, + mem_alloc_origin origin + MEM_STAT_DECL) : + m_n_elements (h.m_n_elements), m_n_deleted (h.m_n_deleted), + m_searches (0), m_collisions (0), m_ggc (ggc), + m_gather_mem_stats (gather_mem_stats) +{ + size_t size = h.m_size; + + if (m_gather_mem_stats) + hash_table_usage.register_descriptor (this, origin, ggc + FINAL_PASS_MEM_STAT); + + m_entries = alloc_entries (size PASS_MEM_STAT); + for (size_t i = 0; i < size; ++i) + { + value_type &entry = h.m_entries[i]; + if (is_deleted (entry)) + mark_deleted (m_entries[i]); + else if (!is_empty (entry)) + m_entries[i] = entry; + } + m_size = size; + m_size_prime_index = h.m_size_prime_index; +} + +template class Allocator> hash_table::~hash_table () { for (size_t i = m_size - 1; i < m_size; i--) diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-variadic3.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-variadic3.C new file mode 100644 index 0000000..76b6b3f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-variadic3.C @@ -0,0 +1,9 @@ +// PR c++/68309 +// { dg-do compile { target c++11 } } + +template void f(Ts...); +template T g(T); +template void print(Ts... args) { + [&] { f(g(args)...); }(); +} +int main() { print(5.2); }