Truly independent web browser
Flap: Give closure blocks the dominators of their references
Andreas Kling2f8cb82f542e
diff --git a/Libraries/LibJS/Flap/src/ssa/analysis.rs b/Libraries/LibJS/Flap/src/ssa/analysis.rsindex cf6b0cb1588..5ef5c0f1506 100644--- a/Libraries/LibJS/Flap/src/ssa/analysis.rs+++ b/Libraries/LibJS/Flap/src/ssa/analysis.rs@@ -19,9 +19,32 @@ pub(crate) struct ControlFlowGraph {impl ControlFlowGraph {pub(crate) fn compute(function: &Function) -> Self {+ Self::compute_with(function, false)+ }++ /// Like `compute()`, with an edge from every block that takes a reference+ /// to a block to that block. A closure block only runs through indirect+ /// jumps to references to it, after the block that took the reference+ /// ran, so these edges give the closure's code a dominator tree.+ pub(crate) fn compute_with_block_references(function: &Function) -> Self {+ Self::compute_with(function, true)+ }++ fn compute_with(function: &Function, with_block_references: bool) -> Self {let mut predecessors = vec![Vec::new(); function.blocks.len()];let mut successors = vec![Vec::new(); function.blocks.len()];for (block_index, block) in function.blocks.iter().enumerate() {+ if with_block_references {+ for instruction in &block.instructions {+ let Operation::BlockReference(target) = function.instructions[instruction.0].operation else {+ continue;+ };+ if target.0 < function.blocks.len() {+ successors[block_index].push(target);+ predecessors[target.0].push(BlockId(block_index));+ }+ }+ }for edge in block.terminator.iter().flat_map(|terminator| terminator.successors()) {if edge.block.0 >= function.blocks.len() {continue;diff --git a/Libraries/LibJS/Flap/src/ssa/inline.rs b/Libraries/LibJS/Flap/src/ssa/inline.rsindex a5d7d732fb5..1994410e62d 100644--- a/Libraries/LibJS/Flap/src/ssa/inline.rs+++ b/Libraries/LibJS/Flap/src/ssa/inline.rs@@ -963,6 +963,34 @@ handler Checked(dst: out Operand, src: in Operand) {assert!(cfg.is_reachable(BlockId(fallback)));}+ #[test]+ fn inlines_calls_in_closures_reached_through_indirect_jumps() {+ let (mut handler, callees) = lower(+ r#"+inline fn note() {+}++inline fn choose(condition: u32, yes: Label, no: Label) {+ guard condition != 0 else no;+ goto yes;+}++handler Choose(condition: u32, target: BytecodeOffset) {+ let yes = || {+ note();+ jump(target);+ };+ let no = || @cold {+ exit;+ };+ choose(condition, yes, no);+}+"#,+ );+ // NB: Inlining note() splits the closure, whose rest uses the target the closure captured.+ assert_eq!(inline_calls(&mut handler, &callees).unwrap(), 2);+ }+#[test]fn resolves_inlined_indirect_jumps_to_closure_blocks() {let (mut handler, callees) = lower(diff --git a/Libraries/LibJS/Flap/src/ssa/mod.rs b/Libraries/LibJS/Flap/src/ssa/mod.rsindex e6d7bbb5bd0..ec22b749e31 100644--- a/Libraries/LibJS/Flap/src/ssa/mod.rs+++ b/Libraries/LibJS/Flap/src/ssa/mod.rs@@ -982,7 +982,10 @@ impl Function {fn validate_uses(&self) -> Result<(), String> {let cfg = analysis::ControlFlowGraph::compute(self);- let dominators = analysis::DominatorTree::compute(self, &cfg);+ // NB: Closure blocks are dominated by the blocks that take references+ // to them, which no terminator edge says.+ let dominators =+ analysis::DominatorTree::compute(self, &analysis::ControlFlowGraph::compute_with_block_references(self));let instruction_layout = analysis::InstructionLayout::compute(self)?;let guards = analysis::GuardExits::compute(self, &cfg);let reachable = analysis::reachable_blocks(self, &cfg);