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()); } +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 ®istry) impl->unitAttr = AttributeUniquer::get(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 //===----------------------------------------------------------------------===//