Skip to content

Conversation

@makslevental
Copy link
Contributor

@makslevental makslevental commented Dec 8, 2025

Add APFloat software implementation for math.fma, math.abs, math.isnan, math.isfinite, math.isinf, math.isnormal for reduced precision (fp4*, fp6*, fp8*).

@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch from 49f813a to 37ef4b2 Compare December 8, 2025 22:57
@github-actions
Copy link

github-actions bot commented Dec 8, 2025

🐧 Linux x64 Test Results

  • 7235 tests passed
  • 598 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 8, 2025

🪟 Windows x64 Test Results

  • 3358 tests passed
  • 411 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 8, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch 7 times, most recently from debb4a9 to b0028ca Compare December 9, 2025 22:03
@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch 5 times, most recently from 2e4e026 to 7533d56 Compare December 10, 2025 01:43
@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch from d9e35f0 to 9d203b2 Compare December 12, 2025 22:45
@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch from 6d89bc0 to 8e4a9ac Compare December 16, 2025 22:37
@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch from 8e4a9ac to 8cba26c Compare December 16, 2025 22:53
@makslevental makslevental marked this pull request as ready for review December 16, 2025 23:03
@makslevental makslevental requested a review from kuhar as a code owner December 16, 2025 23:03
@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2025

@llvm/pr-subscribers-mlir-math

Author: Maksim Levental (makslevental)

Changes

Patch is 42.95 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/171221.diff

17 Files Affected:

  • (added) mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h (+21)
  • (modified) mlir/include/mlir/Conversion/Passes.h (+1)
  • (modified) mlir/include/mlir/Conversion/Passes.td (+15)
  • (modified) mlir/include/mlir/Dialect/Func/Utils/Utils.h (+16)
  • (renamed) mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp (+25-67)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt (+49)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp (+210)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.cpp (+22)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.h (+21)
  • (removed) mlir/lib/Conversion/ArithToAPFloat/CMakeLists.txt (-19)
  • (modified) mlir/lib/Conversion/CMakeLists.txt (+1-1)
  • (modified) mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp (+5-2)
  • (modified) mlir/lib/Dialect/Func/Utils/Utils.cpp (+39)
  • (modified) mlir/lib/ExecutionEngine/APFloatWrappers.cpp (+64-1)
  • (renamed) mlir/test/Conversion/ArithAndMathToAPFloat/arith-to-apfloat.mlir ()
  • (added) mlir/test/Conversion/ArithAndMathToAPFloat/math-to-apfloat.mlir (+66)
  • (added) mlir/test/Integration/Dialect/Math/CPU/test-apfloat-emulation.mlir (+76)
diff --git a/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
new file mode 100644
index 0000000000000..86179a1611d5e
--- /dev/null
+++ b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
@@ -0,0 +1,21 @@
+//===- MathToAPFloat.h - Math to APFloat impl conversion ---*- C++ ------*-===//
+//
+// Part of the APFloat Project, under the Apache License v2.0 with APFloat
+// Exceptions. See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH APFloat-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+#define MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+
+#include <memory>
+
+namespace mlir {
+class Pass;
+
+#define GEN_PASS_DECL_MATHTOAPFLOATCONVERSIONPASS
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+#endif // MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h
index 82bdfd02661a6..05ec2f8ce2538 100644
--- a/mlir/include/mlir/Conversion/Passes.h
+++ b/mlir/include/mlir/Conversion/Passes.h
@@ -44,6 +44,7 @@
 #include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
 #include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
 #include "mlir/Conversion/LinalgToStandard/LinalgToStandard.h"
+#include "mlir/Conversion/MathToAPFloat/MathToAPFloat.h"
 #include "mlir/Conversion/MathToEmitC/MathToEmitCPass.h"
 #include "mlir/Conversion/MathToFuncs/MathToFuncs.h"
 #include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index fcbaf3ccc1486..7f24e58671aab 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -775,6 +775,21 @@ def ConvertMathToLibmPass : Pass<"convert-math-to-libm", "ModuleOp"> {
   ];
 }
 
+//===----------------------------------------------------------------------===//
+// MathToAPFloat
+//===----------------------------------------------------------------------===//
+
+def MathToAPFloatConversionPass
+    : Pass<"convert-math-to-apfloat", "ModuleOp"> {
+  let summary = "Convert Math ops to APFloat runtime library calls";
+  let description = [{
+    This pass converts supported Math ops to APFloat-based runtime library
+    calls (APFloatWrappers.cpp). APFloat is a software implementation of
+    floating-point mathmetic operations.
+  }];
+  let dependentDialects = ["math::MathDialect", "func::FuncDialect"];
+}
+
 //===----------------------------------------------------------------------===//
 // MathToLLVM
 //===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/Func/Utils/Utils.h b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
index 00d50874a2e8d..079c1f461b6ed 100644
--- a/mlir/include/mlir/Dialect/Func/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
@@ -67,6 +67,22 @@ FailureOr<FuncOp> lookupFnDecl(SymbolOpInterface symTable, StringRef name,
                                FunctionType funcT,
                                SymbolTableCollection *symbolTables = nullptr);
 
+/// Create a FuncOp decl and insert it into `symTable` operation. If
+/// `symbolTables` is provided, then the decl will be inserted into the
+/// SymbolTableCollection.
+FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                    FunctionType funcT, bool setPrivate,
+                    SymbolTableCollection *symbolTables = nullptr);
+
+/// Helper function to look up or create the symbol for a runtime library
+/// function with the given parameter types. Returns an int64_t, unless a
+/// different result type is specified.
+FailureOr<FuncOp>
+lookupOrCreateFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                     TypeRange paramTypes,
+                     SymbolTableCollection *symbolTables = nullptr,
+                     Type resultType = {});
+
 } // namespace func
 } // namespace mlir
 
diff --git a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
similarity index 88%
rename from mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
rename to mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
index 79816fc6e3bf1..b9ba94ef08098 100644
--- a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Conversion/ArithToAPFloat/ArithToAPFloat.h"
+#include "Utils.h"
 
 #include "mlir/Dialect/Arith/IR/Arith.h"
 #include "mlir/Dialect/Arith/Transforms/Passes.h"
@@ -25,47 +26,6 @@ namespace mlir {
 using namespace mlir;
 using namespace mlir::func;
 
-static FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable,
-                           StringRef name, FunctionType funcT, bool setPrivate,
-                           SymbolTableCollection *symbolTables = nullptr) {
-  OpBuilder::InsertionGuard g(b);
-  assert(!symTable->getRegion(0).empty() && "expected non-empty region");
-  b.setInsertionPointToStart(&symTable->getRegion(0).front());
-  FuncOp funcOp = FuncOp::create(b, symTable->getLoc(), name, funcT);
-  if (setPrivate)
-    funcOp.setPrivate();
-  if (symbolTables) {
-    SymbolTable &symbolTable = symbolTables->getSymbolTable(symTable);
-    symbolTable.insert(funcOp, symTable->getRegion(0).front().begin());
-  }
-  return funcOp;
-}
-
-/// Helper function to look up or create the symbol for a runtime library
-/// function with the given parameter types. Returns an int64_t, unless a
-/// different result type is specified.
-static FailureOr<FuncOp>
-lookupOrCreateApFloatFn(OpBuilder &b, SymbolOpInterface symTable,
-                        StringRef name, TypeRange paramTypes,
-                        SymbolTableCollection *symbolTables = nullptr,
-                        Type resultType = {}) {
-  if (!resultType)
-    resultType = IntegerType::get(symTable->getContext(), 64);
-  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
-  auto funcT = FunctionType::get(b.getContext(), paramTypes, {resultType});
-  FailureOr<FuncOp> func =
-      lookupFnDecl(symTable, funcName, funcT, symbolTables);
-  // Failed due to type mismatch.
-  if (failed(func))
-    return func;
-  // Successfully matched existing decl.
-  if (*func)
-    return *func;
-
-  return createFnDecl(b, symTable, funcName, funcT,
-                      /*setPrivate=*/true, symbolTables);
-}
-
 /// Helper function to look up or create the symbol for a runtime library
 /// function for a binary arithmetic operation.
 ///
@@ -81,14 +41,9 @@ lookupOrCreateBinaryFn(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
                        SymbolTableCollection *symbolTables = nullptr) {
   auto i32Type = IntegerType::get(symTable->getContext(), 32);
   auto i64Type = IntegerType::get(symTable->getContext(), 64);
-  return lookupOrCreateApFloatFn(b, symTable, name, {i32Type, i64Type, i64Type},
-                                 symbolTables);
-}
-
-static Value getSemanticsValue(OpBuilder &b, Location loc, FloatType floatTy) {
-  int32_t sem = llvm::APFloatBase::SemanticsToEnum(floatTy.getFloatSemantics());
-  return arith::ConstantOp::create(b, loc, b.getI32Type(),
-                                   b.getIntegerAttr(b.getI32Type(), sem));
+  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
+  return lookupOrCreateFnDecl(b, symTable, funcName,
+                              {i32Type, i64Type, i64Type}, symbolTables);
 }
 
 /// Given two operands of vector type and vector result type (with the same
