[libc-commits] [libc] [llvm] [SelectionDAG] Emit error instead of asserting for unsupported FP_EXTENDAssertion failed for fpextdouble to x86 fp80 (PR #182660)

via libc-commits libc-commits at lists.llvm.org
Sat Feb 21 01:11:07 PST 2026


https://github.com/bala-bhargav created https://github.com/llvm/llvm-project/pull/182660

Replace the assertion failure in `DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND` with a user-friendly error when no libcall is available for the requested 

Fix
Replace the `assert` with an `emitError` call that reports the unsupported 
conversion gracefully and returns a poison value


PR : #182449

>From d2cb52fccd1c807be1b27001c61599b084b4202e Mon Sep 17 00:00:00 2001
From: bhargav <penugondabalabharghav at gmail.com>
Date: Thu, 12 Feb 2026 07:33:03 +0530
Subject: [PATCH 1/2] [libc][math] Refactor bf16divf implementation to
 header-only in src/__support/math folder.

Move bf16divf implementation from src/math/generic/bf16divf.cpp to a
header-only LIBC_INLINE constexpr function in
src/__support/math/bf16divf.h, following the established pattern for
shared math functions.

Part of #147386.
Fixes #181023.
---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16divf.h                   | 23 +++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 11 ++++++++
 libc/src/__support/math/bf16divf.h            | 28 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +---
 libc/src/math/generic/bf16divf.cpp            |  7 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 17 +++++++++++
 9 files changed, 85 insertions(+), 10 deletions(-)
 create mode 100644 libc/shared/math/bf16divf.h
 create mode 100644 libc/src/__support/math/bf16divf.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 93f7d6a8156cf..b7012583f06dd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -32,6 +32,7 @@
 #include "math/atanhf16.h"
 #include "math/bf16add.h"
 #include "math/bf16addf128.h"
+#include "math/bf16divf.h"
 #include "math/canonicalize.h"
 #include "math/canonicalizebf16.h"
 #include "math/canonicalizef.h"
diff --git a/libc/shared/math/bf16divf.h b/libc/shared/math/bf16divf.h
new file mode 100644
index 0000000000000..c004235d5382f
--- /dev/null
+++ b/libc/shared/math/bf16divf.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16divf function --------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_BF16DIVF_H
+#define LLVM_LIBC_SHARED_MATH_BF16DIVF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16divf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16divf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16DIVF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index aaab78c01a891..4d4d86272b186 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -351,6 +351,17 @@ add_header_library(
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  bf16divf
+  HDRS
+    bf16divf.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.div
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   canonicalize
   HDRS
diff --git a/libc/src/__support/math/bf16divf.h b/libc/src/__support/math/bf16divf.h
new file mode 100644
index 0000000000000..ad5cb57aac828
--- /dev/null
+++ b/libc/src/__support/math/bf16divf.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for bf16divf ----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVF_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr bfloat16 bf16divf(float x, float y) {
+  return fputil::generic::div<bfloat16>(x, y);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 75b51b4587bea..da0de72affc50 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5192,11 +5192,7 @@ add_entrypoint_object(
   HDRS
     ../bf16divf.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.generic.div
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.bf16divf
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16divf.cpp b/libc/src/math/generic/bf16divf.cpp
index 2054a6417b078..767366a42d682 100644
--- a/libc/src/math/generic/bf16divf.cpp
+++ b/libc/src/math/generic/bf16divf.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16divf.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/generic/div.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/bf16divf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16divf, (float x, float y)) {
-  return fputil::generic::div<bfloat16>(x, y);
+  return math::bf16divf(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9dfbedee66a33..a9e11f3a2ba49 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -28,6 +28,7 @@ add_fp_unittest(
     libc.src.__support.math.atanhf16
     libc.src.__support.math.bf16add
     libc.src.__support.math.bf16addf128
+    libc.src.__support.math.bf16divf
     libc.src.__support.math.canonicalize
     libc.src.__support.math.canonicalizebf16
     libc.src.__support.math.canonicalizef
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 2d7ee388f754d..0f9938d7bccbc 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -230,6 +230,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 
 TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
+  EXPECT_FP_EQ(bfloat16(2.0f), LIBC_NAMESPACE::shared::bf16divf(4.0f, 2.0f));
 
   bfloat16 canonicalizebf16_cx = bfloat16(0.0);
   bfloat16 canonicalizebf16_x = bfloat16(0.0);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 608180b90e87b..b51f9bb33268a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2655,6 +2655,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16divf",
+    hdrs = ["src/__support/math/bf16divf.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_canonicalize",
     hdrs = ["src/__support/math/canonicalize.h"],
@@ -4425,6 +4435,13 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "bf16divf",
+    additional_deps = [
+        ":__support_math_bf16divf",
+    ],
+)
+
 libc_math_function(
     name = "canonicalizef",
     additional_deps = [

>From 343b3daeb81c7fea5bc83e4d1cb55291c0413769 Mon Sep 17 00:00:00 2001
From: bhargav <penugondabalabharghav at gmail.com>
Date: Sat, 21 Feb 2026 14:36:27 +0530
Subject: [PATCH 2/2] [SelectionDAG] Emit error instead of asserting for
 unsupported FP_EXTEND (#182449)

---
 llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp |  8 +++++++-
 .../CodeGen/AArch64/unsupported-fpext-x86-fp80.ll    | 12 ++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/AArch64/unsupported-fpext-x86-fp80.ll

diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
index 16453f220bb50..84dccf6581d7c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
@@ -655,7 +655,13 @@ SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
   }
 
   RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
-  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
+  if(LC == RTLIB::UNKNOWN_LIBCALL){
+    DAG.getContext()->emitError("Unsupported FP_EXTEND!");
+    if(IsStrict){
+      ReplaceValueWith(SDValue(N, 1), Chain);      
+    }
+    return DAG.getPOISON(NVT);
+  }
   TargetLowering::MakeLibCallOptions CallOptions;
   EVT OpVT = N->getOperand(IsStrict ? 1 : 0).getValueType();
   CallOptions.setTypeListBeforeSoften(OpVT, N->getValueType(0));
diff --git a/llvm/test/CodeGen/AArch64/unsupported-fpext-x86-fp80.ll b/llvm/test/CodeGen/AArch64/unsupported-fpext-x86-fp80.ll
new file mode 100644
index 0000000000000..412677d942417
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/unsupported-fpext-x86-fp80.ll
@@ -0,0 +1,12 @@
+; RUN: not llc -mtriple=aarch64-unknown-linux-gnu < %s 2>&1 | FileCheck %s
+
+; Verify that we get a user-friendly error instead of an assertion failure
+; when trying to fpext to x86_fp80 on AArch64 (which has no libcall for it).
+; See: https://github.com/llvm/llvm-project/issues/182449
+
+; CHECK: error: Unsupported FP_EXTEND!
+
+define x86_fp80 @test_fpext_double_to_x86_fp80(double %x) {
+  %ext = fpext double %x to x86_fp80
+  ret x86_fp80 %ext
+}



More information about the libc-commits mailing list