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

normalize mir::Constant differently from ty::Const in preparation for valtrees #83207

Merged
merged 11 commits into from Apr 2, 2021

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Mar 16, 2021

Valtrees are unable to represent many kind of constant values (this is on purpose). For constants that are used at runtime, we do not need a valtree representation and can thus use a different form of evaluation. In order to make this explicit and less fragile, I added a fold_constant method to TypeFolder and implemented it for normalization. Normalization can now, when it wants to eagerly evaluate a constant, normalize mir::Constant directly into a mir::ConstantKind::Val instead of relying on the ty::Const evaluation.

In the future we can get rid of the ty::Const in there entirely and add our own Unevaluated variant to mir::ConstantKind. This would allow us to remove the promoted field from ty::ConstKind::Unevaluated, as promoteds can never occur in the type system.

cc @rust-lang/wg-const-eval

r? @lcnr

@oli-obk oli-obk added the S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. label Mar 16, 2021
@rust-highfive
Copy link
Collaborator

Some changes occured to rustc_codegen_cranelift

cc @bjorn3

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 16, 2021
@rust-log-analyzer

This comment has been minimized.

compiler/rustc_mir/src/interpret/operand.rs Outdated Show resolved Hide resolved
compiler/rustc_mir/src/interpret/operand.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/normalize_erasing_regions.rs Outdated Show resolved Hide resolved
compiler/rustc_trait_selection/src/traits/project.rs Outdated Show resolved Hide resolved
compiler/rustc_trait_selection/src/traits/project.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

I don't really get what this is about.^^ Normalization is not an area of the compiler that I ever had much contact with.

What I would have expected to happen is that normalization only occurs on constants that arise in types. But then why is mir::Constant relevant at all? I thought that type is for constants that occur in code... as in, it only exists as the type for wgat is stored in an Operand::Constant.

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 17, 2021

What I would have expected to happen is that normalization only occurs on constants that arise in types. But then why is mir::Constant relevant at all? I thought that type is for constants that occur in code... as in, it only exists as the type for wgat is stored in an Operand::Constant.

I did try not eagerly evaluating mir::Constant during normalization, but that causes cycle errors for const_evaluatable_checked tests, so I opted to normalize and revisit in the future.

Normalization is performed on MIR data structures when monomorphizing said data structures. Since constants that occur in types have MIR bodies for getting evaluated, there is some link between the two.

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 17, 2021

All review comments have been addressed

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

I did try not eagerly evaluating mir::Constant during normalization, but that causes cycle errors for const_evaluatable_checked tests, so I opted to normalize and revisit in the future.

Normalization is performed on MIR data structures when monomorphizing said data structures. Since constants that occur in types have MIR bodies for getting evaluated, there is some link between the two.

Okay... no idea what this entails but I'll trust it makes sense. :)

@oli-obk oli-obk removed the S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. label Mar 19, 2021
@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 19, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 19, 2021
@bors
Copy link
Contributor

bors commented Mar 19, 2021

⌛ Trying commit 0c73c0f99a34797048663ed97f23bef7b789ca1f with merge 91aa315e65b19839dca8da5a8cca9ff37c5614b3...

@bors
Copy link
Contributor

bors commented Mar 19, 2021

☀️ Try build successful - checks-actions
Build commit: 91aa315e65b19839dca8da5a8cca9ff37c5614b3 (91aa315e65b19839dca8da5a8cca9ff37c5614b3)

@rust-timer
Copy link
Collaborator

Queued 91aa315e65b19839dca8da5a8cca9ff37c5614b3 with parent b97fd3e, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (91aa315e65b19839dca8da5a8cca9ff37c5614b3): comparison url.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 19, 2021
@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 20, 2021

The stress tests regressed. Looking at the details, we have 40% fewer eval_to_const_value_raw query invocations and 80% fewer normalize_generic_arg_after_erasing_regions invocations, but a 4% increase in time taken for eval_to_allocation_raw.

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 22, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 22, 2021
@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (7409b4198347a26b96fdc2e2066c937a2af64972): comparison url.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 1, 2021
@bors
Copy link
Contributor

bors commented Apr 1, 2021

☀️ Try build successful - checks-actions
Build commit: e47c8d1017e71dbf47d9d88f3f9e8c008248c9e7 (e47c8d1017e71dbf47d9d88f3f9e8c008248c9e7)

