[Mlir-commits] [mlir] [MLIR] Add I{8, 16}Enum tablegen classes (PR #190825)

Robert Konicar llvmlistbot at llvm.org
Tue Apr 7 12:27:26 PDT 2026


https://github.com/Jezurko updated https://github.com/llvm/llvm-project/pull/190825

>From 718800629122f070636229eef919abaf4614759f Mon Sep 17 00:00:00 2001
From: Robert Konicar <rkonicar at mail.muni.cz>
Date: Tue, 7 Apr 2026 19:09:50 +0200
Subject: [PATCH 1/4] [MLIR] Add I{8,16}Enum tablegen classes

Add utility tablegen classes for creating 8 and 16 bit enums, simplifying defining enums that fit into smaller types.
---
 mlir/include/mlir/IR/EnumAttr.td           |  8 ++++++++
 mlir/test/lib/Dialect/Test/TestEnumDefs.td | 14 ++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/mlir/include/mlir/IR/EnumAttr.td b/mlir/include/mlir/IR/EnumAttr.td
index cfa2ac89350a1..6eef5075fe18a 100644
--- a/mlir/include/mlir/IR/EnumAttr.td
+++ b/mlir/include/mlir/IR/EnumAttr.td
@@ -48,6 +48,10 @@ class IntEnumAttrCaseBase<I intType, string sym, string strVal, int intVal> :
 
 // Cases of integer enums with a specific type. By default, the string
 // representation is the same as the C++ symbol name.
+class I8EnumCase<string sym, int val, string str = sym>
+  : EnumCase<sym, val, str, 8>;
+class I16EnumCase<string sym, int val, string str = sym>
+  : EnumCase<sym, val, str, 16>;
 class I32EnumCase<string sym, int val, string str = sym>
   : EnumCase<sym, val, str, 32>;
 class I64EnumCase<string sym, int val, string str = sym>
@@ -292,6 +296,10 @@ class IntEnum<string name, string summary, list<EnumCase> cases, int width>
           summary),
       cases, width>;
 
+class I8Enum<string name, string summary, list<EnumCase> cases>
+    : IntEnum<name, summary, cases, 8>;
+class I16Enum<string name, string summary, list<EnumCase> cases>
+    : IntEnum<name, summary, cases, 16>;
 class I32Enum<string name, string summary, list<EnumCase> cases>
     : IntEnum<name, summary, cases, 32>;
 class I64Enum<string name, string summary, list<EnumCase> cases>
diff --git a/mlir/test/lib/Dialect/Test/TestEnumDefs.td b/mlir/test/lib/Dialect/Test/TestEnumDefs.td
index bf805c4c54998..5d7c60d53c2c0 100644
--- a/mlir/test/lib/Dialect/Test/TestEnumDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestEnumDefs.td
@@ -65,6 +65,20 @@ def TestSimpleEnum : I32Enum<"SimpleEnum", "", [
   let cppNamespace = "::test";
 }
 
