Skip to content

transmute::<u8, bool>() generates bad code #104832

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

Closed
ChayimFriedman2 opened this issue Nov 24, 2022 · 3 comments
Closed

transmute::<u8, bool>() generates bad code #104832

ChayimFriedman2 opened this issue Nov 24, 2022 · 3 comments
Labels
A-codegen Area: Code generation C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ChayimFriedman2
Copy link
Contributor

pub fn bar(v: u8) -> bool {
    unsafe { std::mem::transmute(v) }
}

Generates (https://rust.godbolt.org/z/xzYdeffxq, note the redundant and):

mov     eax, edi
and     al, 1
ret

This is because we generate a redundant trunc/zext:

// NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
// conversions allow handling `bool`s the same as `u8`s.
let src = bx.from_immediate(src.immediate());
let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout));
Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);

define zeroext i1 @_ZN7example3bar17h1b5c1a9c0782d25bE(i8 %v) unnamed_addr #0 {
  %0 = alloca i8, align 1
  %1 = trunc i8 %v to i1
  %2 = zext i1 %1 to i8
  store i8 %2, ptr %0, align 1
  %3 = load i8, ptr %0, align 1, !range !2, !noundef !3
  %4 = trunc i8 %3 to i1
  br label %bb1

bb1:                                              ; preds = %start
  ret i1 %4
}
@Rageking8
Copy link
Contributor

@rustbot label +C-bug +T-compiler +A-codegen +I-slow

@rustbot rustbot added A-codegen Area: Code generation C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 24, 2022
@deltragon
Copy link
Contributor

This seems related to #96140, where the transmute went the other way.
The conclusion there was:

#96140 (comment)

Yea, the only actionable thing I can see here is to remove the extra asm instructions for perf reasons or to allow follow up optimizations to trigger.

I guess let's close this and if someone encounters a repro for more complex programs that have missing opts we can revisit

@ChayimFriedman2
Copy link
Contributor Author

Yep, this is a dup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants