[llvm] [mlir] [mlir][EmitC] Add MathToEmitC pass for math function lowering to EmitC (PR #113799)

Simon Camphausen via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 29 07:06:25 PDT 2024


================
@@ -0,0 +1,65 @@
+//===- MathToEmitC.cpp - Math to EmitC Pass Implementation ----------===//
+//
+// 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/MathToEmitC/MathToEmitC.h"
+
+#include "mlir/Dialect/EmitC/IR/EmitC.h"
+#include "mlir/Dialect/Math/IR/Math.h"
+#include "mlir/Transforms/DialectConversion.h"
+
+using namespace mlir;
+
+namespace {
+template <typename OpType>
+class LowerToEmitCCallOpaque : public mlir::OpRewritePattern<OpType> {
+  std::string calleeStr;
+
+public:
+  LowerToEmitCCallOpaque(MLIRContext *context, std::string calleeStr)
+      : OpRewritePattern<OpType>(context), calleeStr(std::move(calleeStr)) {}
+
+  LogicalResult matchAndRewrite(OpType op,
+                                PatternRewriter &rewriter) const override;
+};
+
+template <typename OpType>
+LogicalResult LowerToEmitCCallOpaque<OpType>::matchAndRewrite(
+    OpType op, PatternRewriter &rewriter) const {
+  auto actualOp = mlir::cast<OpType>(op);
+  if (!llvm::all_of(
+          actualOp->getOperands(),
+          [](Value operand) { return isa<FloatType>(operand.getType()); }) ||
+      !llvm::all_of(actualOp->getResultTypes(),
+                    [](mlir::Type type) { return isa<FloatType>(type); })) {
+    op.emitError("non-float types are not supported");
+    return mlir::failure();
+  }
+  mlir::StringAttr callee = rewriter.getStringAttr(calleeStr);
+  rewriter.replaceOpWithNewOp<mlir::emitc::CallOpaqueOp>(
+      actualOp, actualOp.getType(), callee, actualOp->getOperands());
----------------
simon-camp wrote:

Using `calleeStr` directly should work I think.

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


More information about the llvm-commits mailing list