This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Refactor ShapedType into an interface
ClosedPublic

Authored by rriddle on Jan 10 2022, 10:56 AM.

Details

Summary

ShapedType was created in a time before interfaces, and is one of the earliest
type base classes in the ecosystem. This commit refactors ShapedType into
an interface, which is what it would have been if interfaces had existed at that
time. The API of ShapedType and it's derived classes are essentially untouched
by this refactor, with the exception being the API surrounding kDynamicIndex
(which requires a sole home).

For now, the API of ShapedType and its name have been kept as consistent to
the current state of the world as possible (to help with potential migration churn,
among other reasons). Moving forward though, we should look into potentially
restructuring its API and possible its name as well (it should really have "Interface"
at the end like other interfaces at the very least).

One other potentially interesting note is that I've attached the ShapedType::Trait
to TensorType/BaseMemRefType to act as mixins for the ShapedType API. This
is kind of weird, but allows for sharing the same API (i.e. preventing API loss from
the transition from base class -> Interface). This inheritance doesn't affect any
of the derived classes, it is just for API mixin.

Diff Detail

Event Timeline

rriddle created this revision.Jan 10 2022, 10:56 AM
rriddle requested review of this revision.Jan 10 2022, 10:56 AM
nicolasvasilache accepted this revision.Jan 11 2022, 3:52 AM

Nice, thanks much!

This revision is now accepted and ready to land.Jan 11 2022, 3:52 AM
stellaraccident accepted this revision.Jan 11 2022, 8:19 AM

Thank you River - this is much nicer than I thought it would be.

mehdi_amini added inline comments.Jan 11 2022, 12:23 PM
mlir/unittests/IR/ShapedTypeTest.cpp
33

Why isn't this spelled cast<ShapedType>(...)? Isn't it an interface cast here?

33

Or rather: MemRefType::Builder(...).cast<ShapedType>()

rriddle added inline comments.Jan 11 2022, 12:58 PM
mlir/unittests/IR/ShapedTypeTest.cpp
33

It's conceptually similar, but derived types can convert to the interface type without a direct cast (in this case MemRefType -> ShapedType) similarly to how derived classes can convert to base classes.

mehdi_amini accepted this revision.Jan 11 2022, 1:43 PM
jpienaar accepted this revision.Jan 11 2022, 1:50 PM
jpienaar added a subscriber: jpienaar.

Nice, thanks!

mlir/include/mlir/IR/BuiltinTypeInterfaces.td
117

Why auto here?

mlir/unittests/IR/ShapedTypeTest.cpp
33

Is (ShapedType) even needed given implict conversion and type assigned to? (Having the cast would be nice for consistency though and then using auto as the type is already spelled out on RHS)

86

Same here, is it needed for the EQ ?

rriddle marked 5 inline comments as done.Jan 11 2022, 1:53 PM
rriddle added inline comments.
mlir/include/mlir/IR/BuiltinTypeInterfaces.td
117

The return type is different depending on if this is on the interface class or the trait class given that these methods are shared (return could be ShapedType/MemRefType/VectorType/etc..

mlir/unittests/IR/ShapedTypeTest.cpp
33

Yes it's needed, because there is already one implicit conversion (MemRefType::Builder to MemRefType).

86

Yeah, had some equality errors otherwise.

rriddle updated this revision to Diff 399435.Jan 12 2022, 1:38 PM
rriddle edited the summary of this revision. (Show Details)
rriddle marked 3 inline comments as done.
This revision was automatically updated to reflect the committed changes.