Skip to content

Reimplement ConstProp using DataflowConstProp#110719

Closed
cjgillot wants to merge 4 commits intorust-lang:masterfrom
cjgillot:single-const-prop
Closed

Reimplement ConstProp using DataflowConstProp#110719
cjgillot wants to merge 4 commits intorust-lang:masterfrom
cjgillot:single-const-prop

Conversation

@cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Apr 23, 2023

This PR attempts to remove the current ConstProp optimization, and replace it by an implementation based on DataflowConstProp.

The current ConstProp twists the MIR interpreter to do things it is not designed to do, including manipulating generic types. Meanwhile, @jachris has implemented a new DataflowConstProp pass that only uses a few chosen methods for arithmetic.

The main change concerns how propagatable places and operations are tracked. In the current ConstProp, all places are tracked and all operations executed, and we removed wrong or non-propagatable results. This new implementation does the opposite : place tracking is opt-in, and we only propagate supported operations. I believe this approach to be less risky from a soundness point of view.

As a consequence, this new implementation is more restricted. The main limitation is that we stop propagating inside a basic block, and only propagate SSA locals. This makes the implementation simpler, and is more consistent with this "opt-in" logic.

Some operations are added inside this PR (transmutes, algebraic simplifications, Len operations). The handling of statics and non-scalar consts are left to a follow-up PR.

This PR does not remove the const-prop lints (arithmetic overflow and unconditional panic), as I need to design a way to pass more span information around.

There has been quite a few perf runs below. Marking as ready for review, since I removed the massive regressions on diesel/keccak/serde. Kept as waiting-on-author until I bless CI.

Regressed optimizations:

  • array indexing;
  • non-scalar constants;
  • assignment of immutable statics;
  • creating rvalues with non-scalar types;
  • integer-to-pointer transmutes.

Based on #110728, #110732, #110820

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 23, 2023
@cjgillot

This comment was marked as outdated.

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 23, 2023
@bors

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@cjgillot

This comment was marked as outdated.

@rust-timer

This comment has been minimized.

@bors

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as outdated.

@rust-timer

This comment has been minimized.

@rust-timer

This comment was marked as outdated.

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 23, 2023
@cjgillot cjgillot force-pushed the single-const-prop branch from 4837cfb to 79c1168 Compare April 23, 2023 18:17
@cjgillot

This comment was marked as outdated.

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 23, 2023
@bors

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as outdated.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2023
@cjgillot cjgillot force-pushed the single-const-prop branch from 79c1168 to 39b5ce5 Compare April 23, 2023 19:00
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 23, 2023
@cjgillot
Copy link
Contributor Author

I've put this PR on hold, because I'm not quite sure of the direction I'm going with it. The DataflowConstProp is tailored to propagating scalars, and that makes it more difficult to propagate non scalar information.
I'm leaving this PR as waiting on author until I get to agree with myself.

@cjgillot cjgillot added the A-mir-opt Area: MIR optimizations label Jul 8, 2023
@cjgillot cjgillot marked this pull request as draft July 11, 2023 18:48
@bors
Copy link
Collaborator

bors commented Jul 14, 2023

🔒 Merge conflict

This pull request and the master branch diverged in a way that cannot be automatically merged. Please rebase on top of the latest master branch, and let the reviewer approve again.

How do I rebase?

Assuming self is your fork and upstream is this repository, you can resolve the conflict following these steps:

  1. git checkout single-const-prop (switch to your branch)
  2. git fetch upstream master (retrieve the latest master)
  3. git rebase upstream/master -p (rebase on top of it)
  4. Follow the on-screen instruction to resolve conflicts (check git status if you got lost).
  5. git push self single-const-prop --force-with-lease (update this PR)

You may also read Git Rebasing to Resolve Conflicts by Drew Blessing for a short tutorial.

Please avoid the "Resolve conflicts" button on GitHub. It uses git merge instead of git rebase which makes the PR commit history more difficult to read.

Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Cargo.lock conflict is handled during merge and rebase. This is normal, and you should still perform step 5 to update this PR.

