Hello, I have encountered some problems in three conversion passes with same error with message failed to legalize operation 'xxx' that was explicitly marked illegal.
The reasons for this error are similar: the code target.addIllegalDialect<XXX>() makes some operations cannot be converted are marked illegal.
#bug 1: ConvertMathToLibmPass
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.
/home/ty/test.mlir:8:8: error: failed to legalize operation 'math.powf' that was explicitly marked illegal
%0 = math.powf %arg0, %c : f32
^
#bug 2: BufferizationToMemRefPass
BufferizationToMemRefPass can only handle bufferization.clone, and set the BufferizationDialect illegal after conversion by target.addIllegalDialect<bufferization::BufferizationDialect>(). The operation bufferization.to_memref cannot be converted and is marked as illegal, thus causing this error.
/home/ty/test.mlir:3:8: error: failed to legalize operation 'spv.Constant' that was explicitly marked illegal
%8 = spv.Constant [dense<3.0> : vector<2xf32>] : !spv.array<1xvector<2xf32>>
^
I think there may be some problems with the code design.
I have committed the three bugs mentioned above in github. Welcome to verify and discuss together.
Passes in MLIR are generally allowed to error out if their input doesn’t satisfy certain invariants, like presence or absence of certain ops. It all depends on the contract that the pass provides, e.g., A-to-B conversion passes may have a contract that there are no operations from the A dialect in the resulting IR. If they are unable to achieve this, an error is justified.
For conversion passes, it may also be reasonable to perform a partial conversion and leave the unhandled ops as is. This is up for maintainers of the respective passes.
Side note: some people view convert-A-to-B passes as testing infrastructure for the conversion patterns and expect MLIR clients to use the patterns directly instead of calling the passes.
I agree in the general sense, in the case of the “convert-math-to-libm” pass I’m not convinced we should error here: either we’re missing implementations for these conversions or we should mark these legal.