Currently GCC considers the 'inline' keyword as a hint for inlining, not just a request for comdat semantics. This is a problem in C++20 where arbitrarily complex functions are marked 'constexpr' so that they can be used at compile time, and a constexpr function is implicitly an inline function. But we do not want the function to get additional weighting for runtime optimizations just because it is eligible for use in compile-time constant expressions. It would be useful to have a new attribute that tells the inliner to ignore whether the function is 'inline' and only consider its size, use in hot/cold regions etc. Alternatively, do not interpret 'constexpr' on its own as an inlining hint, and only treat it as a hint when a constexpr function is explicitly declared with the 'inline' specifier (or is defined in the class body). constexpr void f() { } // not an inline hint constexpr inline void g() { } // inline hint inline constexpr void h() { } // inline hint struct X { constexpr void i() { } // inline hint };
We do have DECL_DECLARED_INLINE_P flag, but that is set even with just constexpr keyword. For the cases we were discussing on IRC, functions which should be constexpr e.g. in C++20 and not in C++17 and earlier, but which should still have comdat semantics, but not be really suggested as better idea for inlining, I think we want an attribute, but as constexpr is something that is done very often, it might be best to have another generic FUNCTION_DECL bit next to DECL_DECLARED_INLINE_P, which the new attribute (something in between noinline on inline function and always_inline) would clear and we'd clear it on constexpr functions that aren't explicitly inline too?
(In reply to Jonathan Wakely from comment #0) > Currently GCC considers the 'inline' keyword as a hint for inlining, not > just a request for comdat semantics. > > This is a problem in C++20 where arbitrarily complex functions are marked > 'constexpr' so that they can be used at compile time, and a constexpr > function is implicitly an inline function. But we do not want the function > to get additional weighting for runtime optimizations just because it is > eligible for use in compile-time constant expressions. > > It would be useful to have a new attribute that tells the inliner to ignore > whether the function is 'inline' and only consider its size, use in hot/cold > regions etc. > > Alternatively, do not interpret 'constexpr' on its own as an inlining hint, > and only treat it as a hint when a constexpr function is explicitly declared > with the 'inline' specifier (or is defined in the class body). > I would prefer this second approach. > > constexpr void f() { } // not an inline hint > constexpr inline void g() { } // inline hint > inline constexpr void h() { } // inline hint > > struct X { > constexpr void i() { } // inline hint > };
(In reply to Eric Gallager from comment #2) > I would prefer this second approach. The downside of it is that it requires adding a keyword that the standard says is completely redundant, in order to get compiler-specific behaviour. It's not clear why it's there, or if it's OK to remove it. A compiler-specific attribute obviously has compiler-specific effects.
(In reply to Jonathan Wakely from comment #3) > (In reply to Eric Gallager from comment #2) > > I would prefer this second approach. > > The downside of it is that it requires adding a keyword that the standard > says is completely redundant, in order to get compiler-specific behaviour. > It's not clear why it's there, or if it's OK to remove it. > > A compiler-specific attribute obviously has compiler-specific effects. As someone who likes redundancy when it's done for the benefit of explicitness, I don't necessarily see that as a downside.
But it wouldn't be redundant for GCC, it would have a different, non-portable meaning. Does it say "inline constexpr" because the code was written by someone who likes redundancy for the sake of explicitness, or because somebody wants the GCC-specific optimisation hint? If you have to add a comment to the code saying "don't remove this keyword, it's here because it does something" then the meaning is neither redundant, not explicit. It's non-obvious and an implicit meaning not backed up by other compilers or the standard. IMO the ideal solution is for GCC to stop using whether a function is inline as an optimisation hint. Then we wouldn't need some extra GCC-specific way to override that.
I just noticed this PR and wonder if there is anything to do on inliner side. It uses DECL_DECLARED_INLINE that was invented to distinguish between implicit inlines and explicit ones. So even if it would be bit misnamed it should mean "this is an inline hint for inliner", so I guess frontend needs to distinguish between constexpr and normal places where inline hint still means "inline more"? Inliner is really not on level to be able to completely ignore used inline hints without regressing various code. I made inline weaker for -O2 in GCC10 but for -O3 we still take it very seriously and I do not see way out of that: in many cases it is very hard to predict how much optimization will happen after inlining and a lot of code is carefully crafted under assumption that some specific inline happens (and a lot of such code is in C++)
I think we might want to add __attribute__((cold)) to std::vector functions that are now implicitly inline due to the addition of `constexpr`. We probably don't want to use noinline as that's too strong.
Note that cold attribute is also quite strong since it turns optimize_size codegen that is often a lot slower. Reading the discussion again, I don't think we have a way to make inline keyword ignored by inliner. We can make not_really_inline attribute (better name would be welcome).
Another solution is just to use __builtin_expect or [[likely]] or [[unlikely]] at the call site. That wouldn't need any compiler changes.
N.B. If you're going to do any more profiling + optimizing of std::vector, please do it with -std=gnu++20 so that everything is constexpr. Otherwise you're only testing C++17 which is the last version before everything in std::vector becomes constexpr. For the later standards the optimizations will probably behave very differently.
(In reply to Jan Hubicka from comment #8) > Reading the discussion again, I don't think we have a way to make inline > keyword ignored by inliner. We can make not_really_inline attribute (better > name would be welcome). "fake_inline"?
There's nothing fake about them, they are definitely inline functions as far as the language rules. But in some cases we don't want the compiler to use that fact as an optimisation hint.
(In reply to Jonathan Wakely from comment #12) > There's nothing fake about them, they are definitely inline functions as far > as the language rules. But in some cases we don't want the compiler to use > that fact as an optimisation hint. "quasi_inline"? "pseudo_inline"? "unoptimizable_inline"? "strictly_inline"? "honorary_inline"? "inline_in_name_only"? "ceremonially_inline"?
To me, an "inline" function is one that the compiler inlines. It just happens that the `inline` keyword also means both comdat semantics, and possibly hiding the symbol to make it internal (-fvisibility-inlines-hidden). It also just happens to be the case that the vast majority of the time I mark a function `inline`, it is because of this, not because of the compiler hint. `static` of course also specifies internal linkage, but I generally prefer the comdat semantics: I'd rather merge than duplicate the definitions. If there is a new keyword or pragma meaning comdat semantics (and preferably also specifying internal linkage), I would rather have the name reference that. I'd rather have a positive name about what it does, than negative: "quasi_inline: like inline, except it does everything inline does except the inline part". Why define as a set diff -- naming it after the thing it does not do! -- if you could define it in the affirmative, based on what it does in the first place?
(In reply to Jonathan Wakely from comment #5) > > IMO the ideal solution is for GCC to stop using whether a function is inline > as an optimisation hint. Then we wouldn't need some extra GCC-specific way > to override that. I'd vote to just do this and provide an attribute that's weaker than [[gnu::always_inline]] for those that need it. No need to change any underlying language stuff.
*** Bug 117550 has been marked as a duplicate of this bug. ***
https://inbox.sourceware.org/gcc-patches/ZzYehtM9WcSgrSpc@tucnak/ was posted for feeble-inline although there was a lot of discussion on IRC afterwards.
Jakub posted constexpr_only which is related attribute here: https://gcc.gnu.org/pipermail/gcc-patches/2025-December/704656.html I am not 100% sure that is the approach we want though ...