2019-06-21 07:15:04

by Mathieu Malaterre

[permalink] [raw]
Subject: [PATCH] crypto: Fix build for clang

The header file `longlong.h` makes uses of GNU extensions, this trigger
an error when compiling this code with clang. Add a special flag to make
clang tolerate this syntax.

Silence the following warnings triggered using W=1:

CC lib/mpi/generic_mpih-mul1.o
../lib/mpi/generic_mpih-mul1.c:37:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
-fheinous-gnu-extensions
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
: "=r" ((USItype) ph) \
~~~~~~~~~~^~

and

CC lib/mpi/generic_mpih-mul2.o
../lib/mpi/generic_mpih-mul2.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
-fheinous-gnu-extensions
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
: "=r" ((USItype) ph) \
~~~~~~~~~~^~

1 warning generated.
CC lib/mpi/generic_mpih-mul3.o
../lib/mpi/generic_mpih-mul3.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
-fheinous-gnu-extensions
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
: "=r" ((USItype) ph) \
~~~~~~~~~~^~

Or even:

../lib/mpi/mpih-div.c:99:16: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with -fheinous-gnu-extensions
sub_ddmmss(n1, n0, n1, n0, d1, d0);
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

Cc: Joel Stanley <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Signed-off-by: Mathieu Malaterre <[email protected]>
---
lib/mpi/Makefile | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/lib/mpi/Makefile b/lib/mpi/Makefile
index d5874a7f5ff9..de4d96e988a3 100644
--- a/lib/mpi/Makefile
+++ b/lib/mpi/Makefile
@@ -5,6 +5,13 @@

obj-$(CONFIG_MPILIB) = mpi.o

+ifdef CONFIG_CC_IS_CLANG
+CFLAGS_generic_mpih-mul1.o += -fheinous-gnu-extensions
+CFLAGS_generic_mpih-mul2.o += -fheinous-gnu-extensions
+CFLAGS_generic_mpih-mul3.o += -fheinous-gnu-extensions
+CFLAGS_mpih-div.o += -fheinous-gnu-extensions
+endif
+
mpi-y = \
generic_mpih-lshift.o \
generic_mpih-mul1.o \
--
2.20.1


2019-06-21 07:31:30

by Joel Stanley

[permalink] [raw]
Subject: Re: [PATCH] crypto: Fix build for clang

On Fri, 21 Jun 2019 at 07:13, Mathieu Malaterre <[email protected]> wrote:
>
> The header file `longlong.h` makes uses of GNU extensions, this trigger
> an error when compiling this code with clang. Add a special flag to make
> clang tolerate this syntax.

Another old copy of longlong.h in the kernel!

This looks similar to another clang related warnings I fixed in the
powerpc math-emu code. There's an updated version of these macros in
GCC, and we updated the kernel version to match the GCC version. Can
you see if a similar change would work here?

https://lore.kernel.org/linuxppc-dev/[email protected]/
https://git.kernel.org/torvalds/c/b682c8692442711684befe413cf93cf01c5324ea

Cheers,

Joel

>
> Silence the following warnings triggered using W=1:
>
> CC lib/mpi/generic_mpih-mul1.o
> ../lib/mpi/generic_mpih-mul1.c:37:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
> -fheinous-gnu-extensions
> umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
> ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
> : "=r" ((USItype) ph) \
> ~~~~~~~~~~^~
>
> and
>
> CC lib/mpi/generic_mpih-mul2.o
> ../lib/mpi/generic_mpih-mul2.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
> -fheinous-gnu-extensions
> umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
> ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
> : "=r" ((USItype) ph) \
> ~~~~~~~~~~^~
>
> 1 warning generated.
> CC lib/mpi/generic_mpih-mul3.o
> ../lib/mpi/generic_mpih-mul3.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
> -fheinous-gnu-extensions
> umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
> ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
> : "=r" ((USItype) ph) \
> ~~~~~~~~~~^~
>
> Or even:
>
> ../lib/mpi/mpih-div.c:99:16: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with -fheinous-gnu-extensions
> sub_ddmmss(n1, n0, n1, n0, d1, d0);
> ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
>
> Cc: Joel Stanley <[email protected]>
> Cc: Masahiro Yamada <[email protected]>
> Signed-off-by: Mathieu Malaterre <[email protected]>
> ---
> lib/mpi/Makefile | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/lib/mpi/Makefile b/lib/mpi/Makefile
> index d5874a7f5ff9..de4d96e988a3 100644
> --- a/lib/mpi/Makefile
> +++ b/lib/mpi/Makefile
> @@ -5,6 +5,13 @@
>
> obj-$(CONFIG_MPILIB) = mpi.o
>
> +ifdef CONFIG_CC_IS_CLANG
> +CFLAGS_generic_mpih-mul1.o += -fheinous-gnu-extensions
> +CFLAGS_generic_mpih-mul2.o += -fheinous-gnu-extensions
> +CFLAGS_generic_mpih-mul3.o += -fheinous-gnu-extensions
> +CFLAGS_mpih-div.o += -fheinous-gnu-extensions
> +endif
> +
> mpi-y = \
> generic_mpih-lshift.o \
> generic_mpih-mul1.o \
> --
> 2.20.1
>

