diff mlir/unittests/TableGen/EnumsGenTest.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 1d019706d866
children c4bab56944e8
line wrap: on
line diff
--- a/mlir/unittests/TableGen/EnumsGenTest.cpp	Tue Jun 15 19:13:43 2021 +0900
+++ b/mlir/unittests/TableGen/EnumsGenTest.cpp	Tue Jun 15 19:15:29 2021 +0900
@@ -6,23 +6,29 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "mlir/IR/BuiltinAttributes.h"
+#include "mlir/IR/BuiltinTypes.h"
+#include "mlir/IR/MLIRContext.h"
+#include "mlir/Support/LLVM.h"
+
 #include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/Optional.h"
-#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
+
 #include "gmock/gmock.h"
+
 #include <type_traits>
 
-// Pull in generated enum utility declarations
+/// Pull in generated enum utility declarations and definitions.
 #include "EnumsGenTest.h.inc"
-// And definitions
+
 #include "EnumsGenTest.cpp.inc"
 
-// Test namespaces and enum class/utility names
+/// Test namespaces and enum class/utility names.
 using Outer::Inner::ConvertToEnum;
 using Outer::Inner::ConvertToString;
 using Outer::Inner::StrEnum;
+using Outer::Inner::StrEnumAttr;
 
 TEST(EnumsGenTest, GeneratedStrEnumDefinition) {
   EXPECT_EQ(0u, static_cast<uint64_t>(StrEnum::CaseA));
@@ -112,3 +118,41 @@
   auto none = symbolizePrettyIntEnum("Case1");
   EXPECT_FALSE(none);
 }
+
+TEST(EnumsGenTest, GeneratedIntAttributeClass) {
+  mlir::MLIRContext ctx;
+  I32Enum rawVal = I32Enum::Case5;
+
+  I32EnumAttr enumAttr = I32EnumAttr::get(&ctx, rawVal);
+  EXPECT_NE(enumAttr, nullptr);
+  EXPECT_EQ(enumAttr.getValue(), rawVal);
+
+  mlir::Type intType = mlir::IntegerType::get(&ctx, 32);
+  mlir::Attribute intAttr = mlir::IntegerAttr::get(intType, 5);
+  EXPECT_TRUE(intAttr.isa<I32EnumAttr>());
+  EXPECT_EQ(intAttr, enumAttr);
+}
+
+TEST(EnumsGenTest, GeneratedStringAttributeClass) {
+  mlir::MLIRContext ctx;
+  StrEnum rawVal = StrEnum::CaseA;
+
+  StrEnumAttr enumAttr = StrEnumAttr::get(&ctx, rawVal);
+  EXPECT_NE(enumAttr, nullptr);
+  EXPECT_EQ(enumAttr.getValue(), rawVal);
+
+  mlir::Attribute strAttr = mlir::StringAttr::get(&ctx, "CaseA");
+  EXPECT_TRUE(strAttr.isa<StrEnumAttr>());
+  EXPECT_EQ(strAttr, enumAttr);
+}
+
+TEST(EnumsGenTest, GeneratedBitAttributeClass) {
+  mlir::MLIRContext ctx;
+
+  mlir::Type intType = mlir::IntegerType::get(&ctx, 32);
+  mlir::Attribute intAttr = mlir::IntegerAttr::get(
+      intType,
+      static_cast<uint32_t>(BitEnumWithNone::Bit1 | BitEnumWithNone::Bit3));
+  EXPECT_TRUE(intAttr.isa<BitEnumWithNoneAttr>());
+  EXPECT_TRUE(intAttr.isa<BitEnumWithoutNoneAttr>());
+}