Find a file
Matthias Krüger 073b1208f0
Rollup merge of #91266 - jam1garner:fmt-ptr-fix, r=dtolnay
Use non-generic inner function for pointer formatting

Previously, despite the implementation being type-unaware, `fmt::Pointer`'s implementation for `*const T` in monomorphized. This affects:

* `fmt::Debug` for `*const T`
* `fmt::Debug` for `*mut T`
* `fmt::Pointer` for `*const T`
* `fmt::Pointer` for `*mut T`

And since the implementation is non-trivial, this results in a large amount of LLVM bitcode being generated. For example, with a large bindgen project with Debug implementations enabled, it will generate a lot of calls to `fmt::Debug for *const T`, which in turn will perform codegen for a copy of this function for every type.

For example, in a real-world bindgen'd header I've been testing with (4,189,245 lines of bindgen Rust with layout tests disabled) the difference between a slightly old nightly (`rustc 1.58.0-nightly (e249ce6b2 2021-10-30)`) and this PR:

<details>
<summary>Nightly (Click to Expand)</summary>

```
  Lines           Copies         Function name
  -----           ------         -------------
  7256000 (100%)  216544 (100%)  (TOTAL)
  1815449 (25.0%)  24206 (11.2%) <*const T as core::fmt::Pointer>::fmt
   300248 (4.1%)   29579 (13.7%) <&T as core::fmt::Debug>::fmt
   290328 (4.0%)   24194 (11.2%) <*mut T as core::fmt::Pointer>::fmt
   217746 (3.0%)   24194 (11.2%) <*mut T as core::fmt::Debug>::fmt
   123329 (1.7%)    1486 (0.7%)  core::fmt::builders::DebugList::entries
    72790 (1.0%)    1486 (0.7%)  core::slice::iter::Iter<T>::post_inc_start
    71313 (1.0%)    1486 (0.7%)  core::slice::iter::Iter<T>::new
    68329 (0.9%)    1486 (0.7%)  <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::next
    38636 (0.5%)    1486 (0.7%)  <[T] as core::fmt::Debug>::fmt
    26874 (0.4%)    1493 (0.7%)  core::array::<impl core::fmt::Debug for [T; N]>::fmt
    22290 (0.3%)    1486 (0.7%)  core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
    19407 (0.3%)    1493 (0.7%)  core::array::<impl core::ops::index::Index<I> for [T; N]>::index
    19318 (0.3%)    1486 (0.7%)  core::slice::<impl [T]>::iter
    17832 (0.2%)    1486 (0.7%)  core::ptr::const_ptr::<impl *const T>::offset
    17832 (0.2%)    1486 (0.7%)  core::ptr::mut_ptr::<impl *mut T>::offset
    16346 (0.2%)    1486 (0.7%)  <core::ops::range::RangeFull as core::slice::index::SliceIndex<[T]>>::index
    13374 (0.2%)    1486 (0.7%)  <I as core::iter::traits::collect::IntoIterator>::into_iter
    13374 (0.2%)    1486 (0.7%)  core::ptr::const_ptr::<impl *const T>::add
    13371 (0.2%)    1486 (0.7%)  core::ptr::const_ptr::<impl *const T>::is_null
    13371 (0.2%)    1486 (0.7%)  core::ptr::mut_ptr::<impl *mut T>::is_null
    11888 (0.2%)    1486 (0.7%)  core::slice::<impl [T]>::as_ptr
    11879 (0.2%)    1486 (0.7%)  core::ptr::non_null::NonNull<T>::new_unchecked
     7421 (0.1%)    1486 (0.7%)  core::ptr::non_null::NonNull<T>::as_ptr

```

</details>

<details>
<summary>This PR (Click to Expand)</summary>

