rust-lang / rust

Empowering everyone to build reliable and efficient software.

Home Page:https://www.rust-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ICE: `left: MutatingUse(Call), right: NonUse(VarDebugInfo)`

cbeuw opened this issue · comments

Code

The crash probably has something to do with an MIR back edge to a basic block terminated by a Call with RET as destination.

This can only be triggered with -Zmir-opt-level=2. For some reason it doesn't happen with -Zmir-opt-level=3

#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;

pub fn a() -> char {
    'a'
}

#[custom_mir(dialect = "runtime", phase = "initial")]
pub fn ice(arg: char) -> char {
    mir! (
    let c: char;
    let discr: i32;
    {
        Goto(bb1)
    }
    bb1 = {
        RET = 'a';
        c = RET;
        Call(RET, bb2, a())
    }
    bb2 = {
        c = arg;
        RET = c;
        discr = 42;
        match discr {
            0 => bb1,
            _ => bb3
        }
    }
    bb3 = {
        Return()
    })
}

pub fn main() {
    ice('a');
}

Meta

rustc --version --verbose:

rustc 1.71.0-nightly (1c42cb4ef 2023-04-26)
binary: rustc
commit-hash: 1c42cb4ef0544fbfaa500216e53382d6b079c001
commit-date: 2023-04-26
host: aarch64-apple-darwin
release: 1.71.0-nightly
LLVM version: 16.0.2

Error output

thread 'rustc' panicked at 'assertion failed: `(left == right)`
  left: `MutatingUse(Call)`,
 right: `NonUse(VarDebugInfo)`', compiler/rustc_mir_transform/src/nrvo.rs:204:13
