Skip to contentSkip to conversation

crypto: blake2b - Fix clang optimization for ARMv7-M

PATCH In review Arnd Bergmann 3 participants 5 messages May 5, 2020 – May 15, 2020 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: blake2b - Fix clang optimization for ARMv7-M

When building for ARMv7-M, clang-9 or higher tries to unroll some loops, which ends up confusing the register allocator to the point of generating rather bad code and using more than the warning limit for stack frames:

warning: stack frame size of 1200 bytes in function 'blake2b_compress' [-Wframe-larger-than=]

Forcing it to not unroll the final loop avoids this problem.

Fixes 91d689337fe8 ("crypto: blake2b - add blake2b generic implementation")
---
 crypto/blake2b_generic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/blake2b_generic.c +3 -1
index 1d262374fa4e..0ffd8d92e308 100644
@@ -129,7 +129,9 @@ static void blake2b_compress(struct blake2b_state *S,
 	ROUND(9);
 	ROUND(10);
 	ROUND(11);
-
+#ifdef CONFIG_CC_IS_CLANG
+#pragma nounroll /* https://bugs.llvm.org/show_bug.cgi?id=45803 */
+#endif
 	for (i = 0; i < 8; ++i)
 		S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
 }
Signature · 1 line
2.26.0
#1 lore raw
Quoted 19 lines · Arnd Bergmann

When building for ARMv7-M, clang-9 or higher tries to unroll some loops, which ends up confusing the register allocator to the point of generating rather bad code and using more than the warning limit for stack frames:

warning: stack frame size of 1200 bytes in function 'blake2b_compress' [-Wframe-larger-than=]

Forcing it to not unroll the final loop avoids this problem.

Fixes 91d689337fe8 ("crypto: blake2b - add blake2b generic implementation")
---
 crypto/blake2b_generic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/blake2b_generic.c +0 -0
index 1d262374fa4e..0ffd8d92e308 100644

crypto/blake2b_generic.c static void blake2b_compress(struct blake2b_state *S, · line 132

Quoted 5 lines
crypto/blake2b_generic.c +1 -1
@@ -129,7 +129,9 @@ static void blake2b_compress(struct blake2b_state *S,
 	ROUND(9);
 	ROUND(10);
 	ROUND(11);
-
+#ifdef CONFIG_CC_IS_CLANG

Given your comment in the bug:

"The code is written to assume no loops are unrolled"

Does it make sense to make this unconditional and take compiler heuristics out of it?

Quoted 8 lines
crypto/blake2b_generic.c +2 -0
+#pragma nounroll /* https://bugs.llvm.org/show_bug.cgi?id=45803 */
+#endif
 	for (i = 0; i < 8; ++i)
 		S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
 }
Signature · 1 line
2.26.0
#2 lore raw
Quoted 7 lines · Nathan Chancellor

crypto/blake2b_generic.c static void blake2b_compress(struct blake2b_state *S, · line 132

Quoted 2 lines
crypto/blake2b_generic.c +1 -1
-
+#ifdef CONFIG_CC_IS_CLANG

Given your comment in the bug:

"The code is written to assume no loops are unrolled"

Quoted 7 lines

Does it make sense to make this unconditional and take compiler heuristics out of it?

crypto/blake2b_generic.c static void blake2b_compress(struct blake2b_state *S, · line 136

Quoted 4 lines
crypto/blake2b_generic.c +2 -0
+#pragma nounroll /* https://bugs.llvm.org/show_bug.cgi?id=45803 */
+#endif
      for (i = 0; i < 8; ++i)
              S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];

No, that would not work, as gcc does not support this pragma.

Arnd

Reviewed-by
#3 lore raw
Quoted 12 lines · Arnd Bergmann
Quoted 10 lines · Nathan Chancellor

crypto/blake2b_generic.c static void blake2b_compress(struct blake2b_state *S, · line 132

Quoted 2 lines
crypto/blake2b_generic.c +1 -1
-
+#ifdef CONFIG_CC_IS_CLANG

Given your comment in the bug:

"The code is written to assume no loops are unrolled"

Does it make sense to make this unconditional and take compiler heuristics out of it?

Quoted 8 lines
Quoted 4 lines

crypto/blake2b_generic.c static void blake2b_compress(struct blake2b_state *S, · line 136

Quoted 4 lines
crypto/blake2b_generic.c +2 -0
+#pragma nounroll /* https://bugs.llvm.org/show_bug.cgi?id=45803 */
+#endif
      for (i = 0; i < 8; ++i)
              S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];

No, that would not work, as gcc does not support this pragma.

Arnd

Ah fair enough.

maintainer
#4 lore raw
In reply to Arnd Bergmann
Quoted 6 lines · Arnd Bergmann

When building for ARMv7-M, clang-9 or higher tries to unroll some loops, which ends up confusing the register allocator to the point of generating rather bad code and using more than the warning limit for stack frames:

warning: stack frame size of 1200 bytes in function 'blake2b_compress' [-Wframe-larger-than=]

Quoted 7 lines

Forcing it to not unroll the final loop avoids this problem.

Fixes 91d689337fe8 ("crypto: blake2b - add blake2b generic implementation")
---
 crypto/blake2b_generic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt