Skip to content

Commit 474c901

Browse files
committed
give up on gcc ilog2() constant optimizations
gcc-7 has an "optimization" pass that completely screws up, and generates the code expansion for the (impossible) case of calling ilog2() with a zero constant, even when the code gcc compiles does not actually have a zero constant. And we try to generate a compile-time error for anybody doing ilog2() on a constant where that doesn't make sense (be it zero or negative). So now gcc7 will fail the build due to our sanity checking, because it created that constant-zero case that didn't actually exist in the source code. There's a whole long discussion on the kernel mailing about how to work around this gcc bug. The gcc people themselevs have discussed their "feature" in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72785 but it's all water under the bridge, because while it looked at one point like it would be solved by the time gcc7 was released, that was not to be. So now we have to deal with this compiler braindamage. And the only simple approach seems to be to just delete the code that tries to warn about bad uses of ilog2(). So now "ilog2()" will just return 0 not just for the value 1, but for any non-positive value too. It's not like I can recall anybody having ever actually tried to use this function on any invalid value, but maybe the sanity check just meant that such code never made it out in public. Reported-by: Laura Abbott <[email protected]> Cc: John Stultz <[email protected]>, Cc: Thomas Gleixner <[email protected]> Cc: Ard Biesheuvel <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4977ab6 commit 474c901

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

include/linux/log2.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
#include <linux/types.h>
1616
#include <linux/bitops.h>
1717

18-
/*
19-
* deal with unrepresentable constant logarithms
20-
*/
21-
extern __attribute__((const, noreturn))
22-
int ____ilog2_NaN(void);
23-
2418
/*
2519
* non-constant log of base 2 calculators
2620
* - the arch may override these in asm/bitops.h if they can be implemented
@@ -85,7 +79,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
8579
#define ilog2(n) \
8680
( \
8781
__builtin_constant_p(n) ? ( \
88-
(n) < 1 ? ____ilog2_NaN() : \
82+
(n) < 2 ? 0 : \
8983
(n) & (1ULL << 63) ? 63 : \
9084
(n) & (1ULL << 62) ? 62 : \
9185
(n) & (1ULL << 61) ? 61 : \
@@ -148,10 +142,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
148142
(n) & (1ULL << 4) ? 4 : \
149143
(n) & (1ULL << 3) ? 3 : \
150144
(n) & (1ULL << 2) ? 2 : \
151-
(n) & (1ULL << 1) ? 1 : \
152-
(n) & (1ULL << 0) ? 0 : \
153-
____ilog2_NaN() \
154-
) : \
145+
1 ) : \
155146
(sizeof(n) <= 4) ? \
156147
__ilog2_u32(n) : \
157148
__ilog2_u64(n) \

tools/include/linux/log2.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
#ifndef _TOOLS_LINUX_LOG2_H
1313
#define _TOOLS_LINUX_LOG2_H
1414

15-
/*
16-
* deal with unrepresentable constant logarithms
17-
*/
18-
extern __attribute__((const, noreturn))
19-
int ____ilog2_NaN(void);
20-
2115
/*
2216
* non-constant log of base 2 calculators
2317
* - the arch may override these in asm/bitops.h if they can be implemented
@@ -78,7 +72,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
7872
#define ilog2(n) \
7973
( \
8074
__builtin_constant_p(n) ? ( \
81-
(n) < 1 ? ____ilog2_NaN() : \
75+
(n) < 2 ? 0 : \
8276
(n) & (1ULL << 63) ? 63 : \
8377
(n) & (1ULL << 62) ? 62 : \
8478
(n) & (1ULL << 61) ? 61 : \
@@ -141,10 +135,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
141135
(n) & (1ULL << 4) ? 4 : \
142136
(n) & (1ULL << 3) ? 3 : \
143137
(n) & (1ULL << 2) ? 2 : \
144-
(n) & (1ULL << 1) ? 1 : \
145-
(n) & (1ULL << 0) ? 0 : \
146-
____ilog2_NaN() \
147-
) : \
138+
1 ) : \
148139
(sizeof(n) <= 4) ? \
149140
__ilog2_u32(n) : \
150141
__ilog2_u64(n) \

0 commit comments

Comments
 (0)