rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.52k stars 94 forks source link

ICE: `Offset in NominalSPOffset is greater than 2GB; should hit impl limit first: TryFromIntError(())` #1434

Open matthiaskrgr opened 7 months ago

matthiaskrgr commented 7 months ago

auto-reduced (treereduce-rust):

#[repr(align(536870912))]

enum Aligned {
    Zero = 0,
    One = 1,
}

fn main() {
    let aligned = Aligned::Zero;

    assert_eq!(tou8(Aligned::Zero), 0);
}

fn tou8(al: Aligned) -> u8 {
    al as u8
}

original:

// run-pass
// allows aligned custom discriminant enums to cast into other types
// See the issue #92464 for more info
#[repr(align(536870912))]
#[repr(align(8))]
enum Aligned {
    Zero = 0,
    One = 1,
}

fn main() {
    let aligned = Aligned::Zero;
    let fo = aligned as u8;
    println!("foo {}", fo);
    assert_eq!(fo, 0);
    println!("{}", tou8(Aligned::Zero));
    assert_eq!(tou8(Aligned::Zero), 0);
}

#[inline(never)]
fn tou8(al: Aligned) -> u8 {
    // Cast behind a function call so ConstProp does not see it
    // (so that we can test codegen).
    al as u8
}

Version information

rustc 1.76.0-nightly (d5fab3376 2023-12-02)
binary: rustc
commit-hash: d5fab33766917085588d9bb4fb9477776695d98b
commit-date: 2023-12-02
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5

Command: /home/matthias/.rustup/toolchains/master/bin/rustc -Zcodegen-backend=cranelift

Program output

