all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Emacs' C: static inline considered useless nowadays?
@ 2022-10-16 22:08 Matt Armstrong
  2022-10-17  0:43 ` Stefan Monnier
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Matt Armstrong @ 2022-10-16 22:08 UTC (permalink / raw)
  To: emacs-devel

For Emacs, I would think:

a) In header files, use Emacs' INLINE and NO_INLINE macros.

b) In .c files, use static, EXTERN_INLINE, but never 'inline' since it
does nothing.

I'm seeking confirmation (or refutation) of (a) and (b).  I'm not asking
generally, but for Emacs' C code.

Specifically, ignoring the uselesness of the function itself, assuming
the code is in a .c file, is the use of the "inline" keyword redundant
with static, below?

    static inline int
    add_two_int (int a, int b)
    {
        return a + b
    }

I've spent the last few decades coding with an undersanding that
"inline" is about linkage and allows one to place code in header files
so that it *may* be inlined, but that compilers long ago stopped using
it as a meaningful inlining hint.  But this is mostly colored by how gcc
and clang behave with C++, and not much else.

-- 
matt (sent from an Emacs running the feature/noverlay branch)



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-16 22:08 Emacs' C: static inline considered useless nowadays? Matt Armstrong
@ 2022-10-17  0:43 ` Stefan Monnier
  2022-10-17  3:13   ` Matt Armstrong
  2022-10-17  6:10 ` Eli Zaretskii
  2022-10-18 11:58 ` Richard Stallman
  2 siblings, 1 reply; 15+ messages in thread
From: Stefan Monnier @ 2022-10-17  0:43 UTC (permalink / raw)
  To: Matt Armstrong; +Cc: emacs-devel

Matt Armstrong [2022-10-16 15:08:51] wrote:
> I've spent the last few decades coding with an undersanding that
> "inline" is about linkage and allows one to place code in header files
> so that it *may* be inlined, but that compilers long ago stopped using
> it as a meaningful inlining hint.  But this is mostly colored by how gcc
> and clang behave with C++, and not much else.

I believe what you say does hold true for "optimized builds".
I'd be interested to know if it's true for lower levels of optimization
as well.


        Stefan "compiling with -Og"




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-17  0:43 ` Stefan Monnier
@ 2022-10-17  3:13   ` Matt Armstrong
  2022-10-17  3:33     ` Óscar Fuentes
  0 siblings, 1 reply; 15+ messages in thread
From: Matt Armstrong @ 2022-10-17  3:13 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> Matt Armstrong [2022-10-16 15:08:51] wrote:
>> I've spent the last few decades coding with an undersanding that
>> "inline" is about linkage and allows one to place code in header files
>> so that it *may* be inlined, but that compilers long ago stopped using
>> it as a meaningful inlining hint.  But this is mostly colored by how gcc
>> and clang behave with C++, and not much else.
>
> I believe what you say does hold true for "optimized builds".
> I'd be interested to know if it's true for lower levels of optimization
> as well.
>
>         Stefan "compiling with -Og"

Seems the answer, thanks to godbolt, is "it depends", but "static
inline" does enable inlining in gcc's -Og, so it has its use.

For this program:

    static inline int static_inline_add(int x, int y) { return x + y; }
    static int static_add(int x, int y) { return x + y; }
    int add_three(int x, int y, int z) {
        return static_add(x, static_inline_add(y, z));
    }

gcc and clang has the same behavior:

    -O0: nither static functions are inlined into 'add_three'
    -Og: only 'static_inline_add' is inlined
    -O1: 'static_add' is also inlined

Microsoft Visual Studio has more optimization knobs.

    /Od: neither are inlined
    /Ot: neither are inlined
    /Ox: both are inlined
    /Odb1: only static_inline_add is inlined
    /Odb2: only static_inline_add is inlined
    /O1: both are inlined
    /O2: both are inlined

