dillon1000/react

Commit

[rust] MergeConsecutiveBlocks and block helpers

Ports `MergeConsecutiveBlocks` to Rust. This was a tricky one: as we iterate 
through the blocks _if_ the block ends up being merged with its predecessor we 
need to consume it and modify its predecessor block (ie, mutating two things 
from the same data structure - shared mutation!). But if we _don't_ need to 
merge, then we need to not drop the current block. Ie, we sort of need to 
conditionally take ownership of the current block during iteration and put it 
back. 

I added a `BlockRewriter` helper type for this which has a helper to iterate 
safely. It calls the iterator lambda, moving blocks one at a time into the 
lambda. The lambda returns either `Keep(block)` to give the block back and keep 
it or `Remove` to tell the rewriter to drop the block. Thanks to making the 
Blocks data structure hold `Option<Box<BasicBlock>>` items, "moving" the block 
actually just means nulling out the option and conceptually giving ownership of 
the pointer to the callback - the data itself never moves. 

This involved creating a custom `Blocks` wrapper type, which i had been putting 
off doing. This cleans up a bunch of other logic around traversing blocks.
Browse files
Changed paths14 files
First-parent comparison
M compiler/forget/Cargo.lock ModifiedM compiler/forget/crates/forget_build_hir/src/builder.rs ModifiedM compiler/forget/crates/forget_fixtures/tests/fixtures_test.rs ModifiedM compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@constant-propagation-constant-if-condition.js.snap ModifiedM compiler/forget/crates/forget_fixtures/tests/snapshots/[email protected] ModifiedM compiler/forget/crates/forget_hir/src/function.rs ModifiedM compiler/forget/crates/forget_hir/src/initialize.rs ModifiedM compiler/forget/crates/forget_hir/src/lib.rs ModifiedA compiler/forget/crates/forget_hir/src/merge_consecutive_blocks.rs AddedM compiler/forget/crates/forget_hir/src/print.rs ModifiedM compiler/forget/crates/forget_optimization/Cargo.toml ModifiedM compiler/forget/crates/forget_optimization/src/constant_propagation.rs ModifiedM compiler/forget/crates/forget_ssa/src/eliminate_redundant_phis.rs ModifiedM compiler/forget/crates/forget_ssa/src/enter.rs Modified
Patch

Files changed

Rendering syntax-highlighted changes…