```
   Lines           Copies         Function name
  -----           ------         -------------
  5684504 (100%)  216542 (100%)  (TOTAL)
   300248 (5.3%)   29579 (13.7%) <&T as core::fmt::Debug>::fmt
   290328 (5.1%)   24194 (11.2%) <*mut T as core::fmt::Pointer>::fmt
   266265 (4.7%)   24206 (11.2%) <*const T as core::fmt::Pointer>::fmt
   217746 (3.8%)   24194 (11.2%) <*mut T as core::fmt::Debug>::fmt
   101039 (1.8%)    1486 (0.7%)  core::fmt::builders::DebugList::entries
    72790 (1.3%)    1486 (0.7%)  core::slice::iter::Iter<T>::post_inc_start
    71313 (1.3%)    1486 (0.7%)  core::slice::iter::Iter<T>::new
    68329 (1.2%)    1486 (0.7%)  <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::next
    38636 (0.7%)    1486 (0.7%)  <[T] as core::fmt::Debug>::fmt
    26874 (0.5%)    1493 (0.7%)  core::array::<impl core::fmt::Debug for [T; N]>::fmt
    22290 (0.4%)    1486 (0.7%)  core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
    19407 (0.3%)    1493 (0.7%)  core::array::<impl core::ops::index::Index<I> for [T; N]>::index
    19318 (0.3%)    1486 (0.7%)  core::slice::<impl [T]>::iter
    17832 (0.3%)    1486 (0.7%)  core::ptr::const_ptr::<impl *const T>::offset
    17832 (0.3%)    1486 (0.7%)  core::ptr::mut_ptr::<impl *mut T>::offset
    16346 (0.3%)    1486 (0.7%)  <core::ops::range::RangeFull as core::slice::index::SliceIndex<[T]>>::index
    13374 (0.2%)    1486 (0.7%)  <I as core::iter::traits::collect::IntoIterator>::into_iter
    13374 (0.2%)    1486 (0.7%)  core::ptr::const_ptr::<impl *const T>::add
    13371 (0.2%)    1486 (0.7%)  core::ptr::const_ptr::<impl *const T>::is_null
    13371 (0.2%)    1486 (0.7%)  core::ptr::mut_ptr::<impl *mut T>::is_null
    11888 (0.2%)    1486 (0.7%)  core::slice::<impl [T]>::as_ptr
    11879 (0.2%)    1486 (0.7%)  core::ptr::non_null::NonNull<T>::new_unchecked
     7421 (0.1%)    1486 (0.7%)  core::ptr::non_null::NonNull<T>::as_ptr

```

</details>

Output generated using `cargo llvm-lines` version 0.4.12.

Summary of differences:

| rustc Version | Total LLVM line count | `*const T as fmt::Pointer` LLVM lines | Compilation Time |
|-|-|-|-|
| `nightly` | 7256000 | 1815449 (25.0% of binary) | 537.014 |
| PR | 5684504 (-21.65%) | 266265 (4.7% of binary) (-85.3% from nightly) | 502.990 |

This results in a pretty noticeable as the majority of rustc's time is spent in either codegen or LLVM, in this case, and is significantly improved by disabling derives for `fmt::Debug`, as it prevents generating all this LLVM IR to be handled.

Here's a run time comparison with nightly on the same codebase (commit 454cc5fb built from source vs 37c8f25 from my PR built from source):

<details>
<summary>nightly (Click to Expand)</summary>

