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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] A bug within computeNecessaryMaterializations during dialect conversion
        </td>
    </tr>

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

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

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

<pre>
    There might be a bug when MLIR dialect conversion infrastructure trying to legalize unresolved type materializations. The related codes are located in mlir/lib/Transforms/Utils/DialectConversion.cpp:2609(commit id: 4b7a08f0f8e23999ee79154f4f241790f51a044b)

```c++
for (Operation *user : llvm::make_early_inc_range(opResult.getUsers())) {
      auto castOp = dyn_cast<UnrealizedConversionCastOp>(user);
      if (!castOp)
        continue;
      if (castOp->getResultTypes() == inputOperands.getTypes()) {
        replaceMaterialization(rewriterImpl, opResult, inputOperands,
 inverseMapping);
 necessaryMaterializations.remove(materializationOps.lookup(user));
 }
    }
``` 

Within this code snippet, if current unresolved materialization op has a user which has the same result type as the operand type of current unresolved materialization op, ALL uses of current unresolved materialization will be replaced by current op's operands. This transformation might cause operand type mismatch for those user ops that are NOT UnrealizedConversionCastOp
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVNFuozoQ_RrnZdQIbAjwwEO22UgrtVtp1eo-VsYM4LvGtmzTKvfrr2zSNO3uSousEAZ75pzhzOHey1EjtqT8QsrDhi9hMq59lUpJPmcZzSktNp3pT-3jhA5hluMUoEPg0C0jvE6o4f7u2w_oJVcoAgijX9B5aTRIPTjug1tEWBxCcCepRwgGFI5cyf8QFu3QG_WCPYSTRZh5QCfjOx6k0X4LjxOCQ8UD9iBMjx64Q1BGpIjUMCvpCD0q2RF6fHRc-8G42RN6fApSxfthRXZ7AbYV1hK2p7usIbQWZp5lANkTtoeiq3hWD9lQI2VN0yBW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