This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Promote the SubElementInterfaces to a core Attribute/Type construct
ClosedPublic

Authored by rriddle on Jan 20 2023, 9:39 PM.

Details

Summary

This commit restructures the sub element infrastructure to be a core part
of attributes and types, instead of being relegated to an interface. This
establishes sub element walking/replacement as something "always there",
which makes it easier to rely on for correctness/etc (which various bits of
infrastructure want, such as Symbols).

Attribute/Type now have walk and replace methods directly
accessible, which provide power API for interacting with sub elements. As
part of this, a new AttrTypeWalker class is introduced that supports caching
walked attributes/types, and a friendlier API (see the simplification of symbol
walking in SymbolTable.cpp).

Diff Detail

Event Timeline

rriddle created this revision.Jan 20 2023, 9:39 PM
rriddle requested review of this revision.Jan 20 2023, 9:39 PM
Mogball accepted this revision.Jan 20 2023, 10:03 PM

Beautiful

I assume AttrTypeHandler is now the only way to override the walk behaviour for an attribute or type? (And provide it for unusual attributes and types that might be written in C++..)

mlir/include/mlir/IR/AttrTypeSubElements.h
112

Is this for cycles?

mlir/lib/IR/CMakeLists.txt
30

Alphabetical pls

mlir/lib/IR/SymbolTable.cpp
494

Nice

mlir/unittests/IR/InterfaceTest.cpp
50

I think this test was making just making sure pointer traits for interfaces work. Do we still need the test?

This revision is now accepted and ready to land.Jan 20 2023, 10:03 PM

Nice graduation (gets us even a step towards introspection ... Well more pieces needed :-)). Have you thought about exposing these C side?

rriddle marked 4 inline comments as done.Jan 23 2023, 11:16 AM

Beautiful

I assume AttrTypeHandler is now the only way to override the walk behaviour for an attribute or type? (And provide it for unusual attributes and types that might be written in C++..)

You can just define the immediate walk methods like before. I didn't include anything on it, because I don't think anyone should be doing that in 99.99% of cases.

mlir/include/mlir/IR/AttrTypeSubElements.h
112

Yeah, and for general caching. It removes the need for the user to cache visited attributes/types themselves, and only worry about the actual walk code.

mlir/unittests/IR/InterfaceTest.cpp
50

I just dropped it because it tests the exact same code path as types (given that they use the same infra, and that will likely never change), not really useful by itself IMO.

rriddle updated this revision to Diff 491463.Jan 23 2023, 11:23 AM
rriddle marked 2 inline comments as done.

Have you thought about exposing these C side?

Not particularly. I'm not that involved with the C API, and given that has been need driven (and I don't have a need), I generally leave that to interested parties.

akuegel added inline comments.
mlir/lib/IR/AttrTypeSubElements.cpp
31

This seems to prevent iterating through all elements of an ArrayAttr that contains the same attribute more than once.
Example:

xla_entry_computation_result_layout = [

  dense<> : tensor<0xindex>,
  dense<> : tensor<0xindex>
]

This is an attribute that captures the layouts of the results of an XLA computation. Previously, we used walkSubAttrs to iterate over all elements of the array, now it seems there is no comparable walk() method for this.

mlir/unittests/IR/AttributeTest.cpp
435

It would be good if this test could add trueAttr again, to show that it would be skipped during walk().

rriddle added inline comments.Jan 30 2023, 10:14 AM
mlir/lib/IR/AttrTypeSubElements.cpp
31

You'll want to use the walkImmediateSubElements functions (which might need a slight up lift in API), which guarantee they walk all of the immediately nested things. walkSubElements always walked recursively, which didn't really have any reason to re-walk the same Attribute/Type multiple times.

akuegel added inline comments.Jan 30 2023, 11:15 PM
mlir/lib/IR/AttrTypeSubElements.cpp
31

I tried (very shortly) to use walkImmediateSubElements, but couldn't make it to work. It also seems easier to instead do a for-loop through the array. I guess for the array use case, we are fine with the for loop. Having a walk() function that not skips elements would make our code slightly easier though, as we either have a single DenseElementsAttr or an array of DenseElementsAttr, and a single walk call would cover both cases.

mlir/unittests/IR/AttributeTest.cpp
435

I have created https://reviews.llvm.org/D142956 for this.

mlir/unittests/IR/InterfaceTest.cpp