From cfbea652cf7e1d61cd7c64b3171aee8ca7faf0f3 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Tue, 24 Sep 2024 15:14:39 -0500 Subject: [PATCH] [flang][Semantics] Add LangOptions to SemanticsContext The motivation for this is to make OpenMP settings visible in the semantic checks (OpenMP version in particular). --- flang/include/flang/Semantics/semantics.h | 10 ++++++++-- flang/lib/Frontend/CompilerInvocation.cpp | 3 ++- flang/lib/Semantics/semantics.cpp | 3 ++- flang/tools/bbc/bbc.cpp | 18 +++++++++++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/flang/include/flang/Semantics/semantics.h b/flang/include/flang/Semantics/semantics.h index e73f9d2e85d589..4ea9f9e081b701 100644 --- a/flang/include/flang/Semantics/semantics.h +++ b/flang/include/flang/Semantics/semantics.h @@ -12,6 +12,7 @@ #include "scope.h" #include "symbol.h" #include "flang/Common/Fortran-features.h" +#include "flang/Common/LangOptions.h" #include "flang/Evaluate/common.h" #include "flang/Evaluate/intrinsics.h" #include "flang/Evaluate/target.h" @@ -65,7 +66,8 @@ using ConstructStack = std::vector; class SemanticsContext { public: SemanticsContext(const common::IntrinsicTypeDefaultKinds &, - const common::LanguageFeatureControl &, parser::AllCookedSources &); + const common::LanguageFeatureControl &, const common::LangOptions &, + parser::AllCookedSources &); ~SemanticsContext(); const common::IntrinsicTypeDefaultKinds &defaultKinds() const { @@ -73,7 +75,10 @@ class SemanticsContext { } const common::LanguageFeatureControl &languageFeatures() const { return languageFeatures_; - }; + } + const common::LangOptions &langOptions() const { + return langOpts_; + } int GetDefaultKind(TypeCategory) const; int doublePrecisionKind() const { return defaultKinds_.doublePrecisionKind(); @@ -273,6 +278,7 @@ class SemanticsContext { const common::IntrinsicTypeDefaultKinds &defaultKinds_; const common::LanguageFeatureControl &languageFeatures_; + const common::LangOptions &langOpts_; parser::AllCookedSources &allCookedSources_; std::optional location_; std::vector searchDirectories_; diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index d40f0c03716925..3c3e9e5659a8ee 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -1559,7 +1559,8 @@ CompilerInvocation::getSemanticsCtx( auto &fortranOptions = getFortranOpts(); auto semanticsContext = std::make_unique( - getDefaultKinds(), fortranOptions.features, allCookedSources); + getDefaultKinds(), fortranOptions.features, getLangOpts(), + allCookedSources); semanticsContext->set_moduleDirectory(getModuleDir()) .set_searchDirectories(fortranOptions.searchDirectories) diff --git a/flang/lib/Semantics/semantics.cpp b/flang/lib/Semantics/semantics.cpp index 8592d1e5d6217e..1f2980b07b3e0e 100644 --- a/flang/lib/Semantics/semantics.cpp +++ b/flang/lib/Semantics/semantics.cpp @@ -348,9 +348,10 @@ class CommonBlockMap { SemanticsContext::SemanticsContext( const common::IntrinsicTypeDefaultKinds &defaultKinds, const common::LanguageFeatureControl &languageFeatures, + const common::LangOptions &langOpts, parser::AllCookedSources &allCookedSources) : defaultKinds_{defaultKinds}, languageFeatures_{languageFeatures}, - allCookedSources_{allCookedSources}, + langOpts_{langOpts}, allCookedSources_{allCookedSources}, intrinsics_{evaluate::IntrinsicProcTable::Configure(defaultKinds_)}, globalScope_{*this}, intrinsicModulesScope_{globalScope_.MakeScope( Scope::Kind::IntrinsicModules, nullptr)}, diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp index f8bd396cd99877..0979dd3dc1ec14 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/LangOptions.h" #include "flang/Common/OpenMP-features.h" #include "flang/Common/Version.h" #include "flang/Common/default-kinds.h" @@ -524,6 +525,21 @@ int main(int argc, char **argv) { options.predefinitions.emplace_back( "__flang_patchlevel__"s, std::string{FLANG_VERSION_PATCHLEVEL_STRING}); + Fortran::common::LangOptions langOpts; + langOpts.NoGPULib = setNoGPULib; + langOpts.OpenMPVersion = setOpenMPVersion; + langOpts.OpenMPIsTargetDevice = enableOpenMPDevice; + langOpts.OpenMPIsGPU = enableOpenMPGPU; + langOpts.OpenMPForceUSM = enableOpenMPForceUSM; + langOpts.OpenMPTargetDebug = setOpenMPTargetDebug; + langOpts.OpenMPThreadSubscription = setOpenMPThreadSubscription; + langOpts.OpenMPTeamSubscription = setOpenMPTeamSubscription; + langOpts.OpenMPNoThreadState = setOpenMPNoThreadState; + langOpts.OpenMPNoNestedParallelism = setOpenMPNoNestedParallelism; + std::transform(targetTriplesOpenMP.begin(), targetTriplesOpenMP.end(), + std::back_inserter(langOpts.OMPTargetTriples), + [](const std::string &str) { return llvm::Triple(str); }); + // enable parsing of OpenMP if (enableOpenMP) { options.features.Enable(Fortran::common::LanguageFeature::OpenMP); @@ -555,7 +571,7 @@ int main(int argc, char **argv) { Fortran::parser::AllSources allSources; Fortran::parser::AllCookedSources allCookedSources(allSources); Fortran::semantics::SemanticsContext semanticsContext{ - defaultKinds, options.features, allCookedSources}; + defaultKinds, options.features, langOpts, allCookedSources}; semanticsContext.set_moduleDirectory(moduleDir) .set_moduleFileSuffix(moduleSuffix) .set_searchDirectories(includeDirs)