Repository: rust
Issue: panic: "failed to open bitcode file" when changing `-Ccodegen-units` in successive compiles.
Labels: I-ICE,T-compiler,A-incr-comp,C-bug,S-has-mcve

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

### Code

Starting from this project: https://github.com/ferrous-systems/armv7r-issues

Removing most of the code from `src/main.rs` still shows the issue.

Using:
`cargo rustc -- --emit asm -Copt-level=1`
followed by
`cargo rustc -- --emit asm -Copt-level=1 -Ccodegen-units=2`

```Rust
#![no_std]
#![no_main]

#[no_mangle]
fn main() {}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}
```


### Meta
This issue also reproduces on nightly.

`rustc +nightly --version --verbose`:
```
rustc 1.81.0-nightly (6292b2af6 2024-07-02)
binary: rustc
commit-hash: 6292b2af620dbd771ebb687c3a93c69ba8f97268
commit-date: 2024-07-02
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7
```

### Error output

```
   Compiling qemu-armv7r v0.1.0 (/Users/chrisnc/src/armv7r-issues)
thread 'rustc' panicked at /rustc/6292b2af620dbd771ebb687c3a93c69ba8f97268/compiler/rustc_codegen_ssa/src/back/write.rs:2141:29:
failed to open bitcode file `/Users/chrisnc/src/armv7r-issues/target/armv7r-none-eabihf/debug/incremental/qemu_armv7r-16rtq30pg0b9p/s-gxp28wkh98-0iff86z-working/8u05ong4ix7unkiauqfotgf5i.pre-lto.bc`: No such file or directory (os error 2)
stack backtrace:
   0:        0x105626e30 - <std::sys::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h9c69c577c7e61277
   1:        0x10566a124 - core::fmt::write::h0ff32c698967c260
   2:        0x10561ce64 - std::io::Write::write_fmt::h68081478e7fca433
   3:        0x105626c88 - std::sys::backtrace::print::h952c1605ace59248
   4:        0x105629298 - std::panicking::default_hook::{{closure}}::h1d94a07a51b5b4da
   5:        0x105628f64 - std::panicking::default_hook::h93d78b31552d8f13
   6:        0x10f4620e8 - <alloc[5f18fa49521524e4]::boxed::Box<rustc_driver_impl[44dc044c7477c0c7]::install_ice_hook::{closure#0}> as core[dd0dba577d9e0cd5]::ops::function::Fn<(&dyn for<'a, 'b> core[dd0dba577d9e0cd5]::ops::function::Fn<(&'a std[c84e633633b52a31]::panic::PanicHookInfo<'b>,), Output = ()> + core[dd0dba577d9e0cd5]::marker::Send + core[dd0dba577d9e0cd5]::marker::Sync, &std[c84e633633b52a31]::panic::PanicHookInfo)>>::call
   7:        0x105629e28 - std::panicking::rust_panic_with_hook::h7764565a30d42f96
   8:        0x105629738 - std::panicking::begin_panic_handler::{{closure}}::h84ae08041b2ea0e8
   9:        0x1056272bc - std::sys::backtrace::__rust_end_short_backtrace::h28b44aa47fc04c46
  10:        0x105629400 - _rust_begin_unwind
  11:        0x105684a6c - core::panicking::panic_fmt::h5ea902cc3a8a6b42
  12:        0x10f0e88b4 - rustc_codegen_ssa[8a5a420e0577f0f3]::back::write::submit_pre_lto_module_to_llvm::<rustc_codegen_llvm[3dfe5eb6e3f62aaa]::LlvmCodegenBackend>::{closure#0}
  13:        0x10f0e457c - rustc_codegen_ssa[8a5a420e0577f0f3]::back::write::submit_pre_lto_module_to_llvm::<rustc_codegen_llvm[3dfe5eb6e3f62aaa]::LlvmCodegenBackend>
  14:        0x10f18649c - rustc_codegen_ssa[8a5a420e0577f0f3]::base::codegen_crate::<rustc_codegen_llvm[3dfe5eb6e3f62aaa]::LlvmCodegenBackend>
  15:        0x10f1f2d48 - <rustc_codegen_llvm[3dfe5eb6e3f62aaa]::LlvmCodegenBackend as rustc_codegen_ssa[8a5a420e0577f0f3]::traits::backend::CodegenBackend>::codegen_crate
  16:        0x10fdb5574 - <rustc_session[23f273eca2a921aa]::session::Session>::time::<alloc[5f18fa49521524e4]::boxed::Box<dyn core[dd0dba577d9e0cd5]::any::Any>, rustc_interface[cf687af9d086092b]::passes::start_codegen::{closure#0}>
  17:        0x10fd8e638 - rustc_interface[cf687af9d086092b]::passes::start_codegen
  18:        0x10fdab0e4 - <rustc_middle[b703e35f0ce38cab]::ty::context::GlobalCtxt>::enter::<<rustc_interface[cf687af9d086092b]::queries::Queries>::codegen_and_build_linker::{closure#0}, core[dd0dba577d9e0cd5]::result::Result<rustc_interface[cf687af9d086092b]::queries::Linker, rustc_span[b639692432321f11]::ErrorGuaranteed>>
  19:        0x10fdd9090 - <rustc_interface[cf687af9d086092b]::queries::Queries>::codegen_and_build_linker
  20:        0x10f47aacc - <rustc_interface[cf687af9d086092b]::interface::Compiler>::enter::<rustc_driver_impl[44dc044c7477c0c7]::run_compiler::{closure#0}::{closure#1}, core[dd0dba577d9e0cd5]::result::Result<core[dd0dba577d9e0cd5]::option::Option<rustc_interface[cf687af9d086092b]::queries::Linker>, rustc_span[b639692432321f11]::ErrorGuaranteed>>
  21:        0x10f46d714 - rustc_span[b639692432321f11]::create_session_globals_then::<core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>, rustc_interface[cf687af9d086092b]::util::run_in_thread_with_globals<rustc_interface[cf687af9d086092b]::util::run_in_thread_pool_with_globals<rustc_interface[cf687af9d086092b]::interface::run_compiler<core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>, rustc_driver_impl[44dc044c7477c0c7]::run_compiler::{closure#0}>::{closure#1}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>::{closure#0}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}>
  22:        0x10f4656e4 - std[c84e633633b52a31]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[cf687af9d086092b]::util::run_in_thread_with_globals<rustc_interface[cf687af9d086092b]::util::run_in_thread_pool_with_globals<rustc_interface[cf687af9d086092b]::interface::run_compiler<core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>, rustc_driver_impl[44dc044c7477c0c7]::run_compiler::{closure#0}>::{closure#1}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>::{closure#0}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>
  23:        0x10f454454 - <<std[c84e633633b52a31]::thread::Builder>::spawn_unchecked_<rustc_interface[cf687af9d086092b]::util::run_in_thread_with_globals<rustc_interface[cf687af9d086092b]::util::run_in_thread_pool_with_globals<rustc_interface[cf687af9d086092b]::interface::run_compiler<core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>, rustc_driver_impl[44dc044c7477c0c7]::run_compiler::{closure#0}>::{closure#1}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>::{closure#0}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[dd0dba577d9e0cd5]::result::Result<(), rustc_span[b639692432321f11]::ErrorGuaranteed>>::{closure#2} as core[dd0dba577d9e0cd5]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  24:        0x105632374 - std::sys::pal::unix::thread::Thread::new::thread_start::h6557bb11ff30771a
  25:        0x18de02f94 - __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: please make sure that you have updated to the latest nightly

note: please attach the file at `/Users/chrisnc/src/armv7r-issues/rustc-ice-2024-07-03T15_18_49-59251.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C opt-level=1 -C codegen-units=2 -C incremental=[REDACTED] -C link-arg=-Tlinker.ld

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `qemu-armv7r` (bin "qemu-armv7r")
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
   Compiling qemu-armv7r v0.1.0 (/Users/chrisnc/src/armv7r-issues)
