Skip to content

[TableGen] Use getValue instead of getInt for enum attributes #144030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions mlir/include/mlir/IR/EnumAttr.td
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ class EnumCase<string sym, int intVal, string strVal, int widthVal> {
class IntEnumAttrCaseBase<I intType, string sym, string strVal, int intVal> :
EnumCase<sym, intVal, strVal, intType.bitwidth>,
SignlessIntegerAttrBase<intType, "case " # strVal> {
let predicate =
CPred<"::llvm::cast<::mlir::IntegerAttr>($_self).getInt() == " # intVal>;
let predicate = CPred<[{
::llvm::cast<::mlir::IntegerAttr>($_self).getValue().eq(::llvm::APInt(}]
# intType.bitwidth # ", "
# intVal #
"))">;
}

// Cases of integer enums with a specific type. By default, the string
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/IR/attribute.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ func.func @allowed_cases_pass() {
%0 = "test.i32_enum_attr"() {attr = 5: i32} : () -> i32
// CHECK: test.i32_enum_attr
%1 = "test.i32_enum_attr"() {attr = 10: i32} : () -> i32
// CHECK: test.i32_enum_attr
%2 = "test.i32_enum_attr"() {attr = 2147483648: i32} : () -> i32
// CHECK: test.i32_enum_attr
%3 = "test.i32_enum_attr"() {attr = 4294967295: i32} : () -> i32
return
}

Expand Down
8 changes: 6 additions & 2 deletions mlir/test/lib/Dialect/Test/TestEnumDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ include "mlir/IR/EnumAttr.td"

def I32Case5: I32EnumAttrCase<"case5", 5>;
def I32Case10: I32EnumAttrCase<"case10", 10>;
def I32CaseSignedMaxPlusOne
: I32EnumAttrCase<"caseSignedMaxPlusOne", 2147483648>;
def I32CaseUnsignedMax : I32EnumAttrCase<"caseUnsignedMax", 4294967295>;

def SomeI32Enum: I32EnumAttr<
"SomeI32Enum", "", [I32Case5, I32Case10]>;
def SomeI32Enum : I32EnumAttr<"SomeI32Enum", "",
[I32Case5, I32Case10, I32CaseSignedMaxPlusOne,
I32CaseUnsignedMax]>;

def I64Case5: I64EnumAttrCase<"case5", 5>;
def I64Case10: I64EnumAttrCase<"case10", 10>;
Expand Down
6 changes: 4 additions & 2 deletions mlir/tools/mlir-tblgen/EnumsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ static void emitSpecializedAttrDef(const Record &enumDef, raw_ostream &os) {

os << formatv("{0} {1}::getValue() const {{\n", enumName, attrClassName);

os << formatv(" return static_cast<{0}>(::mlir::IntegerAttr::getInt());\n",
enumName);
os << formatv(
" return "
"static_cast<{0}>(::mlir::IntegerAttr::getValue().getZExtValue());\n",
enumName);

os << "}\n";
}
Expand Down
Loading