I couldn't find a way to trigger Visual Studio into behaving like gcc.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-17  3:13   ` Matt Armstrong
@ 2022-10-17  3:33     ` Óscar Fuentes
  0 siblings, 0 replies; 15+ messages in thread
From: Óscar Fuentes @ 2022-10-17  3:33 UTC (permalink / raw)
  To: emacs-devel

Matt Armstrong <matt@rfc20.org> writes:

> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>> Matt Armstrong [2022-10-16 15:08:51] wrote:
>>> I've spent the last few decades coding with an undersanding that
>>> "inline" is about linkage and allows one to place code in header files
>>> so that it *may* be inlined, but that compilers long ago stopped using
>>> it as a meaningful inlining hint.  But this is mostly colored by how gcc
>>> and clang behave with C++, and not much else.
>>
>> I believe what you say does hold true for "optimized builds".
>> I'd be interested to know if it's true for lower levels of optimization
>> as well.
>>
>>         Stefan "compiling with -Og"
>
> Seems the answer, thanks to godbolt, is "it depends", but "static
> inline" does enable inlining in gcc's -Og, so it has its use.
>
> For this program:
>
>     static inline int static_inline_add(int x, int y) { return x + y; }
>     static int static_add(int x, int y) { return x + y; }
>     int add_three(int x, int y, int z) {
>         return static_add(x, static_inline_add(y, z));
>     }
>
> gcc and clang has the same behavior:
>
>     -O0: nither static functions are inlined into 'add_three'
>     -Og: only 'static_inline_add' is inlined
>     -O1: 'static_add' is also inlined

See https://gcc.gnu.org/onlinedocs/gcc/Inline.html for some enlightening
comments on this topic.

Please note that modern compilers implement complex heuristics for
deciding when a function is inlined (and the presence of the `inline'
keyword is just another factor.) An "static inline" function may not be
inlined even at -O2 if your compiler decides that it is better left
un-inlined.




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-16 22:08 Emacs' C: static inline considered useless nowadays? Matt Armstrong
  2022-10-17  0:43 ` Stefan Monnier
@ 2022-10-17  6:10 ` Eli Zaretskii
  2022-10-17 19:08   ` Matt Armstrong
  2022-10-18 11:58 ` Richard Stallman
  2 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2022-10-17  6:10 UTC (permalink / raw)
  To: Matt Armstrong; +Cc: emacs-devel

> From: Matt Armstrong <matt@rfc20.org>
> Date: Sun, 16 Oct 2022 15:08:51 -0700
> 
> For Emacs, I would think:
> 
> a) In header files, use Emacs' INLINE and NO_INLINE macros.
> 
> b) In .c files, use static, EXTERN_INLINE, but never 'inline' since it
> does nothing.
> 
> I'm seeking confirmation (or refutation) of (a) and (b).  I'm not asking
> generally, but for Emacs' C code.

See conf_post.h, around line 395: it explains the issue and the
expected usage of these in our sources.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-17  6:10 ` Eli Zaretskii
@ 2022-10-17 19:08   ` Matt Armstrong
  2022-10-17 19:12     ` Eli Zaretskii
  0 siblings, 1 reply; 15+ messages in thread
From: Matt Armstrong @ 2022-10-17 19:08 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Matt Armstrong <matt@rfc20.org>
>> Date: Sun, 16 Oct 2022 15:08:51 -0700
>> 
>> For Emacs, I would think:
>> 
>> a) In header files, use Emacs' INLINE and NO_INLINE macros.
>> 
>> b) In .c files, use static, EXTERN_INLINE, but never 'inline' since it
>> does nothing.
>> 
>> I'm seeking confirmation (or refutation) of (a) and (b).  I'm not asking
>> generally, but for Emacs' C code.
>
> See conf_post.h, around line 395: it explains the issue and the
> expected usage of these in our sources.

conf_post.h didn't answer my question since it seems to pertain to code
in header files.

I realize now that the conclusion of this thread so far is unclear:

a) "static inline" in .c files is okay.  No need for macros.

b) Criteria for when to use "static inline" is not clear.  Do we do this
   ad hoc as -Og builds are discovered to be slow?  Do it for all static
   functions?  Don't care?  I can see cases for any of these.

c) Neither (a) nor (b) are written down, and as Eli points out,
   conf_post.h suggests that at least sometimes you've got to use
   macros for inline functions in Emacs code, but that header is about
   how to do things in header files.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-17 19:08   ` Matt Armstrong
@ 2022-10-17 19:12     ` Eli Zaretskii
  2022-10-17 20:33       ` Matt Armstrong
  0 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2022-10-17 19:12 UTC (permalink / raw)
  To: Matt Armstrong; +Cc: emacs-devel