2019-06-21 07:45:20

by Mathieu Malaterre

[permalink] [raw]
Subject: Re: [PATCH] crypto: Fix build for clang

Joel,

On Fri, Jun 21, 2019 at 9:30 AM Joel Stanley <[email protected]> wrote:
>
> On Fri, 21 Jun 2019 at 07:13, Mathieu Malaterre <[email protected]> wrote:
> >
> > The header file `longlong.h` makes uses of GNU extensions, this trigger
> > an error when compiling this code with clang. Add a special flag to make
> > clang tolerate this syntax.
>
> Another old copy of longlong.h in the kernel!
>
> This looks similar to another clang related warnings I fixed in the
> powerpc math-emu code. There's an updated version of these macros in
> GCC, and we updated the kernel version to match the GCC version. Can
> you see if a similar change would work here?
>
> https://lore.kernel.org/linuxppc-dev/[email protected]/
> https://git.kernel.org/torvalds/c/b682c8692442711684befe413cf93cf01c5324ea

Great ! Thanks for the pointer. Looks like a much bettter solution in
the long term. Will try asap.

> Cheers,
>
> Joel
>
> >
> > Silence the following warnings triggered using W=1:
> >
> > CC lib/mpi/generic_mpih-mul1.o
> > ../lib/mpi/generic_mpih-mul1.c:37:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
> > -fheinous-gnu-extensions
> > umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
> > ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
> > : "=r" ((USItype) ph) \
> > ~~~~~~~~~~^~
> >
> > and
> >
> > CC lib/mpi/generic_mpih-mul2.o
> > ../lib/mpi/generic_mpih-mul2.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
> > -fheinous-gnu-extensions
> > umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
> > ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
> > : "=r" ((USItype) ph) \
> > ~~~~~~~~~~^~
> >
> > 1 warning generated.
> > CC lib/mpi/generic_mpih-mul3.o
> > ../lib/mpi/generic_mpih-mul3.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
> > -fheinous-gnu-extensions
> > umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
> > ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
> > : "=r" ((USItype) ph) \
> > ~~~~~~~~~~^~
> >
> > Or even:
> >
> > ../lib/mpi/mpih-div.c:99:16: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with -fheinous-gnu-extensions
> > sub_ddmmss(n1, n0, n1, n0, d1, d0);
> > ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
> >
> > Cc: Joel Stanley <[email protected]>
> > Cc: Masahiro Yamada <[email protected]>
> > Signed-off-by: Mathieu Malaterre <[email protected]>
> > ---
> > lib/mpi/Makefile | 7 +++++++
> > 1 file changed, 7 insertions(+)
> >
> > diff --git a/lib/mpi/Makefile b/lib/mpi/Makefile
> > index d5874a7f5ff9..de4d96e988a3 100644
> > --- a/lib/mpi/Makefile
> > +++ b/lib/mpi/Makefile
> > @@ -5,6 +5,13 @@
> >
> > obj-$(CONFIG_MPILIB) = mpi.o
> >
> > +ifdef CONFIG_CC_IS_CLANG
> > +CFLAGS_generic_mpih-mul1.o += -fheinous-gnu-extensions
> > +CFLAGS_generic_mpih-mul2.o += -fheinous-gnu-extensions
> > +CFLAGS_generic_mpih-mul3.o += -fheinous-gnu-extensions
> > +CFLAGS_mpih-div.o += -fheinous-gnu-extensions
> > +endif
> > +
> > mpi-y = \
> > generic_mpih-lshift.o \
> > generic_mpih-mul1.o \
> > --
> > 2.20.1
> >

2019-06-21 19:57:21

by Mathieu Malaterre

[permalink] [raw]
Subject: [PATCH v2] lib/mpi: fix build with clang

Remove superfluous casts on output operands to avoid warnings on the
following macros:

* add_ssaaaa(sh, sl, ah, al, bh, bl)
* sub_ddmmss(sh, sl, ah, al, bh, bl)
* umul_ppmm(ph, pl, m0, m1)

Special care has been taken to keep the diff as minimal as possible from
the original header file `longlong.h` from gcc, only importing the
minimal change to make the compilation with clang pass.

Silence the following warnings triggered using W=1:

CC lib/mpi/generic_mpih-mul1.o
../lib/mpi/generic_mpih-mul1.c:37:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
-fheinous-gnu-extensions
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
: "=r" ((USItype) ph) \
~~~~~~~~~~^~

and

CC lib/mpi/generic_mpih-mul2.o
../lib/mpi/generic_mpih-mul2.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
-fheinous-gnu-extensions
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
: "=r" ((USItype) ph) \
~~~~~~~~~~^~

1 warning generated.
CC lib/mpi/generic_mpih-mul3.o
../lib/mpi/generic_mpih-mul3.c:36:13: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with
-fheinous-gnu-extensions
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/mpi/longlong.h:824:20: note: expanded from macro 'umul_ppmm'
: "=r" ((USItype) ph) \
~~~~~~~~~~^~

Or even:

../lib/mpi/mpih-div.c:99:16: error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast or build with -fheinous-gnu-extensions
sub_ddmmss(n1, n0, n1, n0, d1, d0);
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

