perf: nudge LLVM codegen good to_bytes_be#569
Merged
prestwich merged 1 commit intorecmo:mainfrom Apr 21, 2026
Merged
Conversation
Special case the BYTES = LIMBS * 8 so to compel LLVM generate the proper machine code. In the wild, it was observed that LLVM[^1] generated this on x86-64: ``` ;; ── limb3 (rdx) — written BYTE BY BYTE ── mov %rdx,%rax ; copy limb3 shr $0x38,%rax ; extract byte 0 (bits 63..56) mov %al, (%rcx,%rbx) ; store byte 0 mov %rdx,%rax shr $0x30,%rax ; extract byte 1 (bits 55..48) mov %al, 0x1(%rcx,%rbx) ; store byte 1 mov %rdx,%rax shr $0x28,%rax ; extract byte 2 (bits 47..40) mov %al, 0x2(%rcx,%rbx) ; store byte 2 mov %rdx,%rax shr $0x20,%rax ; extract byte 3 (bits 39..32) mov %al, 0x3(%rcx,%rbx) ; store byte 3 mov %edx,%eax shr $0x18,%eax ; extract byte 4 (bits 31..24) mov %al, 0x4(%rcx,%rbx) ; store byte 4 mov %edx,%eax shr $0x10,%eax ; extract byte 5 (bits 23..16) mov %al, 0x5(%rcx,%rbx) ; store byte 5 mov %dh, 0x6(%rcx,%rbx) ; store byte 6 (bits 15..8) mov %dl, 0x7(%rcx,%rbx) ; store byte 7 (bits 7..0) ;; ── limbs 2,1 — bswap+qword ── bswap %r15 ; byte-swap limb2 mov %r15, 0x8(%rcx,%rbx) ; store 8 bytes at once bswap %r14 ; byte-swap limb1 mov %r14, 0x10(%rcx,%rbx) ; store 8 bytes at once ;; ── limb0 (r13) — BYTE BY BYTE again ── mov %r13,%rax shr $0x38,%rax ; extract byte 24 mov %al, 0x18(%rcx,%rbx) ; store mov %r13,%rax shr $0x30,%rax ; extract byte 25 mov %al, 0x19(%rcx,%rbx) ; store mov %r13,%rax shr $0x28,%rax ; extract byte 26 mov %al, 0x1a(%rcx,%rbx) ; store mov %r13,%rax shr $0x20,%rax ; extract byte 27 mov %al, 0x1b(%rcx,%rbx) ; store mov %eax,%edx shr $0x18,%edx ; extract byte 28 mov %dl, 0x1c(%rcx,%rbx) ; store mov %eax,%edx shr $0x10,%edx ; extract byte 29 mov %dl, 0x1d(%rcx,%rbx) ; store mov %ah, 0x1e(%rcx,%rbx) ; store byte 30 mov %al, 0x1f(%rcx,%rbx) ; store byte 31 ``` On M1 every limb was generated byte-by-byte. This changeset is verified to generate moves + bswap. [^1]: rustc 1.94.0-nightly (2026-01-12) with LLVM 21.1.8.
DaniPopes
approved these changes
Apr 21, 2026
prestwich
approved these changes
Apr 21, 2026
Merging this PR will degrade performance by 40.08%
Performance Changes
Comparing |
Collaborator
|
decimal parsing regressions seem uimportant to me? cc @DaniPopes |
Contributor
|
looks like due to another compiler version bump? most of these benchmarks don't use this function 1.95 was released on 16 April and last main run was 7 April |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Special case the BYTES = LIMBS * 8 so to compel LLVM generate the proper machine code.
In the wild, it was observed that LLVM1 generated very silly code on x86-64,
Disassembly on x86-64
On M1 every limb was generated byte-by-byte.
This changeset is verified to generate moves + bswap.
Footnotes
rustc 1.94.0-nightly (2026-01-12) with LLVM 21.1.8. ↩