@@ -197,7 +152,7 @@ struct BinaryArithOpToAPFloatConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -231,8 +186,9 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn = lookupOrCreateApFloatFn(
-        rewriter, symTable, "convert", {i32Type, i32Type, i64Type});
+    FailureOr<FuncOp> fn =
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert",
+                             {i32Type, i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -250,9 +206,10 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           std::array<Value, 3> params = {inSemValue, outSemValue, operandBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -289,8 +246,8 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_to_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert_to_int",
+                             {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -308,7 +265,7 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outIntTy = cast<IntegerType>(resultType);
           Value outWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
@@ -350,9 +307,9 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
     auto i1Type = IntegerType::get(symTable->getContext(), 1);
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_from_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_convert_from_int",
+        {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -377,7 +334,8 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
 
           // Call APFloat function.
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           Value inWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
               rewriter.getIntegerAttr(i32Type, inIntTy.getWidth()));
@@ -421,8 +379,8 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "compare",
-                                {i32Type, i64Type, i64Type}, nullptr, i8Type);
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_compare",
+                             {i32Type, i64Type, i64Type}, nullptr, i8Type);
     if (failed(fn))
       return fn;
 
@@ -443,7 +401,7 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           Value comparisonResult =
               func::CallOp::create(rewriter, loc, TypeRange(i8Type),
@@ -569,8 +527,8 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "neg", {i32Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_neg", {i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -588,7 +546,7 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, operand1));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, operandBits};
           Value negatedBits =
               func::CallOp::create(rewriter, loc, TypeRange(i64Type),
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
new file mode 100644
index 0000000000000..bad8226ac88ec
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
@@ -0,0 +1,49 @@
+add_mlir_library(ArithAndMathToAPFloatUtils
+  Utils.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  LINK_LIBS PUBLIC
+  MLIRArithDialect
+  )
+
+add_mlir_conversion_library(MLIRArithToAPFloat
+  ArithToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/ArithToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRArithDialect
+  MLIRArithTransforms
+  MLIRFuncDialect
+  MLIRFuncUtils
+  MLIRVectorDialect
+  )
+
+add_mlir_conversion_library(MLIRMathToAPFloat
+  MathToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/MathToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRMathDialect
+  MLIRFuncDialect
+  MLIRFuncUtils
+  )
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
new file mode 100644
index 0000000000000..9cd5a41daf7d8
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
@@ -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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
+    SmallVector<Value> params = {semValue, operandBits};
+    Value negatedBits = func::CallOp::create(rewriter, loc, TypeRange(i64Type),
+                                             SymbolRefAttr::get(*fn), params)
+                            ->getResult(0);
+
+    // Truncate result to the original width.
+    Value truncatedBits =
+        arith::TruncIOp::create(rewriter, loc, intWType, negatedBits);
+    rewriter.replaceOp(
+        op, arith::BitcastOp::create(rewriter, loc, floatTy, truncatedBits));
+    return success();
+  }
+
+  SymbolOpInterface symTable;
+};
+
+template <typename OpTy>
+struct IsOpToAPFloatConversion final : OpRewritePattern<OpTy> {
+  IsOpToAPFloatConversion(MLIRContext *context, const char *APFloatName,
+                          SymbolOpInterface symTable,
+                          PatternBenefit benefit = 1)
+      : OpRewritePattern<OpTy>(context, benefit), symTable(symTable),
+        APFloatName(APFloatName) {};
+
+  LogicalResult matchAndRewrite(OpTy op,
+                                PatternRewriter &rewriter) const override {
+    // Get APFloat function from runtime library.
+    auto i1 = IntegerType::get(symTable->getContext(), 1);
+    auto i32Type = IntegerType::get(symTable->getContext(), 32);
+    auto i64Type = IntegerType::get(symTable->getContext(), 64);
+    std::string funcName =
+        (llvm::Twine("_mlir_apfloat_is") + APFloatName).str();
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, funcName, {i32Type, i64Type}, nullptr, i1);
+    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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2025

@llvm/pr-subscribers-mlir-execution-engine

Author: Maksim Levental (makslevental)

Changes

Patch is 42.95 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/171221.diff

17 Files Affected:

  • (added) mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h (+21)
  • (modified) mlir/include/mlir/Conversion/Passes.h (+1)
  • (modified) mlir/include/mlir/Conversion/Passes.td (+15)
  • (modified) mlir/include/mlir/Dialect/Func/Utils/Utils.h (+16)
  • (renamed) mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp (+25-67)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt (+49)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp (+210)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.cpp (+22)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.h (+21)
  • (removed) mlir/lib/Conversion/ArithToAPFloat/CMakeLists.txt (-19)
  • (modified) mlir/lib/Conversion/CMakeLists.txt (+1-1)
  • (modified) mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp (+5-2)
  • (modified) mlir/lib/Dialect/Func/Utils/Utils.cpp (+39)
  • (modified) mlir/lib/ExecutionEngine/APFloatWrappers.cpp (+64-1)
  • (renamed) mlir/test/Conversion/ArithAndMathToAPFloat/arith-to-apfloat.mlir ()
  • (added) mlir/test/Conversion/ArithAndMathToAPFloat/math-to-apfloat.mlir (+66)
  • (added) mlir/test/Integration/Dialect/Math/CPU/test-apfloat-emulation.mlir (+76)
diff --git a/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
new file mode 100644
index 0000000000000..86179a1611d5e
--- /dev/null
+++ b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
@@ -0,0 +1,21 @@
+//===- MathToAPFloat.h - Math to APFloat impl conversion ---*- C++ ------*-===//
+//
+// Part of the APFloat Project, under the Apache License v2.0 with APFloat
+// Exceptions. See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH APFloat-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+#define MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+
+#include <memory>
+
+namespace mlir {
+class Pass;
+
+#define GEN_PASS_DECL_MATHTOAPFLOATCONVERSIONPASS
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+#endif // MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h
index 82bdfd02661a6..05ec2f8ce2538 100644
--- a/mlir/include/mlir/Conversion/Passes.h
+++ b/mlir/include/mlir/Conversion/Passes.h
@@ -44,6 +44,7 @@
 #include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
 #include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
 #include "mlir/Conversion/LinalgToStandard/LinalgToStandard.h"
+#include "mlir/Conversion/MathToAPFloat/MathToAPFloat.h"
 #include "mlir/Conversion/MathToEmitC/MathToEmitCPass.h"
 #include "mlir/Conversion/MathToFuncs/MathToFuncs.h"
 #include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index fcbaf3ccc1486..7f24e58671aab 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -775,6 +775,21 @@ def ConvertMathToLibmPass : Pass<"convert-math-to-libm", "ModuleOp"> {
   ];
 }
 
+//===----------------------------------------------------------------------===//
+// MathToAPFloat
+//===----------------------------------------------------------------------===//
+
+def MathToAPFloatConversionPass
+    : Pass<"convert-math-to-apfloat", "ModuleOp"> {
+  let summary = "Convert Math ops to APFloat runtime library calls";
+  let description = [{
+    This pass converts supported Math ops to APFloat-based runtime library
+    calls (APFloatWrappers.cpp). APFloat is a software implementation of
+    floating-point mathmetic operations.
+  }];
+  let dependentDialects = ["math::MathDialect", "func::FuncDialect"];
+}
+
 //===----------------------------------------------------------------------===//
 // MathToLLVM
 //===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/Func/Utils/Utils.h b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
index 00d50874a2e8d..079c1f461b6ed 100644
--- a/mlir/include/mlir/Dialect/Func/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
@@ -67,6 +67,22 @@ FailureOr<FuncOp> lookupFnDecl(SymbolOpInterface symTable, StringRef name,
                                FunctionType funcT,
                                SymbolTableCollection *symbolTables = nullptr);
 
+/// Create a FuncOp decl and insert it into `symTable` operation. If
+/// `symbolTables` is provided, then the decl will be inserted into the
+/// SymbolTableCollection.
+FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                    FunctionType funcT, bool setPrivate,
+                    SymbolTableCollection *symbolTables = nullptr);
+
+/// Helper function to look up or create the symbol for a runtime library
+/// function with the given parameter types. Returns an int64_t, unless a
+/// different result type is specified.
+FailureOr<FuncOp>
+lookupOrCreateFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                     TypeRange paramTypes,
+                     SymbolTableCollection *symbolTables = nullptr,
+                     Type resultType = {});
+
 } // namespace func
 } // namespace mlir
 
diff --git a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
similarity index 88%
rename from mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
rename to mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
index 79816fc6e3bf1..b9ba94ef08098 100644
--- a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Conversion/ArithToAPFloat/ArithToAPFloat.h"
+#include "Utils.h"
 
 #include "mlir/Dialect/Arith/IR/Arith.h"
 #include "mlir/Dialect/Arith/Transforms/Passes.h"
@@ -25,47 +26,6 @@ namespace mlir {
 using namespace mlir;
 using namespace mlir::func;
 
-static FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable,
-                           StringRef name, FunctionType funcT, bool setPrivate,
-                           SymbolTableCollection *symbolTables = nullptr) {
-  OpBuilder::InsertionGuard g(b);
-  assert(!symTable->getRegion(0).empty() && "expected non-empty region");
-  b.setInsertionPointToStart(&symTable->getRegion(0).front());
-  FuncOp funcOp = FuncOp::create(b, symTable->getLoc(), name, funcT);
-  if (setPrivate)
-    funcOp.setPrivate();
-  if (symbolTables) {
-    SymbolTable &symbolTable = symbolTables->getSymbolTable(symTable);
-    symbolTable.insert(funcOp, symTable->getRegion(0).front().begin());
-  }
-  return funcOp;
-}
-
-/// Helper function to look up or create the symbol for a runtime library
-/// function with the given parameter types. Returns an int64_t, unless a
-/// different result type is specified.
-static FailureOr<FuncOp>
-lookupOrCreateApFloatFn(OpBuilder &b, SymbolOpInterface symTable,
-                        StringRef name, TypeRange paramTypes,
-                        SymbolTableCollection *symbolTables = nullptr,
-                        Type resultType = {}) {
-  if (!resultType)
-    resultType = IntegerType::get(symTable->getContext(), 64);
-  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
-  auto funcT = FunctionType::get(b.getContext(), paramTypes, {resultType});
-  FailureOr<FuncOp> func =
-      lookupFnDecl(symTable, funcName, funcT, symbolTables);
-  // Failed due to type mismatch.
-  if (failed(func))
-    return func;
-  // Successfully matched existing decl.
-  if (*func)
-    return *func;
-
-  return createFnDecl(b, symTable, funcName, funcT,
-                      /*setPrivate=*/true, symbolTables);
-}
-
 /// Helper function to look up or create the symbol for a runtime library
 /// function for a binary arithmetic operation.
 ///
@@ -81,14 +41,9 @@ lookupOrCreateBinaryFn(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
                        SymbolTableCollection *symbolTables = nullptr) {
   auto i32Type = IntegerType::get(symTable->getContext(), 32);
   auto i64Type = IntegerType::get(symTable->getContext(), 64);
-  return lookupOrCreateApFloatFn(b, symTable, name, {i32Type, i64Type, i64Type},
-                                 symbolTables);
-}
-
-static Value getSemanticsValue(OpBuilder &b, Location loc, FloatType floatTy) {
-  int32_t sem = llvm::APFloatBase::SemanticsToEnum(floatTy.getFloatSemantics());
-  return arith::ConstantOp::create(b, loc, b.getI32Type(),
-                                   b.getIntegerAttr(b.getI32Type(), sem));
+  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
+  return lookupOrCreateFnDecl(b, symTable, funcName,
+                              {i32Type, i64Type, i64Type}, symbolTables);
 }
 
 /// Given two operands of vector type and vector result type (with the same
