From patchwork Tue May 16 19:34:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 94871 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp657811vqo; Tue, 16 May 2023 12:35:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7fjdZkYWGzLoGuVkBw0gQA91b51panaRerECkyezeOxcp4bAnoo2JukDyBWq/5mgLL0MS2 X-Received: by 2002:a17:907:869f:b0:96a:f688:db80 with SMTP id qa31-20020a170907869f00b0096af688db80mr10606323ejc.67.1684265739654; Tue, 16 May 2023 12:35:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684265739; cv=none; d=google.com; s=arc-20160816; b=sRTYuCOxN/E5XBQJ14+XxtUCLvV9ksFgQYxm3R/BXS8JyeM/68nbpilGVYOTiou3JG BkBexehmAGkQMCo3dbUXxm/CqIuaywAbVtOL782jwkiJwT1ddcihs721uZU8wdOrX1CG pO6j6ZBSoHVRn8XfYQQVVJMMp7V/KJa0ZOHwTWdtNG9B+ntK3ou5522stxEJx9hNjdkR 4xsEADR5jcICzXamZKEUbIz2qBIJ/gbefZmshgckGxySfRkwTfTLqK4ttUmfcy95TxC2 pxSuKsBq8mXX6G30cieluQHsKCtXMPg3XmEqUBV7s0Z7hLCLRPBplAApfwQ511/zi+mZ Niug== 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:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=hpqTxl8XNyreknANLKaYwl6lDTbDYgqtRQCesPgtJGs=; b=OLK+4aBL+p9J8CKrWBtD2ZfeItwjwRetZlMGt9ojqO/SXwv/p5gP/GWv+sAD+3RN1l 8x3tv1eLpl1B0EkgxRZYKvpcLU/WNr/UdKGcQumjwHp9GcbDYM3D05Df0YLQs0qM4Lvd s8B6kcwu9anMcfKRYmbuSDfLhLkb2SUaUwE4f4cTtvC4O8/Dad144pg390owvtZdMFLX WPREQIqh3H/2XNP4T8r+sfGUvM7kz+F5pF7rppdXy7VjYJa09wULBr3ggGupJJ1P1GOC mdJaeb0pWIME0F0KPmGAB5undN7vtbTj0b+7pOo7dCLbALO/2n5OoLDF1fId4rECI7yU Jx5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="f22MFcx/"; 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pv27-20020a170907209b00b0094ed24294dfsi15900428ejb.324.2023.05.16.12.35.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 12:35:39 -0700 (PDT) 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="f22MFcx/"; 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 A5544385783F for ; Tue, 16 May 2023 19:35:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A5544385783F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684265732; bh=hpqTxl8XNyreknANLKaYwl6lDTbDYgqtRQCesPgtJGs=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=f22MFcx/Tm8K3n7bUVS4XQ5T4Ri1i2PQhIcRLzgp/zLV+qOfV9aXkCbghERVuY4V2 zaLT0hb/i/br+LxkB8X4s3ByNEn5fIvkIEEZBpCzxAb9pBQfTRJRY5Ccnn8j7xlZPt nuJBLfBYhDILZNihFA1QrC1qPSS4bNWsZ43VftFU= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 6539F3858C83 for ; Tue, 16 May 2023 19:34:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6539F3858C83 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-654-xRI4A5rsPuqYav0b_dIg8A-1; Tue, 16 May 2023 15:34:46 -0400 X-MC-Unique: xRI4A5rsPuqYav0b_dIg8A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 57C6D185A7A2 for ; Tue, 16 May 2023 19:34:46 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1AC801121314; Tue, 16 May 2023 19:34:45 +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 34GJYh1O2453580 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 16 May 2023 21:34:44 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 34GJYh2I2453579; Tue, 16 May 2023 21:34:43 +0200 Date: Tue, 16 May 2023 21:34:42 +0200 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Don't try to initialize zero width bitfields in zero initialization [PR109868] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.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?1766080631879504165?= X-GMAIL-MSGID: =?utf-8?q?1766080631879504165?= Hi! My GCC 12 change to avoid removing zero-sized bitfields as they are important for ABI and are needed for layout compatibility traits apparently causes zero sized bitfields to be initialized in the IL, which at least in 13+ results in ICEs in the ranger which is upset about zero precision types. I think we could even avoid initializing other unnamed bitfields, but unfortunately !CONSTRUCTOR_NO_CLEARING doesn't mean in the middle-end clearing of padding bits and until we have some new flag that represents the request to clear padding bits, I think it is better to keep zeroing non-zero sized unnamed bitfields. In addition to skipping those fields, I have changed the logic how UNION_TYPEs are handled, the current code was a little bit weird in that e.g. if first non-static data member had error_mark_node type, we'd happily zero initialize the second non-static data member, etc. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/13, perhaps even 12? 2023-05-16 Jakub Jelinek PR c++/109868 * init.cc (build_zero_init_1): Don't initialize zero-width bitfields. For unions only initialize the first FIELD_DECL. * g++.dg/init/pr109868.C: New test. Jakub --- gcc/cp/init.cc.jj 2023-05-01 23:07:05.147417750 +0200 +++ gcc/cp/init.cc 2023-05-16 10:01:14.512489727 +0200 @@ -189,15 +189,21 @@ build_zero_init_1 (tree type, tree nelts init = build_zero_cst (type); else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type))) { - tree field; + tree field, next; vec *v = NULL; /* Iterate over the fields, building initializations. */ - for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) + for (field = TYPE_FIELDS (type); field; field = next) { + next = DECL_CHAIN (field); + if (TREE_CODE (field) != FIELD_DECL) continue; + /* For unions, only the first field is initialized. */ + if (TREE_CODE (type) == UNION_TYPE) + next = NULL_TREE; + if (TREE_TYPE (field) == error_mark_node) continue; @@ -212,6 +218,11 @@ build_zero_init_1 (tree type, tree nelts continue; } + /* Don't add zero width bitfields. */ + if (DECL_C_BIT_FIELD (field) + && integer_zerop (DECL_SIZE (field))) + continue; + /* Note that for class types there will be FIELD_DECLs corresponding to base classes as well. Thus, iterating over TYPE_FIELDs will result in correct initialization of @@ -230,10 +241,6 @@ build_zero_init_1 (tree type, tree nelts if (value) CONSTRUCTOR_APPEND_ELT(v, field, value); } - - /* For unions, only the first field is initialized. */ - if (TREE_CODE (type) == UNION_TYPE) - break; } /* Build a constructor to contain the initializations. */ --- gcc/testsuite/g++.dg/init/pr109868.C.jj 2023-05-16 09:43:54.706278293 +0200 +++ gcc/testsuite/g++.dg/init/pr109868.C 2023-05-16 09:44:16.581966894 +0200 @@ -0,0 +1,13 @@ +// PR c++/109868 +// { dg-do compile } +// { dg-options "-O2" } + +struct A { virtual void foo (); }; +struct B { long b; int : 0; }; +struct C : A { B c; }; + +void +bar (C *p) +{ + *p = C (); +}