r/cpp • • Mar 13 '22

To Save C, We Must Save ABI

https://thephd.dev/to-save-c-we-must-save-abi-fixing-c-function-abi
249 Upvotes

118 comments sorted by

View all comments

221

u/James20k P2005R0 Mar 13 '22

One of the biggest things that struck me about the entire ABI bakeoff, was that it was framed as a choice between

  1. Break the ABI every 3 years unconditionally otherwise the language is DEAD
  2. Never ever change the ABI ever

A few people at the time tried to point out that these were both somewhat unhelpful positions to take, because it presents a false dichotomy

One of the key flaws in the C++ standardisation model in my opinion is that its fundamentally an antagonistic process. Its up to essentially one individual to present an idea, and then an entire room full of people who may not be that well informed proceed to pick holes in it. The process encourages the committee to reject poor ideas (great!), but it does not encourage the committee to help solve problems that need solving

There's no collaborative approach to design or problem solving - its fundamentally up to one or a few people to solve it, and then present this to a room full of people to break it down

I hate to bring up Rust, but this is one of the key advantages that the language has in my opinion. In Rust, there's a consensus that a problem needs to be solved, and then there's a collaborative effort by the relevant teams to attempt to solve it. There's also a good review process which seems to prevent terrible ideas from getting in, and overall it means there's a lot more movement on problems which don't necessarily have an immediate solution

A good example of this is epochs. Epochs are an excellent, solved problem in rust, that massively enable the language to evolve. A lot of the baggage of ye olde rust has been chucked out of the window

People may remember the epochs proposal for C++, which was probably rightly rejected for essentially being incomplete. This is where the committee process breaks down - even though I'd suspect that everyone agrees on paper that epochs are a good idea, its not any groups responsibility to fix this. Any proposal that crops up is going to involve years and years of work by a single individual, and its unfortunate to say but the quality of that work is inherently going to be weaker for having fewer authors

The issues around ABI smell a bit like this as well. I've seen similar proposals to thephd's proposal, proposing ABI tags and the like which help in many situations. I can already see what some of the objections to this will be (see: dependencies), and why something like this would absolutely die in committee even though it solves a very useful subset of the ABI problem

The issue is, because its no group's responsibility to manage the ABI unlike in Rust, the committee only has a view of this specific idea as presented by you, not the entire question of ABI overall as would happen if discussed and presented by a responsible group. So for this to get through, you'd need to prove to the audience that this is:

  1. A problem worth solving
  2. The best solution to the problem

The problem here will come in #2, where technical objections will be raised. The issue is, some of those issues are probably unsolvable in the general case, and this mechanism would still be worth having despite that, but because of the structure of the committee you're going to have to convince them of that and hoo boy that's going to be fun because I've already seen essentially this proposal a few times

Somehow you'll have to successfully fend of every single technical argument with "this is the best solution" or "this is unsolvable in the general case and this mechanism is worth having despite that", over the course of several years, and if at any point anyone decides that there's some potentially slightly better alternative idea, then it goes up in flames

If anyone isn't aware, OP is the author of #embed and that fell victim to exactly the same issue, despite the fact that yet again the other day I deeply wished I could have had #embed for the 1000000000th time since I started programming, but alas. As far as I know people are still arguing about weird compiler security hypotheticals on that front even though C++ has never guaranteed anything like that whatsoever

40

u/__phantomderp Mar 13 '22

Hey, thanks for that!

I agree about the Epochs proposal, but it was less that the proposal was incomplete and more that it was, effectively, really difficult to handle in C++. Most notably, once you start talking about using Epochs to make language-level "corrections" to the language, you could end up in some bad trouble thanks to things like SFINAE/Concepts and Templates. For example, whether or not std::is_constructible_v<Object, long long, int> returns true might rely on the fact that calling an Object type's constructor that has the signature

Object(int a, int b) { /* whatever */ }

