From patchwork Tue Sep 10 18:15:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1983456 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=Nce7n15x; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=yrF4ZApf; dkim-atps=neutral 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=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X3Bhg0ltkz1y1C for ; Wed, 11 Sep 2024 04:15:46 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 67EF33858C41 for ; Tue, 10 Sep 2024 18:15:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 337C43858D39; Tue, 10 Sep 2024 18:15:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 337C43858D39 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 337C43858D39 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725992121; cv=pass; b=pgrjk1ZdDSby/iha9YyQeTfENg51fdWde0m/Am2njDuKvmG2XY+cJjNxAkpAR4B/MGFAVxehqp+v5T1Lom02aBeowG70UBHiDoc7A+5rHaM6CvyqA9G5dSsqMrVfa+enQ4GPhSr/r/hMO2IjPzP2M+yd/q64Aw4lUE/jcmMcm/E= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725992121; c=relaxed/simple; bh=RXSPBiIV0fkqNKrCDLfe1RUDict32FQvDuD2BPREp5k=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=HJB45od+IHjr+/0nlHG2ce5psEsOtE4SO0DNVPixJh3VkFHslW906Z97LHcxNp74LTMUafEcz2AgggJ3GQkCCbzv6/unbCXGlw19jPNma0ngsj9F5HWM7jc3nPUY0fsDvCEyfZFvlVDaJsLsgUEElMtqWT5HOo07Lbhiq1e6vlU= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48AHNcVY009367; Tue, 10 Sep 2024 18:15:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:content-transfer-encoding :content-type:mime-version; s=corp-2023-11-20; bh=v8FjDNw2xXCOGw wwDYu6LZowiVPS1skdKhZN5S1uNFI=; b=Nce7n15xjZrrle/EhyjOEj4aID5m2s jIDFaVc+wddygh63WmyBgtYxqwJKEbbsPyfSeVc7w4/mXt5SxisR58HWWkPL55mS wmbQK3EF2dVm0ihGKul+o+EPxxx6zJ2DShFgcA+hSU85itd/QagDJaCUAl1oVZyV JwmRE0C9gd2+9p0x1cfyH8m46i/gapqwuLUjmrg3KpwcVjzwDJRpvpocwcL0Rm1m +yzLL+qTU84HKbPphgUBbjPqFOhLLAeLF1kqMPxVZpGth6f/K69B89kzmgIl32Oy VVrthRtXQmnAlNZnIDlFCyByXkDpiDTI49NuSPAlw7WRkE4yyR2xyTdw== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41gfctebm8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 10 Sep 2024 18:15:16 +0000 (GMT) Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 48AGn8ML031577; Tue, 10 Sep 2024 18:15:15 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 41gd99erm0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 10 Sep 2024 18:15:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IP5mgWJ6toJ9Bcq0LmSi2kvhVYUImUgr6goD4Bdf2JcLB/xzSWqGSg03VLecytcnFc5AAOkvXHeVDsh38cYbatpwHcMKFEUQMT/8xLWsktHvKlixeTY7+Xr+m2t8bk+nXM0y8UViEkJXpHe1RUMFwQ7yccPMneauqB1f6GJc+51EgUZHmRRmwkfVXfdf5FNv4vKbNHra+ygdwnXRIt4gf23Oykg0Y6rnsEhzAH9Le25tQ2dGKwm6u6nuQcClu3icHWEkxUwFtaXcZvmuE8TV/heaC5bC6BLijrOvVTOogyLOJmGpBEUDqKZ7HbZHvmCzxUteLWnyJQK4mpOSNvbihw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=v8FjDNw2xXCOGwwwDYu6LZowiVPS1skdKhZN5S1uNFI=; b=vvD5T5WM5svv+4+C5UVf/dA1YT4+iY8QkDitscXeZlzUdfLU7DAcxwHor6+l8Srr31NYnIHaUtUbGsRKnx7G2NFpSWjFrzRfqGpTYu7g8pHzeiVses0bd3ZVpLDIWBcNZ1PTkGH91jG/ffWKWvtPCR3rpqZLC3T3fvd+xbH0TPvHLwoLQgyCyQ9gNtVAQnSbjjg71V/b4qiDUPY40Rj01gHrM3h/fytnT1kugbxS2ghP/LvraAzwNOJS7AVIl85Be1pAc2Yk11AKd8SfzxDzqUsZ77BKbh/xhRBHPfpQtiJD7lzrIdDxvKazJ/PvnviCCG6Ku0k5GBAJl8eSpi5BZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=v8FjDNw2xXCOGwwwDYu6LZowiVPS1skdKhZN5S1uNFI=; b=yrF4ZApfj6KJWzclONtDaTfs/O2QMFunHR25HVSAFSRqoKZ2eSrN/JIGwdP8AcY5eZFqBjUJwtGArvV3AYW6HG0WFpdpS+Iw/n0jDS2q44junW2zIcgmTm65mIOPexrWKuHMH6WZwUWx5/LAlitj31LgwIYKZVDLEBy2wZwuONY= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SA2PR10MB4762.namprd10.prod.outlook.com (2603:10b6:806:114::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.12; Tue, 10 Sep 2024 18:15:12 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::8a:61cd:764a:e47]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::8a:61cd:764a:e47%4]) with mapi id 15.20.7962.008; Tue, 10 Sep 2024 18:15:12 +0000 From: Qing Zhao To: josmyers@redhat.com, uecker@tugraz.at, jakub@redhat.com Cc: keescook@chromium.org, isanbard@gmail.com, siddhesh@gotplt.org, sjames@gcc.gnu.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v3] Provide new GCC builtin __builtin_counted_by_ref [PR116016] Date: Tue, 10 Sep 2024 18:15:09 +0000 Message-Id: <20240910181509.2452465-1-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 X-ClientProxiedBy: SJ0PR03CA0090.namprd03.prod.outlook.com (2603:10b6:a03:331::35) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|SA2PR10MB4762:EE_ X-MS-Office365-Filtering-Correlation-Id: 48c01dcb-a912-4a1a-72ea-08dcd1c48337 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|1800799024; X-Microsoft-Antispam-Message-Info: ImeSvmmQVdAMcir6BExQxjgiYvf3xADbEjI1W/FdbIMEob3J5Z+iE65EEkiuunsuGrnpuU7NoNrcDL4NMNdkdrxriL2B+G0Ufn6Bylhxs5mQl377Yrnu9+0z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ggplBiNnaHSD+V6Opoi1JF0/m9vyx9VCAFKLDzjHDYINN85uFOeBzCkc3qZ666/BSEbx3BS/h6S1GIk4GPJgcTZGzMwfkreavnn9TTmF3oiiA+ImdenTqghpHK+X9M= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CY8PR10MB6538.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(376014)(1800799024); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HmRX12oANLgvYuez5CEHBaN6Iwyo8wO6HXIyMzT9RbTgfvqrmF3W0kNDf/35vHbjwwjgySzvBEgJDcxVB5icTGEI4MYXRCE8tPGOIF3okxn1zMqy9stybY4zc+x61m/06e0VV8AwUQT7AfSckbJUqvqjSe2yKCwDpy+lVhxaPIqL4s5lE240aVEdduT1FAGSPJ2JVCamx3zUWwvOVy2V7un9BY7+rsOzP58dRlEt6oVAFJGMJwX30ANLA2oxj1y6AF46FpLk5f++dUQ4uOugq+8G0/Z/kRPbUdHGfpnt9nODdjZvktnFjVJl+zxZehS1Pr4pwxAKaAAIedcYhOJ//dmRX8h1Jk2FQe/Q66nzrjBmFk5nmqQXwWRKWtBbpqBuGlXb/ziwr9CL3vfPjL1OhXWMXJtjKQFE7wDNgXgdP3kMUOjebmVBtmmRIafgzHkfKsng8bfRnA9t/4TY4tuaBwvfW4LXPGXTCWkCmW5ejYvBZNgB2g2iCODp7LwXsEKzdtKU7hglUNGYOVdy6Pph4dV2amMOIQ32kyFg4M9y9K6D9iOPEX2YCWXK4Di0YFag24I6T9jejyneHZ7FYjk613HincyXYbjwcI4cbmABmTSMO0yIgAsHoqfWPHrTar3VFTuQRPzg0QRAGY6KX++gTtcjzmZFADd2Twlybv0db7XeTp/3GewAg0leScwlTockHOzQZTxfQ3dPhPI4V8i2jLKUMuJnKUmFX0l8MRhf0OPR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hwTYd6ECw+ZYptdY1Q1wUa1Xis4Xa234gHTpSal1qmR4s/AIBiIa5eI9YSLQJCtR9icMex14BW8OF0lOOteULxuXy3hWRc98iPnH7oHu7rKzGjDkPoYhnQjOL11oU1F9qATZZ/YtZ3DyVRjTRyRL+FaNG03ENTDY1Xc5Qdp9WE2jxAHYmFGY8S/5cM7b9NMSf6xq+obJ8ZKtEN0jI2nFqGH2O/EwKYhzOYwG7FHuhAdDo/D+BtDa7bLQZiqOvcMN+2jB6GIr2vHA/ysMlKEtwcs7htEJ36uzoMyF1V0EuBTQ6XGeEHtqDsql/PvT7Gn6MLPXLXwjTeojreaA9AqlCbRPESbs6d9E0MBtvGKyXiDcFdDuqCe0WpXMMUgq4vvvYS2BxfDyAMbVrDgS6lFkhUh4P1F78i70Xv1ZAbfv4zkChsFptRRgITVJiPFLn/rj7nj4MGXox/vARnwZ8= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 48c01dcb-a912-4a1a-72ea-08dcd1c48337 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 18:15:12.6705 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: YkJGzuHh7FOFfUQXXClwz94wJrJL3hwls2Ck9c49+u+na1B3XwBWvquNxZVZztcdUijNgzTlE07GNu6SCWfLNw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4762 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-10_06,2024-09-09_02,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 bulkscore=0 suspectscore=0 phishscore=0 mlxscore=0 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2408220000 definitions=main-2409100136 X-Proofpoint-GUID: R6D-mYzOlC0HCZuCx5zqdvcvcO616MNp X-Proofpoint-ORIG-GUID: R6D-mYzOlC0HCZuCx5zqdvcvcO616MNp X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, This is the 3rd version of the patch. compared to the 2nd version, the only change is the update in testing cases per Martin's suggestions. when the 2nd version is compared to the first version, the major changes are: 1. change the name of the builtin from __builtin_get_counted_by to __builtin_counted_by_ref in order to reflect the fact that the returned value of it is a reference to the object. 2. make typeof(__builtin_counted_by_ref) working. 3. update the testing case to use the new builtin inside _Generic. bootstrapped and regress tested on both X86 and aarch64. no issue. Okay for the trunk? thanks. Qing. =================================== With the addition of the 'counted_by' attribute and its wide roll-out within the Linux kernel, a use case has been found that would be very nice to have for object allocators: being able to set the counted_by counter variable without knowing its name. For example, given: struct foo { ... int counter; ... struct bar array[] __attribute__((counted_by (counter))); } *p; The existing Linux object allocators are roughly: #define alloc(P, FAM, COUNT) ({ \ size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \ __p = kmalloc(__size, GFP); \ }) Right now, any addition of a counted_by annotation must also include an open-coded assignment of the counter variable after the allocation: p = alloc(p, array, how_many); p->counter = how_many; In order to avoid the tedious and error-prone work of manually adding the open-coded counted-by intializations everywhere in the Linux kernel, a new GCC builtin __builtin_counted_by_ref will be very useful to be added to help the adoption of the counted-by attribute. -- Built-in Function: TYPE __builtin_counted_by_ref (PTR) The built-in function '__builtin_counted_by_ref' checks whether the array object pointed by the pointer PTR has another object associated with it that represents the number of elements in the array object through the 'counted_by' attribute (i.e. the counted-by object). If so, returns a pointer to the corresponding counted-by object. If such counted-by object does not exist, returns a NULL pointer. This built-in function is only available in C for now. The argument PTR must be a pointer to an array. The TYPE of the returned value must be a pointer type pointing to the corresponding type of the counted-by object or VOID pointer type in case of a NULL pointer being returned. With this new builtin, the central allocator could be updated to: #define alloc(P, FAM, COUNT) ({ \ __auto_type __p = &(P); \ __auto_type __c = (COUNT); \ size_t __size = sizeof(*(*__p)) + sizeof(*((*__p)->FAM)) * __c; \ if ((*__p = kmalloc(__size))) { \ __auto_type ret = __builtin_counted_by_ref((*__p)->FAM); \ *_Generic(ret, void *: &(size_t){0}, default: ret) = __c; \ } \ }) And then structs can gain the counted_by attribute without needing additional open-coded counter assignments for each struct, and unannotated structs could still use the same allocator. PR c/116016 gcc/c-family/ChangeLog: * c-common.cc: Add new __builtin_counted_by_ref. * c-common.h (enum rid): Add RID_BUILTIN_COUNTED_BY_REF. gcc/c/ChangeLog: * c-decl.cc (names_builtin_p): Add RID_BUILTIN_COUNTED_BY_REF. * c-parser.cc (has_counted_by_object): New routine. (get_counted_by_ref): New routine. (c_parser_postfix_expression): Handle New RID_BUILTIN_COUNTED_BY_REF. * c-tree.h: New global in_builtin_counted_by_ref. * c-typeck.cc (build_component_ref): Enable generating .ACCESS_WITH_SIZE inside typeof when inside builtin_counted_by_ref. gcc/ChangeLog: * doc/extend.texi: Add documentation for __builtin_counted_by_ref. gcc/testsuite/ChangeLog: * gcc.dg/builtin-counted-by-ref-1.c: New test. * gcc.dg/builtin-counted-by-ref.c: New test. --- gcc/c-family/c-common.cc | 1 + gcc/c-family/c-common.h | 1 + gcc/c/c-decl.cc | 1 + gcc/c/c-parser.cc | 77 +++++++++++++++ gcc/c/c-tree.h | 1 + gcc/c/c-typeck.cc | 7 +- gcc/doc/extend.texi | 55 +++++++++++ .../gcc.dg/builtin-counted-by-ref-1.c | 94 +++++++++++++++++++ gcc/testsuite/gcc.dg/builtin-counted-by-ref.c | 58 ++++++++++++ 9 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/builtin-counted-by-ref-1.c create mode 100644 gcc/testsuite/gcc.dg/builtin-counted-by-ref.c diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index e7e371fd26f..15b90bae8b5 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -430,6 +430,7 @@ const struct c_common_resword c_common_reswords[] = { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR, 0 }, + { "__builtin_counted_by_ref", RID_BUILTIN_COUNTED_BY_REF, D_CONLY }, { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE, 0 }, { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY }, { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 027f077d51b..4400d2c5d43 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -110,6 +110,7 @@ enum rid RID_TYPES_COMPATIBLE_P, RID_BUILTIN_COMPLEX, RID_BUILTIN_SHUFFLE, RID_BUILTIN_SHUFFLEVECTOR, RID_BUILTIN_CONVERTVECTOR, RID_BUILTIN_TGMATH, RID_BUILTIN_HAS_ATTRIBUTE, RID_BUILTIN_ASSOC_BARRIER, RID_BUILTIN_STDC, + RID_BUILTIN_COUNTED_BY_REF, RID_DFLOAT32, RID_DFLOAT64, RID_DFLOAT128, /* TS 18661-3 keywords, in the same sequence as the TI_* values. */ diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index aa7f69d1b7b..1145dde9bb1 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -11788,6 +11788,7 @@ names_builtin_p (const char *name) case RID_BUILTIN_SHUFFLE: case RID_BUILTIN_SHUFFLEVECTOR: case RID_BUILTIN_STDC: + case RID_BUILTIN_COUNTED_BY_REF: case RID_CHOOSE_EXPR: case RID_OFFSETOF: case RID_TYPES_COMPATIBLE_P: diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index aff5af17430..0feddf72f95 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -10647,6 +10647,35 @@ c_parser_predefined_identifier (c_parser *parser) return expr; } +/* Check whether the ARRAY_REF has an counted-by object associated with it + through the "counted_by" attribute. */ +static bool +has_counted_by_object (tree array_ref) +{ + /* Currently, only when the array_ref is an indirect_ref to a call to the + .ACCESS_WITH_SIZE, return true. + More cases can be included later when the counted_by attribute is + extended to other situations. */ + if ((TREE_CODE (array_ref) == INDIRECT_REF) + && is_access_with_size_p (TREE_OPERAND (array_ref, 0))) + return true; + return false; +} + +/* Get the reference to the counted-by object associated with the ARRAY_REF. */ +static tree +get_counted_by_ref (tree array_ref) +{ + /* Currently, only when the array_ref is an indirect_ref to a call to the + .ACCESS_WITH_SIZE, get the corresponding counted_by ref. + More cases can be included later when the counted_by attribute is + extended to other situations. */ + if ((TREE_CODE (array_ref) == INDIRECT_REF) + && is_access_with_size_p (TREE_OPERAND (array_ref, 0))) + return CALL_EXPR_ARG (TREE_OPERAND (array_ref, 0), 1); + return NULL_TREE; +} + /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2, C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to call c_parser_postfix_expression_after_paren_type on encountering them. @@ -11741,6 +11770,54 @@ c_parser_postfix_expression (c_parser *parser) set_c_expr_source_range (&expr, loc, close_paren_loc); break; } + case RID_BUILTIN_COUNTED_BY_REF: + { + vec *cexpr_list; + c_expr_t *e_p; + location_t close_paren_loc; + + in_builtin_counted_by_ref = true; + + c_parser_consume_token (parser); + if (!c_parser_get_builtin_args (parser, + "__builtin_counted_by_ref", + &cexpr_list, false, + &close_paren_loc)) + { + expr.set_error (); + goto error_exit; + } + if (vec_safe_length (cexpr_list) != 1) + { + error_at (loc, "wrong number of arguments to " + "%<__builtin_counted_by_ref%>"); + expr.set_error (); + goto error_exit; + } + + e_p = &(*cexpr_list)[0]; + + if (TREE_CODE (TREE_TYPE (e_p->value)) != ARRAY_TYPE) + { + error_at (loc, "the argument must be an array" + "%<__builtin_counted_by_ref%>"); + expr.set_error (); + goto error_exit; + } + + if (has_counted_by_object ((*cexpr_list)[0].value)) + expr.value + = get_counted_by_ref ((*cexpr_list)[0].value); + else + expr.value + = build_int_cst (build_pointer_type (void_type_node), 0); + + set_c_expr_source_range (&expr, loc, close_paren_loc); + +error_exit: + in_builtin_counted_by_ref = false; + break; + } case RID_BUILTIN_SHUFFLE: { vec *cexpr_list; diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 57befb94c08..1e8a090d3b6 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -737,6 +737,7 @@ extern int c_type_dwarf_attribute (const_tree, int); extern int in_alignof; extern int in_sizeof; extern int in_typeof; +extern bool in_builtin_counted_by_ref; extern bool c_in_omp_for; extern bool c_omp_array_section_p; diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 58b2724b39e..ed9444e2077 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -74,6 +74,9 @@ int in_sizeof; /* The level of nesting inside "typeof". */ int in_typeof; +/* inside "builtin_counted_by_ref". */ +bool in_builtin_counted_by_ref; + /* True when parsing OpenMP loop expressions. */ bool c_in_omp_for; @@ -2848,7 +2851,9 @@ build_component_ref (location_t loc, tree datum, tree component, bool use_datum_quals; tree counted_by_type = NULL_TREE; /* Do not handle counted_by when in typeof and alignof operator. */ - handle_counted_by = handle_counted_by && !in_typeof && !in_alignof; + handle_counted_by = handle_counted_by + && !(in_typeof && !in_builtin_counted_by_ref) + && !in_alignof; tree counted_by_ref = handle_counted_by ? build_counted_by_ref (datum, subdatum, &counted_by_type) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2d795ba7e59..268e50677d7 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -15287,6 +15287,61 @@ initializers of variables usable in constant expressions. For more details refer to the latest revision of the C++ standard. @enddefbuiltin +@defbuiltin{@var{type} __builtin_counted_by_ref (@var{ptr})} +The built-in function @code{__builtin_counted_by_ref} checks whether the array +object pointed by the pointer @var{ptr} has another object associated with it +that represents the number of elements in the array object through the +@code{counted_by} attribute (i.e. the counted-by object). If so, returns a +pointer to the corresponding counted-by object. +If such counted-by object does not exist, returns a NULL pointer. + +This built-in function is only available in C for now. + +The argument @var{ptr} must be a pointer to an array. +The @var{type} of the returned value must be a pointer type pointing to the +corresponding type of the counted-by object or a VOID pointer type in case +of a NULL pointer being returned. + +For example: + +@smallexample +struct foo1 @{ + int counter; + struct bar1 array[] __attribute__((counted_by (counter))); +@} *p; + +struct foo2 @{ + int other; + struct bar2 array[]; +@} *q; +@end smallexample + +@noindent +the following call to the built-in + +@smallexample +__builtin_counted_by_ref (p->array) +@end smallexample + +@noindent +returns: + +@smallexample +&p->counter with type @code{int *}. +@end smallexample + +@noindent +However, the following call to the built-in + +@smallexample +__builtin_counted_by_ref (q->array) +@end smallexample + +@noindent +returns a void NULL pointer. + +@enddefbuiltin + @defbuiltin{void __builtin_clear_padding (@var{ptr})} The built-in function @code{__builtin_clear_padding} function clears padding bits inside of the object representation of object pointed by diff --git a/gcc/testsuite/gcc.dg/builtin-counted-by-ref-1.c b/gcc/testsuite/gcc.dg/builtin-counted-by-ref-1.c new file mode 100644 index 00000000000..19da8495162 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-counted-by-ref-1.c @@ -0,0 +1,94 @@ +/* Test the code generation for the new __builtin_counted_by_ref. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ +#include + +struct annotated { + char b; + int c[] __attribute ((counted_by (b))); +} *array_annotated; + +struct flex { + short b; + int c[]; +} *array_flex; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +struct nested_flex { + struct { + union { + unsigned int b; + float f; + }; + int n; + }; + char c[]; +} *array_nested_flex; + +#define MY_ALLOC(P, FAM, COUNT) ({ \ + __auto_type __p = &(P); \ + __auto_type __c = (COUNT); \ + size_t __size = sizeof(*(*__p)) + sizeof(*((*__p)->FAM)) * __c; \ + if ((*__p = __builtin_malloc(__size))) { \ + __builtin_memset(*__p, 0, __size); \ + __auto_type ret = __builtin_counted_by_ref((*__p)->FAM); \ + *_Generic(ret, void *: &(size_t){0}, default: ret) = __c; \ + if (sizeof (__builtin_counted_by_ref ((*__p)->FAM)) != sizeof (char *)) \ + __builtin_abort (); \ + } \ +}) + +int count; + +int main(int argc, char *argv[]) +{ + MY_ALLOC(array_annotated, c, 10); + if (array_annotated->b != 10) + __builtin_abort (); + + MY_ALLOC(array_flex, c, 20); + if (array_flex->b == 20) + __builtin_abort (); + + MY_ALLOC(array_nested_annotated, c, 30); + if (array_nested_annotated->b != 30) + __builtin_abort (); + + MY_ALLOC(array_nested_flex, c, 40); + if (array_nested_flex->b == 40) + __builtin_abort (); + + count = array_annotated->b * 2 + array_nested_annotated->b * 3; + struct annotated * annotated_p; + struct flex * flex_p; + struct nested_annotated * nested_annotated_p; + struct nested_flex * nested_flex_p; + + MY_ALLOC(annotated_p, c, count); + if (annotated_p->b != count) + __builtin_abort (); + + MY_ALLOC(flex_p, c, count * 2); + if (flex_p->b == count * 2) + __builtin_abort (); + + MY_ALLOC(nested_annotated_p, c, count * 3); + if (nested_annotated_p->b != count * 3) + __builtin_abort (); + + MY_ALLOC(nested_flex_p, c, count * 4); + if (nested_flex_p->b == count * 4) + __builtin_abort (); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/builtin-counted-by-ref.c b/gcc/testsuite/gcc.dg/builtin-counted-by-ref.c new file mode 100644 index 00000000000..751f671f8bc --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-counted-by-ref.c @@ -0,0 +1,58 @@ +/* Testing the correct usage of the new __builtin_counted_by_ref. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +#include + +struct annotated { + size_t b; + int other; + int c[] __attribute ((counted_by (b))); +} *array_annotated; + +struct flex { + size_t b; + int other; + int c[]; +} *array_flex; + +#define MY_ALLOC(P, FAM, COUNT) ({ \ + __auto_type __p = &(P); \ + __auto_type __c = (COUNT); \ + size_t __size = sizeof(*(*__p)) + sizeof(*((*__p)->FAM)) * __c; \ + if ((*__p = __builtin_malloc(__size))) { \ + __builtin_memset(*__p, 0, __size); \ + __auto_type ret = __builtin_counted_by_ref((*__p)->FAM); \ + *_Generic(ret, void *: &(size_t){0}, default: ret) = __c; \ + if (sizeof (__builtin_counted_by_ref ((*__p)->FAM)) != sizeof (char *)) \ + __builtin_abort (); \ + } \ +}) + +extern char c_count; +extern short s_count; +extern int i_count; +extern long l_count; +extern float f_count; + +extern int * foo (); + +int main(int argc, char *argv[]) +{ + /* The good usages. */ + MY_ALLOC(array_annotated, c, 10); + MY_ALLOC(array_flex, c, 20); + MY_ALLOC(array_annotated, c, c_count); + MY_ALLOC(array_flex, c, i_count); + MY_ALLOC(array_annotated, c, l_count); + MY_ALLOC(array_flex, c, c_count * 3); + MY_ALLOC(array_annotated, c, l_count * i_count); + + /* The bad usages, issue errors. */ + __builtin_counted_by_ref (); /* { dg-error "wrong number of arguments to" } */ + __builtin_counted_by_ref (array_annotated->c, 10); /* { dg-error "wrong number of arguments to" } */ + __builtin_counted_by_ref (array_annotated->other); /* { dg-error "the argument must be an array" } */ + __builtin_counted_by_ref (foo()); /* { dg-error "the argument must be an array" } */ + + return 0; +}