[Mlir-commits] [mlir] [mlir][emitc] Support convert arith.extf and arith.truncf to emitc (PR #121184)

Simon Camphausen llvmlistbot at llvm.org
Mon Jan 13 04:32:06 PST 2025


================
@@ -733,6 +733,43 @@ class ItoFCastOpConversion : public OpConversionPattern<CastOp> {
   }
 };
 
+// Floating-point to floating-point conversions.
+template <typename CastOp>
+class FpCastOpConversion : public OpConversionPattern<CastOp> {
+public:
+  FpCastOpConversion(const TypeConverter &typeConverter, MLIRContext *context)
+      : OpConversionPattern<CastOp>(typeConverter, context) {}
+
+  LogicalResult
+  matchAndRewrite(CastOp castOp, typename CastOp::Adaptor adaptor,
+                  ConversionPatternRewriter &rewriter) const override {
+    // Vectors in particular are not supported
+    Type operandType = adaptor.getIn().getType();
+    if (!emitc::isSupportedFloatType(operandType))
+      return rewriter.notifyMatchFailure(castOp,
+                                         "unsupported cast source type");
+    if (auto roundingModeOp =
+            dyn_cast<arith::ArithRoundingModeInterface>(*castOp)) {
+      // Only supporting default rounding mode as of now.
+      if (roundingModeOp.getRoundingModeAttr())
----------------
simon-camp wrote:

I assume the arith dialect implicitly follows the LLVM LangRef, which would mean the default of rounding ties to even is used. `arith::TruncF` is also lowered to `llvm::LLVM::FPTruncOp` when missing the rounding mode attr. So this should match with the C behaviour IIUC. But adding a section to the docs about assumptions would be a good idea.

https://github.com/llvm/llvm-project/pull/121184


More information about the Mlir-commits mailing list