Error message
CONFLICT (modify/delete): tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/pre-codegen/optimizes_into_variable.main.SimplifyLocals-final.after.64bit.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/pre-codegen/optimizes_into_variable.main.SimplifyLocals-final.after.64bit.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/pre-codegen/optimizes_into_variable.main.SimplifyLocals-final.after.32bit.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/pre-codegen/optimizes_into_variable.main.SimplifyLocals-final.after.32bit.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/pre-codegen/optimizes_into_variable.main.PreCodegen.after.64bit.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/pre-codegen/optimizes_into_variable.main.PreCodegen.after.64bit.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/pre-codegen/optimizes_into_variable.main.PreCodegen.after.32bit.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/pre-codegen/optimizes_into_variable.main.PreCodegen.after.32bit.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.64bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.64bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.32bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.32bit.diff left in tree.
Auto-merging tests/mir-opt/pre-codegen/intrinsics.f_u64.PreCodegen.after.mir
CONFLICT (content): Merge conflict in tests/mir-opt/pre-codegen/intrinsics.f_u64.PreCodegen.after.mir
CONFLICT (modify/delete): tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.PreCodegen.after.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.PreCodegen.after.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.Inline.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.Inline.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.PreCodegen.after.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.PreCodegen.after.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff left in tree.
Auto-merging tests/mir-opt/const_prop/transmute.unreachable_ref.ConstProp.64bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.unreachable_ref.ConstProp.64bit.diff
Auto-merging tests/mir-opt/const_prop/transmute.unreachable_ref.ConstProp.32bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.unreachable_ref.ConstProp.32bit.diff
Auto-merging tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.64bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.64bit.diff
Auto-merging tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.32bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.32bit.diff
Auto-merging tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.64bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.64bit.diff
Auto-merging tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.32bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.32bit.diff
Auto-merging tests/mir-opt/const_prop/transmute.less_as_i8.ConstProp.64bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.less_as_i8.ConstProp.64bit.diff
Auto-merging tests/mir-opt/const_prop/transmute.less_as_i8.ConstProp.32bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/transmute.less_as_i8.ConstProp.32bit.diff
CONFLICT (modify/delete): tests/mir-opt/const_prop/slice_len.main.ConstProp.64bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/slice_len.main.ConstProp.64bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/slice_len.main.ConstProp.32bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/slice_len.main.ConstProp.32bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/scalar_literal_propagation.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/scalar_literal_propagation.main.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/return_place.add.PreCodegen.before.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/return_place.add.PreCodegen.before.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/return_place.add.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/return_place.add.ConstProp.diff left in tree.
Auto-merging tests/mir-opt/const_prop/repeat.rs
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/repeat.rs
CONFLICT (modify/delete): tests/mir-opt/const_prop/repeat.main.ConstProp.64bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/repeat.main.ConstProp.64bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/repeat.main.ConstProp.32bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/repeat.main.ConstProp.32bit.diff left in tree.
Auto-merging tests/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff
CONFLICT (modify/delete): tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/mutable_variable_unprop_assign.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/mutable_variable_unprop_assign.main.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/mutable_variable_aggregate_partial_read.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/mutable_variable_aggregate_partial_read.main.ConstProp.diff left in tree.
Auto-merging tests/mir-opt/const_prop/mutable_variable_aggregate.main.ConstProp.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/mutable_variable_aggregate.main.ConstProp.diff
Auto-merging tests/mir-opt/const_prop/mutable_variable.main.ConstProp.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/mutable_variable.main.ConstProp.diff
Auto-merging tests/mir-opt/const_prop/large_array_index.rs
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/large_array_index.rs
CONFLICT (modify/delete): tests/mir-opt/const_prop/large_array_index.main.ConstProp.64bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/large_array_index.main.ConstProp.64bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/large_array_index.main.ConstProp.32bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/large_array_index.main.ConstProp.32bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/issue_67019.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/issue_67019.main.ConstProp.diff left in tree.
Auto-merging tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff
CONFLICT (modify/delete): tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/indirect.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/indirect.main.ConstProp.diff left in tree.
Auto-merging tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff
Auto-merging tests/mir-opt/const_prop/discriminant.main.ConstProp.32bit.diff
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/discriminant.main.ConstProp.32bit.diff
CONFLICT (modify/delete): tests/mir-opt/const_prop/checked_add.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/checked_add.main.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/boxes.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/boxes.main.ConstProp.diff left in tree.
Auto-merging tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.rs
CONFLICT (content): Merge conflict in tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.rs
CONFLICT (modify/delete): tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.64bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.64bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/bad_op_div_by_zero.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/bad_op_div_by_zero.main.ConstProp.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/array_index.main.ConstProp.64bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/array_index.main.ConstProp.64bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/array_index.main.ConstProp.32bit.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/array_index.main.ConstProp.32bit.diff left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/aggregate.main.PreCodegen.after.mir deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/aggregate.main.PreCodegen.after.mir left in tree.
CONFLICT (modify/delete): tests/mir-opt/const_prop/aggregate.main.ConstProp.diff deleted in HEAD and modified in heads/homu-tmp. Version heads/homu-tmp of tests/mir-opt/const_prop/aggregate.main.ConstProp.diff left in tree.
Auto-merging compiler/rustc_mir_transform/src/dataflow_const_prop.rs
CONFLICT (content): Merge conflict in compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Auto-merging compiler/rustc_mir_transform/src/const_prop.rs
CONFLICT (content): Merge conflict in compiler/rustc_mir_transform/src/const_prop.rs
Auto-merging compiler/rustc_mir_dataflow/src/value_analysis.rs
Auto-merging compiler/rustc_middle/src/mir/mod.rs
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your merge.renamelimit variable to at least 1719 and retry the command.
Automatic merge failed; fix conflicts and then commit the result.

