From owner-dev-commits-src-main@freebsd.org Thu Feb 18 12:46:29 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 67D88536A04; Thu, 18 Feb 2021 12:46:29 +0000 (UTC) (envelope-from tijl@freebsd.org) Received: from mailrelay114.isp.belgacom.be (mailrelay114.isp.belgacom.be [195.238.20.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "relay.skynet.be", Issuer "GlobalSign RSA OV SSL CA 2018" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DhDwh4VvJz4RHx; Thu, 18 Feb 2021 12:46:28 +0000 (UTC) (envelope-from tijl@freebsd.org) IronPort-SDR: KHGenJt7xZwth9oKwZTMHennWwwJJ2LsZZt71S9vE9aB25GYBRZVjplDnk64YSkoMHMCyYIPu4 prbmNYlcEW+E0I6ndz/qY1XrVkiHD3wPqeeyMDjX37qnyE1gt8K+CXA0INMA5IjjUI99NUZT0n sQg78bngU1MeeILktVSiq4R2q0xHyGA1IFOfZgrSRrbGFCfRWhu8OQaiZFbKphmsoIdvv5NyD7 9DeelginMGGefcffCnheFiR9EsBwRnFdo4OaM0naxqWuQ334+PJdBBi3iTh5zmm+Fs8xN/7O2R Uw4= IronPort-PHdr: =?us-ascii?q?9a23=3AaNHRkhVW9IWCX22g6ZEVW1ZUDQrV8LGtZVwlr6?= =?us-ascii?q?E/grcLSJyIuqrYZRaCuKdThVPEFb/W9+hDw7KP9fy5CCpfvd3Y4TgrS99lb1?= =?us-ascii?q?c9k8IYnggtUoauKHbQC7rUVRE8B9lIT1R//nu2YgB/Ecf6YEDO8DXptWZBUh?= =?us-ascii?q?rwOhBoKevrB4Xck9q41/yo+53Ufg5EmCexbal9IRmrqQjdrMsbjIRtJqoszh?= =?us-ascii?q?bCv2dFdflRyW50P1yYggzy5t23/J5t8iRQv+wu+stdWqjkfKo2UKJVAi0+P2?= =?us-ascii?q?86+MPkux/DTRCS5nQHSWUZjgBIAwne4x7kWJr6rzb3ufB82CmeOs32UKw0VD?= =?us-ascii?q?G/5KplVBPklCEKPCMi/WrJlsJ/kr5UoBO5pxx+3YHUZp2VNOFjda/ZZN8WWH?= =?us-ascii?q?ZNUtpUWyFHH4iybZYAD/AZMOhWr4fzuUYAoxi8CgmiC+zg1jBGi2Tq3aA4ye?= =?us-ascii?q?ktDR3K0BA+E98IrX/arM/1NKAXUe2t0qfKyDHDZO5I1Df674jIaQ4uofSWUr?= =?us-ascii?q?J2asra1E4iFx/EjlWSrYzoJDOb1v4TvGeF8uduSOyhh3QgqwFrrTii38EhgZ?= =?us-ascii?q?TGiYwJ0F7L7zl5wJorKt2iTk52ed2pHZhRuiyZN4V6X8MsTmJotig6xbAIt5?= =?us-ascii?q?61cDQUxJkn2xPSavKJfoqW7hzsSeqcPDd1iGxldr++hxu/70eux+36W8Kp3l?= =?us-ascii?q?hKqS9FncPNtnALzxHT5NKHReVj8Ui6wjaAyhrf6uZeIUA7jabbJJghwqMqmp?= =?us-ascii?q?oUq0TDESn7k1j1gq+Obkgp+fWk5/75brjpuJOQLZJ4hwPgPqg0lcGyB/kzPB?= =?us-ascii?q?IUUGiB4+u80aXu/Uj+QLpXkPI7irLZsJXGJcQDoa65HhNV3p4j6xmhCzem18?= =?us-ascii?q?wVnXodI1JBeRKHipbmO0vJIPziEfe/glaskDNxy//aOb3hBY7NIWbGkLj7e7?= =?us-ascii?q?Z98FRQyA0pzdBQ/55UC7cBL+zvWkLpt9HUEwU1PxK7zur5Etlxy4ETVXyVDq?= =?us-ascii?q?KWKK/StEWH5uMrI+mCfo8VvzP9JuA55/7vj385nUQQfaez3ZQJdn+3Be9mLF?= =?us-ascii?q?iDbXrrmNcBHn8GvhAiQ+zylF2CTTlTam6vUK0i+DE3EZ6pDYDGRoCogbyBwD?= =?us-ascii?q?y7EoRNZmBcFl+ACHLoeJ+FW/cKcy+SLNVhnSIaWre6UI8uyAuitAjgy7poNu?= =?us-ascii?q?DU4DEXtYr/1Nhp4O3ejQw9+iJqD8iEz26NSGR0nmYJRz8qx6BzuFd9yk2f3q?= =?us-ascii?q?RhmfNYEdhT6uhHUgcgK5HT0fd1C8vvVQ7bedeJUlmmSM28AT4tVtIx38MOY0?= =?us-ascii?q?FlFtW5lBDMwzOmDKMSl7yOAZw0877c32TvKMZn0HbLz6chj0M4TcRRLmKmnb?= =?us-ascii?q?Nw+xLUB47TnEWTj7yqergE3C7R6GeDynKDvENYUAFrSqjFU2ofaVXIotvi/U?= =?us-ascii?q?PNUqWiCbM9MgtO0cSCMLdFasX1jVVaQ/fuIM7eY2awm2irHhmJxqiBbITwe2?= =?us-ascii?q?oDwirSE1MEnB0J8XaBLwg+CT+ro3jCAzx2CVLvf0Ts/PFkp3OhT0870wGKYF?= =?us-ascii?q?d717Wo4R4VgvqcRugd3rIepigutS57HE2g0N7MDNqAvQVhL+1gZoYU5FpX3G?= =?us-ascii?q?TVuhY1BJ2rL6l4ikUZdUwjtlnv3BV+EIlbls4ComknwQBpJeSfylwXJB2C2p?= =?us-ascii?q?WlBlrTYkL1+wuibqfQwRmKzNeU/o8h8vk1gW7P+gazGRxxoD1cz9BJ3i7Etd?= =?us-ascii?q?3xBw0IXMe0Cx5v+g=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A2BEAABSYS5g/wSs8lFiGQEBAQEBAQE?= =?us-ascii?q?BAQEBAQEBAQEBARIBAQEBAQEBAQEBAQFAB4FIgwwVVgFQGo1FhjqCFgM3AYh?= =?us-ascii?q?BgiiRJwsBAQEBAQEBAQEzCgQBAYRNAoIMJjgTAgMBAQEDAgUBAQYBAQEBAQE?= =?us-ascii?q?FBAGGGDkNgjgpAYNjAQUnEz8QCw4KIwtXBhMbglGDCguueoEBM4Q/AYUOgQI?= =?us-ascii?q?GgTgBjT9CggKEKz6CRhcBAxeBKIYTBIJGAT0+EwEbCAgggTk6ATcpj3WMX5x?= =?us-ascii?q?OgwWJOpJFMaM2hjKZP5IbA4ZKgXpNMAiDJFAZDY4rFhSITYVGQAMvAgksAgY?= =?us-ascii?q?KAQEDCYwTAQE?= X-IPAS-Result: =?us-ascii?q?A2BEAABSYS5g/wSs8lFiGQEBAQEBAQEBAQEBAQEBAQEBA?= =?us-ascii?q?RIBAQEBAQEBAQEBAQFAB4FIgwwVVgFQGo1FhjqCFgM3AYhBgiiRJwsBAQEBA?= =?us-ascii?q?QEBAQEzCgQBAYRNAoIMJjgTAgMBAQEDAgUBAQYBAQEBAQEFBAGGGDkNgjgpA?= =?us-ascii?q?YNjAQUnEz8QCw4KIwtXBhMbglGDCguueoEBM4Q/AYUOgQIGgTgBjT9CggKEK?= =?us-ascii?q?z6CRhcBAxeBKIYTBIJGAT0+EwEbCAgggTk6ATcpj3WMX5xOgwWJOpJFMaM2h?= =?us-ascii?q?jKZP5IbA4ZKgXpNMAiDJFAZDY4rFhSITYVGQAMvAgksAgYKAQEDCYwTAQE?= Received: from 4.172-242-81.adsl-dyn.isp.belgacom.be (HELO kalimero.tijl.coosemans.org) ([81.242.172.4]) by relay.proximus.be with ESMTP; 18 Feb 2021 13:46:17 +0100 Received: from localhost (localhost [127.0.0.1]) by kalimero.tijl.coosemans.org (8.16.1/8.16.1) with ESMTP id 11ICkFEi013384; Thu, 18 Feb 2021 13:46:16 +0100 (CET) (envelope-from tijl@FreeBSD.org) Date: Thu, 18 Feb 2021 13:46:14 +0100 From: =?UTF-8?B?VMSzbA==?= Coosemans To: Alexander Richardson Cc: Ryan Libby , John Baldwin , src-committers , dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: 8fa6abb6f4f6 - main - Expose clang's alignment builtins and use them for roundup2/rounddown2 Message-ID: <20210218134614.376e4eaa@FreeBSD.org> In-Reply-To: References: <202102031604.113G4SQq019037@gitrepo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4DhDwh4VvJz4RHx X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Feb 2021 12:46:29 -0000 On Wed, 17 Feb 2021 21:01:05 +0000 Alexander Richardson wrote: > On Wed, 17 Feb 2021 at 20:46, Ryan Libby wrote: >> On Wed, Feb 3, 2021 at 8:04 AM Alex Richardson wrote: >>> The branch main has been updated by arichardson: >>> >>> URL: https://cgit.FreeBSD.org/src/commit/?id=8fa6abb6f4f64f4f23e2920e2aea7996566851a4 >>> >>> commit 8fa6abb6f4f64f4f23e2920e2aea7996566851a4 >>> Author: Alex Richardson >>> AuthorDate: 2021-02-03 15:27:17 +0000 >>> Commit: Alex Richardson >>> CommitDate: 2021-02-03 16:02:54 +0000 >>> >>> Expose clang's alignment builtins and use them for roundup2/rounddown2 >>> >>> This makes roundup2/rounddown2 type- and const-preserving and allows >>> using it on pointer types without casting to uintptr_t first. Not >>> performing pointer-to-integer conversions also helps the compiler's >>> optimization passes and can therefore result in better code generation. >>> When using it with integer values there should be no change other than >>> the compiler checking that the alignment value is a valid power-of-two. >>> >>> I originally implemented these builtins for CHERI a few years ago and >>> they have been very useful for CheriBSD. However, they are also useful >>> for non-CHERI code so I was able to upstream them for Clang 10.0. >>> >>> Rationale from the clang documentation: >>> Clang provides builtins to support checking and adjusting alignment >>> of pointers and integers. These builtins can be used to avoid relying >>> on implementation-defined behavior of arithmetic on integers derived >>> from pointers. Additionally, these builtins retain type information >>> and, unlike bitwise arithmetic, they can perform semantic checking on >>> the alignment value. >>> >>> There is also a feature request for GCC, so GCC may also support it in >>> the future: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98641 >>> >>> Reviewed By: brooks, jhb, imp >>> Differential Revision: https://reviews.freebsd.org/D28332 >>> --- >>> sys/sys/cdefs.h | 19 +++++++++++++++++++ >>> sys/sys/param.h | 4 ++-- >>> 2 files changed, 21 insertions(+), 2 deletions(-) >>> >>> diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h >>> index 75bedd4b8128..72ef942084f2 100644 >>> --- a/sys/sys/cdefs.h >>> +++ b/sys/sys/cdefs.h >>> @@ -884,4 +884,23 @@ >>> #define __guarded_by(x) __lock_annotate(guarded_by(x)) >>> #define __pt_guarded_by(x) __lock_annotate(pt_guarded_by(x)) >>> >>> +/* Alignment builtins for better type checking and improved code generation. */ >>> +/* Provide fallback versions for other compilers (GCC/Clang < 10): */ >>> +#if !__has_builtin(__builtin_is_aligned) >>> +#define __builtin_is_aligned(x, align) \ >>> + (((__uintptr_t)x & ((align) - 1)) == 0) >>> +#endif >>> +#if !__has_builtin(__builtin_align_up) >>> +#define __builtin_align_up(x, align) \ >>> + ((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1)))) >>> +#endif >>> +#if !__has_builtin(__builtin_align_down) >>> +#define __builtin_align_down(x, align) \ >>> + ((__typeof__(x))((x)&(~((align)-1)))) >>> +#endif >>> + >>> +#define __align_up(x, y) __builtin_align_up(x, y) >>> +#define __align_down(x, y) __builtin_align_down(x, y) >>> +#define __is_aligned(x, y) __builtin_is_aligned(x, y) >>> + >> >> Since these are only valid for powers of 2, I think it would be good to >> indicate that in the names (__align_up2() etc). >> >>> #endif /* !_SYS_CDEFS_H_ */ >>> diff --git a/sys/sys/param.h b/sys/sys/param.h >>> index 079357a19d47..d6f1eb21dcd2 100644 >>> --- a/sys/sys/param.h >>> +++ b/sys/sys/param.h >>> @@ -305,9 +305,9 @@ >>> #endif >>> #define nitems(x) (sizeof((x)) / sizeof((x)[0])) >>> #define rounddown(x, y) (((x)/(y))*(y)) >>> -#define rounddown2(x, y) ((x)&(~((y)-1))) /* if y is power of two */ >>> +#define rounddown2(x, y) __align_down(x, y) /* if y is power of two */ >>> #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */ >>> -#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ >>> +#define roundup2(x, y) __align_up(x, y) /* if y is powers of two */ >>> #define powerof2(x) ((((x)-1)&(x))==0) >>> >>> /* Macros for min/max. */ >> >> This broke the gcc build: >> https://ci.freebsd.org/job/FreeBSD-main-amd64-gcc6_build/3200/console >> 00:40:30 --- all_subdir_firewire --- >> 00:40:30 In file included from /workspace/src/sys/sys/types.h:43:0, >> 00:40:30 from /workspace/src/sys/sys/param.h:99, >> 00:40:30 from /workspace/src/sys/dev/firewire/fwohci.c:40: >> 00:40:30 /workspace/src/sys/dev/firewire/fwohci.c: In function >> 'fwohci_get_plen': >> 00:40:30 /workspace/src/sys/dev/firewire/fwohci.c:2699:17: error: >> 'typeof' applied to a bit-field >> 00:40:30 r += roundup2(fp->mode.wreqb.len, sizeof(uint32_t)); >> >> We could: >> - Drop the cast for the fallback. >> - Cast with (__typeof__(+(x))) which unfortunately promotes e.g. char >> to int but otherwise I think behaves okay, and the promotion was >> previously happening for roundup2/rounddown2 anyway. >> - Punt the casting burden to callers of roundup2/rounddown2. > > https://reviews.freebsd.org/D28599 fixes this specific issue. One > reason for the typeof() is to allow using it with pointer types. > Currently there are no such uses, but we have some in CheriBSD. > Unfortunately +(x) would break that. (x)+0 should work for most cases, > but does break void*. > I believe the current approach only breaks with bitfields (which is > hopefully rare), so I think adding casts for GCC when roundup2() in > those cases might be the simplest solution. The comma operator might work here: typeof(((void)0,(x))).