Skip to content

Unused MIR statement directly before return #70531

Closed

Description

@bjorn3
$ rustc foo.rs --crate-type lib --emit mir -Cpanic=abort
fn map(x: Option<Box<()>>) -> Option<Box<()>> {
    match x {
        None => None,
        Some(x) => Some(x),
    }
}
// WARNING: This output format is intended for human consumers only
// and is subject to change without notice. Knock yourself out.
fn  map(_1: std::option::Option<std::boxed::Box<()>>) -> std::option::Option<std::boxed::Box<()>> {
    debug x => _1;                       // in scope 0 at a.rs:1:8: 1:9
    let mut _0: std::option::Option<std::boxed::Box<()>>; // return place in scope 0 at a.rs:1:31: 1:46
    let mut _2: isize;                   // in scope 0 at a.rs:3:9: 3:13
    let _3: std::boxed::Box<()>;         // in scope 0 at a.rs:4:14: 4:15
    let mut _4: std::boxed::Box<()>;     // in scope 0 at a.rs:4:25: 4:26
    let mut _5: isize;                   // in scope 0 at a.rs:6:1: 6:2
    scope 1 {
        debug x => _3;                   // in scope 1 at a.rs:4:14: 4:15
    }

    bb0: {
        _2 = discriminant(_1);           // bb0[0]: scope 0 at a.rs:3:9: 3:13
        switchInt(move _2) -> [0isize: bb3, 1isize: bb1, otherwise: bb2]; // bb0[1]: scope 0 at a.rs:3:9: 3:13
    }

    bb1: {
        StorageLive(_3);                 // bb1[0]: scope 0 at a.rs:4:14: 4:15
        _3 = move ((_1 as Some).0: std::boxed::Box<()>); // bb1[1]: scope 0 at a.rs:4:14: 4:15
        StorageLive(_4);                 // bb1[2]: scope 1 at a.rs:4:25: 4:26
        _4 = move _3;                    // bb1[3]: scope 1 at a.rs:4:25: 4:26
        ((_0 as Some).0: std::boxed::Box<()>) = move _4; // bb1[4]: scope 1 at a.rs:4:20: 4:27
        discriminant(_0) = 1;            // bb1[5]: scope 1 at a.rs:4:20: 4:27
        StorageDead(_4);                 // bb1[6]: scope 1 at a.rs:4:26: 4:27
        StorageDead(_3);                 // bb1[7]: scope 0 at a.rs:4:27: 4:28
        goto -> bb4;                     // bb1[8]: scope 0 at a.rs:2:5: 5:6
    }

    bb2: {
        unreachable;                     // bb2[0]: scope 0 at a.rs:2:11: 2:12
    }

    bb3: {
        discriminant(_0) = 0;            // bb3[0]: scope 0 at a.rs:3:17: 3:21
        goto -> bb4;                     // bb3[1]: scope 0 at a.rs:2:5: 5:6
    }

    bb4: {
        _5 = discriminant(_1);           // bb4[0]: scope 0 at a.rs:6:1: 6:2
        return;                          // bb4[1]: scope 0 at a.rs:6:2: 6:2
    }
}

The _5 = discriminant(_1); doesn't have any effect.

(Labeling A-cranelift because this prevents codegen of the goto -> bb4 like return. Which may result in a few extra instructions being emitted to move registers and jump to the return block for cg_clif.)

@rustbot modify labels: +A-mir +A-mir-opt +A-cranelift

Activity

  1. added
    A-craneliftThings relevant to the [future] cranelift backend
    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
    A-mir-optArea: MIR optimizations
    on Mar 29, 2020
  2. added
    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.
    on Mar 29, 2020
  3. jonas-schievink commented on Mar 29, 2020

    @jonas-schievink
    Contributor

    We already have a pass that propagates the unreachable terminator, it should be possible to make it handle return as well.

    (however return can leave sideeffects visible to the caller, so maybe this should be a proper dead code elimination pass instead)

  4. ecstatic-morse commented on Mar 29, 2020

    @ecstatic-morse
    Contributor

    For background, this is a side-effect of #68528 (see the MIR diagram in the PR summary) and #68943. The MIR for the above example used to contain a frivolous open drop of x. After my changes to drop elaboration, all branches of the open drop become no-op Gotos, which are then removed by the SimplifyCfg pass. The read of the discriminant as part of the open drop remains, however.

    To my knowledge, we don't have a dead-code elimination pass for the MIR, which would be the proper way to handle this.

  5. self-assigned this
    on Mar 30, 2020
  6. added 2 commits that reference this issue on Apr 2, 2020
    b205742
    4cba69e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-craneliftThings relevant to the [future] cranelift backendA-mir-optArea: MIR optimizationsC-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.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions