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

transformations: Persist func arg names as arg_attr #3395

Merged
merged 9 commits into from
Nov 4, 2024

Conversation

n-io
Copy link
Collaborator

@n-io n-io commented Nov 4, 2024

Persisting function arg names to arg attributes as suggested here.

Note, upstream mlir will say op arguments may only have dialect attributes, requiring arg attribute names to be prefixed with a dialect.

@n-io n-io added the transformations Changes or adds a transformatio label Nov 4, 2024
@n-io n-io requested review from superlopuh and dk949 November 4, 2024 14:56
@n-io n-io self-assigned this Nov 4, 2024
@n-io n-io requested a review from alexarice November 4, 2024 14:56
Copy link

codecov bot commented Nov 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.16%. Comparing base (87e1a46) to head (e5c1a58).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3395      +/-   ##
==========================================
+ Coverage   90.13%   90.16%   +0.02%     
==========================================
  Files         452      453       +1     
  Lines       57157    57220      +63     
  Branches     5498     5502       +4     
==========================================
+ Hits        51519    51590      +71     
+ Misses       4180     4175       -5     
+ Partials     1458     1455       -3     

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

Comment on lines 15 to 28
def match_and_rewrite(self, op: func.FuncOp, rewriter: PatternRewriter, /):
arg_attrs = list(op.arg_attrs or (len(op.args) * [DictionaryAttr({})]))
new_arg_attrs: list[DictionaryAttr] = []
for arg, arg_attr in zip(op.args, arg_attrs):
if arg.name_hint:
d_attr = DictionaryAttr(
{"llvm.name": StringAttr(arg.name_hint), **arg_attr.data}
)
else:
d_attr = arg_attr
new_arg_attrs.append(d_attr)

op.arg_attrs = ArrayAttr(new_arg_attrs)
rewriter.has_done_action = True
Copy link
Member

@superlopuh superlopuh Nov 4, 2024

Choose a reason for hiding this comment

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

  • It feels like the first thing to do would be to check whether any of the args aren't anonymous, and to bail out early if that's the case. Could you add a test case for this, also?
  • could you also check whether the old attrs are the same as the new attrs, and avoid has_done_action if they are the same?
  • nit: it would be nice to refactor this to a tuple comprehension instead of appending to a list, just for style reasons

Copy link
Collaborator Author

@n-io n-io Nov 4, 2024

Choose a reason for hiding this comment

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

I had it as a tuple-comp list-comp, and after refactoring it am glad that is encouraged 😃

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 a fan :)

@n-io n-io requested a review from superlopuh November 4, 2024 16:12
class ArgNamesToArgAttrsPass(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, op: func.FuncOp, rewriter: PatternRewriter, /):
arg_attrs = list(op.arg_attrs or (len(op.args) * [DictionaryAttr({})]))
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
arg_attrs = list(op.arg_attrs or (len(op.args) * [DictionaryAttr({})]))
arg_attrs = op.arg_attrs.data if op.arg_attrs is not None else or (DictionaryAttr({}),) * len(op.args)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done as suggested (minus the 'else or')

Co-authored-by: Sasha Lopoukhine <superlopuh@gmail.com>
Comment on lines 15 to 16
def match_and_rewrite(self, op: func.FuncOp, rewriter: PatternRewriter, /):
arg_attrs = list(op.arg_attrs or (len(op.args) * [DictionaryAttr({})]))
Copy link
Member

Choose a reason for hiding this comment

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

Would it not be worth doing something like this?

Suggested change
def match_and_rewrite(self, op: func.FuncOp, rewriter: PatternRewriter, /):
arg_attrs = list(op.arg_attrs or (len(op.args) * [DictionaryAttr({})]))
def match_and_rewrite(self, op: func.FuncOp, rewriter: PatternRewriter, /):
if not any(arg.name_hint for arg in op.args):
return
arg_attrs = list(op.arg_attrs or (len(op.args) * [DictionaryAttr({})]))

This way there's no intermediate thing created if there's nothing to do

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At the cost of an extra pass over the args? I actually didn't think that's what we wanted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done as suggested.

@@ -257,6 +257,7 @@ def callback(x: str):
with ImplicitBuilder(expected_module.body):
function = func.FuncOp("hello", ((index,), (index,)))
with ImplicitBuilder(function.body) as (n,):
n.name_hint = "n"
Copy link
Collaborator Author

@n-io n-io Nov 4, 2024

Choose a reason for hiding this comment

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

This will otherwise show up as a difference in get_available_passes below, as the module specified as string has the name hint.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this fixes a kind of bug in the test, which is that the name hints are not the same in the text and this IR

@n-io n-io requested a review from superlopuh November 4, 2024 17:03
@n-io n-io merged commit 9cbeec8 into main Nov 4, 2024
15 checks passed
@n-io n-io deleted the nicolai/persist-func-arg-names branch November 4, 2024 17:12
EdmundGoodman pushed a commit to EdmundGoodman/xdsl that referenced this pull request Dec 6, 2024
Persisting function arg names to arg attributes as suggested
[here](https://discourse.llvm.org/t/mlir-capi-how-to-set-function-argument-names-arg0/3892/2).

Note, upstream mlir will say `op arguments may only have dialect
attributes`, requiring arg attribute names to be prefixed with a
dialect.

---------

Co-authored-by: n-io <n-io@users.noreply.github.com>
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
transformations Changes or adds a transformatio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants