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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Code design problem in ConvertMathToLibmPass
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    When i use cmd `mlir-opt -convert-math-to-libm`, i got an error : failed to legalize operation.
I think the problem is that the pass `-convert-math-to-libm` does not legitimize some operations (e.g., math.powf,math.floor,math.log), and this code ` target.addIllegalDialect<math::MathDialect>()` in ConvertMathToLibmPass makes these operations are marked illegal.

MathToLibm.cpp:
```
void ConvertMathToLibmPass::runOnOperation() {
  auto module = getOperation();

  RewritePatternSet patterns(&getContext());
 populateMathToLibmConversionPatterns(patterns, /*benefit=*/1);

 ConversionTarget target(getContext());
 target.addLegalDialect<arith::ArithmeticDialect, BuiltinDialect,
 func::FuncDialect, vector::VectorDialect>();
 target.addIllegalDialect<math::MathDialect>();        //This line of code needs to be modified
  if (failed(applyPartialConversion(module, target, std::move(patterns))))
    signalPassFailure();
}
```

Test on commit: [#939f5a33](https://github.com/llvm/llvm-project/commit/939f5a33711370697f9ad1de4267cfc7399dfe86)
Steps to reproduce:
```
mlir-opt -convert-math-to-libm test.mlir
```
test case:
```
func.func @pow_noop(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) {
  %c = arith.constant 1.0 : f32
  %v = arith.constant dense <1.0> : vector<4xf32>
  %0 = math.powf %arg0, %c : f32
  %1 = math.powf %arg1, %v : vector<4xf32>
 return %0, %1 : f32, vector<4xf32>
}
```
The error message is like:
```
/home/ty/test.mlir:8:8: error: failed to legalize operation 'math.powf' that was explicitly marked illegal
  %0 = math.powf %arg0, %c : f32
       ^
```
</pre>
<img width="1px" height="1px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