Remove `analysis` arg from `ResultsVisitor` methods
It's not needed: if a visitor needs access to the analysis it can just hold a reference itself. Details in individual commits.
r? @cjgillot
It's mostly identical to `MaybeLiveLocals`, and we can delegate most of
its operations to `MaybeLiveLocals`.
Note that there was a tiny difference between
`MaybeLiveLocals::apply_call_return_effect` and
`MaybeTransitiveLiveLocals::apply_call_return_effect`: the former uses
`state.kill(local)`, the latter used `state.remove(local)`. The two are
equivalent so the difference didn't matter, but it does demonstrate the
dangers of the code duplication.
Split `apply_primary_terminator_effect`
It currently does two distinct things, and it's a bit nicer it they are separated. Details in individual commits.
r? @cjgillot
This method currently does two things: it applies the effect, and also
computes the edges. However:
- Three of the four call sites don't use the edges.
- Most analyses just return `terminator.edges()` unconditionally.
This commit separates the edge computation into a new method,
`get_terminator_edges()`. It defaults to `terminator.edges()`, which
means that most analyses don't need to define it. And now edges are only
obtained when they are needed (in `Forward::apply_effects_in_block`).
It only has one call site; might as well just use `visit_results`.
Also, clarify that the blocks passed to `visit_results` must be
reachable; this is something I didn't realize, probably because the name
`visit_reachable_results` made `visit_results` sound like its blocks
*don't* need to be reachable.
This commit fixes the error message regression in the previous commit by
recomputing flow information at error-reporting time.
`is_local_ever_initialized` is replaced in two ways.
- In `check_access_permissions`: by the new `first_reaching_init`
function which picks the first init that can reach the error location
along a path that doesn't cross `StorageDead(local)`.
- In `add_used_mut`: by a simple `contains` test.
Currently it tracks a bit for every `Init`, but it only uses the tracked
data for locals. This means it is tracking data for projections that is
unused.
This commit shrinks the domain to only track data for `Local`s, going
from `MixedBitSet<InitIndex>` to `DenseBitSet<Local>`.
This does regress the error messages in one test:
liveness-assign-imm-local-notes.rs. The next commit will fix that.
The commit also removes some `debug` statements which probably haven't
seen use in a long time. They can be re-added easily if anyone needs
them in the future.
perf: Avoid a heap allocation per basic block in MoveData's location maps
`MoveData` holds two `LocationMap`s, and each was an `IndexVec<BasicBlock, Vec<T>>` that heap-allocated one `Vec` per basic block. Building the move data for a body allocated a small vector for every block, twice over. Store the entries in one flat buffer indexed by a per-block start offset, so each map is two allocations regardless of block count.
Improve CFG traversal
This PR implements a new CFG traversal algorithm in `iterate_to_fixpoint` that drastically speeds up a pathological case in `cranelift-codegen`.
r? @cjgillot
`iterate_to_fixpoint` uses a FIFO worklist. It is seeded in a good order
(reverse postorder for forward analyses, postorder for backward
analyses). But when back edges are present, re-dirtied blocks are pushed
to the back of the queue. Successor blocks of these re-dirtied blocks are
then analyzed even though we know the input states may change later on.
This commit switches to a new min-rank algorithm that always processes
the earliest dirty block in dataflow order.
On most code this makes little difference, but cranelift-codegen has one
enormous function with over 18,000 basic blocks. The old algorithm
required 1.5 million calls to `apply_effects_in_block` to reach a
fixpoint for `EverInitializedPlaces`. The new algorithm requires 90,000.
This results in a 17% reduction in instruction counts for a `Check Full`
build.
Note: the new algorithm also avoids doing anything for unreachable
blocks in backward analyses. (The old algorithm computed states but
never used them meaningfully.)
It currently contains a `MaybeBorrowedLocals` cursor. The cursor is
within a `RefCell` because cursor traversal requires mutation. The
cursor is used in the `MaybeRequiresStorage::check_for_move` operation.
Instead of using this cursor within `MaybeRequiresStorage` it's possible
to do a pre-traversal of `MaybeBorrowedLocals` to extract the necessary
information and then give that (immutably) to `MaybeRequiresStorage`.
This commit makes that change. The extracted information is in the new
`KillableLocals` type, which is computed by the `KillableLocalsVisitor`
type within `MaybeRequiresStorage::new`. `MaybeRequiresStorage` no
longer needs lifetimes. `MoveVisitor` is no longer needed. And the
complicated `locals_live_across_suspend_points` gets a little simpler.
- By improving various comments.
- By renaming some fields. This gives a clear split between
`move_outs`/`move_out_loc_map`/`move_out_path_map` and
`inits`/`init_loc_map`/`init_path_map`.
It has a single call site. The commit removes the assertions because they
necessary any more due to the assertions and checks at the call site.
This then removes the need for `index_precedes`.
It's only used by `StateDiffCollector`, and it's just a complicated way
to get the entry state, which can instead be done directly (avoiding the
creation of a `bottom_value` which was immediately overwritten).
This commit adds `Analysis::apply_effect`, which takes an `EffectIndex`
and calls the appropriate `Analysis::apply_*` method.
Once that is in place, it is possible to use it with `next_index` to
write a simple `apply_effects_in_range` method that can be shared
between `Forward` and `Backward`. The end result is much easier to
understand.
`ResultsVisitor` has `visit_block_start` which is called on entry to a
a block in a forwards analysis and on exit from a block in a backwards
analysis. And vice versa for `visit_block_end`.
The only visitor that impls these methods is `StateDiffCollector`, which
does something in `visit_block_start` for a forwards analysis and the
same thing in `visit_block_end` for a backwards analysis. In other
words, `StateDiffCollector` wants to always do the same thing on entry
to a block and never do anything on exit from a block.
This commit replaces `visit_block_{start,end}` with `visit_block_entry`,
which is always called on entry to a block. This is simpler overall.
First steps of late-bound turbofishing (place FnDef behind a dummy binder)
r? oli-obk
This PR is part of rust-lang/rust#156581. This should be functionally identical to prior behavior, with the added fact that FnDef now places its arguments behind a binder.
most changes boil down to
- replacing `args` from `FnDef` with `args.no_bound_vars().unwrap()`
- replacing `Ty::new_fn_def(/* ... */, args)` with `Ty::new_fn_def(/* ... */, ty::Binder::dummy(args))`
Currently, when adding liveness points to region values
in the `RegionValues` struct, the locations of the points
are checked for ranges. This is unnecessarily cautious because
they always are in range by construction.
This adds documentation (including debug assertions) to make this
clearer and removes the checks, which should have a strictly
positive impact on performance.
[style] rustfmt `match`es with comments in or-patterns
Using https://github.com/rust-lang/rustfmt/pull/6893, I reformatted the whole codebase. The result is that `match`es that *should have* been formatted under normal circumstances but are getting skipped now got their expected format. These match expressions were being entirely skipped because they contain or-patterns with comments in between patterns, causing rustfmt to bail out entirely. The or-patterns with comments themselves remain untouched, but now the match arm bodies and other patterns without comments do get formatted under that PR.
Because the fix in rustfmt isn't landed yet, I reworked some of the or-patterns with comments so that formatting doesn't regress. Tried doing this only in larger blocks that are more likely to regress in the meantime.
(Introduced and) removed a bunch of stray backticks \` likely left after an editor autoclosed the intended closing \`, resulting in <code>\`name\`\`</code> in comments.
DestinationPropagation: compute liveness as ranges instead of traveling bitsets
The current implementation of `save_as_liveness` is very slow, and consists in inserting a traveling bitset in an interval set.
As the `MaybeLiveLocals` has a gen-kill property, we can leverage it to make it faster. "Gen" is creating a new interval. "Kill" is ending this interval, ripe to save in the interval set.