Skip to content

[mir-opt] avoid *& when reading primitive from slice #138936

Description

@scottmcm

Today, this code https://play.rust-lang.org/?version=nightly&mode=release&edition=2024&gist=09a5eaf64b3be527fef5a0589d6fe377

use std::cmp::Ordering;
pub fn foo(x: [i32; 1], y: [i32; 1]) -> Ordering {
    Ord::cmp(&x[0], &y[0])
}

gives this optimized MIR:

fn foo(_1: [i32; 1], _2: [i32; 1]) -> std::cmp::Ordering {
    debug x => _1;
    debug y => _2;
    let mut _0: std::cmp::Ordering;
    let _3: &i32;
    let _4: &i32;
    scope 1 (inlined std::cmp::impls::<impl Ord for i32>::cmp) {
        let mut _5: i32;
        let mut _6: i32;
    }

    bb0: {
        _3 = &_1[0 of 1];
        _4 = &_2[0 of 1];
        StorageLive(_5);
        _5 = copy (*_3);
        StorageLive(_6);
        _6 = copy (*_4);
        _0 = Cmp(move _5, move _6);
        StorageDead(_6);
        StorageDead(_5);
        return;
    }
}

It's a shame that we need to take a reference there in the MIR instead of just copying out of the place directly, like

    bb0: {
        StorageLive(_5);
        _5 = copy _1[0 of 1];
        StorageLive(_6);
        _6 = copy _2[0 of 1];
        _0 = Cmp(move _5, move _6);
        StorageDead(_6);
        StorageDead(_5);
        return;
    }

@cjgillot, is this something you think GVN could plausibly do?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsA-mir-opt-GVNArea: MIR opt Global Value Numbering (GVN)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions