[mlir][SymbolTable] Small optimization to walking symbol references
authorRiver Riddle <riddleriver@gmail.com>
Thu, 29 Oct 2020 05:00:55 +0000 (22:00 -0700)
committerRiver Riddle <riddleriver@gmail.com>
Thu, 29 Oct 2020 05:01:10 +0000 (22:01 -0700)
* Check region count for unknown symbol tables first, as it is a faster check
* Add an accessor to MutableDictionaryAttr to get the internal dictionary without creating a new one if it is empty. This avoids an otherwise unnecessary lookup of an MLIRContext.

mlir/include/mlir/IR/Attributes.h
mlir/lib/IR/SymbolTable.cpp

index b4a1473..4a8cc56 100644 (file)
@@ -1621,6 +1621,10 @@ public:
   /// Return the underlying dictionary attribute.
   DictionaryAttr getDictionary(MLIRContext *context) const;
 
+  /// Return the underlying dictionary attribute or null if there are no
+  /// attributes within this dictionary.
+  DictionaryAttr getDictionaryOrNull() const { return attrs; }
+
   /// Return all of the attributes on this operation.
   ArrayRef<NamedAttribute> getAttrs() const;
 
index d4377d6..90b4a9d 100644 (file)
@@ -17,7 +17,7 @@ using namespace mlir;
 /// Return true if the given operation is unknown and may potentially define a
 /// symbol table.
 static bool isPotentiallyUnknownSymbolTable(Operation *op) {
-  return !op->getDialect() && op->getNumRegions() == 1;
+  return op->getNumRegions() == 1 && !op->getDialect();
 }
 
 /// Returns the string name of the given symbol, or None if this is not a
@@ -442,9 +442,9 @@ static WalkResult walkSymbolRefs(
     Operation *op,
     function_ref<WalkResult(SymbolTable::SymbolUse, ArrayRef<int>)> callback) {
   // Check to see if the operation has any attributes.
-  if (op->getMutableAttrDict().empty())
+  DictionaryAttr attrDict = op->getMutableAttrDict().getDictionaryOrNull();
+  if (!attrDict)
     return WalkResult::advance();
-  DictionaryAttr attrDict = op->getAttrDictionary();
 
   // A worklist of a container attribute and the current index into the held
   // attribute list.