@oli-obk
Copy link
Contributor Author

oli-obk commented Apr 1, 2021

Local perf run (with the latest try build, not the same as the perf run above):

--------------------------------------------------------------------------------
           Ir 
--------------------------------------------------------------------------------
 -818,318,722  PROGRAM TOTALS

--------------------------------------------------------------------------------
            Ir  file:function
--------------------------------------------------------------------------------
-3,373,631,810  ???:rustc_mir::interpret::eval_context::InterpCx<M>::layout_of_local
 2,503,347,014  ???:rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_place
 1,221,597,116  ???:rustc_middle::ty::normalize_erasing_regions::<impl rustc_middle::ty::context::TyCtxt>::normalize_erasing_regions
-1,156,828,134  ???:rustc_middle::ty::instance::Instance::subst_mir_and_normalize_erasing_regions
-1,149,246,226  ???:rustc_middle::ty::normalize_erasing_regions::<impl rustc_middle::ty::context::TyCtxt>::subst_and_normalize_erasing_regions
  -399,081,153  ???:<rustc_middle::ty::normalize_erasing_regions::NormalizeAfterErasingRegionsFolder as rustc_middle::ty::fold::TypeFolder>::fold_const
   351,271,730  ???:hashbrown::map::RawEntryBuilder<K,V,S,A>::from_key_hashed_nocheck
   317,144,070  ???:rustc_mir::interpret::operand::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_operand
   309,330,160  ???:core::cmp::impls::<impl core::cmp::PartialEq<&B> for &A>::eq
   220,211,940  ???:<rustc_middle::ty::consts::kind::ConstKind as core::hash::Hash>::hash
   218,956,888  ???:rustc_mir::interpret::operand::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::access_local
    76,021,528  ???:rustc_mir::interpret::eval_context::InterpCx<M>::subst_from_current_frame_and_normalize_erasing_regions
    68,472,035  ???:rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_rvalue_into_place
   -26,214,318  ???:<core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold
    15,728,640  ???:rustc_middle::ty::<impl rustc_data_structures::tagged_ptr::Tag for rustc_middle::traits::Reveal>::from_usize
    -8,912,866  ???:<rustc_middle::ty::layout::LayoutCx<rustc_middle::ty::query::TyCtxtAt> as rustc_target::abi::LayoutOf>::layout_of
    -2,621,721  ???:rustc_mir::interpret::eval_context::InterpCx<M>::load_mir
    -2,621,445  ???:rustc_mir::interpret::terminator::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_fn_call
    -1,311,308  ???:rustc_middle::ty::instance::Instance::resolve_opt_const_arg

@oli-obk
Copy link
Contributor Author

oli-obk commented Apr 1, 2021

@bors r=lcnr (changes to previous approval are: some useless commits removed, some inline attributes added).

@bors
Copy link
Contributor

bors commented Apr 1, 2021

📌 Commit c6676db has been approved by lcnr

@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. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 1, 2021
@bors
Copy link
Contributor

bors commented Apr 1, 2021

⌛ Testing commit c6676db with merge 18b838f728bf7578c7a5821d4e563ac758f60f5b...

@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@oli-obk
Copy link
Contributor Author

oli-obk commented Apr 1, 2021

@bors retry

@bors
Copy link
Contributor

bors commented Apr 1, 2021

⌛ Testing commit c6676db with merge 98107010e61a1adef6724de613e2daf33ca41c9e...

@bors
Copy link
Contributor

bors commented Apr 1, 2021

💥 Test timed out

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 1, 2021
@oli-obk
Copy link
Contributor Author

oli-obk commented Apr 2, 2021

@bors retry

@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 Apr 2, 2021
@bors
Copy link
Contributor

bors commented Apr 2, 2021

⌛ Testing commit c6676db with merge 0978a9e...

@bors
Copy link
Contributor

bors commented Apr 2, 2021

☀️ Test successful - checks-actions
Approved by: lcnr
Pushing 0978a9e to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 2, 2021
@bors bors merged commit 0978a9e into rust-lang:master Apr 2, 2021
@rustbot rustbot added this to the 1.53.0 milestone Apr 2, 2021
@oli-obk oli-obk deleted the valtree2 branch April 2, 2021 13:08
@lcnr lcnr added the A-const-generics Area: const generics (parameters and arguments) label Dec 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet