This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add a new fold API using Generic Adaptors
ClosedPublic

Authored by zero9178 on Jan 3 2023, 5:39 AM.

Details

Summary

This is part of the RFC for a better fold API: https://discourse.llvm.org/t/rfc-a-better-fold-api-using-more-generic-adaptors/67374

This patch implements the required foldHook changes and the TableGen machinery for generating fold method signatures using FoldAdaptor for ops, based on the value of useFoldAPI. It may be one of 2 values, with convenient named constants to create a quasi enum. The new fold method will then be generated if kEmitFoldAdaptorFolder is used.

Since the new FoldAdaptor approach is strictly better than the old signature, part of this patch updates the documentation and all example to encourage use of the new fold signature.
Included are also tests exercising the new API, ensuring proper construction of the FoldAdaptor and proper generation by TableGen.

Diff Detail

Event Timeline

zero9178 created this revision.Jan 3 2023, 5:39 AM
zero9178 requested review of this revision.Jan 3 2023, 5:39 AM
Mogball added inline comments.Jan 3 2023, 8:30 AM
mlir/docs/Canonicalization.md
141

I'd honestly prefer this to be a dialect setting instead of a per-operation setting.

mlir/docs/Tutorials/Toy/Ch-7.md
464

please add an argument name. Omitting argument names is unusual in the MLIR coding style

mlir/include/mlir/IR/OpDefinition.h
1715

can you add a has_fold_adaptor_v? This one is used a few times

mlir/test/IR/test-fold-adaptor.mlir
5

it doesn't seem correct to me to have a test under IR/ contain arith ops

17

please add a newline

zero9178 updated this revision to Diff 486378.Jan 4 2023, 1:37 PM

Address review comments:

  • address style issues
  • use test dialect ops
  • change the fold API to be selectable per dialect instead of per Op
zero9178 marked 5 inline comments as done.Jan 4 2023, 1:41 PM
zero9178 added inline comments.
mlir/docs/Canonicalization.md
141

Thank you for telling me. I tend to agree and tentatively updated the patch, making it a dialect setting. I have also added a reply to the RFC thread to document the new behaviour and how it changes migrations.

mlir/include/mlir/IR/OpDefinition.h
1715

I have gone ahead and have done this for all the detect_* typedefs since there is no real benefit of having the struct version around in existing code. I have also changed the naming to be more consistent with how it is used in LLVM, that is _t suffix for the detector using declaration, and the _v boolean you wanted for the actual boolean.

zero9178 updated this revision to Diff 486507.Jan 5 2023, 2:39 AM
zero9178 marked 2 inline comments as done.

clang-format

You know, if you make kEmitRawAttributesFolder emit a warning, users will migrate a lot faster 😂

Though that means you might at least be on the hook for updating all upstream dialects.

zero9178 edited the summary of this revision. (Show Details)Jan 6 2023, 11:43 AM

You know, if you make kEmitRawAttributesFolder emit a warning, users will migrate a lot faster 😂

Though that means you might at least be on the hook for updating all upstream dialects.

If I was poetic enough to formulate the warning message in a way that users would be moved with enthusiams to switch to the new API I would!
I fear I'd instead receive compliants about being spammed by 500 warnings messages however 😅

Mogball accepted this revision.Jan 8 2023, 1:06 PM

You know, if you make kEmitRawAttributesFolder emit a warning, users will migrate a lot faster 😂

Though that means you might at least be on the hook for updating all upstream dialects.

If I was poetic enough to formulate the warning message in a way that users would be moved with enthusiams to switch to the new API I would!
I fear I'd instead receive compliants about being spammed by 500 warnings messages however 😅

Just hold your ground.

LGTM but please give others a change to take a look at this.

mlir/include/mlir/IR/OpDefinition.h
1816

please add braces here

mlir/test/mlir-tblgen/has-fold-invalid-values.td
16

can you emit a more detailed error message? Like "invalid value for dialect useFoldAPI"? This is the kind of error message that typically results in me grepping the code base for where it came from

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
2339–2344

please add braces here. If one branch has braces, they all should.

This revision is now accepted and ready to land.Jan 8 2023, 1:06 PM
zero9178 updated this revision to Diff 487226.Jan 8 2023, 1:41 PM

Address review comments

zero9178 marked 3 inline comments as done.Jan 8 2023, 1:42 PM

You know, if you make kEmitRawAttributesFolder emit a warning, users will migrate a lot faster 😂

Though that means you might at least be on the hook for updating all upstream dialects.

If I was poetic enough to formulate the warning message in a way that users would be moved with enthusiams to switch to the new API I would!
I fear I'd instead receive compliants about being spammed by 500 warnings messages however 😅

I would implement it such that the warning is emitted at most once per tablegen invocation, that should make it not too noisy I think?
(in-tree dialects need to be clean first of course)

You know, if you make kEmitRawAttributesFolder emit a warning, users will migrate a lot faster 😂

Though that means you might at least be on the hook for updating all upstream dialects.

If I was poetic enough to formulate the warning message in a way that users would be moved with enthusiams to switch to the new API I would!
I fear I'd instead receive compliants about being spammed by 500 warnings messages however 😅

I would implement it such that the warning is emitted at most once per tablegen invocation, that should make it not too noisy I think?
(in-tree dialects need to be clean first of course)

Taking upstream MLIR as benchmark, this leads to "just" 42 warnings, which is definitely less noisey (and the repo probably contains more dialects than most downstream projects).
I was previously under the impression that warnings emitted by TableGen were rather unpopular, especially since there is no way to silence them, except fixing the issue.
I suppose that is what we want in this case though and migrating should be easy, even if it has to be done manually.

That said, what time do you think this warning should be introduced? Immediately right after in-tree is clean, but before changing the default to the new one?

There's a build option to disable TableGen warnings. That should work, of course.

This revision was automatically updated to reflect the committed changes.
rriddle added inline comments.Jan 12 2023, 10:52 AM
mlir/examples/toy/Ch7/mlir/ToyCombine.cpp
34

Do we still need the dyn_cast_or_null here?

zero9178 marked an inline comment as done.Jan 12 2023, 11:04 AM
zero9178 added inline comments.
mlir/examples/toy/Ch7/mlir/ToyCombine.cpp
34

Yes. The FoldAdaptor (which is really just a GenericAdaptor<ArrayRef<Attribute>>) currently doesn't treat Attributes any special. So all the getters currently simply return the element type of the input range, which is Attribute in this case. (just like the Adaptors always return Value as element type of ValueRange).

Changing this so that the return types of FoldAdaptor returns the concrete Attribute type is something I'd like to look into in the future as the next improvement of fold, but this will require a few more changes, the most important one in my mind being that we'll probably need some field in TableGen Type, which tells us what the corresponding Attribute type is. We could then generate a template specialization using these as return types instead.