Skip to content

i2p-p2i-opt miscompiles rust code #147538

@scottmcm

Description

@scottmcm

Making a separate tracking issue for the core problem @nikic identified in #147265 (comment) since it's been in stable since at least May 2024.

The following rust code: https://rust.godbolt.org/z/eT9fbKvbq

#[no_mangle]
unsafe fn f(a: usize) -> u8 {
    let b = g(a) as usize;
    unsafe { *(b as *const u8) }
}

#[cold]
fn g(a: usize) -> *const u8 {
    unsafe { std::mem::transmute(a) }
}

in 1.77 compiled to

f:
        movzx   eax, byte ptr [rdi]
        ret

but starting in 1.78 becomes

f:
        ud2

The change on the rust side is #121282 (cc @saethlin), but that's just exposing the problem in this example -- the root problem could probably be repro'd on older stable too, just in less-obvious ways.

It happens because LLVM removes the ptrtoint+inttoptr that's needed to recover exposed provenance from an otherwise-without_provenance pointer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeI-prioritizeIssue: Indicates that prioritization has been requested for this issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions