Skip to content

Commit

Permalink
[OpenMP][bbc][flang] Add _OPENMP macro definition
Browse files Browse the repository at this point in the history
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
  • Loading branch information
DominikAdamski committed Jun 5, 2023
1 parent 53cc33b commit 4a5ac14
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
52 changes: 52 additions & 0 deletions flang/include/flang/Common/OpenMP-features.h
Original file line number Diff line number Diff line change
@@ -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_
5 changes: 3 additions & 2 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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__",
Expand All @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions flang/test/Driver/bbc-openmp-version-macro.f90
Original file line number Diff line number Diff line change
@@ -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

32 changes: 32 additions & 0 deletions flang/test/Driver/flang-openmp-version-macro.f90
Original file line number Diff line number Diff line change
@@ -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

3 changes: 2 additions & 1 deletion flang/tools/bbc/bbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4a5ac14

Please sign in to comment.