Skip to contentSkip to conversation

unroll: Make unrolled, unrolled_full and unrolled_none function-like

PATCH In review Tzu-Ching Yang 4 participants 9 messages Oct 16 – Oct 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unroll: Make unrolled, unrolled_full and unrolled_none function-like

unrolled, unrolled_full and unrolled_none are object-like macros with lower-case names, so in a file that includes <linux/unroll.h> every later identifier with one of these names is replaced with a _Pragma() operator. A local variable or struct member with one of these names fails to build. A function parameter named "unrolled" is worse: GCC expands "unrolled" to an empty pragma, which leaves the parameter unnamed and removes the name from the expressions that use it, and GCC 11 and later accept that with only -Wunknown-pragmas warnings. For example, GCC compiles

	static int add(int unrolled, int x)
	{
		return unrolled + x;
	}

as "return +x;".

No variable, member or parameter in the tree uses these names yet, but the header is already pulled in by <linux/lsm_hooks.h>, the vhost and libeth headers and a few .c files, and every new includer exposes more code to the clash.

Make the three macros function-like, as unrolled_count() and UNROLL() already are. A function-like macro is expanded only where its name is followed by '(', so variables, struct members and parameters with the same names are left alone. Convert the only users, lib/crypto/blake2b.c and lib/crypto/blake2s.c, to unrolled_full(), and explain the empty argument list in the header comment.

No functional change: blake2b.c and blake2s.c preprocess to the same output as before, and every object that depends on <linux/unroll.h> is byte-identical in x86_64 (GCC 13, Clang 18), riscv64 (GCC 13) and arm64 (Clang 18) builds, apart from the DWARF 5 checksum of the two edited source files in the arm64 debug info.

---
unroll.h has no MAINTAINERS entry (hence Andrew on Cc), and the only users of these three macros are in lib/crypto/, so I think this fits the libcrypto tree best.

Testing: x86_64, riscv64 and arm64 defconfig plus the includers of <linux/unroll.h> (all LSMs, IMA/EVM, BPF LSM, vhost, i40e/ice/idpf), built without the patch and then rebuilt with it in the same source and output directories. Make rebuilt exactly the 135 objects that depend on <linux/unroll.h> or blake2{b,s}.c, plus the composite objects that contain them, so those are the objects compared. lib/crypto/, security/, drivers/vhost/ and idpf give the same result with riscv64 GCC 8.1 and 16.2, x86_64 Clang 17.0.1 and arm64 Clang 23.1.1. GCC and Clang still report the BLAKE2 round loops as completely unrolled.

Stand-alone, with only <linux/unroll.h>: GCC 11.5, 12.5, 13.3, 14.2 and 16.2 build the add() example from the changelog, and it returns x; GCC 8.1, 9.5, 10.5 and Clang 17, 18 and 23 reject it. With this patch, a variable, struct member or parameter with any of the three names builds without warnings and gives the right result with all of these compilers.

The patch applies as is to next-20261014, libcrypto-next (40f47fa10a1d) and net-next (518bf436318e), and none of them has other users of these names. A user added elsewhere with the old spelling (unrolled_full without parentheses) fails to build rather than silently losing the hint.

Not addressed: a use followed by '(' is still expanded, so a function or function pointer with one of these names still clashes when it is called. With arguments, that fails to build. Without arguments, GCC still turns unrolled() into an empty pragma, so a call to a zero-argument function unrolled() declared before the header is included is dropped with only warnings (Clang rejects it).

unrolled and unrolled_none still have no users. If preferred, I can instead remove them, as Simon and Maciej suggested when they were added, and convert only unrolled_full; that also removes the case above.

 include/linux/unroll.h | 12 ++++++++----
 lib/crypto/blake2b.c   |  2 +-
 lib/crypto/blake2s.c   |  2 +-
 3 files changed, 10 insertions(+), 6 deletions(-)
include/linux/unroll.h +8 -4
index 186b71de74..8fde8006eb 100644
@@ -16,7 +16,7 @@
 #endif
 
 /**
- * unrolled - loop attributes to ask the compiler to unroll it
+ * unrolled() - loop attributes to ask the compiler to unroll it
  *
  * Usage:
  *
@@ -33,10 +33,14 @@
  * on what iter x will do with variables) is not a strict requirement, but
  * provides best performance and object code size.
  * Available only on Clang and GCC 8.x onwards.
+ *
+ * unrolled(), unrolled_full() and unrolled_none() take an empty argument
+ * list so that only a use followed by '(' is expanded: variables, struct
+ * members and function parameters with these names are left alone.
  */
 
 /* Ask the compiler to pick an optimal unroll count, Clang only */