@cjgillot cjgillot force-pushed the single-const-prop branch from d9ee84e to c58cc1e Compare July 19, 2023 12:40
@rust-log-analyzer

This comment has been minimized.

@cjgillot cjgillot force-pushed the single-const-prop branch from c58cc1e to f960de6 Compare July 19, 2023 14:21
@bors
Copy link
Collaborator

bors commented Jul 20, 2023

☔ The latest upstream changes (presumably #113758) made this pull request unmergeable. Please resolve the merge conflicts.

@cjgillot cjgillot force-pushed the single-const-prop branch from f960de6 to 542052d Compare July 21, 2023 16:34
@bors
Copy link
Collaborator

bors commented Aug 30, 2023

☔ The latest upstream changes (presumably #114483) made this pull request unmergeable. Please resolve the merge conflicts.

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 8, 2023
Improvements to dataflow const-prop

Partially cherry-picked from rust-lang#110719

r? `@oli-obk`
cc `@jachris`
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 8, 2023
Improvements to dataflow const-prop

Partially cherry-picked from rust-lang#110719

r? `@oli-obk`
cc `@jachris`
@rust-log-analyzer

This comment has been minimized.

@@ -1,11 +1,10 @@
Function name: <issue_84561::Foo as core::cmp::PartialEq>::eq
Raw bytes (14): 0x[01, 01, 00, 02, 01, 04, 0a, 00, 13, 00, 00, 0a, 00, 13]
Raw bytes (9): 0x[01, 01, 00, 01, 01, 04, 0a, 00, 13]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Zalathar This commit blesses the coverage map. However, I have no idea how to check that it is still correct. What do you recommend?

Copy link
Member

Choose a reason for hiding this comment

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

As a rule of thumb, any PR that doesn't touch coverage-specific code should feel free to just re-bless these.

(They're unfortunately quite sensitive to changes in MIR lowering/optimization, in a way that tends to result in different but equivalent mappings.)

Copy link
Member

Choose a reason for hiding this comment

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

In particular, as long as the run-coverage tests still pass, then any changes to the mappings are probably just noise.

(Those tests only run if the profiler runtime is enabled in config.toml, so they don't run by default in dev builds or on PRs, but they do run in the full CI before merging.)

Copy link
Member

Choose a reason for hiding this comment

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

Looking at the actual diff, it seems like some MIR statements that were previously being optimized out are no longer being optimized out. Nothing to worry about from a coverage perspective.

github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Sep 12, 2023
Improvements to dataflow const-prop

Partially cherry-picked from rust-lang/rust#110719

r? `@oli-obk`
cc `@jachris`
@bors
Copy link
Collaborator

bors commented Sep 12, 2023

☔ The latest upstream changes (presumably #115705) made this pull request unmergeable. Please resolve the merge conflicts.

@cjgillot
Copy link
Contributor Author

I'd like to close this PR and pursue another direction for ConstProp.

#109597 creates a general framework for value analyses. I believe that implementing constant-folding in this pass can achieve the same level as what this PR proposes, while being more flexible:

  • we'd only use the interpreter arithmetic API, like dataflow const-prop does, and allow removing ConstPropNonsense;
  • that pass would only propagate whole values, simplifying state storage, and leaving the per-place to the dataflow version;
  • that pass would leverage arithmetic identities over unknown values, which const-prop cannot do;
  • that pass may be able to avoid duplicating constant allocations.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 14, 2023

That makes sense

@RalfJung
Copy link
Member

that pass would leverage arithmetic identities over unknown values, which const-prop cannot do;

const-prop can simplify some things here, right, like x*0 -> 0 or x+0 -> x?

@cjgillot
Copy link
Contributor Author

Yes, but it cannot do x - x = 0, which #116012 could (in #111344).

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 23, 2023
Implement constant propagation on top of MIR SSA analysis

This implements the idea I proposed in rust-lang#110719 (comment)

Based on rust-lang#109597

The value numbering "GVN" pass formulates each rvalue that appears in MIR with an abstract form (the `Value` enum), and assigns an integer `VnIndex` to each. This abstract form can be used to deduplicate values, reusing an earlier local that holds the same value instead of recomputing. This part is proposed in rust-lang#109597.

From this abstract representation, we can perform more involved simplifications, for example in rust-lang#111344.

With the abstract representation `Value`, we can also attempt to evaluate each to a constant using the interpreter. This builds a `VnIndex -> OpTy` map. From this map, we can opportunistically replace an operand or a rvalue with a constant if their value has an associated `OpTy`.

The most relevant commit is [Evaluated computed values to constants.](rust-lang@2767c49)"

r? `@oli-obk`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-mir-opt Area: MIR optimizations perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.