Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIR opts ICE or not depending on file name being compiled #116294

Closed
saethlin opened this issue Sep 30, 2023 · 7 comments · Fixed by #116896
Closed

MIR opts ICE or not depending on file name being compiled #116294

saethlin opened this issue Sep 30, 2023 · 7 comments · Fixed by #116896
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saethlin
Copy link
Member

While trying to build a test for #116291, I renamed my test file and it stopped ICEing. I have no idea what's happening here.

Create a file with this contents called demo.rs

fn main() {
    let func = || 123u8;
    func();
}

Then

cp demo.rs demoo.rs

This compiles fine:

rustc +nightly -Zmir-opt-level=0 -Zmir-enable-passes=+Inline demoo.rs

But this ICEs:

rustc +nightly -Zmir-opt-level=0 -Zmir-enable-passes=+Inline demo.rs
@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Sep 30, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 30, 2023
@cjgillot
Copy link
Contributor

cjgillot commented Oct 1, 2023

// Avoid a cycle here by only using `instance_mir` only if we have
// a lower `DefPathHash` than the callee. This ensures that the callee will
// not inline us. This trick even works with incremental compilation,
// since `DefPathHash` is stable.
if self.tcx.def_path_hash(caller_def_id).local_hash()
< self.tcx.def_path_hash(callee_def_id).local_hash()
{
return Ok(());
}

We skip calling mir_callgraph_reachable by comparing def-path hashes. The def-path hash provides a total order on MIR bodies that respects incremental compilation, and avoids a potentially costly query.

OTOH, it depends on the crate name, which is the file name in your example. If you add a #[crate_name = "blah"] somewhere, that should be reproducible.

@saethlin
Copy link
Member Author

saethlin commented Oct 2, 2023

Setting the crate name would stabilize this for now, but any change to the hash algorithm could cause this test to suddenly pass, even if the compiler regresses in its behavior. Is there a way around that problem?

@thomcc
Copy link
Member

thomcc commented Oct 2, 2023

As someone who has changed the hash algorithm before, a comment in the file that says you might have to fiddle with the crate name to find something where that check works (including a link to this issue) might be enough if there's no other option.

@saethlin
Copy link
Member Author

saethlin commented Oct 2, 2023

I'm not concerned about the test suddenly failing, I'm concerned about it suddenly passing when it shouldn't.

@apiraino
Copy link
Contributor

apiraino commented Oct 18, 2023

@saethlin I can't reproduce the MCVE in your opening comment. However, if I rename the file to main.rs then it emits an ICE (which I suspect is the same you reported here, is it?

