[Mlir-commits] [mlir] 3151190 - Fix quadratic behavior from Operation::setAttr() (NFC)

Mehdi Amini llvmlistbot at llvm.org
Fri May 19 14:01:55 PDT 2023


Author: Mehdi Amini
Date: 2023-05-19T14:01:40-07:00
New Revision: 31511900c68df693b38decaa99dd9f8dba61ef65

URL: https://github.com/llvm/llvm-project/commit/31511900c68df693b38decaa99dd9f8dba61ef65
DIFF: https://github.com/llvm/llvm-project/commit/31511900c68df693b38decaa99dd9f8dba61ef65.diff

LOG: Fix quadratic behavior from Operation::setAttr()  (NFC)

This API tries to ensure some backward compatibility for properties,
but doing so in multiple-layers was causing quadratic behavior.
Instead of `setAttrs()` repeatingly calling to `setAttr()` we inline
the logic and apply it locally in a single traversal.

Fixes #62800

Differential Revision: https://reviews.llvm.org/D150993

Added: 
    

Modified: 
    mlir/lib/IR/Operation.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index 64cbeb6303320..eaf5a471a99ab 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -303,18 +303,36 @@ DictionaryAttr Operation::getAttrDictionary() {
 void Operation::setAttrs(DictionaryAttr newAttrs) {
   assert(newAttrs && "expected valid attribute dictionary");
   if (getPropertiesStorageSize()) {
-    attrs = DictionaryAttr::get(getContext(), {});
-    for (const NamedAttribute &attr : newAttrs)
-      setAttr(attr.getName(), attr.getValue());
-    return;
+    // We're spliting the providing DictionaryAttr by removing the inherentAttr
+    // which will be stored in the properties.
+    SmallVector<NamedAttribute> discardableAttrs;
+    discardableAttrs.reserve(newAttrs.size());
+    for (NamedAttribute attr : newAttrs) {
+      if (std::optional<Attribute> inherentAttr =
+              getInherentAttr(attr.getName()))
+        setInherentAttr(attr.getName(), attr.getValue());
+      else
+        discardableAttrs.push_back(attr);
+    }
+    if (discardableAttrs.size() != newAttrs.size())
+      newAttrs = DictionaryAttr::get(getContext(), discardableAttrs);
   }
   attrs = newAttrs;
 }
 void Operation::setAttrs(ArrayRef<NamedAttribute> newAttrs) {
   if (getPropertiesStorageSize()) {
-    setAttrs(DictionaryAttr::get(getContext(), {}));
-    for (const NamedAttribute &attr : newAttrs)
-      setAttr(attr.getName(), attr.getValue());
+    // We're spliting the providing array of attributes by removing the inherentAttr
+    // which will be stored in the properties.
+    SmallVector<NamedAttribute> discardableAttrs;
+    discardableAttrs.reserve(newAttrs.size());
+    for (NamedAttribute attr : newAttrs) {
+      if (std::optional<Attribute> inherentAttr =
+              getInherentAttr(attr.getName()))
+        setInherentAttr(attr.getName(), attr.getValue());
+      else
+        discardableAttrs.push_back(attr);
+    }
+    attrs = DictionaryAttr::get(getContext(), discardableAttrs);
     return;
   }
   attrs = DictionaryAttr::get(getContext(), newAttrs);


        


More information about the Mlir-commits mailing list