190 points · by immibis · 2y ago · 114 comments
The macros now do type-safe comparisons that work correctly with combinations of different argument types where this is possible, work in a constant context (eg defining array bounds), work correctly in the face of implicit type coercion and include a 3-way min and max that have these same desirable properties (same as the two way min and max).
The problem(s) are it’s a bit slow to compile and the preprocessor expansion (although not necessarily the final generated assembly - didn’t see anyone saying that) is a bit bloated.
So when you make a “why don’t they just…. ?”-suggestion, make sure your suggestion is at least as good as what they have now in terms of the desirable functionality and correctness and then tackles the actual problems in some way. I’m not sure all of the suggestions I have seen here and in the lwn comments succeed at meeting those two criteria.
If somebody proposed a new extension and got llvm and gcc to both implement it today, they would still need something to work on old compilers. The oldest GCC supported by Linux is 10 years old nearly.
The thing they want seems perfectly reasonable to me and wouldn’t only benefit the kernel.
I think implicit type conversion is a mistake, perhaps one of the few true design flaws in C. Languages like haskell and rust went with explicit conversions which is probably a better idea overall, even if it does increase the code verbosity a bit. C++ instead doubled down and added many more ways for implicit conversions to happen.
but also allows you to turn them off for objects, thankfully.
If you would then say "why doesn't min/max just implement a switch on each primitive type", I think on some level it does just do that.
Of course there are others than C. But not many suitable for writing a kernel. And no obvious choice for what Linux could do today. Of course they are starting with Rust, but nobody can predict when that will make the last C macro unnecessary. Unless your prediction is never...
Even Linus accepting Rust is kind of interesting, because his C++ rants also apply to plenty of Rust code bases.
(void) (&_x == &_y);
Is this... a check for type-compatibility? I don't think actually produces a compilation error if the types are incompatible. cgci = XCreateGC(
display,
window,
GCForeground | GCBackground | GCFont,
&(XGCValues) {
.foreground = ccwhite,
.background = ccblack,
.font = cfont->fid,
}
);
[1] C++ requires the fields to be initialized in order; C doesn't have this restriction. $ cd /usr/src/linux
$ rg ' class;'
vmlinux.h
8541:struct class;
13515: long unsigned int class;
16416: unsigned int class;
16917: u8 class;
17351: u8 class;It's not impossible, but the Linux kernel is quite a complex project.
You'll be happy to know that GCC happily compiles C++ then! No need to switch to MSVC.
I'm fact GCC itself successfully switched to C++ from C a few years ago.
struct S {
int foo[max(10, 15)];
}
Isn’t possible in C, macros are the only option.So even if you were to try to use _Generic in a macro to handle type correct dispatch you would not be able to use that macro in many of the contexts it is needed.
Honestly constexpr is something that would really help C, and does not need to bring in any other c++ features. Although I guess in this case the lack of the full template and such features set would mean matching the kernel requirements would still require a macro+_Generic to adopt.
These macros are now so complex that they're reluctant to touch them. Seems like there is a clear need for some thorough tests here? This is exactly the sort of thing that is eminently testable.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char**argv){
puts("hello, world");
return EXIT_SUCCESS;
}
came to something ridiculous on the order of 100k lines.(tested with `gcc -E - | wc -l`)
However, hygienic macros that fix entire classes of issues (including this) is a more compelling argument.
Not to say that it makes the case, only that the strawman you wrote is not good.
Not necessarily.
The preprocessor code here picks up the original source, and blows up the initial code (which is about "min3(long_a, long_b, long_c)") to 47 MB of code. no fancy stuff, just 47MB of C-Code on the disk. That's a lot of code the compiler then has to parse and handle.
If hygienic macros are a first-class citizen in the compiler, the compiler parses the original macro code once and then just modifies it in-memory. There is no reason to write 47MB of code somewhere and read it back, this would just happen as an AST modification in memory.
But that is also a much smaller reason. First-class macros allow the compiler to reason based off of the types and structure of the macro inputs. You don't have to guess if something is constant, bounded, unbounded and such. Strong types can enforce this safely and macros and optimization can use these strong guarantees. And sufficiently strong type information can open doors for far, far more powerful optimizations overall.
Just for the record - I'm fully aware why the kernel is where it is, and why it will stay there, but there is far improved compiler and language theory from there.
Given the number of preprocessor hacks used in the kernel, and the amount of GCC-specific behavior that the codebase depends on, it seems like they are already halfway there.
Stop using tiny hammers and get a big one.