+def TestSimpleEnum8: I8Enum<"SimpleEnum8", "", [
+    I8EnumCase<"a", 254>,
+    I8EnumCase<"b", 255>,
+  ]> {
+  let cppNamespace = "::test";
+}
+
+def TestSimpleEnum16: I16Enum<"SimpleEnum16", "", [
+    I16EnumCase<"a", 65534>,
+    I16EnumCase<"b", 65535>,
+  ]> {
+  let cppNamespace = "::test";
+}
+
 def TestSimpleEnum64 : I64Enum<"SimpleEnum64", "", [
     I64EnumCase<"a", 4294967296>,
     I64EnumCase<"b", 4294967297>

>From d25db27cf37052c61fa9bb609870ef6d2452ada1 Mon Sep 17 00:00:00 2001
From: Robert Konicar <rkonicar at mail.muni.cz>
Date: Tue, 7 Apr 2026 19:57:53 +0200
Subject: [PATCH 2/4] fixup! [MLIR] Add I{8,16}Enum tablegen classes

---
 mlir/test/lib/Dialect/Test/TestAttrDefs.td |  7 +++++++
 mlir/test/lib/Dialect/Test/TestOps.td      | 14 ++++++++++++++
 mlir/test/mlir-tblgen/enums-gen.td         | 20 ++++++++++++++++++++
 3 files changed, 41 insertions(+)

diff --git a/mlir/test/lib/Dialect/Test/TestAttrDefs.td b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
index ea527c962abe4..415b27aeda905 100644
--- a/mlir/test/lib/Dialect/Test/TestAttrDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
@@ -352,6 +352,13 @@ def TestArrayOfUglyAttrs : ArrayOfAttr<Test_Dialect, "ArrayOfUglyAttrs",
 def TestArrayOfInts : ArrayOfAttr<Test_Dialect, "ArrayOfInts",
     "array_of_ints", "int32_t">;
 
+def TestSimpleEnum8Attr : EnumAttr<Test_Dialect, TestSimpleEnum8, "simple_enum_8"> {
+  let assemblyFormat = "`` $value";
+}
+
+def TestSimpleEnum16Attr : EnumAttr<Test_Dialect, TestSimpleEnum16, "simple_enum_16"> {
+  let assemblyFormat = "`` $value";
+}
 // An array of enum attributes.
 def TestSimpleEnumAttr : EnumAttr<Test_Dialect, TestSimpleEnum, "simple_enum"> {
   let assemblyFormat = "`` $value";
diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index a82d5d831a3ce..bb7eacd7b5219 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -489,6 +489,20 @@ def OpWithBitEnumPropNamed : TEST_Op<"op_with_bit_enum_prop_named"> {
   let assemblyFormat = "$value1 ($value2^)? attr-dict";
 }
 
+def TestSimpleEnum8Prop : EnumProp<TestSimpleEnum8>;
+
+def OpWithSimpleEnum8Prop : TEST_Op<"op_with_simple_enum_8_prop"> {
+  let arguments = (ins TestSimpleEnum8Prop:$value);
+  let assemblyFormat = "$value attr-dict";
+}
+
+def TestSimpleEnum16Prop : EnumProp<TestSimpleEnum16>;
+
+def OpWithSimpleEnum16Prop : TEST_Op<"op_with_simple_enum_16_prop"> {
+  let arguments = (ins TestSimpleEnum16Prop:$value);
+  let assemblyFormat = "$value attr-dict";
+}
+
 //===----------------------------------------------------------------------===//
 // Test Bit Enum Attributes
 //===----------------------------------------------------------------------===//
diff --git a/mlir/test/mlir-tblgen/enums-gen.td b/mlir/test/mlir-tblgen/enums-gen.td
index 8489cff7c429d..a4c593a4daf82 100644
--- a/mlir/test/mlir-tblgen/enums-gen.td
+++ b/mlir/test/mlir-tblgen/enums-gen.td
@@ -58,6 +58,16 @@ def MyBitEnum: I32BitEnumAttr<"MyBitEnum", "An example bit enum",
 // DECL:     return p << '"' << valueStr << '"';
 // DECL:   return p << valueStr;
 
+// DECL: enum class MyI8Enum : uint8_t {
+// DECL:   a = 254,
+// DECL:   b = 255,
+// DECL: };
+
+// DECL: enum class MyI16Enum : uint16_t {
+// DECL:   a = 65534,
+// DECL:   b = 65535,
+// DECL: };
+
 // DEF-LABEL: std::string stringifyMyBitEnum
 // DEF: auto val = static_cast<uint32_t>
 // DEF: if (val == 0) return "none";
@@ -130,3 +140,13 @@ def MyNonQuotedPrintBitEnum
 // DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyNonQuotedPrintBitEnum value) {
 // DECL:   auto valueStr = stringifyEnum(value);
 // DECL-NEXT:   return p << valueStr;
+
+def MyI8Enum : I8Enum<"MyI8Enum", "Example I8 enum", [
+    I8EnumCase<"a", 254>,
+    I8EnumCase<"b", 255>
+  ]>;
+
+def MyI16Enum : I16Enum<"MyI16Enum", "Example I16 enum", [
+    I8EnumCase<"a", 65534>,
+    I8EnumCase<"b", 65535>
+  ]>;

>From 1a1554597b65562b1469516ef77650404c1be0a0 Mon Sep 17 00:00:00 2001
From: Robert Konicar <rkonicar at mail.muni.cz>
Date: Tue, 7 Apr 2026 20:51:15 +0200
Subject: [PATCH 3/4] fixup! [MLIR] Add I{8,16}Enum tablegen classes

---
 mlir/test/mlir-tblgen/enums-gen.td | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/test/mlir-tblgen/enums-gen.td b/mlir/test/mlir-tblgen/enums-gen.td
index a4c593a4daf82..5cb5692faa795 100644
--- a/mlir/test/mlir-tblgen/enums-gen.td
+++ b/mlir/test/mlir-tblgen/enums-gen.td
@@ -147,6 +147,6 @@ def MyI8Enum : I8Enum<"MyI8Enum", "Example I8 enum", [
   ]>;
 
 def MyI16Enum : I16Enum<"MyI16Enum", "Example I16 enum", [
-    I8EnumCase<"a", 65534>,
-    I8EnumCase<"b", 65535>
+    I16EnumCase<"a", 65534>,
+    I16EnumCase<"b", 65535>
   ]>;

>From d17cfc3325b06f082b35f18be6b0a364b49cda32 Mon Sep 17 00:00:00 2001
From: Robert Konicar <rkonicar at mail.muni.cz>
Date: Tue, 7 Apr 2026 21:27:11 +0200
Subject: [PATCH 4/4] fixup! [MLIR] Add I{8,16}Enum tablegen classes

---
 mlir/test/mlir-tblgen/enums-gen.td | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/mlir/test/mlir-tblgen/enums-gen.td b/mlir/test/mlir-tblgen/enums-gen.td
index 5cb5692faa795..cf66ad46ad24b 100644
--- a/mlir/test/mlir-tblgen/enums-gen.td
+++ b/mlir/test/mlir-tblgen/enums-gen.td
@@ -63,11 +63,23 @@ def MyBitEnum: I32BitEnumAttr<"MyBitEnum", "An example bit enum",
 // DECL:   b = 255,
 // DECL: };
 
+// DECL: ::std::optional<MyI8Enum> symbolizeMyI8Enum(uint8_t);
+
+// DECL: inline constexpr unsigned getMaxEnumValForMyI8Enum() {
+// DECL:   return 255;
+// DECL: }
+
 // DECL: enum class MyI16Enum : uint16_t {
 // DECL:   a = 65534,
 // DECL:   b = 65535,
 // DECL: };
 
+// DECL: ::std::optional<MyI16Enum> symbolizeMyI16Enum(uint16_t);
+
+// DECL: inline constexpr unsigned getMaxEnumValForMyI16Enum() {
+// DECL:   return 65535;
+// DECL: }
+
 // DEF-LABEL: std::string stringifyMyBitEnum
 // DEF: auto val = static_cast<uint32_t>
 // DEF: if (val == 0) return "none";



More information about the Mlir-commits mailing list