stack backtrace:
   0:        0x104e2d680 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h2e4b316da8d1b868
   1:        0x104e7d624 - core::fmt::write::hee60b630fddd701c
   2:        0x104e23590 - std::io::Write::write_fmt::h7829782ffee0a74a
   3:        0x104e2d4e0 - std::sys_common::backtrace::print::h69a078fe21ab83fc
   4:        0x104e2fea0 - std::panicking::default_hook::{{closure}}::h5166913d03826b1e
   5:        0x104e2fca4 - std::panicking::default_hook::h4211b85bce80d367
   6:        0x10d2b7f80 - rustc_driver_impl[d2744e2f64f48052]::DEFAULT_HOOK::{closure#0}::{closure#0}
   7:        0x104e30494 - std::panicking::rust_panic_with_hook::h4c1dd5e7fe26462e
   8:        0x104e302a8 - std::panicking::begin_panic_handler::{{closure}}::h0964cad812c6bc09
   9:        0x104e2da7c - std::sys_common::backtrace::__rust_end_short_backtrace::h18678632515b788f
  10:        0x104e3003c - _rust_begin_unwind
  11:        0x104ea8e98 - core::panicking::panic_fmt::hd45ba9707abd3d04
  12:        0x104ea9200 - core::panicking::assert_failed_inner::h6f72dad26ebd5426
  13:        0x11156b27c - core[13006027e59f563b]::panicking::assert_failed::<rustc_middle[4bd1076e66066330]::mir::visit::PlaceContext, rustc_middle[4bd1076e66066330]::mir::visit::PlaceContext>
  14:        0x10fa2cdf4 - <rustc_mir_transform[ac6be147cdb829a7]::nrvo::RenameToReturnPlace as rustc_middle[4bd1076e66066330]::mir::visit::MutVisitor>::visit_place
  15:        0x10fa342ec - <rustc_mir_transform[ac6be147cdb829a7]::nrvo::RenameReturnPlace as rustc_middle[4bd1076e66066330]::mir::MirPass>::run_pass
  16:        0x10fb0057c - rustc_mir_transform[ac6be147cdb829a7]::pass_manager::run_passes_inner
  17:        0x10f9f9e08 - rustc_mir_transform[ac6be147cdb829a7]::optimized_mir
  18:        0x110406b40 - rustc_query_system[dfd69d1f88b4517]::query::plumbing::try_execute_query::<rustc_query_impl[4fb062e88122548a]::queries::optimized_mir, rustc_query_impl[4fb062e88122548a]::plumbing::QueryCtxt>
  19:        0x11065bed8 - <rustc_query_impl[4fb062e88122548a]::Queries as rustc_middle[4bd1076e66066330]::ty::query::QueryEngine>::optimized_mir
  20:        0x1111c9c34 - <rustc_middle[4bd1076e66066330]::ty::context::TyCtxt>::instance_mir
  21:        0x10f995a58 - rustc_monomorphize[6f51a5b3b0e2afbf]::collector::collect_neighbours
  22:        0x10f994368 - rustc_monomorphize[6f51a5b3b0e2afbf]::collector::collect_items_rec
  23:        0x10f9b5ab0 - <core[13006027e59f563b]::panic::unwind_safe::AssertUnwindSafe<rustc_data_structures[a04248f14933d50e]::sync::par_for_each_in<alloc[4d30c02dd49f99e9]::vec::Vec<rustc_middle[4bd1076e66066330]::mir::mono::MonoItem>, rustc_monomorphize[6f51a5b3b0e2afbf]::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}> as core[13006027e59f563b]::ops::function::FnOnce<()>>::call_once
  24:        0x10f9a8fa8 - rustc_data_structures[a04248f14933d50e]::sync::par_for_each_in::<alloc[4d30c02dd49f99e9]::vec::Vec<rustc_middle[4bd1076e66066330]::mir::mono::MonoItem>, rustc_monomorphize[6f51a5b3b0e2afbf]::collector::collect_crate_mono_items::{closure#1}::{closure#0}>
  25:        0x10f9b4258 - <rustc_session[a15cea41fa418eb5]::session::Session>::time::<(), rustc_monomorphize[6f51a5b3b0e2afbf]::collector::collect_crate_mono_items::{closure#1}>
  26:        0x10f992828 - rustc_monomorphize[6f51a5b3b0e2afbf]::collector::collect_crate_mono_items
  27:        0x10f9a63a8 - rustc_monomorphize[6f51a5b3b0e2afbf]::partitioning::collect_and_partition_mono_items
  28:        0x1104cf96c - rustc_query_system[dfd69d1f88b4517]::query::plumbing::try_execute_query::<rustc_query_impl[4fb062e88122548a]::queries::collect_and_partition_mono_items, rustc_query_impl[4fb062e88122548a]::plumbing::QueryCtxt>
  29:        0x1106710c8 - <rustc_query_impl[4fb062e88122548a]::Queries as rustc_middle[4bd1076e66066330]::ty::query::QueryEngine>::collect_and_partition_mono_items
  30:        0x1107563ec - rustc_codegen_ssa[fd6d44b2de446342]::back::symbol_export::exported_symbols_provider_local
  31:        0x110430a8c - rustc_query_system[dfd69d1f88b4517]::query::plumbing::try_execute_query::<rustc_query_impl[4fb062e88122548a]::queries::exported_symbols, rustc_query_impl[4fb062e88122548a]::plumbing::QueryCtxt>
  32:        0x110670f04 - <rustc_query_impl[4fb062e88122548a]::Queries as rustc_middle[4bd1076e66066330]::ty::query::QueryEngine>::exported_symbols
  33:        0x110a1e08c - <rustc_metadata[6e54bd569d7c9f96]::rmeta::encoder::EncodeContext>::encode_crate_root
  34:        0x110a29000 - rustc_metadata[6e54bd569d7c9f96]::rmeta::encoder::encode_metadata_impl
  35:        0x1109fc580 - rustc_data_structures[a04248f14933d50e]::sync::join::<rustc_metadata[6e54bd569d7c9f96]::rmeta::encoder::encode_metadata::{closure#0}, rustc_metadata[6e54bd569d7c9f96]::rmeta::encoder::encode_metadata::{closure#1}, (), ()>
  36:        0x110a28824 - rustc_metadata[6e54bd569d7c9f96]::rmeta::encoder::encode_metadata
  37:        0x110a0dcf8 - rustc_metadata[6e54bd569d7c9f96]::fs::encode_and_write_metadata
  38:        0x10d3638cc - rustc_interface[87946e0f114a4094]::passes::start_codegen
  39:        0x10d36ad94 - <rustc_middle[4bd1076e66066330]::ty::context::GlobalCtxt>::enter::<<rustc_interface[87946e0f114a4094]::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core[13006027e59f563b]::result::Result<alloc[4d30c02dd49f99e9]::boxed::Box<dyn core[13006027e59f563b]::any::Any>, rustc_span[a7e895295122ec2d]::ErrorGuaranteed>>
  40:        0x10d31addc - <rustc_interface[87946e0f114a4094]::queries::Queries>::ongoing_codegen
  41:        0x10d29497c - <rustc_interface[87946e0f114a4094]::interface::Compiler>::enter::<rustc_driver_impl[d2744e2f64f48052]::run_compiler::{closure#1}::{closure#2}, core[13006027e59f563b]::result::Result<core[13006027e59f563b]::option::Option<rustc_interface[87946e0f114a4094]::queries::Linker>, rustc_span[a7e895295122ec2d]::ErrorGuaranteed>>
  42:        0x10d259430 - <scoped_tls[c0382597cdb10fcc]::ScopedKey<rustc_span[a7e895295122ec2d]::SessionGlobals>>::set::<rustc_interface[87946e0f114a4094]::interface::run_compiler<core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>, rustc_driver_impl[d2744e2f64f48052]::run_compiler::{closure#1}>::{closure#0}, core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>>
  43:        0x10d2649e4 - std[c618651f4d1565b5]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[87946e0f114a4094]::util::run_in_thread_pool_with_globals<rustc_interface[87946e0f114a4094]::interface::run_compiler<core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>, rustc_driver_impl[d2744e2f64f48052]::run_compiler::{closure#1}>::{closure#0}, core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>>
  44:        0x10d254c20 - <<std[c618651f4d1565b5]::thread::Builder>::spawn_unchecked_<rustc_interface[87946e0f114a4094]::util::run_in_thread_pool_with_globals<rustc_interface[87946e0f114a4094]::interface::run_compiler<core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>, rustc_driver_impl[d2744e2f64f48052]::run_compiler::{closure#1}>::{closure#0}, core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[13006027e59f563b]::result::Result<(), rustc_span[a7e895295122ec2d]::ErrorGuaranteed>>::{closure#1} as core[13006027e59f563b]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  45:        0x104e38be4 - std::sys::unix::thread::Thread::new::thread_start::hd4c5932775b29ea0
  46:        0x187e6bfa8 - __pthread_joiner_wake

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.71.0-nightly (1c42cb4ef 2023-04-26) running on aarch64-apple-darwin

note: compiler flags: --crate-type lib -Z mir-opt-level=2

query stack during panic:
#0 [optimized_mir] optimizing MIR for `ice`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
#2 [exported_symbols] collecting exported symbols for crate `0`
end of query stack
warning: 1 warning emitted

@matthiaskrgr If you're saying my custom MIR is invalid and may never be generated by the normal pipeline, then it should be documented and ideally picked up by the validation pass.

In any case, the problematic MIR can be generated from normal Rust and trigger the same ICE with -Zmir-opt-level=3, so the bug is not in my custom MIR, and one has to look at MIR anyway to debug it 🤷

pub fn a() -> i32 {
    1
}
pub fn b(_: i32) {}

pub fn ice(arg: i32, discr: bool) -> i32 {
    loop {
        let mut c = 1;
        let mut ret = a();

        c = !arg;
        ret = c;
        if discr {
            b(c);
            return ret;
        }
    }
}

pub fn main() {
    ice(1, false);
}

cc @cjgillot and @JakobDegen who seem to know quite a bit about MIR optimisations? (I don't really know who's worked on these 😅)

I see fuzzing is working :) . I'll take a look tonight

Oh, and to confirm, yes - validation missing Mir that is not well-formed is a bug too, at least to the extent that that limitation of the validator is not known and clearly documented.

Sorry, what I ment is this has always been crashing at least since custom mir supported it.
From what I remember custom mir was kind of an internal feature so crashes are expected.
@JakobDegen do we expect rustc to not crash on custon mir / core _intrinsics?
I have seen probably already 15-20 custom mir ices but as far as I remember there were not really worth reporting, is that wrong?

@matthiaskrgr custom_mir is definitely expected to cause ICEs in some cases - it depends on the type of ICE and it requires a little bit of domain knowledge to figure out which ones are ok and which ones aren't. In this case, the important point is that this there's no ICE up to and through the point where Mir validation initially runs at the Runtime::Initial phase. That's supposed to (approximately) mean that the Mir is fine, and so the fact that the ICE appears later is a good sign that this is an actual bug

Found #111005 while looking at this - not the exact same issue though

The linked PR disables the opt, which will mitigate the ICE. As I said there, the pass makes some sketchy assumptions, so this is not a super straight-forward fix. I'm not going to be looking at this though, someone else is free to pick it up.