-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
rustc hangs when generating very large arrays #81188
Comments
You are creating an array containing 4 billion elements, the compiler isn't too happy about that |
Makes sense - still, |
I think this is a duplicate of #81124. |
A similar program causes rustc to abort:
|
Relevant code:
|
Not sure who to ping, maybe @oli-obk - would it be possible to only allocate places if they're something other than a constant value? Something like this: enum MPlaceTy {
Allocated(MemPlace, TyAndLayout),
Constant(Constant, TyAndLayout),
} |
cc @rust-lang/wg-const-eval we could delay the allocation of local const fn foo() -> [u8; u32::max_value() as usize] { panic!("hello") } to actually show the panic and not run out of memory or hang when called |
First of all, even if we end up supporting the case where the function panics, the OOM will still occur once you actually implement the function. So I'd say the benefit of supporting this is rather marginal. On the cost side, the interpreter is already complicated enough as things are; every extra bit of complexity will increase the risk of bugs. If we do anything here, I think the right layer to do that is
This program asks the compiler to allocate |
Specifically this example allocates an all-zero static allocation, which is guaranteed immutable. This kind of static allocation has custom support in LLVM and won't take up any memory until runtime. So if we can track this kind of information through the compiler, we can support that, too. But all of this is a big rabbit hole. Related issues:
A previous attempt for undef bytes: #62655 This issue is still actionable by itself, as just the action of allocating an all-undef array should not take very long. It should either OOM quickly or manage to allocate that array. It should only be marginally slower than @cdmistman can you run a rustc in |
This is not an all-undef array though, it is an all-zero array. A lot more extra hackery is needed to add "lazy zeros" everywhere throughout CTFE. I assume writing all these zeros is what takes so much time. |
The original issue const VALUES: [usize; std::u32::MAX as usize] = { todo!() }; is an all undef array, because we create the array before running the initializer body. Otherwise I would agree with the assessment that interpreting 2^32 index + assignment ops (even if not in actual MIR ops, but just a loop in the interpreter) would be the issue here. |
Consider this Rust program (file named pub const VALUES: [usize; u32::MAX as usize] = [0; u32::MAX as usize];
fn main() {} Run Backtrace(gdb) bt
#0 0x00007fef25ec4772 in rustc_middle::mir::interpret::allocation::Allocation<Tag,Extra>::compress_uninit_range () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#1 0x00007fef25f59b85 in rustc_mir::interpret::memory::Memory<M>::copy_repeatedly () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#2 0x00007fef25f713f6 in rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::copy_op_no_validate () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#3 0x00007fef25f71970 in rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::copy_op_transmute () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#4 0x00007fef25f555f8 in rustc_mir::interpret::eval_context::InterpCx<M>::pop_stack_frame () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#5 0x00007fef25f73e46 in rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::run () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#6 0x00007fef25f33241 in rustc_mir::const_eval::eval_queries::eval_to_allocation_raw_provider () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#7 0x00007fef25f25f20 in rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::eval_to_allocation_raw>::compute () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#8 0x00007fef25ed0c52 in rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#9 0x00007fef25f26e82 in rustc_data_structures::stack::ensure_sufficient_stack () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#10 0x00007fef25fce12a in rustc_query_system::query::plumbing::force_query_with_job () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#11 0x00007fef25fb6479 in rustc_query_system::query::plumbing::get_query_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#12 0x00007fef25f32604 in rustc_mir::const_eval::eval_queries::eval_to_const_value_raw_provider () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#13 0x00007fef26aaeb80 in rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::eval_to_const_value_raw>::compute () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#14 0x00007fef26a713e6 in rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#15 0x00007fef26ab1ca2 in rustc_data_structures::stack::ensure_sufficient_stack () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#16 0x00007fef26b05c18 in rustc_query_system::query::plumbing::force_query_with_job () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#17 0x00007fef26afee89 in rustc_query_system::query::plumbing::get_query_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#18 0x00007fef25f3287a in rustc_mir::const_eval::eval_queries::eval_to_const_value_raw_provider () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#19 0x00007fef270214e7 in rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::eval_to_const_value_raw>::compute () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#20 0x00007fef2703c926 in rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#21 0x00007fef26ee5106 in rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}::{{closure}}::{{closure}} () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#22 0x00007fef26f979ce in rustc_query_system::query::plumbing::force_query_with_job () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#23 0x00007fef263ad230 in rustc_query_system::query::plumbing::get_query_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#24 0x00007fef26407e20 in rustc_middle::mir::interpret::queries::<impl rustc_middle::ty::context::TyCtxt>::const_eval_global_id () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#25 0x00007fef26407c57 in rustc_middle::mir::interpret::queries::<impl rustc_middle::ty::context::TyCtxt>::const_eval_poly () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#26 0x00007fef261db740 in <rustc_lint::BuiltinCombinedLateLintPass as rustc_lint::passes::LateLintPass>::check_item () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#27 0x00007fef25a62b9a in rustc_hir::intravisit::Visitor::visit_nested_item () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#28 0x00007fef264d97d7 in rustc_hir::intravisit::walk_crate () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#29 0x00007fef264e6f3a in rustc_session::utils::<impl rustc_session::session::Session>::time () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#30 0x00007fef26504430 in <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#31 0x00007fef264e9134 in rustc_session::utils::<impl rustc_session::session::Session>::time () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#32 0x00007fef264ff156 in rustc_interface::passes::analysis () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#33 0x00007fef264a70c9 in rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#34 0x00007fef2649ad9e in rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#35 0x00007fef2649d687 in rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}::{{closure}}::{{closure}} () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#36 0x00007fef264816d9 in rustc_query_system::query::plumbing::force_query_with_job () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#37 0x00007fef26480ab2 in rustc_query_system::query::plumbing::get_query_impl () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#38 0x00007fef264a74d1 in rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#39 0x00007fef2649cea4 in rustc_span::with_source_map () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#40 0x00007fef264a876a in rustc_interface::interface::create_compiler_and_run () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#41 0x00007fef2649d52e in rustc_span::with_session_globals () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#42 0x00007fef264a3473 in std::sys_common::backtrace::__rust_begin_short_backtrace () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#43 0x00007fef2648224a in core::ops::function::FnOnce::call_once{{vtable-shim}} () from /home/lzutao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-844665e24828f0f2.so
#44 0x00007fef238ab68a in <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once () at /rustc/4253153db205251f72ea4493687a31e04a2a8ca0/library/alloc/src/boxed.rs:1484
#45 <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once () at /rustc/4253153db205251f72ea4493687a31e04a2a8ca0/library/alloc/src/boxed.rs:1484
#46 std::sys::unix::thread::Thread::new::thread_start () at /rustc/4253153db205251f72ea4493687a31e04a2a8ca0//library/std/src/sys/unix/thread.rs:71
#47 0x00007fef235e14a4 in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0
#48 0x00007fef22f17d0f in clone () from /lib/x86_64-linux-gnu/libc.so.6 |
Thanks, but that's the program that will actually initialize the bits with all zeros, if you replace the initializer body |
Rustc doesn't hang in that case. I just have this error from rustc: error: any use of this value will cause an error
--> hang.rs:2:55
|
2 | pub const VALUES: [usize; std::u32::MAX as usize] = { todo!() };
| ------------------------------------------------------^^^^^^^---
| |
| the evaluated program panicked at 'not yet implemented', hang.rs:2:55
|
= note: `#[deny(const_err)]` on by default
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info Also run through
|
Oh, then I don't know what happened to the original example of @cdmistman Maybe your OS started using swap space and that slowed everything down? I'm going to close this issue as not actionable then. Thanks @lzutao for investigating |
The original issue was
So, it's about RAM usage, not hanging. |
Oh... I read this as "start using.." -> "never stops"... But yea, that seems totally fine to me, as in the happy path you'd also use that much memory. I don't think we need to do anything here except for the discussed things in the actually successfully compiling path for which we already have issues. |
I tried this code:
It seems that generating large arrays causes
rustc
to start using up a LOT of memory - eats up over 32gb for this.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: