Skip to content

Commit 405af8e

Browse files
committed
[mlir] Make bit enum operators constexpr
This allows using the | operator on the values of enum attributes in complie-time constants. Reviewed By: antiagainst Differential Revision: https://reviews.llvm.org/D133159
1 parent 817de30 commit 405af8e

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

mlir/tools/mlir-tblgen/EnumsGen.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,27 @@ getAllBitsUnsetCase(llvm::ArrayRef<EnumAttrCase> cases) {
134134

135135
// Emits the following inline function for bit enums:
136136
//
137-
// inline <enum-type> operator|(<enum-type> a, <enum-type> b);
138-
// inline <enum-type> operator&(<enum-type> a, <enum-type> b);
139-
// inline <enum-type> bitEnumContains(<enum-type> a, <enum-type> b);
137+
// inline constexpr <enum-type> operator|(<enum-type> a, <enum-type> b);
138+
// inline constexpr <enum-type> operator&(<enum-type> a, <enum-type> b);
139+
// inline constexpr bool bitEnumContains(<enum-type> a, <enum-type> b);
140140
static void emitOperators(const Record &enumDef, raw_ostream &os) {
141141
EnumAttr enumAttr(enumDef);
142142
StringRef enumName = enumAttr.getEnumClassName();
143143
std::string underlyingType = std::string(enumAttr.getUnderlyingType());
144-
os << formatv("inline {0} operator|({0} lhs, {0} rhs) {{\n", enumName)
144+
os << formatv("inline constexpr {0} operator|({0} lhs, {0} rhs) {{\n",
145+
enumName)
145146
<< formatv(" return static_cast<{0}>("
146147
"static_cast<{1}>(lhs) | static_cast<{1}>(rhs));\n",
147148
enumName, underlyingType)
148149
<< "}\n";
149-
os << formatv("inline {0} operator&({0} lhs, {0} rhs) {{\n", enumName)
150+
os << formatv("inline constexpr {0} operator&({0} lhs, {0} rhs) {{\n",
151+
enumName)
150152
<< formatv(" return static_cast<{0}>("
151153
"static_cast<{1}>(lhs) & static_cast<{1}>(rhs));\n",
152154
enumName, underlyingType)
153155
<< "}\n";
154156
os << formatv(
155-
"inline bool bitEnumContains({0} bits, {0} bit) {{\n"
157+
"inline constexpr bool bitEnumContains({0} bits, {0} bit) {{\n"
156158
" return (static_cast<{1}>(bits) & static_cast<{1}>(bit)) != 0;\n",
157159
enumName, underlyingType)
158160
<< "}\n";

0 commit comments

Comments
 (0)