-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make try_init_allocator() a ctor #338
Conversation
@@ -4562,7 +4562,7 @@ static void* tmalloc_small(mstate m, size_t nb) { | |||
|
|||
#if __wasilibc_unmodified_upstream // Forward declaration of try_init_allocator. | |||
#else | |||
static void try_init_allocator(void); | |||
__attribute__((constructor(0))) static void try_init_allocator(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entire range of 0-100 I believe is reserved for system functions like this. I'm not convinced 0 is the best choice here, but its not not hard to change in the future.
In emscripten we document the priorities in order to keep track of all the order which this stuff happens: https://github.com/emscripten-core/emscripten/blob/main/system/lib/README.md?plain=1#L9-L28
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Practically speaking, right now whenever we add a new constructor in wasi-libc, we just grep for all the constructors in the wasi-libc tree to figure out the ordering constraints, which seems ok for now, as there aren't very many.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe assigning priority 0
is a correct choice, given:
- dlmalloc is a service which is likely used by other ctors
try_init_allocator
itself doesn't rely on other ctors to be run before
We can also have a documentation somewhere to list all our ctors. I can add one in this PR if you folks think it's appropriate, though not sure where's the best place to add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its ok to simply use grep as Dan says, I was just pointing out that it can get complicated, and that one needs to be careful when choosing the order of these things, once there is more than one.
There are also some things that might (one day) need to happen before malloc initialization (e.g. TLS setup, application of relocations in PIC code) although I don't think any of those apply yet in wasi-libc.
I think this makes sense. We often like to avoid static ctors because they often slow down startup times due to doing extra work up front, however in this case, most programs are calling malloc early in startup anyway already, so the extra work needs to be done anyway. |
Out of interest, why does lazy-initialization inhibit "wizer-like optimization"? |
The static call graph has |
Nice. That makes sense now. Presumably in such cases |
Yeah, the code size win there likely isn't huge, but it's something. Likewise the win of not doing the load+branch in every |
I am now thinking about how this PR interacts with upcoming use cases for wasm which involve calling Is the motivation for this PR to avoid the problem that the malloc init code doesn't know where |
I've now posted https://reviews.llvm.org/D136110 to add a |
Are you interested in knowing the original heap end that was set at compile time, or the current size of the memory? (I guess its the former since you can use an intrinsic to know that current memory size). |
It's to know the original heap. The idea is, between the time when wasm-ld runs and the wasm |
Thanks for all the review comments! Closing this one since #377 is moving in the right direction. |
We can make
try_init_allocator()
a ctor, and even avoid calling it indlmalloc()
. Assigning the ctor with highest priority will enable regular ctors to callmalloc()
safely, andwizer
-like optimization will be capable to strip out some extra bits.