[flang-commits] [flang] 1cfae76 - [Flang] Adjust preprocessing to build modules correctly

Peter Steinfeld via flang-commits flang-commits at lists.llvm.org
Fri Oct 14 12:28:07 PDT 2022


Author: Peter Steinfeld
Date: 2022-10-14T12:27:48-07:00
New Revision: 1cfae76ed91a91aac9444e5a07d31965aae9a628

URL: https://github.com/llvm/llvm-project/commit/1cfae76ed91a91aac9444e5a07d31965aae9a628
DIFF: https://github.com/llvm/llvm-project/commit/1cfae76ed91a91aac9444e5a07d31965aae9a628.diff

LOG: [Flang] Adjust preprocessing to build modules correctly

Several module files in .../llvm-project/flang/module check for the
existence of the macro "__x86_64__" to conditionally compile Fortran
code.  Unfortunately, this macro was not being defined anywhere.  This
patch fixes that for compilations targeting 64 bit x86 machines.

I made the following changes --
  -- Removed the test for 32 bit X86 targets.  The rest of the compiler and
  runtime do not support X86 32 bits.
  -- Added predefined macros to define "__x86_64__"  and "__x86__64" to
  be 1 when the target architecture is 64 bit x86 and the "-cpp" option
  is on the command line.
  -- Changed the cmake file for creating the Fortran module files to use the
  "-cpp" option so that the macro "__x86_64__" will be defined when building
  the module files.
  -- Added a test.

Differential Revision: https://reviews.llvm.org/D135810

Added: 
    flang/test/Driver/predefined-macros-x86.f90

Modified: 
    flang/lib/Frontend/CompilerInvocation.cpp
    flang/tools/f18/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index c1824188104ea..761300b807cc3 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -784,6 +784,11 @@ void CompilerInvocation::setDefaultPredefinitions() {
           Fortran::common::LanguageFeature::OpenMP)) {
     fortranOptions.predefinitions.emplace_back("_OPENMP", "201511");
   }
+  llvm::Triple targetTriple{llvm::Triple(this->targetOpts.triple)};
+  if (targetTriple.getArch() == llvm::Triple::ArchType::x86_64) {
+    fortranOptions.predefinitions.emplace_back("__x86_64__", "1");
+    fortranOptions.predefinitions.emplace_back("__x86_64", "1");
+  }
 }
 
 void CompilerInvocation::setFortranOpts() {
@@ -837,7 +842,7 @@ void CompilerInvocation::setFortranOpts() {
 
 void CompilerInvocation::setSemanticsOpts(
     Fortran::parser::AllCookedSources &allCookedSources) {
-  const auto &fortranOptions = getFortranOpts();
+  auto &fortranOptions = getFortranOpts();
 
   semanticsContext = std::make_unique<semantics::SemanticsContext>(
       getDefaultKinds(), fortranOptions.features, allCookedSources);
@@ -851,8 +856,7 @@ void CompilerInvocation::setSemanticsOpts(
 
   llvm::Triple targetTriple{llvm::Triple(this->targetOpts.triple)};
   // FIXME: Handle real(3) ?
-  if (targetTriple.getArch() != llvm::Triple::ArchType::x86 &&
-      targetTriple.getArch() != llvm::Triple::ArchType::x86_64) {
+  if (targetTriple.getArch() != llvm::Triple::ArchType::x86_64) {
     semanticsContext->targetCharacteristics().DisableType(
         Fortran::common::TypeCategory::Real, /*kind=*/10);
   }

diff  --git a/flang/test/Driver/predefined-macros-x86.f90 b/flang/test/Driver/predefined-macros-x86.f90
new file mode 100644
index 0000000000000..29243fd65109e
--- /dev/null
+++ b/flang/test/Driver/predefined-macros-x86.f90
@@ -0,0 +1,16 @@
+! Test predefined macro for 64 bit X86 architecture
+
+! REQUIRES: x86-registered-target
+
+! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -cpp -E %s | FileCheck %s
+
+! CHECK: integer :: var1 = 1
+! CHECK: integer :: var2 = 1
+
+#if __x86_64__
+  integer :: var1 = __x86_64__
+#endif
+#if __x86_64__
+  integer :: var2 = __x86_64
+#endif
+end program

diff  --git a/flang/tools/f18/CMakeLists.txt b/flang/tools/f18/CMakeLists.txt
index e7a826e0dda5d..b046850557d0f 100644
--- a/flang/tools/f18/CMakeLists.txt
+++ b/flang/tools/f18/CMakeLists.txt
@@ -39,7 +39,7 @@ if (NOT CMAKE_CROSSCOMPILING)
     endif()
     add_custom_command(OUTPUT ${base}.mod
       COMMAND ${CMAKE_COMMAND} -E make_directory ${FLANG_INTRINSIC_MODULES_DIR}
-      COMMAND flang-new -fc1 -fsyntax-only -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
+      COMMAND flang-new -fc1 -cpp -fsyntax-only -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
         ${FLANG_SOURCE_DIR}/module/${filename}.f90
       DEPENDS flang-new ${FLANG_SOURCE_DIR}/module/${filename}.f90 ${depends}
     )


        


More information about the flang-commits mailing list