[flang-commits] [flang] [flang][runtime] long double isn't always f80 (PR #106746)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Fri Aug 30 08:02:23 PDT 2024


https://github.com/tblah created https://github.com/llvm/llvm-project/pull/106746

f80 is only a thing on x86, and even then the size of long double can be changed with compiler flags. Instead set the size according to the host system (this is what is already done for integer types).

---

I spotted this while reading some code and thought it looked like a bug. Please let me know if I misunderstood.

>From 093a10a76ede94c9cc3f9320f489dda4892fb39e Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Fri, 30 Aug 2024 14:53:43 +0000
Subject: [PATCH] [flang][runtime] long double isn't always f80

f80 is only a thing on x86, and even then the size of long double can be
changed with compiler flags. Instead set the size according to the host
system (this is what is already done for integer types).
---
 .../flang/Optimizer/Builder/Runtime/RTBuilder.h     | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h b/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
index 845ba385918d0d..a103861f1510b8 100644
--- a/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
+++ b/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
@@ -341,7 +341,18 @@ constexpr TypeBuilderFunc getModel<const double *>() {
 template <>
 constexpr TypeBuilderFunc getModel<long double>() {
   return [](mlir::MLIRContext *context) -> mlir::Type {
-    return mlir::FloatType::getF80(context);
+    // See TODO at the top of the file. This is configuring for the host system
+    // - it might be incorrect when cross-compiling!
+    constexpr size_t size = sizeof(long double);
+    static_assert(size == 16 || size == 10 || size == 8,
+                  "unsupported long double size");
+    if constexpr (size == 16)
+      return mlir::FloatType::getF128(context);
+    if constexpr (size == 10)
+      return mlir::FloatType::getF80(context);
+    if constexpr (size == 8)
+      return mlir::FloatType::getF64(context);
+    llvm_unreachable("failed static assert");
   };
 }
 template <>



More information about the flang-commits mailing list