@@ -197,7 +152,7 @@ struct BinaryArithOpToAPFloatConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -231,8 +186,9 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn = lookupOrCreateApFloatFn(
-        rewriter, symTable, "convert", {i32Type, i32Type, i64Type});
+    FailureOr<FuncOp> fn =
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert",
+                             {i32Type, i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -250,9 +206,10 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           std::array<Value, 3> params = {inSemValue, outSemValue, operandBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -289,8 +246,8 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_to_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert_to_int",
+                             {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -308,7 +265,7 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outIntTy = cast<IntegerType>(resultType);
           Value outWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
@@ -350,9 +307,9 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
     auto i1Type = IntegerType::get(symTable->getContext(), 1);
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_from_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_convert_from_int",
+        {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -377,7 +334,8 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
 
           // Call APFloat function.
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           Value inWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
               rewriter.getIntegerAttr(i32Type, inIntTy.getWidth()));
@@ -421,8 +379,8 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "compare",
-                                {i32Type, i64Type, i64Type}, nullptr, i8Type);
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_compare",
+                             {i32Type, i64Type, i64Type}, nullptr, i8Type);
     if (failed(fn))
       return fn;
 
@@ -443,7 +401,7 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           Value comparisonResult =
               func::CallOp::create(rewriter, loc, TypeRange(i8Type),
@@ -569,8 +527,8 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "neg", {i32Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_neg", {i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -588,7 +546,7 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, operand1));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, operandBits};
           Value negatedBits =
               func::CallOp::create(rewriter, loc, TypeRange(i64Type),
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
new file mode 100644
index 0000000000000..bad8226ac88ec
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
@@ -0,0 +1,49 @@
+add_mlir_library(ArithAndMathToAPFloatUtils
+  Utils.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  LINK_LIBS PUBLIC
+  MLIRArithDialect
+  )
+
+add_mlir_conversion_library(MLIRArithToAPFloat
+  ArithToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/ArithToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRArithDialect
+  MLIRArithTransforms
+  MLIRFuncDialect
+  MLIRFuncUtils
+  MLIRVectorDialect
+  )
+
+add_mlir_conversion_library(MLIRMathToAPFloat
+  MathToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/MathToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRMathDialect
+  MLIRFuncDialect
+  MLIRFuncUtils
+  )
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
new file mode 100644
index 0000000000000..9cd5a41daf7d8
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
@@ -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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
+    SmallVector<Value> params = {semValue, operandBits};
+    Value negatedBits = func::CallOp::create(rewriter, loc, TypeRange(i64Type),
+                                             SymbolRefAttr::get(*fn), params)
+                            ->getResult(0);
+
+    // Truncate result to the original width.
+    Value truncatedBits =
+        arith::TruncIOp::create(rewriter, loc, intWType, negatedBits);
+    rewriter.replaceOp(
+        op, arith::BitcastOp::create(rewriter, loc, floatTy, truncatedBits));
+    return success();
+  }
+
+  SymbolOpInterface symTable;
+};
+
+template <typename OpTy>
+struct IsOpToAPFloatConversion final : OpRewritePattern<OpTy> {
+  IsOpToAPFloatConversion(MLIRContext *context, const char *APFloatName,
+                          SymbolOpInterface symTable,
+                          PatternBenefit benefit = 1)
+      : OpRewritePattern<OpTy>(context, benefit), symTable(symTable),
+        APFloatName(APFloatName) {};
+
+  LogicalResult matchAndRewrite(OpTy op,
+                                PatternRewriter &rewriter) const override {
+    // Get APFloat function from runtime library.
+    auto i1 = IntegerType::get(symTable->getContext(), 1);
+    auto i32Type = IntegerType::get(symTable->getContext(), 32);
+    auto i64Type = IntegerType::get(symTable->getContext(), 64);
+    std::string funcName =
+        (llvm::Twine("_mlir_apfloat_is") + APFloatName).str();
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, funcName, {i32Type, i64Type}, nullptr, i1);
+    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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2025

@llvm/pr-subscribers-mlir-func

Author: Maksim Levental (makslevental)

Changes

Patch is 42.95 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/171221.diff

17 Files Affected:

  • (added) mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h (+21)
  • (modified) mlir/include/mlir/Conversion/Passes.h (+1)
  • (modified) mlir/include/mlir/Conversion/Passes.td (+15)
  • (modified) mlir/include/mlir/Dialect/Func/Utils/Utils.h (+16)
  • (renamed) mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp (+25-67)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt (+49)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp (+210)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.cpp (+22)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.h (+21)
  • (removed) mlir/lib/Conversion/ArithToAPFloat/CMakeLists.txt (-19)
  • (modified) mlir/lib/Conversion/CMakeLists.txt (+1-1)
  • (modified) mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp (+5-2)
  • (modified) mlir/lib/Dialect/Func/Utils/Utils.cpp (+39)
  • (modified) mlir/lib/ExecutionEngine/APFloatWrappers.cpp (+64-1)
  • (renamed) mlir/test/Conversion/ArithAndMathToAPFloat/arith-to-apfloat.mlir ()
  • (added) mlir/test/Conversion/ArithAndMathToAPFloat/math-to-apfloat.mlir (+66)
  • (added) mlir/test/Integration/Dialect/Math/CPU/test-apfloat-emulation.mlir (+76)
diff --git a/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
new file mode 100644
index 0000000000000..86179a1611d5e
--- /dev/null
+++ b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
@@ -0,0 +1,21 @@
+//===- MathToAPFloat.h - Math to APFloat impl conversion ---*- C++ ------*-===//
+//
+// Part of the APFloat Project, under the Apache License v2.0 with APFloat
+// Exceptions. See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH APFloat-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+#define MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+
+#include <memory>
+
+namespace mlir {
+class Pass;
+
+#define GEN_PASS_DECL_MATHTOAPFLOATCONVERSIONPASS
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+#endif // MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h
index 82bdfd02661a6..05ec2f8ce2538 100644
--- a/mlir/include/mlir/Conversion/Passes.h
+++ b/mlir/include/mlir/Conversion/Passes.h
@@ -44,6 +44,7 @@
 #include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
 #include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
 #include "mlir/Conversion/LinalgToStandard/LinalgToStandard.h"
+#include "mlir/Conversion/MathToAPFloat/MathToAPFloat.h"
 #include "mlir/Conversion/MathToEmitC/MathToEmitCPass.h"
 #include "mlir/Conversion/MathToFuncs/MathToFuncs.h"
 #include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index fcbaf3ccc1486..7f24e58671aab 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -775,6 +775,21 @@ def ConvertMathToLibmPass : Pass<"convert-math-to-libm", "ModuleOp"> {
   ];
 }
 
+//===----------------------------------------------------------------------===//
+// MathToAPFloat
+//===----------------------------------------------------------------------===//
+
+def MathToAPFloatConversionPass
+    : Pass<"convert-math-to-apfloat", "ModuleOp"> {
+  let summary = "Convert Math ops to APFloat runtime library calls";
+  let description = [{
+    This pass converts supported Math ops to APFloat-based runtime library
+    calls (APFloatWrappers.cpp). APFloat is a software implementation of
+    floating-point mathmetic operations.
+  }];
+  let dependentDialects = ["math::MathDialect", "func::FuncDialect"];
+}
+
 //===----------------------------------------------------------------------===//
 // MathToLLVM
 //===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/Func/Utils/Utils.h b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
index 00d50874a2e8d..079c1f461b6ed 100644
--- a/mlir/include/mlir/Dialect/Func/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
@@ -67,6 +67,22 @@ FailureOr<FuncOp> lookupFnDecl(SymbolOpInterface symTable, StringRef name,
                                FunctionType funcT,
                                SymbolTableCollection *symbolTables = nullptr);
 
+/// Create a FuncOp decl and insert it into `symTable` operation. If
+/// `symbolTables` is provided, then the decl will be inserted into the
+/// SymbolTableCollection.
+FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                    FunctionType funcT, bool setPrivate,
+                    SymbolTableCollection *symbolTables = nullptr);
+
+/// Helper function to look up or create the symbol for a runtime library
+/// function with the given parameter types. Returns an int64_t, unless a
+/// different result type is specified.
+FailureOr<FuncOp>
+lookupOrCreateFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                     TypeRange paramTypes,
+                     SymbolTableCollection *symbolTables = nullptr,
+                     Type resultType = {});
+
 } // namespace func
 } // namespace mlir
 
diff --git a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
similarity index 88%
rename from mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
rename to mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
index 79816fc6e3bf1..b9ba94ef08098 100644
--- a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Conversion/ArithToAPFloat/ArithToAPFloat.h"
+#include "Utils.h"
 
 #include "mlir/Dialect/Arith/IR/Arith.h"
 #include "mlir/Dialect/Arith/Transforms/Passes.h"
@@ -25,47 +26,6 @@ namespace mlir {
 using namespace mlir;
 using namespace mlir::func;
 
-static FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable,
-                           StringRef name, FunctionType funcT, bool setPrivate,
-                           SymbolTableCollection *symbolTables = nullptr) {
-  OpBuilder::InsertionGuard g(b);
-  assert(!symTable->getRegion(0).empty() && "expected non-empty region");
-  b.setInsertionPointToStart(&symTable->getRegion(0).front());
-  FuncOp funcOp = FuncOp::create(b, symTable->getLoc(), name, funcT);
-  if (setPrivate)
-    funcOp.setPrivate();
-  if (symbolTables) {
-    SymbolTable &symbolTable = symbolTables->getSymbolTable(symTable);
-    symbolTable.insert(funcOp, symTable->getRegion(0).front().begin());
-  }
-  return funcOp;
-}
-
-/// Helper function to look up or create the symbol for a runtime library
-/// function with the given parameter types. Returns an int64_t, unless a
-/// different result type is specified.
-static FailureOr<FuncOp>
-lookupOrCreateApFloatFn(OpBuilder &b, SymbolOpInterface symTable,
-                        StringRef name, TypeRange paramTypes,
-                        SymbolTableCollection *symbolTables = nullptr,
-                        Type resultType = {}) {
-  if (!resultType)
-    resultType = IntegerType::get(symTable->getContext(), 64);
-  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
-  auto funcT = FunctionType::get(b.getContext(), paramTypes, {resultType});
-  FailureOr<FuncOp> func =
-      lookupFnDecl(symTable, funcName, funcT, symbolTables);
-  // Failed due to type mismatch.
-  if (failed(func))
-    return func;
-  // Successfully matched existing decl.
-  if (*func)
-    return *func;
-
-  return createFnDecl(b, symTable, funcName, funcT,
-                      /*setPrivate=*/true, symbolTables);
-}
-
 /// Helper function to look up or create the symbol for a runtime library
 /// function for a binary arithmetic operation.
 ///
@@ -81,14 +41,9 @@ lookupOrCreateBinaryFn(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
                        SymbolTableCollection *symbolTables = nullptr) {
   auto i32Type = IntegerType::get(symTable->getContext(), 32);
   auto i64Type = IntegerType::get(symTable->getContext(), 64);
-  return lookupOrCreateApFloatFn(b, symTable, name, {i32Type, i64Type, i64Type},
-                                 symbolTables);
-}
-
-static Value getSemanticsValue(OpBuilder &b, Location loc, FloatType floatTy) {
-  int32_t sem = llvm::APFloatBase::SemanticsToEnum(floatTy.getFloatSemantics());
-  return arith::ConstantOp::create(b, loc, b.getI32Type(),
-                                   b.getIntegerAttr(b.getI32Type(), sem));
+  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
+  return lookupOrCreateFnDecl(b, symTable, funcName,
+                              {i32Type, i64Type, i64Type}, symbolTables);
 }
 
 /// Given two operands of vector type and vector result type (with the same
