Live data from Hacker News

C++ in the Linux Kernel

threatstack.com

71–80 of 102 posts

Re: C++ in the Linux Kernel

#71
post #22
post #5

Lol, part of me likes the effort taken just because, but the kernel devs _really_ do not want C++. One hint: "struct class" https://elixir.bootlin.com/linux/latest/source/include/linux...

Wasn't there a period recently — around 1995 or so — when the Linux kernel had to be compiled with a C++ compiler?

Quoting from http://vger.kernel.org/lkml/#s15-3 :

"In the dark old days, in the time that most of you hadn't even heard of the word "Linux", the kernel was once modified to be compiled under g++. That lasted for a few revisions. People complained about the performance drop. It turned out that compiling a piece of C code with g++ would give you worse code. It shouldn't have made a difference, but it did. Been there, done that."

Re: C++ in the Linux Kernel

#72
post #53
post #44

Earlier quoted context omitted.

> Assuming I don't care about ABI stability, what's the fastest calling convention? I'd assume a modern optimizing compiler will, in situations where it's permitted, create completely novel calling conventions depending on the situation. Whole program optimization is one area you might see this.

The compiler tends to be limited in how it can change calling conventions by external visibility of the functions. Generally if you compile a function down to an object file the compiler will want to make that object file linkable with any other object files importing that symbol properly. Whole program optimization gives the compiler some ways around that. I am not sure how much freedom it gives the compiler.

With LTO the compiler should be free to fudge the calling convention for most calls even between translation units.

Here is GCC doing that optimization for a static noinline function: https://godbolt.org/z/cn6Wz9Kvn

Similarly, compilers can also clone functions if it makes sense to propagate constants from call sites into the function. Example: https://godbolt.org/z/59z6xT75n

I'm sure there is more room for improvement. A perfect compiler would always optimized programs as a whole and only regard function boundaries as hints at best. In practice, you have to keep complexity in check somehow.

Re: C++ in the Linux Kernel

#73
post #66

Earlier quoted context omitted.

> that would be very weird on Linux. The x86_64 linux ABI mandates that the first arguments go on registers afaik (I'm assuming x86_64 here since the post mentions linux distros which are overwhelmingly x64). What compiler would default to a pure stack-based calling convention ? Certainly not GCC or clang, no ? The System V i386 ABI passes parameters through the stack. Perhaps that is what the author is referring to,…

> The System V i386 ABI passes parameters through the stack. No. The C/C++ ABI is quite uniform across architectures. The first 1..N (N is ISA dependant) parameters that can fit into a CPU register are passed via registers. The first input parameter that _can't_ fit into a register (e.g. a structure passed by value) is pushed onto the stack, with every other following parameter being pushed onto the stack as well. N+…

> The C/C++ ABI is quite uniform across architectures.

How can it be? What about an architecture without conventional registers? And for example I work on an implementation of C/C++ that logically uses the heap for its ABI.

Re: C++ in the Linux Kernel

#74
post #70

Earlier quoted context omitted.

I understand that for C++ programmers "That's possible" versus "That's a good idea" is a distinction without a difference, however for the rest of us the fact you can use templates as much as you like in, say, Windows drivers, does not magically mean it's a good idea to write complex templated code in Windows drivers. The constraints in /kernel like forbidding exceptions are because otherwise they (Microsoft) need to…

Here is another example, running C++ straight on car firmware free of Linux politics via AUTOSAR certification standard. https://www.parasoft.com/blog/breaking-down-the-autosar-c14-... But what do they state specifically? Ah, right. > "The document allows in particular the usage of dynamic memory, exceptions, templates, inheritance and virtual functions." https://www.autosar.org/fileadmin/user_upload/standards/adap..…

It definitely feels like we're talking past each other. I keep telling you why people think it's a bad idea, and you keep showing that you're allowed to do it anyway. We know. That's the difference between impossible and a bad idea.

Re: C++ in the Linux Kernel

#75

> A first-year computer science student can tell you that the arguments get pushed onto the stack. In other words, a call to this 3GL function results in the following assembly pseudo code Are people this ignorant when it comes to C/C++ or any systems language? ABI & calling conventions were introduced early in my C & C++ textbooks (age 13 btw, not even close to college years).

I think it would depend on which system you were introduced into. Also 99% sure in my classes in the mid 90s they taught stack push. Which made sense as registers were pretty valuable. It was not until RISC came along, and register renaming, that you could consider 'wasting' them on passing args in the general case. In the 'DOS'/'Win16' world calling conventions were all over the place. You could get into trouble real quick if you did not pay attention to those calling convention modifiers. Especially if you were using libs from different compilers. In the linux world where you can control the whole stack it is easier to say 'this way and if you stray away from it, good luck'.

Small sample of the remnants of that in the DOS world. https://docs.microsoft.com/en-us/cpp/cpp/argument-passing-an...

Re: C++ in the Linux Kernel

#76
post #71
post #22

Earlier quoted context omitted.

Wasn't there a period recently — around 1995 or so — when the Linux kernel had to be compiled with a C++ compiler?

Quoting from http://vger.kernel.org/lkml/#s15-3 : "In the dark old days, in the time that most of you hadn't even heard of the word "Linux", the kernel was once modified to be compiled under g++. That lasted for a few revisions. People complained about the performance drop. It turned out that compiling a piece of C code with g++ would give you worse code. It shouldn't have made a difference, but it did. Been there, d…

I wonder if it is still true. C++ compilers have come a long way. (as have C compilers). C++ is 99% a superset of C, I'm not sure how much of that last is used in the kernel, so it might be too much effort, but C++ is in a few cases stricter than C in ways that compilers can use to optimize. Many C programs run faster when compiled in C++ these days.

If there is a difference (either way) I'd expect it to be something you can measure, but not something you would notice in the real world on one computer. (though at google scale it probably shows up)

Re: C++ in the Linux Kernel

#77
Valueless Article. Please stop posting these sort of articles which have no information content.

The article is merely a rant because the author doesn't have much of an idea of how C++ actually works. Merely knowing the syntax doesn't make one a "C++ programmer" and this is even more true when you are messing around in the Kernel. The article contains no specifics only general statements making me think this was put up to just be a "hit piece".

Re: C++ in the Linux Kernel

#78
post #22
post #5

Lol, part of me likes the effort taken just because, but the kernel devs _really_ do not want C++. One hint: "struct class" https://elixir.bootlin.com/linux/latest/source/include/linux...

Wasn't there a period recently — around 1995 or so — when the Linux kernel had to be compiled with a C++ compiler?

Why was that the case?

Re: C++ in the Linux Kernel

#79
post #69
post #66

Earlier quoted context omitted.

> The System V i386 ABI passes parameters through the stack. No. The C/C++ ABI is quite uniform across architectures. The first 1..N (N is ISA dependant) parameters that can fit into a CPU register are passed via registers. The first input parameter that _can't_ fit into a register (e.g. a structure passed by value) is pushed onto the stack, with every other following parameter being pushed onto the stack as well. N+…

> No. The C/C++ ABI is quite uniform across architectures. The first 1..N (N is ISA dependant) parameters that can fit into a CPU register are passed via registers. Here is the "System V i386 ABI" mentioned above: https://refspecs.linuxfoundation.org/elf/abi386-4.pdf (from https://refspecs.linuxfoundation.org/ ). It clearly passes all arguments on the stack, and none on registers ("Function Calling Sequence" starting…

You are not incorrect, and I shall ruminate on why I had thought that the SysV ABI on i386 used %rax ÷ %rex as input function parameters without having to resort to the use -mpregparm. Thanks for the correction.
Post reply on HN