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: 66185 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9724F3858C78 for ; Thu, 9 Mar 2023 19:41:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9724F3858C78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678390892; 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=D3o0cTqSKDRv/+s88EeyldU13PEKarqyHk1dhfgsnmH/Lt4hGKjcToKr9eMtOQT0Y DTyuqQOqMVNFBBz6kJ3yCgYsvZFwfLQ0KvZM3pc95TXl6LHEdYx54+CyK6mQd/t5OL 5Le3l+oxlmBdpbdvRGY07mx84xTxYdoHoH1vYzKY= 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 (); +}