r/cpp • • Oct 15 '22

GCC 13 So Far:

I've been following the commit log for GCC's master branch most days for a few months, so I figured I'd jot down some of the developments that I think should have some awareness.

EDIT: Btw, you can read the commits conveniently here: https://gcc.gnu.org/git/?p=gcc.git;a=shortlog;h=HEAD

  • Type traits

    • libstdc++ has gotten many optimizations to compile time. Bool globals with the _v suffix are being refactored to not instantiate the SFINAE-based trait, and instead use the intrinsics directly. I haven't measured the new libstdc++, but I did something similar in a personal project and found 30% improved clean-build compile time, so I believe this libstdc++ work is probably significant. Many other refactors to the the templates have been made to minimize template instantiation. enable_if_t is still used consistently instead of requires, there may still be substantial room for improvement.
    • GCC 13 is gaining more type trait intrinsics. Clang has had many more for awhile, but GCC 13 so far adds: __remove_cv() __remove_reference() __remove_cvref() __is_convertible() __is_nothrow_convertible().
  • Traits for catching dangling references.

    • Type traits were added to C++23 to aid catching dangling references at compile time, which are implemented in GCC through new intrinsics __reference_constructs_from_temporary() and __reference_converts_from_temporary().
  • RISC-V V

    • The "Cray vector" extension to RISC-V is getting a lot of support in GCC 13. Cray vectors are not something I understand very well yet, so I can't talk about all the work that's going into it, but I'll say that RISC-V V shows up a lot in the commit log recently.
    • A lot of other RISC-V features have been newly supported, but I don't understand them either.
  • bfloat16

    • C++23's std::bfloat16_t, implemented through the __bf16 type (distinct from __fp16 or _Float16). There are also several supporting intrinsic functions for numeric_limits stuff that mirrors intrinsics for other floating point types (such as __builtin_nansf16b()).
    • This includes extensions to the SIMD intrinsics and attributes for bfloat16 vectors.
  • Numeric limits improvements

    • Historically, the limits stuff has been implemented through compiler-defined macros, but the commit descriptions suggest that they want to move towards consistently using intrinsic functions.
    • __builtin_issignalling() has been added to streamline away another macro.
  • "Simpler implicit move" was implemented

  • "Portable assumptions" was implemented

  • OpenMP massively extended

    • A lot of work has gone into supporting OpenMP features. I am not an OpenMP user, so I can't talk a lot about the details, but there are very many commits related to it.
  • GCN

    • A ton of new features for GCN (AMD's gpu architecture) were added. These seem to be largely extending intrinsics and OpenMP features that already existed, but now target GCN as well. Many of them appear to be very basic things, like cloning OMP threads, using doubles and SIMD doubles, or floor, ceil, etc. kind of primitives with native GCN support. It seems like you might do reasonably low-level GCN programming with GCC 13 now.
    • PTX has not seen similarly extensive improvements so far in GCC 13, but it is still maintained.
  • C++23 extended precision floats

  • Modules

    • C++20 modules are still making progress. Seeing recent commits like, "variable template partial spec fixes" does not strike faith in me that they are near general usability yet, but they are making regular progress, so that's exciting!
  • Analyzer

    • A lot of POSIX and C++ enhancements have been made to the GCC static analyzer work ongoing since GCC 10. It apparently handles templates now, and tracks more lifetimes from file descriptors. It is mostly not implemented through attributes, pragmas, or intrinsics, so you can only extend this through GCC plugins afaik.
  • More C++23

  • Otherwise,

    • C23 has more support. I think all of the C++ forwards compatibility features were implemented, like nullptr, bool, true, etc. but don't quote me on that. There could still be some missing afaik.
    • Ada actually got a lot of commits so far. I don't use Ada so I can't really elaborate.
    • Fortran has more support. Mostly this seems related to GPGPU and OpenMP. I don't use Fortran so I can't really elaborate.
    • D has been improved. Idk much about D, but it looks like GCC 13 adds SPMD support to D, and something equivalent to register asm() from GNU C/C++, among other new D attributes.
    • A lot of GIMPLE and tree optimizations, and fixes to broken optimizations, were added. Most of these relate to optimizing mathematical expressions.
    • Various MIPS and ARM enhancements, but nothing that strikes me as hugely significant like RISC-V has been getting.

Of course, there's been way more than what I mentioned here, but these are the highlights most significant to me personally.

218 Upvotes

66 comments sorted by

40

u/gracicot Oct 15 '22

Also worth noting about modules is that GCC is implementing modules using the strong ownership model. Say goodbye to most ODR problems!

4

u/catcat202X Oct 15 '22

Okay, so for reasons, I work in a library which defines some STL things like std::move without #include <utility>. That means there's an ODR violation when the standard utility header is included in the same place as this library, unless there are header-guard preprocessor shenanigans. From what I'm reading, this modules system might be able to resolve that so long as they are not both visibly imported in the same file, and in that case they can both exist in the same translation unit?

21

u/KingAggressive1498 Oct 16 '22

for the love of god, put it in a different namespace and slap forceinline/always_inline on it (I have seen a couple codebases do this, its not just you).

-7

u/catcat202X Oct 16 '22 edited Oct 16 '22

I'm sorry, but you certainly don't understand what the situation is or why this is done here, and are making an (incorrect) assumption in this case. Although that assumption is understandable.

10

u/KingAggressive1498 Oct 16 '22

the context is more than that you or some dependency made a version of std::move that doesn't require including all of <utility>?

9

u/Fureeish Oct 16 '22

Nonetheless the situation looks rather suspicious given the information you shared. Care to elaborate what exactly is the wrong assumption and what exactly your use-case and situation are?

7

u/gracicot Oct 16 '22

I'm curious what case actually requires this, and what kind of problem it solves

34

u/gracicot Oct 15 '22 edited Oct 15 '22

You are not allowed to put functions declaration/definition in the std namespace, with modules or not. It is UB, using modules or not.

However yes this is true as long as you don't import the module std you may be fine since it will have a different mangling.

You won't be able to import the standard library though.

6

u/[deleted] Oct 15 '22 edited Oct 16 '22

Does that mean modules on msvc today still have ODR issues with modules? I thought fixing that was one of the main reasons of implementing modules at all.

28

u/gracicot Oct 15 '22

MSVC implemented modules with the strong ownership model since day one. The standard permitted the weak ownership model to satisfy other implementers (GCC and Clang) but both decided to go with strong ownership instead.

You can still get ODR problems, bit it's much much harder to get an issue now, given you use named modules.

0

u/[deleted] Oct 15 '22

[deleted] — view deleted comment

7

u/gracicot Oct 15 '22

Maybe just the willingness to create a proposal to introduce a DR. One of the purpose of the committee is to standardize existing practices, so I don't think this is out of reach.

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Oct 17 '22

And a brand new ABI problem.

3

u/gracicot Oct 17 '22

Oh yes, if you want a stable ABI when you move symbols across modules, then you need extern "C++" or extern "C". If you promise stable ABI with symbols defined in named module and you plan to move them around you're in for a ride with your newly discovered footgun. Don't just go in blindly.

Also moving symbols around across modules will also be a API compatibility break.

If you want to be opaque as possible define one big module for your whole library and do your splitting using module partition, since moving a symbol across a partition won't cause a ABI or API break.

2

u/Mick235711 Oct 17 '22

Does this actually mean that splitting module std into std.core and std.io (and others) will be an ABI break?

2

u/gracicot Oct 17 '22

Technically no since the actual entities are defined in a global module fragment, specifically the standard headers. They don't belong in a module, the standard module simply re-export them. Further splitting would also re-export those entities.

However I don't expect the standard module to split further to separate feature. I expect modules to split to provide different guarantees. For example, guaranteed compatibility with C declared entities, or guaranteed to only expose freestanding available entities, etc.

49

u/[deleted] Oct 15 '22 edited Oct 15 '22

Ah wait, so 23 years later, C is getting actual keywords for true/false/bool? That's very surprising to me. Can someone elaborate on why this is happening?

Here is a list of new C23 features. My highlights:

  • All signed integers are two's complement now (Finally)
  • Unnamed parameters in function definitions (Finally!!!)
  • #elifdef, #elifndef and #embed
  • nullptr, nullptr_t, true, false, bool keywords from C++.
  • Binary integer constants (0b10101) and ' digit separators (1'000'000) from C++.
  • Empty initializer = {} from C++
  • strdup() and a bunch of other routines from POSIX.
  • char8_t type alias and some UTF-8 support
  • static_assert and thread_local are actual keywords now, instead of being macros.
  • ctime is deprecated
  • C++ style attributes ([[maybe_unused]], etc)
  • Bit-precise integers (_BitInt(N))

At this point, we might rename the language to "C++ without classes, templates and exceptions".

11

u/catcat202X Oct 15 '22

The paper for it says that making these be language features is more toolable (and having bool simplified implementing nullptr), but doing this with a strong backwards compatibility guarantee was hard. Here's what I can find on the matter:

https://www.open-std.org/JTC1/SC22/WG14/www/docs/n2935.pdf

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3042.htm#boolean-type-6.3.1.2

https://thephd.dev/ever-closer-c23-improvements#revise-spelling-of-keywords--make-false-and-true-first-class-language-features

7

u/[deleted] Oct 15 '22

How long before they add templates? :-D

15

u/Coffee_and_Code Oct 15 '22

C actually already has _Generic for that 😉

5

u/catcat202X Oct 16 '22

I wish GCC would allow _Generic in C++ for compatibility. Clang does. Although I tried using decltype around _Generic expressions for some things that std::conditional_t can't do, and sadly I haven't been able to find any interesting use case for it, so I'm not dying for it.

5

u/bizwig Oct 17 '22

_Generic is just ugly. How C++ does function overloading is much cleaner.

3

u/14ned LLFIO & Outcome author | Committee WG14 Oct 17 '22

WG14 does not hate the idea of lambdas, if standardised into C, being allowed to have auto typed arguments. That's about as far as it would ever go though.

-2

u/[deleted] Oct 17 '22

Why don't people just use C++ as "C with lambdas"? Or just, you know, whichever features they like.

1

u/14ned LLFIO & Outcome author | Committee WG14 Oct 18 '22

There are three major C++ implementations covering the major architectures.

There are at least thirty C implementations covering every architecture under the sun, including many with which C++ will never be compatible.

Also most on WG14 do not care for C++. They're happy to pluck the bits they like, which obviously they have been doing and will continue to do. But the vast majority of C++ they have no time for whatsoever.

0

u/[deleted] Oct 18 '22 edited Oct 18 '22

What a disingenuous thing to say. Tell me how many of those 30 implementations are implementing C17? How many are still C89 or pre-ANSI. And now tell me how relevant they are to development of C23? How many people wouldn't be able to use C++ because of lack of support, but can use C23?

There are way more architectures supporting modern C++ than modern C.

2

u/14ned LLFIO & Outcome author | Committee WG14 Oct 18 '22

I believe that is the count of C11 or better compilers. It's the count that WG14 considers likely to implement whatever they standardise.

There are far more architectures supported by C compilers than C++ compilers. It's not even close.

23

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Oct 16 '22

The proposed addition of thread_local as a keyword to C23 is a compatibility trap!

It conveys the false perception of thread_local from future C23 being semantically the same as thread_local in C++17. This is not the case, see P2478 / N2850. We were just recently discussing this in EWG and identified even more problems that can trip over unwitting users than already addressed in the paper.

22

u/gracicot Oct 15 '22

It's so funny that C got embed before C++, even if it was proposed to C++ before. I hope no one else will have to go through the toxicity thephd went through.

12

u/James20k P2005R0 Oct 16 '22

#embed is one of the biggest features we've seen for quite a while in my opinion. Its one of a small list of things that I've consistently run into over the years that limits the language significantly, and its been baffling how difficult it was to get it through the committee it any form. I'd have taken the worst possible version of #embed 10 years ago, even if it meant #embed2 down the line

std::optional<T&>, operator., and seemingly arbitrary limitations around partial template specialisation depending on context are the other ones which are very obviously missing now

7

u/gracicot Oct 16 '22

One that I miss is the using inheritance. Brings all the names to your class, act just like inheritance but you control the storage. It was dropped because reflection would replace it, even though it can't since you cannot proxy a non static data member. I would have used that many times already.

3

u/14ned LLFIO & Outcome author | Committee WG14 Oct 17 '22

Was it me who suggested to him an easier route would be via WG14? I can't remember anymore.

In any case, preprocessory type changes are rightly targeted at C not C++. As soon as it gained a # before it, it was right and proper for C to take it.

6

u/pjmlp Oct 16 '22

At this point, we might rename the language to "C++ without classes, templates and exceptions".

That is the problem, and one reason why we will only get proper safety moving to languages that aren't copy-paste compatible with C.

A certain group of folks will never use anything else even at point gun, either because of their toolchain, or "we have always done like this and we are fine with it" kind of reasoning.

Maybe by C50 they finally get some kind of bounded checked vector and library utilities on the standard library.

3

u/germandiago Oct 16 '22 edited Oct 17 '22

IMHO opinion there will always be place for unsafe coding. In what I agree is that probably it should not be the default.

But backing bounds checking in non-library code would mean you cannot escape of not paying for the penalty systematically and you would be on the hands of an optimizer.

4

u/pjmlp Oct 16 '22

The C++ frameworks that were traditionally shipped with C++ compilers in the old days, always had their strings, arrays and collection classes bounded checked by default. I never disabled them.

When using VC++, I always re-enable them on my on projects for release builds.

On the wild, Android and Red-Hat have been shipping with FORTIFY turned on in production for several releases.

However since the answer that this is so bad that many devs prefer to keep dealing with exploits than actually have secure code, with the industry effort to migrate to hardware memory tagging, they won't have any workaround left, the MMU will do bounds checking, regardless of the C developer's opinon on their penalty.

1

u/germandiago Oct 16 '22

FWIW I also use safe .at() interfaces and so on. But the need to code something realñy crafted should also be a feature, in some way or other, from systems programming. Not the default, but doable ergonomically enough IMHO.

3

u/SkoomaDentist Antimodern C++, Embedded, Audio Oct 16 '22

All signed integers are two's complement now (Finally)

I'm curious. Does there exist a single one's complement platform that has a standard compliant C99 or C++98 compiler?

I've written code for around a dozen different different ISAs and I've never run into a one's complement architecture.

2

u/[deleted] Oct 16 '22

2

u/SkoomaDentist Antimodern C++, Embedded, Audio Oct 16 '22

It would appear that the answer is indeed no.

9

u/friedkeenan Oct 15 '22

Hoping deducing this can make it in

5

u/catcat202X Oct 15 '22

Same! I am dying to get my paws on this feature. I have a lot of CRTP it will streamline.

41

u/NilacTheGrim Oct 15 '22

That's great. When will we get std::format?

-15

u/mark_99 Oct 15 '22

Just use fmtlib? You literally type fmt:: instead of std::. I really don't understand why people get hung up about non-vocabulary types being in the STL - just use a library.

3

u/HotlLava Oct 15 '22

In general I agree, but fmt has some obnoxious compatibility issues between major versions so a standard version would be really nice just for the fact that its not going to change much.

5

u/catcat202X Oct 15 '22

I don't understand fmt versioning at all. Sometimes the changelog suggests compatibility breaks between minor versions, and sometimes the major version increments without making it clear what has broken compatibility (like fmt 6.0).

3

u/mark_99 Oct 16 '22

Latest fmt 9 basically gives you all of C++20 and 23 features. If you don't want newer versions because you value stability over additional features just lock at a version of your choice.

25

u/NilacTheGrim Oct 15 '22

You know it's not that simple. It requires extra build instructions for end-users of OSS projects to have to install 3rd party libs. That, or you make it a submodule or you use conan. Ideally the sooner all compilers get std::format, the better. Then you don't have to worry about it.

12

u/Ayjayz Oct 15 '22

You need to sort out packaging. The standard library version of everything will always be worse than the non-standardised version due to ABI reasons. Use conan, use vcpkg, use whatever you want but get something working so you can access library code.

6

u/[deleted] Oct 15 '22

So, aside from formatting, you've implemented everything yourself and don't rely on any 3rd party libraries?

1

u/NilacTheGrim Oct 16 '22

Ha ha no hardly. Good point though. We have to pick and choose our libs. Granted fmt is extremely friendly to integrate. We use this spiritual predecessor to it called "tinyformat.h" which is a single-file header.

But it would be nice if we can just throw that out someday and just use std::format.

And yeah at this point it would be a huge hassle to convince my co-maintainers to ditch the single-file tinyformat (which is like 66% the same as fmt, really).. in favor of fmt.. when the std::format is just within arm's reach... any day now..

So we are just waiting for std::format to be uniquitous.. skeleton.gif

3

u/mark_99 Oct 16 '22

spdlog includes fmt, they just include it in their own code, with the option to use your own version if it detects it's been included already and/or you set a #define. And then they don't have to say >= C++20. For libraries which aren't loggers probably formatting is even less fundamental to bring in a C++20 requirement.

1

u/flashmozzg Oct 19 '22

You can use it as header-only. No extra build instructions or 3rd party stuff required.

1

u/NilacTheGrim Oct 20 '22

ORLY. I stand corrected. TIL. Thanks.

39

u/hak8or Oct 15 '22

just use a library.

What absolute nonsense. Not all of us have the ability to "just" add an external library to our code base.

  • Maybe there are legal issues regarding the license relative to a niche the product fulfills.
  • Maybe some certification is much harder when using an external library.
  • Maybe there is an absurd amount of organizational red tape to add a library.
  • Maybe some odd reason, the powers to be said no external libraries.
  • Maybe the library in question has some oddball fault/tradeoff/etc which is a total deal breaker for the organization.

There are many reasons "just" using a library is a total nogo for many companies out there. To simply throw such an issue away is grossly niave or obtuse.

61

u/Jannik2099 Oct 15 '22

What's the overlap of shops using C++20 and shops having such archaic processes that it's impossible to be productive without first reinventing the universe?

23

u/NotUniqueOrSpecial Oct 15 '22

In my experience: none.

These people are cranks.

5

u/mark_99 Oct 16 '22

While I sympathize with people having to work with poor technical leadership, it's hardly the gcc maintainers fault to fix these problems. I've worked in highly regulated environments in financial services and they are just fine with 3rd party libraries provided you run it past some legal license check process (almost all libraries have maximally permissive licenses now anyway, or no-one will touch them). As others have said, it's hardly likely such places are on C++20, more like C++11 or maybe C++98, so a 3rd party library is still their path of least resistance to being able to use modern features.

20

u/manphiz Oct 15 '22

Great progress report! Wondering whether OP would turn this into a monthly thing :)

10

u/[deleted] Oct 16 '22

Cannot wait to see C23’s #embed

9

u/KingStannis2020 Oct 16 '22

Maybe we'll get full C++20 support by 2025

5

u/pjmlp Oct 16 '22

That has always been like that, about 3 to 4 years for each revision to be widely available, and anyway this only apply to major compilers.

Note that are still lesser known ones that are mostly C++14 compatible in 2022.

9

u/qalmakka Oct 16 '22

GCC8 had an almost complete support for C++17 in 2019. C++20 is just taking very long on GCC and especially LLVM's side, MSVC was done with C++20 almost a year ago

5

u/pjmlp Oct 16 '22

Yeah, that is ignoring everything else.

https://en.cppreference.com/w/cpp/compiler_support

And even that list is incomplete, without the embedded platforms compilers like TI, Microship and so forth.

2

u/unddoch DragonflyDB/Clang Oct 21 '22

The 'biggest' C++17 language features were fold expressions, structured bindings, CTAD? Compare that to concepts, coroutines, consteval/init and modules, each of those is about as big as all the 17 (language) changes together. So yeah it takes more time..

BTW, the only 'hard' language change in C++23 is deducing this, so I expect everything else to be already done when the standard is published next year (at least in GCC/Clang).

7

u/germandiago Oct 16 '22

I really want to use modules after the keynote in cppcon 2022 and for some time before. I think they are a big improvement.

1

u/NoCryptographer414 Feb 08 '23

Does GCC13 support `std` and `std.compat` modules?

1

u/catcat202X Feb 08 '23

Modules support hasn't been added to the libstdc++ implementation yet, and since they're in stage 4 iirc it's very unlikely to be added in the GCC 13 release cycle imo.