[Mlir-commits] [mlir] [mlir][math] Add FP software implementation lowering pass: math-to-apfloat (PR #171221)
Matthias Springer
llvmlistbot at llvm.org
Wed Dec 17 01:27:46 PST 2025
================
@@ -0,0 +1,210 @@
+//===- MathToAPFloat.cpp - Mathmetic to APFloat Conversion ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Conversion/MathToAPFloat/MathToAPFloat.h"
+#include "Utils.h"
+
+#include "mlir/Dialect/Func/IR/FuncOps.h"
+#include "mlir/Dialect/Func/Utils/Utils.h"
+#include "mlir/Dialect/Math/IR/Math.h"
+#include "mlir/Dialect/Math/Transforms/Passes.h"
+#include "mlir/Dialect/Vector/IR/VectorOps.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/IR/Verifier.h"
+#include "mlir/Transforms/WalkPatternRewriteDriver.h"
+
+namespace mlir {
+#define GEN_PASS_DEF_MATHTOAPFLOATCONVERSIONPASS
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+using namespace mlir;
+using namespace mlir::func;
+
+struct AbsFOpToAPFloatConversion final : OpRewritePattern<math::AbsFOp> {
+ AbsFOpToAPFloatConversion(MLIRContext *context, SymbolOpInterface symTable,
+ PatternBenefit benefit = 1)
+ : OpRewritePattern<math::AbsFOp>(context, benefit), symTable(symTable) {}
+
+ LogicalResult matchAndRewrite(math::AbsFOp op,
+ PatternRewriter &rewriter) const override {
+ // Get APFloat function from runtime library.
+ auto i32Type = IntegerType::get(symTable->getContext(), 32);
+ auto i64Type = IntegerType::get(symTable->getContext(), 64);
+ FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+ rewriter, symTable, "_mlir_apfloat_abs", {i32Type, i64Type});
+ if (failed(fn))
+ return fn;
+ Location loc = op.getLoc();
+ rewriter.setInsertionPoint(op);
+ // Cast operands to 64-bit integers.
+ auto operand = op.getOperand();
+ auto floatTy = cast<FloatType>(operand.getType());
+ if (floatTy.getIntOrFloatBitWidth() > 64) {
+ return rewriter.notifyMatchFailure(op,
----------------
matthias-springer wrote:
"return failure/notifyMatchFailure()" should be before any kind of IR modification. (`lookupOrCreateFnDecl` modifies the IR.)
https://github.com/llvm/llvm-project/pull/171221
More information about the Mlir-commits
mailing list