```
time:   2.370; rss:   56MB -> 1118MB (+1062MB)	parse_crate
time:   0.000; rss: 1118MB -> 1118MB (   +0MB)	attributes_injection
time:   0.000; rss: 1118MB -> 1118MB (   +0MB)	incr_comp_prepare_session_directory
time:   0.000; rss: 1118MB -> 1118MB (   +0MB)	incr_comp_garbage_collect_session_directories
time:   0.000; rss: 1120MB -> 1120MB (   +0MB)	plugin_loading
time:   0.000; rss: 1120MB -> 1120MB (   +0MB)	plugin_registration
time:   0.000; rss: 1120MB -> 1120MB (   +0MB)	crate_injection
time:  13.897; rss: 1120MB -> 3147MB (+2027MB)	expand_crate
time:   0.002; rss: 3147MB -> 3147MB (   +0MB)	check_unused_macros
time:  13.900; rss: 1120MB -> 3147MB (+2027MB)	macro_expand_crate
time:   0.002; rss: 3147MB -> 3147MB (   +0MB)	maybe_building_test_harness
time:   0.503; rss: 3147MB -> 3147MB (   +0MB)	AST_validation
time:   0.000; rss: 3147MB -> 3147MB (   +0MB)	maybe_create_a_macro_crate
time:   0.002; rss: 3147MB -> 3147MB (   +0MB)	finalize_imports
time:   0.502; rss: 3147MB -> 3153MB (   +6MB)	finalize_macro_resolutions
time:   4.478; rss: 3153MB -> 3574MB ( +420MB)	late_resolve_crate
time:   0.000; rss: 3574MB -> 3574MB (   +0MB)	resolve_main
time:   0.332; rss: 3574MB -> 3574MB (   +0MB)	resolve_check_unused
time:   0.000; rss: 3574MB -> 3574MB (   +0MB)	resolve_report_errors
time:   0.279; rss: 3574MB -> 3574MB (   +0MB)	resolve_postprocess
time:   5.595; rss: 3147MB -> 3574MB ( +427MB)	resolve_crate
time:   0.382; rss: 3574MB -> 3574MB (   +0MB)	complete_gated_feature_checking
time:  20.526; rss: 1120MB -> 3574MB (+2454MB)	configure_and_expand
time:   0.000; rss: 3574MB -> 3574MB (   +0MB)	prepare_outputs
time:   0.000; rss: 3574MB -> 3574MB (   +0MB)	blocked_on_dep_graph_loading
time:  65.992; rss: 3574MB -> 6317MB (+2743MB)	hir_lowering
time:   1.117; rss: 6317MB -> 6323MB (   +6MB)	early_lint_checks
time:   1.447; rss: 6323MB -> 6271MB (  -52MB)	drop_ast
time:   0.002; rss: 5838MB -> 5838MB (   +0MB)	setup_global_ctxt
time:   0.000; rss: 5843MB -> 5843MB (   +0MB)	looking_for_entry_point
time:   0.313; rss: 5843MB -> 5844MB (   +1MB)	looking_for_derive_registrar
time:   9.652; rss: 5843MB -> 6065MB ( +222MB)	misc_checking_1
time:   9.713; rss: 6065MB -> 6769MB ( +704MB)	type_collecting
time:   0.665; rss: 6769MB -> 6769MB (   +0MB)	impl_wf_inference
time:   0.064; rss: 6769MB -> 6769MB (   +0MB)	unsafety_checking
time:   3.095; rss: 6769MB -> 6792MB (  +23MB)	coherence_checking
time:  21.282; rss: 6792MB -> 7546MB ( +754MB)	wf_checking
time:   5.404; rss: 7546MB -> 7681MB ( +135MB)	item_types_checking
time:  79.665; rss: 7681MB -> 8075MB ( +394MB)	item_bodies_checking
time: 120.166; rss: 6065MB -> 8081MB (+2016MB)	type_check_crate
time:   2.038; rss: 8081MB -> 8085MB (   +4MB)	match_checking
time:   1.300; rss: 8085MB -> 8113MB (  +28MB)	liveness_and_intrinsic_checking
time:   3.338; rss: 8081MB -> 8113MB (  +32MB)	misc_checking_2
time:  68.612; rss: 8113MB -> 9285MB (+1172MB)	MIR_borrow_checking
time:   0.622; rss: 9285MB -> 9301MB (  +17MB)	MIR_effect_checking
time:   0.000; rss: 9301MB -> 9301MB (   +0MB)	layout_testing
time:   4.331; rss: 9383MB -> 9510MB ( +127MB)	death_checking
time:   0.032; rss: 9510MB -> 9510MB (   +0MB)	unused_lib_feature_checking
time:   4.444; rss: 9510MB -> 9568MB (  +58MB)	crate_lints
time:  59.563; rss: 9568MB -> 9576MB (   +8MB)	module_lints
time:  64.006; rss: 9510MB -> 9576MB (  +66MB)	lint_checking
time:   4.127; rss: 9576MB -> 9639MB (  +62MB)	privacy_checking_modules
time:  77.984; rss: 9301MB -> 9639MB ( +337MB)	misc_checking_3
time:   0.311; rss: 10357MB -> 10357MB (   +0MB)	monomorphization_collector_root_collections
time:  14.051; rss: 10357MB -> 10573MB ( +217MB)	monomorphization_collector_graph_walk
time:   1.759; rss: 10573MB -> 10652MB (  +79MB)	partition_and_assert_distinct_symbols
time:  28.518; rss: 9639MB -> 10711MB (+1072MB)	generate_crate_metadata
time:   0.000; rss: 10711MB -> 10711MB (   +0MB)	find_cgu_reuse
time:  63.408; rss: 10711MB -> 12272MB (+1560MB)	codegen_to_LLVM_IR
time:  64.916; rss: 10711MB -> 12267MB (+1556MB)	codegen_crate
time:   0.000; rss: 12261MB -> 12261MB (   +0MB)	assert_dep_graph
time:   0.000; rss: 12261MB -> 12261MB (   +0MB)	check_dirty_clean
time:   0.664; rss: 12230MB -> 12210MB (  -20MB)	encode_query_results_for(rustc_query_impl::queries::type_of)
time:   2.111; rss: 12210MB -> 12043MB ( -167MB)	encode_query_results_for(rustc_query_impl::queries::generics_of)
time:   0.108; rss: 12043MB -> 12057MB (  +14MB)	encode_query_results_for(rustc_query_impl::queries::predicates_of)
time:   0.004; rss: 12057MB -> 12059MB (   +2MB)	encode_query_results_for(rustc_query_impl::queries::mir_const_qualif)
time:   0.665; rss: 12059MB -> 12121MB (  +62MB)	encode_query_results_for(rustc_query_impl::queries::mir_for_ctfe)
time:  16.149; rss: 12121MB -> 12148MB (  +28MB)	encode_query_results_for(rustc_query_impl::queries::optimized_mir)
time:   0.000; rss: 12148MB -> 12148MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::covered_file_name)
time:   0.000; rss: 12148MB -> 12148MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::covered_code_regions)
time:   0.010; rss: 12148MB -> 12150MB (   +2MB)	encode_query_results_for(rustc_query_impl::queries::promoted_mir)
time:   0.052; rss: 12150MB -> 12155MB (   +4MB)	encode_query_results_for(rustc_query_impl::queries::unsafety_check_result)
time:   0.003; rss: 12155MB -> 12156MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::thir_check_unsafety)
time:  11.428; rss: 12156MB -> 11748MB ( -408MB)	encode_query_results_for(rustc_query_impl::queries::typeck)
time:   0.000; rss: 11748MB -> 11748MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::diagnostic_only_typeck)
time:   0.094; rss: 11748MB -> 11756MB (   +8MB)	encode_query_results_for(rustc_query_impl::queries::used_trait_imports)
time:   0.272; rss: 11756MB -> 11778MB (  +22MB)	encode_query_results_for(rustc_query_impl::queries::mir_borrowck)
time:   0.054; rss: 11778MB -> 11778MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::eval_to_allocation_raw)
time:   0.005; rss: 11778MB -> 11779MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::eval_to_const_value_raw)
time:   0.021; rss: 11779MB -> 11784MB (   +5MB)	encode_query_results_for(rustc_query_impl::queries::check_match)
time:   0.041; rss: 11784MB -> 11786MB (   +2MB)	encode_query_results_for(rustc_query_impl::queries::symbol_name)
time:   0.743; rss: 11786MB -> 11815MB (  +29MB)	encode_query_results_for(rustc_query_impl::queries::codegen_fn_attrs)
time:   0.043; rss: 11815MB -> 11816MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::codegen_fulfill_obligation)
time:   0.674; rss: 11816MB -> 11840MB (  +25MB)	encode_query_results_for(rustc_query_impl::queries::specialization_graph_of)
time:   0.000; rss: 11840MB -> 11840MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::adt_drop_tys)
time:   0.000; rss: 11840MB -> 11840MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::adt_significant_drop_tys)
time:   0.005; rss: 11840MB -> 11841MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::unused_generic_params)
time:  33.153; rss: 12232MB -> 11841MB ( -390MB)	encode_query_results
time:  88.943; rss: 11955MB -> 11783MB ( -173MB)	LLVM_passes(crate)
time:  38.854; rss: 12259MB -> 10095MB (-2164MB)	incr_comp_serialize_result_cache
time:  39.030; rss: 12261MB -> 10095MB (-2166MB)	incr_comp_persist_result_cache
time:   0.000; rss: 10095MB -> 10095MB (   +0MB)	incr_comp_persist_dep_graph
time:  39.064; rss: 12257MB -> 10095MB (-2162MB)	serialize_dep_graph
time:  19.047; rss: 10095MB -> 10307MB ( +212MB)	free_global_ctxt
time:   0.000; rss: 10307MB -> 10307MB (   +0MB)	join_worker_thread
time:   0.519; rss: 10307MB -> 10307MB (   +0MB)	copy_all_cgu_workproducts_to_incr_comp_cache_dir
time:   0.522; rss: 10307MB -> 10307MB (   +0MB)	finish_ongoing_codegen
time:   0.000; rss: 10307MB -> 10307MB (   +0MB)	llvm_dump_timing_file
time:   0.002; rss: 10307MB -> 10307MB (   +0MB)	serialize_work_products
time:   0.001; rss: 9542MB -> 9542MB (   +0MB)	incr_comp_finalize_session_directory
time:   0.000; rss: 9542MB -> 9542MB (   +0MB)	link_binary_check_files_are_writeable
time:   7.835; rss: 9542MB -> 9544MB (   +2MB)	link_rlib
time:   0.000; rss: 9544MB -> 9544MB (   +0MB)	link_binary_remove_temps
time:   7.872; rss: 9542MB -> 9544MB (   +2MB)	link_binary
time:   7.944; rss: 9542MB -> 9201MB ( -341MB)	link_crate
time:   8.495; rss: 10307MB -> 9201MB (-1106MB)	link
time: 537.014; rss:   33MB -> 3715MB (+3682MB)	total
```

