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

Transformation: Loop Invariant Code Motion #3027

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

RRavikiran66
Copy link
Collaborator

@RRavikiran66 RRavikiran66 commented Aug 13, 2024

This pull request initiates the development of loop invariant code motion (LICM) optimizations. I have implemented helper functions like canBeHoisted , move_out_of_the_region, 'isMemoryeffectfree'. I am not sure about speculatable so for now it contains same body as 'isMemoryeffectfree' I think it is working for scf.for, and I have added few tests cases to verify as well.

@RRavikiran66 RRavikiran66 self-assigned this Aug 13, 2024
@RRavikiran66 RRavikiran66 added the transformations Changes or adds a transformatio label Aug 13, 2024
@RRavikiran66 RRavikiran66 changed the title Transformation: Loop Invariant Code Motion Transformation: Loop Invariant Code Motion(WIP) Aug 13, 2024
Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

This is great! Let's merge can_be_hoisted as a first step, in a separate PR. I would recommend renaming it, and changing the condition argument to target_region, as well as adding back the check for side effects.

@superlopuh
Copy link
Member

Oh actually the documentation lies about the side effects check, that's done outside of the helper in CPP, we might as well follow their logic for now.

@RRavikiran66
Copy link
Collaborator Author

Oh actually the documentation lies about the side effects check, that's done outside of the helper in CPP, we might as well follow their logic for now.

I have included your changes in the new commit but I forgot rename the canBeHoisted I will do it in the next commit. I have implemented helper functions like move_out_of_the_region, 'isMemoryeffectfree'. I am not sure about speculatable so for now it contains same body as 'isMemoryeffectfree' I think it is working for scf.for, and I have added few tests cases to verify as well. I think code can be optimized, I will take that up in the next commit as well.

@superlopuh
Copy link
Member

Can you please fix all the linting errors? Running make install-precommit and make precommit should help, it's quite hard to review otherwise. Also, if it's ready for review, could you please remove the WIP label and hit the Ready for review button in GitHub?

@RRavikiran66 RRavikiran66 changed the title Transformation: Loop Invariant Code Motion(WIP) Transformation: Loop Invariant Code Motion Aug 19, 2024
Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

This is great! I think the PR is ready to review, fantastic to see it working. Could you please mark it as such in the GitHub interface?

Comment on lines +15 to +16
# is_side_effect_free,
# only_has_effect,
Copy link
Member

Choose a reason for hiding this comment

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

commented code

Copy link
Member

Choose a reason for hiding this comment

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

Please let the person who commented resolve the conversation themselves in the PR. Why did you resolve my comment? We generally try to not have any commented code in the xDSL code base, so I would recommend deleting the above two lines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I resolved them, Cause I thought running precommit will remove unnecessary imports which I realized is not true for commented code, I didn't look for it either. I will be cautious of this next time

Copy link

codecov bot commented Aug 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.86%. Comparing base (1f000a1) to head (f840154).
Report is 41 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3027      +/-   ##
==========================================
+ Coverage   89.83%   89.86%   +0.03%     
==========================================
  Files         411      417       +6     
  Lines       51698    52546     +848     
  Branches     8036     8144     +108     
==========================================
+ Hits        46442    47220     +778     
- Misses       3970     4011      +41     
- Partials     1286     1315      +29     

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

@RRavikiran66
Copy link
Collaborator Author

I will create three PR for rest of the Helper Functions

@RRavikiran66 RRavikiran66 marked this pull request as ready for review August 20, 2024 12:44
Comment on lines +33 to +49
def canBeHoisted(op: Operation, region_target: Region) -> bool | None:
# Do not move terminators.
if op.has_trait(IsTerminator):
return False

# Walk the nested operations and check that all used values are either
# defined outside of the loop or in a nested region, but not at the level of
# the loop body.
for child in op.walk():
for operand in child.operands:
for own in operand.owner.walk():
if not isinstance(own, scf.For):
if op.is_ancestor(operand.owner):
continue
if region_target.is_ancestor(own):
return False
return True
Copy link
Member

Choose a reason for hiding this comment

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

Could you please first open a PR just for this helper? This is the key piece of logic in this PR, and it would be great to have a separately tested function that we could discuss. There are a number of changes that I would recommend making to this PR, but it feels like these should be as part of a focused discussion.

Copy link
Member

Choose a reason for hiding this comment

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

It may seem like more PRs will mean that merging it takes more time but it will almost certainly take less time overall to merge the changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't understand few things:
1)If I create a helper function, How can I test the helper function without calling in a class?
2)The test setup should also be a part of this PR right?

Copy link
Member

Choose a reason for hiding this comment

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

I would recommend adding a pytest test for the function specifically, it's saved me a few times when writing tricky code, as it's easier to pinpoint the error than with the filecheck. In this case, I would recommend constructing a loop nest manually, using a builder, and checking that all the branches are covered, in a file like tests/transforms/test_loop_invariant_code_motion.py.

@superlopuh
Copy link
Member

I had a chat with Ravikiran, and he's currently working towards a paper with a deadline on the 22 of November, will wait until then to update this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
transformations Changes or adds a transformatio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants