Messages in this thread | | | From | Sergey Senozhatsky <> | Date | Sat, 16 Mar 2019 16:18:41 +0900 | Subject | Re: [PATCH] tcp: don't use __constant_cpu_to_be32 |
| |
On (03/16/19 14:19), Sergey Senozhatsky wrote: > > See my recent commit a0517a0f7ef23 ("selftests/bpf: use > > __bpf_constant_htons in test_prog.c") where compiler was not smart > > enough to figure it out (you can google similar issues for GCC < 4.8). > > Allow me to disagree. > > The error in a0517a0f7ef23 says > > error: implicit declaration of function '__builtin_bswap16' > > which doesn't sound like "the compiler was not smart". > > Let's look at __swab16 > > 100 #ifdef __HAVE_BUILTIN_BSWAP16__ > 101 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x)) > 102 #else > 103 #define __swab16(x) \ > 104 (__builtin_constant_p((__u16)(x)) ? \ > 105 ___constant_swab16(x) : \ > 106 __fswab16(x)) > 107 #endif > > We can call __builtin_bswap16() only when the compiler has that builtin, > which is what #ifdef __HAVE_BUILTIN_BSWAP16__ for. > > But this is not what tools/testing/selftests/bpf/bpf_endian.h does > > # define __bpf_ntohs(x) __builtin_bswap16(x) > # define __bpf_htons(x) __builtin_bswap16(x) > > So I sort of suspect that what should have been done was that > __HAVE_BUILTIN_BSWAP16__ ifdef, just like what include/uapi/linux/swab.h > does.
E.g.
#define ____const(x) printf("const %d\n", (x)) #define ____noconst(x) printf("noconst %d\n", (x)) #define const_test(x) (__builtin_constant_p(x) ? ____const((x)) : ____noconst((x)))
The compiler should optimize out !__builtin_constant_p (dead code elimination phase - dce), if it can compile the code in the first place.
So if we replace ____noconst with
#define ____noconst(x) printf2("noconst %d\n", (x))
then compiler wouldn't be able to compile the code and thus wouldn't be able to perform dce.
__builtin_constant_p, unlike `#if 0', is not handled by the preprocessor.
Verified with gcc 8.3, gcc 4.9, gcc 4.7 at compiler explorer website https://gcc.godbolt.org/ - GCC is smart enough to figure out __builtin_constant_p and do dce, given that it compile the code.
-ss
| |