[libc-commits] [libc] [llvm] [libc][WIP] Add guard for bfloat16 type (PR #221327)
via libc-commits
libc-commits at lists.llvm.org
Sat Oct 24 07:21:44 PDT 2026
https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/221327
>From b11d2d3d60eee811c0c23b83aa1a2f87d8ff6058 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 5 Sep 2026 01:07:42 +0530
Subject: [PATCH 1/4] initial commit
---
libc/include/llvm-libc-macros/CMakeLists.txt | 6 ++
.../llvm-libc-macros/bfloat16-macros.h | 19 ++++
.../macros/properties/CMakeLists.txt | 101 +++++++++---------
libc/test/src/__support/FPUtil/CMakeLists.txt | 12 +++
.../FPUtil/builtin_bfloat16_test.cpp | 27 +++++
.../llvm-project-overlay/libc/BUILD.bazel | 5 +
6 files changed, 120 insertions(+), 50 deletions(-)
create mode 100644 libc/include/llvm-libc-macros/bfloat16-macros.h
create mode 100644 libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index ed934df81886fe..097c49f4f327d4 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -48,6 +48,12 @@ add_macro_header(
assert-macros.h
)
+add_macro_header(
+ bfloat16_macros
+ HDR
+ bfloat16-macros.h
+)
+
add_macro_header(
byteswap_macros
HDR
diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
new file mode 100644
index 00000000000000..5d1a9a46fcb1be
--- /dev/null
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -0,0 +1,19 @@
+#ifndef LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
+#define LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
+
+// Clang supports __bf16 when __is_identifier(__bf16) is 0
+// GCC supports __bf16 starting in GCC 13 for x86, ARM, AArch64
+#if defined(__clang__)
+# if !__is_identifier(__bf16)
+# if !defined(__i386__) || defined(__SSE2__)
+# define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+# endif
+# endif
+#elif defined(__GNUC__) && (__GNUC__ >= 13)
+# if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) || \
+ defined(__aarch64__) || defined(__arm__)
+# define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+# endif
+#endif
+
+#endif // LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index dfa2f9c572492a..6ce47b3f724f4c 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -1,50 +1,51 @@
-add_header_library(
- architectures
- HDRS
- architectures.h
-)
-
-add_header_library(
- compiler
- HDRS
- compiler.h
-)
-
-add_header_library(
- os
- HDRS
- os.h
-)
-
-add_header_library(
- cpu_features
- HDRS
- cpu_features.h
- DEPENDS
- .architectures
-)
-
-add_header_library(
- types
- HDRS
- types.h
- DEPENDS
- .architectures
- .compiler
- .cpu_features
- .os
- libc.hdr.float_macros
- libc.hdr.stdint_proxy
- libc.include.llvm-libc-macros.float16_macros
- libc.include.llvm-libc-types.float128
-)
-
-add_header_library(
- complex_types
- HDRS
- complex_types.h
- DEPENDS
- .types
- libc.include.llvm-libc-types.cfloat16
- libc.include.llvm-libc-types.cfloat128
-)
+add_header_library(
+ architectures
+ HDRS
+ architectures.h
+)
+
+add_header_library(
+ compiler
+ HDRS
+ compiler.h
+)
+
+add_header_library(
+ os
+ HDRS
+ os.h
+)
+
+add_header_library(
+ cpu_features
+ HDRS
+ cpu_features.h
+ DEPENDS
+ .architectures
+)
+
+add_header_library(
+ types
+ HDRS
+ types.h
+ DEPENDS
+ .architectures
+ .compiler
+ .cpu_features
+ .os
+ libc.hdr.float_macros
+ libc.hdr.stdint_proxy
+ libc.include.llvm-libc-macros.float16_macros
+ libc.include.llvm-libc-macros.bfloat16_macros
+ libc.include.llvm-libc-types.float128
+)
+
+add_header_library(
+ complex_types
+ HDRS
+ complex_types.h
+ DEPENDS
+ .types
+ libc.include.llvm-libc-types.cfloat16
+ libc.include.llvm-libc-types.cfloat128
+)
diff --git a/libc/test/src/__support/FPUtil/CMakeLists.txt b/libc/test/src/__support/FPUtil/CMakeLists.txt
index 082f13ff9d661e..dc0c014b41a69a 100644
--- a/libc/test/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/test/src/__support/FPUtil/CMakeLists.txt
@@ -1,5 +1,17 @@
add_libc_testsuite(libc-fputil-tests PARENT libc-support-tests)
+add_fp_unittest(
+ builtin_bfloat16_test
+ SUITE
+ libc-fputil-tests
+ SRCS
+ builtin_bfloat16_test.cpp
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.macros.properties.types
+)
+
add_fp_unittest(
dyadic_float_test
NEED_MPFR
diff --git a/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp b/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
new file mode 100644
index 00000000000000..658db59631fed6
--- /dev/null
+++ b/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unit tests for builtin __bf16 type --------------------------------===//
+//
+// 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 "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/properties/types.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+
+using BFloat16 = LIBC_NAMESPACE::fputil::BFloat16;
+
+TEST(LlvmLibcBuiltinBFloat16Test, TypeSizeAndAlignment) {
+ static_assert(sizeof(__bf16) == 2);
+ static_assert(alignof(__bf16) == 2);
+ __bf16 a = 1.0;
+ double b = 1.0;
+ static_assert(__bf16(b) == a);
+}
+
+#endif // LIBC_TYPES_HAS_BUILTIN_BFLOAT16
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 210e2e1eb4a09a..8fcbe72e2bd988 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -544,6 +544,11 @@ libc_support_library(
deps = [":llvm_libc_macros_float_macros"],
)
+libc_support_library(
+ name = "llvm_libc_macros_bfloat16_macros",
+ hdrs = ["include/llvm-libc-macros/bfloat16-macros.h"],
+)
+
libc_support_library(
name = "llvm_libc_types_cfloat128",
hdrs = ["include/llvm-libc-types/cfloat128.h"],
>From 588860f8dd9a57ba973c9343fbf1df30034ec25b Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 5 Sep 2026 01:08:11 +0530
Subject: [PATCH 2/4] clang-format
---
.../include/llvm-libc-macros/bfloat16-macros.h | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
index 5d1a9a46fcb1be..dbb7b9f040cdd1 100644
--- a/libc/include/llvm-libc-macros/bfloat16-macros.h
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -4,16 +4,16 @@
// Clang supports __bf16 when __is_identifier(__bf16) is 0
// GCC supports __bf16 starting in GCC 13 for x86, ARM, AArch64
#if defined(__clang__)
-# if !__is_identifier(__bf16)
-# if !defined(__i386__) || defined(__SSE2__)
-# define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
-# endif
-# endif
+#if !__is_identifier(__bf16)
+#if !defined(__i386__) || defined(__SSE2__)
+#define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#endif
+#endif
#elif defined(__GNUC__) && (__GNUC__ >= 13)
-# if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) || \
- defined(__aarch64__) || defined(__arm__)
-# define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
-# endif
+#if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) || \
+ defined(__aarch64__) || defined(__arm__)
+#define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#endif
#endif
#endif // LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
>From 6dc46b99ab751056e52a09d495269473d0491b6b Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 5 Sep 2026 01:14:19 +0530
Subject: [PATCH 3/4] lf
---
.../macros/properties/CMakeLists.txt | 102 +++++++++---------
1 file changed, 51 insertions(+), 51 deletions(-)
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index 6ce47b3f724f4c..7eeabbbb916b0c 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -1,51 +1,51 @@
-add_header_library(
- architectures
- HDRS
- architectures.h
-)
-
-add_header_library(
- compiler
- HDRS
- compiler.h
-)
-
-add_header_library(
- os
- HDRS
- os.h
-)
-
-add_header_library(
- cpu_features
- HDRS
- cpu_features.h
- DEPENDS
- .architectures
-)
-
-add_header_library(
- types
- HDRS
- types.h
- DEPENDS
- .architectures
- .compiler
- .cpu_features
- .os
- libc.hdr.float_macros
- libc.hdr.stdint_proxy
- libc.include.llvm-libc-macros.float16_macros
- libc.include.llvm-libc-macros.bfloat16_macros
- libc.include.llvm-libc-types.float128
-)
-
-add_header_library(
- complex_types
- HDRS
- complex_types.h
- DEPENDS
- .types
- libc.include.llvm-libc-types.cfloat16
- libc.include.llvm-libc-types.cfloat128
-)
+add_header_library(
+ architectures
+ HDRS
+ architectures.h
+)
+
+add_header_library(
+ compiler
+ HDRS
+ compiler.h
+)
+
+add_header_library(
+ os
+ HDRS
+ os.h
+)
+
+add_header_library(
+ cpu_features
+ HDRS
+ cpu_features.h
+ DEPENDS
+ .architectures
+)
+
+add_header_library(
+ types
+ HDRS
+ types.h
+ DEPENDS
+ .architectures
+ .compiler
+ .cpu_features
+ .os
+ libc.hdr.float_macros
+ libc.hdr.stdint_proxy
+ libc.include.llvm-libc-macros.float16_macros
+ libc.include.llvm-libc-macros.bfloat16_macros
+ libc.include.llvm-libc-types.float128
+)
+
+add_header_library(
+ complex_types
+ HDRS
+ complex_types.h
+ DEPENDS
+ .types
+ libc.include.llvm-libc-types.cfloat16
+ libc.include.llvm-libc-types.cfloat128
+)
>From ade3fb8b39640563a407f805c177292431835a96 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 24 Oct 2026 19:51:20 +0530
Subject: [PATCH 4/4] add check_bfloat16
---
libc/cmake/modules/CheckCompilerFeatures.cmake | 3 +++
libc/cmake/modules/compiler_features/check_bfloat16.cpp | 5 +++++
libc/src/__support/macros/properties/types.h | 1 +
3 files changed, 9 insertions(+)
create mode 100644 libc/cmake/modules/compiler_features/check_bfloat16.cpp
diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index eaf702710b1cce..4473419ea5b812 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -4,6 +4,7 @@
set(
ALL_COMPILER_FEATURES
+ "bfloat16"
"builtin_ceil_floor_rint_trunc"
"builtin_fmax_fmin"
"builtin_fmaxf16_fminf16"
@@ -129,6 +130,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
set(LIBC_TYPES_HAS_CFLOAT16 TRUE)
elseif(${feature} STREQUAL "cfloat128")
set(LIBC_TYPES_HAS_CFLOAT128 TRUE)
+ elseif(${feature} STREQUAL "bfloat16")
+ set(LIBC_TYPES_HAS_BUILTIN_BFLOAT16 TRUE)
elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")
set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)
elseif(${feature} STREQUAL "builtin_fmax_fmin")
diff --git a/libc/cmake/modules/compiler_features/check_bfloat16.cpp b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
new file mode 100644
index 00000000000000..90005a921af930
--- /dev/null
+++ b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
@@ -0,0 +1,5 @@
+#include "include/llvm-libc-macros/bfloat16-macros.h"
+
+#ifndef LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#error unsupported
+#endif
\ No newline at end of file
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index aad4c41005c454..881430f4f92472 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -12,6 +12,7 @@
#include "hdr/float_macros.h" // LDBL_MANT_DIG, LDBL_MAX_EXP
#include "hdr/stdint_proxy.h" // UINT64_MAX, __SIZEOF_INT128__
+#include "include/llvm-libc-macros/bfloat16-macros.h" // LIBC_TYPES_HAS_BUILTIN_BFLOAT16
#include "include/llvm-libc-macros/float16-macros.h" // LIBC_TYPES_HAS_FLOAT16
#include "include/llvm-libc-types/float128.h" // float128
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
More information about the libc-commits
mailing list