Skip to content

Commit be26be6

Browse files
committed
[mlir] Separate enum information from attributes, improve EnumProp
This commit pulls apart the inherent attribute dependence of classes like EnumAttrInfo and EnumAttrCase, factoring them out into simpler EnumCase and EnumInfo variants. This allows specifying the cases of an enum without needing to make the cases, or the EnumInfo itself, a subclass of SignlessIntegerAttrBase. The existing classes are retained as subclasses of the new ones, both for backwards compatibility and to allow attribute-specific information. In addition, the new BitEnum class changes its default printer/parser behavior: cases when multiple keywords appear, like having both nuw and nsw in overflow flags, will no longer be quoted by the operator<<, and the FieldParser instance will now expect multiple keywords. All instances of BitEnumAttr retain the old behavior. Finally, this commit improves the `EnumProp` class, causing it to wrap around an `EnumInfo` just like `EnumAttr` does, and introducing logic for converting to/from attributes and bitcode (while using autogenerated parsers and printers). The commit also provides the `NamedEnumProp` class for the common pattern of `name<enum_value>` in MLIR code, and provides `EnumPropWithAttrForm` to allow upgrading enum attributes to properties without breaking the generic form of assembly. (Sadly, bitcode auto-upgrade is hampered by the lack of the ability to optionally parse an attribute.)
1 parent d0d33d2 commit be26be6

28 files changed

+1197
-608
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,16 @@ def DISubprogramFlags : I32BitEnumAttr<
485485
// IntegerOverflowFlags
486486
//===----------------------------------------------------------------------===//
487487

488-
def IOFnone : I32BitEnumAttrCaseNone<"none">;
489-
def IOFnsw : I32BitEnumAttrCaseBit<"nsw", 0>;
490-
def IOFnuw : I32BitEnumAttrCaseBit<"nuw", 1>;
488+
def IOFnone : I32BitEnumCaseNone<"none">;
489+
def IOFnsw : I32BitEnumCaseBit<"nsw", 0>;
490+
def IOFnuw : I32BitEnumCaseBit<"nuw", 1>;
491491

492-
def IntegerOverflowFlags : I32BitEnumAttr<
492+
def IntegerOverflowFlags : I32BitEnum<
493493
"IntegerOverflowFlags",
494494
"LLVM integer overflow flags",
495495
[IOFnone, IOFnsw, IOFnuw]> {
496496
let separator = ", ";
497497
let cppNamespace = "::mlir::LLVM";
498-
let genSpecializedAttr = 0;
499498
let printBitEnumPrimaryGroups = 1;
500499
}
501500

@@ -504,6 +503,11 @@ def LLVM_IntegerOverflowFlagsAttr :
504503
let assemblyFormat = "`<` $value `>`";
505504
}
506505

506+
def LLVM_IntegerOverflowFlagsProp :
507+
NamedEnumPropWithAttrForm<IntegerOverflowFlags, "overflow", LLVM_IntegerOverflowFlagsAttr> {
508+
let defaultValue = enum.cppType # "::" # "none";
509+
}
510+
507511
//===----------------------------------------------------------------------===//
508512
// FastmathFlags
509513
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class LLVM_IntArithmeticOpWithOverflowFlag<string mnemonic, string instName,
6060
list<Trait> traits = []> :
6161
LLVM_ArithmeticOpBase<AnySignlessInteger, mnemonic, instName,
6262
!listconcat([DeclareOpInterfaceMethods<IntegerOverflowFlagsInterface>], traits)> {
63-
dag iofArg = (ins EnumProp<"IntegerOverflowFlags", "", "IntegerOverflowFlags::none">:$overflowFlags);
63+
dag iofArg = (ins LLVM_IntegerOverflowFlagsProp:$overflowFlags);
6464
let arguments = !con(commonArgs, iofArg);
6565

6666
string mlirBuilder = [{
@@ -69,7 +69,7 @@ class LLVM_IntArithmeticOpWithOverflowFlag<string mnemonic, string instName,
6969
$res = op;
7070
}];
7171
let assemblyFormat = [{
72-
$lhs `,` $rhs `` custom<OverflowFlags>($overflowFlags) attr-dict `:` type($res)
72+
$lhs `,` $rhs ($overflowFlags^)? attr-dict `:` type($res)
7373
}];
7474
string llvmBuilder =
7575
"$res = builder.Create" # instName #
@@ -563,10 +563,10 @@ class LLVM_CastOpWithOverflowFlag<string mnemonic, string instName, Type type,
563563
Type resultType, list<Trait> traits = []> :
564564
LLVM_Op<mnemonic, !listconcat([Pure], [DeclareOpInterfaceMethods<IntegerOverflowFlagsInterface>], traits)>,
565565
LLVM_Builder<"$res = builder.Create" # instName # "($arg, $_resultType, /*Name=*/\"\", op.hasNoUnsignedWrap(), op.hasNoSignedWrap());"> {
566-
let arguments = (ins type:$arg, EnumProp<"IntegerOverflowFlags", "", "IntegerOverflowFlags::none">:$overflowFlags);
566+
let arguments = (ins type:$arg, LLVM_IntegerOverflowFlagsProp:$overflowFlags);
567567
let results = (outs resultType:$res);
568568
let builders = [LLVM_OneResultOpBuilder];
569-
let assemblyFormat = "$arg `` custom<OverflowFlags>($overflowFlags) attr-dict `:` type($arg) `to` type($res)";
569+
let assemblyFormat = "$arg ($overflowFlags^)? attr-dict `:` type($arg) `to` type($res)";
570570
string llvmInstName = instName;
571571
string mlirBuilder = [{
572572
auto op = $_builder.create<$_qualCppClassName>(

0 commit comments

Comments
 (0)