> From: Matt Armstrong <matt@rfc20.org>
> Cc: emacs-devel@gnu.org
> Date: Mon, 17 Oct 2022 12:08:38 -0700
> 
> > See conf_post.h, around line 395: it explains the issue and the
> > expected usage of these in our sources.
> 
> conf_post.h didn't answer my question since it seems to pertain to code
> in header files.

Why would you need 'inline' anywhere else?



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-17 19:12     ` Eli Zaretskii
@ 2022-10-17 20:33       ` Matt Armstrong
  2022-10-18  2:30         ` Eli Zaretskii
  0 siblings, 1 reply; 15+ messages in thread
From: Matt Armstrong @ 2022-10-17 20:33 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Matt Armstrong <matt@rfc20.org>
>> Cc: emacs-devel@gnu.org
>> Date: Mon, 17 Oct 2022 12:08:38 -0700
>>
>> > See conf_post.h, around line 395: it explains the issue and the
>> > expected usage of these in our sources.
>>
>> conf_post.h didn't answer my question since it seems to pertain to code
>> in header files.
>
> Why would you need 'inline' anywhere else?

That is the question I started the thread with, in different words.  If
you grep our sources 'static inline' appears in .c files, and I wondered
why.

Stefan gave an answer in the form of a question (he was concerned about
what gcc did at optimization level -Og).  I investigated and found that
gcc (and clang) will inline a static function at -Og level only if the
function uses the inline keyword.  So a "static inline" function is
useful in the case where you want some key static functions inlined, but
everything else still relatively unoptimized.




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-17 20:33       ` Matt Armstrong
@ 2022-10-18  2:30         ` Eli Zaretskii
  2022-10-18 13:32           ` Stefan Monnier
  0 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2022-10-18  2:30 UTC (permalink / raw)
  To: Matt Armstrong; +Cc: emacs-devel

> From: Matt Armstrong <matt@rfc20.org>
> Cc: emacs-devel@gnu.org
> Date: Mon, 17 Oct 2022 13:33:18 -0700
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> >> From: Matt Armstrong <matt@rfc20.org>
> >> Cc: emacs-devel@gnu.org
> >> Date: Mon, 17 Oct 2022 12:08:38 -0700
> >>
> >> > See conf_post.h, around line 395: it explains the issue and the
> >> > expected usage of these in our sources.
> >>
> >> conf_post.h didn't answer my question since it seems to pertain to code
> >> in header files.
> >
> > Why would you need 'inline' anywhere else?
> 
> That is the question I started the thread with, in different words.  If
> you grep our sources 'static inline' appears in .c files, and I wondered
> why.

Never mind that; it's a historic accident.  Just use 'static' and let
the compiler decide when to inline.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-16 22:08 Emacs' C: static inline considered useless nowadays? Matt Armstrong
  2022-10-17  0:43 ` Stefan Monnier
  2022-10-17  6:10 ` Eli Zaretskii
@ 2022-10-18 11:58 ` Richard Stallman
  2 siblings, 0 replies; 15+ messages in thread
From: Richard Stallman @ 2022-10-18 11:58 UTC (permalink / raw)
  To: Matt Armstrong; +Cc: emacs-devel

[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

ISTR that GCC did give some weight to `inline' when deciding whether
to inline a function.  But I don't recall for certain -- when I worked
on GCC was 30 years ago.

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-18  2:30         ` Eli Zaretskii
@ 2022-10-18 13:32           ` Stefan Monnier
  2022-10-18 15:09             ` Eli Zaretskii
  0 siblings, 1 reply; 15+ messages in thread
From: Stefan Monnier @ 2022-10-18 13:32 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Matt Armstrong, emacs-devel

> Never mind that; it's a historic accident.  Just use 'static' and let
> the compiler decide when to inline.

Since both `gcc` and `clang` obey the `inline` attribute in that case
for options like `-Og`, it can still be a good idea to keep them in
those places where we think it makes a significant difference.


        Stefan




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-18 13:32           ` Stefan Monnier
@ 2022-10-18 15:09             ` Eli Zaretskii
  2022-10-18 17:01               ` tomas
  2022-10-18 19:22               ` Stefan Monnier
  0 siblings, 2 replies; 15+ messages in thread
From: Eli Zaretskii @ 2022-10-18 15:09 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: matt, emacs-devel

> From: Stefan Monnier <monnier@iro.umontreal.ca>
> Cc: Matt Armstrong <matt@rfc20.org>,  emacs-devel@gnu.org
> Date: Tue, 18 Oct 2022 09:32:32 -0400
> 
> > Never mind that; it's a historic accident.  Just use 'static' and let
> > the compiler decide when to inline.
> 
> Since both `gcc` and `clang` obey the `inline` attribute in that case
> for options like `-Og`, it can still be a good idea to keep them in
> those places where we think it makes a significant difference.

Experience shows that we have no idea where it makes a difference.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-18 15:09             ` Eli Zaretskii
@ 2022-10-18 17:01               ` tomas
  2022-10-18 19:22               ` Stefan Monnier
  1 sibling, 0 replies; 15+ messages in thread
