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

core: preorder walk of blocks in nested operations #3367

Merged
merged 5 commits into from
Feb 10, 2025

Conversation

gabrielrodcanal
Copy link
Contributor

Preorder walk of blocks in nested operations. This corresponds to the behaviour of WalkOrder::Preorder in MLIR. The test in the PR was developed in parallel with an analysis pass in MLIR to ensure equivalent behaviour in xDSL. For reproducibility, the relevant method in MLIR is:

void PreorderBlocks::print(raw_ostream &os) const {
  operation->walk<WalkOrder::PreOrder>([&](Block *block) {
    os << "--- Block: \n";
    block->print(os);
    os << "\n";
  });
}

Copy link

codecov bot commented Oct 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.28%. Comparing base (2b2f51e) to head (6becd71).
Report is 14 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3367      +/-   ##
==========================================
+ Coverage   91.27%   91.28%   +0.01%     
==========================================
  Files         461      462       +1     
  Lines       57656    57745      +89     
  Branches     5571     5578       +7     
==========================================
+ Hits        52623    52714      +91     
+ Misses       3609     3608       -1     
+ Partials     1424     1423       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -967,6 +967,13 @@ def walk(
if region_first:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps rename the post_order.py file to traversals.py and move it there?

Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to keep this logic in ir.py

xdsl/ir/core.py Outdated
@@ -967,6 +967,13 @@ def walk(
if region_first:
yield self

def walk_blocks_preorder(self) -> Iterator[Block]:
for region in self.regions:
for block in region.blocks:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not what I would have expected a preorder traversal of blocks to do. Does llvm simply iterate over them in the order they appear?

Copy link
Member

Choose a reason for hiding this comment

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

Now that I think about it, it would probably be worth adding a walk method on Block that mirrors the API of the Operation walk, and to change this to forward to that one. I also prefer our walk_regions_first to Preorder as it's more declarative, maybe for blocks it could be walk_child_blocks_first? pre-post order doesn't make sense in the absence of left and right children

Copy link
Collaborator

Choose a reason for hiding this comment

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

I more meant that I would have expected a "traversal" to explore blocks via a depth first search of successors from each block, rather than just return each block in order

Copy link
Member

Choose a reason for hiding this comment

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

Ah no that's not what I would expect TBH. @gabrielrodcanal, if you don't feel like implementing reversed iteration and post-order then I think it would still be worth adding walk on Block, and walk_blocks on Operation, and we can add the optional parameters later without API breaking.

@gabrielrodcanal gabrielrodcanal added the core xDSL core (ir, textual format, ...) label Oct 31, 2024
@superlopuh
Copy link
Member

Any updates on this? Will you have some time to iterate on it this week?

@superlopuh
Copy link
Member

Taking over this PR, hope that's ok @gabrielrodcanal

@compor
Copy link
Collaborator

compor commented Feb 7, 2025

Taking over this PR, hope that's ok @gabrielrodcanal

I'm interested in having a look if you don't mind; I'll relinquish if it's too much for my time ATM

Copy link
Collaborator

@math-fehr math-fehr left a comment

Choose a reason for hiding this comment

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

👍

@superlopuh superlopuh merged commit df9eec3 into main Feb 10, 2025
16 checks passed
@superlopuh superlopuh deleted the gabriel/walk_preorder_blocks branch February 10, 2025 15:39
oluwatimilehin pushed a commit to oluwatimilehin/xdsl that referenced this pull request Feb 13, 2025
Preorder walk of blocks in nested operations. This corresponds to the
behaviour of WalkOrder::Preorder in MLIR. The test in the PR was
developed in parallel with an analysis pass in MLIR to ensure equivalent
behaviour in xDSL. For reproducibility, the relevant method in MLIR is:
```
void PreorderBlocks::print(raw_ostream &os) const {
  operation->walk<WalkOrder::PreOrder>([&](Block *block) {
    os << "--- Block: \n";
    block->print(os);
    os << "\n";
  });
}
```

---------

Co-authored-by: Sasha Lopoukhine <superlopuh@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core xDSL core (ir, textual format, ...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants