diff options
| author | Chris Lattner <clattner@nondot.org> | 2021-05-25 14:38:01 -0700 |
|---|---|---|
| committer | Chris Lattner <clattner@nondot.org> | 2021-05-25 14:58:17 -0700 |
| commit | aaa2982d7191eaf8638c91d0aa5b16f5523a1fc8 (patch) | |
| tree | 20d68553001d1f1245487b99082f32e7cf03d71a | |
| parent | a6a57f03be40cf2c546805155624a206649092ec (diff) | |
[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
| -rw-r--r-- | mlir/include/mlir/IR/BuiltinAttributes.td | 19 | ||||
| -rw-r--r-- | mlir/lib/IR/BuiltinAttributes.cpp | 4 | ||||
| -rw-r--r-- | mlir/lib/IR/MLIRContext.cpp | 8 |
3 files changed, 29 insertions, 2 deletions
diff --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td index 2a142faa7204..5d6646bbb370 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 b36a24435a9c..7c7264876bc9 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 f438c0008551..51e09522aafc 100644 --- a/mlir/lib/IR/MLIRContext.cpp +++ b/mlir/lib/IR/MLIRContext.cpp @@ -332,6 +332,7 @@ public: UnitAttr unitAttr; UnknownLoc unknownLocAttr; DictionaryAttr emptyDictionaryAttr; + StringAttr emptyStringAttr; public: MLIRContextImpl() : identifiers(identifierAllocator) {} @@ -400,6 +401,8 @@ MLIRContext::MLIRContext(const DialectRegistry ®istry) 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 //===----------------------------------------------------------------------===// |