</details>

<details>
<summary>This PR (Click to Expand)</summary>

```
time:   2.379; rss:   51MB -> 1116MB (+1064MB)	parse_crate
time:   0.003; rss: 1116MB -> 1116MB (   +0MB)	attributes_injection
time:   0.002; rss: 1116MB -> 1116MB (   +0MB)	incr_comp_prepare_session_directory
time:   0.000; rss: 1116MB -> 1116MB (   +0MB)	incr_comp_garbage_collect_session_directories
time:   0.000; rss: 1116MB -> 1116MB (   +0MB)	plugin_loading
time:   0.000; rss: 1116MB -> 1116MB (   +0MB)	plugin_registration
time:   0.003; rss: 1118MB -> 1118MB (   +0MB)	crate_injection
time:  13.376; rss: 1118MB -> 3143MB (+2025MB)	expand_crate
time:   0.002; rss: 3143MB -> 3143MB (   +0MB)	check_unused_macros
time:  13.379; rss: 1118MB -> 3143MB (+2025MB)	macro_expand_crate
time:   0.002; rss: 3143MB -> 3143MB (   +0MB)	maybe_building_test_harness
time:   0.479; rss: 3143MB -> 3143MB (   +0MB)	AST_validation
time:   0.002; rss: 3143MB -> 3143MB (   +0MB)	maybe_create_a_macro_crate
time:   0.005; rss: 3143MB -> 3143MB (   +0MB)	finalize_imports
time:   0.520; rss: 3143MB -> 3125MB (  -18MB)	finalize_macro_resolutions
time:   4.446; rss: 3125MB -> 3577MB ( +453MB)	late_resolve_crate
time:   0.000; rss: 3577MB -> 3577MB (   +0MB)	resolve_main
time:   0.336; rss: 3577MB -> 3577MB (   +0MB)	resolve_check_unused
time:   0.000; rss: 3577MB -> 3577MB (   +0MB)	resolve_report_errors
time:   0.295; rss: 3577MB -> 3578MB (   +0MB)	resolve_postprocess
time:   5.602; rss: 3143MB -> 3578MB ( +435MB)	resolve_crate
time:   0.388; rss: 3578MB -> 3578MB (   +0MB)	complete_gated_feature_checking
time:  20.014; rss: 1116MB -> 3578MB (+2462MB)	configure_and_expand
time:   0.000; rss: 3578MB -> 3578MB (   +0MB)	prepare_outputs
time:   0.000; rss: 3578MB -> 3578MB (   +0MB)	blocked_on_dep_graph_loading
time:  64.219; rss: 3578MB -> 6313MB (+2736MB)	hir_lowering
time:   1.102; rss: 6313MB -> 6319MB (   +6MB)	early_lint_checks
time:   1.426; rss: 6319MB -> 6268MB (  -52MB)	drop_ast
time:   0.005; rss: 5834MB -> 5836MB (   +2MB)	setup_global_ctxt
time:   0.000; rss: 5838MB -> 5838MB (   +0MB)	looking_for_entry_point
time:   0.292; rss: 5838MB -> 5840MB (   +1MB)	looking_for_derive_registrar
time:   9.553; rss: 5838MB -> 6060MB ( +222MB)	misc_checking_1
time:   9.949; rss: 6060MB -> 6764MB ( +704MB)	type_collecting
time:   0.630; rss: 6764MB -> 6764MB (   +0MB)	impl_wf_inference
time:   0.060; rss: 6764MB -> 6764MB (   +0MB)	unsafety_checking
time:   3.054; rss: 6764MB -> 6787MB (  +23MB)	coherence_checking
time:  20.702; rss: 6787MB -> 7533MB ( +746MB)	wf_checking
time:   5.194; rss: 7533MB -> 7668MB ( +135MB)	item_types_checking
time:  74.677; rss: 7668MB -> 8062MB ( +394MB)	item_bodies_checking
time: 114.497; rss: 6060MB -> 8068MB (+2008MB)	type_check_crate
time:   1.891; rss: 8068MB -> 8072MB (   +4MB)	match_checking
time:   1.292; rss: 8072MB -> 8100MB (  +28MB)	liveness_and_intrinsic_checking
time:   3.183; rss: 8068MB -> 8100MB (  +32MB)	misc_checking_2
time:  68.845; rss: 8100MB -> 9279MB (+1179MB)	MIR_borrow_checking
time:   0.587; rss: 9279MB -> 9295MB (  +17MB)	MIR_effect_checking
time:   0.000; rss: 9295MB -> 9295MB (   +0MB)	layout_testing
time:   4.443; rss: 9377MB -> 9504MB ( +127MB)	death_checking
time:   0.034; rss: 9504MB -> 9504MB (   +0MB)	unused_lib_feature_checking
time:   4.409; rss: 9504MB -> 9562MB (  +58MB)	crate_lints
time:  56.490; rss: 9562MB -> 9571MB (   +8MB)	module_lints
time:  60.900; rss: 9504MB -> 9571MB (  +66MB)	lint_checking
time:   4.147; rss: 9571MB -> 9633MB (  +62MB)	privacy_checking_modules
time:  75.094; rss: 9295MB -> 9633MB ( +337MB)	misc_checking_3
time:   0.315; rss: 10357MB -> 10357MB (   +0MB)	monomorphization_collector_root_collections
time:  14.501; rss: 10357MB -> 10571MB ( +215MB)	monomorphization_collector_graph_walk
time:   1.763; rss: 10571MB -> 10661MB (  +89MB)	partition_and_assert_distinct_symbols
time:  29.035; rss: 9633MB -> 10706MB (+1073MB)	generate_crate_metadata
time:   0.000; rss: 10706MB -> 10706MB (   +0MB)	find_cgu_reuse
time:  30.913; rss: 10706MB -> 12150MB (+1444MB)	codegen_to_LLVM_IR
time:  31.108; rss: 10706MB -> 12150MB (+1444MB)	codegen_crate
time:   0.000; rss: 12150MB -> 12150MB (   +0MB)	assert_dep_graph
time:   0.000; rss: 12150MB -> 12150MB (   +0MB)	check_dirty_clean
time:   0.416; rss: 12152MB -> 12199MB (  +46MB)	encode_query_results_for(rustc_query_impl::queries::type_of)
time:   1.259; rss: 12199MB -> 12211MB (  +12MB)	encode_query_results_for(rustc_query_impl::queries::generics_of)
time:   0.095; rss: 12211MB -> 12193MB (  -18MB)	encode_query_results_for(rustc_query_impl::queries::predicates_of)
time:   0.005; rss: 12193MB -> 12195MB (   +2MB)	encode_query_results_for(rustc_query_impl::queries::mir_const_qualif)
time:   0.828; rss: 12195MB -> 12208MB (  +14MB)	encode_query_results_for(rustc_query_impl::queries::mir_for_ctfe)
time:  17.880; rss: 12208MB -> 11987MB ( -222MB)	encode_query_results_for(rustc_query_impl::queries::optimized_mir)
time:   0.000; rss: 11987MB -> 11987MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::covered_file_name)
time:   0.000; rss: 11987MB -> 11987MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::covered_code_regions)
time:   0.007; rss: 11987MB -> 11988MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::promoted_mir)
time:   0.049; rss: 11988MB -> 11992MB (   +4MB)	encode_query_results_for(rustc_query_impl::queries::unsafety_check_result)
time:   0.002; rss: 11992MB -> 11994MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::thir_check_unsafety)
time:  38.049; rss: 11994MB -> 12093MB (  +99MB)	encode_query_results_for(rustc_query_impl::queries::typeck)
time:   0.000; rss: 12093MB -> 12093MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::diagnostic_only_typeck)
time:   0.024; rss: 12093MB -> 12095MB (   +2MB)	encode_query_results_for(rustc_query_impl::queries::used_trait_imports)
time:   0.372; rss: 12095MB -> 12053MB (  -42MB)	encode_query_results_for(rustc_query_impl::queries::mir_borrowck)
time:   0.015; rss: 12053MB -> 12053MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::eval_to_allocation_raw)
time:   0.005; rss: 12053MB -> 12054MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::eval_to_const_value_raw)
time:   0.003; rss: 12054MB -> 12056MB (   +2MB)	encode_query_results_for(rustc_query_impl::queries::check_match)
time:   0.037; rss: 12056MB -> 11899MB ( -157MB)	encode_query_results_for(rustc_query_impl::queries::symbol_name)
time:   0.667; rss: 11899MB -> 11708MB ( -191MB)	encode_query_results_for(rustc_query_impl::queries::codegen_fn_attrs)
time:   0.045; rss: 11708MB -> 11709MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::codegen_fulfill_obligation)
time:   0.295; rss: 11709MB -> 11734MB (  +25MB)	encode_query_results_for(rustc_query_impl::queries::specialization_graph_of)
time:   0.000; rss: 11734MB -> 11734MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::adt_drop_tys)
time:   0.000; rss: 11734MB -> 11734MB (   +0MB)	encode_query_results_for(rustc_query_impl::queries::adt_significant_drop_tys)
time:   0.005; rss: 11734MB -> 11734MB (   +1MB)	encode_query_results_for(rustc_query_impl::queries::unused_generic_params)
time:  60.063; rss: 12152MB -> 11734MB ( -418MB)	encode_query_results
time:  76.745; rss: 12007MB -> 11699MB ( -308MB)	LLVM_passes(crate)
time:  61.634; rss: 12150MB -> 10557MB (-1593MB)	incr_comp_serialize_result_cache
time:  61.637; rss: 12150MB -> 10557MB (-1593MB)	incr_comp_persist_result_cache
time:   0.001; rss: 10557MB -> 10557MB (   +0MB)	incr_comp_persist_dep_graph
time:  61.641; rss: 12150MB -> 10557MB (-1593MB)	serialize_dep_graph
time:  15.601; rss: 10557MB -> 10242MB ( -315MB)	free_global_ctxt
time:   0.000; rss: 10242MB -> 10242MB (   +0MB)	join_worker_thread
time:   0.368; rss: 10242MB -> 10242MB (   +0MB)	copy_all_cgu_workproducts_to_incr_comp_cache_dir
time:   0.375; rss: 10242MB -> 10242MB (   +0MB)	finish_ongoing_codegen
time:   0.000; rss: 10242MB -> 10242MB (   +0MB)	llvm_dump_timing_file
time:   0.002; rss: 10242MB -> 10242MB (   +0MB)	serialize_work_products
time:   0.001; rss: 9668MB -> 9668MB (   +0MB)	incr_comp_finalize_session_directory
time:   0.000; rss: 9668MB -> 9668MB (   +0MB)	link_binary_check_files_are_writeable
time:   1.469; rss: 9668MB -> 9671MB (   +3MB)	link_rlib
time:   0.000; rss: 9671MB -> 9671MB (   +0MB)	link_binary_remove_temps
time:   1.506; rss: 9668MB -> 9671MB (   +3MB)	link_binary
time:   1.622; rss: 9668MB -> 9329MB ( -339MB)	link_crate
time:   2.037; rss: 10242MB -> 9329MB ( -913MB)	link
time: 502.990; rss:   32MB -> 5888MB (+5855MB)	total
```