-#define unrolled							\
+#define unrolled()							\
 	__pick_unrolled(clang loop unroll(enable), /* nothing */)
 
 /* Unroll each @n iterations of the loop */
@@ -44,11 +48,11 @@
 	__pick_unrolled(clang loop unroll_count(n), GCC unroll n)
 
 /* Unroll the whole loop */
-#define unrolled_full							\
+#define unrolled_full()							\
 	__pick_unrolled(clang loop unroll(full), GCC unroll 65534)
 
 /* Never unroll the loop */
-#define unrolled_none							\
+#define unrolled_none()							\
 	__pick_unrolled(clang loop unroll(disable), GCC unroll 1)
 
 #define UNROLL(N, MACRO, args...) CONCATENATE(__UNROLL_, N)(MACRO, args)
lib/crypto/blake2b.c +1 -1
index 55d6c437f3..1f885b1f83 100644
@@ -81,7 +81,7 @@ blake2b_compress_generic(struct blake2b_ctx *ctx,
 		 * Not worthwhile on 32-bit kernels because the code size is
 		 * already so large there due to BLAKE2b using 64-bit words.
 		 */
-		unrolled_full
+		unrolled_full()
 #endif
 		for (int r = 0; r < 12; r++) {
 			G(r, 0, v[0], v[4], v[8], v[12]);
lib/crypto/blake2s.c +1 -1
index 24f7f34334..2260ed9a3d 100644
@@ -76,7 +76,7 @@ blake2s_compress_generic(struct blake2s_ctx *ctx,
 		 * Unroll the rounds loop to enable constant-folding of the
 		 * blake2s_sigma values.
 		 */
-		unrolled_full
+		unrolled_full()
 		for (int r = 0; r < 10; r++) {
 			G(r, 0, v[0], v[4], v[8], v[12]);
 			G(r, 1, v[1], v[5], v[9], v[13]);

base-commit: 40f47fa10a1d19c74f48a13d97b2eb19d5fb938d

Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 71 lines · Tzu-Ching Yang

unrolled, unrolled_full and unrolled_none are object-like macros with lower-case names, so in a file that includes <linux/unroll.h> every later identifier with one of these names is replaced with a _Pragma() operator. A local variable or struct member with one of these names fails to build. A function parameter named "unrolled" is worse: GCC expands "unrolled" to an empty pragma, which leaves the parameter unnamed and removes the name from the expressions that use it, and GCC 11 and later accept that with only -Wunknown-pragmas warnings. For example, GCC compiles

	static int add(int unrolled, int x)
	{
		return unrolled + x;
	}

as "return +x;".

No variable, member or parameter in the tree uses these names yet, but the header is already pulled in by <linux/lsm_hooks.h>, the vhost and libeth headers and a few .c files, and every new includer exposes more code to the clash.

Make the three macros function-like, as unrolled_count() and UNROLL() already are. A function-like macro is expanded only where its name is followed by '(', so variables, struct members and parameters with the same names are left alone. Convert the only users, lib/crypto/blake2b.c and lib/crypto/blake2s.c, to unrolled_full(), and explain the empty argument list in the header comment.

No functional change: blake2b.c and blake2s.c preprocess to the same output as before, and every object that depends on <linux/unroll.h> is byte-identical in x86_64 (GCC 13, Clang 18), riscv64 (GCC 13) and arm64 (Clang 18) builds, apart from the DWARF 5 checksum of the two edited source files in the arm64 debug info.

---
unroll.h has no MAINTAINERS entry (hence Andrew on Cc), and the only users of these three macros are in lib/crypto/, so I think this fits the libcrypto tree best.

Testing: x86_64, riscv64 and arm64 defconfig plus the includers of <linux/unroll.h> (all LSMs, IMA/EVM, BPF LSM, vhost, i40e/ice/idpf), built without the patch and then rebuilt with it in the same source and output directories. Make rebuilt exactly the 135 objects that depend on <linux/unroll.h> or blake2{b,s}.c, plus the composite objects that contain them, so those are the objects compared. lib/crypto/, security/, drivers/vhost/ and idpf give the same result with riscv64 GCC 8.1 and 16.2, x86_64 Clang 17.0.1 and arm64 Clang 23.1.1. GCC and Clang still report the BLAKE2 round loops as completely unrolled.

Stand-alone, with only <linux/unroll.h>: GCC 11.5, 12.5, 13.3, 14.2 and 16.2 build the add() example from the changelog, and it returns x; GCC 8.1, 9.5, 10.5 and Clang 17, 18 and 23 reject it. With this patch, a variable, struct member or parameter with any of the three names builds without warnings and gives the right result with all of these compilers.

The patch applies as is to next-20261014, libcrypto-next (40f47fa10a1d) and net-next (518bf436318e), and none of them has other users of these names. A user added elsewhere with the old spelling (unrolled_full without parentheses) fails to build rather than silently losing the hint.

Not addressed: a use followed by '(' is still expanded, so a function or function pointer with one of these names still clashes when it is called. With arguments, that fails to build. Without arguments, GCC still turns unrolled() into an empty pragma, so a call to a zero-argument function unrolled() declared before the header is included is dropped with only warnings (Clang rejects it).

Quoted 4 lines

unrolled and unrolled_none still have no users. If preferred, I can instead remove them, as Simon and Maciej suggested when they were added, and convert only unrolled_full; that also removes the case above.

Thanks. If there are no objections I'll take this patch via libcrypto-next. It was too late to make the 7.4 pull request though, so just FYI it will be a couple weeks before I apply it.

- Eric

#2 lore raw
In reply to Tzu-Ching Yang

From: Tzu-Ching Yang <[email protected]>
Date: Fri, 16 Oct 2026 10:56:13 +0800

Quoted 1 line

unrolled, unrolled_full and unrolled_none are object-like macros with

Because those are attributes. With function-like names such patterns:

unrolled()
for (int i = 0; i < n; i++)

are counter-intuitive and confusing.

We have either plain-named attributes like notrace (BTW what if you name your variable as notrace?) or underscored like __always_inline. If you really need to name your variables unrolled, then I'd expect changing these macros to __unrolled etc, not to unrolled(). Although I'm not sure we really need it, there's tons of such macros in the kernel -- try naming your variable current.

Quoted 3 lines

lower-case names, so in a file that includes <linux/unroll.h> every later identifier with one of these names is replaced with a _Pragma() operator. A local variable or struct member with one of these names

Thanks,
Olek

#3 lore raw
Quoted 6 lines · Alexander Lobakin

We have either plain-named attributes like notrace (BTW what if you name your variable as notrace?) or underscored like __always_inline. If you really need to name your variables unrolled, then I'd expect changing these macros to __unrolled etc, not to unrolled(). Although I'm not sure we really need it, there's tons of such macros in the kernel -- try naming your variable current.

A variable named notrace or current fails to build, and so does one named unrolled_full. The one that worries me is unrolled: on GCC it expands to an empty _Pragma(), so a function parameter of that name compiles with only -Wunknown-pragmas warnings (not errors without CONFIG_WERROR) and silently loses its name:

	int add(int unrolled, int x) { return unrolled + x; }

builds as "return x;" with GCC 11 to 16 (Clang rejects it). Nothing in the tree does that today; this is about not leaving the trap for the next file that includes <linux/unroll.h>.

I see three ways to close it:

1. This patch: make the three macros function-like, as unrolled_count() and UNROLL() already are. Variables, members and parameters with these names are then left alone. Cost: an attribute that looks like a call.

2. Your suggestion: rename them to __unrolled, __unrolled_full and __unrolled_none, like __always_inline. The attribute syntax stays, and nobody names a variable that way. Cost: inconsistent names, unless unrolled_count() is renamed as well.

3. Drop unrolled and unrolled_none, which have no users. This removes the silent case and touches only unroll.h; a variable named unrolled_full still fails to build, but with an error, not silently.

Which would you prefer? I can send v2 for 2 or 3, or leave 1 as posted.

Thanks,
Tzu-Ching

#4 lore raw
In reply to Alexander Lobakin
Quoted 11 lines · On Wed, 21 Oct 2026 17:02:30 +0200 Alexander Lobakin…

From: Tzu-Ching Yang <[email protected]>
Date: Fri, 16 Oct 2026 10:56:13 +0800

Quoted 1 line

unrolled, unrolled_full and unrolled_none are object-like macros with

Because those are attributes. With function-like names such patterns:

unrolled()
for (int i = 0; i < n; i++)

are counter-intuitive and confusing.

Is it possible to make the syntax:

	unrolled_for(....)

Or is that made 'all hard and difficult' because he setting is sticky?

David

Quoted 14 lines

We have either plain-named attributes like notrace (BTW what if you name your variable as notrace?) or underscored like __always_inline. If you really need to name your variables unrolled, then I'd expect changing these macros to __unrolled etc, not to unrolled(). Although I'm not sure we really need it, there's tons of such macros in the kernel -- try naming your variable current.

Quoted 3 lines

lower-case names, so in a file that includes <linux/unroll.h> every later identifier with one of these names is replaced with a _Pragma() operator. A local variable or struct member with one of these names

Thanks,
Olek

#5 lore raw
Quoted 4 lines · David Laight

Is it possible to make the syntax:

	unrolled_for(....)

Or is that made 'all hard and difficult' because he setting is sticky?

The pragma binds only to the loop that follows it, so it can go inside the macro:

#define unrolled_full_for(...) \
	__pick_unrolled(clang loop unroll(full), GCC unroll 65534) \
	for (__VA_ARGS__)

	unrolled_full_for (int i = 0, j = 7; i < 8; i++, j--)
		s += a[i] * b[j];

GCC 8 to 16 and Clang 17 to 23 unroll this fully, and the same works with break and continue in the body, nested loops and a loop inside another macro. The count form has the same shape:

	unrolled_count_for (4, int i = 0; i < n; i++)
		s += a[i];

As a fourth option, the _for forms would replace the four unrolled*
macros: no clash with identifiers, and the loop reads like for_each_*(); the cost is a new loop spelling at every user. Olek, Eric, would you prefer this over options 1 to 3 from my earlier reply?

Thanks,
Tzu-Ching

#6 lore raw

From: Tzu-Ching Yang <[email protected]>
Date: Thu, 22 Oct 2026 19:09:59 +0800

Quoted 20 lines · David Laight
Quoted 4 lines

Is it possible to make the syntax:

	unrolled_for(....)

Or is that made 'all hard and difficult' because he setting is sticky?

The pragma binds only to the loop that follows it, so it can go inside the macro:

#define unrolled_full_for(...) \
	__pick_unrolled(clang loop unroll(full), GCC unroll 65534) \
	for (__VA_ARGS__)

	unrolled_full_for (int i = 0, j = 7; i < 8; i++, j--)
		s += a[i] * b[j];

GCC 8 to 16 and Clang 17 to 23 unroll this fully, and the same works with break and continue in the body, nested loops and a loop inside another macro. The count form has the same shape:

Quoted 7 lines
	unrolled_count_for (4, int i = 0; i < n; i++)
		s += a[i];

As a fourth option, the _for forms would replace the four unrolled*
macros: no clash with identifiers, and the loop reads like for_each_*(); the cost is a new loop spelling at every user. Olek, Eric, would you prefer this over options 1 to 3 from my earlier reply?

I had considered this option in the past, but IIRC the unrolled attribute can also apply to while etc, this would limit it to for only.

Honestly I think I'd just drop the unused versions (unrolled and unrolled_none) since they've gained no users for their entire lifetime (sorta understandable since the former relies too much on the compiler and the latter makes sense only in some very constrained environments where usually, the related compiler flags are used globally instead).

Quoted 3 lines

Thanks,
Tzu-Ching

Thanks,
Olek

#7 lore raw
Quoted 26 lines · On Fri, 23 Oct 2026 15:42:28 +0200 Alexander Lobakin…

From: Tzu-Ching Yang <[email protected]>
Date: Thu, 22 Oct 2026 19:09:59 +0800

Quoted 23 lines · David Laight
Quoted 4 lines

Is it possible to make the syntax:

	unrolled_for(....)

Or is that made 'all hard and difficult' because he setting is sticky?

The pragma binds only to the loop that follows it, so it can go inside the macro:

#define unrolled_full_for(...) \
	__pick_unrolled(clang loop unroll(full), GCC unroll 65534) \
	for (__VA_ARGS__)

	unrolled_full_for (int i = 0, j = 7; i < 8; i++, j--)
		s += a[i] * b[j];

GCC 8 to 16 and Clang 17 to 23 unroll this fully, and the same works with break and continue in the body, nested loops and a loop inside another macro. The count form has the same shape:

	unrolled_count_for (4, int i = 0; i < n; i++)
		s += a[i];
Quoted 8 lines
Quoted 4 lines

As a fourth option, the _for forms would replace the four unrolled*
macros: no clash with identifiers, and the loop reads like for_each_*(); the cost is a new loop spelling at every user. Olek, Eric, would you prefer this over options 1 to 3 from my earlier reply?

I had considered this option in the past, but IIRC the unrolled attribute can also apply to while etc, this would limit it to for only.

Is that really useful?
Partially unrolling a variable iteration count loop most likely just trashes the I-cache.
(The unroll can't use simd instructions because they aren't normally available in kernel - ok, some cpu have a 'load pair'.) The only exception is very short loops where unrolling once can help because the cpu cant execute a single clock loop.
The compiler is likely to make a pigs breakfast of those loops so they end up in asm anyway - look at the xor parity code.

The blake code is really a special case where the massive unrolling lets a load of iteration-dependant values be treated as constants. (And I'm sure it can be coded so that they can be loaded and the loops rolled up without slowing the code down, I've tried a couple of times and failed - but there are enough clocks available for memory reads that it should be possible.)

Also while(foo) is just for(;foo;).

David

Quoted 13 lines

Honestly I think I'd just drop the unused versions (unrolled and unrolled_none) since they've gained no users for their entire lifetime (sorta understandable since the former relies too much on the compiler and the latter makes sense only in some very constrained environments where usually, the related compiler flags are used globally instead).

Quoted 3 lines

Thanks,
Tzu-Ching

Thanks,
Olek

#8 lore raw

From: David Laight <[email protected]>
Date: Fri, 23 Oct 2026 17:38:45 +0100

Quoted 38 lines
Quoted 26 lines · On Fri, 23 Oct 2026 15:42:28 +0200 Alexander Lobakin…

From: Tzu-Ching Yang <[email protected]>
Date: Thu, 22 Oct 2026 19:09:59 +0800

Quoted 23 lines · David Laight
Quoted 4 lines

Is it possible to make the syntax:

	unrolled_for(....)

Or is that made 'all hard and difficult' because he setting is sticky?

The pragma binds only to the loop that follows it, so it can go inside the macro:

#define unrolled_full_for(...) \
	__pick_unrolled(clang loop unroll(full), GCC unroll 65534) \
	for (__VA_ARGS__)

	unrolled_full_for (int i = 0, j = 7; i < 8; i++, j--)
		s += a[i] * b[j];

GCC 8 to 16 and Clang 17 to 23 unroll this fully, and the same works with break and continue in the body, nested loops and a loop inside another macro. The count form has the same shape:

	unrolled_count_for (4, int i = 0; i < n; i++)
		s += a[i];
Quoted 8 lines
Quoted 4 lines

As a fourth option, the _for forms would replace the four unrolled*
macros: no clash with identifiers, and the loop reads like for_each_*(); the cost is a new loop spelling at every user. Olek, Eric, would you prefer this over options 1 to 3 from my earlier reply?

I had considered this option in the past, but IIRC the unrolled attribute can also apply to while etc, this would limit it to for only.

Quoted 5 lines

Is that really useful?
Partially unrolling a variable iteration count loop most likely just trashes the I-cache.
(The unroll can't use simd instructions because they aren't normally available in kernel - ok, some cpu have a 'load pair'.)

In XSk xmit code, we have loops with the number of iterations being a variable (number of packets queued by the userspace program), but using unrolling by 8 packets helps there a lot if iterations are really independent from each other (except the counter itself). Pick a lower or bigger value and the performance (which can sometimes reach 90 Mpps if everything is optimized to the top) will drop, unrolled_full is not an option (not a compile-time constant), leave it without any manual unrolling and the compiler won't do anything ('cause not a compile-time constant).

Quoted 4 lines

The only exception is very short loops where unrolling once can help because the cpu cant execute a single clock loop.
The compiler is likely to make a pigs breakfast of those loops so they end up in asm anyway - look at the xor parity code.

Thanks,
Olek