https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/238153
Register parsed TableGen source buffers before processing imported records. Format the predicate before wrapping it in the generated success return, and propagate failure without appending an invalid declaration. Adapt shared formatting messages to PDLL diagnostics. Anchor the primary at the import in the main document so LSP publishes it, and retain the constraint definition, multiclass instantiations, and nested PDLL import locations as notes. Formatting errors return through the parser and leave the language server alive for subsequent edits. Deprecate pointer-taking factories, lazy object types, PHKind shims, and the unchecked summary helper while retaining compatibility behavior. Migrate successful format tests and suppress warnings locally around intentional legacy tests. Assisted-by: Codex >From 883cb94460c663383c1ce5a776d584b4c793277e Mon Sep 17 00:00:00 2001 From: Mehdi Amini <[email protected]> Date: Sun, 1 Nov 2026 05:24:30 -0800 Subject: [PATCH] [mlir][tblgen] Finish checked formatting migration through PDLL Register parsed TableGen source buffers before processing imported records. Format the predicate before wrapping it in the generated success return, and propagate failure without appending an invalid declaration. Adapt shared formatting messages to PDLL diagnostics. Anchor the primary at the import in the main document so LSP publishes it, and retain the constraint definition, multiclass instantiations, and nested PDLL import locations as notes. Formatting errors return through the parser and leave the language server alive for subsequent edits. Deprecate pointer-taking factories, lazy object types, PHKind shims, and the unchecked summary helper while retaining compatibility behavior. Migrate successful format tests and suppress warnings locally around intentional legacy tests. Document recoverable import errors in the PDLL reference. Validation: rebuild mlir-tblgen, TableGen units, mlir-pdll, PDLL LSP, mlir-opt, and FileCheck. All 82 unit tests and 110 TableGen/PDLL/LSP lit tests pass. Compile checks verify checked API availability and legacy deprecation diagnostics. Cover direct and nested imports, fail-fast handling, escaped templates, multiclass notes, source URIs, and LSP recovery. Assisted-by: Codex --- mlir/docs/PDLL.md | 7 + mlir/include/mlir/TableGen/CodeGenHelpers.h | 2 + mlir/include/mlir/TableGen/Format.h | 23 +++- mlir/lib/TableGen/CodeGenHelpers.cpp | 4 + mlir/lib/TableGen/Format.cpp | 7 + mlir/lib/Tools/PDLL/Parser/Parser.cpp | 123 ++++++++++++++---- .../mlir-pdll-lsp-server/format-errors.test | 79 +++++++++++ mlir/test/mlir-pdll/Parser/format-errors.pdll | 78 +++++++++++ mlir/unittests/TableGen/FormatTest.cpp | 99 +++++++++++--- 9 files changed, 371 insertions(+), 51 deletions(-) create mode 100644 mlir/test/mlir-pdll-lsp-server/format-errors.test create mode 100644 mlir/test/mlir-pdll/Parser/format-errors.pdll diff --git a/mlir/docs/PDLL.md b/mlir/docs/PDLL.md index c6e352fd647daf..a3ffa567a5e554 100644 --- a/mlir/docs/PDLL.md +++ b/mlir/docs/PDLL.md @@ -263,6 +263,13 @@ them implicitly accessible within PDLL. This is important, as ODS information allows for certain PDLL constructs, such as the [`operation` expression](#operation), to become much more powerful. +Imported attribute and type constraint predicates can substitute `$_self` with +the constraint's input. If a predicate refers to an unavailable substitution, +the import fails with a diagnostic listing the failed template and available +bindings. The error is anchored at the import in the main PDLL document, with +notes identifying the constraint definition and any nested imports. Formatting +failures return through the parser without terminating the host process. + ### Patterns In any pattern descriptor language, pattern definition is at the core. In PDLL, diff --git a/mlir/include/mlir/TableGen/CodeGenHelpers.h b/mlir/include/mlir/TableGen/CodeGenHelpers.h index a604d101523a4d..b7074e21fff190 100644 --- a/mlir/include/mlir/TableGen/CodeGenHelpers.h +++ b/mlir/include/mlir/TableGen/CodeGenHelpers.h @@ -296,6 +296,8 @@ std::string stringify(T &&t) { /// into: /// "failed to verify " << bar /// where bar is the result of formatting "foo" with ctx during code generation. +[[deprecated("use buildErrorStreamingStringChecked or " + "buildErrorStreamingStringOrFatal")]] std::string buildErrorStreamingString( StringRef message, const FmtContext &ctx, ErrorStreamType errorStreamType = ErrorStreamType::InString); diff --git a/mlir/include/mlir/TableGen/Format.h b/mlir/include/mlir/TableGen/Format.h index 5ab0ef179ebcac..ce1f67e91cb934 100644 --- a/mlir/include/mlir/TableGen/Format.h +++ b/mlir/include/mlir/TableGen/Format.h @@ -17,6 +17,7 @@ #include "mlir/Support/LLVM.h" #include "llvm/ADT/StringMap.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/SMLoc.h" @@ -114,6 +115,9 @@ SmallVector<FmtDiagnostic> getFmtDiagnostics(const FmtError &error); /// through unchanged, including non-formatting leaves in an ErrorList. llvm::Error addFmtOrigin(llvm::Error error, const FmtOrigin &origin); +// Compatibility types remain implementation building blocks until removal. +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH + /// Format context containing substitutions for special placeholders. /// /// Builtin placeholders have convenience setters and share storage and lookup @@ -128,7 +132,7 @@ llvm::Error addFmtOrigin(llvm::Error error, const FmtOrigin &origin); class FmtContext { public: // Placeholder kinds - enum class PHKind : char { + enum class [[deprecated("use named string lookup")]] PHKind : char { None, Custom, // For custom placeholders Builder, // For the $_builder placeholder @@ -147,9 +151,11 @@ class FmtContext { FmtContext &withBuilder(Twine subst); FmtContext &withSelf(Twine subst); + [[deprecated("use getSubstFor(StringRef)")]] std::optional<StringRef> getSubstFor(PHKind placeholder) const; std::optional<StringRef> getSubstFor(StringRef placeholder) const; + [[deprecated("use named string lookup")]] static PHKind getPlaceHolderKind(StringRef str); /// Sorted, owned snapshots, including builtin bindings and empty @@ -164,7 +170,8 @@ class FmtContext { /// Struct representing a replacement segment for the formatted string. It can /// be a segment of the formatting template (for `Literal`) or a replacement /// parameter (for `PositionalPH`, `PositionalRangePH` and `SpecialPH`). -struct FmtReplacement { +struct [[deprecated("use tgfmt with a context reference or tgfmtOrFatal")]] +FmtReplacement { enum class Type { Empty, Literal, @@ -193,7 +200,8 @@ struct FmtReplacement { static constexpr size_t kUnset = -1; }; -class FmtObjectBase { +class [[deprecated("use tgfmt with a context reference or tgfmtOrFatal")]] +FmtObjectBase { private: static std::pair<FmtReplacement, StringRef> splitFmtSegment(StringRef fmt); static std::vector<FmtReplacement> parseFormatString(StringRef fmt); @@ -260,7 +268,8 @@ class FmtObjectBase { }; template <typename Tuple> -class FmtObject : public FmtObjectBase { +class [[deprecated("use tgfmt with a context reference or tgfmtOrFatal")]] +FmtObject : public FmtObjectBase { // Storage for the parameter adapters. Since the base class erases the type // of the parameters, we have to own the storage for the parameters here, and // have the base class store type-erased pointers into this tuple. @@ -283,7 +292,8 @@ class FmtObject : public FmtObjectBase { } }; -class FmtStrVecObject : public FmtObjectBase { +class [[deprecated("use tgfmt with a context reference or tgfmtOrFatal")]] +FmtStrVecObject : public FmtObjectBase { public: using StrFormatAdapter = llvm::support::detail::FormatFunctor<std::string>; @@ -338,6 +348,7 @@ class FmtStrVecObject : public FmtObjectBase { /// 2. This utility does not support format layout because it is rarely needed /// in C++ code generation. template <typename... Ts> +[[deprecated("use tgfmt(fmt, ctx) and handle its Expected, or tgfmtOrFatal")]] inline auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&...vals) -> FmtObject<decltype(std::make_tuple( llvm::support::detail::FormatFunctor<Ts>(std::forward<Ts>(vals))...))> { @@ -350,6 +361,7 @@ inline auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&...vals) } /// Like tgfmt, but take replacement parameters as an array of strings instead. +[[deprecated("use tgfmtv(fmt, ctx) and handle its Expected, or tgfmtvOrFatal")]] inline FmtStrVecObject tgfmtv(StringRef fmt, const FmtContext *ctx, ArrayRef<std::string> params) { return FmtStrVecObject(fmt, ctx, params); @@ -404,6 +416,7 @@ inline llvm::Expected<std::string> tgfmtvChecked(StringRef fmt, ArrayRef<std::string> params) { return tgfmtv(fmt, ctx, params); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP } // namespace tblgen } // namespace mlir diff --git a/mlir/lib/TableGen/CodeGenHelpers.cpp b/mlir/lib/TableGen/CodeGenHelpers.cpp index 96ed992cc09c5d..bead5f50cc7939 100644 --- a/mlir/lib/TableGen/CodeGenHelpers.cpp +++ b/mlir/lib/TableGen/CodeGenHelpers.cpp @@ -186,6 +186,8 @@ FmtOrigin mlir::tblgen::getConstraintFmtOrigin(const Property &property, purpose); } +// The legacy summary helper retains marker behavior during migration. +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH /// Helper to generate a C++ string expression from a given message. /// Message can contain '{{...}}' placeholders that are substituted with /// C-expressions via tgfmt. @@ -241,6 +243,8 @@ buildErrorStreamingStringImpl(StringRef message, const FmtContext &ctx, return os.str(); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP + std::string mlir::tblgen::buildErrorStreamingString( StringRef message, const FmtContext &ctx, ErrorStreamType errorStreamType) { return llvm::cantFail( diff --git a/mlir/lib/TableGen/Format.cpp b/mlir/lib/TableGen/Format.cpp index ea044e9cc323a7..fc4efecb0c3e93 100644 --- a/mlir/lib/TableGen/Format.cpp +++ b/mlir/lib/TableGen/Format.cpp @@ -174,6 +174,8 @@ FmtContext &FmtContext::withSelf(Twine subst) { return addSubst("_self", subst); } +// GCC also diagnoses deprecated types inside their out-of-line definitions. +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH std::optional<StringRef> FmtContext::getSubstFor(FmtContext::PHKind placeholder) const { switch (placeholder) { @@ -188,6 +190,8 @@ FmtContext::getSubstFor(FmtContext::PHKind placeholder) const { llvm_unreachable("unknown placeholder kind"); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP + std::optional<StringRef> FmtContext::getSubstFor(StringRef placeholder) const { auto it = substMap.find(placeholder); if (it == substMap.end()) @@ -210,6 +214,7 @@ void FmtContext::printBindings(raw_ostream &os) const { ::printBindings(os, getBindings()); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH FmtContext::PHKind FmtContext::getPlaceHolderKind(StringRef str) { return StringSwitch<FmtContext::PHKind>(str) .Case("_builder", FmtContext::PHKind::Builder) @@ -408,3 +413,5 @@ FmtStrVecObject::FmtStrVecObject(FmtStrVecObject &&that) for (auto &p : parameters) adapters.push_back(p); } + +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP diff --git a/mlir/lib/Tools/PDLL/Parser/Parser.cpp b/mlir/lib/Tools/PDLL/Parser/Parser.cpp index 8ea0b26c7bb981..bbbbadf84898f1 100644 --- a/mlir/lib/Tools/PDLL/Parser/Parser.cpp +++ b/mlir/lib/Tools/PDLL/Parser/Parser.cpp @@ -164,9 +164,16 @@ class Parser { LogicalResult parseTdInclude(StringRef filename, SMRange fileLoc, SmallVectorImpl<ast::Decl *> &decls); - /// Process the records of a parsed tablegen include file. - void processTdIncludeRecords(const llvm::RecordKeeper &tdRecords, - SmallVectorImpl<ast::Decl *> &decls); + /// Process parsed TableGen records, stopping before appending an invalid + /// decl. + LogicalResult processTdIncludeRecords(const llvm::RecordKeeper &tdRecords, + SMRange includeLoc, + SmallVectorImpl<ast::Decl *> &decls); + + /// Adapt a formatting failure to an import error and source-aware notes. + LogicalResult emitODSFormatError(llvm::Error error, + const tblgen::Constraint &constraint, + SMRange includeLoc); /// Create a user defined native constraint for a constraint imported from /// ODS. @@ -176,10 +183,10 @@ class Parser { SMRange loc, ast::Type type, StringRef nativeType, StringRef docString); template <typename ConstraintT> - ast::Decl * + FailureOr<ast::Decl *> createODSNativePDLLConstraintDecl(const tblgen::Constraint &constraint, SMRange loc, ast::Type type, - StringRef nativeType); + StringRef nativeType, SMRange includeLoc); //===--------------------------------------------------------------------===// // Decls @@ -856,18 +863,16 @@ LogicalResult Parser::parseTdInclude(StringRef filename, llvm::SMRange fileLoc, if (llvm::TableGenParseFile(tdSrcMgr, tdRecords)) return failure(); - // Process the parsed records. - processTdIncludeRecords(tdRecords, decls); - - // After we are done processing, move all of the tablegen source buffers to - // the main parser source mgr. This allows for directly using source locations - // from the .td files without needing to remap them. + // Record processing can diagnose immediately at a TableGen definition. Its + // buffers must already be registered in the parser's source manager. parserSrcMgr.takeSourceBuffersFrom(tdSrcMgr, fileLoc.End); - return success(); + return processTdIncludeRecords(tdRecords, fileLoc, decls); } -void Parser::processTdIncludeRecords(const llvm::RecordKeeper &tdRecords, - SmallVectorImpl<ast::Decl *> &decls) { +LogicalResult +Parser::processTdIncludeRecords(const llvm::RecordKeeper &tdRecords, + SMRange includeLoc, + SmallVectorImpl<ast::Decl *> &decls) { // Return the length kind of the given value. auto getLengthKind = [](const auto &value) { if (value.isOptional()) @@ -934,9 +939,12 @@ void Parser::processTdIncludeRecords(const llvm::RecordKeeper &tdRecords, continue; tblgen::Attribute constraint(def); - decls.push_back(createODSNativePDLLConstraintDecl<ast::AttrConstraintDecl>( + auto decl = createODSNativePDLLConstraintDecl<ast::AttrConstraintDecl>( constraint, convertLocToRange(def->getLoc().front()), attrTy, - constraint.getStorageType())); + constraint.getStorageType(), includeLoc); + if (failed(decl)) + return failure(); + decls.push_back(*decl); } /// Type constraints. for (const llvm::Record *def : tdRecords.getAllDerivedDefinitions("Type")) { @@ -944,9 +952,12 @@ void Parser::processTdIncludeRecords(const llvm::RecordKeeper &tdRecords, continue; tblgen::TypeConstraint constraint(def); - decls.push_back(createODSNativePDLLConstraintDecl<ast::TypeConstraintDecl>( + auto decl = createODSNativePDLLConstraintDecl<ast::TypeConstraintDecl>( constraint, convertLocToRange(def->getLoc().front()), typeTy, - constraint.getCppType())); + constraint.getCppType(), includeLoc); + if (failed(decl)) + return failure(); + decls.push_back(*decl); } /// OpInterfaces. ast::Type opTy = ast::OperationType::get(ctx); @@ -971,6 +982,68 @@ void Parser::processTdIncludeRecords(const llvm::RecordKeeper &tdRecords, decls.push_back(createODSNativePDLLConstraintDecl<ast::OpConstraintDecl>( def->getName(), codeBlock, loc, opTy, cppClassName, desc)); } + return success(); +} + +LogicalResult Parser::emitODSFormatError(llvm::Error error, + const tblgen::Constraint &constraint, + SMRange includeLoc) { + llvm::SourceMgr &sourceMgr = lexer.getSourceMgr(); + auto pointRange = [](SMLoc loc) { return SMRange(loc, loc); }; + + // LSP publishes diagnostics for the main document only. Preserve that anchor + // even when a TableGen import is nested inside one or more PDLL includes. + SMRange mainLoc = includeLoc; + SmallVector<SMLoc> includeChain; + unsigned buffer = sourceMgr.FindBufferContainingLoc(mainLoc.Start); + while (buffer && buffer != sourceMgr.getMainFileID()) { + includeChain.push_back(mainLoc.Start); + SMLoc parent = sourceMgr.getParentIncludeLoc(buffer); + if (!parent.isValid()) + break; + mainLoc = pointRange(parent); + buffer = sourceMgr.FindBufferContainingLoc(parent); + } + + auto origin = tblgen::FmtOrigin::at(constraint.getDef().getLoc(), + "PDLL constraint '" + + constraint.getUniqueDefName() + "'"); + error = tblgen::addFmtOrigin(std::move(error), origin); + llvm::handleAllErrors( + std::move(error), + [&](const tblgen::FmtError &formatError) { + auto messages = tblgen::getFmtDiagnostics(formatError); + auto diag = + ctx.getDiagEngine().emitError(mainLoc, messages.front().message); + SMRange definitionLoc = + pointRange(constraint.getDef().getLoc().front()); + diag->attachNote("constraint '" + constraint.getUniqueDefName() + + "' is defined here", + definitionLoc); + auto addInstantiations = [&](ArrayRef<SMLoc> locations) { + if (locations.empty()) + return; + for (SMLoc loc : locations.drop_front()) + diag->attachNote("instantiated from multiclass", pointRange(loc)); + }; + addInstantiations(messages.front().locations); + for (const auto &message : llvm::drop_begin(messages)) { + diag->attachNote(message.message, + message.locations.empty() + ? definitionLoc + : pointRange(message.locations.front())); + if (!message.locations.empty()) + addInstantiations(message.locations); + } + for (SMLoc loc : includeChain) + diag->attachNote("while importing TableGen constraints", + pointRange(loc)); + }, + // Preserve a useful diagnostic if formatting gains other error types. + [&](const llvm::ErrorInfoBase &other) { + (void)emitError(mainLoc, other.message()); + }); + return failure(); } template <typename ConstraintT> @@ -995,16 +1068,16 @@ ast::Decl *Parser::createODSNativePDLLConstraintDecl( } template <typename ConstraintT> -ast::Decl * -Parser::createODSNativePDLLConstraintDecl(const tblgen::Constraint &constraint, - SMRange loc, ast::Type type, - StringRef nativeType) { +FailureOr<ast::Decl *> Parser::createODSNativePDLLConstraintDecl( + const tblgen::Constraint &constraint, SMRange loc, ast::Type type, + StringRef nativeType, SMRange includeLoc) { // Format the condition template. tblgen::FmtContext fmtContext; fmtContext.withSelf("self"); - std::string codeBlock = tblgen::tgfmt( - "return ::mlir::success(" + constraint.getConditionTemplate() + ");", - &fmtContext); + auto condition = tblgen::tgfmt(constraint.getConditionTemplate(), fmtContext); + if (!condition) + return emitODSFormatError(condition.takeError(), constraint, includeLoc); + std::string codeBlock = "return ::mlir::success(" + *condition + ");"; // If documentation was enabled, build the doc string for the generated // constraint. It would be nice to do this lazily, but TableGen information is diff --git a/mlir/test/mlir-pdll-lsp-server/format-errors.test b/mlir/test/mlir-pdll-lsp-server/format-errors.test new file mode 100644 index 00000000000000..573f968a2952a0 --- /dev/null +++ b/mlir/test/mlir-pdll-lsp-server/format-errors.test @@ -0,0 +1,79 @@ +// RUN: split-file %s %t +// RUN: mlir-pdll-lsp-server -pdll-extra-dir %t -pdll-extra-dir %S/../../include -lit-test < %t/request.json | FileCheck %s + +//--- request.json +{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"pdll","capabilities":{},"trace":"off"}} +// ----- +{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{ + "uri":"test:///format.pdll", "languageId":"pdll", "version":1, + "text":"#include \"include/format-error.td\"" +}}} +// CHECK: "method": "textDocument/publishDiagnostics", +// CHECK: "message": "cannot substitute '$missing' while formatting PDLL constraint 'Broken': missing named binding", +// CHECK-NEXT: "range": { +// CHECK-NEXT: "end": { +// CHECK-NEXT: "character": 34, +// CHECK-NEXT: "line": 0 +// CHECK-NEXT: }, +// CHECK-NEXT: "start": { +// CHECK-NEXT: "character": 9, +// CHECK-NEXT: "line": 0 +// CHECK: "relatedInformation": [ +// CHECK: "line": 1 +// CHECK: "uri": "file:{{.*}}/include/format-error.td" +// CHECK: "message": "constraint 'Broken' is defined here" +// CHECK: "message": "cannot substitute '$_builder' at template byte offset 12: missing named binding" +// CHECK: "message": "template: ($missing + $_builder)" +// CHECK: "message": "available named substitutions: $_self" +// CHECK: "uri": "test:///format.pdll", +// CHECK: "version": 1 +// ----- +{"jsonrpc":"2.0","method":"textDocument/didChange","params":{"textDocument":{ + "uri":"test:///format.pdll", "version":2 +},"contentChanges":[{"text":"#include \"include/format-error.pdll\""}]}} +// CHECK: "method": "textDocument/publishDiagnostics", +// CHECK: "message": "cannot substitute '$missing' while formatting PDLL constraint 'Broken': missing named binding", +// CHECK-NEXT: "range": { +// CHECK-NEXT: "end": { +// CHECK-NEXT: "character": 36, +// CHECK-NEXT: "line": 0 +// CHECK-NEXT: }, +// CHECK-NEXT: "start": { +// CHECK-NEXT: "character": 36, +// CHECK-NEXT: "line": 0 +// CHECK: "relatedInformation": [ +// CHECK: "uri": "file:{{.*}}/include/format-error.td" +// CHECK: "message": "constraint 'Broken' is defined here" +// CHECK: "uri": "file:{{.*}}/include/format-error-middle.pdll" +// CHECK: "message": "while importing TableGen constraints" +// CHECK: "uri": "file:{{.*}}/include/format-error.pdll" +// CHECK: "message": "while importing TableGen constraints" +// CHECK: "uri": "test:///format.pdll", +// CHECK: "version": 2 +// ----- +// The server must survive both formatting failures and process a valid edit. +{"jsonrpc":"2.0","method":"textDocument/didChange","params":{"textDocument":{ + "uri":"test:///format.pdll", "version":3 +},"contentChanges":[{"text":"Pattern { erase _: Op; }"}]}} +// CHECK: "method": "textDocument/publishDiagnostics", +// CHECK-NEXT: "params": { +// CHECK-NEXT: "diagnostics": [], +// CHECK-NEXT: "uri": "test:///format.pdll", +// CHECK-NEXT: "version": 3 +// ----- +{"jsonrpc":"2.0","id":3,"method":"shutdown"} +// ----- +{"jsonrpc":"2.0","method":"exit"} + +//--- include/format-error.td +include "mlir/IR/OpBase.td" +def Broken : Type<CPred<"$missing + $_builder">, "broken">; + +//--- include/format-error-wrapper.td +include "include/format-error.td" + +//--- include/format-error.pdll +#include "include/format-error-middle.pdll" + +//--- include/format-error-middle.pdll +#include "include/format-error-wrapper.td" diff --git a/mlir/test/mlir-pdll/Parser/format-errors.pdll b/mlir/test/mlir-pdll/Parser/format-errors.pdll new file mode 100644 index 00000000000000..f2d3c736e30e05 --- /dev/null +++ b/mlir/test/mlir-pdll/Parser/format-errors.pdll @@ -0,0 +1,78 @@ +// RUN: split-file %s %t +// RUN: not mlir-pdll %t/attr.pdll -I %t -I %S/../../../include 2>&1 | FileCheck %s --check-prefix=ATTR +// RUN: not mlir-pdll %t/escaped.pdll -I %t -I %S/../../../include 2>&1 | FileCheck %s --check-prefix=ESCAPED +// RUN: not mlir-pdll %t/multi.pdll -I %t -I %S/../../../include 2>&1 | FileCheck %s --check-prefix=MULTI +// RUN: not mlir-pdll %t/direct.pdll -I %t -I %S/../../../include 2>&1 | FileCheck %s --check-prefix=DIRECT --implicit-check-not="PDLL constraint 'ZBroken'" --implicit-check-not="<no-subst-found>" +// RUN: not mlir-pdll %t/nested.pdll -I %t -I %S/../../../include 2>&1 | FileCheck %s --check-prefix=NESTED --implicit-check-not="<no-subst-found>" +// RUN: mlir-pdll %t/valid.pdll -I %t -I %S/../../../include | FileCheck %s --check-prefix=VALID + +// DIRECT: direct.pdll:1:10: error: cannot substitute '$missing' while formatting PDLL constraint 'ABroken': missing named binding +// DIRECT: bad.td:2:5: note: constraint 'ABroken' is defined here +// DIRECT: note: cannot substitute '$missing' at template byte offset 12: missing named binding +// DIRECT: note: cannot substitute '$3' at template byte offset 23: positional argument out of range +// DIRECT: note: template: ($missing + $missing + $3) +// DIRECT: note: failed occurrence starts at template byte offset 1 +// DIRECT: note: available named substitutions: $_self +// DIRECT: note: no positional substitutions were supplied + +// The primary location must reach the main document through the PDLL include. +// The definition is inside a second-level TableGen include. +// NESTED: nested.pdll:1:22: error: cannot substitute '$missing' while formatting PDLL constraint 'ABroken' +// NESTED: bad.td:2:5: note: constraint 'ABroken' is defined here +// NESTED: note: available named substitutions: $_self +// NESTED: middle.pdll:1:10: note: while importing TableGen constraints +// NESTED: inner.pdll:1:23: note: while importing TableGen constraints + +// VALID: Name<Valid> ResultType<Tuple<>> Code<return ::mlir::success((self.isInteger(32)));> + +// ESCAPED: escaped.pdll:1:10: error: cannot substitute '$missing' +// ESCAPED: note: template: (\"quoted\" + $missing\n + $_self) +// ESCAPED: note: available named substitutions: $_self + +// MULTI: multi.pdll:1:10: error: cannot substitute '$missing' while formatting PDLL constraint 'BrokenType' +// MULTI: multi.td:3:7: note: constraint 'BrokenType' is defined here +// MULTI: multi.td:5:{{[0-9]+}}: note: instantiated from multiclass +// MULTI: note: available named substitutions: $_self + +// ATTR: attr.pdll:1:10: error: cannot substitute '$missing' while formatting PDLL constraint 'BrokenAttr' +// ATTR: attr.td:2:5: note: constraint 'BrokenAttr' is defined here +// ATTR: note: available named substitutions: $_self + +//--- direct.pdll +#include "bad.td" +//--- nested.pdll +#include "inner.pdll" +//--- inner.pdll +#include "middle.pdll" +//--- middle.pdll +#include "wrapper.td" +//--- wrapper.td +include "bad.td" +//--- bad.td +include "mlir/IR/OpBase.td" +def ABroken : Type<CPred<"$missing + $missing + $3">, "broken">; +def ZBroken : Type<CPred<"$_builder">, "also broken">; +//--- valid.pdll +#include "valid.td" +//--- valid.td +include "mlir/IR/OpBase.td" +def Valid : Type<CPred<"$_self.isInteger(32)">, "valid">; +//--- escaped.pdll +#include "escaped.td" +//--- escaped.td +include "mlir/IR/OpBase.td" +def Escaped : Type<CPred<[{"quoted" + $missing + + $_self}]>, "escaped">; +//--- multi.pdll +#include "multi.td" +//--- multi.td +include "mlir/IR/OpBase.td" +multiclass Bad { + def Type : Type<CPred<"$missing">, "broken">; +} +defm Broken : Bad; +//--- attr.pdll +#include "attr.td" +//--- attr.td +include "mlir/IR/OpBase.td" +def BrokenAttr : Attr<CPred<"$missing">, "broken attribute">; diff --git a/mlir/unittests/TableGen/FormatTest.cpp b/mlir/unittests/TableGen/FormatTest.cpp index fb474bf10b2561..30659a21717f45 100644 --- a/mlir/unittests/TableGen/FormatTest.cpp +++ b/mlir/unittests/TableGen/FormatTest.cpp @@ -31,14 +31,14 @@ takeFormatError(llvm::Expected<std::string> result) { TEST(FormatTest, EmptyFmtStr) { FmtContext ctx; - std::string result = std::string(tgfmt("", &ctx)); + std::string result = llvm::cantFail(tgfmt("", ctx)); EXPECT_TRUE(result.empty()); } /// Allow extra unused positional parameters. TEST(FormatTest, EmptyFmtStrExtraParams) { FmtContext ctx; - std::string result = std::string(tgfmt("", &ctx, "a", "b", "c")); + std::string result = llvm::cantFail(tgfmt("", ctx, "a", "b", "c")); EXPECT_TRUE(result.empty()); } @@ -46,35 +46,35 @@ TEST(FormatTest, EmptyFmtStrExtraParams) { TEST(FormatTest, EmptyFmtStrPopulatedCtx) { FmtContext ctx; ctx.withBuilder("builder"); - std::string result = std::string(tgfmt("", &ctx)); + std::string result = llvm::cantFail(tgfmt("", ctx)); EXPECT_TRUE(result.empty()); } TEST(FormatTest, LiteralFmtStr) { FmtContext ctx; - std::string result = std::string(tgfmt("void foo {}", &ctx)); + std::string result = llvm::cantFail(tgfmt("void foo {}", ctx)); EXPECT_THAT(result, StrEq("void foo {}")); } /// Print single dollar literally. TEST(FormatTest, AdjacentDollar) { FmtContext ctx; - std::string result = std::string(tgfmt("$", &ctx)); + std::string result = llvm::cantFail(tgfmt("$", ctx)); EXPECT_THAT(result, StrEq("$")); } /// Print dangling dollar literally. TEST(FormatTest, DanglingDollar) { FmtContext ctx; - std::string result = std::string(tgfmt("foo bar baz$", &ctx)); + std::string result = llvm::cantFail(tgfmt("foo bar baz$", ctx)); EXPECT_THAT(result, StrEq("foo bar baz$")); } /// Allow escape dollars with '$$'. TEST(FormatTest, EscapeDollars) { FmtContext ctx; - std::string result = - std::string(tgfmt("$$ $$$$ $$$0 $$$_self", &ctx.withSelf("self"), "-0")); + std::string result = llvm::cantFail( + tgfmt("$$ $$$$ $$$0 $$$_self", ctx.withSelf("self"), "-0")); EXPECT_THAT(result, StrEq("$ $$ $-0 $self")); } @@ -84,59 +84,66 @@ TEST(FormatTest, PositionalFmtStr) { int c = 42; char d = 'd'; std::string result = - std::string(tgfmt("$0 $1 $2 $3", &ctx, "a", b, c + 1, d)); + llvm::cantFail(tgfmt("$0 $1 $2 $3", ctx, "a", b, c + 1, d)); EXPECT_THAT(result, StrEq("a b 43 d")); } /// Output the placeholder if missing substitution. -TEST(FormatTest, PositionalFmtStrMissingParams) { +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH +TEST(FormatTest, LegacyPositionalFmtStrMissingParams) { FmtContext ctx; std::string result = std::string(tgfmt("$0 %1 $2", &ctx)); EXPECT_THAT(result, StrEq("$0<no-subst-found> %1 $2<no-subst-found>")); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP /// Allow flexible reference of positional parameters. TEST(FormatTest, PositionalFmtStrFlexibleRef) { FmtContext ctx; - std::string result = std::string(tgfmt("$2 $0 $2", &ctx, "a", "b", "c")); + std::string result = llvm::cantFail(tgfmt("$2 $0 $2", ctx, "a", "b", "c")); EXPECT_THAT(result, StrEq("c a c")); } TEST(FormatTest, PositionalFmtStrNoWhitespace) { FmtContext ctx; - std::string result = std::string(tgfmt("foo$0bar", &ctx, "-")); + std::string result = llvm::cantFail(tgfmt("foo$0bar", ctx, "-")); EXPECT_THAT(result, StrEq("foo-bar")); } TEST(FormatTest, PlaceHolderFmtStrWithSelf) { FmtContext ctx; - std::string result = std::string(tgfmt("$_self", &ctx.withSelf("sss"))); + std::string result = llvm::cantFail(tgfmt("$_self", ctx.withSelf("sss"))); EXPECT_THAT(result, StrEq("sss")); } TEST(FormatTest, PlaceHolderFmtStrWithBuilder) { FmtContext ctx; - std::string result = std::string(tgfmt("$_builder", &ctx.withBuilder("bbb"))); + std::string result = + llvm::cantFail(tgfmt("$_builder", ctx.withBuilder("bbb"))); EXPECT_THAT(result, StrEq("bbb")); } -TEST(FormatTest, PlaceHolderMissingCtx) { +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH +TEST(FormatTest, LegacyPlaceHolderMissingCtx) { std::string result = std::string(tgfmt("$_op", nullptr)); EXPECT_THAT(result, StrEq("$_op<no-subst-found>")); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP -TEST(FormatTest, PlaceHolderMissingSubst) { +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH +TEST(FormatTest, LegacyPlaceHolderMissingSubst) { FmtContext ctx; std::string result = std::string(tgfmt("$_op", &ctx.withBuilder("builder"))); EXPECT_THAT(result, StrEq("$_op<no-subst-found>")); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP /// Test commonly used delimiters in C++. TEST(FormatTest, PlaceHolderFmtStrDelimiter) { FmtContext ctx; ctx.addSubst("m", ""); - std::string result = std::string(tgfmt("$m{$m($m[$m]$m)$m}$m|", &ctx)); + std::string result = llvm::cantFail(tgfmt("$m{$m($m[$m]$m)$m}$m|", ctx)); EXPECT_THAT(result, StrEq("{([])}|")); } @@ -147,23 +154,25 @@ TEST(FormatTest, CustomPlaceHolderFmtStrPlaceHolderChars) { ctx.addSubst("m1", "1 "); ctx.addSubst("m2C", "2 "); ctx.addSubst("M_3", "3 "); - std::string result = std::string(tgfmt("$m$m1$m2C$M_3", &ctx)); + std::string result = llvm::cantFail(tgfmt("$m$m1$m2C$M_3", ctx)); EXPECT_THAT(result, StrEq("0 1 2 3 ")); } -TEST(FormatTest, CustomPlaceHolderFmtStrUnregisteredPlaceHolders) { +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH +TEST(FormatTest, LegacyCustomPlaceHolderFmtStrUnregisteredPlaceHolders) { FmtContext ctx; std::string result = std::string(tgfmt("foo($awesome, $param)", &ctx)); EXPECT_THAT(result, StrEq("foo($awesome<no-subst-found>, $param<no-subst-found>)")); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP TEST(FormatTest, MixedFmtStr) { FmtContext ctx; ctx.withBuilder("bbb"); - std::string result = std::string(tgfmt("$_builder.build($_self, {$0, $1})", - &ctx.withSelf("sss"), "a", "b")); + std::string result = llvm::cantFail(tgfmt("$_builder.build($_self, {$0, $1})", + ctx.withSelf("sss"), "a", "b")); EXPECT_THAT(result, StrEq("bbb.build(sss, {a, b})")); } @@ -177,7 +186,9 @@ TEST(FormatTest, CheckedBuiltinRegistrationAndSnapshots) { ASSERT_TRUE(static_cast<bool>(result)); EXPECT_EQ(*result, "builder(self)"); ctx.withSelf("first").addSubst("_self", "second"); + LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH EXPECT_EQ(ctx.getSubstFor(FmtContext::PHKind::Self), "second"); + LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP ctx.withSelf("third"); EXPECT_EQ(ctx.getSubstFor("_self"), "third"); auto bindings = ctx.getBindings(); @@ -223,7 +234,9 @@ TEST(FormatTest, CheckedDecimalAndRangeBoundaries) { auto result = tgfmtvChecked("$00 $08 $010 $9...", FmtContext{}, values); ASSERT_TRUE(static_cast<bool>(result)); EXPECT_EQ(*result, "0 8 10 9, 10"); + LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH EXPECT_EQ(tgfmtv("$08 $010 $10...", nullptr, values).str(), "8 10 10"); + LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP auto single = tgfmtvChecked("$10...", FmtContext{}, values); ASSERT_TRUE(static_cast<bool>(single)); EXPECT_EQ(*single, "10"); @@ -236,6 +249,7 @@ TEST(FormatTest, CheckedDecimalAndRangeBoundaries) { FmtIssueKind::RangeStartOutOfRange); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH TEST(FormatTest, CheckedInvalidIndexAndLiteralSyntax) { llvm::StringRef huge = "$999999999999999999999999999999999999999999999999999..."; @@ -249,6 +263,7 @@ TEST(FormatTest, CheckedInvalidIndexAndLiteralSyntax) { ASSERT_TRUE(static_cast<bool>(result)); EXPECT_EQ(*result, "$ $- $? fooxbar x..2 $"); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP TEST(FormatTest, CheckedInvalidIndexWithoutRange) { llvm::StringRef huge = "$99999999999999999999999999999999999999999999999999"; @@ -260,6 +275,7 @@ TEST(FormatTest, CheckedInvalidIndexWithoutRange) { EXPECT_EQ(error->getIssues()[0].offset, 0u); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH TEST(FormatTest, CheckedMissingBuiltinAndNullLegacyContext) { auto missing = takeFormatError(tgfmtChecked("$_self", FmtContext{})); ASSERT_TRUE(missing); @@ -270,6 +286,7 @@ TEST(FormatTest, CheckedMissingBuiltinAndNullLegacyContext) { EXPECT_EQ(noContext->getIssues()[0].kind, FmtIssueKind::MissingNamedBinding); EXPECT_TRUE(noContext->getBindings().empty()); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP TEST(FormatTest, ErrorLogIncludesFailureAndAvailability) { auto result = tgfmtChecked("$_self $2", FmtContext{}, "only"); @@ -367,6 +384,7 @@ TEST(FormatTest, BindingLogsEscapeAndSort) { EXPECT_EQ(snapshotLog, log); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH TEST(FormatTest, LegacyRemainsLazy) { FmtContext ctx; ctx.withSelf("before"); @@ -377,6 +395,7 @@ TEST(FormatTest, LegacyRemainsLazy) { EXPECT_EQ(legacy.str(), "after"); EXPECT_EQ(*eager, "before"); } +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP TEST(FormatTest, DiagnosticsPreserveSourceContextsAndOwnMessages) { const char source[] = "definition multiclass use outer"; @@ -549,3 +568,41 @@ TEST(FormatTest, SummaryErrorRetainsEscapedSegmentAndBindingSnapshot) { ctx.withSelf("changed"); EXPECT_EQ(error->getBindings()[0].replacement, "value"); } + +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH +TEST(FormatTest, LegacySummaryRetainsMarkers) { + EXPECT_EQ(buildErrorStreamingString("bad {{$missing}}", FmtContext{}), + "bad \" << $missing<no-subst-found> << \""); +} +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP + +TEST(FormatTest, CheckedSummaryReportsTheSecondExpression) { + auto error = takeFormatError(buildErrorStreamingStringChecked( + "first {{$_self}} then {{$missing}}", FmtContext({{"_self", "value"}}))); + ASSERT_TRUE(error); + EXPECT_EQ(error->getTemplate(), "$missing"); + EXPECT_EQ(error->getIssues().size(), 1u); + EXPECT_EQ(error->getIssues()[0].offset, 0u); +} + +TEST(FormatTest, FatalVariadicFormattingSuccess) { + EXPECT_EQ(tgfmtOrFatal(FmtOrigin::describe("test"), "$_self $0", + FmtContext({{"_self", "self"}}), 42), + "self 42"); +} + +#if GTEST_HAS_DEATH_TEST +TEST(FormatTest, FatalVariadicFormattingUsesTheCallerOrigin) { + EXPECT_EXIT((void)tgfmtOrFatal(FmtOrigin::describe("caller predicate"), + "$missing", FmtContext{}), + ::testing::ExitedWithCode(1), + "while formatting caller predicate"); +} +#endif + +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH +TEST(FormatTest, LegacyPositionalFormattingSuccess) { + FmtContext ctx; + EXPECT_EQ(tgfmt("$1 $0 $1...", &ctx, "zero", "one").str(), "one zero one"); +} +LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
