This is an archive of the discontinued LLVM Phabricator instance.

[mlir] support recursive types in type conversion infra
ClosedPublic

Authored by ftynse on Nov 10 2021, 9:55 AM.

Details

Summary

MLIR supports recursive types but they could not be handled by the conversion
infrastructure directly as it would result in infinite recursion in
convertType for elemental types. Support this case by keeping the "call
stack" of nested type conversions in the TypeConverter class and by passing it
as an optional argument to the individual conversion callback. The callback can
then check if a specific type is present on the stack more than once to detect
and handle the recursive case.

This approach is preferred to the alternative approach of having a separate
callback dedicated to handling only the recursive case as the latter was
observed to introduce ~3% time overhead on a 50MB IR file even if it did not
contain recursive types.

This approach is also preferred to keeping a local stack in type converters
that need to handle recursive types as that would compose poorly in case of
out-of-tree or cross-project extensions.

Diff Detail

Event Timeline

ftynse created this revision.Nov 10 2021, 9:55 AM
ftynse requested review of this revision.Nov 10 2021, 9:55 AM

Thanks for summarizing the different approaches considered!

mlir/lib/Transforms/Utils/DialectConversion.cpp
2936

typo: Convresion

mlir/test/lib/Dialect/Test/TestPatterns.cpp
1053

is_contained?

1053

Can you add a comment here? Given that this would return SimpleAType for nested references of TestRecursiveType, which seems non-obvious.

ftynse updated this revision to Diff 388446.Nov 19 2021, 2:53 AM
ftynse marked 3 inline comments as done.

Address review.

ftynse added inline comments.Nov 19 2021, 2:54 AM
mlir/test/lib/Dialect/Test/TestPatterns.cpp
1053

The check is to have more than one occurrence, is_contained would check for more than zero. Added another comment to clarify that.

rriddle accepted this revision.Nov 19 2021, 12:09 PM
rriddle added inline comments.
mlir/test/lib/Dialect/Test/TestPatterns.cpp
1053

Right thanks, glossed over that we always have at least one entry. I suppose we could also have done is_contained(callStack.drop_back())? Not important though.

This revision is now accepted and ready to land.Nov 19 2021, 12:09 PM
ftynse updated this revision to Diff 388876.Nov 22 2021, 4:20 AM
ftynse marked an inline comment as done.

Address review.

This revision was automatically updated to reflect the committed changes.