Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove orphaned blocks from cfg to improve
simplify_cfg
pass. (…
…#6198) # Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* Inspired by #6184, I noticed that after simplifying a constant jmpif we wouldn't inline the block following the inlined block into the first block, as an example consider the SSA ``` After Dead Instruction Elimination: brillig fn main f0 { b0(v0: Field, v1: Field): constrain v0 == Field 1 constrain v1 == Field 0 jmpif u1 0 then: b2, else: b1 b2(): v8 = add v0, Field 1 jmp b3(v0, v8) b3(v2: Field, v3: Field): v9 = add v0, Field 1 constrain v2 == v9 constrain v3 == v0 constrain v0 == Field 1 constrain v1 == Field 0 return b1(): v7 = add v0, Field 1 jmp b3(v7, v0) } ``` Here we can see that we never enter `b2`, instead going `b0 -> b1 -> b3`. However when the simplify_cfg pass looks at the predecessors of `b3` it sees both `b1` **and `b2`** despite `b2` not being reachable anymore. It then doesn't inline `b3` into its sole predecessor. On master we'll simplify the CFG for the SSA to give the program: ``` After Simplifying: brillig fn main f0 { b0(v0: Field, v1: Field): constrain v0 == Field 1 constrain v1 == Field 0 v6 = add v0, Field 1 jmp b1(v6, v0) b1(v2: Field, v3: Field): v7 = add v0, Field 1 constrain v2 == v7 constrain v3 == v0 constrain v0 == Field 1 constrain v1 == Field 0 return } ``` This PR checks the previous successors of any block which has it's CFG recomputed to check if they've been left orphaned, if so we remove it from the CFG entirely. We now instead fully simplify the CFG to give the program: ``` After Simplifying: brillig fn main f0 { b0(v0: Field, v1: Field): constrain v0 == Field 1 constrain v1 == Field 0 v4 = add v0, Field 1 v5 = add v0, Field 1 constrain v4 == v5 constrain v0 == Field 1 constrain v1 == Field 0 return } ``` ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information