rust-lang / rust-lang/rust

DeadStoreElimination can create calls where return and arguments overlap

Open
#162,997 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-mir-opt C-bug I-miscompile I-prioritize T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

DeadStoreElimination wants to turn copy arguments into move arguments, but that transformation is not always valid. The pass does not account for this case:

_1 = f(copy _1);

but we also need a callee with codegen that clearly relies on the return place and argument not overlapping. This works, but uses custom_mir for both:

#![feature(custom_mir, core_intrinsics)]
#![allow(internal_features)]

use std::intrinsics::mir::*;

#[repr(C)]
struct Big {
    a: u64,
    b: u64,
    c: u64,
    d: u64,
    e: u64,
}

#[inline(never)]
#[custom_mir(dialect = "runtime")]
fn f(x: Big) -> Big {
    mir! {
        type RET = Big;
        {
            RET.a = 1;
            RET.b = x.a;
            RET.c = 0;
            RET.d = 0;
            RET.e = 0;
            Return()
        }
    }
}

#[inline(never)]
#[custom_mir(dialect = "runtime")]
fn caller(x: Big) -> Big {
    mir! {
        {
            Call(x = f(x), ReturnTo(bb1), UnwindUnreachable())
        }
        bb1 = {
            RET = x;
            Return()
        }
    }
}

fn main() {
    let x = Big { a: 42, b: 0, c: 0, d: 0, e: 0 };
    let y = caller(x);
    assert_eq!(y.b, 42);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DeadStoreElimination pass and run the supplied custom_mir Rust reproduction to observe the return-place and argument overlap. Trace the copy-to-move transformation for the shown call and ensure the completed change preserves the non-overlapping contract so the program's assertion passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.