Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICE: evaluation of constant value failed | #[inline] causes compiler panic when bench-marking #83937

Closed
DzenanJupic opened this issue Apr 6, 2021 · 4 comments
Labels
C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@DzenanJupic
Copy link

DzenanJupic commented Apr 6, 2021

I'm currently experimenting with a bunch of sorting algorithms. To reduce boilerplate, I made use of the const_trait_impl feature.

It seems like this feature breaks when benchmarking an #[inline] function. It however works well when not using #[inline], or when not benchmarking.

Minimal example:

Code

Cargo.toml
[package]
name = "temp"
version = "0.1.0"
edition = "2018"

[[bench]]
name = "counting_sort"
// lib.rs
#![feature(const_trait_impl)]
#![allow(incomplete_features)]

pub trait UsizeConversions {
    fn into_usize(self) -> usize;
}

impl const UsizeConversions for u8 {
    fn into_usize(self) -> usize {
        self as usize
    }
}

pub trait CountingSort {
    fn sort(&mut self);
}

impl CountingSort for [u8] {
    #[inline] // when this is removed, everything works well
    fn sort(&mut self) {
        let _counts = [0usize; u8::MAX.into_usize() + 1];
    }
}

#[test]
fn works_in_tests() {
    let mut data = vec![5, 2, 8, 10];
    CountingSort::sort(&mut *data);
}
benches/counting_sort.rs
#![feature(test)]

extern crate test;

use temp::CountingSort;
use test::Bencher;

#[bench]
fn counting_sort(b: &mut Bencher) {
    b.iter(|| {
        let mut data = vec![5, 2, 8, 10]; 
        CountingSort::sort(&mut *data);
    });
}

Meta

rustc --version --verbose:

rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)
binary: rustc
commit-hash: 07e0e2ec268c140e607e1ac7f49f145612d0f597
commit-date: 2021-03-24
host: x86_64-pc-windows-msvc
release: 1.53.0-nightly
LLVM version: 12.0.0

Command

 cargo bench

Error output

error[E0080]: evaluation of constant value failed
  --> C:\Users\info\Code\Rust\temp\src\lib.rs:21:32
   |
21 |         let _counts = [0usize; u8::MAX.into_usize() + 1];
   |                                ^^^^^^^^^^^^^^^^^^^^ calling non-const function `<u8 as UsizeConversions>::into_usize`

error: internal compiler error: compiler\rustc_codegen_llvm\src\context.rs:793:17: failed to get layout for `[usize; _]`: the type `[usize; _]` has an unknown layout
Backtrace

