[flang-commits] [flang] 4a5ac14 - [OpenMP][bbc][flang] Add _OPENMP macro definition

Dominik Adamski via flang-commits flang-commits at lists.llvm.org
Mon Jun 5 03:07:20 PDT 2023


Author: Dominik Adamski
Date: 2023-06-05T05:00:15-05:00
New Revision: 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a

URL: https://github.com/llvm/llvm-project/commit/4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a
DIFF: https://github.com/llvm/llvm-project/commit/4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a.diff

LOG: [OpenMP][bbc][flang] Add _OPENMP macro definition

OpenMP standard (section 3.3 for OpenMP 5.2) requires that _OPENMP macro
contains release date of given OpenMP standard version.

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

Reviewed By: kiranchandramohan

Added: 
    flang/include/flang/Common/OpenMP-features.h
    flang/test/Driver/bbc-openmp-version-macro.f90
    flang/test/Driver/flang-openmp-version-macro.f90

Modified: 
    flang/lib/Frontend/CompilerInvocation.cpp
    flang/tools/bbc/bbc.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Common/OpenMP-features.h b/flang/include/flang/Common/OpenMP-features.h
new file mode 100644
index 0000000000000..480c8b4c1013f
--- /dev/null
+++ b/flang/include/flang/Common/OpenMP-features.h
@@ -0,0 +1,52 @@
+//===-- include/flang/Common/OpenMP-features.h -----------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_COMMON_OPENMP_FEATURES_H_
+#define FORTRAN_COMMON_OPENMP_FEATURES_H_
+
+namespace Fortran::common {
+
+/// Set _OPENMP macro according to given version number
+template <typename FortranPredefinitions>
+void setOpenMPMacro(int version, FortranPredefinitions &predefinitions) {
+  switch (version) {
+  case 20:
+    predefinitions.emplace_back("_OPENMP", "200011");
+    break;
+  case 25:
+    predefinitions.emplace_back("_OPENMP", "200505");
+    break;
+  case 30:
+    predefinitions.emplace_back("_OPENMP", "200805");
+    break;
+  case 31:
+    predefinitions.emplace_back("_OPENMP", "201107");
+    break;
+  case 40:
+    predefinitions.emplace_back("_OPENMP", "201307");
+    break;
+  case 45:
+    predefinitions.emplace_back("_OPENMP", "201511");
+    break;
+  case 50:
+    predefinitions.emplace_back("_OPENMP", "201811");
+    break;
+  case 51:
+    predefinitions.emplace_back("_OPENMP", "202011");
+    break;
+  case 52:
+    predefinitions.emplace_back("_OPENMP", "202111");
+    break;
+  case 11:
+  default:
+    predefinitions.emplace_back("_OPENMP", "199911");
+    break;
+  }
+}
+} // namespace Fortran::common
+#endif // FORTRAN_COMMON_OPENMP_FEATURES_H_

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 02973178d9c28..55fe9890be2aa 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -12,6 +12,7 @@
 
 #include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Common/Fortran-features.h"
+#include "flang/Common/OpenMP-features.h"
 #include "flang/Common/Version.h"
 #include "flang/Frontend/CodeGenOptions.h"
 #include "flang/Frontend/PreprocessorOptions.h"
@@ -1005,7 +1006,6 @@ void CompilerInvocation::setDefaultFortranOpts() {
 void CompilerInvocation::setDefaultPredefinitions() {
   auto &fortranOptions = getFortranOpts();
   const auto &frontendOptions = getFrontendOpts();
-
   // Populate the macro list with version numbers and other predefinitions.
   fortranOptions.predefinitions.emplace_back("__flang__", "1");
   fortranOptions.predefinitions.emplace_back("__flang_major__",
@@ -1022,7 +1022,8 @@ void CompilerInvocation::setDefaultPredefinitions() {
   }
   if (frontendOptions.features.IsEnabled(
           Fortran::common::LanguageFeature::OpenMP)) {
-    fortranOptions.predefinitions.emplace_back("_OPENMP", "201511");
+    Fortran::common::setOpenMPMacro(getLangOpts().OpenMPVersion,
+                                    fortranOptions.predefinitions);
   }
   llvm::Triple targetTriple{llvm::Triple(this->targetOpts.triple)};
   if (targetTriple.getArch() == llvm::Triple::ArchType::x86_64) {

diff  --git a/flang/test/Driver/bbc-openmp-version-macro.f90 b/flang/test/Driver/bbc-openmp-version-macro.f90
new file mode 100644
index 0000000000000..7118021cba03a
--- /dev/null
+++ b/flang/test/Driver/bbc-openmp-version-macro.f90
@@ -0,0 +1,32 @@
+! Test predefined _OPENMP macro which denotes OpenMP version
+
+! RUN: bbc -fopenmp -o -  %s | FileCheck %s --check-prefix=DEFAULT-OPENMP-VERSION
+! RUN: bbc -fopenmp -fopenmp-version=11 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-11
+! RUN: bbc -fopenmp -fopenmp-version=11 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-11
+! RUN: bbc -fopenmp -fopenmp-version=20 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-20
+! RUN: bbc -fopenmp -fopenmp-version=25 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-25
+! RUN: bbc -fopenmp -fopenmp-version=30 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-30
+! RUN: bbc -fopenmp -fopenmp-version=31 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-31
+! RUN: bbc -fopenmp -fopenmp-version=40 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-40
+! RUN: bbc -fopenmp -fopenmp-version=45 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-45
+! RUN: bbc -fopenmp -fopenmp-version=50 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-50
+! RUN: bbc -fopenmp -fopenmp-version=51 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-51
+! RUN: bbc -fopenmp -fopenmp-version=52 -o - %s | FileCheck %s --check-prefix=OPENMP-VERSION-52
+
+! DEFAULT-OPENMP-VERSION: {{.*}} = arith.constant 199911 : i32
+! OPENMP-VERSION-11: {{.*}} = arith.constant 199911 : i32
+! OPENMP-VERSION-20: {{.*}} = arith.constant 200011 : i32
+! OPENMP-VERSION-25: {{.*}} = arith.constant 200505 : i32
+! OPENMP-VERSION-30: {{.*}} = arith.constant 200805 : i32
+! OPENMP-VERSION-31: {{.*}} = arith.constant 201107 : i32
+! OPENMP-VERSION-40: {{.*}} = arith.constant 201307 : i32
+! OPENMP-VERSION-45: {{.*}} = arith.constant 201511 : i32
+! OPENMP-VERSION-50: {{.*}} = arith.constant 201811 : i32
+! OPENMP-VERSION-51: {{.*}} = arith.constant 202011 : i32
+! OPENMP-VERSION-52: {{.*}} = arith.constant 202111 : i32
+
+#if _OPENMP
+  integer :: var1 = _OPENMP
+#endif
+end program
+

diff  --git a/flang/test/Driver/flang-openmp-version-macro.f90 b/flang/test/Driver/flang-openmp-version-macro.f90
new file mode 100644
index 0000000000000..73cb322a06c00
--- /dev/null
+++ b/flang/test/Driver/flang-openmp-version-macro.f90
@@ -0,0 +1,32 @@
+! Test predefined _OPENMP macro which denotes OpenMP version
+
+! RUN: %flang_fc1 -fopenmp -cpp -E %s | FileCheck %s --check-prefix=DEFAULT-OPENMP-VERSION
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=11 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-11
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=11 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-11
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=20 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-20
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=25 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-25
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=30 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-30
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=31 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-31
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=40 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-40
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=45 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-45
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=50 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-50
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=51 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-51
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp -E %s | FileCheck %s --check-prefix=OPENMP-VERSION-52
+
+! DEFAULT-OPENMP-VERSION: integer :: var1 = 199911
+! OPENMP-VERSION-11: integer :: var1 = 199911
+! OPENMP-VERSION-20: integer :: var1 = 200011
+! OPENMP-VERSION-25: integer :: var1 = 200505
+! OPENMP-VERSION-30: integer :: var1 = 200805
+! OPENMP-VERSION-31: integer :: var1 = 201107
+! OPENMP-VERSION-40: integer :: var1 = 201307
+! OPENMP-VERSION-45: integer :: var1 = 201511
+! OPENMP-VERSION-50: integer :: var1 = 201811
+! OPENMP-VERSION-51: integer :: var1 = 202011
+! OPENMP-VERSION-52: integer :: var1 = 202111
+
+#if _OPENMP
+  integer :: var1 = _OPENMP
+#endif
+end program
+

diff  --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index 43abea04b8c9c..f00b2189242f0 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Common/Fortran-features.h"
+#include "flang/Common/OpenMP-features.h"
 #include "flang/Common/default-kinds.h"
 #include "flang/Lower/Bridge.h"
 #include "flang/Lower/PFTBuilder.h"
@@ -391,7 +392,7 @@ int main(int argc, char **argv) {
   // enable parsing of OpenMP
   if (enableOpenMP) {
     options.features.Enable(Fortran::common::LanguageFeature::OpenMP);
-    options.predefinitions.emplace_back("_OPENMP", "201511");
+    Fortran::common::setOpenMPMacro(setOpenMPVersion, options.predefinitions);
   }
 
   // enable parsing of OpenACC


        


More information about the flang-commits mailing list