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

Add intrinsics::const_deallocate #92274

Merged
merged 4 commits into from
Jan 29, 2022
Merged

Add intrinsics::const_deallocate #92274

merged 4 commits into from
Jan 29, 2022

Conversation

lilasta
Copy link
Contributor

@lilasta lilasta commented Dec 25, 2021

Tracking issue: #79597
Related: #91884

This allows deallocation of a memory allocated by intrinsics::const_allocate. At the moment, this can be only used to reduce memory usage, but in the future this may be useful to detect memory leaks (If an allocated memory remains after evaluation, raise an error...?).

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 25, 2021
@rust-highfive
Copy link
Collaborator

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 25, 2021
@lilasta
Copy link
Contributor Author

lilasta commented Dec 25, 2021

r? @RalfJung

@lilasta lilasta marked this pull request as ready for review December 25, 2021 12:52
@RalfJung
Copy link
Member

Cc @rust-lang/wg-const-eval

@@ -1918,6 +1918,12 @@ extern "rust-intrinsic" {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub fn const_allocate(size: usize, align: usize) -> *mut u8;

/// Deallocate a memory which allocated by `intrinsics::const_allocate` at compile time.
/// Should not be called at runtime.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it would make more sense to just make this a NOP at runtime (since it looks like it will also be a NOP for some compile-time cases, so this is more consistent).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have const_eval_select for that. The intrinsic should imo not be implemented in backends

Copy link
Contributor Author

@lilasta lilasta Jan 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have const_eval_select for that.

I had thought wrapping const_(de)allocate with const_eval_select works well, but even just defining an function which calls the intrinsic causes a codegen error. So we may need to add nop to codegens anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems strange, the const function shouldn't even be codegen'd... right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the generic args of the const_eval_select call

Copy link
Contributor Author

@lilasta lilasta Jan 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7c6e75dd1cc1d2f14878ee24a3fe43cb
It occurs when compiling as a library crate.
EDIT: It seems that we can avoid a codegen error by using #[inline(always)] but I'm worried about whether it's okay to do or not.

yvt added a commit to r3-os/r3 that referenced this pull request Jan 20, 2022
We need a suballocator for a compile-time heap because the
`const_deallocate` intrinsic isn't implemented yet (there's an open PR
[1]), but we want to reuse the deallocated memory regions.

We are not adding the vendored `rlsf` as a separate crate because, as
explained earlier, this is a temporary measure, and we don't want to
pollute crates.io's namespace.

We can't make the necessary changes to the upstream `rlsf` because
maintaining the compatibility with a stable compiler is very likely to
pose a considerable challenge and unacceptable maintenance burdens.

[1]: rust-lang/rust#92274
yvt added a commit to r3-os/r3 that referenced this pull request Jan 20, 2022
We need a suballocator for a compile-time heap because the
`const_deallocate` intrinsic isn't implemented yet (there's an open PR
[1]), but we want to reuse deallocated memory regions.

We are not adding the vendored `rlsf` as a separate crate because, as
explained earlier, this is a temporary measure, and we don't want to
pollute crates.io's namespace.

We can't make the necessary changes to the upstream `rlsf` because
maintaining the compatibility with a stable compiler is very likely to
pose a considerable challenge and unacceptable maintenance burdens.

[1]: rust-lang/rust#92274
yvt added a commit to r3-os/r3 that referenced this pull request Jan 20, 2022
We need a suballocator for a compile-time heap because the
`const_deallocate` intrinsic isn't implemented yet (there's an open PR
[1]), but we don't want deallocated memory regions to go to waste.

We are not adding the vendored `rlsf` as a separate crate because, as
explained earlier, this is a temporary measure, and we don't want to
pollute crates.io's namespace.

We can't make the necessary changes to the upstream `rlsf` because
maintaining the compatibility with a stable compiler is very likely to
pose a considerable challenge and unacceptable maintenance burdens.

[1]: rust-lang/rust#92274
@lilasta
Copy link
Contributor Author

lilasta commented Jan 22, 2022

As now, as far as I can think of, there is two choices to implement the intrinsics. (Just to add, the reason why I try to change the behavior of const_allocate too is the intrinsic is possible to cause ICE easily. Currently, just including it in a runtime function will cause an ICE.)

  1. Use const_eval_select and #[inline(always)] to wrap the intrinsics:

const_(de)allocate isn't implemented in codegens, so we can't define a function which uses the intrinsic and wrap it with const_eval_select. (The compiler seems to generate a code for almost all functions found in a library crate) However, we may be able to avoid this problem by avoiding code generation with #[inline(always)]. If it always do inlining...

  1. Modify codegen to support calling the intrinsics at runtime.

