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

feat(ssa refactor): dominator tree #1253

Closed
wants to merge 20 commits into from
Closed

feat(ssa refactor): dominator tree #1253

wants to merge 20 commits into from

Conversation

joss-aztec
Copy link
Contributor

@joss-aztec joss-aztec commented Apr 28, 2023

Related issue(s)

Resolves #1116

Description

Summary of changes

  • Add PostOrder for calculating the post-order of a function reachable blocks
  • Adds DominatorTree for querying one block's dominance over another
  • Make finished Ssa's function collection accessible so that the FunctionBuilder can be used in unit tests

Dependency additions / changes

Test additions / changes

Unit test for both of the above

Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt with default settings.
  • I have linked this PR to the issue(s) that it resolves.
  • I have reviewed the changes on GitHub, line by line.
  • I have ensured all changes are covered in the description.

Documentation needs

  • This PR requires documentation updates when merged.

Additional context

Copy link
Contributor

@kevaundray kevaundray left a comment

Choose a reason for hiding this comment

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

Some preliminary comments

Last,
}

// Calculates the post-order of the
Copy link
Contributor

Choose a reason for hiding this comment

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

Noting the unfinished sentence here (for when it comes out of draft)

@@ -0,0 +1,136 @@
use std::collections::HashSet;
Copy link
Contributor

Choose a reason for hiding this comment

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

can you put a paragraph at the top of this using //! ... to explain what post-order is and why it is needed?

@@ -0,0 +1,443 @@
use std::{cmp::Ordering, collections::HashMap};
Copy link
Contributor

Choose a reason for hiding this comment

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

same here for dominator tree -- ie why it is needed and other useful information that would help someone understand the codebase. See at the top of some of Cranelifts files for inspiration on how this is done

@joss-aztec joss-aztec marked this pull request as ready for review April 28, 2023 20:21
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

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

Looks pretty good so far. The tests looks good as well.

crates/noirc_evaluator/src/ssa_refactor/ir/dom.rs Outdated Show resolved Hide resolved
DomNode { reverse_post_order_idx: 0, immediate_dominator: None },
);
for (i, &block_id) in entry_free_post_order.iter().rev().enumerate() {
// Indices have been displaced by 1 by to the removal of the entry node
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo here, "by 1 by to the removal"


use crate::ssa_refactor::ir::{basic_block::BasicBlockId, function::Function};

/// DFT stack state marker for computing the cfg post-order.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we spell out the acronym here?

// Stack successors for visiting. Because items are taken from the top of the
// stack, we push the item that's due for a visit first to the top.
for successor_id in
func.dfg[block_id].successors().collect::<Vec<_>>().into_iter().rev()
Copy link
Contributor

Choose a reason for hiding this comment

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

successors should already be an iterator so we should be able to do:

func.dfg[block_id].successors().rev()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to add DoubleEndedIterator to successors return type.

Comment on lines +235 to +237
let cfg = ControlFlowGraph::with_function(&func);
let post_order = PostOrder::with_function(&func);
let dom_tree = DominatorTree::with_cfg_and_post_order(&cfg, &post_order);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this is a common operation. Are we expected to keep around the cfg and post_order blocks without the dominator tree? If not we can have a helper function to create the first two and pass them into the third instead of creating all three separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought I'd come across a use case of post order independent of dom trees - but possibly I imagined it. What cranelift does is to have the dom tree compute and take ownership of the post order. It then has an accessor for when the two are used in conjunction. Would that be preferred?

Either way I'll add a helper for the cfg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not my imagination - cranelift's dead code elimination pass iterates over post-order without any need for dom trees.

immediate_dominator: Option<BasicBlockId>,
}

impl DomNode {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're spelling out DominatorTree I think we should spell out DominatorNode or DominatorTreeNode as well.

joss-aztec and others added 12 commits May 2, 2023 11:31
- Comment tweaks
- rm unneeded collect iter into vec
- clippy changes
- DominatorTree::with_function helper
- rename DomNode DominatorTreeNode
* Implement first-class functions

* Update crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs

Co-authored-by: kevaundray <[email protected]>

* Implement intrinsics

* Fix no return & duplicate main

* bad git. remove duplicated functions

* Remove Option<Function> in builder

* Undo debug printing in driver

* Fix loading from mutable parameters

* Grammar

* Fix storing to mutable arrays

* Fix unused variable

* Fix array loading

* Change terminology

---------

Co-authored-by: kevaundray <[email protected]>
* chore(ci): Utilize new workflow to build binaries

* Update release.yml

---------

Co-authored-by: Tom French <[email protected]>
* chore(noir): Release 0.5.0

* chore: Update lockfile
#1226)

* chore: replace `aztec_backend` with `acvm-backend-barretenberg`

* feat: update to ACVM 0.10.0

* chore: move `ComputeMerkleRoot` to same match arm as `HashToField128Security`

* chore: bump backend commit

* feat: update stdlib to use new merkle black box function

* fix: bump commit of barretenberg to match acvm-backend-barretenberg

* feat: update `merkle_insert` to use new `compute_merkle_root` function

* chore: update to use ACVM 0.10.3

* chore: bump backend commit
* chore(noir): Release 0.5.1

* chore: Update lockfile
… pass (#1256)

* Add remaining doc comments

* Update crates/noirc_evaluator/src/ssa_refactor/ir/cfg.rs

Co-authored-by: kevaundray <[email protected]>

* Update crates/noirc_evaluator/src/ssa_refactor/ir/cfg.rs

Co-authored-by: kevaundray <[email protected]>

* Address PR feedback

---------

Co-authored-by: kevaundray <[email protected]>
* chore(noir): constrain expr; -> assert(expr);

* chore(noir): replace remaining `constrain` with `assert(expr)`

---------

Co-authored-by: Tom French <[email protected]>
@TomAFrench
Copy link
Member

Whoops, I merged master into this branch and fixed the issue in f8a8933. My version of this branch turned out to be a little behind so I git pull --rebase'd and that seems to have split apart the merge commit.

@joss-aztec
Copy link
Contributor Author

The file diff has exploded. I'm gonna close and cherry-pick into a new branch/PR.

@joss-aztec joss-aztec closed this May 2, 2023
@joss-aztec
Copy link
Contributor Author

Replacement PR: #1278

@TomAFrench TomAFrench deleted the joss/domtree branch July 18, 2023 05:29
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.

Add dominator Tree
5 participants