Skip to content

Discriminant update fails to optimize #122600

Closed
@joboet

Description

The following code should optimize to a single store of the new discriminant, but doesn't (Godbolt).

enum State {
    A([u8; 753]),
    B([u8; 753]),
    //C,
}

unsafe fn update(s: *mut State) {
    let S::A(v) = s.read() else { unreachable_unchecked() };
    s.write(S::B(v));
}

Instead, LLVM copies the value to the stack, copies it again and then writes it back. Interestingly, LLVM optimizes this correctly if a third state without value is added.

Meta

rustc --version --verbose:

rustc 1.78.0-nightly (c67326b06 2024-03-15)
binary: rustc
commit-hash: c67326b063bd27ed04f306ba2e372cd92e0a8751
commit-date: 2024-03-15
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions