Skip to contentSkip to conversation

crypto: Fix build for clang

PATCH In review Mathieu Malaterre 3 participants 6 messages Jun 21, 2019 – Jun 25, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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]>
---
 lib/mpi/Makefile | 7 +++++++
 1 file changed, 7 insertions(+)
lib/mpi/Makefile +7 -0
index d5874a7f5ff9..de4d96e988a3 100644
@@ -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		\
Signature · 1 line
2.20.1
#1 lore raw
Quoted 4 lines · Mathieu Malaterre

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

Quoted 67 lines

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]>
---
 lib/mpi/Makefile | 7 +++++++
 1 file changed, 7 insertions(+)
lib/mpi/Makefile +7 -1
index d5874a7f5ff9..de4d96e988a3 100644
@@ -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

#2 lore raw

Joel,

Quoted 9 lines · Joel Stanley
Quoted 4 lines · Mathieu Malaterre

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!

Quoted 7 lines

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.

Quoted 71 lines

Cheers,

Joel

Quoted 67 lines

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]>
---
 lib/mpi/Makefile | 7 +++++++
 1 file changed, 7 insertions(+)
lib/mpi/Makefile +7 -1
index d5874a7f5ff9..de4d96e988a3 100644
@@ -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

#3 lore raw

PATCH v2 1/1 lib/mpi: fix build with clang

In reply to Mathieu Malaterre

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").

---
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(-)
lib/mpi/longlong.h +35 -70
index 08c60d10747f..f5503dfa42ed 100644
@@ -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 { \
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#4 lore raw

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

Quoted 13 lines · Mathieu Malaterre

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.

Quoted 5 lines

---
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.

Cheers,

Joel

#5 lore raw

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

In reply to Mathieu Malaterre

Hi Mathieu,

Quoted 6 lines · Mathieu Malaterre

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