[Mlir-commits] [mlir] aaa2982 - [MLIR Core] Cache the empty StringAttr like we do for empty dictionaries. NFC.

Chris Lattner llvmlistbot at llvm.org
Tue May 25 14:58:24 PDT 2021


Author: Chris Lattner
Date: 2021-05-25T14:58:17-07:00
New Revision: aaa2982d7191eaf8638c91d0aa5b16f5523a1fc8

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

LOG: [MLIR Core] Cache the empty StringAttr like we do for empty dictionaries. NFC.

MLIRContext holds a few special case values that occur frequently like empty
dictionary and NoneType, which allow us to avoid taking locks to get an instance
of them.  Give the empty StringAttr this treatment as well.  This cuts several
percent off compile time for CIRCT.

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/BuiltinAttributes.td
    mlir/lib/IR/BuiltinAttributes.cpp
    mlir/lib/IR/MLIRContext.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td
index 2a142faa72046..5d6646bbb3703 100644
--- a/mlir/include/mlir/IR/BuiltinAttributes.td
+++ b/mlir/include/mlir/IR/BuiltinAttributes.td
@@ -817,10 +817,25 @@ def Builtin_StringAttr : Builtin_Attr<"String"> {
       return $_get(type.getContext(), bytes, type);
     }]>,
     AttrBuilder<(ins "StringRef":$bytes), [{
+      if (bytes.empty())
+        return get($_ctxt);
       return $_get($_ctxt, bytes, NoneType::get($_ctxt));
-    }]>
+    }]>,
+
+    /// Build an empty string attr with NoneType.
+    AttrBuilder<(ins)>
   ];
-  let extraClassDeclaration = "using ValueType = StringRef;";
+  let extraClassDeclaration = [{
+    using ValueType = StringRef;
+
+  private:
+    /// Return an empty StringAttr with NoneType type. This is a special variant
+    /// of the `get` method that is used by the MLIRContext to cache the
+    /// instance.
+    static StringAttr getEmptyStringAttrUnchecked(MLIRContext *context);
+    friend MLIRContext;
+  public:
+  }];
   let skipDefaultBuilders = 1;
 }
 

diff  --git a/mlir/lib/IR/BuiltinAttributes.cpp b/mlir/lib/IR/BuiltinAttributes.cpp
index b36a24435a9ca..7c7264876bc9d 100644
--- a/mlir/lib/IR/BuiltinAttributes.cpp
+++ b/mlir/lib/IR/BuiltinAttributes.cpp
@@ -196,6 +196,10 @@ DictionaryAttr DictionaryAttr::getEmptyUnchecked(MLIRContext *context) {
   return Base::get(context, ArrayRef<NamedAttribute>());
 }
 
+StringAttr StringAttr::getEmptyStringAttrUnchecked(MLIRContext *context) {
+  return Base::get(context, "", NoneType::get(context));
+}
+
 //===----------------------------------------------------------------------===//
 // FloatAttr
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp
index f438c00085516..51e09522aafc1 100644
--- a/mlir/lib/IR/MLIRContext.cpp
+++ b/mlir/lib/IR/MLIRContext.cpp
@@ -332,6 +332,7 @@ class MLIRContextImpl {
   UnitAttr unitAttr;
   UnknownLoc unknownLocAttr;
   DictionaryAttr emptyDictionaryAttr;
+  StringAttr emptyStringAttr;
 
 public:
   MLIRContextImpl() : identifiers(identifierAllocator) {}
@@ -400,6 +401,8 @@ MLIRContext::MLIRContext(const DialectRegistry &registry)
   impl->unitAttr = AttributeUniquer::get<UnitAttr>(this);
   /// The empty dictionary attribute.
   impl->emptyDictionaryAttr = DictionaryAttr::getEmptyUnchecked(this);
+  /// The empty string attribute.
+  impl->emptyStringAttr = StringAttr::getEmptyStringAttrUnchecked(this);
 
   // Register the affine storage objects with the uniquer.
   impl->affineUniquer
@@ -929,6 +932,11 @@ DictionaryAttr DictionaryAttr::getEmpty(MLIRContext *context) {
   return context->getImpl().emptyDictionaryAttr;
 }
 
+/// Return an empty string.
+StringAttr StringAttr::get(MLIRContext *context) {
+  return context->getImpl().emptyStringAttr;
+}
+
 //===----------------------------------------------------------------------===//
 // AffineMap uniquing
 //===----------------------------------------------------------------------===//


        


More information about the Mlir-commits mailing list