From: tomas @ 2022-10-18 17:01 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 869 bytes --]

On Tue, Oct 18, 2022 at 06:09:13PM +0300, Eli Zaretskii wrote:
> > From: Stefan Monnier <monnier@iro.umontreal.ca>
> > Cc: Matt Armstrong <matt@rfc20.org>,  emacs-devel@gnu.org
> > Date: Tue, 18 Oct 2022 09:32:32 -0400
> > 
> > > Never mind that; it's a historic accident.  Just use 'static' and let
> > > the compiler decide when to inline.
> > 
> > Since both `gcc` and `clang` obey the `inline` attribute in that case
> > for options like `-Og`, it can still be a good idea to keep them in
> > those places where we think it makes a significant difference.
> 
> Experience shows that we have no idea where it makes a difference.

In the case of gcc, it's at least documented [1] (no idea about clang,
but I'd assume it's documented as well).

For proprietary compilers... we don't talk about proprietary compilers,
do we?

;-)

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-18 15:09             ` Eli Zaretskii
  2022-10-18 17:01               ` tomas
@ 2022-10-18 19:22               ` Stefan Monnier
  2022-10-18 19:31                 ` Eli Zaretskii
  1 sibling, 1 reply; 15+ messages in thread
From: Stefan Monnier @ 2022-10-18 19:22 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: matt, emacs-devel

Eli Zaretskii [2022-10-18 18:09:13] wrote:
> Experience shows that we have no idea where it makes a difference.

Yet we do go through a fair bit of trouble to force such inlining
in `lisp.h` (under the control of `DEFINE_KEY_OPS_AS_MACROS`).


        Stefan




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Emacs' C: static inline considered useless nowadays?
  2022-10-18 19:22               ` Stefan Monnier
@ 2022-10-18 19:31                 ` Eli Zaretskii
  0 siblings, 0 replies; 15+ messages in thread
From: Eli Zaretskii @ 2022-10-18 19:31 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: matt, emacs-devel

> From: Stefan Monnier <monnier@iro.umontreal.ca>
> Cc: matt@rfc20.org,  emacs-devel@gnu.org
> Date: Tue, 18 Oct 2022 15:22:22 -0400
> 
> Eli Zaretskii [2022-10-18 18:09:13] wrote:
> > Experience shows that we have no idea where it makes a difference.
> 
> Yet we do go through a fair bit of trouble to force such inlining
> in `lisp.h` (under the control of `DEFINE_KEY_OPS_AS_MACROS`).

Yes.  I was talking about inline functions in *.c files in the message
to which you replied, not about what we do in *.h files.



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-10-18 19:31 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-16 22:08 Emacs' C: static inline considered useless nowadays? Matt Armstrong
2022-10-17  0:43 ` Stefan Monnier
2022-10-17  3:13   ` Matt Armstrong
2022-10-17  3:33     ` Óscar Fuentes
2022-10-17  6:10 ` Eli Zaretskii
2022-10-17 19:08   ` Matt Armstrong
2022-10-17 19:12     ` Eli Zaretskii
2022-10-17 20:33       ` Matt Armstrong
2022-10-18  2:30         ` Eli Zaretskii
2022-10-18 13:32           ` Stefan Monnier
2022-10-18 15:09             ` Eli Zaretskii
2022-10-18 17:01               ` tomas
2022-10-18 19:22               ` Stefan Monnier
2022-10-18 19:31                 ` Eli Zaretskii
2022-10-18 11:58 ` Richard Stallman

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.