thread 'rustc' panicked at 'Box<Any>', /rustc/07e0e2ec268c140e607e1ac7f49f145612d0f597\library\std\src\panic.rs:59:5
stack backtrace:
   0:     0x7ffa437d77ee - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h62553bb33d269d2e
   1:     0x7ffa438027cc - core::fmt::write::h187a24ff0c56237d
   2:     0x7ffa437cb208 - <std::io::IoSlice as core::fmt::Debug>::fmt::h7a51133a1941366b
   3:     0x7ffa437dba02 - std::panicking::take_hook::ha7af702674468c4c
   4:     0x7ffa437db4da - std::panicking::take_hook::ha7af702674468c4c
   5:     0x7ffa2f61b057 - rustc_driver::report_ice::h530a14ca8f015ece
   6:     0x7ffa437dc1e5 - std::panicking::rust_panic_with_hook::hffdabf0e02906eb3
   7:     0x7ffa33682720 - <rustc_errors::emitter::FileWithAnnotatedLines as core::fmt::Debug>::fmt::h138bd76935548e4c
   8:     0x7ffa33681df9 - <rustc_errors::emitter::FileWithAnnotatedLines as core::fmt::Debug>::fmt::h138bd76935548e4c
   9:     0x7ffa339008b1 - rustc_query_system::query::job::report_cycle::h78ff372fa02cb0ad
  10:     0x7ffa3364d5a0 - <rustc_feature::builtin_attrs::AttributeType as core::fmt::Debug>::fmt::h4d81e9bb6564882a
  11:     0x7ffa336541b5 - rustc_errors::HandlerInner::err_count::h836eba26b76f283e
  12:     0x7ffa33651d52 - rustc_errors::Handler::bug::h64fb55725578e531
  13:     0x7ffa335135f8 - <rustc_middle::ty::consts::int::ScalarInt as core::fmt::UpperHex>::fmt::he313431e7a456270
  14:     0x7ffa33513410 - <rustc_middle::ty::consts::int::ScalarInt as core::fmt::UpperHex>::fmt::he313431e7a456270
  15:     0x7ffa33513359 - <rustc_middle::ty::consts::int::ScalarInt as core::fmt::UpperHex>::fmt::he313431e7a456270
  16:     0x7ffa33513529 - <rustc_middle::ty::consts::int::ScalarInt as core::fmt::UpperHex>::fmt::he313431e7a456270
  17:     0x7ffa338f9a17 - rustc_middle::util::bug::bug_fmt::hf44d9914e298ce88
  18:     0x7ffa2f87067a - <rustc_codegen_llvm::context::CodegenCx as rustc_target::abi::LayoutOf>::spanned_layout_of::hbcc603c5009aff0b
  19:     0x7ffa2f87052c - <rustc_codegen_llvm::context::CodegenCx as rustc_target::abi::LayoutOf>::spanned_layout_of::hbcc603c5009aff0b
  20:     0x7ffa2f8fe279 - <rustc_codegen_llvm::back::archive::LlvmArchiveBuilder as rustc_codegen_ssa::back::archive::ArchiveBuilder>::build::ha5a74b8a3be08753
  21:     0x7ffa2f81591f - rustc_codegen_llvm::llvm_::diagnostic::Diagnostic::unpack::ha6027da799128368
  22:     0x7ffa2f8eede9 - <rustc_codegen_llvm::back::archive::LlvmArchiveBuilder as rustc_codegen_ssa::back::archive::ArchiveBuilder>::build::ha5a74b8a3be08753
  23:     0x7ffa2f8799f2 - <rustc_codegen_llvm::back::lto::ThinLTOKeysMap as core::fmt::Debug>::fmt::h267b43b08cde10bf
  24:     0x7ffa2f8a2a56 - <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::link::h1ca676796a6dc6ab
  25:     0x7ffa2f8cb91f - <rustc_codegen_llvm::base::ValueIter as core::iter::traits::iterator::Iterator>::next::h76a924d7c4d8667e
  26:     0x7ffa2f87bc3e - <rustc_codegen_llvm::back::lto::ThinLTOKeysMap as core::fmt::Debug>::fmt::h267b43b08cde10bf
  27:     0x7ffa2f8cb1c2 - <rustc_codegen_llvm::base::ValueIter as core::iter::traits::iterator::Iterator>::next::h76a924d7c4d8667e
  28:     0x7ffa2f88b3ec - <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate::h8a4e9bbd19201980
  29:     0x7ffa2f762dd8 - rustc_interface::passes::BoxedResolver::to_resolver_outputs::h26a866d2af02ca21
  30:     0x7ffa2f76f0b6 - rustc_interface::queries::Queries::ongoing_codegen::h23da4f767055d06d
  31:     0x7ffa2f632c53 - <rustc_middle::ty::SymbolName as core::fmt::Debug>::fmt::h898edfdbf8c7dbdf
  32:     0x7ffa2f6916bc - <rustc_driver::args::Error as core::fmt::Debug>::fmt::h54d0345b5b43328e
  33:     0x7ffa2f6351eb - <rustc_middle::ty::SymbolName as core::fmt::Debug>::fmt::h898edfdbf8c7dbdf
  34:     0x7ffa2f624034 - rustc_driver::pretty::print_after_hir_lowering::h04bf553be4aa75de
  35:     0x7ffa2f63745f - <rustc_middle::ty::SymbolName as core::fmt::Debug>::fmt::h898edfdbf8c7dbdf
  36:     0x7ffa2f65676d - <rustc_hir::intravisit::ErasedMap as rustc_hir::intravisit::Map>::foreign_item::he23d70bdee5d6fd3
  37:     0x7ffa437eb16a - std::sys::windows::thread::Thread::new::h485ded1a10d2ce17
  38:     0x7ffaaac07034 - BaseThreadInitThunk
  39:     0x7ffaac242651 - RtlUserThreadStart

Edit:

rustc note

note: compiler flags: -C opt-level=3 -C embed-bitcode=no
note: some of the compiler flags provided by cargo are hidden
@DzenanJupic DzenanJupic added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 6, 2021
@jonas-schievink jonas-schievink added the F-const_trait_impl `#![feature(const_trait_impl)]` label Apr 6, 2021
DzenanJupic added a commit to DzenanJupic/sort that referenced this issue Apr 6, 2021
We now have the ability to profile our sorting algorithms and compare them to the std sorting algorithms.

Unfortunately, there seems to be a bug in the compiler (rust-lang/rust#83937), that currently prevents using #[inline] on some sort functions when benchmarking (see issue for more information). Therefore we now have a `is_benchmark` feature, that turns these inlines off, when benchmarking.
fanninpm added a commit to fanninpm/glacier that referenced this issue Jun 18, 2021
JohnTitor pushed a commit to fanninpm/glacier that referenced this issue Jun 21, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jun 21, 2021
@JohnTitor
Copy link
Member

Triage: it now works with the latest nightly, I guess #86166 fixed it. Marking as E-needs-test, it'd be great if we could add a test somehow.

@JohnTitor JohnTitor added E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example labels Jun 22, 2021
@tmiasko
Copy link
Contributor

tmiasko commented Jun 22, 2021

A slight modification is required to reproduce the original issue:

--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,7 +19,8 @@ pub trait CountingSort {
 impl CountingSort for [u8] {
     #[inline] // when this is removed, everything works well
     fn sort(&mut self) {
-        let _counts = [0usize; u8::MAX.into_usize() + 1];
+        let _counts = [0usize; u8::MAX.into_usize() * 0];
+        &_counts;
     }
 }

@JohnTitor JohnTitor removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jun 22, 2021
@tmiasko
Copy link
Contributor

tmiasko commented Jun 23, 2021

Constness of associated functions is encoded & decoded without looking at constness of impl. Encoding a result of is_const_fn_raw query in encode_info_for_impl_item could be sufficient for now:

constness: sig.header.constness,

Duplicate of #84846.

@fee1-dead
Copy link
Member

Fixed by #86750.

@tmiasko tmiasko closed this as completed Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants