<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64399>64399</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            MLIR tablegen rewrites generating C++ code that calls non-existing build method
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jcai19
      </td>
    </tr>
</table>

<pre>
    I made the following change to [tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td).

```
 // have non matching batch dimensions but are broadcastable which should fail
 // with V1.
 def BatchMatMulToV2 :
-  Pat<(TF_BatchMatMulOp AnyStaticShapeTensor:$x, AnyStaticShapeTensor:$y,
+ Pat<(TF_BatchMatMulOp:$src AnyStaticShapeTensor:$x, AnyStaticShapeTensor:$y,
                         $adj_x, $adj_y),
- (TF_BatchMatMulV2Op $x, $y, $adj_x, $adj_y)>;
+      (SetAttrs $src, (TF_BatchMatMulV2Op $x, $y, $adj_x, $adj_y))>;
 
 def BatchMatMulToMatMul : Pat<(TF_BatchMatMulOp $x, $y, $adj_x, $adj_y),
 (TF_MatMulOp $x, $y, $adj_x, $adj_y),
```

`SetAttrs` is a NativeCodeCall that calls [this function](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/tensorflow/transforms/rewrite_util.cc#L54), which copies attributes from the source op to the destination op and returns the result of the destination op. I expected the creation of `TF_BatchMatMulV2Op` to remain unchanged. However, it  changed from
```
      ::llvm::SmallVector<::mlir::Type, 4> tblgen_types; (void)tblgen_types;
      for (auto v: castedOp0.getODSResults(0)) {
 tblgen_types.push_back(v.getType());
      }
 tblgen_BatchMatMulV2Op_0 = rewriter.create<::mlir::TF::BatchMatMulV2Op>(odsLoc, tblgen_types, tblgen_values, tblgen_attrs);
```

to 

```
 tblgen_BatchMatMulV2Op_0 = rewriter.create<::mlir::TF::BatchMatMulV2Op>(odsLoc,
 /*x=*/tblgen_value_0,
        /*y=*/tblgen_value_1,
 /*adj_x=*/adj_x,
        /*adj_y=*/adj_y
      );
```

And caused a building failure because the op does not have the build method, which is generated only if return type can be inferred, based on the [doc](https://mlir.llvm.org/docs/DefiningDialects/Operations/#builder-methods).

Checking the `RewriterGen.cpp`, it seems my changes caused the `HandleOpCreation` to go down a different code path (from [here](https://github.com/llvm/llvm-project/blob/main/mlir/tools/mlir-tblgen/RewriterGen.cpp#L1486) in the original case to [here](https://github.com/llvm/llvm-project/blob/main/mlir/tools/mlir-tblgen/RewriterGen.cpp#L1467) instead), which explains the different build method. But I'm not sure if this is expected?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV8tu2zgXfhp6cxBBpmxHWnjhS_03QPpn0ATZGhR5ZLGlSIGkkvjtB6TkxHacdjozmAaBJZLnfvkOxZyTO404J9Mlma5HrPO1sfNvnMlxMSqN2M9voGECwdcIlVHKPEu9A14zvUPwBsh06VE7YytlngndcNO0UqEldNMoGR4nx_1OtTWtS7wg0zWhee1960i2IHRD6GYnfd2VCTfNOe_JolSmDDqY8_hOy68ZQYuEpGuSLobfWTr8xyX0ZkHNnhC00dAwz-sQhDK8gJANaieNdlB2HphFKK1hgjPnWakQnmvJa3C16ZSAikl1KvdZ-hoex4MJILCCZRD8hfkvnXowjxRCbOLpFcAfzJNsRWj-sNke0d21sND7e8-85Pc1a_EhOhyjOnkhdPWD4z2hq8F1uvxYQU_sLP83NMFHf4ROmPi2jYKG9z2hxSvjFbwz7ZHetXBQPmj5UE72iWTLN28Hnfk9-oX31kHvYs_1T_ScqoKPsts_Q4Z_kNm_rHP1WlpByN_mP-uA181DkMgsBemAwf-Zl0-4MgJXTCnwNfPAmVIuwkItHVSd5l4a_Zs63VumXWVs4wjdWHy20uO281IlnBOa3U4nvd9Dk3LTSnTAvLey7Dw6qKxp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