Repository: rust
Issue: rustc segfaults when targeting AVR and compiling core in debug mode
Labels: I-crash,A-LLVM,T-compiler,C-bug,O-AVR

code to reproduce at https://github.com/faithanalog/rustc-avr-bug-repro

searched nightlies: from nightly-2022-06-05 to nightly-2022-09-15
regressed nightly: nightly-2022-06-27
searched commit range: https://github.com/rust-lang/rust/compare/20a6f3a8a8ce5ae18d06b12cd7904bc5294ca753...c80c4b8fdcf3da69cd483e2fec172c9b1f95842c
regressed commit: https://github.com/rust-lang/rust/commit/788ddedb0d88e40db9cd62b6163d5a471813044b

<details>
<summary>bisected with <a href='https://github.com/rust-lang/cargo-bisect-rustc'>cargo-bisect-rustc</a> v0.6.4</summary>


Host triple: x86_64-unknown-linux-gnu
Reproduce with:
```bash
cargo-bisect-rustc --with-src --start 2022-06-05 --end 2022-09-15 --script ./build.sh 
```
</details>

In versions prior to 2022-06-27, compiling `core` for AVR in debug mode generates the following error:

```
error: ran out of registers during register allocation
```

This error isn't ideal, but it's not the subject of this bug report. In versions starting with 2022-06-27, rustc instead segfaults, with an error like this:

```
vi@localhost ~/p/rust-avr-bug-repro (main)> cargo build
   Compiling core v0.0.0 (/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
   Compiling rustc-std-workspace-core v1.99.0 (/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
   Compiling compiler_builtins v0.1.79
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x2811d13)[0x7fd7e2af5d13]
/lib64/libc.so.6(+0x3daa0)[0x7fd7dff76aa0]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.65.0-nightly.so(_ZN4llvm12MachineInstr10addOperandERNS_15MachineFunctionERKNS_14MachineOperandE+0x1f5)[0x7fd7dd080785]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.65.0-nightly.so(+0x4c6c5cc)[0x7fd7df8b75cc]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.65.0-nightly.so(+0x4c6c31f)[0x7fd7df8b731f]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.65.0-nightly.so(_ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE+0x856)[0x7fd7dde70b86]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.65.0-nightly.so(_ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE+0x2f)[0x7fd7dde7019f]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.65.0-nightly.so(_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE+0x213)[0x7fd7dde6fac3]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x232d8d2)[0x7fd7e26118d2]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x232d229)[0x7fd7e2611229]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x232b15a)[0x7fd7e260f15a]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x2327365)[0x7fd7e260b365]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x232650b)[0x7fd7e260a50b]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x2325018)[0x7fd7e2609018]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/librustc_driver-e661066f9d8eca3e.so(+0x2284358)[0x7fd7e2568358]
/home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/libstd-4393e7d07259b8a4.so(rust_metadata_std_81ab5d9a96a0142e+0xfdee3)[0x7fd7e025eee3]
/lib64/libc.so.6(+0x8784a)[0x7fd7dffc084a]
/lib64/libc.so.6(+0x10acec)[0x7fd7e0043cec]
error: could not compile `core`

Caused by:
  process didn't exit successfully: `rustc --crate-name core --edition=2021 /home/vi/.rustup/toolchains/nightly-2022-09-15-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=12ddbf17236d5c10 -C extra-filename=-12ddbf17236d5c10 --out-dir /home/vi/p/rust-avr-bug-repro/target/avr-atmega328p/debug/deps --target /home/vi/p/rust-avr-bug-repro/avr-atmega328p.json -Z force-unstable-if-unmarked -L dependency=/home/vi/p/rust-avr-bug-repro/target/avr-atmega328p/debug/deps -L dependency=/home/vi/p/rust-avr-bug-repro/target/debug/deps --cap-lints allow` (signal: 11, SIGSEGV: invalid memory reference)
```

This doesn't necessarily happen all the time though. sometimes it errors with `error: ran out of registers during register allocation` like it should be doing, so it seems to be non-deterministic on some versions of rustc. This is why my bisection build script re-runs it 10 times to be sure it segfaults at least once. However, the commit directly before the failing commit returned `error: ran out of registers during register allocation` 10 times, and the failing commit segfaulted all 10 times, so maybe it's deterministic for the failing commit.

`cargo build --release` works just fine and does not generate either error.