From owner-dev-commits-src-main@freebsd.org  Wed Feb 17 20:46:50 2021
Return-Path: <owner-dev-commits-src-main@freebsd.org>
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 A10745320CA;
 Wed, 17 Feb 2021 20:46:50 +0000 (UTC)
 (envelope-from rlibby@gmail.com)
Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com
 [209.85.222.180])
 (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
 client-signature RSA-PSS (2048 bits) client-digest SHA256)
 (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 4DgqdP6r2gz3Mq3;
 Wed, 17 Feb 2021 20:46:49 +0000 (UTC)
 (envelope-from rlibby@gmail.com)
Received: by mail-qk1-f180.google.com with SMTP id m144so14172375qke.10;
 Wed, 17 Feb 2021 12:46:49 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:mime-version:references:in-reply-to:from:date
 :message-id:subject:to:cc;
 bh=C+rKdBrHYLqZ8xAtnZcz5YG3Rl17+5P82eSWdP1yRhM=;
 b=jFg2034mcEBXGjuhfFp5dLn5nxRbxfWyPbd0zTFoQsLrjtFcknSoLR2KlELloR/2tK
 wQPbi7UlquFH0s/V3O5bXSRGzDatsFRXlbuNyki3kOozzj/m9p74ib0X1IMZWUpt4ZoK
 mbBOH/yUVYMTHu9mgdzx3Rzdynj4iBc70fCAsQmumBvNEQZz5ZYY6P3SvuI9yqpdbiD+
 wcXFapPAZEfgR1EQoorxnRoMlvTkHVN050nMuYhwk9c4HbG6oUav1z/HHvvGXYnWRroN
 XjeSOiJuqYbIvpEi3J7NnlvebZWbZjwLGKE1NWsGXUxnaLuqHWnWGuiKwGirgh6iibt+
 86Xw==
X-Gm-Message-State: AOAM530k7E+ejIy6fezGGI4RFyPKj8BpbzHaj9cym/1FHWipdWipei6L
 UqXsA+adeoZ5aSyc4XFvFLLH96V4ARA=
X-Google-Smtp-Source: ABdhPJyqtVrPJDWH5iXuLPpfB+D+WNXEetgDs9bj2kge3+I4KDq0YnUrMBP+9feUdkSnQynR3kW2vw==
X-Received: by 2002:ae9:c30e:: with SMTP id n14mr1060783qkg.291.1613594808371; 
 Wed, 17 Feb 2021 12:46:48 -0800 (PST)
Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com.
 [209.85.160.175])
 by smtp.gmail.com with ESMTPSA id o12sm2542220qko.117.2021.02.17.12.46.48
 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
 Wed, 17 Feb 2021 12:46:48 -0800 (PST)
Received: by mail-qt1-f175.google.com with SMTP id h16so10565968qth.11;
 Wed, 17 Feb 2021 12:46:48 -0800 (PST)
X-Received: by 2002:ac8:d44:: with SMTP id r4mr1092255qti.7.1613594807753;
 Wed, 17 Feb 2021 12:46:47 -0800 (PST)
MIME-Version: 1.0
References: <202102031604.113G4SQq019037@gitrepo.freebsd.org>
In-Reply-To: <202102031604.113G4SQq019037@gitrepo.freebsd.org>
From: Ryan Libby <rlibby@freebsd.org>
Date: Wed, 17 Feb 2021 12:46:36 -0800
X-Gmail-Original-Message-ID: <CAHgpiFyAcK5+Zh0bdOdMw12Yj96RHo08g9qaNh5C50k1jwP1bA@mail.gmail.com>
Message-ID: <CAHgpiFyAcK5+Zh0bdOdMw12Yj96RHo08g9qaNh5C50k1jwP1bA@mail.gmail.com>
Subject: Re: git: 8fa6abb6f4f6 - main - Expose clang's alignment builtins and
 use them for roundup2/rounddown2
To: Alex Richardson <arichardson@freebsd.org>
Cc: src-committers <src-committers@freebsd.org>,
 dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org
Content-Type: text/plain; charset="UTF-8"
X-Rspamd-Queue-Id: 4DgqdP6r2gz3Mq3
X-Spamd-Bar: --
Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none;
 spf=pass (mx1.freebsd.org: domain of rlibby@gmail.com designates
 209.85.222.180 as permitted sender) smtp.mailfrom=rlibby@gmail.com
X-Spamd-Result: default: False [-3.00 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[];
 TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+ip4:209.85.128.0/17];
 RCVD_COUNT_THREE(0.00)[4]; NEURAL_HAM_SHORT(-1.00)[-1.000];
 FORGED_SENDER(0.30)[rlibby@freebsd.org,rlibby@gmail.com];
 MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com];
 RBL_DBL_DONT_QUERY_IPS(0.00)[209.85.222.180:from];
 R_DKIM_NA(0.00)[];
 FROM_NEQ_ENVFROM(0.00)[rlibby@freebsd.org,rlibby@gmail.com];
 ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US];
 ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000];
 FREEFALL_USER(0.00)[rlibby]; FROM_HAS_DN(0.00)[];
 RCPT_COUNT_THREE(0.00)[4]; TO_MATCH_ENVRCPT_ALL(0.00)[];
 NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain];
 DMARC_NA(0.00)[freebsd.org];
 SPAMHAUS_ZRD(0.00)[209.85.222.180:from:127.0.2.255];
 RCVD_IN_DNSWL_NONE(0.00)[209.85.222.180:from];
 RWL_MAILSPIKE_POSSIBLE(0.00)[209.85.222.180:from];
 RCVD_TLS_ALL(0.00)[];
 MAILMAN_DEST(0.00)[dev-commits-src-all,dev-commits-src-main]
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
 <dev-commits-src-main.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/dev-commits-src-main>, 
 <mailto:dev-commits-src-main-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/dev-commits-src-main/>
List-Post: <mailto:dev-commits-src-main@freebsd.org>
List-Help: <mailto:dev-commits-src-main-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/dev-commits-src-main>, 
 <mailto:dev-commits-src-main-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Wed, 17 Feb 2021 20:46:50 -0000

On Wed, Feb 3, 2021 at 8:04 AM Alex Richardson <arichardson@freebsd.org> wrote:
>
> The branch main has been updated by arichardson:
>
> URL: https://cgit.FreeBSD.org/src/commit/?id=8fa6abb6f4f64f4f23e2920e2aea7996566851a4
>
> commit 8fa6abb6f4f64f4f23e2920e2aea7996566851a4
> Author:     Alex Richardson <arichardson@FreeBSD.org>
> AuthorDate: 2021-02-03 15:27:17 +0000
> Commit:     Alex Richardson <arichardson@FreeBSD.org>
> 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.

Ryan