[flang-commits] [flang] [Flang] Add __powerpc__ macro to set c_intmax_t to c_int64_t rather than c_int128_t as PowerPC only supports up to c_int64_t. (PR #81222)

Daniel Chen via flang-commits flang-commits at lists.llvm.org
Tue Feb 13 06:37:46 PST 2024


https://github.com/DanielCChen updated https://github.com/llvm/llvm-project/pull/81222

>From fdc56e6b4f2c5b65d2c7b805221113a932c631f9 Mon Sep 17 00:00:00 2001
From: cdchen-ca <cdchen at ca.ibm.com>
Date: Thu, 8 Feb 2024 22:37:31 -0500
Subject: [PATCH 1/2] [Flang] Add __powerpc__ macro to set c_intmax_t to
 c_int64_t rather than c_int128_t as PowerPC only supports up to c_int64_t.

---
 flang/lib/Frontend/CompilerInvocation.cpp | 13 ++++++++++++-
 flang/module/iso_c_binding.f90            |  4 ++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index ffde7f50087e52..d49e8e283e0d40 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1326,10 +1326,21 @@ void CompilerInvocation::setDefaultPredefinitions() {
     Fortran::common::setOpenMPMacro(getLangOpts().OpenMPVersion,
                                     fortranOptions.predefinitions);
   }
+
   llvm::Triple targetTriple{llvm::Triple(this->targetOpts.triple)};
-  if (targetTriple.getArch() == llvm::Triple::ArchType::x86_64) {
+  switch (targetTriple.getArch()) {
+  default:
+    break;
+  case llvm::Triple::ArchType::x86_64:
     fortranOptions.predefinitions.emplace_back("__x86_64__", "1");
     fortranOptions.predefinitions.emplace_back("__x86_64", "1");
+    break;
+  case llvm::Triple::ArchType::ppc:
+  case llvm::Triple::ArchType::ppcle:
+  case llvm::Triple::ArchType::ppc64:
+  case llvm::Triple::ArchType::ppc64le:
+    fortranOptions.predefinitions.emplace_back("__powerpc__", "1");
+    break;
   }
 }
 
diff --git a/flang/module/iso_c_binding.f90 b/flang/module/iso_c_binding.f90
index 9a7e68f3314463..1661fd5a6dcf6a 100644
--- a/flang/module/iso_c_binding.f90
+++ b/flang/module/iso_c_binding.f90
@@ -47,7 +47,11 @@ module iso_c_binding
     c_long_long = c_int64_t, &
     c_signed_char = c_int8_t, &
     c_size_t = kind(c_sizeof(1)), &
+#if __powerpc__
+    c_intmax_t = c_int64_t, &
+#else
     c_intmax_t = c_int128_t, &
+#endif
     c_intptr_t = c_size_t, &
     c_ptrdiff_t = c_size_t
   integer, parameter, public :: &

>From 74c95049852c1306198621f68cb7bbf590b7cab3 Mon Sep 17 00:00:00 2001
From: cdchen-ca <cdchen at ca.ibm.com>
Date: Mon, 12 Feb 2024 17:47:27 -0500
Subject: [PATCH 2/2] [Flang] Address review comment to add a LIT test. Also
 added comments to indicate that this macro is generic for PowerPC.

---
 flang/lib/Frontend/CompilerInvocation.cpp       |  2 ++
 flang/test/Driver/predefined-macros-powerpc.f90 | 11 +++++++++++
 2 files changed, 13 insertions(+)
 create mode 100644 flang/test/Driver/predefined-macros-powerpc.f90

diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index d49e8e283e0d40..4707de0e976ca7 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1339,6 +1339,8 @@ void CompilerInvocation::setDefaultPredefinitions() {
   case llvm::Triple::ArchType::ppcle:
   case llvm::Triple::ArchType::ppc64:
   case llvm::Triple::ArchType::ppc64le:
+    // '__powerpc__' is a generic macro for any PowerPC cases. e.g. Max integer
+    // size.
     fortranOptions.predefinitions.emplace_back("__powerpc__", "1");
     break;
   }
diff --git a/flang/test/Driver/predefined-macros-powerpc.f90 b/flang/test/Driver/predefined-macros-powerpc.f90
new file mode 100644
index 00000000000000..b3d2b617fb1fad
--- /dev/null
+++ b/flang/test/Driver/predefined-macros-powerpc.f90
@@ -0,0 +1,11 @@
+! Test predefined macro for PowerPC architecture
+
+! RUN: %flang_fc1 -cpp -E %s | FileCheck %s
+! REQUIRES: target=powerpc{{.*}}
+
+! CHECK: integer :: var1 = 1
+
+#if __powerpc__
+  integer :: var1 = __powerpc__
+#endif
+end program



More information about the flang-commits mailing list