Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicated block in control flow uplifting #750

Merged
merged 1 commit into from
Jun 23, 2022

Conversation

Y-Nak
Copy link
Member

@Y-Nak Y-Nak commented Jun 23, 2022

What was wrong?

close #749
The problem is that the same basic block is included in serialized StructuralInst and then translated into yul more than once in the below CFG condition.

  1. CFG doesn't have a canonical exit block
  2. Two paths can be merged at a certain block

e.g.,
graphviz
In this CFG, bb2 is a merge block of two paths, bb0 -> bb1 -> bb4 -> bb2 and bb0 -> bb2.
But the compiler can't find merge block properly, and it lead to ICE as a result.

How was it fixed?

I wrongly assumed that if merge block should be the post dominator of if header.
To find if merge block correctly, we should follow the below algorithm.

1. Findif merge block candidate

When we see the branch instruction in a non-loop header, we can find a merge block candidate by applying the following criteria to two destination blocks of the branch instruction respectively.

block `cand` can be a candidate of a `merge` block iff
  1. `cand` is a dominance frontier of `block`.
  2. `cand` is NOT  a dominator of `block`.
  3. `cand` is NOT a "merge" block of a parent `if`.
  4. `cand` is NOT a "loop_exit" block of parent `loop`.

2. Find merge block.

We can find the merge block according to the result of 1..

a. When both destinations have the same candidate, then the candidate is the merge block.

b. When only one of the destinations has a candidate, then the candidate is the merge block iff the candidate is identical to another destination that doesn't have a candidate.

c. When none of the destinations has a candidate, then a `block` is the merge block iff 
  1. the `block` is the post dominator of the `branch` instruction's block
  2. the `block` is inside the same loop if the `branch` instruction's block is in a loop.

To-Do

  • OPTIONAL: Update Spec if applicable

  • Add entry to the release notes (may forgo for trivial changes)

  • Clean up commit history

@Y-Nak Y-Nak force-pushed the fix-invalid-inst-order branch 2 times, most recently from a2392aa to c9a0a4a Compare June 23, 2022 00:17
Copy link
Collaborator

@cburgdorf cburgdorf left a comment

Choose a reason for hiding this comment

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

Looks good!

@Y-Nak Y-Nak merged commit 3b81a0f into ethereum:master Jun 23, 2022
@Y-Nak Y-Nak deleted the fix-invalid-inst-order branch June 23, 2022 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Certain code with if statement leads to an ICE
2 participants