@@ -197,7 +152,7 @@ struct BinaryArithOpToAPFloatConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -231,8 +186,9 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn = lookupOrCreateApFloatFn(
-        rewriter, symTable, "convert", {i32Type, i32Type, i64Type});
+    FailureOr<FuncOp> fn =
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert",
+                             {i32Type, i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -250,9 +206,10 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           std::array<Value, 3> params = {inSemValue, outSemValue, operandBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -289,8 +246,8 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_to_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert_to_int",
+                             {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -308,7 +265,7 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outIntTy = cast<IntegerType>(resultType);
           Value outWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
@@ -350,9 +307,9 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
     auto i1Type = IntegerType::get(symTable->getContext(), 1);
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_from_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_convert_from_int",
+        {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -377,7 +334,8 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
 
           // Call APFloat function.
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           Value inWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
               rewriter.getIntegerAttr(i32Type, inIntTy.getWidth()));
@@ -421,8 +379,8 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "compare",
-                                {i32Type, i64Type, i64Type}, nullptr, i8Type);
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_compare",
+                             {i32Type, i64Type, i64Type}, nullptr, i8Type);
     if (failed(fn))
       return fn;
 
@@ -443,7 +401,7 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           Value comparisonResult =
               func::CallOp::create(rewriter, loc, TypeRange(i8Type),
@@ -569,8 +527,8 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "neg", {i32Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_neg", {i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -588,7 +546,7 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, operand1));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, operandBits};
           Value negatedBits =
               func::CallOp::create(rewriter, loc, TypeRange(i64Type),
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
new file mode 100644
index 0000000000000..bad8226ac88ec
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
@@ -0,0 +1,49 @@
+add_mlir_library(ArithAndMathToAPFloatUtils
+  Utils.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  LINK_LIBS PUBLIC
+  MLIRArithDialect
+  )
+
+add_mlir_conversion_library(MLIRArithToAPFloat
+  ArithToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/ArithToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRArithDialect
+  MLIRArithTransforms
+  MLIRFuncDialect
+  MLIRFuncUtils
+  MLIRVectorDialect
+  )
+
+add_mlir_conversion_library(MLIRMathToAPFloat
+  MathToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/MathToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRMathDialect
+  MLIRFuncDialect
+  MLIRFuncUtils
+  )
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
new file mode 100644
index 0000000000000..9cd5a41daf7d8
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
@@ -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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
+    SmallVector<Value> params = {semValue, operandBits};
+    Value negatedBits = func::CallOp::create(rewriter, loc, TypeRange(i64Type),
+                                             SymbolRefAttr::get(*fn), params)
+                            ->getResult(0);
+
+    // Truncate result to the original width.
+    Value truncatedBits =
+        arith::TruncIOp::create(rewriter, loc, intWType, negatedBits);
+    rewriter.replaceOp(
+        op, arith::BitcastOp::create(rewriter, loc, floatTy, truncatedBits));
+    return success();
+  }
+
+  SymbolOpInterface symTable;
+};
+
+template <typename OpTy>
+struct IsOpToAPFloatConversion final : OpRewritePattern<OpTy> {
+  IsOpToAPFloatConversion(MLIRContext *context, const char *APFloatName,
+                          SymbolOpInterface symTable,
+                          PatternBenefit benefit = 1)
+      : OpRewritePattern<OpTy>(context, benefit), symTable(symTable),
+        APFloatName(APFloatName) {};
+
+  LogicalResult matchAndRewrite(OpTy op,
+                                PatternRewriter &rewriter) const override {
+    // Get APFloat function from runtime library.
+    auto i1 = IntegerType::get(symTable->getContext(), 1);
+    auto i32Type = IntegerType::get(symTable->getContext(), 32);
+    auto i64Type = IntegerType::get(symTable->getContext(), 64);
+    std::string funcName =
+        (llvm::Twine("_mlir_apfloat_is") + APFloatName).str();
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, funcName, {i32Type, i64Type}, nullptr, i1);
+    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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2025

@llvm/pr-subscribers-mlir

Author: Maksim Levental (makslevental)

Changes

Patch is 42.95 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/171221.diff

17 Files Affected:

  • (added) mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h (+21)
  • (modified) mlir/include/mlir/Conversion/Passes.h (+1)
  • (modified) mlir/include/mlir/Conversion/Passes.td (+15)
  • (modified) mlir/include/mlir/Dialect/Func/Utils/Utils.h (+16)
  • (renamed) mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp (+25-67)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt (+49)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp (+210)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.cpp (+22)
  • (added) mlir/lib/Conversion/ArithAndMathToAPFloat/Utils.h (+21)
  • (removed) mlir/lib/Conversion/ArithToAPFloat/CMakeLists.txt (-19)
  • (modified) mlir/lib/Conversion/CMakeLists.txt (+1-1)
  • (modified) mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp (+5-2)
  • (modified) mlir/lib/Dialect/Func/Utils/Utils.cpp (+39)
  • (modified) mlir/lib/ExecutionEngine/APFloatWrappers.cpp (+64-1)
  • (renamed) mlir/test/Conversion/ArithAndMathToAPFloat/arith-to-apfloat.mlir ()
  • (added) mlir/test/Conversion/ArithAndMathToAPFloat/math-to-apfloat.mlir (+66)
  • (added) mlir/test/Integration/Dialect/Math/CPU/test-apfloat-emulation.mlir (+76)
diff --git a/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
new file mode 100644
index 0000000000000..86179a1611d5e
--- /dev/null
+++ b/mlir/include/mlir/Conversion/MathToAPFloat/MathToAPFloat.h
@@ -0,0 +1,21 @@
+//===- MathToAPFloat.h - Math to APFloat impl conversion ---*- C++ ------*-===//
+//
+// Part of the APFloat Project, under the Apache License v2.0 with APFloat
+// Exceptions. See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH APFloat-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+#define MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
+
+#include <memory>
+
+namespace mlir {
+class Pass;
+
+#define GEN_PASS_DECL_MATHTOAPFLOATCONVERSIONPASS
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+#endif // MLIR_CONVERSION_MATHTOAPFLOAT_MATHTOAPFLOAT_H
diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h
index 82bdfd02661a6..05ec2f8ce2538 100644
--- a/mlir/include/mlir/Conversion/Passes.h
+++ b/mlir/include/mlir/Conversion/Passes.h
@@ -44,6 +44,7 @@
 #include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
 #include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
 #include "mlir/Conversion/LinalgToStandard/LinalgToStandard.h"
+#include "mlir/Conversion/MathToAPFloat/MathToAPFloat.h"
 #include "mlir/Conversion/MathToEmitC/MathToEmitCPass.h"
 #include "mlir/Conversion/MathToFuncs/MathToFuncs.h"
 #include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index fcbaf3ccc1486..7f24e58671aab 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -775,6 +775,21 @@ def ConvertMathToLibmPass : Pass<"convert-math-to-libm", "ModuleOp"> {
   ];
 }
 
+//===----------------------------------------------------------------------===//
+// MathToAPFloat
+//===----------------------------------------------------------------------===//
+
+def MathToAPFloatConversionPass
+    : Pass<"convert-math-to-apfloat", "ModuleOp"> {
+  let summary = "Convert Math ops to APFloat runtime library calls";
+  let description = [{
+    This pass converts supported Math ops to APFloat-based runtime library
+    calls (APFloatWrappers.cpp). APFloat is a software implementation of
+    floating-point mathmetic operations.
+  }];
+  let dependentDialects = ["math::MathDialect", "func::FuncDialect"];
+}
+
 //===----------------------------------------------------------------------===//
 // MathToLLVM
 //===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/Func/Utils/Utils.h b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
index 00d50874a2e8d..079c1f461b6ed 100644
--- a/mlir/include/mlir/Dialect/Func/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/Func/Utils/Utils.h
@@ -67,6 +67,22 @@ FailureOr<FuncOp> lookupFnDecl(SymbolOpInterface symTable, StringRef name,
                                FunctionType funcT,
                                SymbolTableCollection *symbolTables = nullptr);
 
+/// Create a FuncOp decl and insert it into `symTable` operation. If
+/// `symbolTables` is provided, then the decl will be inserted into the
+/// SymbolTableCollection.
+FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                    FunctionType funcT, bool setPrivate,
+                    SymbolTableCollection *symbolTables = nullptr);
+
+/// Helper function to look up or create the symbol for a runtime library
+/// function with the given parameter types. Returns an int64_t, unless a
+/// different result type is specified.
+FailureOr<FuncOp>
+lookupOrCreateFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
+                     TypeRange paramTypes,
+                     SymbolTableCollection *symbolTables = nullptr,
+                     Type resultType = {});
+
 } // namespace func
 } // namespace mlir
 
diff --git a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
similarity index 88%
rename from mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
rename to mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
index 79816fc6e3bf1..b9ba94ef08098 100644
--- a/mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Conversion/ArithToAPFloat/ArithToAPFloat.h"
+#include "Utils.h"
 
 #include "mlir/Dialect/Arith/IR/Arith.h"
 #include "mlir/Dialect/Arith/Transforms/Passes.h"
@@ -25,47 +26,6 @@ namespace mlir {
 using namespace mlir;
 using namespace mlir::func;
 
-static FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable,
-                           StringRef name, FunctionType funcT, bool setPrivate,
-                           SymbolTableCollection *symbolTables = nullptr) {
-  OpBuilder::InsertionGuard g(b);
-  assert(!symTable->getRegion(0).empty() && "expected non-empty region");
-  b.setInsertionPointToStart(&symTable->getRegion(0).front());
-  FuncOp funcOp = FuncOp::create(b, symTable->getLoc(), name, funcT);
-  if (setPrivate)
-    funcOp.setPrivate();
-  if (symbolTables) {
-    SymbolTable &symbolTable = symbolTables->getSymbolTable(symTable);
-    symbolTable.insert(funcOp, symTable->getRegion(0).front().begin());
-  }
-  return funcOp;
-}
-
-/// Helper function to look up or create the symbol for a runtime library
-/// function with the given parameter types. Returns an int64_t, unless a
-/// different result type is specified.
-static FailureOr<FuncOp>
-lookupOrCreateApFloatFn(OpBuilder &b, SymbolOpInterface symTable,
-                        StringRef name, TypeRange paramTypes,
-                        SymbolTableCollection *symbolTables = nullptr,
-                        Type resultType = {}) {
-  if (!resultType)
-    resultType = IntegerType::get(symTable->getContext(), 64);
-  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
-  auto funcT = FunctionType::get(b.getContext(), paramTypes, {resultType});
-  FailureOr<FuncOp> func =
-      lookupFnDecl(symTable, funcName, funcT, symbolTables);
-  // Failed due to type mismatch.
-  if (failed(func))
-    return func;
-  // Successfully matched existing decl.
-  if (*func)
-    return *func;
-
-  return createFnDecl(b, symTable, funcName, funcT,
-                      /*setPrivate=*/true, symbolTables);
-}
-
 /// Helper function to look up or create the symbol for a runtime library
 /// function for a binary arithmetic operation.
 ///
@@ -81,14 +41,9 @@ lookupOrCreateBinaryFn(OpBuilder &b, SymbolOpInterface symTable, StringRef name,
                        SymbolTableCollection *symbolTables = nullptr) {
   auto i32Type = IntegerType::get(symTable->getContext(), 32);
   auto i64Type = IntegerType::get(symTable->getContext(), 64);
-  return lookupOrCreateApFloatFn(b, symTable, name, {i32Type, i64Type, i64Type},
-                                 symbolTables);
-}
-
-static Value getSemanticsValue(OpBuilder &b, Location loc, FloatType floatTy) {
-  int32_t sem = llvm::APFloatBase::SemanticsToEnum(floatTy.getFloatSemantics());
-  return arith::ConstantOp::create(b, loc, b.getI32Type(),
-                                   b.getIntegerAttr(b.getI32Type(), sem));
+  std::string funcName = (llvm::Twine("_mlir_apfloat_") + name).str();
+  return lookupOrCreateFnDecl(b, symTable, funcName,
+                              {i32Type, i64Type, i64Type}, symbolTables);
 }
 
 /// Given two operands of vector type and vector result type (with the same
