diff --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp --- a/mlir/tools/mlir-tblgen/RewriterGen.cpp +++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp @@ -255,7 +255,13 @@ raw_indented_ostream::DelimitedScope scope(os); - os << "if(!" << opName << ") return ::mlir::failure();\n"; + auto fmt = tree.getNativeCodeTemplate(); + if (fmt.count("$_self") != 1) + PrintFatalError(loc, "NativeCodeCall must have $_self as argument for " + "passing the defining Operation"); + + emitMatchCheck(opName, /*matchStr=*/opName.str(), + formatv("\"$_self of {0} is nullptr\"", fmt)); for (int i = 0, e = tree.getNumArgs(); i != e; ++i) { std::string argName = formatv("arg{0}_{1}", depth, i); if (DagNode argTree = tree.getArgAsNestedDag(i)) { @@ -276,16 +282,11 @@ std::string locToUse; std::tie(hasLocationDirective, locToUse) = getLocation(tree); - auto fmt = tree.getNativeCodeTemplate(); - if (fmt.count("$_self") != 1) { - PrintFatalError(loc, "NativeCodeCall must have $_self as argument for " - "passing the defining Operation"); - } - auto nativeCodeCall = std::string(tgfmt( fmt, &fmtCtx.addSubst("_loc", locToUse).withSelf(opName.str()), capture)); - os << "if (failed(" << nativeCodeCall << ")) return ::mlir::failure();\n"; + emitMatchCheck(opName, formatv("!failed({0})", nativeCodeCall), + formatv("\"{0} return failure\"", nativeCodeCall)); for (int i = 0, e = tree.getNumArgs(); i != e; ++i) { auto name = tree.getArgName(i); @@ -343,8 +344,9 @@ castedName, opName, op.getQualCppClassName()); // Skip the operand matching at depth 0 as the pattern rewriter already does. if (depth != 0) { - // Skip if there is no defining operation (e.g., arguments to function). - os << formatv("if (!{0}) return ::mlir::failure();\n", castedName); + emitMatchCheck(castedName, /*matchStr=*/castedName, + formatv("\"{0} is not {1} type\"", castedName, + op.getQualCppClassName())); } if (tree.getNumArgs() != op.getNumArgs()) { PrintFatalError(loc, formatv("op '{0}' argument number mismatch: {1} in "