1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
|
//===- EnumsGen.cpp - MLIR enum utility generator -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// EnumsGen generates common utility functions for enums.
//
//===----------------------------------------------------------------------===//
#include "FormatGen.h"
#include "mlir/TableGen/Attribute.h"
#include "mlir/TableGen/Format.h"
#include "mlir/TableGen/GenInfo.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"
using llvm::formatv;
using llvm::isDigit;
using llvm::PrintFatalError;
using llvm::raw_ostream;
using llvm::Record;
using llvm::RecordKeeper;
using llvm::StringRef;
using mlir::tblgen::Attribute;
using mlir::tblgen::EnumAttr;
using mlir::tblgen::EnumAttrCase;
using mlir::tblgen::FmtContext;
using mlir::tblgen::tgfmt;
static std::string makeIdentifier(StringRef str) {
if (!str.empty() && isDigit(static_cast<unsigned char>(str.front()))) {
std::string newStr = std::string("_") + str.str();
return newStr;
}
return str.str();
}
static void emitEnumClass(const Record &enumDef, StringRef enumName,
StringRef underlyingType, StringRef description,
const std::vector<EnumAttrCase> &enumerants,
raw_ostream &os) {
os << "// " << description << "\n";
os << "enum class " << enumName;
if (!underlyingType.empty())
os << " : " << underlyingType;
os << " {\n";
for (const auto &enumerant : enumerants) {
auto symbol = makeIdentifier(enumerant.getSymbol());
auto value = enumerant.getValue();
if (value >= 0) {
os << formatv(" {0} = {1},\n", symbol, value);
} else {
os << formatv(" {0},\n", symbol);
}
}
os << "};\n\n";
}
static void emitParserPrinter(const EnumAttr &enumAttr, StringRef qualName,
StringRef cppNamespace, raw_ostream &os) {
if (enumAttr.getUnderlyingType().empty() ||
enumAttr.getConstBuilderTemplate().empty())
return;
auto cases = enumAttr.getAllCases();
// Check which cases shouldn't be printed using a keyword.
llvm::BitVector nonKeywordCases(cases.size());
for (auto [index, caseVal] : llvm::enumerate(cases))
if (!mlir::tblgen::canFormatStringAsKeyword(caseVal.getStr()))
nonKeywordCases.set(index);
// Generate the parser and the start of the printer for the enum.
const char *parsedAndPrinterStart = R"(
namespace mlir {
template <typename T, typename>
struct FieldParser;
template<>
struct FieldParser<{0}, {0}> {{
template <typename ParserT>
static FailureOr<{0}> parse(ParserT &parser) {{
// Parse the keyword/string containing the enum.
std::string enumKeyword;
auto loc = parser.getCurrentLocation();
if (failed(parser.parseOptionalKeywordOrString(&enumKeyword)))
return parser.emitError(loc, "expected keyword for {2}");
// Symbolize the keyword.
if (::std::optional<{0}> attr = {1}::symbolizeEnum<{0}>(enumKeyword))
return *attr;
return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
}
};
} // namespace mlir
namespace llvm {
inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, {0} value) {{
auto valueStr = stringifyEnum(value);
)";
os << formatv(parsedAndPrinterStart, qualName, cppNamespace,
enumAttr.getSummary());
// If all cases require a string, always wrap.
if (nonKeywordCases.all()) {
os << " return p << '\"' << valueStr << '\"';\n"
"}\n"
"} // namespace llvm\n";
return;
}
// If there are any cases that can't be used with a keyword, switch on the
// case value to determine when to print in the string form.
if (nonKeywordCases.any()) {
os << " switch (value) {\n";
for (auto it : llvm::enumerate(cases)) {
if (nonKeywordCases.test(it.index()))
continue;
StringRef symbol = it.value().getSymbol();
os << llvm::formatv(" case {0}::{1}:\n", qualName,
makeIdentifier(symbol));
}
os << " break;\n"
" default:\n"
" return p << '\"' << valueStr << '\"';\n"
" }\n";
// If this is a bit enum, conservatively print the string form if the value
// is not a power of two (i.e. not a single bit case) and not a known case.
} else if (enumAttr.isBitEnum()) {
// Process the known multi-bit cases that use valid keywords.
llvm::SmallVector<EnumAttrCase *> validMultiBitCases;
for (auto [index, caseVal] : llvm::enumerate(cases)) {
uint64_t value = caseVal.getValue();
if (value && !llvm::has_single_bit(value) && !nonKeywordCases.test(index))
validMultiBitCases.push_back(&caseVal);
}
if (!validMultiBitCases.empty()) {
os << " switch (value) {\n";
for (EnumAttrCase *caseVal : validMultiBitCases) {
StringRef symbol = caseVal->getSymbol();
os << llvm::formatv(" case {0}::{1}:\n", qualName,
llvm::isDigit(symbol.front()) ? ("_" + symbol)
: symbol);
}
os << " return p << valueStr;\n"
" default:\n"
" break;\n"
" }\n";
}
// All other multi-bit cases should be printed as strings.
os << formatv(" auto underlyingValue = "
"static_cast<std::make_unsigned_t<{0}>>(value);\n",
qualName);
os << " if (underlyingValue && !llvm::has_single_bit(underlyingValue))\n"
" return p << '\"' << valueStr << '\"';\n";
}
os << " return p << valueStr;\n"
"}\n"
"} // namespace llvm\n";
}
static void emitDenseMapInfo(StringRef qualName, std::string underlyingType,
StringRef cppNamespace, raw_ostream &os) {
if (underlyingType.empty())
underlyingType =
std::string(formatv("std::underlying_type_t<{0}>", qualName));
const char *const mapInfo = R"(
namespace llvm {
template<> struct DenseMapInfo<{0}> {{
using StorageInfo = ::llvm::DenseMapInfo<{1}>;
static inline {0} getEmptyKey() {{
return static_cast<{0}>(StorageInfo::getEmptyKey());
}
static inline {0} getTombstoneKey() {{
return static_cast<{0}>(StorageInfo::getTombstoneKey());
}
static unsigned getHashValue(const {0} &val) {{
return StorageInfo::getHashValue(static_cast<{1}>(val));
}
static bool isEqual(const {0} &lhs, const {0} &rhs) {{
return lhs == rhs;
}
};
})";
os << formatv(mapInfo, qualName, underlyingType);
os << "\n\n";
}
static void emitMaxValueFn(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef maxEnumValFnName = enumAttr.getMaxEnumValFnName();
auto enumerants = enumAttr.getAllCases();
unsigned maxEnumVal = 0;
for (const auto &enumerant : enumerants) {
int64_t value = enumerant.getValue();
// Avoid generating the max value function if there is an enumerant without
// explicit value.
if (value < 0)
return;
maxEnumVal = std::max(maxEnumVal, static_cast<unsigned>(value));
}
// Emit the function to return the max enum value
os << formatv("inline constexpr unsigned {0}() {{\n", maxEnumValFnName);
os << formatv(" return {0};\n", maxEnumVal);
os << "}\n\n";
}
// Returns the EnumAttrCase whose value is zero if exists; returns std::nullopt
// otherwise.
static std::optional<EnumAttrCase>
getAllBitsUnsetCase(llvm::ArrayRef<EnumAttrCase> cases) {
for (auto attrCase : cases) {
if (attrCase.getValue() == 0)
return attrCase;
}
return std::nullopt;
}
// Emits the following inline function for bit enums:
//
// inline constexpr <enum-type> operator|(<enum-type> a, <enum-type> b);
// inline constexpr <enum-type> operator&(<enum-type> a, <enum-type> b);
// inline constexpr <enum-type> operator^(<enum-type> a, <enum-type> b);
// inline constexpr <enum-type> operator~(<enum-type> bits);
// inline constexpr bool bitEnumContainsAll(<enum-type> bits, <enum-type> bit);
// inline constexpr bool bitEnumContainsAny(<enum-type> bits, <enum-type> bit);
// inline constexpr <enum-type> bitEnumClear(<enum-type> bits, <enum-type> bit);
// inline constexpr <enum-type> bitEnumSet(<enum-type> bits, <enum-type> bit,
// bool value=true);
static void emitOperators(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
std::string underlyingType = std::string(enumAttr.getUnderlyingType());
int64_t validBits = enumDef.getValueAsInt("validBits");
const char *const operators = R"(
inline constexpr {0} operator|({0} a, {0} b) {{
return static_cast<{0}>(static_cast<{1}>(a) | static_cast<{1}>(b));
}
inline constexpr {0} operator&({0} a, {0} b) {{
return static_cast<{0}>(static_cast<{1}>(a) & static_cast<{1}>(b));
}
inline constexpr {0} operator^({0} a, {0} b) {{
return static_cast<{0}>(static_cast<{1}>(a) ^ static_cast<{1}>(b));
}
inline constexpr {0} operator~({0} bits) {{
// Ensure only bits that can be present in the enum are set
return static_cast<{0}>(~static_cast<{1}>(bits) & static_cast<{1}>({2}u));
}
inline constexpr bool bitEnumContainsAll({0} bits, {0} bit) {{
return (bits & bit) == bit;
}
inline constexpr bool bitEnumContainsAny({0} bits, {0} bit) {{
return (static_cast<{1}>(bits) & static_cast<{1}>(bit)) != 0;
}
inline constexpr {0} bitEnumClear({0} bits, {0} bit) {{
return bits & ~bit;
}
inline constexpr {0} bitEnumSet({0} bits, {0} bit, /*optional*/bool value=true) {{
return value ? (bits | bit) : bitEnumClear(bits, bit);
}
)";
os << formatv(operators, enumName, underlyingType, validBits);
}
static void emitSymToStrFnForIntEnum(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
StringRef symToStrFnName = enumAttr.getSymbolToStringFnName();
StringRef symToStrFnRetType = enumAttr.getSymbolToStringFnRetType();
auto enumerants = enumAttr.getAllCases();
os << formatv("{2} {1}({0} val) {{\n", enumName, symToStrFnName,
symToStrFnRetType);
os << " switch (val) {\n";
for (const auto &enumerant : enumerants) {
auto symbol = enumerant.getSymbol();
auto str = enumerant.getStr();
os << formatv(" case {0}::{1}: return \"{2}\";\n", enumName,
makeIdentifier(symbol), str);
}
os << " }\n";
os << " return \"\";\n";
os << "}\n\n";
}
static void emitSymToStrFnForBitEnum(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
StringRef symToStrFnName = enumAttr.getSymbolToStringFnName();
StringRef symToStrFnRetType = enumAttr.getSymbolToStringFnRetType();
StringRef separator = enumDef.getValueAsString("separator");
auto enumerants = enumAttr.getAllCases();
auto allBitsUnsetCase = getAllBitsUnsetCase(enumerants);
os << formatv("{2} {1}({0} symbol) {{\n", enumName, symToStrFnName,
symToStrFnRetType);
os << formatv(" auto val = static_cast<{0}>(symbol);\n",
enumAttr.getUnderlyingType());
// If we have unknown bit set, return an empty string to signal errors.
int64_t validBits = enumDef.getValueAsInt("validBits");
os << formatv(" assert({0}u == ({0}u | val) && \"invalid bits set in bit "
"enum\");\n",
validBits);
if (allBitsUnsetCase) {
os << " // Special case for all bits unset.\n";
os << formatv(" if (val == 0) return \"{0}\";\n\n",
allBitsUnsetCase->getStr());
}
os << " ::llvm::SmallVector<::llvm::StringRef, 2> strs;\n";
// Add case string if the value has all case bits, and remove them to avoid
// printing again. Used only for groups, when printBitEnumPrimaryGroups is 1.
const char *const formatCompareRemove = R"(
if ({0}u == ({0}u & val)) {{
strs.push_back("{1}");
val &= ~static_cast<{2}>({0});
}
)";
// Add case string if the value has all case bits. Used for individual bit
// cases, and for groups when printBitEnumPrimaryGroups is 0.
const char *const formatCompare = R"(
if ({0}u == ({0}u & val))
strs.push_back("{1}");
)";
// Optionally elide bits that are members of groups that will also be printed
// for more concise output.
if (enumAttr.printBitEnumPrimaryGroups()) {
os << " // Print bit enum groups before individual bits\n";
// Emit comparisons for group bit cases in reverse tablegen declaration
// order, removing bits for groups with all bits present.
for (const auto &enumerant : llvm::reverse(enumerants)) {
if ((enumerant.getValue() != 0) &&
enumerant.getDef().isSubClassOf("BitEnumAttrCaseGroup")) {
os << formatv(formatCompareRemove, enumerant.getValue(),
enumerant.getStr(), enumAttr.getUnderlyingType());
}
}
// Emit comparisons for individual bit cases in tablegen declaration order.
for (const auto &enumerant : enumerants) {
if ((enumerant.getValue() != 0) &&
enumerant.getDef().isSubClassOf("BitEnumAttrCaseBit"))
os << formatv(formatCompare, enumerant.getValue(), enumerant.getStr());
}
} else {
// Emit comparisons for ALL nonzero cases (individual bits and groups) in
// tablegen declaration order.
for (const auto &enumerant : enumerants) {
if (enumerant.getValue() != 0)
os << formatv(formatCompare, enumerant.getValue(), enumerant.getStr());
}
}
os << formatv(" return ::llvm::join(strs, \"{0}\");\n", separator);
os << "}\n\n";
}
static void emitStrToSymFnForIntEnum(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
StringRef strToSymFnName = enumAttr.getStringToSymbolFnName();
auto enumerants = enumAttr.getAllCases();
os << formatv("::std::optional<{0}> {1}(::llvm::StringRef str) {{\n",
enumName, strToSymFnName);
os << formatv(" return ::llvm::StringSwitch<::std::optional<{0}>>(str)\n",
enumName);
for (const auto &enumerant : enumerants) {
auto symbol = enumerant.getSymbol();
auto str = enumerant.getStr();
os << formatv(" .Case(\"{1}\", {0}::{2})\n", enumName, str,
makeIdentifier(symbol));
}
os << " .Default(::std::nullopt);\n";
os << "}\n";
}
static void emitStrToSymFnForBitEnum(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
std::string underlyingType = std::string(enumAttr.getUnderlyingType());
StringRef strToSymFnName = enumAttr.getStringToSymbolFnName();
StringRef separator = enumDef.getValueAsString("separator");
StringRef separatorTrimmed = separator.trim();
auto enumerants = enumAttr.getAllCases();
auto allBitsUnsetCase = getAllBitsUnsetCase(enumerants);
os << formatv("::std::optional<{0}> {1}(::llvm::StringRef str) {{\n",
enumName, strToSymFnName);
if (allBitsUnsetCase) {
os << " // Special case for all bits unset.\n";
StringRef caseSymbol = allBitsUnsetCase->getSymbol();
os << formatv(" if (str == \"{1}\") return {0}::{2};\n\n", enumName,
allBitsUnsetCase->getStr(), makeIdentifier(caseSymbol));
}
// Split the string to get symbols for all the bits.
os << " ::llvm::SmallVector<::llvm::StringRef, 2> symbols;\n";
// Remove whitespace from the separator string when parsing.
os << formatv(" str.split(symbols, \"{0}\");\n\n", separatorTrimmed);
os << formatv(" {0} val = 0;\n", underlyingType);
os << " for (auto symbol : symbols) {\n";
// Convert each symbol to the bit ordinal and set the corresponding bit.
os << formatv(" auto bit = "
"llvm::StringSwitch<::std::optional<{0}>>(symbol.trim())\n",
underlyingType);
for (const auto &enumerant : enumerants) {
// Skip the special enumerant for None.
if (auto val = enumerant.getValue())
os.indent(6) << formatv(".Case(\"{0}\", {1})\n", enumerant.getStr(), val);
}
os.indent(6) << ".Default(::std::nullopt);\n";
os << " if (bit) { val |= *bit; } else { return ::std::nullopt; }\n";
os << " }\n";
os << formatv(" return static_cast<{0}>(val);\n", enumName);
os << "}\n\n";
}
static void emitUnderlyingToSymFnForIntEnum(const Record &enumDef,
raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
std::string underlyingType = std::string(enumAttr.getUnderlyingType());
StringRef underlyingToSymFnName = enumAttr.getUnderlyingToSymbolFnName();
auto enumerants = enumAttr.getAllCases();
// Avoid generating the underlying value to symbol conversion function if
// there is an enumerant without explicit value.
if (llvm::any_of(enumerants, [](EnumAttrCase enumerant) {
return enumerant.getValue() < 0;
}))
return;
os << formatv("::std::optional<{0}> {1}({2} value) {{\n", enumName,
underlyingToSymFnName,
underlyingType.empty() ? std::string("unsigned")
: underlyingType)
<< " switch (value) {\n";
for (const auto &enumerant : enumerants) {
auto symbol = enumerant.getSymbol();
auto value = enumerant.getValue();
os << formatv(" case {0}: return {1}::{2};\n", value, enumName,
makeIdentifier(symbol));
}
os << " default: return ::std::nullopt;\n"
<< " }\n"
<< "}\n\n";
}
static void emitSpecializedAttrDef(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
StringRef attrClassName = enumAttr.getSpecializedAttrClassName();
llvm::Record *baseAttrDef = enumAttr.getBaseAttrClass();
Attribute baseAttr(baseAttrDef);
// Emit classof method
os << formatv("bool {0}::classof(::mlir::Attribute attr) {{\n",
attrClassName);
mlir::tblgen::Pred baseAttrPred = baseAttr.getPredicate();
if (baseAttrPred.isNull())
PrintFatalError("ERROR: baseAttrClass for EnumAttr has no Predicate\n");
std::string condition = baseAttrPred.getCondition();
FmtContext verifyCtx;
verifyCtx.withSelf("attr");
os << tgfmt(" return $0;\n", /*ctx=*/nullptr, tgfmt(condition, &verifyCtx));
os << "}\n";
// Emit get method
os << formatv("{0} {0}::get(::mlir::MLIRContext *context, {1} val) {{\n",
attrClassName, enumName);
StringRef underlyingType = enumAttr.getUnderlyingType();
// Assuming that it is IntegerAttr constraint
int64_t bitwidth = 64;
if (baseAttrDef->getValue("valueType")) {
auto *valueTypeDef = baseAttrDef->getValueAsDef("valueType");
if (valueTypeDef->getValue("bitwidth"))
bitwidth = valueTypeDef->getValueAsInt("bitwidth");
}
os << formatv(" ::mlir::IntegerType intType = "
"::mlir::IntegerType::get(context, {0});\n",
bitwidth);
os << formatv(" ::mlir::IntegerAttr baseAttr = "
"::mlir::IntegerAttr::get(intType, static_cast<{0}>(val));\n",
underlyingType);
os << formatv(" return ::llvm::cast<{0}>(baseAttr);\n", attrClassName);
os << "}\n";
// Emit getValue method
os << formatv("{0} {1}::getValue() const {{\n", enumName, attrClassName);
os << formatv(" return static_cast<{0}>(::mlir::IntegerAttr::getInt());\n",
enumName);
os << "}\n";
}
static void emitUnderlyingToSymFnForBitEnum(const Record &enumDef,
raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
std::string underlyingType = std::string(enumAttr.getUnderlyingType());
StringRef underlyingToSymFnName = enumAttr.getUnderlyingToSymbolFnName();
auto enumerants = enumAttr.getAllCases();
auto allBitsUnsetCase = getAllBitsUnsetCase(enumerants);
os << formatv("::std::optional<{0}> {1}({2} value) {{\n", enumName,
underlyingToSymFnName, underlyingType);
if (allBitsUnsetCase) {
os << " // Special case for all bits unset.\n";
os << formatv(" if (value == 0) return {0}::{1};\n\n", enumName,
makeIdentifier(allBitsUnsetCase->getSymbol()));
}
int64_t validBits = enumDef.getValueAsInt("validBits");
os << formatv(" if (value & ~static_cast<{0}>({1}u)) return std::nullopt;\n",
underlyingType, validBits);
os << formatv(" return static_cast<{0}>(value);\n", enumName);
os << "}\n";
}
static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef enumName = enumAttr.getEnumClassName();
StringRef cppNamespace = enumAttr.getCppNamespace();
std::string underlyingType = std::string(enumAttr.getUnderlyingType());
StringRef description = enumAttr.getSummary();
StringRef strToSymFnName = enumAttr.getStringToSymbolFnName();
StringRef symToStrFnName = enumAttr.getSymbolToStringFnName();
StringRef symToStrFnRetType = enumAttr.getSymbolToStringFnRetType();
StringRef underlyingToSymFnName = enumAttr.getUnderlyingToSymbolFnName();
auto enumerants = enumAttr.getAllCases();
llvm::SmallVector<StringRef, 2> namespaces;
llvm::SplitString(cppNamespace, namespaces, "::");
for (auto ns : namespaces)
os << "namespace " << ns << " {\n";
// Emit the enum class definition
emitEnumClass(enumDef, enumName, underlyingType, description, enumerants, os);
// Emit conversion function declarations
if (llvm::all_of(enumerants, [](EnumAttrCase enumerant) {
return enumerant.getValue() >= 0;
})) {
os << formatv(
"::std::optional<{0}> {1}({2});\n", enumName, underlyingToSymFnName,
underlyingType.empty() ? std::string("unsigned") : underlyingType);
}
os << formatv("{2} {1}({0});\n", enumName, symToStrFnName, symToStrFnRetType);
os << formatv("::std::optional<{0}> {1}(::llvm::StringRef);\n", enumName,
strToSymFnName);
if (enumAttr.isBitEnum()) {
emitOperators(enumDef, os);
} else {
emitMaxValueFn(enumDef, os);
}
// Generate a generic `stringifyEnum` function that forwards to the method
// specified by the user.
const char *const stringifyEnumStr = R"(
inline {0} stringifyEnum({1} enumValue) {{
return {2}(enumValue);
}
)";
os << formatv(stringifyEnumStr, symToStrFnRetType, enumName, symToStrFnName);
// Generate a generic `symbolizeEnum` function that forwards to the method
// specified by the user.
const char *const symbolizeEnumStr = R"(
template <typename EnumType>
::std::optional<EnumType> symbolizeEnum(::llvm::StringRef);
template <>
inline ::std::optional<{0}> symbolizeEnum<{0}>(::llvm::StringRef str) {
return {1}(str);
}
)";
os << formatv(symbolizeEnumStr, enumName, strToSymFnName);
const char *const attrClassDecl = R"(
class {1} : public ::mlir::{2} {
public:
using ValueType = {0};
using ::mlir::{2}::{2};
static bool classof(::mlir::Attribute attr);
static {1} get(::mlir::MLIRContext *context, {0} val);
{0} getValue() const;
};
)";
if (enumAttr.genSpecializedAttr()) {
StringRef attrClassName = enumAttr.getSpecializedAttrClassName();
StringRef baseAttrClassName = "IntegerAttr";
os << formatv(attrClassDecl, enumName, attrClassName, baseAttrClassName);
}
for (auto ns : llvm::reverse(namespaces))
os << "} // namespace " << ns << "\n";
// Generate a generic parser and printer for the enum.
std::string qualName =
std::string(formatv("{0}::{1}", cppNamespace, enumName));
emitParserPrinter(enumAttr, qualName, cppNamespace, os);
// Emit DenseMapInfo for this enum class
emitDenseMapInfo(qualName, underlyingType, cppNamespace, os);
}
static bool emitEnumDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
llvm::emitSourceFileHeader("Enum Utility Declarations", os);
auto defs = recordKeeper.getAllDerivedDefinitionsIfDefined("EnumAttrInfo");
for (const auto *def : defs)
emitEnumDecl(*def, os);
return false;
}
static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
EnumAttr enumAttr(enumDef);
StringRef cppNamespace = enumAttr.getCppNamespace();
llvm::SmallVector<StringRef, 2> namespaces;
llvm::SplitString(cppNamespace, namespaces, "::");
for (auto ns : namespaces)
os << "namespace " << ns << " {\n";
if (enumAttr.isBitEnum()) {
emitSymToStrFnForBitEnum(enumDef, os);
emitStrToSymFnForBitEnum(enumDef, os);
emitUnderlyingToSymFnForBitEnum(enumDef, os);
} else {
emitSymToStrFnForIntEnum(enumDef, os);
emitStrToSymFnForIntEnum(enumDef, os);
emitUnderlyingToSymFnForIntEnum(enumDef, os);
}
if (enumAttr.genSpecializedAttr())
emitSpecializedAttrDef(enumDef, os);
for (auto ns : llvm::reverse(namespaces))
os << "} // namespace " << ns << "\n";
os << "\n";
}
static bool emitEnumDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
llvm::emitSourceFileHeader("Enum Utility Definitions", os);
auto defs = recordKeeper.getAllDerivedDefinitionsIfDefined("EnumAttrInfo");
for (const auto *def : defs)
emitEnumDef(*def, os);
return false;
}
// Registers the enum utility generator to mlir-tblgen.
static mlir::GenRegistration
genEnumDecls("gen-enum-decls", "Generate enum utility declarations",
[](const RecordKeeper &records, raw_ostream &os) {
return emitEnumDecls(records, os);
});
// Registers the enum utility generator to mlir-tblgen.
static mlir::GenRegistration
genEnumDefs("gen-enum-defs", "Generate enum utility definitions",
[](const RecordKeeper &records, raw_ostream &os) {
return emitEnumDefs(records, os);
});
|