This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Implement replacement of SymbolRefAttrs in Dialect attributes using SubElementAttr interface
ClosedPublic

Authored by zero9178 on Oct 7 2021, 2:39 PM.

Details

Summary

This patch extends the SubElementAttr interface to allow replacing a contained sub attribute. The attribute that should be replaced is identified by an index which denotes the n-th element returned by the accompanying walkImmediateSubElements method.

Using this addition the patch implements replacing SymbolRefAttrs contained within any dialect attributes.

Diff Detail

Event Timeline

zero9178 created this revision.Oct 7 2021, 2:39 PM
zero9178 requested review of this revision.Oct 7 2021, 2:39 PM
rriddle requested changes to this revision.Oct 11 2021, 6:53 PM

Awesome! Thanks for taking this on. This has been a TODO for a long time (and more recently since I added the sub element interfaces).

Can you also update the documentation for SymbolRefAttr to remove the restrictions on where it can be held? (https://github.com/llvm/llvm-project/blob/998e067a0a579bd483c2a2d411385e58494ffbca/mlir/include/mlir/IR/BuiltinAttributes.td#L951)

mlir/include/mlir/IR/BuiltinAttributes.td
69

Please wrap tablegen files at 80 characters.

mlir/include/mlir/IR/SubElementInterfaces.td
38–41

Can you add an explanation for the result value?

43

Can you drop the std:: here? We don't do that anywhere else.

46

Do we need a default implementation? Is there a downside to enforcing this method be defined? This would also remove the error handling that would arise from the default implementation.

mlir/lib/IR/BuiltinAttributes.cpp
56

Please run clang-format, and also drop the std:: from size_t.

58

nit: Spell out auto here.

225

Same here.

mlir/lib/IR/SymbolTable.cpp
494–495

You should be able to do walkSubAttrs here.

500

Prefer proper constructor calls.

832

Drop the llvm:: here.

833–835

walkSubAttrs?

841–842

Why not update all attributes and then rebuild once at the end? Otherwise, this may result in a bunch of uniquing that isn't necessary

This revision now requires changes to proceed.Oct 11 2021, 6:53 PM
zero9178 updated this revision to Diff 379152.Oct 12 2021, 1:01 PM

Addressed reviewers comments:

  • Fixed style issues
  • Changed corresponding documentation
  • Avoid redundant uniquing by replacing attributes in a batch
zero9178 marked 9 inline comments as done.Oct 12 2021, 1:07 PM
zero9178 added inline comments.
mlir/include/mlir/IR/SubElementInterfaces.td
46

I am not quite sure about this one as well. The cases I was thinking of in particular were types and attributes that do not contain any attributes.
In this case simply calling this operation is invalid. As this is a clear programmer error I chose to use an assert statement for now. As there are even many types in builtin alone that implement SubElementTypeInterface but don't have attributes I felt like this might warrant a default implementation.

mlir/lib/IR/SymbolTable.cpp
833–835

walkSubAttrs recursively walks through the attributes and does so in a depth first order which would yield wrong indices and more attributes than can be replaced by the interface. replaceImmediateSubAttribute is meant as a counterpart to walkImmediateSubElements as they should only be concerned with attributes that are immediately contained within the attribute.

rriddle added inline comments.Oct 13 2021, 12:31 AM
mlir/include/mlir/IR/SubElementInterfaces.td
47–48

Can you use llvm_unreachable instead?

mlir/lib/IR/SymbolTable.cpp
833–835

I forgot originally, but if we expand support we will also likely need to support references inside of Types as well. That can likely be deferred, but the documentation will need to indicate that holding references inside of Types (including attributes within those types) is not supported.

zero9178 updated this revision to Diff 379313.Oct 13 2021, 2:19 AM

Address reviewer comments:

  • Document that symbol reference attributes are currently not supported inside of types
  • Use llvm_unreachble instead of assert

Friendly bump :)

rriddle accepted this revision.Oct 28 2021, 9:48 AM

Friendly bump :)

Thanks for the ping, appreciate it! Got bogged down with some other work.

Did a scan, LGTM.

This revision is now accepted and ready to land.Oct 28 2021, 9:48 AM

I'm getting weird intermittent build errors that I think are associated with this change. So far:

ld.lld: error: undefined symbol: mlir::SubElementAttrInterface::replaceImmediateSubAttribute(llvm::ArrayRef<std::pair<unsigned long, mlir::Attribute> >) const
>>> referenced by SymbolTable.cpp
In file included from /usr/local/google/home/gcmn/src/iree/third_party/llvm-project/mlir/include/mlir/IR/BuiltinAttributes.h:13:
/usr/local/google/home/gcmn/src/iree/third_party/llvm-project/mlir/include/mlir/IR/SubElementInterfaces.h:21:10: fatal error: 'mlir/IR/SubElementAttrInterfaces.h.inc' file not found
#include "mlir/IR/SubElementAttrInterfaces.h.inc"

My guess is that this is something missing in the CMake deps, but I actually don't really understand how that can cause a linker error. I'm poking around, but wanted to give a heads up in case others are seeing the same thing

I'm getting weird intermittent build errors that I think are associated with this change.

My theory is now that this was an issue in mlir-hlo with missing deps and somehow that *also* poisoned my ccache. This change probably just tickled something.