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[venom]: improve unused variable removal pass #4055

Merged
merged 10 commits into from
May 27, 2024

Conversation

harkal
Copy link
Collaborator

@harkal harkal commented May 26, 2024

What I did

Implemented a better variable elimination pass. This new implementation is efficient and manages to remove all unused variables in one run.

Additionally, this PR adds some extra utility functions to OrderedSet()

How I did it

How to verify it

Commit message

This commit improves the variable elimination pass by efficiently removing 
all instructions that produce unused output. This new algorithm removes all
unused variables in one run.

Additionally, it adds the utility functions `pop()` and `addmany()` to the
`OrderedSet()` class.

The `DFTAnalysis` is also augmented with a method to remove uses:
`remove_use(self, op: IRVariable, inst: IRInstruction)`

Description for the changelog

Cute Animal Picture

                 __
                / _)--< 
       _.----._/ /          
     /          /  Dinosnek          
 __/ (  |  (   |             
/__.-'|_|--|__|             

@harkal harkal marked this pull request as ready for review May 26, 2024 19:57
i -= 2
for operand in inst.get_inputs():
new_uses = self.dfg.remove_use(operand, inst)
for use in new_uses:
Copy link
Member

Choose a reason for hiding this comment

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

can use self.work_list.addmany(new_uses)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Even I can’t keep up with all the cool utility functions I write 😅

def remove_use(self, op: IRVariable, inst: IRInstruction) -> list[IRInstruction]:
uses = self._dfg_inputs.get(op, [])
uses.remove(inst)
return uses
Copy link
Member

Choose a reason for hiding this comment

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

nit: i'd prefer an API which does one thing at a time, so remove_use() just removes the item, and then the caller needs another call to .get_uses()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It’s a pattern I use regularly, but I have no strong preference about doing it in two steps

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

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

nice algorithm :) looks like it's faster too! left a couple nits, otherwise looks good to merge

Comment on lines 21 to 22
for _, inst in self.dfg.outputs.items():
work_list.add(inst)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for _, inst in self.dfg.outputs.items():
work_list.add(inst)
uses = self.dfg.outputs.values()
work_list.addmany(uses)

Copy link
Member

Choose a reason for hiding this comment

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

i'm ok with either way tbh

@charles-cooper charles-cooper enabled auto-merge (squash) May 27, 2024 14:03
@charles-cooper charles-cooper changed the title feat[venom]: improve unused variable removal feat[venom]: improve unused variable removal pass May 27, 2024
@charles-cooper charles-cooper merged commit 608feda into vyperlang:master May 27, 2024
157 checks passed
@harkal harkal deleted the feat/remove_unused_improvement branch May 27, 2024 15:42
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.

2 participants