From patchwork Thu Mar 9 19:40:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 67082 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp480864wrd; Thu, 9 Mar 2023 11:41:48 -0800 (PST) X-Google-Smtp-Source: AK7set/QvJYhSOoZcXGT96P+lnyIBNywxT//yIf4OffvfF0alkL23PzkLtqvuUJOu3o2YhjXPlRw X-Received: by 2002:a17:907:1b24:b0:8b1:7a86:b06f with SMTP id mp36-20020a1709071b2400b008b17a86b06fmr28362543ejc.63.1678390908779; Thu, 09 Mar 2023 11:41:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678390908; cv=none; d=google.com; s=arc-20160816; b=Asan4Ccg1R9QXEt/t1UsIfZ8UQVdQaFmIqpCo5wHYdrXmjIkJ1nhLbHsyphYPw0mOE kB5OC2DcYNaFvB0S8nTwIR3oPR7GnWxxLTPRNXCUHU8Z0EhCKXvhGrCEn/vQXVAwOVxc P7aUiH64Is4e1BmYErAM/NqiDZea/w6ig21glU/fUn8m9AjaOZcBthDE3udRnzGTfTIn p07ndKeK5j7krDd7ZAiHEGHFVVe1nRhMec4d2aB/AWUTZo0JH+b93TYQk3NBrQqCiR9Y KWiKhIlJpQocahGtkPR5XIkOqCJobIuYgAXB6LqPlojgOO5FMHugY5B8hVhJIcn2XQZ3 dY+w== 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=tWt5U1GdfRTPJ2pk99sucyxHg6/5vwO+SzSL1uwqsHM=; b=Q90dJX3r4oHtLdPD97QRjTz+FWhvIBrXG4XNO20vUZM7lMOUdRrVMnAPBgLRs4QaK/ NIqD55fk3oQVTeZVfsoa84HTKNPG2D1TST2wFO2Q0MsOqIFZiIx7FmbZig4IG9UfsqXr GP9RzXnpdtHHXh/t7uEJkfxu1A4VuhbYyAviO5W1lHo6dxupsZq6kb9iAmKCQIS6QVh+ ffSNJbt0QEimo9McH6Pqshie0tOqQh3vZK47yui1sEXlCWZUVxo7eNJeKbKUvzoipsYQ zo94k+42Z1SfPdkQZxMX5EvZ4fO0NLXw1Cx09hUA8BeQ4DPw2LMEioRtWg+jWyPajC/p cI+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="NK/qQdGC"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id q11-20020a1709060f8b00b008b1816e7568si3714406ejj.501.2023.03.09.11.41.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 11:41:48 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="NK/qQdGC"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 87D443858C5E for ; Thu, 9 Mar 2023 19:41:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87D443858C5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678390907; bh=tWt5U1GdfRTPJ2pk99sucyxHg6/5vwO+SzSL1uwqsHM=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=NK/qQdGCPC6VcHAZ+1Rn8xezYucVjBGLG7UqShMuulU5tQOlM6rasJnkopTV9qtjw 9/zCdyhu8bsLdo56rki65TV6znrcSDPuhcugnSzsdKOFa/PTqaaFeXOoA60EEgmcnR 5HhPLAePnDK9G8AlK0W8mNHga2cnHYl2rmAeOs9Y= 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 3735F385703A for ; Thu, 9 Mar 2023 19:40:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3735F385703A 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-493-hdXTUOdIPo-x0uoBHHgfpA-1; Thu, 09 Mar 2023 14:40:07 -0500 X-MC-Unique: hdXTUOdIPo-x0uoBHHgfpA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 22C713806115 for ; Thu, 9 Mar 2023 19:40:07 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D57C8492C3E; Thu, 9 Mar 2023 19:40:06 +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 329Je4pM2890146 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 9 Mar 2023 20:40:04 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 329Je41b2890145; Thu, 9 Mar 2023 20:40:04 +0100 Date: Thu, 9 Mar 2023 20:40:03 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, abi: Fix up class layout with bitfields [PR109039] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 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 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?1759920425525819944?= X-GMAIL-MSGID: =?utf-8?q?1759920425525819944?= Hi! The following testcase FAILs, because starting with r12-6028 the S class has only 2 bytes, not enough to hold one 7-bit bitfield, one 8-bit bitfield and one 8-bit char field. The reason is that when end_of_class attempts to compute dsize, it simply adds byte_position of the field and DECL_SIZE_UNIT (and uses maximum from those offsets). The problematic bit-field in question has bit_position 7, byte_position 0, DECL_SIZE 8 and DECL_SIZE_UNIT 1. So, byte_position + DECL_SIZE_UNIT is 1, even when the bitfield only has a single bit in the first byte and 7 further bits in the second byte, so per the Itanium ABI it should be 2: "In either case, update dsize(C) to include the last byte containing (part of) the bit-field, and update sizeof(C) to max(sizeof(C),dsize(C))." The following patch fixes it by computing bitsize of the end and using CEIL_DIV_EXPR division to round it to next byte boundary and convert from bits to bytes. While this is an ABI change, classes with such incorrect layout couldn't have worked properly, so I doubt anybody is actually running it often in the wild. Thus I think adding some ABI warning for it is unnecessary. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk (and after a while for GCC 12)? 2023-03-09 Jakub Jelinek PR c++/109039 * class.cc (end_of_class): For bit-fields, instead of computing offset as sum of byte_position (field) and DECL_SIZE_UNIT (field), compute it as sum of bit_position (field) and DECL_SIZE (field) divided by BITS_PER_UNIT rounded up. * g++.dg/abi/no_unique_address7.C: New test. Jakub --- gcc/cp/class.cc.jj 2023-02-04 06:22:17.053407477 +0100 +++ gcc/cp/class.cc 2023-03-09 18:02:43.967815721 +0100 @@ -6476,7 +6476,15 @@ end_of_class (tree t, eoc_mode mode) size of the type (usually 1) for computing nvsize. */ size = TYPE_SIZE_UNIT (TREE_TYPE (field)); - offset = size_binop (PLUS_EXPR, byte_position (field), size); + if (DECL_BIT_FIELD_TYPE (field)) + { + offset = size_binop (PLUS_EXPR, bit_position (field), + DECL_SIZE (field)); + offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node); + offset = fold_convert (sizetype, offset); + } + else + offset = size_binop (PLUS_EXPR, byte_position (field), size); if (tree_int_cst_lt (result, offset)) result = offset; } --- gcc/testsuite/g++.dg/abi/no_unique_address7.C.jj 2023-03-09 18:09:08.397205087 +0100 +++ gcc/testsuite/g++.dg/abi/no_unique_address7.C 2023-03-09 18:08:56.439379395 +0100 @@ -0,0 +1,33 @@ +// PR c++/109039 +// { dg-do run { target c++11 } } + +struct X { + signed short x0 : 7; + signed short x1 : 8; + X () : x0 (1), x1 (2) {} + int get () { return x0 + x1; } +}; + +struct S { + [[no_unique_address]] X x; + signed char c; + S () : c (0) {} +}; + +S s; + +int +main () +{ + if (s.x.x0 != 1 || s.x.x1 != 2 || s.c != 0) + __builtin_abort (); + s.x.x0 = -1; + s.x.x1 = -1; + if (s.x.x0 != -1 || s.x.x1 != -1 || s.c != 0) + __builtin_abort (); + s.c = -1; + s.x.x0 = 0; + s.x.x1 = 0; + if (s.x.x0 != 0 || s.x.x1 != 0 || s.c != -1) + __builtin_abort (); +}