-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Bad codegen with simple match statement #68867
Comments
Also cc @nox / @SimonSapin as servo uses this kind of pattern quite a lot too |
I wonder if this affects the built-in |
Seems like |
However a manually implemented version of that is: https://rust.godbolt.org/z/fmYvsb It'd be good to know what kind of code does rustc generate for this case... Does it exploit internals to poke at the representation directly? |
The code that the derive expands to is: https://rust.godbolt.org/z/P3ptVh |
Might be a good candidate for a MIR opt. I believe LLVM has historically shied away from doing this kind of thing out of compile time concerns. |
Cool, TIL! So the |
|
Ah, true! And |
I've always wanted to write such a peephole optimisation for match expressions over That's a low-hanging fruit and I suspect will bring improvements all across the board but days are still only 24 hours long. |
Cf this, too: servo/servo@456c18f |
…ComputeSquaredDistance). See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761
…duced here. See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760
…derive(ComputeSquaredDistance). r=heycam See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761
…s introduced here. r=heycam See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760
…derive(ComputeSquaredDistance). r=heycam See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761 --HG-- extra : moz-landing-system : lando
…s introduced here. r=heycam See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760 --HG-- extra : moz-landing-system : lando
…ComputeSquaredDistance). See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761
…duced here. See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760
…derive(ComputeSquaredDistance). r=heycam See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761 UltraBlame original commit: bd4711d31ec6ab173b77406cb4ede0fa7a484af3
…s introduced here. r=heycam See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760 UltraBlame original commit: c083885bd068a47a2e8890ad2e8541b1a635cdf1
…ComputeSquaredDistance). See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761
…duced here. See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760
…derive(ComputeSquaredDistance). r=heycam See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761 UltraBlame original commit: bd4711d31ec6ab173b77406cb4ede0fa7a484af3
…s introduced here. r=heycam See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760 UltraBlame original commit: c083885bd068a47a2e8890ad2e8541b1a635cdf1
…derive(ComputeSquaredDistance). r=heycam See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761 UltraBlame original commit: bd4711d31ec6ab173b77406cb4ede0fa7a484af3
…s introduced here. r=heycam See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760 UltraBlame original commit: c083885bd068a47a2e8890ad2e8541b1a635cdf1
Yeah, other workarounds for this include servo/servo@8870ae7 and servo/servo@cfa8aee. |
…ComputeSquaredDistance). See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761
…duced here. See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760
After #75119 final asm appears much better. type CSSFloat = f32;
pub enum ViewportPercentageLength {
Vw(CSSFloat),
Vh(CSSFloat),
Vmin(CSSFloat),
Vmax(CSSFloat),
}
impl ViewportPercentageLength {
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
use self::ViewportPercentageLength::*;
Ok(match (self, other) {
(&Vw(one), &Vw(other)) => Vw(one + other),
(&Vh(one), &Vh(other)) => Vh(one + other),
(&Vmin(one), &Vmin(other)) => Vmin(one + other),
(&Vmax(one), &Vmax(other)) => Vmax(one + other),
_ => return Err(()),
})
}
}
#[no_mangle]
pub extern "C" fn sum_them(
one: &ViewportPercentageLength,
other: &ViewportPercentageLength,
out: &mut ViewportPercentageLength,
) -> bool {
match one.try_sum(other) {
Ok(v) => {
*out = v;
true
}
Err(()) => false,
}
}
fn main() {} before: rustup run nightly-2020-09-20 rustc 68867.rs --emit=asm -O -C opt-level=3 -o 2020-09-20.s .section .text.sum_them,"ax",@progbits
.globl sum_them
.p2align 4, 0x90
.type sum_them,@function
sum_them:
.cfi_startproc
movl (%rdi), %eax
movss 4(%rdi), %xmm0
movl (%rsi), %ecx
movss 4(%rsi), %xmm1
leaq .LJTI5_0(%rip), %rsi
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
.LBB5_1:
xorl %eax, %eax
testl %ecx, %ecx
je .LBB5_8
retq
.LBB5_3:
movl $2, %eax
cmpl $2, %ecx
je .LBB5_8
.LBB5_9:
xorl %eax, %eax
retq
.LBB5_5:
movl $3, %eax
cmpl $3, %ecx
jne .LBB5_9
.LBB5_8:
addss %xmm1, %xmm0
movl %eax, (%rdx)
movss %xmm0, 4(%rdx)
movb $1, %al
retq
.LBB5_7:
movl $1, %eax
cmpl $1, %ecx
jne .LBB5_9
jmp .LBB5_8
.Lfunc_end5:
.size sum_them, .Lfunc_end5-sum_them
.cfi_endproc
.section .rodata.sum_them,"a",@progbits
.p2align 2
.LJTI5_0:
.long .LBB5_1-.LJTI5_0
.long .LBB5_7-.LJTI5_0
.long .LBB5_3-.LJTI5_0
.long .LBB5_5-.LJTI5_0 after: rustup run nightly-2020-09-21 rustc 68867.rs --emit=asm -O -C opt-level=3 -o 2020-09-21.s .section .text.sum_them,"ax",@progbits
.globl sum_them
.p2align 4, 0x90
.type sum_them,@function
sum_them:
.cfi_startproc
movl (%rdi), %eax
cmpl %eax, (%rsi)
jne .LBB5_1
movss 4(%rdi), %xmm0
addss 4(%rsi), %xmm0
movl %eax, (%rdx)
movss %xmm0, 4(%rdx)
movb $1, %al
retq
.LBB5_1:
xorl %eax, %eax
retq
.Lfunc_end5:
.size sum_them, .Lfunc_end5-sum_them
.cfi_endproc |
Yeah indeed, thanks for fixing this! |
The following code:
Generates the following assembly on Rust Nightly when compiled with
-C opt-level=3
:Godbolt link: https://rust.godbolt.org/z/JfkEez
It seems to generate one branch for each case of the statement, when I would've expected it to look more like:
cc @michaelwoerister @heycam
The text was updated successfully, but these errors were encountered: