This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add NamedAttrList
ClosedPublic

Authored by jpienaar on May 5 2020, 6:16 PM.

Details

Summary

This is a wrapper around vector of NamedAttributes that keeps track of whether sorted and does some minimal effort to remain sorted (doing more, e.g., appending attributes in sorted order, could be done in follow up). It contains whether sorted and if a DictionaryAttr is queried, it caches the returned DictionaryAttr along with whether sorted.

Change MutableDictionaryAttr to always return a non-null Attribute even when empty (reserve null cases for errors). To this end change the getter to take a context as input so that the empty DictionaryAttr could be queried. Also create one instance of the empty dictionary attribute that could be reused without needing to lock context etc.

Update infer type op interface to use DictionaryAttr and use NamedAttrList to avoid incurring multiple conversion costs.

Fix bug in sorting helper function.

Diff Detail

Event Timeline

jpienaar created this revision.May 5 2020, 6:16 PM
Herald added a reviewer: herhut. · View Herald Transcript
Herald added 1 blocking reviewer(s): rriddle. · View Herald Transcript
Herald added a reviewer: aartbik. · View Herald Transcript
Herald added a reviewer: ftynse. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
jpienaar updated this revision to Diff 262287.May 5 2020, 8:23 PM

Update flang side usage.

mravishankar resigned from this revision.May 6 2020, 9:57 AM
rriddle added inline comments.May 6 2020, 11:33 AM
mlir/include/mlir/IR/Attributes.h
315

nit: whether the values were sorted?

mlir/include/mlir/IR/FunctionSupport.h
521

Can we just add the overload for Operation::removeAttr that takes a string?

mlir/lib/IR/Attributes.cpp
112

nit: merge the if with the previous else

168

Add a comment for why you are doing this.

mlir/lib/IR/MLIRContext.cpp
394

ArrayRef<NamedAttribute>()

mlir/lib/IR/OperationSupport.cpp
38

setPointerAndInt

71

setPointerAndInt, here and a few other places

84

nit: Can you add a private 'isSorted' method?
also:

if (isSorted())

dictionarySorted.setInt(attrs.empty() || ...)

?

94

I would have expected a lower_bound if it was sorted? Also, can we add a static method to deduplicate all of these?

137

Can we add public operator< for NamedAttribute and NamedAttribute/StringRef, likely also Identifier and Identifier/StringRef.

560

We should have MutableDictionaryAttr be directly hashable so we don't need this here.

mlir/lib/Pass/IRPrinting.cpp
36

Same here. We could generate a hash to add here instead of using the pointer.

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
881

This seems unrelated?

rriddle added inline comments.May 6 2020, 11:35 AM
mlir/lib/IR/MLIRContext.cpp
751

We could just have a private DictionaryAttr::getEmpty(MLIRContext *context) that the other methods call. That would allow for moving the other part of this back to Attributes.cpp

DavidTruby resigned from this revision.May 6 2020, 12:06 PM
jpienaar updated this revision to Diff 262468.May 6 2020, 1:50 PM
jpienaar marked 15 inline comments as done.

Add getEmpty (and move other constructor back), added hashing for MutableDictionaryAttr, added comparison for NamedAttribute.

jpienaar marked an inline comment as done.May 6 2020, 1:51 PM
jpienaar added inline comments.
mlir/lib/IR/Attributes.cpp
168

Updated to getEmpty and now should be more self-documenting, thanks

mlir/lib/IR/MLIRContext.cpp
751

Good idea, this cleaned this up.

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
881

Not really, without this we end up calling a method that has a ArrayRef<NamedAttribute> as operand and so end up still converting back and forth and iterating through the attributes once redundantly.

jpienaar updated this revision to Diff 262680.May 7 2020, 9:38 AM

Remove compare helper methods that now just map to operator<.

rriddle accepted this revision.May 7 2020, 11:02 AM
rriddle added inline comments.
mlir/include/mlir/IR/Attributes.h
1705

When would it be .empty() here if we are using nullptr for empty?

1707

The .cast<Attribute> seems unnecessary.

mlir/include/mlir/IR/FunctionSupport.h
581–584

attributes.empty()?

585

this->getOperation()->getContext()?

mlir/lib/IR/MLIRContext.cpp
749

context -> dictionary?

This revision is now accepted and ready to land.May 7 2020, 11:02 AM
jpienaar updated this revision to Diff 262730.May 7 2020, 12:33 PM
jpienaar marked 6 inline comments as done.

Addressed review comments

This revision was automatically updated to reflect the committed changes.