From patchwork Fri Aug 16 06:55:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1973060 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Zm0bZZtJ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WlXp01qZ8z1yYl for ; Fri, 16 Aug 2024 16:56:06 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1EEED3857022 for ; Fri, 16 Aug 2024 06:56:04 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by sourceware.org (Postfix) with ESMTPS id AA57A3858C3A for ; Fri, 16 Aug 2024 06:55:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA57A3858C3A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AA57A3858C3A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::631 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723791339; cv=none; b=P5oMBjjZ7l9rkXVyXfuxz94fNV7jkft7dEPh1BUKzeqZzGAMpbVFvey5vTqJ8fMd6nWkgoZyctTqJwdNbGzZjuOTj45O1Ab6RONoLqCvOQZlq6ZCmSRg8tAZA5Qgfrte8q3ivnKazNHV0PJcI/LXneOkkGn2CpSErcYxi/4sz3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723791339; c=relaxed/simple; bh=3Dt1xkxAmblnOwN9YfjXD4hT9rAXf/ZwRXWae4h6v8s=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=BWh9A7LuczTf3rHWLm6cNdvPKaVYK4kKQMAHo3RRuuw7/2PgZQNiq8QcE+rKgr2jgB0XhY3GbEuVTSPpbX1al7HqbAzSSjhGlETmOX0VbqT25UfL4WW+3IAonREe1yLdVFRy3RJkyohF4BeuQmgbG6vQ7YMUEc+yr1rsSRaMI1U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-2020e83eca1so1449225ad.2 for ; Thu, 15 Aug 2024 23:55:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723791336; x=1724396136; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=EP1xNUtKmZH1XXT987HUeL5Z8YeWTXI9ofISdkHBqo8=; b=Zm0bZZtJkyTNTmNWPrcdXhi3puVXy9IKSJeHtBtrzCojZEo5jbyMPr7XQ31IP+MHne 5XXZJNd6+LQ/mDP67M3JzB5PnQhsOD8iJvgCQjTZlzzhELWbx2VMDFd6FwrhinrUEcub cv6DfLdUdrzPBFD/wOdG+uOa3qvLsNJXdwGEOqIft/EKXzM5wGQqKvfiFZSwa7NprjH6 VN/ndq2lpY1npAhesMY+xwOtaP7ACbMCCo00NxNgo2keItXPjS1a8bi++8GgEHk4BM26 0ZQAeReYKJ0+LNYKEPe1gSlyIP+iyWtaXPg/Xrx4Ur52VvL6ANcgB7XHJ/axl1jv5oig HfCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723791336; x=1724396136; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EP1xNUtKmZH1XXT987HUeL5Z8YeWTXI9ofISdkHBqo8=; b=p1T5hu7CG6AFuNIbFu+CTHT0lMxwckXrePCPiAsDjnX1XJ/pItc2R8KLqPqN37kj7Y ueI0mi2HakmNEbXzFSb6FFQoul/5m1HE03+iUNbloPCBPnVKpkWUVWya/KmY8em3ea6h fes8egK66TbCCyp4OlzRE/RiGTxlBVXekcFI7dWxIjA8vlYMryEgvobEKUsROGTDO5ia x82YRA52dLszDY+RVScSDuzyXG7YMKm8SdHsVEqeQE1E6Ih4tV6TXetUJYfbf9VQ+rg7 JE4BYhhfA8poAbYyn5jTSXWeZ06foDsJLOK0fYQEzh12xPntn99B8H+LGT7Rcm6Bcxly nLPg== X-Gm-Message-State: AOJu0YxFaXmem1KSr1shU1feq0f0fMMfG0+FDJdl1p5wg8IzRkReWI3S a2HsbDI/VZ5VobX03Vei4Hcb2+cGKEKgL0WzIQPaRtvGMcvFwDQ39aPOhw== X-Google-Smtp-Source: AGHT+IFXCLpk3cLetiAUbdOYc5mkm4FZcD5d3StfA1uP+sU+4elxDGXIU0McpGATJn/B6rEj7QBKrA== X-Received: by 2002:a17:903:41c7:b0:201:e7c2:bcf9 with SMTP id d9443c01a7336-20203ee746dmr24932295ad.39.1723791336366; Thu, 15 Aug 2024 23:55:36 -0700 (PDT) Received: from Thaum. (110-174-164-242.static.tpgi.com.au. [110.174.164.242]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f038b3ddsm19647575ad.205.2024.08.15.23.55.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2024 23:55:35 -0700 (PDT) Message-ID: <66bef7e7.170a0220.bd16d.84ab@mx.google.com> X-Google-Original-Message-ID: Date: Fri, 16 Aug 2024 16:55:30 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++/modules: Avoid rechecking initializers when streaming NTTPs [PR116382] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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.30 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 Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? -- >8 -- When reading an NTTP we call get_template_parm_object which delegates setting of DECL_INITIAL to the general cp_finish_decl procedure, which calls check_initializer to validate and record it. Apart from being unnecessary (it must have already been validated by the writing module), this also causes errors in cases like the linked PR, as validating may end up needing to call lazy_load_pendings to determine any specialisations that may exist which violates assumptions of the modules streaming code. This patch works around the issue by adding a flag to get_template_parm_object to disable these checks when not needed. PR c++/116382 gcc/cp/ChangeLog: * cp-tree.h (get_template_parm_object): Add check_init param. * module.cc (trees_in::tree_node): Pass check_init=false when building NTTPs. * pt.cc (get_template_parm_object): Prevent cp_finish_decl from validating the initializer when check_init=false. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-nttp-1_a.C: New test. * g++.dg/modules/tpl-nttp-1_b.C: New test. Signed-off-by: Nathaniel Shead Signed-off-by: Nathaniel Shead Reviewed-by: Jason Merrill --- gcc/cp/cp-tree.h | 3 ++- gcc/cp/module.cc | 6 +++++- gcc/cp/pt.cc | 13 +++++++++++-- gcc/testsuite/g++.dg/modules/tpl-nttp-1_a.C | 8 ++++++++ gcc/testsuite/g++.dg/modules/tpl-nttp-1_b.C | 6 ++++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/tpl-nttp-1_a.C create mode 100644 gcc/testsuite/g++.dg/modules/tpl-nttp-1_b.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 039c70710a2..a9ce44bb214 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7626,7 +7626,8 @@ enum { nt_opaque = false, nt_transparent = true }; extern tree alias_template_specialization_p (const_tree, bool); extern tree dependent_alias_template_spec_p (const_tree, bool); extern bool dependent_opaque_alias_p (const_tree); -extern tree get_template_parm_object (tree expr, tree mangle); +extern tree get_template_parm_object (tree expr, tree mangle, + bool check_init = true); extern tree tparm_object_argument (tree); extern bool explicit_class_specialization_p (tree); extern bool push_tinst_level (tree); diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index c3218bd5caf..0a4ceffa3d6 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -9938,7 +9938,11 @@ trees_in::tree_node (bool is_use) tree name = tree_node (); if (!get_overrun ()) { - res = get_template_parm_object (init, name); + /* We don't want to check the initializer as that may require + name lookup, which could recursively start lazy loading. + Instead we know that INIT is already valid so we can just + apply that directly. */ + res = get_template_parm_object (init, name, /*check_init=*/false); int tag = insert (res); dump (dumper::TREE) && dump ("Created nttp object:%d %N", tag, name); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 1c531f456be..a5d52906389 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -7361,10 +7361,11 @@ create_template_parm_object (tree expr, tsubst_flags_t complain) static GTY(()) hash_map *tparm_obj_values; /* Find or build an nttp object for (already-validated) EXPR with name - NAME. */ + NAME. When CHECK_INIT is false we don't need to process the initialiser, + it's already been done. */ tree -get_template_parm_object (tree expr, tree name) +get_template_parm_object (tree expr, tree name, bool check_init/*=true*/) { tree decl = get_global_binding (name); if (decl) @@ -7390,6 +7391,14 @@ get_template_parm_object (tree expr, tree name) hash_map_safe_put (tparm_obj_values, decl, copy); } + if (!check_init) + { + /* The expr is the already processed initializer, set it on the NTTP + object now so that cp_finish_decl doesn't do it again later. */ + DECL_INITIAL (decl) = expr; + DECL_INITIALIZED_P (decl) = 1; + } + pushdecl_top_level_and_finish (decl, expr); return decl; diff --git a/gcc/testsuite/g++.dg/modules/tpl-nttp-1_a.C b/gcc/testsuite/g++.dg/modules/tpl-nttp-1_a.C new file mode 100644 index 00000000000..46f6ecd0c1d --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/tpl-nttp-1_a.C @@ -0,0 +1,8 @@ +// PR c++/116382 +// { dg-additional-options "-fmodules-ts -std=c++20" } +// { dg-module-cmi m:a } + +module m:a; +template struct X {}; +template nttp> struct index {}; +template struct index<{}>; diff --git a/gcc/testsuite/g++.dg/modules/tpl-nttp-1_b.C b/gcc/testsuite/g++.dg/modules/tpl-nttp-1_b.C new file mode 100644 index 00000000000..a3241e7bc25 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/tpl-nttp-1_b.C @@ -0,0 +1,6 @@ +// PR c++/116382 +// { dg-additional-options "-fmodules-ts -std=c++20" } +// { dg-module-cmi m } + +export module m; +import :a;