This is an archive of the discontinued LLVM Phabricator instance.

[mlir][irdl] Support variadicity check in operations
ClosedPublic

Authored by math-fehr on Jun 29 2023, 5:34 AM.

Details

Summary

This patch adds support for loading IRDL operations that are
using optional or variadics operands and results.

If an operation declares more than one optional/variadic operand
or result, then it requires the segment sizes in the attribute
dictionary, and otherwise it is computed using the number of
operands or results.

Currently, a variadic operand or result definiton expects all
operands and results in that definition to have the same type.
This restriction will be removed in a following patch.

Depends on D153983

Diff Detail

Event Timeline

math-fehr created this revision.Jun 29 2023, 5:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2023, 5:34 AM
math-fehr requested review of this revision.Jun 29 2023, 5:34 AM
Mogball added inline comments.Jun 29 2023, 9:58 AM
mlir/lib/Dialect/IRDL/IRDLLoading.cpp
56
70

please add braces here

77
97

same here

99

please use static_cast

113
125

please spell out this auto

133

please use static_cast

249

please use prefix, and above as well

304

drop trivial braces

325

here too

Thank you for adding me to the review
I honestly learned something new about MLIR while reviewing this :)
I left my suggestions below

mlir/lib/Dialect/IRDL/IRDLLoading.cpp
77–79

llvm::enumerate with more than one argument actually zips them also

96–98

Yet, std::reduce is C++17 feature and may not be supported by the minimum version compilers required by MLIR
In that case, it may be replaced with std::accumulate

P.S. For some reason there are no llvm::accumulate or llvm::reduce in the STLExtras.h

131–138

It seems like this whole check could be moved outside of a loop because it is not dependent on the variadicity variable

133

Probably should be renamed to something like nonSingleSegmentSize

137

Is variadicities.size() exact enough value to represent, for example, number of minimal expected operands?
Say we have: irdl.operands(single %0, optional %1, single %2). As far as I understand, it is a possible situation in this code because we have only one non-single operand.
So, minimal number of expected operands is actually 2 but the error would say 3

Sorry to be picky about this, this might not be so important

140
286–304

Optional suggestion

If you do not want to use the "algorithm", there is other way to make code more nice:

SmallVector<Variadicity> operandVariadicity(operandsOp->getVariadicity().size());
for (auto& [attr, variadicity] : llvm::zip(operandsOp->getVariadicity(), operandVariadicity)) {
  variadicity = attr.cast<VariadicityAttr>().getValue();
}
323

Same here for the loop

math-fehr updated this revision to Diff 540438.Jul 14 2023, 8:51 AM
math-fehr marked 18 inline comments as done.

Address comments, and add missing tests

mlir/lib/Dialect/IRDL/IRDLLoading.cpp
96–98

Oh yes thanks! MLIR is now using C++17, so hopefully std::accumulate sholud work!

131–138

Right, I rewrote this part a bit to make it more readable!

137

Oh yes you are totally right, this is not picky at all and the error message is important!
Here it should be variadicities.size() - 1, since you have n-1 operands for the single ones, and then the optional/variadic operand can be empty.

286–304

Hmm, here I would actually prefer the original version, since it is only a simple for loop. I feel that adding transform or the zip with assignment adds a bit too much of complexity, what do you think? @Mogball, what would be the most LLVM-like way of writing that?

unterumarmung accepted this revision.Jul 17 2023, 10:53 AM

LGTM

mlir/lib/Dialect/IRDL/IRDLLoading.cpp
286–304

I'd prefer calling reserve for the given size or passing the size value to the constructor.
Just in terms of efficiency.

This revision is now accepted and ready to land.Jul 17 2023, 10:53 AM
Mogball accepted this revision.Aug 16 2023, 9:32 AM
math-fehr updated this revision to Diff 551765.Aug 19 2023, 8:39 AM

Update to main

math-fehr updated this revision to Diff 551834.Aug 20 2023, 7:38 AM

Update to latest main

This revision was landed with ongoing or failed builds.Aug 20 2023, 7:39 AM
This revision was automatically updated to reflect the committed changes.
math-fehr reopened this revision.Aug 20 2023, 8:03 AM

Reopen, as buildbot failed because of std::reduce

This revision is now accepted and ready to land.Aug 20 2023, 8:03 AM
math-fehr updated this revision to Diff 551840.Aug 20 2023, 8:12 AM

Remove uses of std::reduce

This revision was landed with ongoing or failed builds.Aug 20 2023, 8:23 AM
This revision was automatically updated to reflect the committed changes.