This may be little bit troublesome since it needs to be implemented in all codegen backends. Just modify rustc_codegen_ssa and it will work. At runtime, const_allocate just returns a null pointer, and const_deallocate does nothing. I think this is fine.

Please let me know if there's a better solution.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2022

The inline(always) solution seems like the right one, but the PR for that is a bit in the future.

Until then, we could add something to the pre codegen cleanup that replaces just const_eval_select with its runtime argument

@lilasta
Copy link
Contributor Author

lilasta commented Jan 22, 2022

Until then, we could add something to the pre codegen cleanup that replaces just const_eval_select with its runtime argument

Assuming that we do it with rustc_mir_transform, is there any guarantee that the compile-time argument will not be codegen'd after the replacement? (if the argument isn't used by others)
Or is there a pass to remove a dead code?

I think it's troublesome but reimplementing const_eval_select with builtin macro may be another candidate.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2022

We have separate MIR for CTFE and for runtime. As long as this opt is in the optimized_mir optimizations, it will never end up in CTFE

@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2022

Wait I misunderstood. That is not an issue I think, with this change, the monomorphization collector should be able to do everything correctly

@lilasta
Copy link
Contributor Author

lilasta commented Jan 23, 2022

I added mirpass which replaces, but I still get a codegen error... It looks like we still need #[inline(always)].
I have added the commit for your reference.

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 25, 2022

Do you have a backtrace available? Is it encountering a call to that intrinsic or is it just trying to generate code so people can call the intrinsic in downstream crates?

@lilasta
Copy link
Contributor Author

lilasta commented Jan 25, 2022

The codegen error occurs during building core with stage1 compiler.

Backtrace:
Assembling stage1 compiler (x86_64-unknown-linux-gnu)
Building stage1 std artifacts (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
   Compiling compiler_builtins v0.1.66
   Compiling core v0.0.0 (/home/woppopo/Repo/rust/library/core)
   Compiling libc v0.2.108
   Compiling cc v1.0.69
   Compiling memchr v2.4.1
   Compiling std v0.0.0 (/home/woppopo/Repo/rust/library/std)
   Compiling unwind v0.0.0 (/home/woppopo/Repo/rust/library/unwind)
   Compiling rustc-std-workspace-core v1.99.0 (/home/woppopo/Repo/rust/library/rustc-std-workspace-core)
error: internal compiler error: compiler/rustc_codegen_llvm/src/intrinsic.rs:371:18: unknown intrinsic 'const_allocate'

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1169:9
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
             at ./library/std/src/panicking.rs:609:12
   1: std::panic::panic_any::<rustc_errors::ExplicitBug>
             at ./library/std/src/panic.rs:60:5
   2: <rustc_errors::HandlerInner>::bug
             at ./compiler/rustc_errors/src/lib.rs:1169:9
   3: <rustc_errors::Handler>::bug
             at ./compiler/rustc_errors/src/lib.rs:864:9
   4: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
             at ./compiler/rustc_middle/src/util/bug.rs:34:34
   5: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, ()>::{closure#0}
             at ./compiler/rustc_middle/src/ty/context.rs:1836:40
   6: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, ()>::{closure#0}, ()>
             at ./compiler/rustc_middle/src/ty/context.rs:1788:22
   7: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, ()>
             at ./compiler/rustc_middle/src/ty/context.rs:1836:9
   8: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>
             at ./compiler/rustc_middle/src/util/bug.rs:30:5
   9: rustc_middle::util::bug::bug_fmt
             at ./compiler/rustc_middle/src/util/bug.rs:14:5
  10: <rustc_codegen_llvm::builder::Builder as rustc_codegen_ssa::traits::intrinsic::IntrinsicCallMethods>::codegen_intrinsic_call
  11: <rustc_codegen_ssa::mir::FunctionCx<rustc_codegen_llvm::builder::Builder>>::codegen_intrinsic_call
             at ./compiler/rustc_codegen_ssa/src/mir/intrinsic.rs:571:17
  12: <rustc_codegen_ssa::mir::FunctionCx<rustc_codegen_llvm::builder::Builder>>::codegen_call_terminator
             at ./compiler/rustc_codegen_ssa/src/mir/block.rs:748:17
  13: <rustc_codegen_ssa::mir::FunctionCx<rustc_codegen_llvm::builder::Builder>>::codegen_terminator
             at ./compiler/rustc_codegen_ssa/src/mir/block.rs:1080:17
  14: <rustc_codegen_ssa::mir::FunctionCx<rustc_codegen_llvm::builder::Builder>>::codegen_block
             at ./compiler/rustc_codegen_ssa/src/mir/block.rs:1019:9
  15: rustc_codegen_ssa::mir::codegen_mir::<rustc_codegen_llvm::builder::Builder>
             at ./compiler/rustc_codegen_ssa/src/mir/mod.rs:246:9
  16: rustc_codegen_ssa::base::codegen_instance::<rustc_codegen_llvm::builder::Builder>
             at ./compiler/rustc_codegen_ssa/src/base.rs:361:5
  17: <rustc_middle::mir::mono::MonoItem as rustc_codegen_ssa::mono_item::MonoItemExt>::define::<rustc_codegen_llvm::builder::Builder>
             at ./compiler/rustc_codegen_ssa/src/mono_item.rs:69:17
  18: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
             at ./compiler/rustc_codegen_llvm/src/base.rs:92:17
  19: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl::<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}
             at ./compiler/rustc_query_system/src/dep_graph/graph.rs:326:53
  20: <rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps::<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}::{closure#0}
             at ./compiler/rustc_middle/src/dep_graph/mod.rs:55:46
  21: rustc_middle::ty::context::tls::enter_context::<<rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}
             at ./compiler/rustc_middle/src/ty/context.rs:1771:50
  22: rustc_middle::ty::context::tls::set_tlv::<rustc_middle::ty::context::tls::enter_context<<rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
             at ./compiler/rustc_middle/src/ty/context.rs:1755:9
  23: rustc_middle::ty::context::tls::enter_context::<<rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
             at ./compiler/rustc_middle/src/ty/context.rs:1771:9
  24: <rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps::<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}
             at ./compiler/rustc_middle/src/dep_graph/mod.rs:55:13
  25: rustc_middle::ty::context::tls::with_context::<<rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}
             at ./compiler/rustc_middle/src/ty/context.rs:1799:40
  26: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_context<<rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
             at ./compiler/rustc_middle/src/ty/context.rs:1788:22
  27: rustc_middle::ty::context::tls::with_context::<<rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
             at ./compiler/rustc_middle/src/ty/context.rs:1799:9
  28: <rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps::<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>::{closure#0}, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
             at ./compiler/rustc_middle/src/dep_graph/mod.rs:52:9
  29: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl::<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
             at ./compiler/rustc_query_system/src/dep_graph/graph.rs:326:22
  30: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
             at ./compiler/rustc_query_system/src/dep_graph/graph.rs:274:13
  31: rustc_codegen_llvm::base::compile_codegen_unit
             at ./compiler/rustc_codegen_llvm/src/base.rs:62:23
  32: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::compile_codegen_unit
             at ./compiler/rustc_codegen_llvm/src/lib.rs:118:9
  33: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
             at ./compiler/rustc_codegen_ssa/src/base.rs:684:38
  34: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
             at ./compiler/rustc_codegen_llvm/src/lib.rs:328:18
  35: rustc_interface::passes::start_codegen::{closure#0}
             at ./compiler/rustc_interface/src/passes.rs:1093:9
  36: <rustc_data_structures::profiling::VerboseTimingGuard>::run::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
             at ./compiler/rustc_data_structures/src/profiling.rs:644:9
  37: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
             at ./compiler/rustc_session/src/utils.rs:16:9
  38: rustc_interface::passes::start_codegen
             at ./compiler/rustc_interface/src/passes.rs:1092:19
  39: <rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}
             at ./compiler/rustc_interface/src/queries.rs:254:20
  40: <rustc_interface::passes::QueryContext>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>::{closure#0}
             at ./compiler/rustc_interface/src/passes.rs:821:42
  41: rustc_middle::ty::context::tls::enter_context::<<rustc_interface::passes::QueryContext>::enter<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>::{closure#0}
             at ./compiler/rustc_middle/src/ty/context.rs:1771:50
  42: rustc_middle::ty::context::tls::set_tlv::<rustc_middle::ty::context::tls::enter_context<<rustc_interface::passes::QueryContext>::enter<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>
             at ./compiler/rustc_middle/src/ty/context.rs:1755:9
  43: rustc_middle::ty::context::tls::enter_context::<<rustc_interface::passes::QueryContext>::enter<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>
             at ./compiler/rustc_middle/src/ty/context.rs:1771:9
  44: <rustc_interface::passes::QueryContext>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>
             at ./compiler/rustc_interface/src/passes.rs:821:9
  45: <rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}
             at ./compiler/rustc_interface/src/queries.rs:245:13
  46: <rustc_interface::queries::Query<alloc::boxed::Box<dyn core::any::Any>>>::compute::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}>
             at ./compiler/rustc_interface/src/queries.rs:38:28
  47: rustc_driver::run_compiler::{closure#1}::{closure#2}
             at ./compiler/rustc_driver/src/lib.rs:410:13
  48: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorReported>>
             at ./compiler/rustc_interface/src/queries.rs:393:19
  49: rustc_driver::run_compiler::{closure#1}
             at ./compiler/rustc_driver/src/lib.rs:315:22
  50: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#1}
             at ./compiler/rustc_interface/src/interface.rs:229:13
  51: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
             at ./compiler/rustc_span/src/lib.rs:1011:5
  52: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>
             at ./compiler/rustc_interface/src/interface.rs:223:5
  53: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}
             at ./compiler/rustc_interface/src/interface.rs:245:12
  54: rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}
             at ./compiler/rustc_interface/src/util.rs:148:13
  55: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>
             at /home/woppopo/.cargo/registry/src/github.com-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:137:9
  56: rustc_span::create_session_globals_then::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}>
             at ./compiler/rustc_span/src/lib.rs:109:5
  57: rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}
             at ./compiler/rustc_interface/src/util.rs:146:9
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: 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.60.0-dev running on x86_64-unknown-linux-gnu