@@ -197,7 +152,7 @@ struct BinaryArithOpToAPFloatConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -231,8 +186,9 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn = lookupOrCreateApFloatFn(
-        rewriter, symTable, "convert", {i32Type, i32Type, i64Type});
+    FailureOr<FuncOp> fn =
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert",
+                             {i32Type, i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -250,9 +206,10 @@ struct FpToFpConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           std::array<Value, 3> params = {inSemValue, outSemValue, operandBits};
           auto resultOp = func::CallOp::create(rewriter, loc,
                                                TypeRange(rewriter.getI64Type()),
@@ -289,8 +246,8 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_to_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_convert_to_int",
+                             {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -308,7 +265,7 @@ struct FpToIntConversion final : OpRewritePattern<OpTy> {
               arith::BitcastOp::create(rewriter, loc, inIntWType, operand1));
 
           // Call APFloat function.
-          Value inSemValue = getSemanticsValue(rewriter, loc, inFloatTy);
+          Value inSemValue = getAPFloatSemanticsValue(rewriter, loc, inFloatTy);
           auto outIntTy = cast<IntegerType>(resultType);
           Value outWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
@@ -350,9 +307,9 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
     auto i1Type = IntegerType::get(symTable->getContext(), 1);
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "convert_from_int",
-                                {i32Type, i32Type, i1Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_convert_from_int",
+        {i32Type, i32Type, i1Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -377,7 +334,8 @@ struct IntToFpConversion final : OpRewritePattern<OpTy> {
 
           // Call APFloat function.
           auto outFloatTy = cast<FloatType>(resultType);
-          Value outSemValue = getSemanticsValue(rewriter, loc, outFloatTy);
+          Value outSemValue =
+              getAPFloatSemanticsValue(rewriter, loc, outFloatTy);
           Value inWidthValue = arith::ConstantOp::create(
               rewriter, loc, i32Type,
               rewriter.getIntegerAttr(i32Type, inIntTy.getWidth()));
@@ -421,8 +379,8 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
     FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "compare",
-                                {i32Type, i64Type, i64Type}, nullptr, i8Type);
+        lookupOrCreateFnDecl(rewriter, symTable, "_mlir_apfloat_compare",
+                             {i32Type, i64Type, i64Type}, nullptr, i8Type);
     if (failed(fn))
       return fn;
 
@@ -443,7 +401,7 @@ struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, rhs));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, lhsBits, rhsBits};
           Value comparisonResult =
               func::CallOp::create(rewriter, loc, TypeRange(i8Type),
@@ -569,8 +527,8 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
     // Get APFloat function from runtime library.
     auto i32Type = IntegerType::get(symTable->getContext(), 32);
     auto i64Type = IntegerType::get(symTable->getContext(), 64);
-    FailureOr<FuncOp> fn =
-        lookupOrCreateApFloatFn(rewriter, symTable, "neg", {i32Type, i64Type});
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, "_mlir_apfloat_neg", {i32Type, i64Type});
     if (failed(fn))
       return fn;
 
@@ -588,7 +546,7 @@ struct NegFOpToAPFloatConversion final : OpRewritePattern<arith::NegFOp> {
               arith::BitcastOp::create(rewriter, loc, intWType, operand1));
 
           // Call APFloat function.
-          Value semValue = getSemanticsValue(rewriter, loc, floatTy);
+          Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
           SmallVector<Value> params = {semValue, operandBits};
           Value negatedBits =
               func::CallOp::create(rewriter, loc, TypeRange(i64Type),
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
new file mode 100644
index 0000000000000..bad8226ac88ec
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeLists.txt
@@ -0,0 +1,49 @@
+add_mlir_library(ArithAndMathToAPFloatUtils
+  Utils.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  LINK_LIBS PUBLIC
+  MLIRArithDialect
+  )
+
+add_mlir_conversion_library(MLIRArithToAPFloat
+  ArithToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/ArithToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRArithDialect
+  MLIRArithTransforms
+  MLIRFuncDialect
+  MLIRFuncUtils
+  MLIRVectorDialect
+  )
+
+add_mlir_conversion_library(MLIRMathToAPFloat
+  MathToAPFloat.cpp
+  PARTIAL_SOURCES_INTENDED
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/MathToLLVM
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  ArithAndMathToAPFloatUtils
+  MLIRMathDialect
+  MLIRFuncDialect
+  MLIRFuncUtils
+  )
diff --git a/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
new file mode 100644
index 0000000000000..9cd5a41daf7d8
--- /dev/null
+++ b/mlir/lib/Conversion/ArithAndMathToAPFloat/MathToAPFloat.cpp
@@ -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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue = getAPFloatSemanticsValue(rewriter, loc, floatTy);
+    SmallVector<Value> params = {semValue, operandBits};
+    Value negatedBits = func::CallOp::create(rewriter, loc, TypeRange(i64Type),
+                                             SymbolRefAttr::get(*fn), params)
+                            ->getResult(0);
+
+    // Truncate result to the original width.
+    Value truncatedBits =
+        arith::TruncIOp::create(rewriter, loc, intWType, negatedBits);
+    rewriter.replaceOp(
+        op, arith::BitcastOp::create(rewriter, loc, floatTy, truncatedBits));
+    return success();
+  }
+
+  SymbolOpInterface symTable;
+};
+
+template <typename OpTy>
+struct IsOpToAPFloatConversion final : OpRewritePattern<OpTy> {
+  IsOpToAPFloatConversion(MLIRContext *context, const char *APFloatName,
+                          SymbolOpInterface symTable,
+                          PatternBenefit benefit = 1)
+      : OpRewritePattern<OpTy>(context, benefit), symTable(symTable),
+        APFloatName(APFloatName) {};
+
+  LogicalResult matchAndRewrite(OpTy op,
+                                PatternRewriter &rewriter) const override {
+    // Get APFloat function from runtime library.
+    auto i1 = IntegerType::get(symTable->getContext(), 1);
+    auto i32Type = IntegerType::get(symTable->getContext(), 32);
+    auto i64Type = IntegerType::get(symTable->getContext(), 64);
+    std::string funcName =
+        (llvm::Twine("_mlir_apfloat_is") + APFloatName).str();
+    FailureOr<FuncOp> fn = lookupOrCreateFnDecl(
+        rewriter, symTable, funcName, {i32Type, i64Type}, nullptr, i1);
+    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,
+                                         "bitwidth > 64 bits is not supported");
+    }
+    auto intWType = rewriter.getIntegerType(floatTy.getWidth());
+    Value operandBits = arith::ExtUIOp::create(
+        rewriter, loc, i64Type,
+        arith::BitcastOp::create(rewriter, loc, intWType, operand));
+
+    // Call APFloat function.
+    Value semValue...
[truncated]

auto operand = op.getOperand();
auto floatTy = cast<FloatType>(operand.getType());
if (floatTy.getIntOrFloatBitWidth() > 64) {
return rewriter.notifyMatchFailure(op,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"return failure/notifyMatchFailure()" should be before any kind of IR modification. (lookupOrCreateFnDecl modifies the IR.)

rewriter.setInsertionPoint(op);
// Cast operands to 64-bit integers.
auto operand = op.getOperand();
auto floatTy = cast<FloatType>(operand.getType());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The operand type could be a vector. I added support to ArithToAPFloat recently to support vectors. Maybe we can follow the same design here. (This PR is already large enough, I would just return failure for now if it's not a float type.)

// RUN: --shared-libs=%mlir_c_runner_utils \
// RUN: --shared-libs=%mlir_apfloat_wrappers | FileCheck %s --check-prefix=CHECK-FP8

// Case 2: Only unsupported arithmetics is lowered through APFloat.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more interesting to also lower f32 to APFloat emulation. The reason why I had Case 1 and Case 2 in the other test case is to show that the order of the two passes --convert-math-to-apfloat --convert-to-llvm matters. We already did that in the other test-apfloat-emulation integration test. We don't get any extra test coverage by lowering to LLVM.

I'll leave it up to you, but I would drop Case 2 and use a single FileCheck and entry func for both f8 and f32.

@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch from 1d77e12 to 8e33e31 Compare December 17, 2025 18:28
@makslevental makslevental force-pushed the users/makslevental/math-to-apfloat branch from 8e33e31 to e368626 Compare December 17, 2025 18:29
@makslevental makslevental enabled auto-merge (squash) December 17, 2025 18:29
@makslevental makslevental merged commit 7f1a30e into main Dec 17, 2025
10 checks passed
@makslevental makslevental deleted the users/makslevental/math-to-apfloat branch December 17, 2025 18:37
@keith
Copy link
Member

keith commented Dec 17, 2025

should this directory be renamed as well? ./mlir/include/mlir/Conversion/ArithToAPFloat

@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 17, 2025

LLVM Buildbot has detected a new failure on builder mlir-nvidia running on mlir-nvidia while building mlir at step 6 "build-check-mlir-build-only".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/138/builds/23270

Here is the relevant piece of the build log for the reference
Step 6 (build-check-mlir-build-only) failure: build (failure)
...
212.447 [1315/16/4243] Creating library symlink lib/libMLIRTilingInterface.so
212.537 [1314/16/4244] Linking CXX shared library lib/libMLIRFuncUtils.so.22.0git
212.651 [1313/16/4245] Linking CXX shared library lib/libMLIRShardDialect.so.22.0git
212.659 [1312/16/4246] Creating library symlink lib/libMLIRShardDialect.so
212.754 [1311/16/4247] Linking CXX shared library lib/libMLIRArithUtils.so.22.0git
212.762 [1310/16/4248] Creating library symlink lib/libMLIRArithUtils.so
212.861 [1309/16/4249] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Specialize.cpp.o
212.869 [1308/16/4250] Creating library symlink lib/libMLIRFuncUtils.so
212.920 [1307/16/4251] Linking CXX shared library lib/libMLIRMemRefDialect.so.22.0git
212.977 [1306/16/4252] Linking CXX shared library lib/libMLIRMathToAPFloat.so.22.0git
FAILED: lib/libMLIRMathToAPFloat.so.22.0git 
: && /usr/bin/clang++ -fPIC -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wno-pass-failed -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -Wundef -Werror=mismatched-tags -Werror=global-constructors -O3 -DNDEBUG  -Wl,-z,defs -Wl,-z,nodelete -fuse-ld=lld -Wl,--color-diagnostics   -Wl,--gc-sections -shared -Wl,-soname,libMLIRMathToAPFloat.so.22.0git -o lib/libMLIRMathToAPFloat.so.22.0git tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib:/vol/worker/mlir-nvidia/mlir-nvidia/llvm.obj/lib:"  lib/libArithAndMathToAPFloatUtils.so.22.0git  lib/libMLIRMathDialect.so.22.0git  lib/libMLIRFuncUtils.so.22.0git  lib/libLLVMCore.so.22.0git  lib/libMLIRArithDialect.so.22.0git  lib/libMLIRCastInterfaces.so.22.0git  lib/libMLIRInferIntRangeCommon.so.22.0git  lib/libMLIRShapedOpInterfaces.so.22.0git  lib/libMLIRInferIntRangeInterface.so.22.0git  lib/libMLIRUBDialect.so.22.0git  lib/libMLIRFuncDialect.so.22.0git  lib/libMLIRControlFlowInterfaces.so.22.0git  lib/libMLIRFunctionInterfaces.so.22.0git  lib/libMLIRCallInterfaces.so.22.0git  lib/libMLIRInferTypeOpInterface.so.22.0git  lib/libMLIRSideEffectInterfaces.so.22.0git  lib/libMLIRDialect.so.22.0git  lib/libMLIRDialectUtils.so.22.0git  lib/libMLIRIR.so.22.0git  lib/libMLIRSupport.so.22.0git  lib/libLLVMSupport.so.22.0git  -Wl,-rpath-link,/vol/worker/mlir-nvidia/mlir-nvidia/llvm.obj/lib && :
ld.lld: error: undefined symbol: vtable for mlir::Pass
>>> referenced by MathToAPFloat.cpp
>>>               tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(mlir::Pass::~Pass())
>>> the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)

ld.lld: error: undefined symbol: mlir::FrozenRewritePatternSet::FrozenRewritePatternSet(mlir::RewritePatternSet&&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)
>>> referenced by MathToAPFloat.cpp
>>>               tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:((anonymous namespace)::MathToAPFloatConversionPass::runOnOperation())

ld.lld: error: undefined symbol: mlir::walkAndApplyPatterns(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::RewriterBase::Listener*)
>>> referenced by MathToAPFloat.cpp
>>>               tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:((anonymous namespace)::MathToAPFloatConversionPass::runOnOperation())

ld.lld: error: undefined symbol: mlir::FrozenRewritePatternSet::~FrozenRewritePatternSet()
>>> referenced by MathToAPFloat.cpp
>>>               tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:((anonymous namespace)::MathToAPFloatConversionPass::runOnOperation())

ld.lld: error: undefined symbol: mlir::Pass::initializeOptions(llvm::StringRef, llvm::function_ref<llvm::LogicalResult (llvm::Twine const&)>)
>>> referenced by MathToAPFloat.cpp
>>>               tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(vtable for (anonymous namespace)::MathToAPFloatConversionPass)

ld.lld: error: undefined symbol: mlir::Pass::anchor()
>>> referenced by MathToAPFloat.cpp
>>>               tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(vtable for (anonymous namespace)::MathToAPFloatConversionPass)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
213.065 [1306/15/4253] Linking CXX shared library lib/libMLIRXeVMDialect.so.22.0git
213.364 [1306/14/4254] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Split.cpp.o
213.586 [1306/13/4255] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SwapExtractSliceWithFillPatterns.cpp.o
214.125 [1306/12/4256] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Promotion.cpp.o
214.249 [1306/11/4257] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SplitReduction.cpp.o
217.265 [1306/10/4258] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Tiling.cpp.o
217.869 [1306/9/4259] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/DecomposeGenericByUnfoldingPermutation.cpp.o
219.116 [1306/8/4260] Building CXX object tools/mlir/lib/Dialect/Linalg/TransformOps/CMakeFiles/obj.MLIRLinalgTransformOps.dir/LinalgTransformOps.cpp.o
220.005 [1306/7/4261] Building CXX object tools/mlir/test/lib/Conversion/MemRefToLLVM/CMakeFiles/MLIRTestMemRefToLLVMWithTransforms.dir/TestMemRefToLLVMWithTransforms.cpp.o
222.932 [1306/6/4262] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Transforms.cpp.o
225.666 [1306/5/4263] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Vectorization.cpp.o
226.641 [1306/4/4264] Building CXX object tools/mlir/lib/Dialect/SCF/TransformOps/CMakeFiles/obj.MLIRSCFTransformOps.dir/SCFTransformOps.cpp.o

@makslevental
Copy link
Contributor Author

makslevental commented Dec 17, 2025

should this directory be renamed as well? ./mlir/include/mlir/Conversion/ArithToAPFloat

I had to revert but sure I can do it in the reapply #172716

llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Dec 17, 2025
@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 17, 2025

LLVM Buildbot has detected a new failure on builder amdgpu-offload-rhel-9-cmake-build-only running on rocm-docker-rhel-9 while building mlir at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/205/builds/30917

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: '../llvm-zorg/zorg/buildbot/builders/annotated/amdgpu-offload-cmake.py --jobs=32' (failure)
...
[5247/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorLinearize.cpp.o
[5248/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorInsertExtractStridedSliceRewritePatterns.cpp.o
[5249/8253] Linking CXX shared library lib/libMLIRXeVMDialect.so.22.0git
[5250/8253] Linking CXX shared library lib/libMLIRShardDialect.so.22.0git
[5251/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorEmulateNarrowType.cpp.o
[5252/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorTransferOpTransforms.cpp.o
[5253/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/MorphOps.cpp.o
[5254/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorTransferSplitRewritePatterns.cpp.o
[5255/8253] Creating library symlink lib/libMLIRXeVMDialect.so
[5256/8253] Linking CXX shared library lib/libMLIRMathToAPFloat.so.22.0git
FAILED: lib/libMLIRMathToAPFloat.so.22.0git 
: && /usr/bin/c++ -fPIC -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-array-bounds -Wno-stringop-overread -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -Wundef -Wno-unused-but-set-parameter -Wno-deprecated-copy -O3 -DNDEBUG  -Wl,-z,defs -Wl,-z,nodelete   -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-9-cmake-build-only/build/./lib  -Wl,--gc-sections -shared -Wl,-soname,libMLIRMathToAPFloat.so.22.0git -o lib/libMLIRMathToAPFloat.so.22.0git tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib:/home/botworker/bbot/amdgpu-offload-rhel-9-cmake-build-only/build/lib:"  lib/libArithAndMathToAPFloatUtils.so.22.0git  lib/libMLIRMathDialect.so.22.0git  lib/libMLIRFuncUtils.so.22.0git  lib/libLLVMCore.so.22.0git  lib/libMLIRArithDialect.so.22.0git  lib/libMLIRCastInterfaces.so.22.0git  lib/libMLIRInferIntRangeCommon.so.22.0git  lib/libMLIRShapedOpInterfaces.so.22.0git  lib/libMLIRInferIntRangeInterface.so.22.0git  lib/libMLIRUBDialect.so.22.0git  lib/libMLIRFuncDialect.so.22.0git  lib/libMLIRControlFlowInterfaces.so.22.0git  lib/libMLIRFunctionInterfaces.so.22.0git  lib/libMLIRCallInterfaces.so.22.0git  lib/libMLIRInferTypeOpInterface.so.22.0git  lib/libMLIRSideEffectInterfaces.so.22.0git  lib/libMLIRDialect.so.22.0git  lib/libMLIRDialectUtils.so.22.0git  lib/libMLIRIR.so.22.0git  lib/libMLIRSupport.so.22.0git  lib/libLLVMSupport.so.22.0git  -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-9-cmake-build-only/build/lib && :
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD2Ev+0x3): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD0Ev+0x3): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::runOnOperation()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd3e): undefined reference to `mlir::FrozenRewritePatternSet::FrozenRewritePatternSet(mlir::RewritePatternSet&&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd78): undefined reference to `mlir::walkAndApplyPatterns(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::RewriterBase::Listener*)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd80): undefined reference to `mlir::FrozenRewritePatternSet::~FrozenRewritePatternSet()'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x40): undefined reference to `mlir::Pass::initializeOptions(llvm::StringRef, llvm::function_ref<llvm::LogicalResult (llvm::Twine const&)>)'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x70): undefined reference to `mlir::Pass::anchor()'
collect2: error: ld returned 1 exit status
[5257/8253] Creating library symlink lib/libMLIRShardDialect.so
[5258/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorUnroll.cpp.o
[5259/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSADialect.cpp.o
[5260/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorMaskElimination.cpp.o
[5261/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorTransforms.cpp.o
[5262/8253] Linking CXX shared library lib/libMLIRSparseTensorDialect.so.22.0git
[5263/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSAOps.cpp.o
[5264/8253] Building CXX object tools/mlir/test/lib/Dialect/GPU/CMakeFiles/MLIRGPUTestPasses.dir/TestGpuRewrite.cpp.o
[5265/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Loops.cpp.o
[5266/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PackAndUnpackPatterns.cpp.o
[5267/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Split.cpp.o
[5268/8253] Building CXX object tools/mlir/lib/CMakeFiles/obj.MLIRRegisterAllPasses.dir/RegisterAllPasses.cpp.o
[5269/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/NamedToElementwise.cpp.o
[5270/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Padding.cpp.o
[5271/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PadTilingInterface.cpp.o
[5272/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/BlockPackMatmul.cpp.o
[5273/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SplitReduction.cpp.o
[5274/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SwapExtractSliceWithFillPatterns.cpp.o
[5275/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/DecomposeGenericByUnfoldingPermutation.cpp.o
[5276/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Specialize.cpp.o
[5277/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Tiling.cpp.o
[5278/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Promotion.cpp.o
[5279/8253] Building CXX object tools/mlir/lib/Conversion/GPUToNVVM/CMakeFiles/obj.MLIRGPUToNVVMTransforms.dir/LowerGpuOpsToNVVMOps.cpp.o
[5280/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Transforms.cpp.o
[5281/8253] Building CXX object tools/mlir/test/lib/Dialect/Linalg/CMakeFiles/MLIRLinalgTestPasses.dir/TestLinalgTransforms.cpp.o
[5282/8253] Building CXX object tools/mlir/lib/Dialect/GPU/TransformOps/CMakeFiles/obj.MLIRGPUTransformOps.dir/GPUTransformOps.cpp.o
Step 7 (build cmake config) failure: build cmake config (failure)
...
[5247/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorLinearize.cpp.o
[5248/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorInsertExtractStridedSliceRewritePatterns.cpp.o
[5249/8253] Linking CXX shared library lib/libMLIRXeVMDialect.so.22.0git
[5250/8253] Linking CXX shared library lib/libMLIRShardDialect.so.22.0git
[5251/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorEmulateNarrowType.cpp.o
[5252/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorTransferOpTransforms.cpp.o
[5253/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/MorphOps.cpp.o
[5254/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorTransferSplitRewritePatterns.cpp.o
[5255/8253] Creating library symlink lib/libMLIRXeVMDialect.so
[5256/8253] Linking CXX shared library lib/libMLIRMathToAPFloat.so.22.0git
FAILED: lib/libMLIRMathToAPFloat.so.22.0git 
: && /usr/bin/c++ -fPIC -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-array-bounds -Wno-stringop-overread -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -Wundef -Wno-unused-but-set-parameter -Wno-deprecated-copy -O3 -DNDEBUG  -Wl,-z,defs -Wl,-z,nodelete   -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-9-cmake-build-only/build/./lib  -Wl,--gc-sections -shared -Wl,-soname,libMLIRMathToAPFloat.so.22.0git -o lib/libMLIRMathToAPFloat.so.22.0git tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib:/home/botworker/bbot/amdgpu-offload-rhel-9-cmake-build-only/build/lib:"  lib/libArithAndMathToAPFloatUtils.so.22.0git  lib/libMLIRMathDialect.so.22.0git  lib/libMLIRFuncUtils.so.22.0git  lib/libLLVMCore.so.22.0git  lib/libMLIRArithDialect.so.22.0git  lib/libMLIRCastInterfaces.so.22.0git  lib/libMLIRInferIntRangeCommon.so.22.0git  lib/libMLIRShapedOpInterfaces.so.22.0git  lib/libMLIRInferIntRangeInterface.so.22.0git  lib/libMLIRUBDialect.so.22.0git  lib/libMLIRFuncDialect.so.22.0git  lib/libMLIRControlFlowInterfaces.so.22.0git  lib/libMLIRFunctionInterfaces.so.22.0git  lib/libMLIRCallInterfaces.so.22.0git  lib/libMLIRInferTypeOpInterface.so.22.0git  lib/libMLIRSideEffectInterfaces.so.22.0git  lib/libMLIRDialect.so.22.0git  lib/libMLIRDialectUtils.so.22.0git  lib/libMLIRIR.so.22.0git  lib/libMLIRSupport.so.22.0git  lib/libLLVMSupport.so.22.0git  -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-9-cmake-build-only/build/lib && :
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD2Ev+0x3): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD0Ev+0x3): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::runOnOperation()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd3e): undefined reference to `mlir::FrozenRewritePatternSet::FrozenRewritePatternSet(mlir::RewritePatternSet&&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd78): undefined reference to `mlir::walkAndApplyPatterns(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::RewriterBase::Listener*)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd80): undefined reference to `mlir::FrozenRewritePatternSet::~FrozenRewritePatternSet()'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x40): undefined reference to `mlir::Pass::initializeOptions(llvm::StringRef, llvm::function_ref<llvm::LogicalResult (llvm::Twine const&)>)'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x70): undefined reference to `mlir::Pass::anchor()'
collect2: error: ld returned 1 exit status
[5257/8253] Creating library symlink lib/libMLIRShardDialect.so
[5258/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorUnroll.cpp.o
[5259/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSADialect.cpp.o
[5260/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorMaskElimination.cpp.o
[5261/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorTransforms.cpp.o
[5262/8253] Linking CXX shared library lib/libMLIRSparseTensorDialect.so.22.0git
[5263/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSAOps.cpp.o
[5264/8253] Building CXX object tools/mlir/test/lib/Dialect/GPU/CMakeFiles/MLIRGPUTestPasses.dir/TestGpuRewrite.cpp.o
[5265/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Loops.cpp.o
[5266/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PackAndUnpackPatterns.cpp.o
[5267/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Split.cpp.o
[5268/8253] Building CXX object tools/mlir/lib/CMakeFiles/obj.MLIRRegisterAllPasses.dir/RegisterAllPasses.cpp.o
[5269/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/NamedToElementwise.cpp.o
[5270/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Padding.cpp.o
[5271/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PadTilingInterface.cpp.o
[5272/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/BlockPackMatmul.cpp.o
[5273/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SplitReduction.cpp.o
[5274/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SwapExtractSliceWithFillPatterns.cpp.o
[5275/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/DecomposeGenericByUnfoldingPermutation.cpp.o
[5276/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Specialize.cpp.o
[5277/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Tiling.cpp.o
[5278/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Promotion.cpp.o
[5279/8253] Building CXX object tools/mlir/lib/Conversion/GPUToNVVM/CMakeFiles/obj.MLIRGPUToNVVMTransforms.dir/LowerGpuOpsToNVVMOps.cpp.o
[5280/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Transforms.cpp.o
[5281/8253] Building CXX object tools/mlir/test/lib/Dialect/Linalg/CMakeFiles/MLIRLinalgTestPasses.dir/TestLinalgTransforms.cpp.o
[5282/8253] Building CXX object tools/mlir/lib/Dialect/GPU/TransformOps/CMakeFiles/obj.MLIRGPUTransformOps.dir/GPUTransformOps.cpp.o

@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 17, 2025

LLVM Buildbot has detected a new failure on builder amdgpu-offload-ubuntu-22-cmake-build-only running on rocm-docker-ubu-22 while building mlir at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/203/builds/32128

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: '../llvm-zorg/zorg/buildbot/builders/annotated/amdgpu-offload-cmake.py --jobs=32' (failure)
...
[5215/8253] Creating library symlink lib/libMLIRShardDialect.so
[5216/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorUnroll.cpp.o
[5217/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorMaskElimination.cpp.o
[5218/8253] Linking CXX shared library lib/libMLIRXeVMDialect.so.22.0git
[5219/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/Transforms/CMakeFiles/obj.MLIRX86VectorTransforms.dir/LegalizeForLLVMExport.cpp.o
[5220/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSAOps.cpp.o
[5221/8253] Creating library symlink lib/libMLIRXeVMDialect.so
[5222/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSAInterfaces.cpp.o
[5223/8253] Linking CXX shared library lib/libMLIRSparseTensorDialect.so.22.0git
[5224/8253] Linking CXX shared library lib/libMLIRMathToAPFloat.so.22.0git
FAILED: lib/libMLIRMathToAPFloat.so.22.0git 
: && /usr/bin/c++ -fPIC -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-array-bounds -Wno-stringop-overread -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -Wundef -Wno-unused-but-set-parameter -Wno-deprecated-copy -O3 -DNDEBUG  -Wl,-z,defs -Wl,-z,nodelete   -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-ubuntu-22-cmake-build-only/build/./lib  -Wl,--gc-sections -shared -Wl,-soname,libMLIRMathToAPFloat.so.22.0git -o lib/libMLIRMathToAPFloat.so.22.0git tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib:/home/botworker/bbot/amdgpu-offload-ubuntu-22-cmake-build-only/build/lib:"  lib/libArithAndMathToAPFloatUtils.so.22.0git  lib/libMLIRMathDialect.so.22.0git  lib/libMLIRFuncUtils.so.22.0git  lib/libLLVMCore.so.22.0git  lib/libMLIRArithDialect.so.22.0git  lib/libMLIRCastInterfaces.so.22.0git  lib/libMLIRInferIntRangeCommon.so.22.0git  lib/libMLIRShapedOpInterfaces.so.22.0git  lib/libMLIRInferIntRangeInterface.so.22.0git  lib/libMLIRUBDialect.so.22.0git  lib/libMLIRFuncDialect.so.22.0git  lib/libMLIRControlFlowInterfaces.so.22.0git  lib/libMLIRFunctionInterfaces.so.22.0git  lib/libMLIRCallInterfaces.so.22.0git  lib/libMLIRInferTypeOpInterface.so.22.0git  lib/libMLIRSideEffectInterfaces.so.22.0git  lib/libMLIRDialect.so.22.0git  lib/libMLIRDialectUtils.so.22.0git  lib/libMLIRIR.so.22.0git  lib/libMLIRSupport.so.22.0git  lib/libLLVMSupport.so.22.0git  -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-ubuntu-22-cmake-build-only/build/lib && :
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD2Ev+0x7): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD0Ev+0x7): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::runOnOperation()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd3d): undefined reference to `mlir::FrozenRewritePatternSet::FrozenRewritePatternSet(mlir::RewritePatternSet&&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd77): undefined reference to `mlir::walkAndApplyPatterns(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::RewriterBase::Listener*)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd7f): undefined reference to `mlir::FrozenRewritePatternSet::~FrozenRewritePatternSet()'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x40): undefined reference to `mlir::Pass::initializeOptions(llvm::StringRef, llvm::function_ref<llvm::LogicalResult (llvm::Twine const&)>)'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x70): undefined reference to `mlir::Pass::anchor()'
collect2: error: ld returned 1 exit status
[5225/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/IR/CMakeFiles/obj.MLIRX86VectorDialect.dir/X86VectorDialect.cpp.o
[5226/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSADialect.cpp.o
[5227/8253] Creating library symlink lib/libMLIRSparseTensorDialect.so
[5228/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/Transforms/CMakeFiles/obj.MLIRX86VectorTransforms.dir/VectorContractToPackedTypeDotProduct.cpp.o
[5229/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/Transforms/CMakeFiles/obj.MLIRX86VectorTransforms.dir/VectorContractToFMA.cpp.o
[5230/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SimplifyDepthwiseConv.cpp.o
[5231/8253] Building CXX object tools/mlir/lib/Dialect/GPU/Pipelines/CMakeFiles/obj.MLIRGPUPipelines.dir/GPUToXeVMPipeline.cpp.o
[5232/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Loops.cpp.o
[5233/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PackAndUnpackPatterns.cpp.o
[5234/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/BlockPackMatmul.cpp.o
[5235/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PadTilingInterface.cpp.o
[5236/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/NamedToElementwise.cpp.o
[5237/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Padding.cpp.o
[5238/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Split.cpp.o
[5239/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SwapExtractSliceWithFillPatterns.cpp.o
[5240/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SplitReduction.cpp.o
[5241/8253] Building CXX object tools/mlir/lib/CMakeFiles/obj.MLIRRegisterAllPasses.dir/RegisterAllPasses.cpp.o
[5242/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/DecomposeGenericByUnfoldingPermutation.cpp.o
[5243/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Tiling.cpp.o
[5244/8253] Building CXX object tools/mlir/test/lib/Dialect/ArmSME/CMakeFiles/MLIRArmSMETestPasses.dir/TestLowerToArmSME.cpp.o
[5245/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Specialize.cpp.o
[5246/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Promotion.cpp.o
[5247/8253] Building CXX object tools/mlir/test/lib/Dialect/Linalg/CMakeFiles/MLIRLinalgTestPasses.dir/TestPadFusion.cpp.o
[5248/8253] Building CXX object tools/mlir/lib/Conversion/GPUToNVVM/CMakeFiles/obj.MLIRGPUToNVVMTransforms.dir/LowerGpuOpsToNVVMOps.cpp.o
[5249/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Transforms.cpp.o
[5250/8253] Building CXX object tools/mlir/lib/Dialect/GPU/TransformOps/CMakeFiles/obj.MLIRGPUTransformOps.dir/GPUTransformOps.cpp.o
Step 7 (build cmake config) failure: build cmake config (failure)
...
[5215/8253] Creating library symlink lib/libMLIRShardDialect.so
[5216/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorUnroll.cpp.o
[5217/8253] Building CXX object tools/mlir/lib/Dialect/Vector/Transforms/CMakeFiles/obj.MLIRVectorTransforms.dir/VectorMaskElimination.cpp.o
[5218/8253] Linking CXX shared library lib/libMLIRXeVMDialect.so.22.0git
[5219/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/Transforms/CMakeFiles/obj.MLIRX86VectorTransforms.dir/LegalizeForLLVMExport.cpp.o
[5220/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSAOps.cpp.o
[5221/8253] Creating library symlink lib/libMLIRXeVMDialect.so
[5222/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSAInterfaces.cpp.o
[5223/8253] Linking CXX shared library lib/libMLIRSparseTensorDialect.so.22.0git
[5224/8253] Linking CXX shared library lib/libMLIRMathToAPFloat.so.22.0git
FAILED: lib/libMLIRMathToAPFloat.so.22.0git 
: && /usr/bin/c++ -fPIC -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-array-bounds -Wno-stringop-overread -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -Wundef -Wno-unused-but-set-parameter -Wno-deprecated-copy -O3 -DNDEBUG  -Wl,-z,defs -Wl,-z,nodelete   -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-ubuntu-22-cmake-build-only/build/./lib  -Wl,--gc-sections -shared -Wl,-soname,libMLIRMathToAPFloat.so.22.0git -o lib/libMLIRMathToAPFloat.so.22.0git tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib:/home/botworker/bbot/amdgpu-offload-ubuntu-22-cmake-build-only/build/lib:"  lib/libArithAndMathToAPFloatUtils.so.22.0git  lib/libMLIRMathDialect.so.22.0git  lib/libMLIRFuncUtils.so.22.0git  lib/libLLVMCore.so.22.0git  lib/libMLIRArithDialect.so.22.0git  lib/libMLIRCastInterfaces.so.22.0git  lib/libMLIRInferIntRangeCommon.so.22.0git  lib/libMLIRShapedOpInterfaces.so.22.0git  lib/libMLIRInferIntRangeInterface.so.22.0git  lib/libMLIRUBDialect.so.22.0git  lib/libMLIRFuncDialect.so.22.0git  lib/libMLIRControlFlowInterfaces.so.22.0git  lib/libMLIRFunctionInterfaces.so.22.0git  lib/libMLIRCallInterfaces.so.22.0git  lib/libMLIRInferTypeOpInterface.so.22.0git  lib/libMLIRSideEffectInterfaces.so.22.0git  lib/libMLIRDialect.so.22.0git  lib/libMLIRDialectUtils.so.22.0git  lib/libMLIRIR.so.22.0git  lib/libMLIRSupport.so.22.0git  lib/libLLVMSupport.so.22.0git  -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-ubuntu-22-cmake-build-only/build/lib && :
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD2Ev+0x7): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD0Ev+0x7): undefined reference to `vtable for mlir::Pass'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: in function `(anonymous namespace)::MathToAPFloatConversionPass::runOnOperation()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd3d): undefined reference to `mlir::FrozenRewritePatternSet::FrozenRewritePatternSet(mlir::RewritePatternSet&&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd77): undefined reference to `mlir::walkAndApplyPatterns(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::RewriterBase::Listener*)'
/usr/bin/ld: MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0xd7f): undefined reference to `mlir::FrozenRewritePatternSet::~FrozenRewritePatternSet()'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x40): undefined reference to `mlir::Pass::initializeOptions(llvm::StringRef, llvm::function_ref<llvm::LogicalResult (llvm::Twine const&)>)'
/usr/bin/ld: tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x70): undefined reference to `mlir::Pass::anchor()'
collect2: error: ld returned 1 exit status
[5225/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/IR/CMakeFiles/obj.MLIRX86VectorDialect.dir/X86VectorDialect.cpp.o
[5226/8253] Building CXX object tools/mlir/lib/Dialect/WasmSSA/IR/CMakeFiles/obj.MLIRWasmSSADialect.dir/WasmSSADialect.cpp.o
[5227/8253] Creating library symlink lib/libMLIRSparseTensorDialect.so
[5228/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/Transforms/CMakeFiles/obj.MLIRX86VectorTransforms.dir/VectorContractToPackedTypeDotProduct.cpp.o
[5229/8253] Building CXX object tools/mlir/lib/Dialect/X86Vector/Transforms/CMakeFiles/obj.MLIRX86VectorTransforms.dir/VectorContractToFMA.cpp.o
[5230/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SimplifyDepthwiseConv.cpp.o
[5231/8253] Building CXX object tools/mlir/lib/Dialect/GPU/Pipelines/CMakeFiles/obj.MLIRGPUPipelines.dir/GPUToXeVMPipeline.cpp.o
[5232/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Loops.cpp.o
[5233/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PackAndUnpackPatterns.cpp.o
[5234/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/BlockPackMatmul.cpp.o
[5235/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PadTilingInterface.cpp.o
[5236/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/NamedToElementwise.cpp.o
[5237/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Padding.cpp.o
[5238/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Split.cpp.o
[5239/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SwapExtractSliceWithFillPatterns.cpp.o
[5240/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SplitReduction.cpp.o
[5241/8253] Building CXX object tools/mlir/lib/CMakeFiles/obj.MLIRRegisterAllPasses.dir/RegisterAllPasses.cpp.o
[5242/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/DecomposeGenericByUnfoldingPermutation.cpp.o
[5243/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Tiling.cpp.o
[5244/8253] Building CXX object tools/mlir/test/lib/Dialect/ArmSME/CMakeFiles/MLIRArmSMETestPasses.dir/TestLowerToArmSME.cpp.o
[5245/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Specialize.cpp.o
[5246/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Promotion.cpp.o
[5247/8253] Building CXX object tools/mlir/test/lib/Dialect/Linalg/CMakeFiles/MLIRLinalgTestPasses.dir/TestPadFusion.cpp.o
[5248/8253] Building CXX object tools/mlir/lib/Conversion/GPUToNVVM/CMakeFiles/obj.MLIRGPUToNVVMTransforms.dir/LowerGpuOpsToNVVMOps.cpp.o
[5249/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Transforms.cpp.o
[5250/8253] Building CXX object tools/mlir/lib/Dialect/GPU/TransformOps/CMakeFiles/obj.MLIRGPUTransformOps.dir/GPUTransformOps.cpp.o

@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 17, 2025

LLVM Buildbot has detected a new failure on builder amdgpu-offload-rhel-8-cmake-build-only running on rocm-docker-rhel-8 while building mlir at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/204/builds/30938

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: '../llvm-zorg/zorg/buildbot/builders/annotated/amdgpu-offload-cmake.py --jobs=32' (failure)
...
[5166/8253] Linking CXX shared library lib/libMLIRFuncInlinerExtension.so.22.0git
[5167/8253] Creating library symlink lib/libMLIRWasmSSADialect.so
[5168/8253] Creating library symlink lib/libMLIRTransformDialectUtils.so
[5169/8253] Linking CXX shared library lib/libMLIRArithUtils.so.22.0git
[5170/8253] Linking CXX shared library lib/libMLIRXeVMDialect.so.22.0git
[5171/8253] Creating library symlink lib/libMLIRFuncInlinerExtension.so
[5172/8253] Creating library symlink lib/libMLIRArithUtils.so
[5173/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUBlocking.cpp.o
[5174/8253] Creating library symlink lib/libMLIRXeVMDialect.so
[5175/8253] Linking CXX shared library lib/libMLIRMathToAPFloat.so.22.0git
FAILED: lib/libMLIRMathToAPFloat.so.22.0git 
: && /usr/bin/c++ -fPIC -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-array-bounds -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wno-comment -Wno-misleading-indentation -fdiagnostics-color -ffunction-sections -fdata-sections -Wundef -Wno-unused-but-set-parameter -Wno-deprecated-copy -O3 -DNDEBUG  -Wl,-z,defs -Wl,-z,nodelete   -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/build/./lib  -Wl,--gc-sections -shared -Wl,-soname,libMLIRMathToAPFloat.so.22.0git -o lib/libMLIRMathToAPFloat.so.22.0git tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib:/home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/build/lib:"  lib/libArithAndMathToAPFloatUtils.so.22.0git  lib/libMLIRMathDialect.so.22.0git  lib/libMLIRFuncUtils.so.22.0git  lib/libLLVMCore.so.22.0git  lib/libMLIRArithDialect.so.22.0git  lib/libMLIRCastInterfaces.so.22.0git  lib/libMLIRInferIntRangeCommon.so.22.0git  lib/libMLIRShapedOpInterfaces.so.22.0git  lib/libMLIRInferIntRangeInterface.so.22.0git  lib/libMLIRUBDialect.so.22.0git  lib/libMLIRFuncDialect.so.22.0git  lib/libMLIRControlFlowInterfaces.so.22.0git  lib/libMLIRFunctionInterfaces.so.22.0git  lib/libMLIRCallInterfaces.so.22.0git  lib/libMLIRInferTypeOpInterface.so.22.0git  lib/libMLIRSideEffectInterfaces.so.22.0git  lib/libMLIRDialect.so.22.0git  lib/libMLIRDialectUtils.so.22.0git  lib/libMLIRIR.so.22.0git  lib/libMLIRSupport.so.22.0git  -lpthread  lib/libLLVMSupport.so.22.0git  -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/build/lib && :
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: In function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD2Ev+0x3): undefined reference to `vtable for mlir::Pass'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: In function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD0Ev+0x3): undefined reference to `vtable for mlir::Pass'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: In function `(anonymous namespace)::MathToAPFloatConversionPass::runOnOperation()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0x7eb): undefined reference to `mlir::FrozenRewritePatternSet::FrozenRewritePatternSet(mlir::RewritePatternSet&&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0x82d): undefined reference to `mlir::walkAndApplyPatterns(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::RewriterBase::Listener*)'
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0x835): undefined reference to `mlir::FrozenRewritePatternSet::~FrozenRewritePatternSet()'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x40): undefined reference to `mlir::Pass::initializeOptions(llvm::StringRef, llvm::function_ref<llvm::LogicalResult (llvm::Twine const&)>)'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x70): undefined reference to `mlir::Pass::anchor()'
collect2: error: ld returned 1 exit status
[5176/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUUnroll.cpp.o
[5177/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUFoldAliasOps.cpp.o
[5178/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PackAndUnpackPatterns.cpp.o
[5179/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUSubgroupDistribute.cpp.o
[5180/8253] Linking CXX shared library lib/libMLIRShardDialect.so.22.0git
[5181/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SimplifyDepthwiseConv.cpp.o
[5182/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUPropagateLayout.cpp.o
[5183/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUWgToSgDistribute.cpp.o
[5184/8253] Linking CXX shared library lib/libMLIRComplexDivisionConversion.so.22.0git
[5185/8253] Linking CXX shared library lib/libMLIRSparseTensorDialect.so.22.0git
[5186/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/BlockPackMatmul.cpp.o
[5187/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Padding.cpp.o
[5188/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PadTilingInterface.cpp.o
[5189/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/NamedToElementwise.cpp.o
[5190/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/MorphOps.cpp.o
[5191/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Split.cpp.o
[5192/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SplitReduction.cpp.o
[5193/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SwapExtractSliceWithFillPatterns.cpp.o
[5194/8253] Building CXX object tools/mlir/lib/CMakeFiles/obj.MLIRRegisterAllPasses.dir/RegisterAllPasses.cpp.o
[5195/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/DecomposeGenericByUnfoldingPermutation.cpp.o
[5196/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Specialize.cpp.o
[5197/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Tiling.cpp.o
[5198/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Promotion.cpp.o
[5199/8253] Building CXX object tools/mlir/lib/Conversion/GPUToNVVM/CMakeFiles/obj.MLIRGPUToNVVMTransforms.dir/LowerGpuOpsToNVVMOps.cpp.o
[5200/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Transforms.cpp.o
In file included from /home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/llvm-project/llvm/include/llvm/Support/TypeName.h:14,
Step 7 (build cmake config) failure: build cmake config (failure)
...
[5166/8253] Linking CXX shared library lib/libMLIRFuncInlinerExtension.so.22.0git
[5167/8253] Creating library symlink lib/libMLIRWasmSSADialect.so
[5168/8253] Creating library symlink lib/libMLIRTransformDialectUtils.so
[5169/8253] Linking CXX shared library lib/libMLIRArithUtils.so.22.0git
[5170/8253] Linking CXX shared library lib/libMLIRXeVMDialect.so.22.0git
[5171/8253] Creating library symlink lib/libMLIRFuncInlinerExtension.so
[5172/8253] Creating library symlink lib/libMLIRArithUtils.so
[5173/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUBlocking.cpp.o
[5174/8253] Creating library symlink lib/libMLIRXeVMDialect.so
[5175/8253] Linking CXX shared library lib/libMLIRMathToAPFloat.so.22.0git
FAILED: lib/libMLIRMathToAPFloat.so.22.0git 
: && /usr/bin/c++ -fPIC -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-array-bounds -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wno-comment -Wno-misleading-indentation -fdiagnostics-color -ffunction-sections -fdata-sections -Wundef -Wno-unused-but-set-parameter -Wno-deprecated-copy -O3 -DNDEBUG  -Wl,-z,defs -Wl,-z,nodelete   -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/build/./lib  -Wl,--gc-sections -shared -Wl,-soname,libMLIRMathToAPFloat.so.22.0git -o lib/libMLIRMathToAPFloat.so.22.0git tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib:/home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/build/lib:"  lib/libArithAndMathToAPFloatUtils.so.22.0git  lib/libMLIRMathDialect.so.22.0git  lib/libMLIRFuncUtils.so.22.0git  lib/libLLVMCore.so.22.0git  lib/libMLIRArithDialect.so.22.0git  lib/libMLIRCastInterfaces.so.22.0git  lib/libMLIRInferIntRangeCommon.so.22.0git  lib/libMLIRShapedOpInterfaces.so.22.0git  lib/libMLIRInferIntRangeInterface.so.22.0git  lib/libMLIRUBDialect.so.22.0git  lib/libMLIRFuncDialect.so.22.0git  lib/libMLIRControlFlowInterfaces.so.22.0git  lib/libMLIRFunctionInterfaces.so.22.0git  lib/libMLIRCallInterfaces.so.22.0git  lib/libMLIRInferTypeOpInterface.so.22.0git  lib/libMLIRSideEffectInterfaces.so.22.0git  lib/libMLIRDialect.so.22.0git  lib/libMLIRDialectUtils.so.22.0git  lib/libMLIRIR.so.22.0git  lib/libMLIRSupport.so.22.0git  -lpthread  lib/libLLVMSupport.so.22.0git  -Wl,-rpath-link,/home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/build/lib && :
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: In function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD2Ev+0x3): undefined reference to `vtable for mlir::Pass'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: In function `(anonymous namespace)::MathToAPFloatConversionPass::~MathToAPFloatConversionPass()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPassD0Ev+0x3): undefined reference to `vtable for mlir::Pass'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o: In function `(anonymous namespace)::MathToAPFloatConversionPass::runOnOperation()':
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0x7eb): undefined reference to `mlir::FrozenRewritePatternSet::FrozenRewritePatternSet(mlir::RewritePatternSet&&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0x82d): undefined reference to `mlir::walkAndApplyPatterns(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::RewriterBase::Listener*)'
MathToAPFloat.cpp:(.text._ZN12_GLOBAL__N_127MathToAPFloatConversionPass14runOnOperationEv+0x835): undefined reference to `mlir::FrozenRewritePatternSet::~FrozenRewritePatternSet()'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x40): undefined reference to `mlir::Pass::initializeOptions(llvm::StringRef, llvm::function_ref<llvm::LogicalResult (llvm::Twine const&)>)'
tools/mlir/lib/Conversion/ArithAndMathToAPFloat/CMakeFiles/obj.MLIRMathToAPFloat.dir/MathToAPFloat.cpp.o:(.data.rel.ro._ZTVN12_GLOBAL__N_127MathToAPFloatConversionPassE+0x70): undefined reference to `mlir::Pass::anchor()'
collect2: error: ld returned 1 exit status
[5176/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUUnroll.cpp.o
[5177/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUFoldAliasOps.cpp.o
[5178/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PackAndUnpackPatterns.cpp.o
[5179/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUSubgroupDistribute.cpp.o
[5180/8253] Linking CXX shared library lib/libMLIRShardDialect.so.22.0git
[5181/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SimplifyDepthwiseConv.cpp.o
[5182/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUPropagateLayout.cpp.o
[5183/8253] Building CXX object tools/mlir/lib/Dialect/XeGPU/Transforms/CMakeFiles/obj.MLIRXeGPUTransforms.dir/XeGPUWgToSgDistribute.cpp.o
[5184/8253] Linking CXX shared library lib/libMLIRComplexDivisionConversion.so.22.0git
[5185/8253] Linking CXX shared library lib/libMLIRSparseTensorDialect.so.22.0git
[5186/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/BlockPackMatmul.cpp.o
[5187/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Padding.cpp.o
[5188/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/PadTilingInterface.cpp.o
[5189/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/NamedToElementwise.cpp.o
[5190/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/MorphOps.cpp.o
[5191/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Split.cpp.o
[5192/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SplitReduction.cpp.o
[5193/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/SwapExtractSliceWithFillPatterns.cpp.o
[5194/8253] Building CXX object tools/mlir/lib/CMakeFiles/obj.MLIRRegisterAllPasses.dir/RegisterAllPasses.cpp.o
[5195/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/DecomposeGenericByUnfoldingPermutation.cpp.o
[5196/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Specialize.cpp.o
[5197/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Tiling.cpp.o
[5198/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Promotion.cpp.o
[5199/8253] Building CXX object tools/mlir/lib/Conversion/GPUToNVVM/CMakeFiles/obj.MLIRGPUToNVVMTransforms.dir/LowerGpuOpsToNVVMOps.cpp.o
[5200/8253] Building CXX object tools/mlir/lib/Dialect/Linalg/Transforms/CMakeFiles/obj.MLIRLinalgTransforms.dir/Transforms.cpp.o
In file included from /home/botworker/bbot/amdgpu-offload-rhel-8-cmake-build-only/llvm-project/llvm/include/llvm/Support/TypeName.h:14,

makslevental added a commit that referenced this pull request Dec 18, 2025
…ath-to-apfloat" (#172714) (#172716)

Reapply #171221 - Fix builder
by linking `MLIRTransformUtils`. Also move headers to
`mlir/Conversion/ArithAndMathToAPFloat`.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Dec 18, 2025
…ing pass: math-to-apfloat" (#172714) (#172716)

Reapply llvm/llvm-project#171221 - Fix builder
by linking `MLIRTransformUtils`. Also move headers to
`mlir/Conversion/ArithAndMathToAPFloat`.
bjacob added a commit to iree-org/iree that referenced this pull request Dec 18, 2025
Integrate
llvm/llvm-project@5f15fee

Local revert of llvm/llvm-project#169614 due to
suspiscion of link to numerical issues observed in
#22649.
* Also had to revert
iree-org/llvm-project@dea9ec8
which had landed on top.

Cherry pick of Bazel fix:
llvm/llvm-project@a341180
* Could not just advance to that as this crosses another
Bazel-regressing commit,
llvm/llvm-project#171221

Signed-off-by: Benoit Jacob <jacob.benoit.1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants