-
Notifications
You must be signed in to change notification settings - Fork 231
Optional SIMD memchr #592
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
Merged
Merged
Optional SIMD memchr #592
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //! add-flags.py(LDFLAGS): -Wl,--stack-first -Wl,--initial-memory=327680 | ||
|
|
||
| #include <__macro_PAGESIZE.h> | ||
| #include <stddef.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void test(char *ptr, size_t length, void *want) { | ||
| void *got = memchr(ptr, 7, length); | ||
| if (got != want) { | ||
| printf("memchr(%p, 7, %lu) = %p, want %p\n", ptr, length, got, want); | ||
| } | ||
| } | ||
|
|
||
| int main(void) { | ||
| char *const LIMIT = (char *)(__builtin_wasm_memory_size(0) * PAGESIZE); | ||
|
|
||
| for (size_t length = 0; length < 64; length++) { | ||
| for (size_t alignment = 0; alignment < 24; alignment++) { | ||
| for (ptrdiff_t pos = -2; pos < length + 2; pos++) { | ||
| // Create a buffer with the given length, at a pointer with the given | ||
| // alignment. Using the offset LIMIT - PAGESIZE - 8 means many buffers | ||
| // will straddle a (Wasm, and likely OS) page boundary. Place the | ||
| // character to find at every position in the buffer, including just | ||
| // prior to it and after its end. | ||
| char *ptr = LIMIT - PAGESIZE - 8 + alignment; | ||
| memset(LIMIT - 2 * PAGESIZE, 0, 2 * PAGESIZE); | ||
| memset(ptr, 5, length); | ||
| ptr[pos] = 7; | ||
|
|
||
| // The first instance of the character is found. | ||
| if (pos >= 0) ptr[pos + 2] = 7; | ||
|
|
||
| // The character is found if it's within range. | ||
| test(ptr, length, 0 <= pos && pos < length ? &ptr[pos] : NULL); | ||
| } | ||
| } | ||
|
|
||
| // Ensure we never read past the end of memory. | ||
| char *ptr = LIMIT - length; | ||
| memset(LIMIT - 2 * PAGESIZE, 0, 2 * PAGESIZE); | ||
| memset(ptr, 5, length); | ||
| ptr[length - 1] = 7; | ||
|
|
||
| // Nothing found on an empty buffer. | ||
| test(ptr, length, length != 0 ? &ptr[length - 1] : NULL); | ||
|
|
||
| // Test for length overflow. | ||
| if (length > 0) test(ptr, SIZE_MAX, &ptr[length - 1]); | ||
| } | ||
|
|
||
| return 0; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure about this: using the
wasm_i8x16_bitmaskdirectly here is a better lowering for x64 thanwasm_v128_any_true.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.
Yes. This is a mitigation for AArch64. We don't have the luxury of knowing the final architecture, and much less the CPU. Or how good the runtime is at (e.g. peephole) optimizing the final generated assembly.
But I'm pretty sure I measured, and at least for large buffers (and wazero) this was a significant improvement on AArch64, for a pretty insignificant cost on 3 different x86-64 CPUs.
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 tried this again with
bench.cwith wasmtime on my Xeon W-2135, and... it's hard to measure. I'm not saying it's not slower, it may, but it's close enough that between processors, VMs, lengths, I'm not sure which is better.So, where do I put something like
bench.c, and how do we settle the matter?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.
It seems like this kind of thing could fit in sightglass, even though this is a bit of a micro-benchmark. You could take a look at this example of how the blake3 benchmark was added:
benchmark.c. In the Dockerfile that builds a benchmark you could add the special "build wasi-libc with SIMD enabled" logic.But you don't have to put it there. I think we could probably settle this using the
bench.cyou provided. I'd probably be comfortable merging this without the special aarch64 optimization now and then submitting that as a second PR once I have a chance to measure a few things. Let me make sure I understand what you're saying precisely: (a) you can't detect a difference usingbitmaskorany_truewith the x64 CPUs you tested but (b) it still makes a very big difference for aarch64?