</details>

(6.34% decrease in runtime, results are consistent across multiple runs)
2021-11-27 11:46:45 +01:00
.github Change paths for dist command to match the components they generate 2021-11-13 07:28:37 -06:00
compiler Rollup merge of #91208 - estebank:eq-constraint, r=cjgillot 2021-11-27 11:46:44 +01:00
library Rollup merge of #91266 - jam1garner:fmt-ptr-fix, r=dtolnay 2021-11-27 11:46:45 +01:00
src Rollup merge of #91208 - estebank:eq-constraint, r=cjgillot 2021-11-27 11:46:44 +01:00
.editorconfig Add .editorconfig 2021-02-02 18:13:18 +01:00
.gitattributes Remove rustfmt tests from top-level .gitattributes 2021-06-04 09:04:54 -04:00
.gitignore Fix invalid rules in .gitignore 2021-10-12 15:09:28 +02:00
.gitmodules Update to the final LLVM 13.0.0 release 2021-10-01 21:06:19 -07:00
.mailmap Add alias for guillaume.gomez@huawei.com 2021-10-24 19:55:04 +02:00
Cargo.lock Bump compiler-builtins to 0.1.53 2021-11-26 10:33:32 +00:00
Cargo.toml Temporarily turn overflow checks off for rustc-rayon-core 2021-10-24 15:36:45 +02:00
CODE_OF_CONDUCT.md
config.toml.example Auto merge of #90054 - michaelwoerister:v0-mangling-in-compiler, r=Mark-Simulacrum 2021-10-23 03:06:21 +00:00
configure
CONTRIBUTING.md Give people a single link they can click in the contributing guide 2021-11-22 13:10:22 -06:00
COPYRIGHT
LICENSE-APACHE
LICENSE-MIT
README.md Give people a single link they can click in the contributing guide 2021-11-22 13:10:22 -06:00
RELEASES.md add 1.56.1 to the release notes 2021-10-31 13:15:27 +01:00
rustfmt.toml Expose portable-simd as core::simd 2021-11-12 16:58:39 -08:00
triagebot.toml rustbot: Allow applying needs-fcp label 2021-11-04 13:36:00 +01:00
x.py Choose the version of python at runtime (portable version) 2021-01-14 21:00:42 -05:00

