rust-lang / rustc_codegen_cranelift

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

"int binop requires lhs and rhs of same type" assertion should not be triggered for wrapping/unchecked shift #1475

Closed taiki-e closed 2 months ago

taiki-e commented 2 months ago

Since nightly-2024-04-03, build with -Z codegen-backend=cranelift panics with the following message.

  thread 'rustc' panicked at src/num.rs:134:9:
  assertion `left == right` failed: int binop requires lhs and rhs of same type
    left: u128
   right: u32
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/88c2f4f5f50ace5ddc7655ea311435104d3659bd/library/std/src/panicking.rs:645:5
     1: core::panicking::panic_fmt
               at /rustc/88c2f4f5f50ace5ddc7655ea311435104d3659bd/library/core/src/panicking.rs:72:14
     2: core::panicking::assert_failed_inner
               at /rustc/88c2f4f5f50ace5ddc7655ea311435104d3659bd/library/core/src/panicking.rs:397:23
     3: core::panicking::assert_failed::<rustc_middle::ty::Ty, rustc_middle::ty::Ty>
     4: rustc_codegen_cranelift::num::codegen_int_binop
     5: rustc_codegen_cranelift::base::codegen_fn_body
     6: rustc_codegen_cranelift::driver::aot::module_codegen::{closure#0}
     7: rustc_codegen_cranelift::driver::aot::module_codegen
     8: rustc_codegen_cranelift::driver::aot::run_aot
     9: <rustc_codegen_cranelift::CraneliftCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
    10: rustc_interface::passes::start_codegen
    11: <rustc_interface::queries::Queries>::codegen_and_build_linker
    12: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{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.

  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/runner/work/portable-atomic/portable-atomic/rustc-ice-2024-04-03T02_58_19-3249.txt` to your bug report

  note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -Z codegen-backend=cranelift -C panic=abort -Z panic_abort_tests

  note: some of the compiler flags provided by cargo are hidden

  query stack during panic:
  end of query stack
  stmt _0 = ShlUnchecked(_1, _3)
  fn core::num::<impl u128>::wrapping_shl(_1: u128, _2: u32) -> u128 {
      debug self => _1;
      debug rhs => _2;
      let mut _0: u128;
      let mut _3: u32;
      scope 1 {
          scope 2 (inlined core::num::<impl u128>::unchecked_shl) {
              debug self => _1;
              debug rhs => _3;
              scope 3 {
              }
          }
      }

      bb0: {
          StorageLive(_3);
          _3 = BitAnd(_2, const 127_u32);
          _0 = ShlUnchecked(_1, _3);
          StorageDead(_3);
          return;
      }
  }

full log: https://github.com/taiki-e/portable-atomic/actions/runs/8532189503/job/23373139667#step:15:386

Triggered assertion is:

https://github.com/rust-lang/rustc_codegen_cranelift/blob/d9f29fa018b7534e14f7452f41667dec3d67e1b7/src/num.rs#L113-L119

It appears that BinOp::*Unchecked variants, which appears to have been added in https://github.com/rust-lang/rustc_codegen_cranelift/commit/9efe5e746a8897b8dc59b3a94ce03f055869e5f6, also needs to be checked.