From patchwork Thu Oct 6 16:19:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1687076 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.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=m3ETSwWU; dkim-atps=neutral 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MjxVF5sR2z1yqk for ; Fri, 7 Oct 2022 03:19:49 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 894A53858036 for ; Thu, 6 Oct 2022 16:19:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 894A53858036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665073186; bh=S3KIMucoQgJdjR+THs8E/y+bS5dGbnUjLiap08LwUTU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=m3ETSwWUSW2AXF9rMGwUKHnhAUtu4espBwE7W3oz72dB/jSBGzfGxYzC40NvK0zn9 qjDFrFsetmmzbK/Z7+CWBHFQ70fA/ZwnhgbwJhJYeyfI7XD9bCNz4aYf7uXaFsdAEg dLylnzYUgtykEy0r0qJa0lTaWXpuH32d1jAT8oZE= 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 3A1293858D32 for ; Thu, 6 Oct 2022 16:19:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3A1293858D32 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-235-TvgtipOEPJy5V8REzYzsRA-1; Thu, 06 Oct 2022 12:19:21 -0400 X-MC-Unique: TvgtipOEPJy5V8REzYzsRA-1 Received: by mail-qt1-f200.google.com with SMTP id y5-20020ac85245000000b003925165520fso1390614qtn.22 for ; Thu, 06 Oct 2022 09:19:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=S3KIMucoQgJdjR+THs8E/y+bS5dGbnUjLiap08LwUTU=; b=fKzCHcDmPFWZLYl8FpqiQ3bVR7QKr6r8Ys/d6inugmUaGhv5vbrTnmt2WY0OIWGi1D 7+29U/W6V8PVNcVAQvKTb13Ut+bfCCQMfQzdYmfuztJifhctvyzeIEYhO0VuRafejG86 gJNuV825ZUcip8O3+jBda/XV9Ef95n0DJpf89FnJ6zWYPB7w2vNY54JKObiYxwbpwQjo jR4kUrnPunT0N8XHoYTEcvxKQ2jC/YqvddsnoNdmqnBlJW/mB6Dcs3klH4nKPYbb3Vxf Ox1Sst4Em6QvMhiusTSxluvnbuPyl8ryv1daIvBTnjR5pbyjreKApJaTZ0M4619YQmnY Fl6Q== X-Gm-Message-State: ACrzQf1q3xcK3nqvf8/ILYyLJvL5v0NqB4UALBNDX2Oo2I69ix0XdEbu 3BhvCpKA5uUUyhvfEHAwFG7LQHupU8di5ChMr3Zu7859S+2sOvXDXP6YSqCimoBSYtKc8v3r7Lf TPvRtemABZ/IUei2A2CmxoR+PqqR3kS3ezVa+enUD6Lzyjd02YSYrhxMAbIqfHDx45l0= X-Received: by 2002:ac8:5b15:0:b0:394:deb7:1d43 with SMTP id m21-20020ac85b15000000b00394deb71d43mr738798qtw.681.1665073159255; Thu, 06 Oct 2022 09:19:19 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6qPeg3U7qJxHWHm6KB7QFT1tR4oWnagtxGrFEMakhnl1XAMo53oc+Qojz7YojWCNHOKy5YNA== X-Received: by 2002:ac8:5b15:0:b0:394:deb7:1d43 with SMTP id m21-20020ac85b15000000b00394deb71d43mr738766qtw.681.1665073158952; Thu, 06 Oct 2022 09:19:18 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id z6-20020ac87ca6000000b003913996dce3sm18098qtv.6.2022.10.06.09.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 09:19:18 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++ modules: static var in inline function [PR104433] Date: Thu, 6 Oct 2022 12:19:16 -0400 Message-Id: <20221006161916.4118820-1-ppalka@redhat.com> X-Mailer: git-send-email 2.38.0.rc2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 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, SPF_HELO_NONE, SPF_NONE, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Cc: nathan@acm.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The below testcase fails to link with the error undefined reference to `f()::y' ultimately because during stream out for the static VAR_DECL y we override DECL_EXTERNAL to true, which later during IPA confuses symbol_table::remove_unreachable_nodes into thinking it's safe to not emit the symbol. The streaming code in question already avoids overriding DECL_EXTERNAL here for DECL_VAR_DECLARED_INLINE_P vars, so it seems natural to do the same for static vars from an DECL_DECLARED_INLINE_P function scope. After this patch (and r13-3134-g09df0d8b14dda6), the following now links: import ; int main() { std::make_shared(); } Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/104433 gcc/cp/ChangeLog: * module.cc (trees_out::core_bools): Don't override DECL_EXTERNAL to true for static variables from an inline function. gcc/testsuite/ChangeLog: * g++.dg/modules/static-2_a.H: New test. * g++.dg/modules/static-2_b.C: New test. --- gcc/cp/module.cc | 3 +++ gcc/testsuite/g++.dg/modules/static-2_a.H | 8 ++++++++ gcc/testsuite/g++.dg/modules/static-2_b.C | 9 +++++++++ 3 files changed, 20 insertions(+) create mode 100644 gcc/testsuite/g++.dg/modules/static-2_a.H create mode 100644 gcc/testsuite/g++.dg/modules/static-2_b.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 79cbb346ffa..11f68794cd2 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -5397,6 +5397,9 @@ trees_out::core_bools (tree t) case VAR_DECL: if (TREE_PUBLIC (t) + && !(TREE_STATIC (t) + && DECL_FUNCTION_SCOPE_P (t) + && DECL_DECLARED_INLINE_P (DECL_CONTEXT (t))) && !DECL_VAR_DECLARED_INLINE_P (t)) is_external = true; break; diff --git a/gcc/testsuite/g++.dg/modules/static-2_a.H b/gcc/testsuite/g++.dg/modules/static-2_a.H new file mode 100644 index 00000000000..b4546932a12 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/static-2_a.H @@ -0,0 +1,8 @@ +// PR c++/104433 +// { dg-additional-options -fmodule-header } +// { dg-module-cmi {} } + +inline int* f() { + static int y = 0; + return &y; +} diff --git a/gcc/testsuite/g++.dg/modules/static-2_b.C b/gcc/testsuite/g++.dg/modules/static-2_b.C new file mode 100644 index 00000000000..bfd35b0fc15 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/static-2_b.C @@ -0,0 +1,9 @@ +// PR c++/104433 +// { dg-additional-options -fmodules-ts } +// { dg-do link } + +import "static-2_a.H"; + +int main() { + f(); +}