Repository navigation
Conversation
|
My use cases for this are twofold:
|
|
|
||
| #ifdef __wasm__ | ||
| #define EM_IMPORT(NAME) __attribute__((import_module("env"), import_name(#NAME))) | ||
| #define EMSCRIPTEN_NOINLINE __attribute__((__noinline__, __annotate__("binaryen.inline=0"))) |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
@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).
|
|
||
|
|
||
| def finalize_wasm(infile, outfile, js_syms): | ||
| def extract_binaryen_inline_annotations(wasm_file): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
| ''' | ||
| self.do_run(src, '2 3 4\n', force_c=True) | ||
|
|
||
| if self.is_wasm(): |
There was a problem hiding this comment.
Maybe just mark this whole tests as no_wasm2js instead of making most of it conditional?
| with webassembly.Module(wasm) as m: | ||
| for section in m.sections(): | ||
| if section.type == webassembly.SecType.CUSTOM: | ||
| self.assertNotEqual(section.name, 'binaryen.inline') |
There was a problem hiding this comment.
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"))) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes, I think so (perhaps conditionally); see my comment in emscripten.py.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Normal
__noinline__only affects LLVM, but often you want to preventany toolchain inlining, including in Binaryen. This new macro does that.
Technically, we annotate the function in LLVM and convert that into
the
@binaryen.inline 0code annotation.This is something we've wanted for a while and I was talking about
recently with @juj