``` warning: unused variable: `aligned` --> /tmp/icemaker_global_tempdir.IhQoDdu5PZ0P/rustc_testrunner_tmpdir_reporting.orosvbyC2kie/mvce.rs:9:9 | 9 | let aligned = Aligned::Zero; | ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_aligned` | = note: `#[warn(unused_variables)]` on by default warning: variant `One` is never constructed --> /tmp/icemaker_global_tempdir.IhQoDdu5PZ0P/rustc_testrunner_tmpdir_reporting.orosvbyC2kie/mvce.rs:5:5 | 3 | enum Aligned { | ------- variant in this enum 4 | Zero = 0, 5 | One = 1, | ^^^ | = note: `#[warn(dead_code)]` on by default thread '' panicked at /rust/deps/cranelift-codegen-0.102.0/src/isa/x64/abi.rs:957:46: Offset in NominalSPOffset is greater than 2GB; should hit impl limit first: TryFromIntError(()) stack backtrace: 0: 0x7f5f0f76150c - std::backtrace_rs::backtrace::libunwind::trace::h479849121ff49a5d at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5 1: 0x7f5f0f76150c - std::backtrace_rs::backtrace::trace_unsynchronized::hc9644f16de94df45 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f5f0f76150c - std::sys_common::backtrace::_print_fmt::h10290334532192e1 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f5f0f76150c - ::fmt::h1f3df53d81057b3e at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f5f0f7b4540 - core::fmt::rt::Argument::fmt::hb8e319fcddae9a31 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/core/src/fmt/rt.rs:142:9 5: 0x7f5f0f7b4540 - core::fmt::write::h80be9ea7045f640f at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/core/src/fmt/mod.rs:1120:17 6: 0x7f5f0f75536f - std::io::Write::write_fmt::h0522d22623a1843b at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/io/mod.rs:1810:15 7: 0x7f5f0f7612f4 - std::sys_common::backtrace::_print::h11954509d6d4a59e at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f5f0f7612f4 - std::sys_common::backtrace::print::h89028ceab4fa2c56 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f5f0f763f87 - std::panicking::default_hook::{{closure}}::ha828cf6bd2b63731 10: 0x7f5f0f763cef - std::panicking::default_hook::h85bef85bc0f3798a at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/panicking.rs:292:9 11: 0x7f5f0c4f9490 - std[be0bda08bb484463]::panicking::update_hook::>::{closure#0} 12: 0x7f5f0f7646c8 - as core::ops::function::Fn>::call::he0dee972ea8e9cc9 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/alloc/src/boxed.rs:2021:9 13: 0x7f5f0f7646c8 - std::panicking::rust_panic_with_hook::he9727088864fdef9 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/panicking.rs:783:13 14: 0x7f5f0f76441e - std::panicking::begin_panic_handler::{{closure}}::h80b7e677079c8e55 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/panicking.rs:657:13 15: 0x7f5f0f7619d6 - std::sys_common::backtrace::__rust_end_short_backtrace::hd9049e8056407812 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f5f0f764182 - rust_begin_unwind at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/panicking.rs:645:5 17: 0x7f5f0f7b0c15 - core::panicking::panic_fmt::h97f53bcafbf77918 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/core/src/panicking.rs:72:14 18: 0x7f5f0f7b1313 - core::result::unwrap_failed::hcb975e08d9c63ed9 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/core/src/result.rs:1649:5 19: 0x7f5f001f559f - ::gen_get_stack_addr 20: 0x7f5f00312548 - cranelift_codegen[d4c1749a8095538]::isa::x64::lower::isle::generated_code::constructor_stack_addr_impl::> 21: 0x7f5f003144f9 - cranelift_codegen[d4c1749a8095538]::isa::x64::lower::isle::generated_code::constructor_lower::> 22: 0x7f5f0015a807 - >::lower:: 23: 0x7f5f00195cad - cranelift_codegen[d4c1749a8095538]::machinst::compile::compile:: 24: 0x7f5f00249a3a - ::compile_function 25: 0x7f5f002d68cd - ::compile_stencil 26: 0x7f5f002d65b7 - ::compile_and_emit 27: 0x7f5f0006bf00 - ::define_function_with_control_plane 28: 0x7f5f00025ef2 - rustc_codegen_cranelift[b6b6b84571df3c3b]::driver::aot::module_codegen::{closure#1} 29: 0x7f5efffc2a16 - std[be0bda08bb484463]::sys_common::backtrace::__rust_begin_short_backtrace::> 30: 0x7f5efffd1616 - <::spawn_unchecked_>::{closure#1} as core[9199d425737c1d72]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 31: 0x7f5f0f76e555 - as core::ops::function::FnOnce>::call_once::h97e5c9d6b1622657 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/alloc/src/boxed.rs:2007:9 32: 0x7f5f0f76e555 - as core::ops::function::FnOnce>::call_once::hb5f6014336ba501b at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/alloc/src/boxed.rs:2007:9 33: 0x7f5f0f76e555 - std::sys::unix::thread::Thread::new::thread_start::h758e18463c2780d9 at /rustc/d5fab33766917085588d9bb4fb9477776695d98b/library/std/src/sys/unix/thread.rs:108:17 34: 0x7f5f096a59eb - 35: 0x7f5f097297cc - 36: 0x0 - 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.76.0-nightly (d5fab3376 2023-12-02) running on x86_64-unknown-linux-gnu note: compiler flags: -Z codegen-backend=cranelift -Z dump-mir-dir=dir query stack during panic: end of query stack warning: 2 warnings emitted ```

bjorn3 commented 7 months ago

This is a Cranelift bug. It should propagate the error out into cg_clif, which would then give a rustc diagnostic for this. Cranelift doesn't support stack frames larger than 2GB. (Neither does LLVM, but LLVM tends to silently miscompile instead)

bjorn3 commented 6 months ago

I can't reproduce this with the latest version in this repo.

matthiaskrgr commented 6 months ago

Seems to be fixed, indeed.

bjorn3 commented 6 months ago

Should be fixed on the next nightly then.

matthiaskrgr commented 4 months ago

@bjorn3 this is crashing again it seems.

matthiaskrgr commented 4 months ago

Regression in nightly-2024-02-11

ending github query because we found starting sha: d44e3b95cb9d410d89cb8ab3233906a33f43756a get_commits_between returning commits, len: 7 commit[0] 2024-02-09: Auto merge of #120852 - matthiaskrgr:rollup-01pr8gj, r=matthiaskrgr commit[1] 2024-02-10: Auto merge of #120862 - matthiaskrgr:rollup-jzfab58, r=matthiaskrgr commit[2] 2024-02-10: Auto merge of #120721 - onur-ozkan:incorrect-llvm-path-on-cross-target, r=albertlarsan68 commit[3] 2024-02-10: Auto merge of #120712 - compiler-errors:async-closures-harmonize, r=oli-obk commit[4] 2024-02-10: Auto merge of #120771 - oli-obk:useless_non_ensure_query_call, r=davidtwco commit[5] 2024-02-10: Auto merge of #120877 - matthiaskrgr:rollup-j1b8mv6, r=matthiaskrgr commit[6] 2024-02-10: Auto merge of #119614 - RalfJung:const-refs-to-static, r=oli-obk

bjorn3 commented 4 months ago

Is it panicking with the same message?

matthiaskrgr commented 4 months ago

Yeah, as if the original fix had been reverted or something :|

at 21:38:22 ❯ ~/.rustup/toolchains/master/bin/rustc cl.rs -Zcodegen-backend=cranelift
warning: unused variable: `aligned`
 --> cl.rs:9:9
  |
9 |     let aligned = Aligned::Zero;
  |         ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_aligned`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: variant `One` is never constructed
 --> cl.rs:5:5
  |
3 | enum Aligned {
  |      ------- variant in this enum
4 |     Zero = 0,
5 |     One = 1,
  |     ^^^
  |
  = note: `#[warn(dead_code)]` on by default

thread '<unnamed>' panicked at /rust/deps/cranelift-codegen-0.104.0/src/isa/x64/abi.rs:957:46:
Offset in NominalSPOffset is greater than 2GB; should hit impl limit first: TryFromIntError(())
stack backtrace:
   0:     0x7fe584768416 - std::backtrace_rs::backtrace::libunwind::trace::he5e0c8035bcf370a
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7fe584768416 - std::backtrace_rs::backtrace::trace_unsynchronized::hba9faf93820c49a0
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fe584768416 - std::sys_common::backtrace::_print_fmt::hd96a65dc4519c6a2
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7fe584768416 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1e43ea967dd08c60
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7fe5847baf90 - core::fmt::rt::Argument::fmt::h7d139ffbc6561f88
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/core/src/fmt/rt.rs:142:9
   5:     0x7fe5847baf90 - core::fmt::write::h1bb9a6b0afdc200a
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/core/src/fmt/mod.rs:1120:17
   6:     0x7fe58475bc9f - std::io::Write::write_fmt::hce12a9b2d6e999ce
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/io/mod.rs:1854:15
   7:     0x7fe5847681f4 - std::sys_common::backtrace::_print::h617917b22169e3a6
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7fe5847681f4 - std::sys_common::backtrace::print::h8e043c0f1534c736
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7fe58476afe7 - std::panicking::default_hook::{{closure}}::h7e3fb3ae3c6c108e
  10:     0x7fe58476ad49 - std::panicking::default_hook::h55b5e261b40d6db7
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/panicking.rs:292:9
  11:     0x7fe58154d15c - std[b3f5a7d6316481fd]::panicking::update_hook::<alloc[49d4571b2a6eb602]::boxed::Box<rustc_driver_impl[2740d49934284c30]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7fe58476b736 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h4a73ab022c7efc08
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/alloc/src/boxed.rs:2030:9
  13:     0x7fe58476b736 - std::panicking::rust_panic_with_hook::h625291f242c593a2
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/panicking.rs:785:13
  14:     0x7fe58476b482 - std::panicking::begin_panic_handler::{{closure}}::h2ad2d559889c7088
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/panicking.rs:659:13
  15:     0x7fe584768916 - std::sys_common::backtrace::__rust_end_short_backtrace::h97f79d64397675a6
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7fe58476b1d4 - rust_begin_unwind
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/panicking.rs:647:5
  17:     0x7fe5847b76e5 - core::panicking::panic_fmt::h8e3943f3224f5dc8
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/core/src/panicking.rs:72:14
  18:     0x7fe5847b7d93 - core::result::unwrap_failed::hf90562567199e58f
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/core/src/result.rs:1653:5
  19:     0x7fe574ff421f - <cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::abi::X64ABIMachineSpec as cranelift_codegen[2b9ae455c3dcf26c]::machinst::abi::ABIMachineSpec>::gen_get_stack_addr
  20:     0x7fe57510f458 - cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::lower::isle::generated_code::constructor_stack_addr_impl::<cranelift_codegen[2b9ae455c3dcf26c]::machinst::isle::IsleContext<cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::lower::isle::generated_code::MInst, cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::X64Backend>>
  21:     0x7fe575111224 - cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::lower::isle::generated_code::constructor_lower::<cranelift_codegen[2b9ae455c3dcf26c]::machinst::isle::IsleContext<cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::lower::isle::generated_code::MInst, cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::X64Backend>>
  22:     0x7fe574f5aaa8 - <cranelift_codegen[2b9ae455c3dcf26c]::machinst::lower::Lower<cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::lower::isle::generated_code::MInst>>::lower::<cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::X64Backend>
  23:     0x7fe574f959e6 - cranelift_codegen[2b9ae455c3dcf26c]::machinst::compile::compile::<cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::X64Backend>
  24:     0x7fe5750476cf - <cranelift_codegen[2b9ae455c3dcf26c]::isa::x64::X64Backend as cranelift_codegen[2b9ae455c3dcf26c]::isa::TargetIsa>::compile_function
  25:     0x7fe5750d478d - <cranelift_codegen[2b9ae455c3dcf26c]::context::Context>::compile_stencil
  26:     0x7fe5750d4487 - <cranelift_codegen[2b9ae455c3dcf26c]::context::Context>::compile_and_emit
  27:     0x7fe574e6eb67 - <cranelift_object[37b74b9f6be5a183]::backend::ObjectModule as cranelift_module[c340c0629507e123]::module::Module>::define_function_with_control_plane
  28:     0x7fe574e281ed - rustc_codegen_cranelift[30aa422767537c2c]::driver::aot::module_codegen::{closure#1}
  29:     0x7fe574dc6586 - std[b3f5a7d6316481fd]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_codegen_cranelift[30aa422767537c2c]::driver::aot::module_codegen::{closure#1}, core[6b66d21a0b634bc5]::result::Result<rustc_codegen_cranelift[30aa422767537c2c]::driver::aot::ModuleCodegenResult, alloc[49d4571b2a6eb602]::string::String>>
  30:     0x7fe574dd48e5 - <<std[b3f5a7d6316481fd]::thread::Builder>::spawn_unchecked_<rustc_codegen_cranelift[30aa422767537c2c]::driver::aot::module_codegen::{closure#1}, core[6b66d21a0b634bc5]::result::Result<rustc_codegen_cranelift[30aa422767537c2c]::driver::aot::ModuleCodegenResult, alloc[49d4571b2a6eb602]::string::String>>::{closure#1} as core[6b66d21a0b634bc5]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  31:     0x7fe584774925 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h42b8a5efac338fd6
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/alloc/src/boxed.rs:2016:9
  32:     0x7fe584774925 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hd13625d7c61bd598
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/alloc/src/boxed.rs:2016:9
  33:     0x7fe584774925 - std::sys::pal::unix::thread::Thread::new::thread_start::hbab9c5508e05c622
                               at /rustc/a166af7729d6f4e6570918701f79dfae750a00c4/library/std/src/sys/pal/unix/thread.rs:108:17
  34:     0x7fe57e66a9eb - <unknown>
  35:     0x7fe57e6ee7cc - <unknown>
  36:                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 `/tmp/rustc-ice-2024-02-11T20_38_29-2225644.txt` to your bug report

note: compiler flags: -Z codegen-backend=cranelift

query stack during panic:
end of query stack
warning: 2 warnings emitted

/tmp via 🦀 v1.78.0-nightly
at 21:39:32 ❯ ~/.rustup/toolchains/master/bin/rustc --version
rustc 1.78.0-nightly (a166af772 2024-02-11)
/tmp via 🦀 v1.78.0-nightly
bjorn3 commented 4 months ago

I can't reproduce this with rustc +nightly-2024-02-12 src/main.rs -Zcodegen-backend=cranelift where src/main.rs is:

#[repr(align(536870912))]

enum Aligned {
    Zero = 0,
    One = 1,
}

fn main() {
    let aligned = Aligned::Zero;

    assert_eq!(tou8(Aligned::Zero), 0);
}

fn tou8(al: Aligned) -> u8 {
    al as u8
}
$ rustc -vV -Zcodegen-backend=cranelift
rustc 1.78.0-nightly (1a648b397 2024-02-11)
binary: rustc
commit-hash: 1a648b397dedc98ada3dd3360f6d661ec2436c56
commit-date: 2024-02-11
host: aarch64-unknown-linux-gnu
release: 1.78.0-nightly
Cranelift version: 0.104.0
matthiaskrgr commented 4 months ago

weird. for me:

rustc 1.78.0-nightly (1a648b397 2024-02-11)
binary: rustc
commit-hash: 1a648b397dedc98ada3dd3360f6d661ec2436c56
commit-date: 2024-02-11
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
Cranelift version: 0.104.0

so the only diff I can spot right now is that I'm on x86 and you're on aarch64, could that make a difference?

Edit: the backtrace also goes through some x86 stuff /rust/deps/cranelift-codegen-0.104.0/src/isa/x64/abi.rs:957:46:

bjorn3 commented 4 months ago

Of course. Will test on x86 in a bit.

bjorn3 commented 4 months ago

On x86 cargo-bisect-rustc for me can't find any nightly for which it actually worked. Even manually checking a random nightly for which it should have worked (nightly-2023-12-31) it gives the same ICE. And with the latest version in this repo it fails too. Maybe I checked it on aarch64 back then too?