[llvm] [flang-rt] Fix TypeCategory for quad-precision COMPLEX (PR #168090)

Carlos Seo via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 19 10:11:08 PST 2025


https://github.com/ceseo updated https://github.com/llvm/llvm-project/pull/168090

>From 1c6794fb0ec5cdacac91d63f3e3a36e9b52447f4 Mon Sep 17 00:00:00 2001
From: Carlos Seo <carlos.seo at linaro.org>
Date: Fri, 14 Nov 2025 17:20:21 +0000
Subject: [PATCH 1/2] [flang-rt] Fix TypeCategory for quad-precision COMPLEX

Modify the TypeCategory for quad-precision COMPLEX to
CFI_type_float128_Complex so it matches the TypeCode returned by
SELECT TYPE lowering.

Fixes #134565
---
 flang-rt/lib/runtime/type-code.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang-rt/lib/runtime/type-code.cpp b/flang-rt/lib/runtime/type-code.cpp
index 9ecde012e7d13..68093decbb34b 100644
--- a/flang-rt/lib/runtime/type-code.cpp
+++ b/flang-rt/lib/runtime/type-code.cpp
@@ -92,7 +92,7 @@ RT_API_ATTRS TypeCode::TypeCode(TypeCategory f, int kind) {
       raw_ = CFI_type_extended_double_Complex;
       break;
     case 16:
-      raw_ = CFI_type_long_double_Complex;
+      raw_ = CFI_type_float128_Complex;
       break;
     }
     break;

>From b52c0be2499f22608a63aaa9508066ed18d129f4 Mon Sep 17 00:00:00 2001
From: Carlos Seo <carlos.seo at linaro.org>
Date: Wed, 19 Nov 2025 18:08:34 +0000
Subject: [PATCH 2/2] [flang-rt] Add testcase for COMPLEX types

---
 flang-rt/unittests/Runtime/CMakeLists.txt |  1 +
 flang-rt/unittests/Runtime/TypeCode.cpp   | 43 +++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 flang-rt/unittests/Runtime/TypeCode.cpp

diff --git a/flang-rt/unittests/Runtime/CMakeLists.txt b/flang-rt/unittests/Runtime/CMakeLists.txt
index e51bc24415773..fca064b226200 100644
--- a/flang-rt/unittests/Runtime/CMakeLists.txt
+++ b/flang-rt/unittests/Runtime/CMakeLists.txt
@@ -40,6 +40,7 @@ add_flangrt_unittest(RuntimeTests
   Time.cpp
   TemporaryStack.cpp
   Transformational.cpp
+  TypeCode.cpp
 
   LINK_LIBS
     flang_rt.runtime.unittest
diff --git a/flang-rt/unittests/Runtime/TypeCode.cpp b/flang-rt/unittests/Runtime/TypeCode.cpp
new file mode 100644
index 0000000000000..5e694055ffc2b
--- /dev/null
+++ b/flang-rt/unittests/Runtime/TypeCode.cpp
@@ -0,0 +1,43 @@
+//===-- unittests/Runtime/TypeCode.cpp --------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "flang-rt/runtime/type-code.h"
+
+using namespace Fortran::runtime;
+using namespace Fortran::common;
+
+TEST(TypeCode, ComplexTypes) {
+  // Test all Complex type kinds to ensure they map correctly
+  struct ComplexTypeMapping {
+    int kind;
+    Fortran::ISO::CFI_type_t expectedType;
+  };
+
+  ComplexTypeMapping mappings[] = {
+      {2, CFI_type_half_float_Complex},
+      {3, CFI_type_bfloat_Complex},
+      {4, CFI_type_float_Complex},
+      {8, CFI_type_double_Complex},
+      {10, CFI_type_extended_double_Complex},
+      {16, CFI_type_float128_Complex},
+  };
+
+  for (const auto &mapping : mappings) {
+    TypeCode tc(TypeCategory::Complex, mapping.kind);
+    EXPECT_EQ(tc.raw(), mapping.expectedType)
+        << "Complex kind " << mapping.kind << " should map to CFI type "
+        << mapping.expectedType;
+    EXPECT_TRUE(tc.IsComplex());
+
+    auto categoryAndKind = tc.GetCategoryAndKind();
+    ASSERT_TRUE(categoryAndKind.has_value());
+    EXPECT_EQ(categoryAndKind->first, TypeCategory::Complex);
+    EXPECT_EQ(categoryAndKind->second, mapping.kind);
+  }
+}



More information about the llvm-commits mailing list