Repository: rust
Issue: Large number of variables causes stack overflow (SIGSEGV)
Labels: I-crash,A-LLVM,A-debuginfo,T-compiler,C-bug,S-has-mcve

While building the [p384](https://crates.io/crates/p384) crate on an experimental platform I encountered a consistent ICE. Initially, the problem seemed to be due to the experimental platform. However, identifying the minimal reproduction revealed that all rustc are likely impacted. Note that the top of the stack is inside LLVM. I wasn't sure whether to report here or in LLVM, but it's clear that the structure that's being passed into LLVM is causing it to recurse too deeply.

This issue is a follow-up to https://github.com/esp-rs/rust/issues/214.

### Minimal Reproduction
#### Directory Structure
```
.
├── Cargo.toml
└── src
    └── lib.rs

1 directory, 2 files
```

#### Contents of: Cargo.toml
```
[package]
name = "p384"
version = "0.14.0-pre"
edition = "2021"
```

#### Contents of: src/lib.rs
- This file has been truncated for readability
- The full file is at: https://pastebin.com/s72e8Akk

```
pub const fn break_me() {
    let _x0 = 0;
    let _x1 = 0;
    let _x2 = 0;
    let _x3 = 0;
    let _x4 = 0;
    let _x5 = 0;
    let _x6 = 0;
    let _x7 = 0;
    let _x8 = 0;
    let _x9 = 0;
.
.
.
    let _x9990 = 0;
    let _x9991 = 0;
    let _x9992 = 0;
    let _x9993 = 0;
    let _x9994 = 0;
    let _x9995 = 0;
    let _x9996 = 0;
    let _x9997 = 0;
    let _x9998 = 0;
    let _x9999 = 0;
}

pub const BREAK_ME: () = break_me();
```

### Meta
`rustc --version --verbose`:
```
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

```

### Error output

```
   Compiling p384 v0.14.0-pre (/home/coder0xff/Dropbox/Documents/Projects/Community/src/community/elliptic-curves/p384)
error: rustc interrupted by SIGSEGV, printing backtrace

/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-ef0b2e016afc8182.so(+0x2c31aa6)[0x7eba1ba31aa6]
/lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7eba18a42520]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(+0x5e66695)[0x7eba16e66695]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit24constructVariableDIEImplERKNS_11DbgVariableEb+0x3eb)[0x7eba171f60ed]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0xca5)[0x7eba17163b25]

### cycle encountered after 5 frames with period 6
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
### recursed 41 times

/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]
/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.76.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE+0x88d)[0x7eba1716370d]

note: rustc unexpectedly overflowed its stack! this is a bug
note: maximum backtrace depth reached, frames may have been lost
note: we would appreciate a report at https://github.com/rust-lang/rust
note: backtrace dumped due to SIGSEGV! resuming signal
error: could not compile `p384` (lib)

Caused by:
  process didn't exit successfully: `/home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name p384 --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=235 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=0d600d9ffd0ec8ef -C extra-filename=-0d600d9ffd0ec8ef --out-dir /home/coder0xff/Dropbox/Documents/Projects/Community/src/community/elliptic-curves/p384/target/debug/deps -C incremental=/home/coder0xff/Dropbox/Documents/Projects/Community/src/community/elliptic-curves/p384/target/debug/incremental -L dependency=/home/coder0xff/Dropbox/Documents/Projects/Community/src/community/elliptic-curves/p384/target/debug/deps` (signal: 11, SIGSEGV: invalid memory reference)
```

<details><summary><strong>Backtrace</strong></summary>
<p>

```
#0  0x00007ffff0466695 in llvm::X86FrameLowering::getFrameIndexReference(llvm::MachineFunction const&, int, llvm::Register&) const () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#1  0x00007ffff07f60ed in llvm::DwarfCompileUnit::constructVariableDIEImpl(llvm::DbgVariable const&, bool) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#2  0x00007ffff0763b25 in llvm::DwarfCompileUnit::createAndAddScopeChildren(llvm::LexicalScope*, llvm::DIE&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3  0x00007ffff076370d in llvm::DwarfCompileUnit::createAndAddScopeChildren(llvm::LexicalScope*, llvm::DIE&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#4  0x00007ffff076370d in llvm::DwarfCompileUnit::createAndAddScopeChildren(llvm::LexicalScope*, llvm::DIE&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
[repeated many times]
#3018 0x00007ffff076370d in llvm::DwarfCompileUnit::createAndAddScopeChildren(llvm::LexicalScope*, llvm::DIE&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3019 0x00007ffff076370d in llvm::DwarfCompileUnit::createAndAddScopeChildren(llvm::LexicalScope*, llvm::DIE&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3020 0x00007ffff076370d in llvm::DwarfCompileUnit::createAndAddScopeChildren(llvm::LexicalScope*, llvm::DIE&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3021 0x00007ffff0762521 in llvm::DwarfCompileUnit::constructSubprogramScopeDIE(llvm::DISubprogram const*, llvm::LexicalScope*) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3022 0x00007ffff07610e8 in llvm::DwarfDebug::endFunctionImpl(llvm::MachineFunction const*) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3023 0x00007ffff07d313d in llvm::DebugHandlerBase::endFunction(llvm::MachineFunction const*) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3024 0x00007ffff076b5cc in llvm::AsmPrinter::emitFunctionBody() () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3025 0x00007ffff0769046 in llvm::X86AsmPrinter::runOnMachineFunction(llvm::MachineFunction&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3026 0x00007ffff05193ba in llvm::FPPassManager::runOnFunction(llvm::Function&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3027 0x00007ffff051866f in llvm::FPPassManager::runOnModule(llvm::Module&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3028 0x00007ffff0720bba in llvm::legacy::PassManagerImpl::run(llvm::Module&) () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-17-rust-1.76.0-stable.so
#3029 0x00007ffff6e16358 in LLVMRustWriteOutputFile () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-ef0b2e016afc8182.so
#3030 0x00007ffff6e15f95 in rustc_codegen_llvm::back::write::write_output_file () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-ef0b2e016afc8182.so
#3031 0x00007ffff6e13713 in rustc_codegen_llvm::back::write::codegen () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-ef0b2e016afc8182.so
#3032 0x00007ffff6e133c2 in rustc_codegen_ssa::back::write::finish_intra_module_work::<rustc_codegen_llvm::LlvmCodegenBackend> () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-ef0b2e016afc8182.so
#3033 0x00007ffff6eabbf1 in std::sys_common::backtrace::__rust_begin_short_backtrace::<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()> () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-ef0b2e016afc8182.so
#3034 0x00007ffff6eab6c2 in <<std::thread::Builder>::spawn_unchecked_<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/coder0xff/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-ef0b2e016afc8182.so
#3035 0x00007ffff23988e5 in alloc::boxed::{impl#47}::call_once<(), dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2015
#3036 alloc::boxed::{impl#47}::call_once<(), alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2015
#3037 std::sys::unix::thread::{impl#2}::new::thread_start () at library/std/src/sys/unix/thread.rs:108
#3038 0x00007ffff2094ac3 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
#3039 0x00007ffff2126850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

```

</p>
</details>