Inspiration is taken from commit b682c8692442 ("powerpc/math-emu: Update
macros from GCC").

Suggested-by: Joel Stanley <[email protected]>
Cc: Segher Boessenkool <[email protected]>
Signed-off-by: Mathieu Malaterre <[email protected]>
---
v2: Instead of passing compat flag to clang to behave as gcc, remove the superfluous cast

lib/mpi/longlong.h | 105 +++++++++++++++------------------------------
1 file changed, 35 insertions(+), 70 deletions(-)

diff --git a/lib/mpi/longlong.h b/lib/mpi/longlong.h
index 08c60d10747f..f5503dfa42ed 100644
--- a/lib/mpi/longlong.h
+++ b/lib/mpi/longlong.h
@@ -753,79 +753,44 @@ do { \
***************************************/
#if (defined(_ARCH_PPC) || defined(_IBMR2)) && W_TYPE_SIZE == 32
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
-do { \
- if (__builtin_constant_p(bh) && (bh) == 0) \
- __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "%r" ((USItype)(ah)), \
- "%r" ((USItype)(al)), \
- "rI" ((USItype)(bl))); \
- else if (__builtin_constant_p(bh) && (bh) == ~(USItype) 0) \
- __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "%r" ((USItype)(ah)), \
- "%r" ((USItype)(al)), \
- "rI" ((USItype)(bl))); \
- else \
- __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "%r" ((USItype)(ah)), \
- "r" ((USItype)(bh)), \
- "%r" ((USItype)(al)), \
- "rI" ((USItype)(bl))); \
-} while (0)
+ do { \
+ if (__builtin_constant_p (bh) && (bh) == 0) \
+ __asm__ ("add%I4c %1,%3,%4\n\taddze %0,%2" \
+ : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
+ else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0) \
+ __asm__ ("add%I4c %1,%3,%4\n\taddme %0,%2" \
+ : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
+ else \
+ __asm__ ("add%I5c %1,%4,%5\n\tadde %0,%2,%3" \
+ : "=r" (sh), "=&r" (sl) \
+ : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl)); \
+ } while (0)
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
-do { \
- if (__builtin_constant_p(ah) && (ah) == 0) \
- __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "r" ((USItype)(bh)), \
- "rI" ((USItype)(al)), \
- "r" ((USItype)(bl))); \
- else if (__builtin_constant_p(ah) && (ah) == ~(USItype) 0) \
- __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "r" ((USItype)(bh)), \
- "rI" ((USItype)(al)), \
- "r" ((USItype)(bl))); \
- else if (__builtin_constant_p(bh) && (bh) == 0) \
- __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "r" ((USItype)(ah)), \
- "rI" ((USItype)(al)), \
- "r" ((USItype)(bl))); \
- else if (__builtin_constant_p(bh) && (bh) == ~(USItype) 0) \
- __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "r" ((USItype)(ah)), \
- "rI" ((USItype)(al)), \
- "r" ((USItype)(bl))); \
- else \
- __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \
- : "=r" ((USItype)(sh)), \
- "=&r" ((USItype)(sl)) \
- : "r" ((USItype)(ah)), \
- "r" ((USItype)(bh)), \
- "rI" ((USItype)(al)), \
- "r" ((USItype)(bl))); \
-} while (0)
+ do { \
+ if (__builtin_constant_p (ah) && (ah) == 0) \
+ __asm__ ("subf%I3c %1,%4,%3\n\tsubfze %0,%2" \
+ : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
+ else if (__builtin_constant_p (ah) && (ah) == ~(USItype) 0) \
+ __asm__ ("subf%I3c %1,%4,%3\n\tsubfme %0,%2" \
+ : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
+ else if (__builtin_constant_p (bh) && (bh) == 0) \
+ __asm__ ("subf%I3c %1,%4,%3\n\taddme %0,%2" \
+ : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
+ else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0) \
+ __asm__ ("subf%I3c %1,%4,%3\n\taddze %0,%2" \
+ : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
+ else \
+ __asm__ ("subf%I4c %1,%5,%4\n\tsubfe %0,%3,%2" \
+ : "=r" (sh), "=&r" (sl) \
+ : "r" (ah), "r" (bh), "rI" (al), "r" (bl)); \
+ } while (0)
#if defined(_ARCH_PPC)
#define umul_ppmm(ph, pl, m0, m1) \
-do { \
- USItype __m0 = (m0), __m1 = (m1); \
- __asm__ ("mulhwu %0,%1,%2" \
- : "=r" ((USItype) ph) \
- : "%r" (__m0), \
- "r" (__m1)); \
- (pl) = __m0 * __m1; \
-} while (0)
+ do { \
+ USItype __m0 = (m0), __m1 = (m1); \
+ __asm__ ("mulhwu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
+ (pl) = __m0 * __m1; \
+ } while (0)
#define UMUL_TIME 15
#define smul_ppmm(ph, pl, m0, m1) \
do { \
--
2.20.1

2019-06-25 04:17:47

by Joel Stanley

[permalink] [raw]
Subject: Re: [PATCH v2] lib/mpi: fix build with clang

On Fri, 21 Jun 2019 at 19:56, Mathieu Malaterre <[email protected]> wrote:
>
> Remove superfluous casts on output operands to avoid warnings on the
> following macros:
>
> * add_ssaaaa(sh, sl, ah, al, bh, bl)
> * sub_ddmmss(sh, sl, ah, al, bh, bl)
> * umul_ppmm(ph, pl, m0, m1)
>
> Special care has been taken to keep the diff as minimal as possible from
> the original header file `longlong.h` from gcc, only importing the
> minimal change to make the compilation with clang pass.


> Suggested-by: Joel Stanley <[email protected]>
> Cc: Segher Boessenkool <[email protected]>
> Signed-off-by: Mathieu Malaterre <[email protected]>
> ---
> v2: Instead of passing compat flag to clang to behave as gcc, remove the superfluous cast

Thanks, I checked your patch against GCC's longlong.h and it looks good.

Reviewed-by: Joel Stanley <[email protected]>

Cheers,

Joel

2019-06-25 19:53:56

by Segher Boessenkool

[permalink] [raw]
Subject: Re: [PATCH v2] lib/mpi: fix build with clang

Hi Mathieu,

On Fri, Jun 21, 2019 at 09:55:54PM +0200, Mathieu Malaterre wrote:
> Remove superfluous casts on output operands to avoid warnings on the
> following macros:
>
> * add_ssaaaa(sh, sl, ah, al, bh, bl)
> * sub_ddmmss(sh, sl, ah, al, bh, bl)
> * umul_ppmm(ph, pl, m0, m1)

The patch is fine of course, but you might want to look at not using
these asm macros at all: GCC can handle multi-word arithmetic just fine
by itself, probably better than these macros do even.


Segher