-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Replace usage of EM_BOOL with C/C++ bool
type. NFC
#22155
Conversation
This change was created using sed to substitutions |
Updated to include struct size saves and code size savings. |
I'm trying to change the type of EM_BOOL and this is currently blocking that: emscripten-core/emscripten#22155
I could do this in two phases perhaps:
|
Why do struct sizes change? I'd expect an int and an unpacked bool to both take 32 bits. Is that wrong? |
I guess Indeed this program printf
|
(same for C++ BTW) |
By itself it's 1 byte, I guess, but inside a struct the field after it might be aligned: #include <stdbool.h>
#include <stdio.h>
class C {
bool x;
int y;
};
int main() {
printf("%ld\n", sizeof(C));
return 0;
} That prints |
Anyhow, yeah, if this causes struct layout changes then splitting the PR as much as possible sgtm. |
Right, that is just normal struct layout rules. If you put the Lowering the alignment of a type can for sure shrink struct sizes. That is the primary use of the alignment of a given type. Higher alignment == worse for struct size. |
This reduces the size of several structs and results in code size savings in some cases. This change is split of from a larger change I have planned to remove the use of EM_BOOL completely: emscripten-core#22155.
This reduces the size of several structs and results in code size savings in some cases. This change is split of from a larger change I have planned to remove the use of EM_BOOL completely: emscripten-core#22155.
This reduces the size of several structs and results in code size savings in some cases. This change is split of from a larger change I have planned to remove the use of EM_BOOL completely: emscripten-core#22155.
This reduces the size of several structs and results in code size savings in some cases. This change is split of from a larger change I have planned to remove the use of EM_BOOL completely: emscripten-core#22155.
This reduces the size of several structs and results in code size savings in some cases. This change is split of from a larger change I have planned to remove the use of EM_BOOL completely: emscripten-core#22155.
This reduces the size of several structs and results in code size savings in some cases. This change is split of from a larger change I have planned to remove the use of EM_BOOL completely: emscripten-core#22155.
This reduces the size of several structs and can result in code size savings in some cases. The reason the code size savings don't show up in trivial examples is (I believe) because this change also increases the use of HEAP8 (where previously some examples only depended on HEAP32). This change is split of from a larger change I have planned to remove the use of EM_BOOL completely: #22155.
Rebased now that #22157 has landed. I'm planning on waiting for a release or two before landing this larger change. |
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.
lgtm in a separate release as you said.
Also, do we have a test that EM_BOOL, EM_TRUE, EM_FALSE
still work as legacy?
Added a test. |
Thanks - which is it though? (this is a pretty big diff and I don't see a separate commit for it, this would have been a nice case to not squash actually I think?) |
Thanks, lgtm. |
This change is now ready to land I think |
bool
type. NFC
Just to note, this looks like a massive user-facing breaking ABI change. The previous sizeof(EM_BOOL) change from 4 bytes to 1 byte also was a breaking ABI change, and it regressed my Emscripten downstream library at juj/wasm_webgpu#46 (I was lucky to have added |
Looking at stdbool.h, it has #ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define true 1
#define false 0
#define bool _Bool
#endif
#define __bool_true_false_are_defined 1
#endif What happens if C code is compiled with -std=c89 or -std=c90 that does not yet have the |
Musl's
|
I think this particular change is an NFC. That change that could be potentially breaking is the one that already landed and was already released in 3.1.62: #22157. Right? |
If you like we can wait a few more releases to see if any other fallout is reported. So far I have only heard of one regression which was someone who was using the |
Just in case I'm missing something, here are the 3 types of possible fallout that I can think of that might result from this type of change:
Are there any others that we should also consider? Note: Only (1) and (2) above can result in silent failure whereas (3) is a compiler error. |
I think these scenarios are probably accurate. In (1) I would note that it is not necessarily only struct sizes that disagree, but also C++ name mangling that changes, since So if someone has a codebase where they define a function with EM_BOOL in the signature in a .cpp file, then they would not have ABI compatibility to link against code that uses those functions compiled with previous version of Emscripten.
Unless I have missed a development(?), Emscripten end users cannot use these
Yeah, the actual breakage occurred in #22157. After that, this renaming of This pair of changes looks like first #22157 broke ABI compatibility of EM_BOOL, and then this PR is moving Emscripten away from using If I were to rewind back, I wonder if it might be less disrupting to keep Although given that this has already happened, I don't wish to cause any more disruptions. |
Emscripten users can use the On that matter there has been some progress towards allowing users to extend C_STRUCTS system. The first part of this already landed in: #22251. |
I was a single user who was affected this way! That change totally broke my wasm_webgpu project for my downstream users, and I didn't know about it until someone reported it as a bug to me. |
That's cool - it would definitely be nice to have "equal" footing for Emscripten end user JS libraries and Emscripten system JS libraries. I understand that user JS libraries can refer to system struct fields, though the usefulness of that is quite limited. Rather, end users most often would like to use the offset mechanism for filling structs in their own projects. The reason I was using EM_BOOL was that it was already a handy way for the "system" to provide a standard boolean type. |
I see, sorry I didn't process that this was the type of failure you had. Was it this exact issue? i.e. hardcoded offsets in your JS code? |
To marshall WebGPU API calls, structs on C side need to be converted to JS side descriptor objects, e.g. and JS code: and I had assumed that the memory layout would stay fixed. Fortunately I had added this at some point though: juj/wasm_webgpu@3345174 |
d3a27c2
to
c59fd01
Compare
These macros were already updated to expand to the C/C++ `bool` type back in emscripten-core#22157. This is the natural NFC followup to that change.
These macros were already updated to expand to the C/C++
bool
typeback in #22157. This is the natural NFC followup to that change.