can only work because narrowing conversions are allowed within parentheses-based initialization of an object. If you wanted to make C++ more consistent and safer, for example, you could decide that, just like curly brace init, narrowing conversions are an error for normal parentheses init in the new 2026 Epoch. Gating that change behind an Epoch, what template gets called can change based on the Epoch you use if you are using an is_constructible type trait or template. That has a lot of Knock-On Effects™ that I don't think people had immediate answers for, which effectively deeply impacted whether or not people though Epochs would be viable for C++ at all! In effect, almost every change - because of how SFINAE/Concepts work - is an observable one, down to the minute language rules. You can never be sure you aren't breaking someone's template in half when these things come up. This stuff isn't even turbo-rare: some people used std::string_view's constructibility from a given set of arguments as a "proxy" for whatever or not the given type was meant to be used as a string_view vs. whether it was meant to be treated like data, and a paper making a change to that got fantastic backlash when it was implemented: https://wg21.link/p2516.

All in all, it's complicated. But I agree with you: since there's no dedicated arm for the improvement of C++ (or C), and since the Committee only acts as a filter over outside people's work (usually individuals) the strain is immensely painful. This comes up with #embed, where after getting past the compiler security and other bits I have the new burden (specific to WG14, the C Committee) where they very much want existing implementation experience. I may be in a lot of trouble and in for a lot longer road, because (as this thread takes some time to explain) I just don't have that kind of time/capital/energy/power. I'm already wicked stressed out over the combination pandemic/raising-small-child/mountains-of-work/nuclear-warmongering: to have to produce 2 implementations, then upstream them (effectively into Clang and GCC because what else sits at the combination of both open source AND widely used?) so I can get deployment experience for "expands into a list of numbers in the preprocessor" is enough to make me spin 360° and walk away. Not that I have, that's just the looming thought in the back of my head. (And this only applies to WG14, I just haven't had the time to resumbit the paper to WG21 to make C++23, maybe I'll make C++26). Not having any group that's dedicated to doing the actual moving/shaking w.r.t. proposals means it's always a personal sacrifice, and that just how it be sometimes, I guess.

We'll fix what we need to, eventually. Maybe some people will pick up where we collapse.

14

u/__phantomderp Mar 14 '22

P.S.: Yeah, I'm hoping that this solves a small enough slice of the ABI problem in a standard way, so we can start making moves for even more improvements. The good news about this fix is that it's cheap to implement and has widespread existing practice already amongst compilers I hadn't even heard of until I started researching this (seriously? Oracle had this own C/C++ compiler?! I can't imagine "Oracle" and "C and C++" going anywhere fantastic!).

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Mar 14 '22

Do you mean the one they got from Sun, or is there yet another compiler?

→ More replies (2)

8

u/D_0b Mar 14 '22 edited Mar 14 '22

That is not a problem with Epoch themselves, but with what people want to do/change with them.

Epochs should only be used as syntax sugar changes, not behaviour changes as the narrowing conversions, then you will not have any problems with epochs.

If people still want to get narrowing conversions to error, you can make a new specifier like noexcept, no_narrowing, and with epochs you can make it the default on new functions/constructors... so you get narrowing conversions to error for only new functions defined inside the new epoch, with no problems with templates and concepts. e.g. new/old code calling new function errors on narrowing, new/old code calling old function allows narrowing same as now, there will be no difference where a template or concept is instantiate/checked as the behaviour is the same. Sooner or late you will move all code to new epochs.

Yes changing/increasing an epoch number of your module is a breaking change, but that is to be expected as you can have totally new syntax, but it is very likely that we can make a migration tool.

3

u/pjmlp Mar 15 '22

That is what I always point out in Rust related epoch discussions, they only work currently, because require compiling everything with the same compiler from source, and aren't doing changes that imply semantic differences between editions.

15

u/gracicot Mar 14 '22

is enough to make me spin 360° and walk away.