note: compiler flags: -Z unstable-options -Z macro-backtrace -Z crate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/") -Z binary-dep-depinfo -Z force-unstable-if-unmarked -C opt-level=3 -C embed-bitcode=no -C debuginfo=1 -C debug-assertions=on -C incremental -C symbol-mangling-version=legacy -C link-args=-Wl,-z,origin -C link-args=-Wl,-rpath,$ORIGIN/../lib -C prefer-dynamic -C llvm-args=-import-instr-limit=10 -C embed-bitcode=yes --crate-type lib

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

query stack during panic:
end of query stack
error: could not compile `core`
warning: build failed, waiting for other jobs to finish...
error: build failed
Build completed unsuccessfully in 0:04:47

The following is mir-dump result of transforming this with ReplaceConstEvalSelect.

Before:
// MIR for `is_const_eval` before ReplaceConstEvalSelect

fn is_const_eval() -> bool {
    let mut _0: bool;                    // return place in scope 0 at src/test/ui/intrinsics/const-eval-select.rs:16:29: 16:33
    let mut _1: ();                      // in scope 0 at src/test/ui/intrinsics/const-eval-select.rs:17:32: 17:34
    scope 1 {
    }

    bb0: {
        StorageLive(_1);                 // scope 1 at src/test/ui/intrinsics/const-eval-select.rs:17:32: 17:34
        _0 = const_eval_select::<(), fn() -> bool {yes}, fn() -> bool {no}, bool>(move _1, yes, no) -> bb1; // scope 1 at src/test/ui/intrinsics/const-eval-select.rs:17:14: 17:44
                                         // mir::Constant
                                         // + span: src/test/ui/intrinsics/const-eval-select.rs:17:14: 17:31
                                         // + literal: Const { ty: unsafe fn((), fn() -> bool {yes}, fn() -> bool {no}) -> bool {std::intrinsics::const_eval_select::<(), fn() -> bool {yes}, fn() -> bool {no}, bool>}, val: Value(Scalar(<ZST>)) }
                                         // mir::Constant
                                         // + span: src/test/ui/intrinsics/const-eval-select.rs:17:36: 17:39
                                         // + literal: Const { ty: fn() -> bool {yes}, val: Value(Scalar(<ZST>)) }
                                         // mir::Constant
                                         // + span: src/test/ui/intrinsics/const-eval-select.rs:17:41: 17:43
                                         // + literal: Const { ty: fn() -> bool {no}, val: Value(Scalar(<ZST>)) }
    }

    bb1: {
        StorageDead(_1);                 // scope 1 at src/test/ui/intrinsics/const-eval-select.rs:17:43: 17:44
        return;                          // scope 0 at src/test/ui/intrinsics/const-eval-select.rs:18:2: 18:2
    }
}
After:
// MIR for `is_const_eval` after ReplaceConstEvalSelect

fn is_const_eval() -> bool {
    let mut _0: bool;                    // return place in scope 0 at src/test/ui/intrinsics/const-eval-select.rs:16:29: 16:33
    let mut _1: ();                      // in scope 0 at src/test/ui/intrinsics/const-eval-select.rs:17:32: 17:34
    scope 1 {
    }

    bb0: {
        StorageLive(_1);                 // scope 1 at src/test/ui/intrinsics/const-eval-select.rs:17:32: 17:34
        _0 = no() -> bb1;                // scope 1 at src/test/ui/intrinsics/const-eval-select.rs:17:14: 17:44
                                         // mir::Constant
                                         // + span: src/test/ui/intrinsics/const-eval-select.rs:17:41: 17:43
                                         // + literal: Const { ty: fn() -> bool {no}, val: Value(Scalar(<ZST>)) }
    }

    bb1: {
        StorageDead(_1);                 // scope 1 at src/test/ui/intrinsics/const-eval-select.rs:17:43: 17:44
        return;                          // scope 0 at src/test/ui/intrinsics/const-eval-select.rs:18:2: 18:2
    }
}

