This is an archive of the discontinued LLVM Phabricator instance.

[mlir] FlatAffineConstraint parsing for unit tests
ClosedPublic

Authored by Dinistro on Nov 5 2021, 6:20 AM.

Details

Summary

This patch adds functionality to parse FlatAffineConstraints from a
StringRef with the intention to be used for unit tests. This should
make the construction of FlatAffineConstraints easier for testing
purposes.

The patch contains an example usage of the functionality in a unit test that
uses FlatAffineConstraints.

Diff Detail

Event Timeline

Dinistro created this revision.Nov 5 2021, 6:20 AM
Dinistro requested review of this revision.Nov 5 2021, 6:20 AM

The usage of the parser in mlir/unittests/Analysis/AffineStructuresTest.cpp serves as an example. If this change to the parser and its usage for FAC unit tests is approved, we can replace the remaining creations of FACs with calls to parseFAC.

bondhugula requested changes to this revision.Nov 5 2021, 9:40 AM
bondhugula added inline comments.
mlir/include/mlir/Parser.h
16

This is going to be a layering issue. The parser library can't depend on the Affine Analysis library, which is where FlatAffineConstraints resides. We'll have to define the parser for FlatAffineConstraints elsewhere. The MLIR parser is meant for just parsing IR AFAIK, and in any case, it can't depend on such analysis structures.

This revision now requires changes to proceed.Nov 5 2021, 9:40 AM

This is useful to have but please define this in the same library as FlatAffineConstraints.

bondhugula added inline comments.Nov 5 2021, 9:46 AM
mlir/unittests/Analysis/AffineStructuresTest.cpp
102

Missing doc comment.

mlir/unittests/Analysis/CMakeLists.txt
7

Sorted order here.

Dinistro updated this revision to Diff 385265.Nov 6 2021, 5:34 AM

Address bondhugula's review comments

Dinistro marked 3 inline comments as done.Nov 6 2021, 5:38 AM

Thanks for your review. I addressed the comments by only exposing parseIntegerSet from the parser library and using it in AffineStructuresParser which is now part of the Affine Analysis library.

bondhugula requested changes to this revision.Nov 6 2021, 10:05 PM

The diff is being shown on the wrong base. Please squash the multiple commits that you have on this revision. phab itself is able to show your revisions as you change them.