thread 'rustc' panicked at /rustc/6292b2af620dbd771ebb687c3a93c69ba8f97268/compiler/rustc_codegen_ssa/src/back/write.rs:2141:29:
failed to open bitcode file `/Users/chrisnc/src/armv7r-issues/target/armv7r-none-eabihf/debug/incremental/qemu_armv7r-16rtq30pg0b9p/s-gxp2apa2nb-1cj3jez-working/8u05ong4ix7unkiauqfotgf5i.pre-lto.bc`: No such file or directory (os error 2)
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: rustc_codegen_ssa::back::write::submit_pre_lto_module_to_llvm::<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}
   3: rustc_codegen_ssa::back::write::submit_pre_lto_module_to_llvm::<rustc_codegen_llvm::LlvmCodegenBackend>
   4: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
   5: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
   6: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
   7: rustc_interface::passes::start_codegen
   8: <rustc_middle::ty::context::GlobalCtxt>::enter::<<rustc_interface::queries::Queries>::codegen_and_build_linker::{closure#0}, core::result::Result<rustc_interface::queries::Linker, rustc_span::ErrorGuaranteed>>
   9: <rustc_interface::queries::Queries>::codegen_and_build_linker
  10: <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#0}::{closure#1}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>>
  11: rustc_span::create_session_globals_then::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.
```

</p>
</details>