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 @@ -369,6 +369,11 @@ // If a constraint is specified, we need to generate C++ statements to // check the constraint. + // Only check the constraint of an optional attribute if it has value. + if (attr.isOptional()) { + os.indent(indent) << "if (tblgen_attr) {\n"; + indent += 2; + } emitMatchCheck( depth, tgfmt(matcher.getConditionTemplate(), &fmtCtx.withSelf("tblgen_attr")), @@ -376,6 +381,10 @@ "{2}\"", op.getOperationName(), namedAttr->name, matcher.getAsConstraint().getDescription())); + if (attr.isOptional()) { + os.indent(indent) << "}\n"; + indent -= 2; + } } // Capture the value