-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-codegenArea: Code generationArea: Code generationA-mir-optArea: MIR optimizationsArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
In this example:
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum Variant {
Zero,
One,
Two,
}
#[inline]
fn unreachable() {
println!("Impossible");
}
extern {
fn exf1();
fn exf2();
}
#[no_mangle]
pub static mut GLOBAL: Variant = Variant::Zero;
pub unsafe fn test() {
let g = GLOBAL;
if g != Variant::Zero {
match g {
Variant::One => exf1(),
Variant::Two => exf2(),
Variant::Zero => unreachable(),
}
}
}
the unreachable()
branch is not removed. Adding any #[repr(n)]
but not #[repr(C)]
to Variant
or making reachable branches call only one of external functions allows the optimization.
There's no such issue with a similar C example using clang so it's likely something on Rust side.
Example links:
Rust https://rust.godbolt.org/z/9oh6sP
C https://godbolt.org/z/Tnbfx6
Metadata
Metadata
Assignees
Labels
A-codegenArea: Code generationArea: Code generationA-mir-optArea: MIR optimizationsArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.