@lilasta
Copy link
Contributor Author

lilasta commented Jan 25, 2022

Oh, I noticed that (perhaps) I made a mistake... I'll check it immediately.

let terminator = block.terminator.as_mut().unwrap();
if let TerminatorKind::Call { func, args, .. } = &mut terminator.kind {
if let ty::FnDef(def_id, _) = func.ty(local_decls, tcx).kind() {
if Some(*def_id) == tcx.lang_items().const_eval_select() {
Copy link
Contributor Author

@lilasta lilasta Jan 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe...
if Some(*def_id) == tcx.lang_items().const_eval_select()
if intrinsic_name == sym::const_eval_select

I misunderstood... These are equivalent...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... it would be weird if that fixes it. while comparing the name is ok, going via the def id takes less work and I don't see why it shouldn't work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... one thing that could in theory happen (but doesn't happen here) is that we create a function pointer to const_eval_select calling something, and then it would get monomorphized.

From the backtrace it looks like we're codegenning the at_compiletime helper function. So... maybe this is happening because of the monomorphization collector. I was afraid of that ^^ let me grab you a link

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok... so... the collector will pick up the generics somewhere in the visitor in

impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {

at this point... I think maybe we should give up XD I'm sorry for sending you on a wild goose chase. Let's just codegen the intrinsics to abort or sth and be done with it. Unfortunate, but it seems to fragile to continue here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we cannot make it abort, it should just be a NOP at runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both intrinsics could just abort, as we can use const eval select for the runtime behavior. Implementing const_alloc cannot just be a nop, only dealloc can. So we'd need some complex logic anyway

…onst. Does nothing at runtime.

`const_allocate`:  Returns a null pointer at runtime.
@oli-obk
Copy link
Contributor

oli-obk commented Jan 28, 2022

@bors r+

thanks for bearing with me during this back and forth!

@bors
Copy link
Contributor

bors commented Jan 28, 2022

📌 Commit 7a7144f has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 28, 2022
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub fn const_allocate(size: usize, align: usize) -> *mut u8;

/// Deallocate a memory which allocated by `intrinsics::const_allocate` at compile time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should mention that it does nothing when the allocation was created by another constant than the one that is currently being evaluated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 28, 2022

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 28, 2022
@oli-obk
Copy link
Contributor

oli-obk commented Jan 29, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Jan 29, 2022

📌 Commit 9728cc4 has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 29, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 29, 2022
…askrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#88205 (Add Explanation For Error E0772)
 - rust-lang#92274 (Add `intrinsics::const_deallocate`)
 - rust-lang#93236 (Make `NonNull::new` `const`)
 - rust-lang#93299 (Fix dot separator when there is no source link)
 - rust-lang#93410 (kmc-solid: Implement `net::FileDesc::duplicate`)
 - rust-lang#93424 (fix nit)
 - rust-lang#93431 (remove unused `jemallocator` crate)
 - rust-lang#93453 (Add GUI theme change test)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9e86a43 into rust-lang:master Jan 29, 2022
@rustbot rustbot added this to the 1.60.0 milestone Jan 29, 2022
@lilasta lilasta deleted the const_deallocate branch January 30, 2022 07:32
yvt added a commit to r3-os/r3 that referenced this pull request Feb 3, 2022
… a null pointer

The change to `const_allocate`'s behavior was introduced by
[rust-lang/rust#92274][1].

This means two things: For one, we no longer need `const_eval_select`
to keep the compiler from panicking. For two, it can now return a null
pointer, for which we must be prepared.

[1]: rust-lang/rust#92274
yvt added a commit to r3-os/r3 that referenced this pull request Feb 3, 2022
…ing with `rlsf`

Since CTFE-heap deallocation is now [supported natively][1], we don't
need our own sub-allocator anymore.

This also speeds up the compilation.

[1]: rust-lang/rust#92274
yvt added a commit to r3-os/r3 that referenced this pull request Feb 3, 2022
…ting with `rlsf`

Since CTFE-heap deallocation is now [supported natively][1], we don't
need our own sub-allocator anymore.

This also speeds up the compilation.

[1]: rust-lang/rust#92274
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants