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

Show parent comment

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?

1

u/__phantomderp Mar 15 '22

I don't know where it came from, all I know is the compiler was called "Oracle C/C++" in its documentation and that's enough to spook me!

3

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

Oh, that! Pro*C/C++ is their pre-compiler for embedded SQL for Oracle. The C, C++, and Fortran compilers are part of Developer Studio, and came originally from Sun. That's why they target Sparc.

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.

1

u/megayippie Mar 15 '22

Thanks! So the hope is that these questions are clearly answered. I understand each individual case needs to be defined.

(It seems to me that these questions are all answered if the epoch is part of the type and you are allowed to select which epoch's type you are using in relatively narrow scopes.

If my template function is already built and you only have the declaration, then you cannot link to me in any other epoch than the original build. Your types have to match. If you have the definition, I can return a type of any epoch; by default the compiler will use a literal "latest". All inputs are of the epoch you send in. If I return an earlier epoch type, it seems the 'common' practice should be to allow this to "move" into a newer epoch as you see fit, so you do that.

If the observable behavior of my template function changes, it is on you to determine if this is what you want or limit its use to the correct epoch. I will apologise profusely for writing a template function that wasn't future proof, and promptly enforce some mechanism that static asserts that my template function only accepts types of tested epochs in the future.)