From patchwork Fri Feb 9 15:51:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1897141 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=IigzfYMd; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4TWdd95d5wz23j0 for ; Sat, 10 Feb 2024 02:51:41 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EF4203858439 for ; Fri, 9 Feb 2024 15:51:38 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 24F0E3858C35 for ; Fri, 9 Feb 2024 15:51:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 24F0E3858C35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 24F0E3858C35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707493878; cv=none; b=EvOC84BKyIt7LWmAWi2BLCONQySKVCcOeNGiD2m+BnNbrbiqnJT6x9X6cXTcrdvk4copxSa5wYJAwbo4UHXFBRHDKcDxh2TMb87tLfet/w6anHHqQupaj/upBG/8qHK6ED+CxAPSmzWXf3NayatuJhAl7F7XhATbxbK3Fhx5O9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707493878; c=relaxed/simple; bh=024vT6mFFwOjiFd70Vt8I9R5Ugz3A4YAljkav8ghkuc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=o/gMpMtnJ45Fz/8PFiwAa869d+UTYv6e+1jAYZqG5qSd0XmurAM9uE1wIntFxhXKWwm5AUzFffjj03cVwg0Cpl/ohiu0pkkMShptjSXUU9pmqn+uCMclU5r+FzYos8JklIPT1GHoO9lFbo/nPtZqBTMWuSAVFngXMdHo1KI4amM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707493876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7bXM2rrYQ3f52F1JLu6odZbhhDzHtYFT6X+fo7hHbO0=; b=IigzfYMdscCZYBJykUlnZ6/SILDbTBUYDMhhvNae1/nHfzySPQrgRKh0Bm0Qp2oP4o0O8n AzShtXAhirVf7Ch4ETLNpiTjb0WvjXuEIBnyGrIJPJlU7sBfozVPBADjnGzrwbTsodNNN3 whoLzGxpMrwfG1sC7O5xsngpTgkv7yQ= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-631-V-5Xa0fxNEGf_Wg4v1i8rg-1; Fri, 09 Feb 2024 10:51:15 -0500 X-MC-Unique: V-5Xa0fxNEGf_Wg4v1i8rg-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6805f615543so18136456d6.1 for ; Fri, 09 Feb 2024 07:51:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707493874; x=1708098674; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7bXM2rrYQ3f52F1JLu6odZbhhDzHtYFT6X+fo7hHbO0=; b=Qq9GxorVE+kYiPPPWCFLH7YJeQvzsGjiEcimOn/jb2AprJTwnJBCgDc0otFNiVcqkA r4gacFeHhooM4nNLBVr1je2htRg/CodcvwjvhsCibYqXEWUHU6DN8Wlwt5FZiVPF8cOj +GXzq+cMAHHffT4/CGcL5wKhmt3vA1lcWTQratCjupWhmgGJ5UiA4GvttOD7RXkA2a63 Jnwti49pT+So9TXrIvdQDjIKt4TC7VuwUXLowtGvEkcPjxO38636/VyhBSZpeRZLxDGk 8iSX+BZFMW1sUs8MALcuYI7GLZHWF2lVsazxKPKTIex5Ftwfxgkmb6hvmhNl4NIfMBMI IxZg== X-Gm-Message-State: AOJu0YzU6x6uQ74YG9dy3GOt8jd2NgdnIJKRFg5SltSOvTzNvVONG6lR LjKQ+IhU66NPtnaGe2oAZmzbLvKXLGbM3X1Xob1Vro6wtElLY6/DzC5AzKLyr070Y2e/w8+oCsL l/Kcz4g6fuSXI4mNtF9HMgUMUxOcY3blD7PjPFv3pcn2XHTl5zKAxnEnKXB5fcTItzmQb28iD5t W6VQBqSkmzKEkbpDGQpOzFTzzGRdf2JRTp0L0e X-Received: by 2002:a05:6214:b6b:b0:68c:d964:5e05 with SMTP id ey11-20020a0562140b6b00b0068cd9645e05mr1105250qvb.5.1707493874028; Fri, 09 Feb 2024 07:51:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IE0oLKqP32so+ICkSKw/mfWq5voy84B6AsNl/StTKkokHqC4qlPq58GJkUkLeOzmlEtn5x+fg== X-Received: by 2002:a05:6214:b6b:b0:68c:d964:5e05 with SMTP id ey11-20020a0562140b6b00b0068cd9645e05mr1105229qvb.5.1707493873688; Fri, 09 Feb 2024 07:51:13 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWsa0xFICo3+5detGKvbA3Ws7KFSfo8t3x5TxVlBvnKzzwIwYlMSvsXvb3N5Nx3eBAl6t9AT7kJj9P0cbZLPvErIxWKG2n3qTB3nK5T8unEoeY= Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id mg17-20020a056214561100b0068cbda3396fsm924637qvb.68.2024.02.09.07.51.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 07:51:12 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, nathan@acm.org, Patrick Palka Subject: [PATCH] c++: avoid name lookup during mangling Date: Fri, 9 Feb 2024 10:51:10 -0500 Message-ID: <20240209155110.552199-1-ppalka@redhat.com> X-Mailer: git-send-email 2.43.0.561.g235986be82 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_BLACK 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, does this look reasonable? -- >8 -- It turns out that with modules we can call mangle_decl recursively, which is a problem because the global mangling state isn't recursion aware. The recursion happens from write_closure_type_name, which calls lambda function, which performs name lookup, which can trigger lazy loading, which can call maybe_clone_body for a newly loaded function, which can inspect DECL_ASSEMBLER_NAME, which enters mangling. This was observed when using fmtlib as a module with trunk, and leads to a bogus "mangling conflicts with a previous mangle error" followed by an ICE from cdtor_comdat_group due to a mangling mismatch. I considered making our mangling state recursion-aware, but it seems this lambda_function call is the only source of name lookup during mangling, and in turn the only source of potential recursion with modules so perhaps it's better to just address that. To that end, this patch adds a new field to LAMBDA_EXPR holding the op() of the closure type, so that lambda_function can just inspect this field rather than having to perform name lookup. With this patch fmtlib can be successfully imported as a module (with a few minor source tweaks). In passing this patch adds streaming of LAMBDA_EXPR_REGEN_INFO which I noticed is missing. gcc/cp/ChangeLog: * cp-tree.h (LAMBDA_EXPR_FUNCTION): Define. (tree_lambda_expr::function): New member. * lambda.cc (lambda_function): Use LAMBDA_EXPR_FUNCTION instead of performing name lookup. * module.cc (trees_out::core_vals): Stream LAMBDA_EXPR_REGEN_INFO and LAMBDA_EXPR_FUNCTION. (trees_in::core_vals): Likewise. * parser.cc (cp_parser_lambda_declarator_opt): Set LAMBDA_EXPR_FUNCTION. * pt.cc (tsubst_lambda_expr): Likewise. --- gcc/cp/cp-tree.h | 5 +++++ gcc/cp/lambda.cc | 20 +++++--------------- gcc/cp/module.cc | 4 ++++ gcc/cp/parser.cc | 1 + gcc/cp/pt.cc | 1 + 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 969c7239c97..3bef35fd90c 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -1543,6 +1543,10 @@ enum cp_lambda_default_capture_mode_type { #define LAMBDA_EXPR_CLOSURE(NODE) \ (TREE_TYPE (LAMBDA_EXPR_CHECK (NODE))) +/* The op() of the lambda closure type as would be found by name lookup. */ +#define LAMBDA_EXPR_FUNCTION(NODE) \ + (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->function) + struct GTY (()) tree_lambda_expr { struct tree_typed typed; @@ -1550,6 +1554,7 @@ struct GTY (()) tree_lambda_expr tree this_capture; tree extra_scope; tree regen_info; + tree function; vec *pending_proxies; location_t locus; enum cp_lambda_default_capture_mode_type default_capture_mode : 2; diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index 1d37e5a52b9..c3ec9381c19 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -165,22 +165,12 @@ begin_lambda_type (tree lambda) tree lambda_function (tree lambda) { - tree type; - if (TREE_CODE (lambda) == LAMBDA_EXPR) - type = LAMBDA_EXPR_CLOSURE (lambda); - else - type = lambda; - gcc_assert (LAMBDA_TYPE_P (type)); - /* Don't let debug_tree cause instantiation. */ - if (CLASSTYPE_TEMPLATE_INSTANTIATION (type) - && !COMPLETE_OR_OPEN_TYPE_P (type)) + if (CLASS_TYPE_P (lambda)) + lambda = CLASSTYPE_LAMBDA_EXPR (lambda); + tree callop = LAMBDA_EXPR_FUNCTION (lambda); + if (!callop) return NULL_TREE; - lambda = lookup_member (type, call_op_identifier, - /*protect=*/0, /*want_type=*/false, - tf_warning_or_error); - if (lambda) - lambda = STRIP_TEMPLATE (get_first_fn (lambda)); - return lambda; + return STRIP_TEMPLATE (callop); } /* True if EXPR is an expression whose type can be used directly in lambda diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 3c2fef0e3f4..7ae965d38e4 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -6317,6 +6317,8 @@ trees_out::core_vals (tree t) WT (((lang_tree_node *)t)->lambda_expression.capture_list); WT (((lang_tree_node *)t)->lambda_expression.this_capture); WT (((lang_tree_node *)t)->lambda_expression.extra_scope); + WT (((lang_tree_node *)t)->lambda_expression.regen_info); + WT (((lang_tree_node *)t)->lambda_expression.function); /* pending_proxies is a parse-time thing. */ gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies); if (state) @@ -6818,6 +6820,8 @@ trees_in::core_vals (tree t) RT (((lang_tree_node *)t)->lambda_expression.capture_list); RT (((lang_tree_node *)t)->lambda_expression.this_capture); RT (((lang_tree_node *)t)->lambda_expression.extra_scope); + RT (((lang_tree_node *)t)->lambda_expression.regen_info); + RT (((lang_tree_node *)t)->lambda_expression.function); /* lambda_expression.pending_proxies is NULL */ ((lang_tree_node *)t)->lambda_expression.locus = state->read_location (*this); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index c4292c49ce2..3fe975cc213 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -12111,6 +12111,7 @@ cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr) else if (parser->fully_implicit_function_template_p) fco = finish_fully_implicit_template (parser, fco); + LAMBDA_EXPR_FUNCTION (lambda_expr) = fco; finish_member_declaration (fco); record_lambda_scope_sig_discriminator (lambda_expr, fco); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index f9abb21a9a2..5743b105af3 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -19719,6 +19719,7 @@ tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) r = error_mark_node; goto out; } + LAMBDA_EXPR_FUNCTION (r) = inst; finish_member_declaration (inst); record_lambda_scope_sig_discriminator (r, inst);