Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp =================================================================== --- flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -3772,6 +3772,11 @@ mathConvertionPM.addPass( mlir::createConvertMathToFuncs(mathToFuncsOptions)); mathConvertionPM.addPass(mlir::createConvertComplexToStandardPass()); + // Convert Math dialect operations into LLVM dialect operations. + // There is no way to prefer MathToLLVM patterns over MathToLibm + // patterns (applied below), so we have to run MathToLLVM conversion here. + mathConvertionPM.addNestedPass( + mlir::createConvertMathToLLVMPass()); if (mlir::failed(runPipeline(mathConvertionPM, mod))) return signalPassFailure(); @@ -3826,9 +3831,8 @@ mlir::arith::populateArithToLLVMConversionPatterns(typeConverter, pattern); mlir::cf::populateControlFlowToLLVMConversionPatterns(typeConverter, pattern); - // Convert math-like dialect operations, which can be produced - // when late math lowering mode is used, into llvm dialect. - mlir::populateMathToLLVMConversionPatterns(typeConverter, pattern); + // Math operations that have not been converted yet must be converted + // to Libm. mlir::populateMathToLibmConversionPatterns(pattern); mlir::populateComplexToLLVMConversionPatterns(typeConverter, pattern); mlir::ConversionTarget target{*context};