Bug 93008 - Need a way to make inlining heuristics ignore whether a function is inline (especially for constexpr in >= c++20)
Summary: Need a way to make inlining heuristics ignore whether a function is inline (e...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: code-size, missed-optimization
: 117550 (view as bug list)
Depends on:
Blocks:
 
Reported: 2019-12-19 13:42 UTC by Jonathan Wakely
Modified: 2025-12-31 20:35 UTC (History)
11 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-11-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2019-12-19 13:42:31 UTC
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
};
Comment 1 Jakub Jelinek 2019-12-19 13:50:00 UTC
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?
Comment 2 Eric Gallager 2019-12-21 15:27:04 UTC
(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
> };
Comment 3 Jonathan Wakely 2019-12-21 17:24:56 UTC
(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.
Comment 4 Eric Gallager 2019-12-21 19:38:04 UTC
(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.
Comment 5 Jonathan Wakely 2019-12-21 20:33:04 UTC
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.
Comment 6 Jan Hubicka 2020-11-09 07:26:50 UTC
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++)
Comment 7 Jonathan Wakely 2024-04-19 11:31:24 UTC
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.
Comment 8 Jan Hubicka 2024-04-22 08:28:20 UTC
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).
Comment 9 Jonathan Wakely 2024-04-22 08:31:37 UTC
Another solution is just to use __builtin_expect or [[likely]] or [[unlikely]] at the call site. That wouldn't need any compiler changes.
Comment 10 Jonathan Wakely 2024-04-22 08:33:24 UTC
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.
Comment 11 Eric Gallager 2024-05-04 20:52:53 UTC
(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"?
Comment 12 Jonathan Wakely 2024-05-04 23:16:26 UTC
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.
Comment 13 Eric Gallager 2024-05-06 01:02:17 UTC
(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"?
Comment 14 Chris Elrod 2024-05-06 02:23:20 UTC
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?
Comment 15 Luke Dalessandro 2024-05-06 02:31:50 UTC
(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.
Comment 16 Sam James 2024-11-13 06:31:42 UTC
*** Bug 117550 has been marked as a duplicate of this bug. ***
Comment 17 Sam James 2024-12-13 15:23:55 UTC
https://inbox.sourceware.org/gcc-patches/ZzYehtM9WcSgrSpc@tucnak/ was posted for feeble-inline although there was a lot of discussion on IRC afterwards.
Comment 18 Drea Pinski 2025-12-31 20:35:53 UTC
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 ...