The Rust Programming Language

This is the main source code repository for Rust. It contains the compiler, standard library, and documentation.

Note: this README is for users rather than contributors. If you wish to contribute to the compiler, you should read the Getting Started section of the rustc-dev-guide instead. You can ask for help in the #new members Zulip stream.

Quick Start

Read "Installation" from The Book.

Installing from Source

The Rust build system uses a Python script called x.py to build the compiler, which manages the bootstrapping process. It lives in the root of the project.

The x.py command can be run directly on most systems in the following format:

./x.py <subcommand> [flags]

This is how the documentation and examples assume you are running x.py.

Systems such as Ubuntu 20.04 LTS do not create the necessary python command by default when Python is installed that allows x.py to be run directly. In that case you can either create a symlink for python (Ubuntu provides the python-is-python3 package for this), or run x.py using Python itself:

# Python 3
python3 x.py <subcommand> [flags]

# Python 2.7
python2.7 x.py <subcommand> [flags]

More information about x.py can be found by running it with the --help flag or reading the rustc dev guide.

Building on a Unix-like system

  1. Make sure you have installed the dependencies:

    • g++ 5.1 or later or clang++ 3.5 or later
    • python 3 or 2.7
    • GNU make 3.81 or later
    • cmake 3.13.4 or later
    • ninja
    • curl
    • git
    • ssl which comes in libssl-dev or openssl-devel
    • pkg-config if you are compiling on Linux and targeting Linux
  2. Clone the source with git:

    git clone https://github.com/rust-lang/rust.git
    cd rust
    
  1. Configure the build settings:

    The Rust build system uses a file named config.toml in the root of the source tree to determine various configuration settings for the build. Copy the default config.toml.example to config.toml to get started.

    cp config.toml.example config.toml
    

    If you plan to use x.py install to create an installation, it is recommended that you set the prefix value in the [install] section to a directory.

    Create install directory if you are not installing in default directory

  2. Build and install:

    ./x.py build && ./x.py install
    

    When complete, ./x.py install will place several programs into $PREFIX/bin: rustc, the Rust compiler, and rustdoc, the API-documentation tool. This install does not include Cargo, Rust's package manager. To build and install Cargo, you may run ./x.py install cargo or set the build.extended key in config.toml to true to build and install all tools.

Building on Windows

There are two prominent ABIs in use on Windows: the native (MSVC) ABI used by Visual Studio, and the GNU ABI used by the GCC toolchain. Which version of Rust you need depends largely on what C/C++ libraries you want to interoperate with: for interop with software produced by Visual Studio use the MSVC build of Rust; for interop with GNU software built using the MinGW/MSYS2 toolchain use the GNU build.

MinGW

MSYS2 can be used to easily build Rust on Windows:

  1. Grab the latest MSYS2 installer and go through the installer.

  2. Run mingw32_shell.bat or mingw64_shell.bat from wherever you installed MSYS2 (i.e. C:\msys64), depending on whether you want 32-bit or 64-bit Rust. (As of the latest version of MSYS2 you have to run msys2_shell.cmd -mingw32 or msys2_shell.cmd -mingw64 from the command line instead)

  3. From this terminal, install the required tools:

    # Update package mirrors (may be needed if you have a fresh install of MSYS2)
    pacman -Sy pacman-mirrors
    
    # Install build tools needed for Rust. If you're building a 32-bit compiler,
    # then replace "x86_64" below with "i686". If you've already got git, python,
    # or CMake installed and in PATH you can remove them from this list. Note
    # that it is important that you do **not** use the 'python2', 'cmake' and 'ninja'
    # packages from the 'msys2' subsystem. The build has historically been known
    # to fail with these packages.
    pacman -S git \
                make \
                diffutils \
                tar \
                mingw-w64-x86_64-python \
                mingw-w64-x86_64-cmake \
                mingw-w64-x86_64-gcc \
                mingw-w64-x86_64-ninja
    
  4. Navigate to Rust's source code (or clone it), then build it:

    ./x.py build && ./x.py install
    