$ rustc +nightly -Zmir-opt-level=0 -Zmir-enable-passes=+Inline main.rs 
thread 'rustc' panicked at compiler/rustc_mir_transform/src/inline/cycle.rs:156:27:
attempted to read from stolen value: rustc_middle::mir::Body
stack backtrace:
   0:     0x7f7aaa59ffbc - std::backtrace_rs::backtrace::libunwind::trace::hd6a7c2408f5003fe
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f7aaa59ffbc - std::backtrace_rs::backtrace::trace_unsynchronized::hbbc68487a03350de
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f7aaa59ffbc - std::sys_common::backtrace::_print_fmt::h4620261dcde397d9
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7f7aaa59ffbc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5e8e1dcae280ebdd
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f7aaa607a50 - core::fmt::rt::Argument::fmt::h4f50feaa84de76c4
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/core/src/fmt/rt.rs:142:9
   5:     0x7f7aaa607a50 - core::fmt::write::h2391d78b55686755
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/core/src/fmt/mod.rs:1117:17
   6:     0x7f7aaa59326f - std::io::Write::write_fmt::h08bc60a5812bedb0
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/io/mod.rs:1762:15
   7:     0x7f7aaa59fda4 - std::sys_common::backtrace::_print::h3ac59787d4916530
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f7aaa59fda4 - std::sys_common::backtrace::print::h6b91567860602c48
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f7aaa5a2bd7 - std::panicking::default_hook::{{closure}}::h18adb66df1e0675c
  10:     0x7f7aaa5a291a - std::panicking::default_hook::hcdf907f977ae0434
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/panicking.rs:292:9
  11:     0x7f7aa7725d2a - std[a539495bdaa40edd]::panicking::update_hook::<alloc[6ee63e62fc072b57]::boxed::Box<rustc_driver_impl[90397df04d7f8043]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f7aaa5a3378 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h074c8a69d92d0c6f
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/alloc/src/boxed.rs:2021:9
  13:     0x7f7aaa5a3378 - std::panicking::rust_panic_with_hook::hf8b62b650b17db03
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/panicking.rs:735:13
  14:     0x7f7aaa5a30d1 - std::panicking::begin_panic_handler::{{closure}}::h20b26ce07b8fc4d3
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/panicking.rs:609:13
  15:     0x7f7aaa5a04d6 - std::sys_common::backtrace::__rust_end_short_backtrace::h748e550b55758a6e
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/sys_common/backtrace.rs:170:18
  16:     0x7f7aaa5a2e02 - rust_begin_unwind
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/panicking.rs:597:5
  17:     0x7f7aaa604075 - core::panicking::panic_fmt::h6004d4095008084f
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/core/src/panicking.rs:72:14
  18:     0x7f7aa914b38e - rustc_mir_transform[3f48c9ea3e99e87e]::inline::cycle::mir_inliner_callees
  19:     0x7f7aa914aba8 - rustc_query_impl[a315f5f311a78fca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a315f5f311a78fca]::query_impl::mir_inliner_callees::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 16usize]>>
  20:     0x7f7aa914ab67 - <rustc_query_impl[a315f5f311a78fca]::query_impl::mir_inliner_callees::dynamic_query::{closure#2} as core[8479cc789c9f689c]::ops::function::FnOnce<(rustc_middle[3b5a5dff77aaafdc]::ty::context::TyCtxt, rustc_middle[3b5a5dff77aaafdc]::ty::instance::InstanceDef)>>::call_once
  21:     0x7f7aa914a60e - rustc_query_system[c2422e4c6a409a1d]::query::plumbing::try_execute_query::<rustc_query_impl[a315f5f311a78fca]::DynamicConfig<rustc_query_system[c2422e4c6a409a1d]::query::caches::DefaultCache<rustc_middle[3b5a5dff77aaafdc]::ty::instance::InstanceDef, rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[a315f5f311a78fca]::plumbing::QueryCtxt, false>
  22:     0x7f7aa914a3bf - rustc_query_impl[a315f5f311a78fca]::query_impl::mir_inliner_callees::get_query_non_incr::__rust_end_short_backtrace
  23:     0x7f7aa94eaa09 - rustc_mir_transform[3f48c9ea3e99e87e]::inline::cycle::mir_callgraph_reachable::process
  24:     0x7f7aa94e87af - rustc_mir_transform[3f48c9ea3e99e87e]::inline::cycle::mir_callgraph_reachable
  25:     0x7f7aa94e86ad - rustc_query_impl[a315f5f311a78fca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a315f5f311a78fca]::query_impl::mir_callgraph_reachable::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 1usize]>>
  26:     0x7f7aa94e867f - <rustc_query_impl[a315f5f311a78fca]::query_impl::mir_callgraph_reachable::dynamic_query::{closure#2} as core[8479cc789c9f689c]::ops::function::FnOnce<(rustc_middle[3b5a5dff77aaafdc]::ty::context::TyCtxt, (rustc_middle[3b5a5dff77aaafdc]::ty::instance::Instance, rustc_span[123652b6fc40218d]::def_id::LocalDefId))>>::call_once
  27:     0x7f7aa94e84b0 - rustc_query_system[c2422e4c6a409a1d]::query::plumbing::try_execute_query::<rustc_query_impl[a315f5f311a78fca]::DynamicConfig<rustc_query_system[c2422e4c6a409a1d]::query::caches::DefaultCache<(rustc_middle[3b5a5dff77aaafdc]::ty::instance::Instance, rustc_span[123652b6fc40218d]::def_id::LocalDefId), rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a315f5f311a78fca]::plumbing::QueryCtxt, false>
  28:     0x7f7aa94e825e - rustc_query_impl[a315f5f311a78fca]::query_impl::mir_callgraph_reachable::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7f7aa90cfd90 - <rustc_mir_transform[3f48c9ea3e99e87e]::inline::Inliner>::try_inlining
  30:     0x7f7aa88cdae0 - <rustc_mir_transform[3f48c9ea3e99e87e]::inline::Inliner>::process_blocks
  31:     0x7f7aa894888a - <rustc_mir_transform[3f48c9ea3e99e87e]::inline::Inline as rustc_middle[3b5a5dff77aaafdc]::mir::MirPass>::run_pass
  32:     0x7f7aa88e6d5b - rustc_mir_transform[3f48c9ea3e99e87e]::pass_manager::run_passes_inner
  33:     0x7f7aa8ec73f0 - rustc_mir_transform[3f48c9ea3e99e87e]::optimized_mir
  34:     0x7f7aa884fe9d - rustc_query_impl[a315f5f311a78fca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a315f5f311a78fca]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 8usize]>>
  35:     0x7f7aa884f385 - rustc_query_system[c2422e4c6a409a1d]::query::plumbing::try_execute_query::<rustc_query_impl[a315f5f311a78fca]::DynamicConfig<rustc_query_system[c2422e4c6a409a1d]::query::caches::DefaultCache<rustc_span[123652b6fc40218d]::def_id::DefId, rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[a315f5f311a78fca]::plumbing::QueryCtxt, false>
  36:     0x7f7aa884ebde - rustc_query_impl[a315f5f311a78fca]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  37:     0x7f7aa5c82351 - <rustc_middle[3b5a5dff77aaafdc]::ty::context::TyCtxt>::instance_mir
  38:     0x7f7aa6a45357 - rustc_monomorphize[931c96c42f7f6df4]::collector::collect_items_rec::{closure#0}
  39:     0x7f7aa960a553 - rustc_monomorphize[931c96c42f7f6df4]::collector::collect_items_rec
  40:     0x7f7aa967cd46 - rustc_monomorphize[931c96c42f7f6df4]::partitioning::collect_and_partition_mono_items
  41:     0x7f7aa967c1e4 - rustc_query_impl[a315f5f311a78fca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a315f5f311a78fca]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 24usize]>>
  42:     0x7f7aa967c1c9 - <rustc_query_impl[a315f5f311a78fca]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[8479cc789c9f689c]::ops::function::FnOnce<(rustc_middle[3b5a5dff77aaafdc]::ty::context::TyCtxt, ())>>::call_once
  43:     0x7f7aa967bb7e - rustc_query_system[c2422e4c6a409a1d]::query::plumbing::try_execute_query::<rustc_query_impl[a315f5f311a78fca]::DynamicConfig<rustc_query_system[c2422e4c6a409a1d]::query::caches::SingleCache<rustc_middle[3b5a5dff77aaafdc]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[a315f5f311a78fca]::plumbing::QueryCtxt, false>
  44:     0x7f7aa967b8c9 - rustc_query_impl[a315f5f311a78fca]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  45:     0x7f7aa93f0468 - <rustc_codegen_llvm[f038090cdd5b2578]::LlvmCodegenBackend as rustc_codegen_ssa[536d019e2e3a26f2]::traits::backend::CodegenBackend>::codegen_crate
  46:     0x7f7aa8deca8d - rustc_interface[24b999bbebef6ad6]::passes::start_codegen
  47:     0x7f7aa94d332e - <rustc_interface[24b999bbebef6ad6]::queries::Queries>::ongoing_codegen
  48:     0x7f7aa94d1623 - std[a539495bdaa40edd]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[24b999bbebef6ad6]::util::run_in_thread_with_globals<rustc_interface[24b999bbebef6ad6]::interface::run_compiler<core[8479cc789c9f689c]::result::Result<(), rustc_span[123652b6fc40218d]::ErrorGuaranteed>, rustc_driver_impl[90397df04d7f8043]::run_compiler::{closure#1}>::{closure#0}, core[8479cc789c9f689c]::result::Result<(), rustc_span[123652b6fc40218d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8479cc789c9f689c]::result::Result<(), rustc_span[123652b6fc40218d]::ErrorGuaranteed>>
  49:     0x7f7aa94d08b3 - <<std[a539495bdaa40edd]::thread::Builder>::spawn_unchecked_<rustc_interface[24b999bbebef6ad6]::util::run_in_thread_with_globals<rustc_interface[24b999bbebef6ad6]::interface::run_compiler<core[8479cc789c9f689c]::result::Result<(), rustc_span[123652b6fc40218d]::ErrorGuaranteed>, rustc_driver_impl[90397df04d7f8043]::run_compiler::{closure#1}>::{closure#0}, core[8479cc789c9f689c]::result::Result<(), rustc_span[123652b6fc40218d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8479cc789c9f689c]::result::Result<(), rustc_span[123652b6fc40218d]::ErrorGuaranteed>>::{closure#1} as core[8479cc789c9f689c]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  50:     0x7f7aaa5ae2f5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::haf26a310f9d4b307
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/alloc/src/boxed.rs:2007:9
  51:     0x7f7aaa5ae2f5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h063cbb715347676f
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/alloc/src/boxed.rs:2007:9
  52:     0x7f7aaa5ae2f5 - std::sys::unix::thread::Thread::new::thread_start::h9a5c664a6fa7c19f
                               at /rustc/09df6108c84fdec400043d99d9ee232336fd5a9f/library/std/src/sys/unix/thread.rs:108:17
  53:     0x7f7aa4aa63ec - <unknown>
  54:     0x7f7aa4b26a4c - <unknown>
  55:                0x0 - <unknown>

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: please attach the file at `/home/$user/tmp/rustc-ice-2023-10-18T09:24:26.801860128Z-8552.txt` to your bug report

note: compiler flags: -Z mir-opt-level=0 -Z mir-enable-passes=+Inline

To summarize:

  • ✔️ rustc +nightly -Zmir-opt-level=0 -Zmir-enable-passes=+Inline demoo.rs
  • ✔️ rustc +nightly -Zmir-opt-level=0 -Zmir-enable-passes=+Inline demo.rs
  • 💀 rustc +nightly -Zmir-opt-level=0 -Zmir-enable-passes=+Inline main.rs

I'm really puzzled. Any idea? 🤔

EDIT: worth mentioning I'm on the latest nightly:

$ rustc --version
rustc 1.75.0-nightly (09df6108c 2023-10-17)

@tmiasko
Copy link
Contributor

tmiasko commented Oct 18, 2023

Setting the crate name would stabilize this for now, but any change to the hash algorithm could cause this test to suddenly pass, even if the compiler regresses in its behavior. Is there a way around that problem?

For testing purposes? Repeat the test a number of times to ensure that it is unlikely that it will pass by an accident.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 18, 2023
Only check in a single place if a pass is enabled.

Fixes rust-lang#116294
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 18, 2023
Only check in a single place if a pass is enabled.

Fixes rust-lang#116294
@saethlin
Copy link
Member Author

saethlin commented Oct 19, 2023

@apiraino Yes, that is the ICE in question, and your difficulty in reproducing this underscores the problem here. Any ICE that relies on MIR inlining within a crate (as opposed to inlining a function from another crate) may stop reproducing basically at any time. I believe this is the same reason that #113908 bisects to unrelated commits.

fmease added a commit to fmease/rust that referenced this issue Oct 19, 2023
Only check in a single place if a pass is enabled.

Fixes rust-lang#116294
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 19, 2023
Only check in a single place if a pass is enabled.

Fixes rust-lang#116294
@bors bors closed this as completed in 096943a Oct 19, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 19, 2023
Rollup merge of rust-lang#116896 - cjgillot:single-inline, r=oli-obk

Only check in a single place if a pass is enabled.

Fixes rust-lang#116294
RalfJung pushed a commit to RalfJung/miri that referenced this issue Oct 19, 2023
Only check in a single place if a pass is enabled.

Fixes rust-lang/rust#116294
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants