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 @@ -349,7 +349,7 @@ if (!name.empty()) os << formatv("{0} = {1};\n", name, castedName); - for (int i = 0, e = tree.getNumArgs(); i != e; ++i) { + for (int i = 0, e = tree.getNumArgs(), numPrevAttrs = 0; i != e; ++i) { auto opArg = op.getArg(i); std::string argName = formatv("op{0}", depth + 1); @@ -365,10 +365,12 @@ } os << "{\n"; + // We need to subtract the number of attributes before this operand to get + // the index in the operand list. os.indent() << formatv( "auto *{0} = " "(*{1}.getODSOperands({2}).begin()).getDefiningOp();\n", - argName, castedName, i); + argName, castedName, i - numPrevAttrs); emitMatch(argTree, argName, depth + 1); os << formatv("tblgen_ops[{0}] = {1};\n", ++opCounter, argName); os.unindent() << "}\n"; @@ -380,6 +382,9 @@ emitOperandMatch(tree, castedName, i, depth); } else if (opArg.is()) { emitAttributeMatch(tree, opName, i, depth); + // getODSOperands do not count Attributes, + // so we need to track how many we have seen so far. + ++numPrevAttrs; } else { PrintFatalError(loc, "unhandled case when matching op"); }