Why does mir have so many temporaries?

https://play.rust-lang.org/?gist=c72e1e292dbfcbcc05d9e2929981acdc&version=nightly

fn add(_1: f32, _2: f32) -> f32 {
    let mut _0: f32;                     // return pointer
    scope 1 {
        let _3: f32;                     // "f" in scope 1 at src/main.rs:1:8: 1:9
        let _4: f32;                     // "f1" in scope 1 at src/main.rs:1:16: 1:18
    }
    let mut _5: f32;
    let mut _6: f32;

    bb0: {
        StorageLive(_3);                 // scope 0 at src/main.rs:1:8: 1:9
        _3 = _1;                         // scope 0 at src/main.rs:1:8: 1:9
        StorageLive(_4);                 // scope 0 at src/main.rs:1:16: 1:18
        _4 = _2;                         // scope 0 at src/main.rs:1:16: 1:18
        StorageLive(_5);                 // scope 1 at src/main.rs:2:5: 2:6
        _5 = _3;                         // scope 1 at src/main.rs:2:5: 2:6
        StorageLive(_6);                 // scope 1 at src/main.rs:2:9: 2:11
        _6 = _4;                         // scope 1 at src/main.rs:2:9: 2:11
        _0 = Add(_5, _6);                // scope 1 at src/main.rs:2:5: 2:11
        StorageDead(_6);                 // scope 1 at src/main.rs:2:11: 2:11
        StorageDead(_5);                 // scope 1 at src/main.rs:2:11: 2:11
        StorageDead(_4);                 // scope 0 at src/main.rs:3:2: 3:2
        StorageDead(_3);                 // scope 0 at src/main.rs:3:2: 3:2
        return;                          // scope 1 at src/main.rs:3:2: 3:2
    }
}

Couldn’t this just become

fn add(_1: f32, _2: f32) -> f32 {
    let mut _0: f32;                     // return pointer
    scope 1 {
        let _3: f32;                     // "f" in scope 1 at src/main.rs:1:8: 1:9
        let _4: f32;                     // "f1" in scope 1 at src/main.rs:1:16: 1:18
    }

    bb0: {
        StorageLive(_3);                 // scope 0 at src/main.rs:1:8: 1:9
        _3 = _1;                         // scope 0 at src/main.rs:1:8: 1:9
        StorageLive(_4);                 // scope 0 at src/main.rs:1:16: 1:18
        _4 = _2;                         // scope 0 at src/main.rs:1:16: 1:18
        _0 = Add(_3, _4);                // scope 1 at src/main.rs:2:5: 2:11
        StorageDead(_4);                 // scope 0 at src/main.rs:3:2: 3:2
        StorageDead(_3);                 // scope 0 at src/main.rs:3:2: 3:2
        return;                          // scope 1 at src/main.rs:3:2: 3:2
    }
}

Or even better without the _3 and _4 temps?

This is currently my SPIR-V output from mir vs GLSL. https://gist.github.com/MaikKlein/17732fea9066e2051a6cdcef3ae80323

1 Like

I’m pretty certain that the playground does not apply mir optimizations. Try running locally with -Zmir-opt-level=3 to get rid of any clutter.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.