mlir/lib/Analysis/CMakeLists.txt
5 ↗(On Diff #385265)

You don't need a separate file for a single function. It can be created when more are to be added. Could you just add the parsing function to AffineStructures.h/.cpp?

This revision now requires changes to proceed.Nov 6 2021, 10:05 PM
Dinistro updated this revision to Diff 385411.Nov 8 2021, 12:25 AM

Squash commits and moving parsing functionality in AffineStructures.(h|cpp)

Dinistro updated this revision to Diff 385438.Nov 8 2021, 3:42 AM

Add missing MLIRParser dependency to MLIRLoopAnalysis

Dinistro marked an inline comment as done.Nov 8 2021, 3:45 AM
Groverkss added inline comments.Nov 12 2021, 2:03 PM
mlir/unittests/Analysis/AffineStructuresParserTest.cpp
33

"stings" should be "strings". Same in other places.

Dinistro updated this revision to Diff 387012.Nov 13 2021, 12:38 AM

Fix typos in error messages of AffineStructuresParserTest.

Dinistro marked an inline comment as done.Nov 13 2021, 12:38 AM
bondhugula requested changes to this revision.Nov 13 2021, 7:25 PM
bondhugula added inline comments.
mlir/include/mlir/Analysis/AffineStructures.h
1079 ↗(On Diff #387012)

This isn't parsing a FlatAffineConstraints -- instead, parseIntegerSetToFAC?

mlir/unittests/Analysis/AffineStructuresParserTest.cpp
2

Missing header block along with a file-level summary comment.

17–19

Drop trivial braces.

64–88

Creating an MLIRContext for each parseAndCompare doesn't look great. Instead, create it outside and pass it to parseAndCompare.

mlir/unittests/Analysis/AffineStructuresTest.cpp
105

This should be taking an MLIRContext as an argument instead of creating one each time -- also to be consistent with other similar methods.

mlir/unittests/Analysis/CMakeLists.txt
5

Sorted order here.

This revision now requires changes to proceed.Nov 13 2021, 7:25 PM
Dinistro updated this revision to Diff 387067.Nov 14 2021, 4:51 AM
Dinistro marked 6 inline comments as done.

Addressed additional comments from bondhugula.

Thanks for your comments. All of them should be addressed now.

This revision is now accepted and ready to land.Nov 14 2021, 8:26 AM
This revision was automatically updated to reflect the committed changes.
rriddle added inline comments.Nov 14 2021, 11:21 PM
mlir/lib/Analysis/AffineStructures.cpp
3870 ↗(On Diff #387109)

Analysis should not be depending on the parser, please revert this.

Commented inline, but we should not be depending on the parser from Analysis. Can you revert this and expose the functionality via the parser lib? We shouldn't be exposing parsing functionality in Analysis.

mehdi_amini added inline comments.Nov 14 2021, 11:24 PM
mlir/lib/Analysis/AffineStructures.cpp
3870 ↗(On Diff #387109)

I reverted in d5730647accf for now.

Exposing this from the Parser library would make it dependent on Analysis, see bondhugula's earlier comments. Would this be an acceptable dependency or do you have another suggestion on how to solve this cleanly?

Exposing this from the Parser library would make it dependent on Analysis, see bondhugula's earlier comments. Would this be an acceptable dependency or do you have another suggestion on how to solve this cleanly?

Thanks for pointing that out (I didn't read the full comments). I don't think we want the dependency in either case. Do we really need the combined method? Seems fairly simple (2-3 lines), and removing it also removes the need for any kind of dependency management.

The whole point of this patch is to provide a helper function for unit tests involving FlatAffineConstraints as construction them manually is error prone. Parsing them from a string is a lot easier and simplifies test writing. Maybe one could make the tests themselves depend on both libraries and do the transformation from IntegerSets to FACs there, but I'm not sure if that's clean either.

The whole point of this patch is to provide a helper function for unit tests involving FlatAffineConstraints as construction them manually is error prone. Parsing them from a string is a lot easier and simplifies test writing. Maybe one could make the tests themselves depend on both libraries and do the transformation from IntegerSets to FACs there, but I'm not sure if that's clean either.

If it's just for unit tests, we shouldn't be adding it in the public API at all. This should be limited, and likely just local to the unit test files/directory.

Dinistro reopened this revision.Nov 15 2021, 5:00 AM
This revision is now accepted and ready to land.Nov 15 2021, 5:00 AM
Dinistro updated this revision to Diff 387219.Nov 15 2021, 5:01 AM

Moved the parseIntegerSetToFAC function into the unittests directory to avoid coupling the Analysis and Parser libraries.

I implemented River's suggestions and reopened this revision to ensure that all the comments and previous versions stay in the same place.

Dinistro updated this revision to Diff 387262.Nov 15 2021, 7:44 AM

Fix linting error and revert unrelated changes.

Groverkss added inline comments.Nov 15 2021, 8:11 AM
mlir/lib/Parser/AffineParser.cpp
733

For unit tests, we probably do not want diagnostic information to be printed. Can you somehow pass llvm::nulls() to SourceMgrDiagnosticHandler when it is used for unittests?

bondhugula accepted this revision.Nov 15 2021, 8:38 AM

Moved the parseIntegerSetToFAC function into the unittests directory to avoid coupling the Analysis and Parser libraries.

This looks fine to me with the unit tests library depending on Parser. A dependency on the entire parser library is a bit weird, but if needed, the affine parser library could be split out into a smaller library in the future.

Dinistro updated this revision to Diff 387302.Nov 15 2021, 9:50 AM

Add flag for printing diagnostics. This ensures that parser tests do not flood llvm:errs() with unimportant infos.

Dinistro marked an inline comment as done.Nov 15 2021, 9:53 AM
grosser accepted this revision.Nov 15 2021, 10:04 AM

This looks good from my side as well. Let's see what @rriddle says.

What do you think @rriddle ?

Having a dependency on the parser from a unit test looks fine, LGTM.

This revision was automatically updated to reflect the committed changes.