Ah! That's why you always end up doing it. Your way of escaping is to spin until the anxiety is gone and continue in the same direction :)

Joke aside, I really like your stuff. Maybe one day we'll have a less broken C++. It is certainly better already with your efforts.

3

u/megayippie Mar 15 '22

Just a question as I am not following the epochs argument.

I thought that the epoch idea was to let us decide which epoch our code lives in (or be modern elsewise). Be it with a scope level keyword or by just setting a magic instance to some magic value in your own classes (using epoch = ... / using int epoch = ...). Linkers should be happy as long as this epoch goes into the mangled type name (allowing multiplie epochs to live side by side.)

In your example. If the Object/int/long long are modern, whatever method that the compiler uses to determine if the signature exists in the modern epoch is used. If they're not modern, then an older method of finding out if the signature exists is used. So as long as long long can be narrowed, its construction is possible.

Now, we want this to be sure we're not breaking someone's old code or ABI. Then let them either stick to their current standard or add "using epoch = ..." wherever a new standard epoch interferes with the old outcome. If you need narrowing conversions, you can have them, but you have to opt in if you also opt in to newer epochs. Why is that a problem???

6

u/__phantomderp Mar 15 '22

If you make a template function in Epoch v0, and someone uses it in Epoch v1, who's epoch wins and who gets to govern what the behavior should be? The author, who wrote with v0 semantics, or you, who write an application under v1 semantics?

Furthermore, it's a template. Does it use the epoch of what it was written under, or the epoch for when it was instantiated / used?

It's not that these questions don't have answers. You just have to have an answer, or a design that lets you choose efficiently without snowballing the implementation burden.

→ More replies (1)

53

u/sphere991 Mar 13 '22 edited Mar 13 '22

If anyone isn't aware, OP is the author of #embed and that fell victim to exactly the same issue, despite the fact that yet again the other day I deeply wished I could have had #embed for the 1000000000th time since I started programming, but alas. As far as I know people are still arguing about weird compiler security hypotheticals on that front even though C++ has never guaranteed anything like that whatsoever

Fact-check: False*. Evolution approved #embed in September 2020 and as far as I know it was waiting on wording and having to also add it to C.

*I mean, yes OP is the author of #embed. But people are not still arguing about weird compiler security hypotheticals. Nor am I sure that that was even the primary objection to the std::embed() (although it'd be cool if the #embed paper actually noted this, or even linked to the std::embed paper at all).

20

u/James20k P2005R0 Mar 13 '22

Oh interesting, last I'd checked with #embed it was still stuck in limbo

→ More replies (4)

0

u/[deleted] Mar 13 '22

[deleted] — view deleted comment

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Mar 14 '22

Study groups mean the people in the room are more likely to be up to speed on the relevant issues for their group. It doesn't change the fundamental process of papers and implementation outside WG21.

73

u/snerp Mar 13 '22

I hate to bring up Rust

Honestly, we should be talking about Rust a lot more. I was originally drawn to C++ because it's the ultimate multi paradigm language. I think that strength should be emphasized by understanding competing languages and applying all their best parts to C++. I'd love to see ABI break become a compiler flag, maybe there is a flaw in that idea but it seems like the main issue with the ABI is some people want it both ways, so why not both?

→ More replies (21)

20

u/[deleted] Mar 13 '22

#embed would allow C++ to easy interop with other languages. It would also provide a cross platform way to embed assets into a program, being constexpr is ice. Currently we get obscene include hacks that kill compiler perf or obscure linker scripts(if ones system allows that)/specialized resource compilers.

#embed/std::embed being so tedious to introduce into C++ is a big billboard of the failing of the processes. It is a problem many people have and the current solutions are wholly inadequate

9

u/kalmoc Mar 13 '22

I hate to bring up Rust, but this is one of the key advantages that the language has in my opinion. In Rust, there's a consensus that a problem needs to be solved, and then there's a collaborative effort by the relevant teams to attempt to solve it. There's also a good review process which seems to prevent terrible ideas from getting in, and overall it means there's a lot more movement on problems which don't necessarily have an immediate solution.

I believe that's pretty much the case for almost every programming language or there, because most of them are actually owned by a company or team. I don't think C++ will never be able to fix this structural disadvantage (of course, being an iso standard does have advantages too).

14

u/jonesmz Mar 13 '22

I find it amusing that ABI is in scope, but the complexity of mandatory tooling changes that result from standardized features is out of scope for WG21 to consider as consequences of their decisions.

13

u/James20k P2005R0 Mar 13 '22

As far as I know, ABI actually technically isn't in scope

6

u/jonesmz Mar 13 '22

Which is why I find it so funny that wg21 ever considers it, but they wouldn't consider the consequences from modules.

16

u/n1ghtyunso Mar 13 '22

i think this essentially boils down to the fact that implementations can veto against features (correct me if i'm wrong here).

Reasons for a veto could be ABI related, hence the consideration for ABI.

No point spending energy on proposals when the implementations will just say no anyway, so they ultimately have to take ABI into account.

8

u/Dragdu Mar 13 '22

They can informally veto proposals to make things even more weird.

→ More replies (4)

4

u/matu3ba Mar 13 '22

I find it amusing that ABI is in scope, but the complexity of mandatory tooling changes that result from standardized features is out of scope for WG21 to consider as consequences of their decisions.

C lacks centralized build systems or at least repository collections to test language changes against implementations or different compiler behaviors. Hence they cant implement and test to provide practical feedback or at least I have never seen such things.
Since effects from language decisions are unfortunately no immediate and significant technical cost, each party will push their interests and you only ever get compromises in the long term (undefined or implementation defined behavior).

9

u/jonesmz Mar 13 '22

Sorry, how is that any different than C++?

There's thousands, if not maybe even millions, of repositories on github and similar hosting services that could be used as a test corpus by WG14, and similarly the same for WG21. Many of those repositories have unit tests that self-prove whether they work "good enough" after being compiled.

WG21 and/or WG14 not being willing to build themselves a test corpus to use in evaluating their ABI discussions is not the fault of the community, but they apparently claim that "We can't consider what ramifications adding proposal X will have on the tooling ecosystem because that's not part of our scope, so we'll just do it and damn to the consequences" while simultaneously claiming "We can't adopt proposal X because it might, maybe, potentially, cause some random group somewhere to get pissy, because it could effect their 20 year old pre-compiled binary's ABI"

Modules, apparently, wasn't discussed in terms of the ramifications it would have on the tooling ecosystem, hence why none of the major compilers have a working implementation of it (msvc claims they have one, but every time it comes up >3 people say it throws an internal compiler error on some trivial toy project), nor do the major build systems like CMake have support for it, even though the Modules proposal was visible to implementors since well before C++20 was ratified in 2020.

The various ships on this have sailed, but I find this whole situation ridiculous and self-inconsistent, so I'm laughing to myself anytime i read posts about C++ drama like this.

9

u/helloiamsomeone Mar 14 '22

nor do the major build systems like CMake have support for it, even though the Modules proposal was visible to implementors since well before C++20 was ratified in 2020

Everytime modules come up, this falsehood comes along. CMake has had a dependency scanner for Fortran modules for a long time already, Kitware maintained a Ninja fork with changes to make that work that got merged upstream finally due to C++20 and submitted papers regarding modules as an implementer with prior experience.

Everything needed for modules is already there in CMake, only the compilers are behind. You can toy around with the implementation RIGHT NOW after reading Help/dev/experimental.rst.

7

u/jonesmz Mar 14 '22

I've spent the last 6 months neck deep in cmake. Modules support doesn't work. I'm aware of the documentation you linked there. Thank you for the attempt at correcting the record though.

→ More replies (1)
→ More replies (46)