RE: [PATCH next v2 2/5] minmax: Allow min()/max()/clamp() if the arguments have the same signedness.

From: David Laight
Date: Sun Jul 30 2023 - 13:33:58 EST


From: Nathan Chancellor
> Sent: 30 July 2023 18:06
>
> On Sun, Jul 30, 2023 at 02:55:50PM +0000, David Laight wrote:
> > From: kernel test robot
> > > Sent: 29 July 2023 03:01
> > >
> > > kernel test robot noticed the following build errors:
> > >
> > ...)
> > > compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project.git
> > > 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
> > > reproduce: (https://download.01.org/0day-ci/archive/20230729/202307290943.ODVeyeK6-
> > > lkp@xxxxxxxxx/reproduce)
> > >
> > ...
> > > >> mm/percpu.c:3102:10: error: static assertion expression is not an integral constant expression
> > > base = min(ptr, base);
> > > ^~~~~~~~~~~~~~
> > ... ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > include/linux/minmax.h:23:2: note: expanded from macro '__types_ok'
> > > (is_signed_type(typeof(x)) == is_signed_type(typeof(y)))
> > > ^
> > ...
> > > mm/percpu.c:3102:10: note: cast that performs the conversions of a reinterpret_cast is not allowed
> > > in a constant expression
> >
> > That is a C++ error that seems to have crept into C.
> > The relevant definition is:
> >
> > #define is_signed_type(type) (((type)(-1)) < (type)1)
> >
> > This seems to have been fixed in clang 16.0.0.
>
> Indeed, it looks like
>
> https://github.com/llvm/llvm-project/commit/a181de452df311d7647329120d05f4eb9c158b6c
>
> fixed this as a result of the discussion at
>
> https://github.com/llvm/llvm-project/issues/57687, which certainly makes
> sense.

Annoyingly it also isn't a 'proper' compile time constant for pointers.
Wrapping as:
__builtin_choose_expr(__is_constexpr(is_signed(type(x)), is_signed_type(x), 0)
fixes it for old clang and is also needed to fix the >= 0 check in 5/5.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)