From patchwork Wed Nov 16 10:35:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 20893 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp67195wru; Wed, 16 Nov 2022 02:36:39 -0800 (PST) X-Google-Smtp-Source: AA0mqf548ro8DdImTdkseenSoE8+2J6qsughtB//wJg08q0CWa4vttGNvP073DVatpvpc+jYuRbi X-Received: by 2002:a05:6402:518:b0:467:8813:cab5 with SMTP id m24-20020a056402051800b004678813cab5mr15383163edv.369.1668594999773; Wed, 16 Nov 2022 02:36:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668594999; cv=none; d=google.com; s=arc-20160816; b=jE7GRh96C5BJNFNR8hcSVbbdmFx3+aRzBzf6TJXCcfIOm8OqoD7AMpLXnEsp4AJWef BSbDNB4SeSbl8GnaZlp6BXPTzGcO51PkIdbXuNVFde8GXLRSJWWx27al2ohIYwwvjngY srS4XOvBN5894VY4JHkgdw2gNJaccGmxF6lmCjiv9TBkdmOnuLr4nmb4ta8k5fb/JTvP CwiqTV3VU8Xpwubws/tM3vTmczB83Kcw94KoSa2D0BBRo4S5GNw5EZh2lBiWWFnEqU9m eJx9dskxFpU9eg57vU4Un3pCZ1yeORl2eKU50Aw6FT2E6q5H80Khq+hP0yER+egwMWgq 4ijQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=78P/XZicl5y1UZSEVqFWAVpM4Q2SlUFOiFsrsx6fdio=; b=m24G0V1F3Cx/fNKf1ad/jhB2/762ADzU1VrD6EH99pdkppqO6kvwVri6C51SoRbmgN 6V11Tt7cqkOiTlQRuc37RwlMmR3Q0lTWkcSaJi6yQicyZdnbqZU0u6FDgtP5XQcuBpqt yET6ZzsOzhi+wGrGEh4inTM4W7IIUyRPzjfhJiIaPDPqtDRMP/7+afGhrFGO+zT0vo8g bqxVbYxkEmwscaajFwOlo+dAZ8BSea8qh2Gb3vh+8IUS1KHUE0Y1KOC3vpj6ryLaTiR9 HEyGZnEXELTMl7KnjBPSwaBTAlVDGEtdemDHhcteu3kbkp8lAeUv7JkPPukF5H78RtSH JjrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=XABdbiCj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gs34-20020a1709072d2200b0078d930212c0si14467881ejc.347.2022.11.16.02.36.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Nov 2022 02:36:39 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=XABdbiCj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C24503959C8B for ; Wed, 16 Nov 2022 10:36:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C24503959C8B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668594998; bh=78P/XZicl5y1UZSEVqFWAVpM4Q2SlUFOiFsrsx6fdio=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=XABdbiCjybRzCZwEMlAzfeS/86LQGSn4NxFcN3YJcYVJdJl09GGu8oFXSC2WL+2ut 0hZXJkaamRkDOxey1jynps0LOu/OYyClyBdmy0DFnIRhPWTCLdVl7QwGEV61W82FHh yO8kk/2Iy1P2eydyHk7/PIK+OEMvWHUCnYI1ABRE= 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 1636A3959C62 for ; Wed, 16 Nov 2022 10:35:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1636A3959C62 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-115-n-p-Hzr5MJ-qYhHeYqoSVw-1; Wed, 16 Nov 2022 05:35:53 -0500 X-MC-Unique: n-p-Hzr5MJ-qYhHeYqoSVw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3C1E01C09C82 for ; Wed, 16 Nov 2022 10:35:53 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EDB102024CC8; Wed, 16 Nov 2022 10:35:52 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2AGAZmSe2461725 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 16 Nov 2022 11:35:48 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AGAZkiW2461724; Wed, 16 Nov 2022 11:35:46 +0100 Date: Wed, 16 Nov 2022 11:35:46 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v2: Alignment changes to layout compatibility/common initial sequence - DR2583 Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749648670437486349?= X-GMAIL-MSGID: =?utf-8?q?1749648670437486349?= On Tue, Nov 15, 2022 at 05:57:26PM -0500, Jason Merrill wrote: > > So, my understanding of this is we shouldn't check TYPE_ALIGN in > > layout_compatible_type_p, but instead DECL_ALIGN in > > next_common_initial_seqence. > > Agreed. > > > +#if __cpp_lib_is_layout_compatible >= 201907L > > +static_assert (std::is_corresponding_member (&A::i, &B::i), ""); > > +static_assert (alignof (char) == 8 || !std::is_corresponding_member (&A::c, &B::c), ""); > > Maybe > > std_is_corresponding_member (&A::c, &B::c) == (alignof (char) == 8) > > ? > > Could also use an alignas(alignof(type)) case to verify that it matches a > member with no alignas. So like this? The previous patch successfully bootstrapped/regtested on x86_64-linux and i686-linux, only the new testcase changed and that one passes make check, ok for trunk? 2022-11-16 Jakub Jelinek * typeck.cc (next_common_initial_sequence): Return false members have different DECL_ALIGN. (layout_compatible_type_p): Don't test TYPE_ALIGN of ENUMERAL_TYPE or CLASS_TYPE_P. * g++.dg/cpp2a/is-layout-compatible3.C: Expect enums with different alignas to be layout compatible, while classes with different alignas on members layout incompatible. * g++.dg/DRs/dr2583.C: New test. Jakub --- gcc/cp/typeck.cc.jj 2022-11-13 04:53:46.010682269 -1200 +++ gcc/cp/typeck.cc 2022-11-13 23:14:41.355180354 -1200 @@ -1833,6 +1833,8 @@ next_common_initial_sequence (tree &memb1 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1))) != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2))) return false; + if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2)) + return false; if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2))) return false; return true; @@ -1854,15 +1856,13 @@ layout_compatible_type_p (tree type1, tr type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); if (TREE_CODE (type1) == ENUMERAL_TYPE) - return (TYPE_ALIGN (type1) == TYPE_ALIGN (type2) - && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)) + return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)) && same_type_p (finish_underlying_type (type1), finish_underlying_type (type2))); if (CLASS_TYPE_P (type1) && std_layout_type_p (type1) && std_layout_type_p (type2) - && TYPE_ALIGN (type1) == TYPE_ALIGN (type2) && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))) { tree field1 = TYPE_FIELDS (type1); --- gcc/testsuite/g++.dg/cpp2a/is-layout-compatible3.C.jj 2021-08-18 21:42:27.414421719 -1200 +++ gcc/testsuite/g++.dg/cpp2a/is-layout-compatible3.C 2022-11-13 23:20:05.008776825 -1200 @@ -55,10 +55,10 @@ static_assert (!std::is_layout_compatibl static_assert (!std::is_layout_compatible_v); static_assert (!std::is_layout_compatible_v); static_assert (!std::is_layout_compatible_v); -static_assert (!std::is_layout_compatible_v); +static_assert (std::is_layout_compatible_v); static_assert (!std::is_layout_compatible_v); static_assert (!std::is_layout_compatible_v); static_assert (!std::is_layout_compatible_v); -static_assert (std::is_layout_compatible_v); +static_assert (!std::is_layout_compatible_v); static_assert (std::is_layout_compatible_v); static_assert (std::is_layout_compatible_v); --- gcc/testsuite/g++.dg/DRs/dr2583.C.jj 2022-11-16 11:28:03.200831332 +0100 +++ gcc/testsuite/g++.dg/DRs/dr2583.C 2022-11-16 11:31:34.421978999 +0100 @@ -0,0 +1,45 @@ +// DR 2583 - Common initial sequence should consider over-alignment. +// { dg-do compile { target c++11 } } + +#include + +struct A { + int i; + char c; +}; + +struct B { + int i; + alignas(8) char c; +}; + +struct C { + int i; + alignas(alignof(char)) char c; +}; + +struct D { + alignas(alignof(int)) int i; + char c; +}; + +struct S0 { + alignas(16) char x[128]; + int i; +}; + +struct alignas(16) S1 { + char x[128]; + int i; +}; + +#if __cpp_lib_is_layout_compatible >= 201907L +static_assert (std::is_corresponding_member (&A::i, &B::i), ""); +static_assert (std::is_corresponding_member (&A::c, &B::c) == (alignof (char) == 8), ""); +static_assert (std::is_corresponding_member (&A::i, &C::i), ""); +static_assert (std::is_corresponding_member (&A::c, &C::c), ""); +static_assert (std::is_corresponding_member (&A::i, &D::i), ""); +static_assert (std::is_corresponding_member (&A::c, &D::c), ""); +static_assert (std::is_corresponding_member (&S0::x, &S1::x) == (alignof (char) == 16), ""); +static_assert (std::is_corresponding_member (&S0::i, &S1::i) == (alignof (char) == 16), ""); +#endif