diff --git a/mlir/include/mlir/TableGen/Constraint.h b/mlir/include/mlir/TableGen/Constraint.h --- a/mlir/include/mlir/TableGen/Constraint.h +++ b/mlir/include/mlir/TableGen/Constraint.h @@ -59,6 +59,12 @@ /// `Optional<>`/`Variadic<>` type constraints). StringRef getDefName() const; + /// Returns a unique name for the TablGen def of this constraint. This is + /// generally just the name of the def, but in some cases where the current + /// def is anonymous, the name of the base def is attached (to provide more + /// context on the def). + std::string getUniqueDefName() const; + Kind getKind() const { return kind; } protected: @@ -66,6 +72,9 @@ const llvm::Record *def; private: + /// Return the name of the base def if there is one, or None otherwise. + Optional getBaseDefName() const; + // What kind of constraint this is. Kind kind; }; diff --git a/mlir/include/mlir/Tools/PDLL/ODS/Constraint.h b/mlir/include/mlir/Tools/PDLL/ODS/Constraint.h --- a/mlir/include/mlir/Tools/PDLL/ODS/Constraint.h +++ b/mlir/include/mlir/Tools/PDLL/ODS/Constraint.h @@ -31,9 +31,15 @@ /// This class represents a generic ODS constraint. class Constraint { public: - /// Return the name of this constraint. + /// Return the unique name of this constraint. StringRef getName() const { return name; } + /// Return the demangled name of this constraint. This tries to strip out bits + /// of the name that are purely for uniquing, and show the underlying name. As + /// such, this name does guarantee uniqueness and should only be used for + /// logging or other lossy friendly "pretty" output. + StringRef getDemangledName() const; + /// Return the summary of this constraint. StringRef getSummary() const { return summary; } diff --git a/mlir/lib/TableGen/Constraint.cpp b/mlir/lib/TableGen/Constraint.cpp --- a/mlir/lib/TableGen/Constraint.cpp +++ b/mlir/lib/TableGen/Constraint.cpp @@ -58,25 +58,48 @@ } StringRef Constraint::getDefName() const { + if (Optional baseDefName = getBaseDefName()) + return *baseDefName; + return def->getName(); +} + +std::string Constraint::getUniqueDefName() const { + std::string defName = def->getName().str(); + + // Non-anonymous classes already have a unique name from the def. + if (!def->isAnonymous()) + return defName; + + // Otherwise, this is an anonymous class. In these cases we still use the def + // name, but we also try attach the name of the base def when present to make + // the name more obvious. + if (Optional baseDefName = getBaseDefName()) + return (*baseDefName + "(" + defName + ")").str(); + return defName; +} + +Optional Constraint::getBaseDefName() const { // Functor used to check a base def in the case where the current def is // anonymous. - auto checkBaseDefFn = [&](StringRef baseName) { - if (const auto *init = dyn_cast(def->getValueInit(baseName))) - return Constraint(init->getDef(), kind).getDefName(); - return def->getName(); + auto checkBaseDefFn = [&](StringRef baseName) -> Optional { + if (const auto *defValue = def->getValue(baseName)) { + if (const auto *defInit = dyn_cast(defValue->getValue())) + return Constraint(defInit->getDef(), kind).getDefName(); + } + return llvm::None; }; switch (kind) { case CK_Attr: if (def->isAnonymous()) return checkBaseDefFn("baseAttr"); - return def->getName(); + return llvm::None; case CK_Type: if (def->isAnonymous()) return checkBaseDefFn("baseType"); - return def->getName(); + return llvm::None; default: - return def->getName(); + return llvm::None; } } diff --git a/mlir/lib/Tools/PDLL/ODS/CMakeLists.txt b/mlir/lib/Tools/PDLL/ODS/CMakeLists.txt --- a/mlir/lib/Tools/PDLL/ODS/CMakeLists.txt +++ b/mlir/lib/Tools/PDLL/ODS/CMakeLists.txt @@ -1,4 +1,5 @@ add_mlir_library(MLIRPDLLODS + Constraint.cpp Context.cpp Dialect.cpp Operation.cpp diff --git a/mlir/lib/Tools/PDLL/ODS/Constraint.cpp b/mlir/lib/Tools/PDLL/ODS/Constraint.cpp new file mode 100644 --- /dev/null +++ b/mlir/lib/Tools/PDLL/ODS/Constraint.cpp @@ -0,0 +1,26 @@ +//===- Constraint.cpp -----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "mlir/Tools/PDLL/ODS/Constraint.h" + +using namespace mlir; +using namespace mlir::pdll::ods; + +//===----------------------------------------------------------------------===// +// Constraint +//===----------------------------------------------------------------------===// + +StringRef Constraint::getDemangledName() const { + StringRef demangledName = name; + + // Drop the "anonymous" suffix if present. + size_t anonymousSuffix = demangledName.find("(anonymous_"); + if (anonymousSuffix != StringRef::npos) + demangledName = demangledName.take_front(anonymousSuffix); + return demangledName; +} diff --git a/mlir/lib/Tools/PDLL/ODS/Context.cpp b/mlir/lib/Tools/PDLL/ODS/Context.cpp --- a/mlir/lib/Tools/PDLL/ODS/Context.cpp +++ b/mlir/lib/Tools/PDLL/ODS/Context.cpp @@ -120,7 +120,7 @@ auto kind = attr.isOptional() ? VariableLengthKind::Optional : VariableLengthKind::Single; - printVariableLengthCst(attr.getConstraint().getName(), kind); + printVariableLengthCst(attr.getConstraint().getDemangledName(), kind); }); os << " }\n"; } @@ -132,7 +132,7 @@ llvm::interleaveComma( operands, os, [&](const OperandOrResult &operand) { os << operand.getName() << " : "; - printVariableLengthCst(operand.getConstraint().getName(), + printVariableLengthCst(operand.getConstraint().getDemangledName(), operand.getVariableLengthKind()); }); os << " }\n"; @@ -144,7 +144,7 @@ printer.startLine() << "Results { "; llvm::interleaveComma(results, os, [&](const OperandOrResult &result) { os << result.getName() << " : "; - printVariableLengthCst(result.getConstraint().getName(), + printVariableLengthCst(result.getConstraint().getDemangledName(), result.getVariableLengthKind()); }); os << " }\n"; @@ -155,7 +155,8 @@ printer.objectEnd(); } for (const AttributeConstraint *cst : sortMapByName(attributeConstraints)) { - printer.startLine() << "AttributeConstraint `" << cst->getName() << "` {\n"; + printer.startLine() << "AttributeConstraint `" << cst->getDemangledName() + << "` {\n"; printer.indent(); printer.startLine() << "Summary: " << cst->getSummary() << "\n"; @@ -163,7 +164,8 @@ printer.objectEnd(); } for (const TypeConstraint *cst : sortMapByName(typeConstraints)) { - printer.startLine() << "TypeConstraint `" << cst->getName() << "` {\n"; + printer.startLine() << "TypeConstraint `" << cst->getDemangledName() + << "` {\n"; printer.indent(); printer.startLine() << "Summary: " << cst->getSummary() << "\n"; diff --git a/mlir/lib/Tools/PDLL/Parser/Parser.cpp b/mlir/lib/Tools/PDLL/Parser/Parser.cpp --- a/mlir/lib/Tools/PDLL/Parser/Parser.cpp +++ b/mlir/lib/Tools/PDLL/Parser/Parser.cpp @@ -774,7 +774,7 @@ ods::Context &odsContext = ctx.getODSContext(); auto addTypeConstraint = [&](const tblgen::NamedTypeConstraint &cst) -> const ods::TypeConstraint & { - return odsContext.insertTypeConstraint(cst.constraint.getDefName(), + return odsContext.insertTypeConstraint(cst.constraint.getUniqueDefName(), cst.constraint.getSummary(), cst.constraint.getCPPClassName()); }; @@ -800,7 +800,7 @@ for (const tblgen::NamedAttribute &attr : op.getAttributes()) { odsOp->appendAttribute( attr.name, attr.attr.isOptional(), - odsContext.insertAttributeConstraint(attr.attr.getAttrDefName(), + odsContext.insertAttributeConstraint(attr.attr.getUniqueDefName(), attr.attr.getSummary(), attr.attr.getStorageType())); } @@ -891,8 +891,8 @@ std::string codeBlock = tblgen::tgfmt(constraint.getConditionTemplate(), &fmtContext); - return createODSNativePDLLConstraintDecl(constraint.getDefName(), - codeBlock, loc, type); + return createODSNativePDLLConstraintDecl( + constraint.getUniqueDefName(), codeBlock, loc, type); } //===----------------------------------------------------------------------===// diff --git a/mlir/test/mlir-pdll/Parser/include/ops.td b/mlir/test/mlir-pdll/Parser/include/ops.td --- a/mlir/test/mlir-pdll/Parser/include/ops.td +++ b/mlir/test/mlir-pdll/Parser/include/ops.td @@ -7,7 +7,11 @@ def OpAllEmpty : Op; def OpAllSingle : Op { - let arguments = (ins I64:$operand, I64Attr:$attr); + let arguments = (ins + I64:$operand, + I64Attr:$attr, + Confined:$nonNegativeAttr + ); let results = (outs I64:$result); } diff --git a/mlir/test/mlir-pdll/Parser/include_td.pdll b/mlir/test/mlir-pdll/Parser/include_td.pdll --- a/mlir/test/mlir-pdll/Parser/include_td.pdll +++ b/mlir/test/mlir-pdll/Parser/include_td.pdll @@ -12,7 +12,7 @@ // CHECK-NEXT: } // CHECK: Operation `test.all_single` { -// CHECK-NEXT: Attributes { attr : I64Attr } +// CHECK-NEXT: Attributes { attr : I64Attr, nonNegativeAttr : I64Attr } // CHECK-NEXT: Operands { operand : I64 } // CHECK-NEXT: Results { result : I64 } // CHECK-NEXT: }