rust-lang / rust-lang/rust

high memory requirement during monomorphization

Open
#125,212 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-monomorphization C-optimization I-compilemem T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I noticed this while fuzzing because rustc would crash after hitting the memory limit inside the sandbox
We need around 1.5 gb of memory during monomorphization here it seems.
Next solver does not help.

I tried this code:
rustc file.rs -Ztime-passes -Clink-dead-code

struct Foo {
    bar: dyn for<'r> Fn(usize, &'r dyn Fn(
            &dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(u32))))))))),
        ))
}

fn main() {
}

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (8c127df75 2024-05-16)
binary: rustc
commit-hash: 8c127df75fde3d5ad8ef9af664962a7676288b52
commit-date: 2024-05-16
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4
Backtrace

 rustc ice.rs -Ztime-passes -Clink-dead-code
time:   0.000; rss:   46MB ->   47MB (   +2MB)	parse_crate
time:   0.000; rss:   48MB ->   49MB (   +1MB)	setup_global_ctxt
time:   0.000; rss:   50MB ->   50MB (   +1MB)	crate_injection
time:   0.002; rss:   50MB ->   63MB (  +12MB)	expand_crate
time:   0.002; rss:   50MB ->   63MB (  +12MB)	macro_expand_crate
time:   0.000; rss:   63MB ->   63MB (   +0MB)	AST_validation
time:   0.000; rss:   63MB ->   63MB (   +0MB)	finalize_imports
time:   0.000; rss:   63MB ->   67MB (   +4MB)	finalize_macro_resolutions
time:   0.000; rss:   67MB ->   67MB (   +0MB)	late_resolve_crate
time:   0.000; rss:   67MB ->   67MB (   +0MB)	resolve_check_unused
time:   0.000; rss:   67MB ->   67MB (   +0MB)	resolve_postprocess
time:   0.001; rss:   63MB ->   67MB (   +5MB)	resolve_crate
time:   0.000; rss:   68MB ->   68MB (   +0MB)	complete_gated_feature_checking
time:   0.000; rss:   68MB ->   69MB (   +2MB)	looking_for_entry_point
time:   0.000; rss:   70MB ->   71MB (   +0MB)	unused_lib_feature_checking
time:   0.000; rss:   68MB ->   71MB (   +3MB)	misc_checking_1
time:   0.001; rss:   71MB ->   78MB (   +7MB)	coherence_checking
time:   0.001; rss:   71MB ->   79MB (   +8MB)	type_check_crate
time:   0.000; rss:   79MB ->   81MB (   +2MB)	MIR_borrow_checking
time:   0.000; rss:   81MB ->   82MB (   +1MB)	MIR_effect_checking
warning: struct `Foo` is never constructed
 --> ice.rs:1:8
  |
1 | struct Foo {
  |        ^^^
  |
  = note: `#[warn(dead_code)]` on by default

time:   0.000; rss:   86MB ->   86MB (   +0MB)	module_lints
time:   0.000; rss:   86MB ->   86MB (   +0MB)	lint_checking
time:   0.002; rss:   82MB ->   86MB (   +4MB)	misc_checking_3
time:   0.000; rss:   86MB ->   87MB (   +1MB)	monomorphization_collector_root_collections
time:   5.850; rss:   87MB -> 1537MB (+1450MB)	monomorphization_collector_graph_walk
time:   0.000; rss: 1537MB -> 1538MB (   +1MB)	partition_and_assert_distinct_symbols
time:   0.000; rss: 1538MB -> 1541MB (   +3MB)	write_allocator_module
time:   0.001; rss: 1547MB -> 1557MB (   +9MB)	codegen_to_LLVM_IR
time:   5.853; rss:   86MB -> 1557MB (+1471MB)	codegen_crate
time:   0.000; rss: 1557MB -> 1557MB (   +0MB)	serialize_dep_graph
time:   0.023; rss: 1557MB -> 1279MB ( -277MB)	LLVM_passes
time:   0.078; rss: 1557MB ->  209MB (-1349MB)	free_global_ctxt
time:   0.051; rss:  209MB ->  209MB (   +0MB)	run_linker
time:   0.052; rss:  209MB ->  209MB (   +0MB)	link_binary
time:   0.052; rss:  209MB ->  209MB (   +0MB)	link_crate
time:   0.052; rss:  209MB ->  209MB (   +0MB)	link
warning: 1 warning emitted

time:   5.999; rss:   32MB ->  102MB (  +70MB)	total

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the supplied nested dyn Fn example with rustc 1.80.0-nightly and -Ztime-passes -Clink-dead-code; the reported spike is in monomorphization_collector_graph_walk. Compare peak memory while investigating that compiler stage, and consider the issue addressed when the reproducer no longer requires the reported ~1.5 GB or hits the sandbox limit.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.