[Mlir-commits] [mlir] b3a5f53 - [mlir] Add `empty` to `StringAttr`

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Sep 5 14:45:40 PDT 2023


Author: Mogball
Date: 2023-09-05T21:45:27Z
New Revision: b3a5f5394f1f148ff9c7ab7bc0c837235eda6cc6

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

LOG: [mlir] Add `empty` to `StringAttr`

It was common to see `value.getValue().empty()` or `value.size()`
instead of the idiomatic `value.empty()`.

Depends on D159455

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/BuiltinAttributes.td
    mlir/lib/Dialect/EmitC/IR/EmitC.cpp
    mlir/lib/Target/LLVMIR/DebugTranslation.cpp
    mlir/test/lib/Dialect/Test/TestDialect.cpp
    mlir/unittests/IR/AttributeTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td
index 075eee456a7b58f..093303495469c37 100644
--- a/mlir/include/mlir/IR/BuiltinAttributes.td
+++ b/mlir/include/mlir/IR/BuiltinAttributes.td
@@ -1064,6 +1064,9 @@ def Builtin_StringAttr : Builtin_Attr<"String", [TypedAttrInterface]> {
     /// Return the number of bytes in this string.
     size_t size() const { return getValue().size(); }
 
+    /// Return whether the string is empty.
+    bool empty() const { return getValue().empty(); }
+
     /// Iterate over the underlying string data.
     StringRef::iterator begin() const { return getValue().begin(); }
     StringRef::iterator end() const { return getValue().end(); }

diff  --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index 0aac33e22cbc40b..1c95ed07702d1db 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -149,7 +149,7 @@ LogicalResult emitc::ConstantOp::verify() {
 
   // Value must not be empty
   StringAttr strAttr = llvm::dyn_cast<StringAttr>(getValueAttr());
-  if (strAttr && strAttr.getValue().empty())
+  if (strAttr && strAttr.empty())
     return emitOpError() << "value must not be empty";
 
   auto value = cast<TypedAttr>(getValueAttr());
@@ -160,9 +160,7 @@ LogicalResult emitc::ConstantOp::verify() {
   return success();
 }
 
-OpFoldResult emitc::ConstantOp::fold(FoldAdaptor adaptor) {
-  return getValue();
-}
+OpFoldResult emitc::ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }
 
 //===----------------------------------------------------------------------===//
 // IncludeOp

diff  --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
index fcd0ad3676952f0..2053f5bcef06aa6 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -84,7 +84,7 @@ llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
 }
 
 llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
-  if (!stringAttr || stringAttr.getValue().empty())
+  if (!stringAttr || stringAttr.empty())
     return nullptr;
   return llvm::MDString::get(llvmCtx, stringAttr);
 }

diff  --git a/mlir/test/lib/Dialect/Test/TestDialect.cpp b/mlir/test/lib/Dialect/Test/TestDialect.cpp
index af148534f366160..ae4c9a85605e1c5 100644
--- a/mlir/test/lib/Dialect/Test/TestDialect.cpp
+++ b/mlir/test/lib/Dialect/Test/TestDialect.cpp
@@ -859,7 +859,7 @@ void CustomResultsNameOp::getAsmResultNames(
   ArrayAttr value = getNames();
   for (size_t i = 0, e = value.size(); i != e; ++i)
     if (auto str = dyn_cast<StringAttr>(value[i]))
-      if (!str.getValue().empty())
+      if (!str.empty())
         setNameFn(getResult(i), str.getValue());
 }
 

diff  --git a/mlir/unittests/IR/AttributeTest.cpp b/mlir/unittests/IR/AttributeTest.cpp
index a707dfa58be1976..9afbce037b408c0 100644
--- a/mlir/unittests/IR/AttributeTest.cpp
+++ b/mlir/unittests/IR/AttributeTest.cpp
@@ -432,7 +432,7 @@ TEST(SparseElementsAttrTest, GetZero) {
 
   auto zeroStringValue =
       cast<StringAttr>(sparseString.getValues<Attribute>()[{1, 1}]);
-  EXPECT_TRUE(zeroStringValue.getValue().empty());
+  EXPECT_TRUE(zeroStringValue.empty());
   EXPECT_TRUE(zeroStringValue.getType() == stringTy);
 }
 


        


More information about the Mlir-commits mailing list