MSVC

MSVC builds of Rust additionally require an installation of Visual Studio 2017 (or later) so rustc can use its linker. The simplest way is to get the Visual Studio, check the “C++ build tools” and “Windows 10 SDK” workload.

(If you're installing cmake yourself, be careful that “C++ CMake tools for Windows” doesn't get included under “Individual components”.)

With these dependencies installed, you can build the compiler in a cmd.exe shell with:

python x.py build

Currently, building Rust only works with some known versions of Visual Studio. If you have a more recent version installed and the build system doesn't understand, you may need to force rustbuild to use an older version. This can be done by manually calling the appropriate vcvars file before running the bootstrap.

CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
python x.py build

Specifying an ABI

Each specific ABI can also be used from either environment (for example, using the GNU ABI in PowerShell) by using an explicit build triple. The available Windows build triples are:

  • GNU ABI (using GCC)
    • i686-pc-windows-gnu
    • x86_64-pc-windows-gnu
  • The MSVC ABI
    • i686-pc-windows-msvc
    • x86_64-pc-windows-msvc

The build triple can be specified by either specifying --build=<triple> when invoking x.py commands, or by copying the config.toml file (as described in Installing From Source), and modifying the build option under the [build] section.

Configure and Make

While it's not the recommended build system, this project also provides a configure script and makefile (the latter of which just invokes x.py).

./configure
make && sudo make install

When using the configure script, the generated config.mk file may override the config.toml file. To go back to the config.toml file, delete the generated config.mk file.

Building Documentation

If youd like to build the documentation, its almost the same:

./x.py doc

The generated documentation will appear under doc in the build directory for the ABI used. I.e., if the ABI was x86_64-pc-windows-msvc, the directory will be build\x86_64-pc-windows-msvc\doc.

Notes

Since the Rust compiler is written in Rust, it must be built by a precompiled "snapshot" version of itself (made in an earlier stage of development). As such, source builds require a connection to the Internet, to fetch snapshots, and an OS that can execute the available snapshot binaries.

Snapshot binaries are currently built and tested on several platforms:

Platform / Architecture x86 x86_64
Windows (7, 8, 10, ...)
Linux (kernel 2.6.32, glibc 2.11 or later)
macOS (10.7 Lion or later) (*)

(*): Apple dropped support for running 32-bit binaries starting from macOS 10.15 and iOS 11. Due to this decision from Apple, the targets are no longer useful to our users. Please read our blog post for more info.

You may find that other platforms work, but these are our officially supported build environments that are most likely to work.

Getting Help

The Rust community congregates in a few places:

Contributing

If you are interested in contributing to the Rust project, please take a look at the Getting Started guide in the rustc-dev-guide.

License

Rust is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.

Trademark

The Rust Foundation owns and protects the Rust and Cargo trademarks and logos (the “Rust Trademarks”).

If you want to use these names or brands, please read the media guide.

Third-party logos may be subject to third-party copyrights and trademarks. See Licenses for details.