[clang] [flang] [flang][OpenMP] Globalize MAIN scalars used in parallel regions (PR #236384)

Sergey Shcherbinin via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 02:04:23 PST 2026


https://github.com/SergeyShch01 updated https://github.com/llvm/llvm-project/pull/236384

>From b93d72d4db64584b1b0492f147df6e2163c8e4e3 Mon Sep 17 00:00:00 2001
From: Sergey Shcherbinin <sscherbinin at nvidia.com>
Date: Wed, 28 Oct 2026 15:36:14 +0400
Subject: [PATCH 1/4] [flang][OpenMP] Globalize MAIN scalars used in parallel
 regions

Place eligible fixed-size intrinsic MAIN scalars of up to 32 bytes with
implicit SAVE semantics in static storage when directly used in explicit
host OpenMP PARALLEL regions. This covers direct references and simple
expressions in standalone, combined and nested constructs, including
regions in MAIN's internal procedures. These scalars would otherwise use
stack storage.

Selection is limited to parallel regions rather than all procedure uses.
Ordinary procedures may inline, exposing captured scalars to register
promotion and making the benefit of globalization less clear. OpenMP
parallel regions are outlined and invoked through the runtime. Direct
global references preserve object identity and storage properties across
this boundary, providing more precise information for alias analysis and
enabling optimizations such as LICM.

To limit compilation overhead:

- Memoize USE/host-association resolution during selection and binding
  preparation.
- Reuse cached scope-variable lists for promoted MAIN hosts and their
  own lowering.
- Preselect ordered global bindings and required aggregate backing
  stores once per host, then reuse them across internal procedures.

An internal frontend option allows disabling the new selection.

The miniWeather mini-app (Fortran/OpenMP) shows a 28% performance
improvement on NVIDIA Grace with 64 OpenMP threads.
---
 clang/include/clang/Options/FlangOptions.td   |   5 +
 flang/docs/ReleaseNotes.md                    |   3 +
 flang/include/flang/Evaluate/tools.h          |   3 +
 flang/include/flang/Lower/HostAssociations.h  |  22 +-
 flang/include/flang/Lower/LoweringOptions.def |   4 +
 flang/include/flang/Lower/PFTBuilder.h        |  13 +-
 flang/include/flang/Semantics/symbol.h        |   5 +-
 flang/lib/Evaluate/tools.cpp                  |   7 +-
 flang/lib/Frontend/CompilerInvocation.cpp     |  12 +
 flang/lib/Frontend/ParserActions.cpp          |   4 +-
 flang/lib/Lower/Bridge.cpp                    |  13 +-
 flang/lib/Lower/HostAssociations.cpp          |  89 ++++-
 flang/lib/Lower/PFTBuilder.cpp                | 348 ++++++++++++++++-
 .../Driver/openmp-main-program-globals.f90    |  11 +
 .../Lower/OpenMP/default-clause-byref.f90     |   8 +-
 flang/test/Lower/OpenMP/default-clause.f90    |   8 +-
 .../OpenMP/main-program-globals-cost.f90      | 126 ++++++
 .../OpenMP/main-program-globals-fallback.f90  | 115 ++++++
 .../Lower/OpenMP/main-program-globals.f90     | 367 ++++++++++++++++++
 flang/test/Lower/volatile-openmp1.f90         |   6 +-
 flang/tools/bbc/bbc.cpp                       |  16 +-
 .../Frontend/CompilerInstanceTest.cpp         |  32 ++
 22 files changed, 1174 insertions(+), 43 deletions(-)
 create mode 100644 flang/test/Driver/openmp-main-program-globals.f90
 create mode 100644 flang/test/Lower/OpenMP/main-program-globals-cost.f90
 create mode 100644 flang/test/Lower/OpenMP/main-program-globals-fallback.f90
 create mode 100644 flang/test/Lower/OpenMP/main-program-globals.f90

diff --git a/clang/include/clang/Options/FlangOptions.td b/clang/include/clang/Options/FlangOptions.td
index b07a6bdd1e606a..d672380260a3f8 100644
--- a/clang/include/clang/Options/FlangOptions.td
+++ b/clang/include/clang/Options/FlangOptions.td
@@ -438,6 +438,11 @@ def fcoarray : Flag<["-"], "fcoarray">,
 
 let Visibility = [FC1Option] in {
 
+def fno_openmp_main_program_globals : Flag<["-"], "fno-openmp-main-program-globals">,
+  Group<f_Group>,
+  HelpText<"Disable static storage selection for small main program scalars "
+           "used in explicit host OpenMP PARALLEL regions">;
+
 def ffp_maxmin_behavior_EQ
     : Joined<["-"], "ffp-maxmin-behavior=">,
       Flags<[HelpHidden]>,
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index f7181b1fe5b47f..98e6aa279acafe 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -87,6 +87,9 @@ page](https://llvm.org/releases/).
 
 ## Non-comprehensive list of changes in this release
 
+- With OpenMP enabled, eligible small main-program scalars used in explicit
+  host `PARALLEL` regions now use static storage by default.
+
 - Added support for the OpenMP implementation-defined extension sentinels
   (OpenMP 5.2, section 3.1): `!$omx`, `c$omx` and `*$omx` in fixed source form
   and `!$ompx` in free source form. These sentinels are recognized like their
diff --git a/flang/include/flang/Evaluate/tools.h b/flang/include/flang/Evaluate/tools.h
index 981fec487ceab1..b001fcfb1de181 100644
--- a/flang/include/flang/Evaluate/tools.h
+++ b/flang/include/flang/Evaluate/tools.h
@@ -1794,6 +1794,9 @@ bool IsObjectPointer(const Symbol *);
 bool IsAllocatableOrObjectPointer(const Symbol *);
 bool IsAutomatic(const Symbol &);
 bool IsSaved(const Symbol &); // saved implicitly or explicitly
+// Test the size boundary used by main-program storage policies. The argument
+// must already be an association root; this does not test SAVE or object kind.
+bool IsSmallMainProgramVariable(const Symbol &);
 bool IsDummy(const Symbol &);
 
 bool IsAssumedRank(const Symbol &);
diff --git a/flang/include/flang/Lower/HostAssociations.h b/flang/include/flang/Lower/HostAssociations.h
index ee9136d80ea946..73e644160368a0 100644
--- a/flang/include/flang/Lower/HostAssociations.h
+++ b/flang/include/flang/Lower/HostAssociations.h
@@ -10,7 +10,10 @@
 #define FORTRAN_LOWER_HOSTASSOCIATIONS_H
 
 #include "mlir/IR/Types.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/SmallVector.h"
+#include <vector>
 
 namespace Fortran {
 namespace semantics {
@@ -21,6 +24,9 @@ class Scope;
 namespace lower {
 class AbstractConverter;
 class SymMap;
+namespace pft {
+struct Variable;
+}
 
 /// Internal procedures in Fortran may access variables declared in the host
 /// procedure directly. We bundle these variables together in a tuple and pass
@@ -35,10 +41,14 @@ class HostAssociations {
   bool hasTupleAssociations() const { return !tupleSymbols.empty(); }
 
   /// Adds a set of Symbols that will be the host associated bindings for this
-  /// host procedure.
+  /// host procedure. getVariables is invoked synchronously only for newly
+  /// promoted globals and must return Program's finalized, stable records.
   void addSymbolsToBind(
       const llvm::SetVector<const Fortran::semantics::Symbol *> &symbols,
-      const Fortran::semantics::Scope &hostScope);
+      const Fortran::semantics::Scope &hostScope,
+      llvm::function_ref<const std::vector<pft::Variable> &()> getVariables);
+
+  const std::vector<pft::Variable> *getVariables() const { return variables; }
 
   /// Code gen the FIR for the local bindings for the host associated symbols
   /// for the host (parent) procedure using `builder`.
@@ -68,6 +78,14 @@ class HostAssociations {
   /// Canonical vector of host associated global symbols.
   llvm::SetVector<const Fortran::semantics::Symbol *> globalSymbols;
 
+  /// Ordered bindings selected once when newly promoted globals occur.
+  /// These records borrow Program's cached variable list; std::map nodes and
+  /// finalized variable lists remain stable through all child bindings.
+  llvm::SmallVector<const pft::Variable *> globalBindings;
+  /// Whole-scope records reused by the host's own lowering as well. A null
+  /// pointer preserves the legacy path when no newly promoted globals occur.
+  const std::vector<pft::Variable> *variables = nullptr;
+
   /// The type of the extra argument to be added to each internal procedure.
   mlir::Type argType;
 
diff --git a/flang/include/flang/Lower/LoweringOptions.def b/flang/include/flang/Lower/LoweringOptions.def
index 4590df1c8a6f8a..f4a0d15c0bb42c 100644
--- a/flang/include/flang/Lower/LoweringOptions.def
+++ b/flang/include/flang/Lower/LoweringOptions.def
@@ -49,6 +49,10 @@ ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
 /// If false, assume that the shapes/types/allocation-status match.
 ENUM_LOWERINGOPT(ReallocateLHS, unsigned, 1, 1)
 
+/// If true, place eligible small main program scalars used directly by explicit
+/// host OpenMP PARALLEL regions in static memory. On by default.
+ENUM_LOWERINGOPT(OpenMPMainProgramGlobals, unsigned, 1, 1)
+
 /// If true, initialize globals without initialization to zero.
 /// On by default.
 ENUM_LOWERINGOPT(InitGlobalZero, unsigned, 1, 1)
diff --git a/flang/include/flang/Lower/PFTBuilder.h b/flang/include/flang/Lower/PFTBuilder.h
index 981a78068c6589..dfa56ab3cddd5f 100644
--- a/flang/include/flang/Lower/PFTBuilder.h
+++ b/flang/include/flang/Lower/PFTBuilder.h
@@ -667,6 +667,10 @@ VariableList getScopeVariableList(const Fortran::semantics::Scope &scope);
 /// depends on. \p symbol itself will be the last variable in the list.
 VariableList getDependentVariableList(const Fortran::semantics::Symbol &);
 
+/// Return the enclosing Program's stable variable-list cache.
+template <typename A>
+ScopeVariableListMap &getScopeVariableListMap(A &node);
+
 struct FunctionLikeUnit;
 /// Create an ordered list of equivalence sets and variables from host
 /// [sub]module scopes of \p funit that are referenced in \p funit. This is
@@ -750,7 +754,11 @@ struct FunctionLikeUnit : public ProgramUnit {
 
   void setHostAssociatedSymbols(
       const llvm::SetVector<const semantics::Symbol *> &symbols) {
-    hostAssociations.addSymbolsToBind(symbols, getScope());
+    hostAssociations.addSymbolsToBind(
+        symbols, getScope(), [&]() -> const VariableList & {
+          return getScopeVariableList(getScope(),
+                                      getScopeVariableListMap(*this));
+        });
   }
 
   /// Return the host associations, if any, from the parent (host) procedure.
@@ -900,7 +908,8 @@ struct Program {
 private:
   std::list<Units> units;
   semantics::CommonBlockList commonBlocks;
-  ScopeVariableListMap scopeVariableListMap; // module and submodule scopes
+  /// Module/submodule scopes and MAIN hosts with newly promoted globals.
+  ScopeVariableListMap scopeVariableListMap;
 };
 
 /// Helper to get location from FunctionLikeUnit/ModuleLikeUnit begin/end
diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index d99e048f627717..b2cd7dc519a977 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -973,7 +973,10 @@ class Symbol {
       OmpThreadprivate, OmpDeclareReduction, OmpFlushed, OmpCriticalLock,
       OmpIfSpecified, OmpNone, OmpPreDetermined, OmpExplicit, OmpImplicit,
       OmpDependObject, OmpInclusiveScan, OmpExclusiveScan, OmpInScanReduction,
-      OmpUniform, OmpAssumesNoOpenmp);
+      OmpUniform, OmpAssumesNoOpenmp,
+      // Main-program storage needed by a host OpenMP outlined region. Set by
+      // lowering after semantic analysis, without changing data-sharing rules.
+      OmpMainGlobal);
   using Flags = common::EnumSet<Flag, Flag_enumSize>;
 
   const Scope &owner() const { return *owner_; }
diff --git a/flang/lib/Evaluate/tools.cpp b/flang/lib/Evaluate/tools.cpp
index 9f560cabd55c8a..c3e1b9b9dc5276 100644
--- a/flang/lib/Evaluate/tools.cpp
+++ b/flang/lib/Evaluate/tools.cpp
@@ -2830,6 +2830,11 @@ bool IsAutomatic(const Symbol &original) {
   return false;
 }
 
+bool IsSmallMainProgramVariable(const Symbol &symbol) {
+  return symbol.owner().kind() == Scope::Kind::MainProgram &&
+      symbol.size() <= 32;
+}
+
 bool IsSaved(const Symbol &original) {
   const Symbol &symbol{GetAssociationRoot(original)};
   const Scope &scope{symbol.owner()};
@@ -2860,7 +2865,7 @@ bool IsSaved(const Symbol &original) {
       (features.IsEnabled(common::LanguageFeature::SaveMainProgram) ||
           (features.IsEnabled(
                common::LanguageFeature::SaveBigMainProgramVariables) &&
-              symbol.size() > 32))) {
+              !IsSmallMainProgramVariable(symbol)))) {
     // With SaveBigMainProgramVariables, keeping all unsaved main program
     // variables of 32 bytes or less on the stack allows keeping numerical and
     // logical scalars, small scalar characters or derived, small arrays, and
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 64ef085761e3fb..3c1b2a7b111404 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1910,6 +1910,12 @@ bool CompilerInvocation::createFromArgs(
   parsePreprocessorArgs(invoc.getPreprocessorOpts(), args);
   parseCodeGenArgs(invoc.getCodeGenOpts(), args, diags);
   success &= parseDoConcurrentMapping(invoc.getCodeGenOpts(), args, diags);
+  // Device DO CONCURRENT targets are generated after PFT construction. Keep
+  // host and device capture layouts identical when these targets are enabled.
+  invoc.loweringOpts.setOpenMPMainProgramGlobals(
+      !args.hasArg(clang::options::OPT_fno_openmp_main_program_globals) &&
+      invoc.getCodeGenOpts().getDoConcurrentMapping() !=
+          CodeGenOptions::DoConcurrentMappingKind::DCMK_Device);
   success &= parseDebugArgs(invoc.getCodeGenOpts(), args, diags,
                             llvm::Triple(invoc.getTargetOpts().triple));
 
@@ -2182,6 +2188,12 @@ CompilerInvocation::getSemanticsCtx(
 void CompilerInvocation::setLoweringOptions() {
   const CodeGenOptions &codegenOpts = getCodeGenOpts();
 
+  // API clients may change the mapping after argument parsing. Reapply the
+  // device capture-layout restriction before constructing the PFT.
+  if (codegenOpts.getDoConcurrentMapping() ==
+      CodeGenOptions::DoConcurrentMappingKind::DCMK_Device)
+    loweringOpts.setOpenMPMainProgramGlobals(false);
+
   // Lower TRANSPOSE as a runtime call under -O0.
   loweringOpts.setOptimizeTranspose(codegenOpts.OptimizationLevel > 0);
   loweringOpts.setUnderscoring(codegenOpts.Underscoring);
diff --git a/flang/lib/Frontend/ParserActions.cpp b/flang/lib/Frontend/ParserActions.cpp
index 95f3ec7d5b9834..cc54f565e9516b 100644
--- a/flang/lib/Frontend/ParserActions.cpp
+++ b/flang/lib/Frontend/ParserActions.cpp
@@ -48,8 +48,10 @@ void dumpProvenance(CompilerInstance &ci) {
 void dumpPreFIRTree(CompilerInstance &ci) {
   auto &parseTree{*ci.getParsing().parseTree()};
 
-  // Use default lowering options for PFT dump
+  // Use default lowering options, preserving the main-program globals setting.
   lower::LoweringOptions loweringOptions{};
+  loweringOptions.setOpenMPMainProgramGlobals(
+      ci.getInvocation().getLoweringOpts().getOpenMPMainProgramGlobals());
   if (auto ast{lower::createPFT(parseTree, ci.getSemanticsContext(),
                                 loweringOptions)}) {
     lower::dumpPFT(llvm::outs(), *ast);
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 67e6d7d9692d3d..6b4654ed71089b 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -6668,10 +6668,17 @@ class FirConverter : public Fortran::lower::AbstractConverter {
            Fortran::lower::pft::getHostModuleVariableList(funit))
         instantiateVar(var, storeMap);
 
+    // New global host associations already borrow a stable scope-variable plan.
+    // Reuse those records rather than recomputing them for the host itself.
+    const auto *variables = funit.hostAssociations.getVariables();
+    std::optional<Fortran::lower::pft::VariableList> localVariables;
+    if (!variables) {
+      variables = &localVariables.emplace(
+          Fortran::lower::pft::getScopeVariableList(scope));
+    }
     // Map function equivalences and variables.
     mlir::Value primaryFuncResultStorage;
-    for (const Fortran::lower::pft::Variable &var :
-         Fortran::lower::pft::getScopeVariableList(scope)) {
+    for (const Fortran::lower::pft::Variable &var : *variables) {
       // Always instantiate aggregate storage blocks.
       if (var.isAggregateStore()) {
         instantiateVar(var, storeMap);
@@ -6698,6 +6705,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       }
     }
 
+    // Preserve the legacy temporary's lifetime when no cached plan exists.
+    localVariables.reset();
     // TODO: should use same mechanism as equivalence?
     // One blocking point is character entry returns that need special handling
     // since they are not locally allocated but come as argument. CHARACTER(*)
diff --git a/flang/lib/Lower/HostAssociations.cpp b/flang/lib/Lower/HostAssociations.cpp
index 496ff4bb5d7ccc..01e5371adf362f 100644
--- a/flang/lib/Lower/HostAssociations.cpp
+++ b/flang/lib/Lower/HostAssociations.cpp
@@ -22,6 +22,10 @@
 #include "flang/Optimizer/Builder/Todo.h"
 #include "flang/Optimizer/Support/FatalError.h"
 #include "flang/Semantics/tools.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/Support/Debug.h"
+#include <algorithm>
 #include <optional>
 
 #define DEBUG_TYPE "flang-host-assoc"
@@ -540,10 +544,12 @@ static mlir::Value genTupleCoor(fir::FirOpBuilder &builder, mlir::Location loc,
 
 void Fortran::lower::HostAssociations::addSymbolsToBind(
     const llvm::SetVector<const Fortran::semantics::Symbol *> &symbols,
-    const Fortran::semantics::Scope &hostScope) {
+    const Fortran::semantics::Scope &hostScope,
+    llvm::function_ref<const std::vector<pft::Variable> &()> getVariables) {
   assert(tupleSymbols.empty() && globalSymbols.empty() &&
          "must be initially empty");
   this->hostScope = &hostScope;
+  bool hasNewGlobals = false;
   for (const auto *s : symbols)
     // GlobalOp are created for non-global threadprivate variable,
     //  so considering them as globals.
@@ -551,10 +557,59 @@ void Fortran::lower::HostAssociations::addSymbolsToBind(
         (*s).test(Fortran::semantics::Symbol::Flag::OmpThreadprivate)) {
       // The ultimate symbol is stored here so that global symbols from the
       // host scope can later be searched in this set.
-      globalSymbols.insert(&s->GetUltimate());
+      const auto &ultimate = s->GetUltimate();
+      globalSymbols.insert(&ultimate);
+      hasNewGlobals |= ultimate.test(semantics::Symbol::Flag::OmpMainGlobal);
     } else {
       tupleSymbols.insert(s);
     }
+  if (hasNewGlobals) {
+    variables = &getVariables();
+    // Aggregate stores precede their aliases. Select backwards so each chosen
+    // alias requests its backing store using the already computed storage key.
+    // Resolve each USE/host association tail once, including rejected records.
+    // Direct objects require no cache lookup or allocation.
+    llvm::DenseMap<const semantics::Symbol *, const semantics::Symbol *> roots;
+    auto root = [&](const semantics::Symbol &symbol) {
+      const auto *original = &symbol;
+      llvm::SmallVector<const semantics::Symbol *> path;
+      while (true) {
+        const semantics::Symbol *next;
+        if (const auto *details = original->detailsIf<semantics::UseDetails>())
+          next = &details->symbol();
+        else if (const auto *details =
+                     original->detailsIf<semantics::HostAssocDetails>())
+          next = &details->symbol();
+        else
+          break;
+        if (auto it = roots.find(original); it != roots.end()) {
+          original = it->second;
+          break;
+        }
+        path.push_back(original);
+        LLVM_DEBUG(llvm::dbgs() << "main-global binding association\n");
+        original = next;
+      }
+      for (const auto *entry : path)
+        roots.try_emplace(entry, original);
+      return original;
+    };
+    llvm::DenseSet<AggregateStoreKey> stores;
+    for (const auto &variable : llvm::reverse(*variables)) {
+      const auto *original =
+          variable.hasSymbol() ? root(variable.getSymbol()) : nullptr;
+      if (original && globalSymbols.contains(original)) {
+        globalBindings.push_back(&variable);
+        if (variable.isAlias())
+          stores.insert({&original->owner(), variable.getAliasOffset()});
+      } else if (variable.isAggregateStore() && variable.isGlobal() &&
+                 stores.contains({variable.getOwningScope(),
+                                  variable.getAggregateStore().getOffset()})) {
+        globalBindings.push_back(&variable);
+      }
+    }
+    std::reverse(globalBindings.begin(), globalBindings.end());
+  }
 }
 
 void Fortran::lower::HostAssociations::hostProcedureBindings(
@@ -593,18 +648,24 @@ void Fortran::lower::HostAssociations::internalProcedureBindings(
     // The host scope variable list is required to deal with host variables
     // that are equivalenced and requires instantiating the right global
     // AggregateStore.
-    for (auto &hostVariable : pft::getScopeVariableList(*hostScope))
-      if ((hostVariable.isAggregateStore() && hostVariable.isGlobal()) ||
-          (hostVariable.hasSymbol() &&
-           globalSymbols.contains(&hostVariable.getSymbol().GetUltimate()))) {
-        Fortran::lower::instantiateVariable(converter, hostVariable, symMap,
-                                            storeMap);
-        // Generate threadprivate Op for host associated variables.
-        if (hostVariable.hasSymbol() &&
-            hostVariable.getSymbol().test(
-                Fortran::semantics::Symbol::Flag::OmpThreadprivate))
-          Fortran::lower::genThreadprivateOp(converter, hostVariable);
-      }
+    auto bind = [&](const pft::Variable &hostVariable) {
+      Fortran::lower::instantiateVariable(converter, hostVariable, symMap,
+                                          storeMap);
+      if (hostVariable.hasSymbol() &&
+          hostVariable.getSymbol().test(
+              Fortran::semantics::Symbol::Flag::OmpThreadprivate))
+        Fortran::lower::genThreadprivateOp(converter, hostVariable);
+    };
+    if (variables) {
+      for (const auto *variable : globalBindings)
+        bind(*variable);
+    } else {
+      for (const auto &variable : pft::getScopeVariableList(*hostScope))
+        if ((variable.isAggregateStore() && variable.isGlobal()) ||
+            (variable.hasSymbol() &&
+             globalSymbols.contains(&variable.getSymbol().GetUltimate())))
+          bind(variable);
+    }
   }
   if (tupleSymbols.empty())
     return;
diff --git a/flang/lib/Lower/PFTBuilder.cpp b/flang/lib/Lower/PFTBuilder.cpp
index 1ecce4b82d2a46..f36f2bd2865977 100644
--- a/flang/lib/Lower/PFTBuilder.cpp
+++ b/flang/lib/Lower/PFTBuilder.cpp
@@ -7,15 +7,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Lower/PFTBuilder.h"
+#include "flang/Evaluate/traverse.h"
 #include "flang/Lower/LoweringOptions.h"
 #include "flang/Lower/Support/Utils.h"
 #include "flang/Parser/dump-parse-tree.h"
+#include "flang/Parser/openmp-utils.h"
 #include "flang/Parser/parse-tree-visitor.h"
 #include "flang/Semantics/openmp-utils.h"
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/tools.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/IntervalMap.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include <algorithm>
@@ -83,6 +86,293 @@ struct UnwrapStmt<parser::UnlabeledStatement<A>> {
 void dumpScope(const semantics::Scope *scope, int depth = -1);
 #endif
 
+/// Collect direct storage uses while retaining complex expressions on the
+/// existing storage path. Skipped nodes do not seed selection from their
+/// children.
+struct MainSymbolVisitor
+    : public evaluate::AllTraverse<MainSymbolVisitor, true, false> {
+  using Base = evaluate::AllTraverse<MainSymbolVisitor, true, false>;
+  /// Borrow the callback for one synchronous expression walk.
+  explicit MainSymbolVisitor(
+      llvm::function_ref<void(const semantics::Symbol &)> callback)
+      : Base{*this}, callback{callback} {}
+  using Base::operator();
+  /// Stream each direct symbol occurrence without building subtree sets.
+  bool operator()(const semantics::Symbol &symbol) const {
+    LLVM_DEBUG(llvm::dbgs() << "main-global symbol\n");
+    callback(symbol);
+    return true;
+  }
+  /// TODO: Collect runtime dependencies of calls, constructors, PDT parameters
+  /// and coarray references, excluding metadata and folded payloads and
+  /// avoiding copies of selector expressions.
+  bool operator()(const semantics::DerivedTypeSpec &) const { return true; }
+  /// Computed derived constants cannot add direct storage uses.
+  bool operator()(const evaluate::Constant<evaluate::SomeDerived> &) const {
+    return true;
+  }
+  bool operator()(const evaluate::CoarrayRef &) const { return true; }
+  bool operator()(const evaluate::ProcedureRef &) const { return true; }
+  /// Defined assignment is a call; ordinary assignment retains both operands.
+  bool operator()(const evaluate::Assignment &assignment) const {
+    if (std::holds_alternative<evaluate::ProcedureRef>(assignment.u))
+      return true;
+    return Base::operator()(assignment);
+  }
+  bool operator()(const evaluate::StructureConstructor &) const { return true; }
+  template <typename A>
+  bool operator()(const evaluate::ArrayConstructor<A> &) const {
+    return true;
+  }
+  /// Avoid syntax collection after deliberately skipping resolved calls.
+  bool Pre(const parser::CallStmt &) { return false; }
+  /// SHARED names are metadata rather than accesses to object storage.
+  bool Pre(const parser::LocalitySpec::Shared &) { return false; }
+  template <typename A>
+  bool Pre(const A &node) {
+    if constexpr (parser::HasTypedExpr<A>::value) {
+      if (const auto *expr = semantics::GetExpr(nullptr, node)) {
+        collect(*expr);
+        return false;
+      }
+    } else if constexpr (parser::HasTypedAssignment<A>::value) {
+      if (node.typedAssignment && node.typedAssignment->v) {
+        collect(*node.typedAssignment->v);
+        return false;
+      }
+    }
+    return true;
+  }
+  bool Pre(const parser::Name &name) {
+    if (name.symbol)
+      callback(*name.symbol);
+    return false;
+  }
+  template <typename A>
+  void Post(const A &) {}
+
+private:
+  /// Traverse the outer typed expression once before returning to parser Walk.
+  template <typename A>
+  void collect(const A &expr) {
+    LLVM_DEBUG(llvm::dbgs() << "main-global expression\n");
+    (*this)(expr);
+  }
+  llvm::function_ref<void(const semantics::Symbol &)> callback;
+};
+
+/// Select small MAIN scalars directly used inside explicit host PARALLEL
+/// regions during PFT construction, before host associations are classified.
+/// Unsupported contexts retain the existing storage policy for the whole MAIN.
+class MainGlobalAnalysis {
+public:
+  /// Disabled analyses do not walk statements or semantic scopes.
+  explicit MainGlobalAnalysis(bool enabled) : enabled{enabled} {}
+  /// Determine membership once per procedure, including serially called
+  /// internal procedures that contain their own PARALLEL regions.
+  void enterFunction(const lower::pft::FunctionLikeUnit &unit) {
+    if (!enabled)
+      return;
+    const auto &scope = unit.getScope();
+    relevant = unit.isMainProgram();
+    if (relevant) {
+      mainScope = &scope;
+      // Bare MAIN SAVE already gives every eligible object global storage.
+      if (scope.hasSAVE()) {
+        enabled = false;
+        relevant = false;
+        return;
+      }
+    }
+    // Internal subprograms cannot themselves contain internal subprograms.
+    // Check only the immediate PFT parent, without semantic submodule ancestry.
+    if (!relevant && mainScope)
+      if (const auto *host =
+              unit.parent.getIf<lower::pft::FunctionLikeUnit>()) {
+        LLVM_DEBUG(llvm::dbgs() << "main-global function parent\n");
+        relevant = host->isMainProgram();
+      }
+    if (relevant && scope.symbol()) {
+      const auto &symbol = *scope.symbol();
+      if (symbol.test(semantics::Symbol::Flag::OmpDeclareTarget))
+        unsupported = true;
+    }
+  }
+  /// Observe directive headers before their bodies for the whole-MAIN fallback.
+  /// TARGET leaves of combined constructs are checked by enterOpenMP().
+  void directive(llvm::omp::Directive id) {
+    if (!relevant || unsupported)
+      return;
+    switch (id) {
+    case llvm::omp::Directive::OMPD_metadirective:
+    case llvm::omp::Directive::OMPD_declare_target:
+    case llvm::omp::Directive::OMPD_target_data:
+    case llvm::omp::Directive::OMPD_target_enter_data:
+    case llvm::omp::Directive::OMPD_target_exit_data:
+    case llvm::omp::Directive::OMPD_target_update:
+      unsupported = true;
+      return;
+    default:
+      return;
+    }
+  }
+  /// Track nesting independently of fallback state, keeping entry and exit
+  /// balanced when an unsupported context is encountered inside PARALLEL.
+  void enterOpenMP(const parser::OpenMPConstruct &construct) {
+    if (!relevant)
+      return;
+    bool parallel = false;
+    for (auto leaf : llvm::omp::getLeafConstructsOrSelf(
+             parser::omp::GetOmpDirectiveName(construct).v)) {
+      parallel |= leaf == llvm::omp::Directive::OMPD_parallel;
+      unsupported |= leaf == llvm::omp::Directive::OMPD_target;
+    }
+    parallels.push_back(parallel);
+    parallelDepth += parallel;
+  }
+  /// Restore the enclosing PARALLEL nesting after its PFT construct.
+  void exitOpenMP() {
+    if (relevant) {
+      parallelDepth -= parallels.back();
+      parallels.pop_back();
+    }
+  }
+  /// Visit executable leaves in PARALLEL, including WHERE/FORALL statements
+  /// without PFT evaluations. Clauses do not seed selection; an IF action has
+  /// its own evaluation and must not be visited a second time.
+  template <typename A>
+  void collect(const A &node) {
+    if (!relevant || unsupported || !parallelDepth)
+      return;
+    auto collectSymbol = [&](const semantics::Symbol &symbol) {
+      const auto *original = root(symbol);
+      if (&original->owner() == mainScope)
+        objects.insert(original);
+    };
+    MainSymbolVisitor visitor{collectSymbol};
+    if constexpr (std::is_same_v<A, lower::pft::Evaluation>) {
+      node.visit([&](const auto &stmt) {
+        if constexpr (std::is_same_v<std::decay_t<decltype(stmt)>,
+                                     parser::IfStmt>)
+          parser::Walk(std::get<parser::ScalarLogicalExpr>(stmt.t), visitor);
+        else if constexpr (lower::pft::isActionStmt<
+                               std::decay_t<decltype(stmt)>> ||
+                           lower::pft::isConstructStmt<
+                               std::decay_t<decltype(stmt)>>)
+          parser::Walk(stmt, visitor);
+      });
+    } else {
+      parser::Walk(node, visitor);
+    }
+  }
+  /// Filter whole original objects, then set storage flags. Set iteration
+  /// order cannot affect these independent decisions or generated declarations.
+  void finalize() {
+    if (unsupported || objects.empty())
+      return;
+    objects.remove_if(
+        [](const semantics::Symbol *symbol) { return !canUseGlobal(*symbol); });
+    if (objects.empty())
+      return;
+    for (const auto &set : mainScope->equivalenceSets())
+      for (const auto &object : set)
+        if (objects.erase(&object.symbol) && objects.empty())
+          return;
+    // Retain legacy storage for the privatization/locality flags below and for
+    // NAMELIST members throughout MAIN, including unused internal procedures.
+    // This deliberately avoids analyzing individual copy bindings.
+    // LocalityReduce uses the original accumulator, so it remains eligible.
+    const semantics::Symbol::Flags excluded{
+        semantics::Symbol::Flag::OmpPrivate,
+        semantics::Symbol::Flag::OmpFirstPrivate,
+        semantics::Symbol::Flag::OmpLastPrivate,
+        semantics::Symbol::Flag::OmpLinear,
+        semantics::Symbol::Flag::OmpReduction,
+        semantics::Symbol::Flag::OmpInReduction,
+        semantics::Symbol::Flag::OmpGroupPrivate,
+        semantics::Symbol::Flag::OmpCopyPrivate,
+        semantics::Symbol::Flag::OmpPreDetermined,
+        semantics::Symbol::Flag::LocalityLocal,
+        semantics::Symbol::Flag::LocalityLocalInit,
+        semantics::Symbol::Flag::InNamelist};
+    llvm::SmallVector<const semantics::Scope *> scopes{mainScope};
+    while (!scopes.empty()) {
+      const auto *scope = scopes.pop_back_val();
+      LLVM_DEBUG(llvm::dbgs() << "main-global scope\n");
+      // This sweep only removes candidates; stop resolving after exhaustion.
+      for (const auto &[name, symbol] : *scope)
+        if ((symbol->flags() & excluded).any() &&
+            objects.erase(root(*symbol)) && objects.empty())
+          return;
+      for (const auto &child : scope->children())
+        scopes.push_back(&child);
+    }
+    for (const auto *symbol : objects)
+      const_cast<semantics::Symbol *>(symbol)->set(
+          semantics::Symbol::Flag::OmpMainGlobal);
+  }
+
+private:
+  /// Memoize every USE/host association tail across collection and filtering.
+  /// ASSOCIATE entities deliberately retain their own identity.
+  const semantics::Symbol *root(const semantics::Symbol &symbol) {
+    const auto *original = &symbol;
+    llvm::SmallVector<const semantics::Symbol *> path;
+    while (true) {
+      if (auto it = roots.find(original); it != roots.end()) {
+        original = it->second;
+        break;
+      }
+      path.push_back(original);
+      LLVM_DEBUG(llvm::dbgs() << "main-global association\n");
+      if (const auto *details =
+              original->detailsIf<semantics::HostAssocDetails>())
+        original = &details->symbol();
+      else if (const auto *details =
+                   original->detailsIf<semantics::UseDetails>())
+        original = &details->symbol();
+      else
+        break;
+    }
+    for (const auto *entry : path)
+      roots.try_emplace(entry, original);
+    LLVM_DEBUG(llvm::dbgs() << "main-global reference: "
+                            << toStringRef(original->name()) << '\n');
+    return original;
+  }
+  /// Leave arrays, descriptors, derived types, automatic objects and special
+  /// runtime/device storage on their existing lowering paths.
+  static bool canUseGlobal(const semantics::Symbol &symbol) {
+    // Privatizers and MIF coarray handles append these fragments to object
+    // names. Leave potential collisions on their existing local-storage path.
+    const auto name = toStringRef(symbol.name());
+    if (name.contains("_private_") || name.contains("_firstprivate_") ||
+        name.ends_with("_coarray_handle"))
+      return false;
+    const auto *object = symbol.detailsIf<semantics::ObjectEntityDetails>();
+    const auto *type = symbol.GetType();
+    return object && !object->IsArray() && type && type->AsIntrinsic() &&
+           semantics::IsSmallMainProgramVariable(symbol) && symbol.size() &&
+           !semantics::IsAllocatableOrPointer(symbol) &&
+           !semantics::IsAutomatic(symbol) && !lower::symbolIsGlobal(symbol) &&
+           !symbol.test(semantics::Symbol::Flag::OmpThreadprivate) &&
+           !symbol.test(semantics::Symbol::Flag::CrayPointer) &&
+           !symbol.test(semantics::Symbol::Flag::CrayPointee) &&
+           !symbol.test(
+               semantics::Symbol::Flag::OmpDeclarativeAllocateDirective) &&
+           !symbol.test(
+               semantics::Symbol::Flag::OmpExecutableAllocateDirective);
+  }
+  bool enabled;
+  bool relevant = false;
+  bool unsupported = false;
+  const semantics::Scope *mainScope = nullptr;
+  unsigned parallelDepth = 0;
+  llvm::SmallVector<bool> parallels;
+  llvm::DenseMap<const semantics::Symbol *, const semantics::Symbol *> roots;
+  llvm::DenseSet<const semantics::Symbol *> objects;
+};
+
 /// The instantiation of a parse tree visitor (Pre and Post) is extremely
 /// expensive in terms of compile and link time. So one goal here is to
 /// limit the bridge to one such instantiation.
@@ -92,17 +382,39 @@ class PFTBuilder {
              const lower::LoweringOptions &loweringOptions)
       : pgm{std::make_unique<lower::pft::Program>(
             semanticsContext.GetCommonBlocks())},
-        semanticsContext{semanticsContext}, loweringOptions{loweringOptions} {
+        semanticsContext{semanticsContext}, loweringOptions{loweringOptions},
+        mainGlobals{loweringOptions.getOpenMPMainProgramGlobals() &&
+                    semanticsContext.languageFeatures().IsEnabled(
+                        common::LanguageFeature::OpenMP) &&
+                    !semanticsContext.langOptions().OpenMPSimd &&
+                    !semanticsContext.langOptions().OpenMPIsTargetDevice &&
+                    // TODO: Support host regions in OpenACC/CUDA units once
+                    // late routine targets and device captures are resolved.
+                    !semanticsContext.languageFeatures().IsEnabled(
+                        common::LanguageFeature::OpenACC) &&
+                    !semanticsContext.languageFeatures().IsEnabled(
+                        common::LanguageFeature::CUDA) &&
+                    !semanticsContext.languageFeatures().IsEnabled(
+                        common::LanguageFeature::SaveMainProgram) &&
+                    !semanticsContext.languageFeatures().IsEnabled(
+                        common::LanguageFeature::DefaultSave)} {
     lower::pft::PftNode pftRoot{*pgm.get()};
     pftParentStack.push_back(pftRoot);
   }
 
   /// Get the result
-  std::unique_ptr<lower::pft::Program> result() { return std::move(pgm); }
+  std::unique_ptr<lower::pft::Program> result() {
+    mainGlobals.finalize();
+    return std::move(pgm);
+  }
 
   template <typename A>
   constexpr bool Pre(const A &a) {
-    if constexpr (lower::pft::isFunctionLike<A>) {
+    if constexpr (std::is_base_of_v<parser::OmpDirectiveSpecification, A>) {
+      if (currentFunctionUnit && !interfaceBodyLevel)
+        mainGlobals.directive(a.DirId());
+      return true;
+    } else if constexpr (lower::pft::isFunctionLike<A>) {
       return enterFunction(a, semanticsContext);
     } else if constexpr (lower::pft::isConstruct<A> ||
                          lower::pft::isDirective<A>) {
@@ -140,6 +452,11 @@ class PFTBuilder {
                 },
             },
             stmt.unwrapped.u);
+      } else if constexpr (std::is_same_v<T, parser::WhereStmt> ||
+                           std::is_same_v<T, parser::ForallStmt>) {
+        // Nested single statements have no PFT evaluation. Keep PFT
+        // construction unchanged and collect their expressions here instead.
+        mainGlobals.collect(stmt.unwrapped);
       } else if constexpr (std::is_same_v<T, parser::UseStmt>) {
         // Process USE statement for debug info generation
         processUseStmt(stmt.unwrapped);
@@ -523,6 +840,7 @@ class PFTBuilder {
     pushEvaluationList(&unit.evaluationList);
     pftParentStack.emplace_back(unit);
     currentFunctionUnit = &unit;
+    mainGlobals.enterFunction(unit);
     LLVM_DEBUG(dumpScope(&unit.getScope()));
     return true;
   }
@@ -581,6 +899,8 @@ class PFTBuilder {
   bool enterConstructOrDirective(const A &constructOrDirective) {
     lower::pft::Evaluation &eval = addEvaluation(
         lower::pft::Evaluation{constructOrDirective, pftParentStack.back()});
+    if constexpr (std::is_same_v<A, parser::OpenMPConstruct>)
+      mainGlobals.enterOpenMP(constructOrDirective);
     eval.evaluationList.reset(new lower::pft::EvaluationList);
     pushEvaluationList(eval.evaluationList.get());
     pftParentStack.emplace_back(eval);
@@ -600,6 +920,8 @@ class PFTBuilder {
     rewriteIfGotos();
     rewriteTrailingCycle();
     auto *eval = constructAndDirectiveStack.back();
+    if (eval->isA<parser::OpenMPConstruct>())
+      mainGlobals.exitOpenMP();
     if (eval->isExecutableDirective() && !isOpenMPLoopConstruct(eval)) {
       // A construct at the end of an (unstructured) OpenACC or OpenMP
       // construct region must have an exit target inside the region.
@@ -673,8 +995,10 @@ class PFTBuilder {
     lower::pft::FunctionLikeUnit *owningProcedure = eval.getOwningProcedure();
     evaluationListStack.back()->emplace_back(std::move(eval));
     lower::pft::Evaluation *p = &evaluationListStack.back()->back();
-    if (p->isActionStmt() || p->isConstructStmt() || p->isEndStmt() ||
-        p->isExecutableDirective()) {
+    const bool isActionOrConstruct = p->isActionStmt() || p->isConstructStmt();
+    if (owningProcedure && isActionOrConstruct)
+      mainGlobals.collect(*p);
+    if (isActionOrConstruct || p->isEndStmt() || p->isExecutableDirective()) {
       if (lastLexicalEvaluation) {
         lastLexicalEvaluation->lexicalSuccessor = p;
         p->printIndex = lastLexicalEvaluation->printIndex + 1;
@@ -1396,6 +1720,7 @@ class PFTBuilder {
   std::vector<lower::pft::PftNode> pftParentStack;
   const semantics::SemanticsContext &semanticsContext;
   const lower::LoweringOptions &loweringOptions;
+  MainGlobalAnalysis mainGlobals;
 
   llvm::SmallVector<bool> containsStmtStack{};
   lower::pft::ContainedUnitList *containedUnitList{};
@@ -1778,7 +2103,10 @@ bool Fortran::lower::definedInCommonBlock(const semantics::Symbol &sym) {
 
 /// Is the symbol `sym` a global?
 bool Fortran::lower::symbolIsGlobal(const semantics::Symbol &sym) {
-  return (semantics::IsSaved(sym) && semantics::CanCUDASymbolBeGlobal(sym)) ||
+  const auto &ultimate = sym.GetUltimate();
+  return ((semantics::IsSaved(ultimate) ||
+           ultimate.test(semantics::Symbol::Flag::OmpMainGlobal)) &&
+          semantics::CanCUDASymbolBeGlobal(sym)) ||
          lower::definedInCommonBlock(sym) || semantics::IsNamedConstant(sym);
 }
 
@@ -2299,8 +2627,12 @@ void Fortran::lower::pft::BlockDataUnit::dump() const {
 const lower::pft::VariableList &
 lower::pft::getScopeVariableList(const semantics::Scope &scope,
                                  ScopeVariableListMap &map) {
-  LLVM_DEBUG(llvm::dbgs() << "\ngetScopeVariableList of [sub]module scope <"
-                          << &scope << "> " << scope.GetName() << "\n");
+  LLVM_DEBUG(
+      llvm::dbgs() << "\ngetScopeVariableList of "
+                   << (scope.kind() == semantics::Scope::Kind::MainProgram
+                           ? "main program"
+                           : "[sub]module")
+                   << " scope <" << &scope << "> " << scope.GetName() << "\n");
   auto iter = map.find(&scope);
   if (iter == map.end()) {
     SymbolDependenceAnalysis sda(scope);
diff --git a/flang/test/Driver/openmp-main-program-globals.f90 b/flang/test/Driver/openmp-main-program-globals.f90
new file mode 100644
index 00000000000000..251dc34343013f
--- /dev/null
+++ b/flang/test/Driver/openmp-main-program-globals.f90
@@ -0,0 +1,11 @@
+! The opt-out is FC1-only: -Xflang forwards it, direct driver use is rejected,
+! and driver --help omits it.
+! REQUIRES: module-independent
+! RUN: %flang -### -fopenmp -Xflang -fno-openmp-main-program-globals -S %s 2>&1 | FileCheck %s --check-prefix=FORWARD
+! RUN: not %flang -### -fopenmp -fno-openmp-main-program-globals -S %s 2>&1 | FileCheck %s --check-prefix=DRIVER
+! RUN: %flang --help | FileCheck %s --check-prefix=HELP --implicit-check-not='-fno-openmp-main-program-globals'
+! FORWARD: "-fc1"{{.*}} "-fno-openmp-main-program-globals"
+! DRIVER: error: unknown argument '-fno-openmp-main-program-globals';
+! DRIVER-SAME: did you mean '-Xflang -fno-openmp-main-program-globals'?
+! HELP: OPTIONS:
+end
diff --git a/flang/test/Lower/OpenMP/default-clause-byref.f90 b/flang/test/Lower/OpenMP/default-clause-byref.f90
index 87fca12c33b229..dc78d6b4c10f94 100644
--- a/flang/test/Lower/OpenMP/default-clause-byref.f90
+++ b/flang/test/Lower/OpenMP/default-clause-byref.f90
@@ -1,11 +1,12 @@
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fno-openmp-main-program-globals -mmlir --force-byref-reduction %s -o - | FileCheck %s --check-prefixes=CHECK,OFF --implicit-check-not='fir.global internal @_QFEz{{[[:space:]]}}'
 ! This test checks lowering of OpenMP parallel directive
 ! with `DEFAULT` clause present.
 
 ! RUN: %flang_fc1 -emit-hlfir -fopenmp -mmlir --force-byref-reduction %s -o - \
-! RUN: | FileCheck %s
+! RUN: | FileCheck %s --check-prefixes=CHECK,ON
 
 ! RUN: bbc -fopenmp -emit-hlfir --force-byref-reduction %s -o - \
-! RUN: | FileCheck %s
+! RUN: | FileCheck %s --check-prefixes=CHECK,ON
 
 !CHECK:  omp.private {type = firstprivate} @[[W_FIRSTPRIVATIZER:_QFEw_firstprivate_i32]] : i32 copy {
 !CHECK:  ^bb0(%[[ORIG_W:.*]]: !fir.ref<i32>, %[[PRIV_W:.*]]: !fir.ref<i32>):
@@ -41,7 +42,8 @@
 !CHECK: %[[X_DECL:.*]]:2 = hlfir.declare %[[X]] uniq_name("_QFEx") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
 !CHECK: %[[Y:.*]] = fir.alloca i32 <{bindc_name = "y", uniq_name = "_QFEy"}>
 !CHECK: %[[Y_DECL:.*]]:2 = hlfir.declare %[[Y]] uniq_name("_QFEy") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK: %[[Z:.*]] = fir.alloca i32 <{bindc_name = "z", uniq_name = "_QFEz"}>
+!ON: %[[Z:.*]] = fir.address_of(@_QFEz) : !fir.ref<i32>
+!OFF: %[[Z:.*]] = fir.alloca i32 <{bindc_name = "z", uniq_name = "_QFEz"}>
 !CHECK: %[[Z_DECL:.*]]:2 = hlfir.declare %[[Z]] uniq_name("_QFEz") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
 !CHECK: omp.parallel private(
 !CHECK-SAME: @[[X_FIRSTPRIVATIZER]] %[[X_DECL]]#0 -> %[[PRIVATE_X:.*]], @[[Y_PRIVATIZER]] %[[Y_DECL]]#0 -> %[[PRIVATE_Y:.*]], @[[W_PRIVATIZER]] %[[W_DECL]]#0 -> %[[PRIVATE_W:.*]] : {{.*}}) {
diff --git a/flang/test/Lower/OpenMP/default-clause.f90 b/flang/test/Lower/OpenMP/default-clause.f90
index 456330c3314eeb..d8eaf01a6afdde 100644
--- a/flang/test/Lower/OpenMP/default-clause.f90
+++ b/flang/test/Lower/OpenMP/default-clause.f90
@@ -1,11 +1,12 @@
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fno-openmp-main-program-globals %s -o - | FileCheck %s --check-prefixes=CHECK,OFF --implicit-check-not='fir.global internal @_QFEz{{[[:space:]]}}'
 ! This test checks lowering of OpenMP parallel directive
 ! with `DEFAULT` clause present.
 
 ! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - \
-! RUN: | FileCheck %s
+! RUN: | FileCheck %s --check-prefixes=CHECK,ON
 
 ! RUN: bbc -fopenmp -emit-hlfir %s -o - \
-! RUN: | FileCheck %s
+! RUN: | FileCheck %s --check-prefixes=CHECK,ON
 
 
 !CHECK: func @_QQmain() attributes {fir.bindc_name = "DEFAULT_CLAUSE_LOWERING"} {
@@ -15,7 +16,8 @@
 !CHECK: %[[X_DECL:.*]]:2 = hlfir.declare %[[X]] uniq_name("_QFEx") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
 !CHECK: %[[Y:.*]] = fir.alloca i32 <{bindc_name = "y", uniq_name = "_QFEy"}>
 !CHECK: %[[Y_DECL:.*]]:2 = hlfir.declare %[[Y]] uniq_name("_QFEy") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK: %[[Z:.*]] = fir.alloca i32 <{bindc_name = "z", uniq_name = "_QFEz"}>
+!ON: %[[Z:.*]] = fir.address_of(@_QFEz) : !fir.ref<i32>
+!OFF: %[[Z:.*]] = fir.alloca i32 <{bindc_name = "z", uniq_name = "_QFEz"}>
 !CHECK: %[[Z_DECL:.*]]:2 = hlfir.declare %[[Z]] uniq_name("_QFEz") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
 !CHECK: omp.parallel private({{.*firstprivate.*}} {{.*}}#0 -> %[[PRIVATE_X:.*]], {{.*}} {{.*}}#0 -> %[[PRIVATE_Y:.*]], {{.*}} {{.*}}#0 -> %[[PRIVATE_W:.*]] : {{.*}}) {
 !CHECK: %[[PRIVATE_X_DECL:.*]]:2 = hlfir.declare %[[PRIVATE_X]] uniq_name("_QFEx") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
diff --git a/flang/test/Lower/OpenMP/main-program-globals-cost.f90 b/flang/test/Lower/OpenMP/main-program-globals-cost.f90
new file mode 100644
index 00000000000000..f91cbe05344865
--- /dev/null
+++ b/flang/test/Lower/OpenMP/main-program-globals-cost.f90
@@ -0,0 +1,126 @@
+! Check reuse of storage-selection and host-binding analysis results. Trace
+! checks require assertions; the IR checks also run without the asserts feature.
+! REQUIRES: module-independent
+! RUN: split-file %s %t
+! RUN: %if asserts %{ bbc -fopenmp -pft-test -debug-only=flang-pft %t/saved.f90 2>&1 | FileCheck %s --check-prefix=OFF %}
+! RUN: %flang_fc1 -fopenmp -fno-automatic -emit-hlfir %if asserts %{ -mllvm -debug-only=flang-pft %} %t/nested.f90 -o %t.auto > %t.auto-trace 2>&1
+! RUN: %if asserts %{ FileCheck %s --check-prefix=OFF --allow-empty < %t.auto-trace %}
+! RUN: FileCheck %s --check-prefix=SAVED < %t.auto
+! RUN: %if asserts %{ bbc -fopenmp -pft-test -debug-only=flang-pft %t/pruned.f90 2>&1 | FileCheck %s --check-prefix=PRUNED %}
+! RUN: %if asserts %{ bbc -fopenmp -pft-test -debug-only=flang-pft %t/nested.f90 2>&1 | FileCheck %s --check-prefix=EXPR %}
+! RUN: %if asserts %{ bbc -fopenmp -fno-openmp-main-program-globals -pft-test -debug-only=flang-pft %t/nested.f90 2>&1 | FileCheck %s --check-prefix=OFF %}
+! RUN: %flang_fc1 -fopenmp -emit-hlfir -module-dir %t %if asserts %{ -mllvm -debug-only=flang-lower-bridge,flang-lower-variable,flang-pft,flang-host-assoc %} %t/owner.f90 -o %t.owner > %t.trace 2>&1
+! RUN: FileCheck %s --check-prefix=IR < %t.owner
+! RUN: FileCheck %s --check-prefix=UNUSED < %t.owner
+! RUN: %if asserts %{ FileCheck %s --check-prefix=STORE < %t.trace %}
+! RUN: %if asserts %{ FileCheck %s --check-prefix=CACHE < %t.trace %}
+! RUN: %if asserts %{ FileCheck %s --check-prefix=ROOTS < %t.trace %}
+! RUN: %if asserts %{ FileCheck %s --check-prefix=TAILS < %t.trace %}
+! RUN: %flang_fc1 -fopenmp -fsave-main-program -emit-hlfir %if asserts %{ -mllvm -debug-only=flang-pft %} %t/nested.f90 -o %t.saved > %t.save-trace 2>&1
+! RUN: %if asserts %{ FileCheck %s --check-prefix=OFF --allow-empty < %t.save-trace %}
+! RUN: FileCheck %s --check-prefix=SAVED < %t.saved
+! SAVED-DAG: fir.global internal @_QFEsink : i32
+! SAVED-DAG: fir.global internal @_QFEx1 : i32
+! EXPR-COUNT-18: main-global symbol
+! EXPR-NOT: main-global symbol
+! EXPR: Program
+! EXPR-NOT: main-global symbol
+! OFF-NOT: {{^main-global }}
+! CACHE-NOT: getScopeVariableList of [sub]program|block scope {{.*}} P{{$}}
+! CACHE-NOT: getScopeVariableList of main program scope
+! CACHE: getScopeVariableList of main program scope
+! CACHE-NOT: getScopeVariableList of main program scope
+! CACHE-NOT: getScopeVariableList of [sub]program|block scope {{.*}} P{{$}}
+! STORE: [bridge - startNewFunction] worker
+! STORE-NOT: instantiateVariable: interval{{.*}} c,
+! STORE-DAG: instantiateVariable: interval{{.*}} a, global
+! STORE-DAG: instantiateVariable: interval{{.*}} alias, global
+! STORE-NOT: instantiateVariable: interval
+! STORE: [bridge - endNewFunction
+! STORE: [bridge - startNewFunction] other
+! STORE-NOT: instantiateVariable: interval{{.*}} c,
+! STORE-DAG: instantiateVariable: interval{{.*}} a, global
+! STORE-DAG: instantiateVariable: interval{{.*}} alias, global
+! STORE-NOT: instantiateVariable: interval
+! STORE: [bridge - endNewFunction
+! IR-LABEL: func.func private @_QFPworker()
+! IR-DAG: fir.address_of(@_QFEalias)
+! IR-DAG: fir.address_of(@_QFEx)
+! IR: omp.parallel
+! IR: return
+! IR-LABEL: func.func private @_QFPother()
+! IR-DAG: %[[MODULE:[^ ]+]] = fir.address_of(@_QMneededEa)
+! IR-DAG: %[[COMMON:[^ ]+]] = fir.address_of(@block_)
+! IR-DAG: %[[USED:[^ ]+]]:2 = hlfir.declare {{.*}} storage(%[[MODULE]][0]) uniq_name("_QMneededEb")
+! IR-DAG: %[[SHARED:[^ ]+]]:2 = hlfir.declare {{.*}} storage(%[[COMMON]][0]) uniq_name("_QFEshared")
+! IR: omp.parallel
+! IR-DAG: fir.load %[[USED]]#0 :
+! IR-DAG: fir.load %[[SHARED]]#0 :
+! IR: return
+! UNUSED-LABEL: func.func private @_QFPworker()
+! UNUSED-NOT: fir.address_of(@_QM{{(neededEc|tail0E)}}
+! UNUSED: return
+! UNUSED-LABEL: func.func private @_QFPother()
+!--- nested.f90
+! Visit nested operands once; SAVE policies and the opt-out bypass selection.
+program p
+integer :: x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,sink
+!$omp parallel
+if (x1>0) sink=x1+(x2+(x3+(x4+(x5+(x6+(x7+(x8+( &
+     x9+(x10+(x11+(x12+(x13+(x14+(x15+x16))))))))))))))
+!$omp end parallel
+end program
+!--- owner.f90
+! Reuse the MAIN variable list and shared USE tails. Bind only the required
+! globals and EQUIVALENCE/COMMON stores in each internal procedure.
+module needed
+  integer :: a,b,c,d; equivalence(a,b),(c,d)
+end module
+module tail0; integer :: v; end module; module tail1; use tail0, only: v; end module
+module tail2; use tail1, only: v; end module; module tail3; use tail2, only: v; end module
+program p
+  use needed, only: unused=>a,used=>b
+  use tail3, only: a1=>v,a2=>v,a3=>v,a4=>v
+  integer,save :: backing(2),alias; equivalence(backing(1),alias)
+  integer :: x,shared; common /block/ shared
+  alias=3; x=7; used=11; shared=13; call worker(); call other()
+contains
+  subroutine worker()
+!$omp parallel num_threads(1)
+    print *,x,alias
+!$omp end parallel
+  end subroutine; subroutine other()
+!$omp parallel num_threads(1)
+    print *,x,alias,used,shared
+!$omp end parallel
+  end subroutine; end program
+! Four MAIN entities, one module object and six OpenMP proxies are resolved once.
+! ROOTS-COUNT-11: main-global association
+! ROOTS-NOT: main-global association
+! Four renames share three USE edges; two needed aliases add two links.
+! TAILS-COUNT-9: main-global binding association
+! TAILS-NOT: main-global binding association
+! PRUNED-NOT: main-global reference: image
+! PRUNED: main-global reference: direct
+! PRUNED-NOT: main-global reference: image
+! PRUNED: Program
+! PRUNED-NOT: main-global reference: image
+!--- pruned.f90
+! Do not select the coarray image selector; direct remains a candidate.
+program p; type t(k)
+    integer,kind :: k=4; integer :: payload=1
+  end type; type(t),parameter :: c=t()
+  integer :: direct,image,co[*]
+!$omp parallel
+  print *,direct,co[image],c
+!$omp end parallel
+end program
+!--- saved.f90
+! A bare SAVE makes the new storage-selection analysis unnecessary.
+program p
+  integer :: x
+  save
+!$omp parallel
+  x=1
+!$omp end parallel
+end program
diff --git a/flang/test/Lower/OpenMP/main-program-globals-fallback.f90 b/flang/test/Lower/OpenMP/main-program-globals-fallback.f90
new file mode 100644
index 00000000000000..7e4d0b40651750
--- /dev/null
+++ b/flang/test/Lower/OpenMP/main-program-globals-fallback.f90
@@ -0,0 +1,115 @@
+! REQUIRES: module-independent
+! RUN: split-file %s %t
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp -DREGION '-DDIRECTIVE=target map(tofrom:x)' '-DEND_DIRECTIVE=end target' -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp -DREGION '-DDIRECTIVE=target parallel map(tofrom:x)' '-DEND_DIRECTIVE=end target parallel' -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp -DREGION '-DDIRECTIVE=target data map(tofrom:x)' '-DEND_DIRECTIVE=end target data' -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp '-DDIRECTIVE=target enter data map(to:x)' -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp '-DDIRECTIVE=target exit data map(delete:x)' -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp '-DDIRECTIVE=target update to(x)' -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -cpp '-DDIRECTIVE=declare target' -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fopenmp-version=52 -emit-hlfir %t/meta.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -cpp -DDIRECTIVE=barrier -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=GLOBAL
+! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/external.f90 -o - | FileCheck %s --check-prefixes=EXTERNAL,GLOBAL
+! RUN: %flang_fc1 -fopenmp -fdo-concurrent-to-openmp=device -fopenmp-targets=nvptx64-nvidia-cuda -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=DEVICE --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fdo-concurrent-to-openmp=device -fopenmp-is-target-device -fopenmp-targets=nvptx64-nvidia-cuda -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=DEVICE --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: bbc -fopenmp -fdo-concurrent-to-openmp=device -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=DEVICE --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -fdo-concurrent-to-openmp=host -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=HOST
+! KEEP: fir.alloca i32 {{.*}}bindc_name = "x"
+! EXTERNAL-LABEL: func.func @_QQmain()
+! EXTERNAL: fir.address_of(@_QFEx)
+! EXTERNAL-LABEL: func.func @_QPoutside()
+! EXTERNAL: omp.target_data
+! GLOBAL: fir.global internal @_QFEx : i32
+!--- internal.f90
+! Each CPP offload variant independently preserves MAIN storage when the
+! directive is in an internal procedure. BARRIER is the host-only control.
+program p
+  integer :: x
+!$omp parallel
+  print *,x
+!$omp end parallel
+  call worker()
+contains
+  subroutine worker()
+!$omp DIRECTIVE
+#ifdef REGION
+    x=1
+!$omp END_DIRECTIVE
+#endif
+  end subroutine
+end program
+!--- meta.f90
+! Even a metadirective selecting NOTHING currently preserves MAIN storage.
+program p
+  integer :: x
+!$omp parallel
+  print *,x
+!$omp end parallel
+!$omp metadirective when(user={condition(.true.)}: nothing)
+  x=1
+end program
+!--- external.f90
+! Offload in another external procedure or an interface body must not disable
+! global storage selection in MAIN.
+program p
+  integer :: x
+  interface
+    subroutine declared()
+!$omp declare target
+    end subroutine
+  end interface
+!$omp parallel
+  print *,x
+!$omp end parallel
+end program
+subroutine outside()
+  integer :: a(4)
+!$omp target data map(tofrom:a)
+  a=1
+!$omp end target data
+end subroutine
+! Device DO CONCURRENT keeps the host tuple and maps it into the target. Host
+! mapping still permits globals and removes the unused tuple argument from f.
+! DEVICE-LABEL: func.func @_QQmain()
+! DEVICE: fir.alloca i32 {{.*}}bindc_name = "x"
+! DEVICE: %[[HOST_TUPLE:[^ ]+]] = fir.alloca tuple<!fir.ref<i32>>
+! DEVICE: %[[TUPLE_MAP:[^ ]+]] = omp.map.info var_ptr(%[[HOST_TUPLE]] : !fir.ref<tuple<!fir.ref<i32>>>
+! DEVICE: omp.target
+! DEVICE-SAME: map_entries({{.*}}%[[TUPLE_MAP]] -> %[[MAPPED_TUPLE:[^, ]+]]{{.*}} : !fir.ref<index>, !fir.ref<index>, !fir.ref<index>, !fir.ref<i32>, !fir.ref<tuple<!fir.ref<i32>>>, !fir.ref<!fir.array<4xi32>>, !fir.ref<index>)
+! DEVICE: %[[TUPLE:[^ ]+]] = fir.declare %[[MAPPED_TUPLE]] uniq_name("") : (!fir.ref<tuple<!fir.ref<i32>>>)
+! DEVICE: fir.call @_QFPf({{[^,]*}}, %[[TUPLE]])
+! DEVICE-LABEL: func.func private @_QFPf(
+! DEVICE-SAME: !fir.ref<tuple<!fir.ref<i32>>>
+! HOST-LABEL: func.func @_QQmain()
+! HOST: fir.address_of(@_QFEx)
+! HOST: fir.call @_QFPf({{[^,]*}})
+! HOST-LABEL: func.func private @_QFPf(
+! HOST-SAME: !fir.ref<i32> {{.*}}) -> i32
+! HOST: fir.global internal @_QFEx
+!--- generated-target.f90
+program p
+  implicit none
+  integer :: x,i,a(4)
+  x=7
+  a=0
+!$omp parallel num_threads(1)
+  print *,x
+!$omp end parallel
+  do concurrent(i=1:4)
+    block
+      integer :: v
+      v=f(i)
+      a(i)=v
+    end block
+  end do
+  if(any(a/=[2,3,4,5])) stop 1
+  call host_use()
+contains
+  pure integer function f(j)
+    integer,intent(in) :: j
+    f=j+1
+  end function
+  subroutine host_use()
+    print *,x
+  end subroutine
+end program
diff --git a/flang/test/Lower/OpenMP/main-program-globals.f90 b/flang/test/Lower/OpenMP/main-program-globals.f90
new file mode 100644
index 00000000000000..5216061533fd87
--- /dev/null
+++ b/flang/test/Lower/OpenMP/main-program-globals.f90
@@ -0,0 +1,367 @@
+! REQUIRES: module-independent
+! RUN: split-file %s %t
+! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/miniweather.f90 -o %t.on
+! RUN: FileCheck %s --check-prefix=ON < %t.on
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.alloca tuple' < %t.on
+! RUN: bbc -fopenmp -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=ON
+! RUN: bbc -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
+! RUN: %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
+! RUN: %flang_fc1 -fopenmp -fopenmp-is-target-device -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE'
+! RUN: %flang_fc1 -fopenmp-simd -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE'
+! RUN: %flang_fc1 -fopenmp -fopenacc -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
+! RUN: %flang_fc1 -fopenmp -x cuda -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
+! RUN: bbc -fopenmp -fopenacc -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
+! RUN: bbc -fopenmp -fcuda -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
+! RUN: %if aarch64-registered-target %{ %flang_fc1 -triple aarch64-unknown-linux-gnu -O3 -ffast-math -fopenmp -emit-llvm %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OPT %}
+! RUN: %if asserts %{ %flang_fc1 -fopenmp -fdebug-dump-pft -mllvm -debug-only=flang-pft %t/miniweather.f90 2>&1 | FileCheck %s --check-prefix=PFT_ON %}
+! RUN: %if asserts %{ %flang_fc1 -fopenmp -fno-openmp-main-program-globals -fdebug-dump-pft -mllvm -debug-only=flang-pft %t/miniweather.f90 2>&1 | FileCheck %s --check-prefix=PFT_OFF %}
+! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/objects.f90 -o %t.objects
+! RUN: FileCheck %s --check-prefix=OBJECTS < %t.objects
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(array|matrix|storage|i|j|ap|ptr|record|private_value|listed|eq1|eq2|x|y|serial_only|x_private_i32|y_firstprivate_i32|allocated|address|pointee|actual|element|extent|rhs|field)[[:space:]]}}' < %t.objects
+! RUN: %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/objects.f90 -o %t.off
+! RUN: FileCheck %s --check-prefix=BOUNDARY < %t.off
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(text|if_condition|if_action|where_value|forall_value)[[:space:]]}}' < %t.off
+! RUN: FileCheck %s --check-prefix=EXPRESSIONS < %t.objects
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(actual|element|extent|rhs|field)[[:space:]]}}' < %t.objects
+! RUN: %flang_fc1 -emit-hlfir %t/objects.f90 -o %t.serial
+! RUN: FileCheck %s --check-prefix=BOUNDARY < %t.serial
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(integer_value|real_value|complex_value|logical_value|text|if_condition|if_action|where_value|forall_value|direct)[[:space:]]}}' < %t.serial
+! RUN: %flang_fc1 -fopenmp -mllvm -strict-fir-volatile-verifier -emit-hlfir %t/mixed.f90 -o %t.mixed
+! RUN: FileCheck %s --check-prefix=MIXED < %t.mixed
+! RUN: FileCheck %s --check-prefix=PROPERTIES < %t.mixed
+! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/callbacks.f90 -o %t.callbacks
+! RUN: FileCheck %s --check-prefix=CALLBACK < %t.callbacks
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.alloca tuple' --implicit-check-not='fir.host_assoc' < %t.callbacks
+! RUN: %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/callbacks.f90 -o - | FileCheck %s --check-prefix=BOX_OFF --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: %flang_fc1 -fopenmp -emit-fir %t/callbacks.f90 -o - | fir-opt --flang-licm | FileCheck %s --check-prefix=LICM
+! RUN: %flang_fc1 -fopenmp -fcoarray -emit-hlfir %t/coarray.f90 -o %t.on
+! RUN: FileCheck %s --check-prefix=COARRAY < %t.on
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFEa_coarray_handle{{[[:space:]]}}' < %t.on
+! RUN: %flang_fc1 -fopenmp -fcoarray -emit-llvm %t/coarray.f90 -o - | FileCheck %s --check-prefix=LLVM
+! MODULE: module
+
+! Check globals and removal of the host tuple for MAIN scalars referenced in an
+! internal procedure's PARALLEL region. Disabled modes retain legacy storage;
+! OPT checks reciprocal computation before the outlined loop.
+! ON-LABEL: func.func @_QQmain()
+! ON: fir.call @_QFPworker()
+! ON-LABEL: func.func private @_QFPworker()
+! ON-DAG: fir.address_of(@_QFEdx)
+! ON-DAG: fir.address_of(@_QFEdz)
+! ON: omp.parallel
+! ON-DAG: fir.global internal @_QFEdx : f64
+! ON-DAG: fir.global internal @_QFEdz : f64
+! OFF-LABEL: func.func @_QQmain()
+! OFF-DAG: fir.alloca f64 {{.*}}bindc_name = "dx"
+! OFF-DAG: fir.alloca f64 {{.*}}bindc_name = "dz"
+! OFF: fir.alloca tuple
+! OFF-LABEL: func.func private @_QFPworker(
+! OFF-SAME: fir.host_assoc
+! OPT-LABEL: define internal void @_QFPworker..omp_par(
+! OPT: omp_collapsed.body.lr.ph:
+! OPT-DAG: %[[DX:[^ ]+]] = load double, ptr @_QFEdx
+! OPT-DAG: %[[DZ:[^ ]+]] = load double, ptr @_QFEdz
+! OPT-DAG: %[[RX:[^ ]+]] = fdiv fast double 1.000000e+00, %[[DX]]
+! OPT-DAG: %[[RZ:[^ ]+]] = fdiv fast double 1.000000e+00, %[[DZ]]
+! OPT: br label %[[BODY:omp_collapsed.body]]
+! OPT: [[BODY]]:
+! OPT-NOT: fdiv
+! OPT: fmul fast double {{.*}}, %[[RX]]
+! OPT-NOT: fdiv
+! OPT: fmul fast double {{.*}}, %[[RZ]]
+! OPT-NOT: fdiv
+! OPT: br i1
+! PFT_ON: {{^main-global symbol$}}
+! PFT_OFF-NOT: {{^main-global }}
+! PFT_OFF: Program
+! PFT_OFF-NOT: {{^main-global }}
+!--- miniweather.f90
+program p
+integer :: nx,nz,i_beg,k_beg
+real(8) :: dx,dz
+nx=4
+nz=3
+i_beg=1
+k_beg=1
+read *,dx
+read *,dz
+call worker()
+contains
+subroutine worker()
+integer :: i,k
+real(8) :: a(4,3)
+!$omp parallel do simd collapse(2) private(i,k)
+do k=1,nz
+ do i=1,nx
+  a(i,k)=(i+i_beg)/dx+(k+k_beg)/dz
+ end do
+end do
+!$omp end parallel do simd
+print *,a
+end subroutine
+end program
+
+! Select intrinsic scalars in nested regions and statement expressions while
+! preserving excluded objects and private copies. Calls and constructors do not
+! seed selection. The 32/33-byte CHARACTER cases distinguish new selection from
+! existing static storage. serial_only checks collection stops at END PARALLEL.
+! OBJECTS-LABEL: func.func @_QQmain()
+! OBJECTS: fir.alloca i32 {{.*}}bindc_name = "serial_only"
+! OBJECTS: omp.parallel
+! OBJECTS-DAG: fir.global internal @_QFEif_condition : i32
+! OBJECTS-DAG: fir.global internal @_QFEif_action : i32
+! OBJECTS-DAG: fir.global internal @_QFEwhere_value : i32
+! OBJECTS-DAG: fir.global internal @_QFEforall_value : i32
+! OBJECTS-DAG: fir.global internal @_QFEinteger_value : i64
+! OBJECTS-DAG: fir.global internal @_QFEreal_value : f64
+! OBJECTS-DAG: fir.global internal @_QFEcomplex_value : complex<f64>
+! OBJECTS-DAG: fir.global internal @_QFElogical_value : !fir.logical<4>
+! OBJECTS-DAG: fir.global internal @_QFEtext : !fir.char<1,32>
+! OBJECTS-DAG: fir.global internal @_QFElarge : !fir.char<1,33>
+! OBJECTS-DAG: fir.global internal @_QFEsaved : i32
+! BOUNDARY-DAG: fir.alloca i32 {{.*}}bindc_name = "direct"
+! BOUNDARY-DAG: fir.alloca !fir.char<1,32> {{.*}}bindc_name = "text"
+! BOUNDARY-DAG: fir.global internal @_QFElarge : !fir.char<1,33>
+! EXPRESSIONS-LABEL: func.func @_QQmain()
+! EXPRESSIONS: omp.parallel
+! EXPRESSIONS: fir.call @_QFPsink(
+! EXPRESSIONS: fir.call @_QFPassign(
+! EXPRESSIONS: fir.global internal @_QFEdirect : i32
+! BOUNDARY-DAG: fir.global internal @_QFEsaved : i32
+!--- objects.f90
+program p
+  integer(8) :: integer_value
+  real(8) :: real_value
+  complex(8) :: complex_value
+  logical :: logical_value
+  integer, save :: saved
+  character(32) :: text
+  character(33) :: large
+  type t
+    integer :: n
+  end type
+  type(t) :: record,value
+  interface assignment(=)
+    procedure assign
+  end interface
+  integer :: direct,actual,element,extent,rhs,field
+  integer :: array(2),matrix(2,2),private_value,listed,eq1,eq2,x,y
+  integer :: if_condition,if_action,serial_only,where_value,forall_value,i,j
+  integer :: x_private_i32,y_firstprivate_i32,allocated,pointee,storage(1)
+  integer(8) :: address
+  pointer(address,pointee)
+!$omp allocate(allocated)
+  integer, allocatable :: ap
+  integer, pointer :: ptr
+  equivalence(eq1,eq2)
+  namelist /group/ listed
+  direct=1; actual=2; element=3
+  extent=4; rhs=5; field=6
+  saved=1; storage=1; address=loc(storage); if_condition=1
+  array=1; matrix=0; where_value=2; forall_value=3
+!$omp parallel
+  print *,array,ap,ptr,record,private_value,listed,eq1,eq2
+  print *,x_private_i32,y_firstprivate_i32,allocated,address,pointee,saved
+  print *,direct,abs(actual),[element],[character(extent)::]
+  print *,t(field)
+  call sink(actual)
+  value=rhs
+  where(array>0)
+    where(array<3) array=where_value
+  end where
+  forall(i=1:2)
+    forall(j=1:2) matrix(i,j)=forall_value
+  end forall
+!$omp single
+!$omp parallel if(.false.)
+  print *,integer_value,real_value,complex_value,logical_value,text,large
+!$omp end parallel
+!$omp end single
+  if (if_condition>0) if_action=1
+!$omp end parallel
+!$omp parallel private(private_value,x) firstprivate(y)
+  private_value=x+y
+!$omp end parallel
+  serial_only=1
+contains
+  subroutine sink(n)
+    integer :: n
+    print *,n
+  end subroutine
+  subroutine assign(to,from)
+    type(t),intent(out) :: to
+    integer,intent(in) :: from
+    to%n=from
+  end subroutine
+end program
+
+! Keep the array in the host tuple while promoted scalars use globals. Preserve
+! EQUIVALENCE offsets and DATA initialization, threadprivate bindings, and
+! VOLATILE/ASYNCHRONOUS attributes.
+! MIXED-LABEL: func.func @_QQmain()
+! MIXED: %[[A:[^: ]+]]:2 = hlfir.declare {{.*}}uniq_name("_QFEa")
+! MIXED: %[[T:[^ ]+]] = fir.alloca tuple<!fir.box<!fir.array<2xi32>>>
+! MIXED: %[[S:[^ ]+]] = fir.coordinate_of %[[T]],
+! MIXED: %[[B:[^ ]+]] = fir.embox %[[A]]#0
+! MIXED: fir.store %[[B]] to %[[S]]
+! MIXED: fir.call @_QFPworker(%[[T]])
+! MIXED-LABEL: func.func private @_QFPworker(
+! MIXED-SAME: %[[H:[^: ]+]]:
+! MIXED: %[[G:[^ ]+]] = fir.address_of(@_QFEalias)
+! MIXED: %[[BYTES:[^ ]+]] = fir.convert %[[G]] : (!fir.ref<!fir.array<2xi32>>) -> !fir.ref<!fir.array<8xi8>>
+! MIXED: %[[FOUR:[^ ]+]] = arith.constant 4 : index
+! MIXED: %[[OFFSET:[^ ]+]] = fir.coordinate_of %[[BYTES]], %[[FOUR]]
+! MIXED: %[[PTR:[^ ]+]] = fir.convert %[[OFFSET]]
+! MIXED: %[[ALIAS:[^: ]+]]:2 = hlfir.declare %[[PTR]] storage(%[[BYTES]][4]) uniq_name("_QFEalias")
+! MIXED: %[[TLS:[^: ]+]]:2 = hlfir.declare {{.*}}uniq_name("_QFEtls")
+! MIXED: %[[ADDR:[^ ]+]] = omp.threadprivate %[[TLS]]#0
+! MIXED: %[[LOCAL:[^: ]+]]:2 = hlfir.declare %[[ADDR]] uniq_name("_QFEtls")
+! MIXED: %[[C:[^ ]+]] = fir.coordinate_of %[[H]],
+! MIXED: %[[BOX:[^ ]+]] = fir.load %[[C]]
+! MIXED: %[[ARRAY:[^ ]+]] = fir.box_addr %[[BOX]]
+! MIXED: hlfir.declare %[[ARRAY]]({{.*}}) uniq_name("_QFEa") fortran_attrs<host_assoc>
+! MIXED: fir.load %[[LOCAL]]#0
+! MIXED: hlfir.assign {{.*}} to %[[LOCAL]]#0
+! MIXED: omp.parallel
+! MIXED: %[[THREAD:[^ ]+]] = omp.threadprivate %[[TLS]]#0
+! MIXED: %[[PRIVATE:[^: ]+]]:2 = hlfir.declare %[[THREAD]] uniq_name("_QFEtls")
+! MIXED: fir.load %[[ALIAS]]#0
+! MIXED: fir.load %[[PRIVATE]]#0
+! MIXED: fir.global internal @_QFEalias
+! MIXED: %[[INIT:[^ ]+]] = fir.undefined !fir.array<2xi32>
+! MIXED: %[[ELEVEN:[^ ]+]] = arith.constant 11 : i32
+! MIXED: %[[FIRST:[^ ]+]] = fir.insert_value %[[INIT]], %[[ELEVEN]], [0 : index]
+! MIXED: %[[TWENTYTWO:[^ ]+]] = arith.constant 22 : i32
+! MIXED: %[[SECOND:[^ ]+]] = fir.insert_value %[[FIRST]], %[[TWENTYTWO]], [1 : index]
+! MIXED: fir.has_value %[[SECOND]]
+! PROPERTIES-LABEL: func.func private @_QFPworker(
+! PROPERTIES-DAG: hlfir.declare {{.*}}uniq_name("_QFEav") fortran_attrs<asynchronous, internal_assoc>
+! PROPERTIES-DAG: hlfir.declare {{.*}}uniq_name("_QFEv") fortran_attrs<volatile, internal_assoc>
+!--- mixed.f90
+program p
+  integer :: x,a(2)
+  integer,volatile :: v
+  integer,asynchronous :: av
+  integer, save :: backing(2),alias,tls
+  equivalence(backing(2),alias)
+  data backing /11,22/
+!$omp threadprivate(tls)
+  x=7; v=1; av=2
+  a=1; alias=2; tls=3
+  call worker()
+contains
+  subroutine worker()
+    tls=tls+1
+!$omp parallel
+    print *,x,a,alias,tls,v,av
+!$omp end parallel
+  end subroutine
+end program
+
+! Removing the last host capture also updates procedure boxes and direct and
+! indirect calls. Loads must stay in loops that call a modifying sibling.
+! CALLBACK-LABEL: func.func @_QQmain()
+! CALLBACK: %[[P:[^: ]+]]:2 = hlfir.declare {{.*}}uniq_name("_QFEcallback")
+! CALLBACK: %[[WORKER:[^ ]+]] = fir.address_of(@_QFPworker) : () -> ()
+! CALLBACK: %[[BOX:[^ ]+]] = fir.emboxproc %[[WORKER]] :
+! CALLBACK: fir.store %[[BOX]] to %[[P]]#0
+! CALLBACK: %[[ACTUAL:[^ ]+]] = fir.address_of(@_QFPworker)
+! CALLBACK: %[[ARG:[^ ]+]] = fir.emboxproc %[[ACTUAL]] :
+! CALLBACK: fir.call @_QPinvoke(%[[ARG]])
+! CALLBACK: omp.parallel
+! CALLBACK: fir.call @_QFPworker()
+! CALLBACK: %[[LOADED:[^ ]+]] = fir.load %[[P]]#0
+! CALLBACK: %[[CODE:[^ ]+]] = fir.box_addr %[[LOADED]]
+! CALLBACK: fir.call %[[CODE]]()
+! BOX_OFF: %[[HOST:[^ ]+]] = fir.alloca tuple<!fir.ref<i32>>
+! BOX_OFF: %[[PTR:[^ ]+]] = fir.address_of(@_QFPworker)
+! BOX_OFF: %[[BOX:[^ ]+]] = fir.emboxproc %[[PTR]], %[[HOST]]
+! BOX_OFF: fir.store %[[BOX]]
+! BOX_OFF: %[[ACTUAL:[^ ]+]] = fir.address_of(@_QFPworker)
+! BOX_OFF: %[[ARG:[^ ]+]] = fir.emboxproc %[[ACTUAL]], %[[HOST]]
+! BOX_OFF: fir.call @_QPinvoke(%[[ARG]])
+! BOX_OFF: func.func private @_QFPworker({{.*}}fir.host_assoc
+! LICM-LABEL: func.func @_QQmain()
+! LICM: %[[MAIN_X:[^ ]+]] = fir.declare {{.*}}uniq_name("_QFEx") fortran_attrs<internal_assoc>
+! LICM: fir.do_loop
+! LICM-NOT: {{^ *[}]}}
+! LICM: fir.call @_QFPbump(
+! LICM-NOT: {{^ *[}]}}
+! LICM: fir.load %[[MAIN_X]] :
+! LICM: {{^ *[}]}}
+! LICM-LABEL: func.func private @_QFPaccumulate(
+! LICM: %[[X:[^ ]+]] = fir.declare {{.*}}uniq_name("_QFEx") fortran_attrs<internal_assoc>
+! LICM: fir.do_loop
+! LICM-NOT: {{^ *[}]}}
+! LICM: fir.call @_QFPbump(
+! LICM-NOT: {{^ *[}]}}
+! LICM: fir.load %[[X]] :
+! LICM: {{^ *[}]}}
+!--- callbacks.f90
+program p
+  interface
+    subroutine invoke(proc)
+      external :: proc
+    end subroutine
+  end interface
+  procedure(), pointer :: callback
+  integer :: x,i,s
+  x=7
+  callback=>worker
+  call invoke(worker)
+!$omp parallel num_threads(1)
+  print *,x
+  call worker()
+  call callback()
+!$omp end parallel
+  x=0
+  call accumulate()
+  x=0
+  s=0
+  do i=1,3
+    call bump()
+    s=s+x
+  end do
+  print *,s
+contains
+  subroutine worker()
+    print *,x
+  end subroutine
+  subroutine accumulate()
+    integer :: i,s
+    s=0
+    do i=1,3
+      call bump()
+      s=s+x
+    end do
+    print *,s
+  end subroutine
+  subroutine bump()
+    x=x+1
+  end subroutine
+end program
+subroutine invoke(proc)
+  external :: proc
+  call proc()
+end subroutine
+
+! Preserve the scalar whose name would collide with a coarray handle; a longer
+! suffix and an ordinary scalar remain eligible for global storage.
+! COARRAY: fir.alloca i32 {{.*}}bindc_name = "a_coarray_handle"
+! COARRAY-DAG: fir.global internal @_QFEa_coarray_handle_extra : i32
+! COARRAY-DAG: fir.global internal @_QFEordinary : i32
+! LLVM: @_QFEa_coarray_handle = linkonce global %{{[^ ]*prif_coarray_handle_type}}
+! LLVM: call void @llvm.memmove{{.*}}(ptr @_QFEa_coarray_handle, ptr {{.*}}, i64 8, i1 false)
+!--- coarray.f90
+program p
+  integer,allocatable :: a[:]
+  integer :: a_coarray_handle,a_coarray_handle_extra,ordinary
+  a_coarray_handle=17
+  a_coarray_handle_extra=19
+  ordinary=23
+  allocate(a[*])
+!$omp parallel num_threads(1)
+  print *,a_coarray_handle,a_coarray_handle_extra,ordinary
+!$omp end parallel
+  deallocate(a)
+end program
diff --git a/flang/test/Lower/volatile-openmp1.f90 b/flang/test/Lower/volatile-openmp1.f90
index 8852437cc51614..f7b961b0694757 100644
--- a/flang/test/Lower/volatile-openmp1.f90
+++ b/flang/test/Lower/volatile-openmp1.f90
@@ -1,4 +1,5 @@
-! RUN: bbc --strict-fir-volatile-verifier -fopenmp %s -o - | FileCheck %s
+! RUN: bbc --strict-fir-volatile-verifier -fopenmp -fno-openmp-main-program-globals %s -o - | FileCheck %s --check-prefixes=CHECK,OFF --implicit-check-not='fir.global internal @_QFEn{{[[:space:]]}}'
+! RUN: bbc --strict-fir-volatile-verifier -fopenmp %s -o - | FileCheck %s --check-prefixes=CHECK,ON
 program main
 implicit none
 integer,volatile::a
@@ -23,7 +24,8 @@ program main
 ! CHECK:           %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_5]] uniq_name("_QFEa") fortran_attrs<volatile> : (!fir.ref<i32, volatile>) -> (!fir.ref<i32, volatile>, !fir.ref<i32, volatile>)
 ! CHECK:           %[[VAL_7:.*]] = fir.alloca i32 <{bindc_name = "i", uniq_name = "_QFEi"}>
 ! CHECK:           %[[VAL_8:.*]]:2 = hlfir.declare %[[VAL_7]] uniq_name("_QFEi") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-! CHECK:           %[[VAL_9:.*]] = fir.alloca i32 <{bindc_name = "n", uniq_name = "_QFEn"}>
+! ON:              %[[VAL_9:.*]] = fir.address_of(@_QFEn) : !fir.ref<i32>
+! OFF:             %[[VAL_9:.*]] = fir.alloca i32 <{bindc_name = "n", uniq_name = "_QFEn"}>
 ! CHECK:           %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_9]] uniq_name("_QFEn") : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
 ! CHECK:           hlfir.assign %[[VAL_2]] to %[[VAL_6]]#0 : i32, !fir.ref<i32, volatile>
 ! CHECK:           hlfir.assign %[[VAL_1]] to %[[VAL_10]]#0 : i32, !fir.ref<i32>
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index c7904ba3560c28..9bbdb4306012f8 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -148,6 +148,12 @@ static llvm::cl::opt<bool> enableOpenMP("fopenmp",
                                         llvm::cl::desc("enable openmp"),
                                         llvm::cl::init(false));
 
+static llvm::cl::opt<bool> disableOpenMPMainProgramGlobals(
+    "fno-openmp-main-program-globals",
+    llvm::cl::desc("Disable static storage selection for small main program "
+                   "scalars used by explicit host OpenMP PARALLEL regions"),
+    llvm::cl::init(false));
+
 static llvm::cl::opt<bool>
     enableOpenMPDevice("fopenmp-is-target-device",
                        llvm::cl::desc("enable openmp device compilation"),
@@ -499,9 +505,13 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
     return mlir::success();
   }
 
+  // Generated device targets require the same capture layout on host and
+  // device.
+  Fortran::lower::LoweringOptions loweringOptions{};
+  loweringOptions.setOpenMPMainProgramGlobals(
+      !disableOpenMPMainProgramGlobals &&
+      enableDoConcurrentToOpenMPConversion.getValue() != "device");
   if (pftDumpTest) {
-    // Use default lowering options for PFT dump test
-    Fortran::lower::LoweringOptions loweringOptions{};
     if (auto ast = Fortran::lower::createPFT(parseTree, semanticsContext,
                                              loweringOptions)) {
       Fortran::lower::dumpPFT(llvm::outs(), *ast);
@@ -521,8 +531,6 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
   fir::KindMapping kindMap(
       &ctx, llvm::ArrayRef<fir::KindTy>{fir::fromDefaultKinds(defKinds)});
   std::string targetTriple = targetMachine.getTargetTriple().normalize();
-  // Use default lowering options for bbc.
-  Fortran::lower::LoweringOptions loweringOptions{};
   loweringOptions.setNoPPCNativeVecElemOrder(enableNoPPCNativeVecElemOrder);
   loweringOptions.setIntegerWrapAround(integerWrapAround);
   loweringOptions.setInitGlobalZero(initGlobalZero);
diff --git a/flang/unittests/Frontend/CompilerInstanceTest.cpp b/flang/unittests/Frontend/CompilerInstanceTest.cpp
index 5f95d2f3edeb7f..ba31bf79df8bb4 100644
--- a/flang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/flang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -118,4 +118,36 @@ TEST(CompilerInstance,
               clang::options::OPT_fno_openacc_default_none_scalars_strict)
           .str());
 }
+
+TEST(CompilerInvocation, MainProgramGlobalsRespectLateDeviceMapping) {
+  CompilerInstance compInst;
+  compInst.createDiagnostics();
+  auto &invocation = compInst.getInvocation();
+  const char *args[] = {"-fopenmp"};
+  ASSERT_TRUE(CompilerInvocation::createFromArgs(
+      invocation, args, compInst.getDiagnostics()));
+
+  using Mapping = CodeGenOptions::DoConcurrentMappingKind;
+  invocation.getCodeGenOpts().setDoConcurrentMapping(Mapping::DCMK_Host);
+  invocation.setLoweringOptions();
+  EXPECT_TRUE(invocation.getLoweringOpts().getOpenMPMainProgramGlobals());
+
+  invocation.getCodeGenOpts().setDoConcurrentMapping(Mapping::DCMK_Device);
+  invocation.setLoweringOptions();
+  EXPECT_FALSE(invocation.getLoweringOpts().getOpenMPMainProgramGlobals());
+}
+
+TEST(CompilerInvocation, MainProgramGlobalsPreserveExplicitDisable) {
+  CompilerInstance compInst;
+  compInst.createDiagnostics();
+  auto &invocation = compInst.getInvocation();
+  const char *args[] = {"-fopenmp", "-fno-openmp-main-program-globals"};
+  ASSERT_TRUE(CompilerInvocation::createFromArgs(
+      invocation, args, compInst.getDiagnostics()));
+
+  invocation.getCodeGenOpts().setDoConcurrentMapping(
+      CodeGenOptions::DoConcurrentMappingKind::DCMK_Host);
+  invocation.setLoweringOptions();
+  EXPECT_FALSE(invocation.getLoweringOpts().getOpenMPMainProgramGlobals());
+}
 } // namespace

>From 08a6f2c4eba10ff15c2ef9a12a37dca5df061b7a Mon Sep 17 00:00:00 2001
From: Sergey Shcherbinin <sscherbinin at nvidia.com>
Date: Thu, 29 Oct 2026 15:46:40 +0400
Subject: [PATCH 2/4] Comment removed

Co-authored-by: Tarun Prabhu <tarunprabhu at gmail.com>
---
 flang/include/flang/Semantics/symbol.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index b2cd7dc519a977..5d25ae6689d25f 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -974,8 +974,6 @@ class Symbol {
       OmpIfSpecified, OmpNone, OmpPreDetermined, OmpExplicit, OmpImplicit,
       OmpDependObject, OmpInclusiveScan, OmpExclusiveScan, OmpInScanReduction,
       OmpUniform, OmpAssumesNoOpenmp,
-      // Main-program storage needed by a host OpenMP outlined region. Set by
-      // lowering after semantic analysis, without changing data-sharing rules.
       OmpMainGlobal);
   using Flags = common::EnumSet<Flag, Flag_enumSize>;
 

>From cd0c3b4177e5a6dbcdcd3b9c4046c70e381af762 Mon Sep 17 00:00:00 2001
From: Sergey Shcherbinin <sscherbinin at nvidia.com>
Date: Thu, 29 Oct 2026 17:16:39 +0400
Subject: [PATCH 3/4] [flang][OpenMP] Avoid LINEAR collisions in MAIN
 globalization

Leave MAIN scalar names containing _linear_ on their existing storage
path to prevent collisions with generated OpenMP LINEAR privatizers.

Return cached host-variable lists as optional ArrayRef views. This keeps
ownership in the PFT cache and removes the container type from the
HostAssociations interface without copying records.
---
 flang/include/flang/Lower/HostAssociations.h    | 15 +++++++++------
 flang/include/flang/Semantics/symbol.h          |  3 +--
 flang/lib/Lower/Bridge.cpp                      |  4 ++--
 flang/lib/Lower/HostAssociations.cpp            |  4 ++--
 flang/lib/Lower/PFTBuilder.cpp                  |  7 ++++---
 .../test/Lower/OpenMP/main-program-globals.f90  | 17 +++++++++++++----
 6 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/flang/include/flang/Lower/HostAssociations.h b/flang/include/flang/Lower/HostAssociations.h
index 73e644160368a0..d865a719b3f243 100644
--- a/flang/include/flang/Lower/HostAssociations.h
+++ b/flang/include/flang/Lower/HostAssociations.h
@@ -10,10 +10,11 @@
 #define FORTRAN_LOWER_HOSTASSOCIATIONS_H
 
 #include "mlir/IR/Types.h"
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallVector.h"
-#include <vector>
+#include <optional>
 
 namespace Fortran {
 namespace semantics {
@@ -46,9 +47,11 @@ class HostAssociations {
   void addSymbolsToBind(
       const llvm::SetVector<const Fortran::semantics::Symbol *> &symbols,
       const Fortran::semantics::Scope &hostScope,
-      llvm::function_ref<const std::vector<pft::Variable> &()> getVariables);
+      llvm::function_ref<llvm::ArrayRef<pft::Variable>()> getVariables);
 
-  const std::vector<pft::Variable> *getVariables() const { return variables; }
+  std::optional<llvm::ArrayRef<pft::Variable>> getVariables() const {
+    return variables;
+  }
 
   /// Code gen the FIR for the local bindings for the host associated symbols
   /// for the host (parent) procedure using `builder`.
@@ -82,9 +85,9 @@ class HostAssociations {
   /// These records borrow Program's cached variable list; std::map nodes and
   /// finalized variable lists remain stable through all child bindings.
   llvm::SmallVector<const pft::Variable *> globalBindings;
-  /// Whole-scope records reused by the host's own lowering as well. A null
-  /// pointer preserves the legacy path when no newly promoted globals occur.
-  const std::vector<pft::Variable> *variables = nullptr;
+  /// Whole-scope records reused by the host's own lowering as well. Disengaged
+  /// when no newly promoted globals occur, preserving the legacy path.
+  std::optional<llvm::ArrayRef<pft::Variable>> variables;
 
   /// The type of the extra argument to be added to each internal procedure.
   mlir::Type argType;
diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index 5d25ae6689d25f..23c5936a7be10b 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -973,8 +973,7 @@ class Symbol {
       OmpThreadprivate, OmpDeclareReduction, OmpFlushed, OmpCriticalLock,
       OmpIfSpecified, OmpNone, OmpPreDetermined, OmpExplicit, OmpImplicit,
       OmpDependObject, OmpInclusiveScan, OmpExclusiveScan, OmpInScanReduction,
-      OmpUniform, OmpAssumesNoOpenmp,
-      OmpMainGlobal);
+      OmpUniform, OmpAssumesNoOpenmp, OmpMainGlobal);
   using Flags = common::EnumSet<Flag, Flag_enumSize>;
 
   const Scope &owner() const { return *owner_; }
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 6b4654ed71089b..f6e46e9d706748 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -6670,10 +6670,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
 
     // New global host associations already borrow a stable scope-variable plan.
     // Reuse those records rather than recomputing them for the host itself.
-    const auto *variables = funit.hostAssociations.getVariables();
+    auto variables = funit.hostAssociations.getVariables();
     std::optional<Fortran::lower::pft::VariableList> localVariables;
     if (!variables) {
-      variables = &localVariables.emplace(
+      variables = localVariables.emplace(
           Fortran::lower::pft::getScopeVariableList(scope));
     }
     // Map function equivalences and variables.
diff --git a/flang/lib/Lower/HostAssociations.cpp b/flang/lib/Lower/HostAssociations.cpp
index 01e5371adf362f..6595c0b213f4fc 100644
--- a/flang/lib/Lower/HostAssociations.cpp
+++ b/flang/lib/Lower/HostAssociations.cpp
@@ -545,7 +545,7 @@ static mlir::Value genTupleCoor(fir::FirOpBuilder &builder, mlir::Location loc,
 void Fortran::lower::HostAssociations::addSymbolsToBind(
     const llvm::SetVector<const Fortran::semantics::Symbol *> &symbols,
     const Fortran::semantics::Scope &hostScope,
-    llvm::function_ref<const std::vector<pft::Variable> &()> getVariables) {
+    llvm::function_ref<llvm::ArrayRef<pft::Variable>()> getVariables) {
   assert(tupleSymbols.empty() && globalSymbols.empty() &&
          "must be initially empty");
   this->hostScope = &hostScope;
@@ -564,7 +564,7 @@ void Fortran::lower::HostAssociations::addSymbolsToBind(
       tupleSymbols.insert(s);
     }
   if (hasNewGlobals) {
-    variables = &getVariables();
+    variables = getVariables();
     // Aggregate stores precede their aliases. Select backwards so each chosen
     // alias requests its backing store using the already computed storage key.
     // Resolve each USE/host association tail once, including rejected records.
diff --git a/flang/lib/Lower/PFTBuilder.cpp b/flang/lib/Lower/PFTBuilder.cpp
index f36f2bd2865977..22d26584179300 100644
--- a/flang/lib/Lower/PFTBuilder.cpp
+++ b/flang/lib/Lower/PFTBuilder.cpp
@@ -343,11 +343,12 @@ class MainGlobalAnalysis {
   /// Leave arrays, descriptors, derived types, automatic objects and special
   /// runtime/device storage on their existing lowering paths.
   static bool canUseGlobal(const semantics::Symbol &symbol) {
-    // Privatizers and MIF coarray handles append these fragments to object
-    // names. Leave potential collisions on their existing local-storage path.
+    // Avoid collisions between user globals and generated privatizer names.
+    // FIXME: Remove the privatizer exclusions once naming is fixed.
+    // See https://github.com/llvm/llvm-project/issues/236705.
     const auto name = toStringRef(symbol.name());
     if (name.contains("_private_") || name.contains("_firstprivate_") ||
-        name.ends_with("_coarray_handle"))
+        name.contains("_linear_") || name.ends_with("_coarray_handle"))
       return false;
     const auto *object = symbol.detailsIf<semantics::ObjectEntityDetails>();
     const auto *type = symbol.GetType();
diff --git a/flang/test/Lower/OpenMP/main-program-globals.f90 b/flang/test/Lower/OpenMP/main-program-globals.f90
index 5216061533fd87..bbb1205c162db4 100644
--- a/flang/test/Lower/OpenMP/main-program-globals.f90
+++ b/flang/test/Lower/OpenMP/main-program-globals.f90
@@ -17,7 +17,7 @@
 ! RUN: %if asserts %{ %flang_fc1 -fopenmp -fno-openmp-main-program-globals -fdebug-dump-pft -mllvm -debug-only=flang-pft %t/miniweather.f90 2>&1 | FileCheck %s --check-prefix=PFT_OFF %}
 ! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/objects.f90 -o %t.objects
 ! RUN: FileCheck %s --check-prefix=OBJECTS < %t.objects
-! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(array|matrix|storage|i|j|ap|ptr|record|private_value|listed|eq1|eq2|x|y|serial_only|x_private_i32|y_firstprivate_i32|allocated|address|pointee|actual|element|extent|rhs|field)[[:space:]]}}' < %t.objects
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(array|matrix|storage|i|j|ap|ptr|record|private_value|listed|eq1|eq2|x|y|serial_only|x_private_i32|y_firstprivate_i32|x_linear_i32|allocated|address|pointee|actual|element|extent|rhs|field)[[:space:]]}}' < %t.objects
 ! RUN: %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/objects.f90 -o %t.off
 ! RUN: FileCheck %s --check-prefix=BOUNDARY < %t.off
 ! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(text|if_condition|if_action|where_value|forall_value)[[:space:]]}}' < %t.off
@@ -102,7 +102,8 @@ subroutine worker()
 end program
 
 ! Select intrinsic scalars in nested regions and statement expressions while
-! preserving excluded objects and private copies. Calls and constructors do not
+! preserving excluded objects and private copies. Generated privatizer names
+! must not collide with excluded scalars. Calls and constructors do not
 ! seed selection. The 32/33-byte CHARACTER cases distinguish new selection from
 ! existing static storage. serial_only checks collection stops at END PARALLEL.
 ! OBJECTS-LABEL: func.func @_QQmain()
@@ -147,7 +148,8 @@ program p
   integer :: direct,actual,element,extent,rhs,field
   integer :: array(2),matrix(2,2),private_value,listed,eq1,eq2,x,y
   integer :: if_condition,if_action,serial_only,where_value,forall_value,i,j
-  integer :: x_private_i32,y_firstprivate_i32,allocated,pointee,storage(1)
+  integer :: x_private_i32,y_firstprivate_i32,x_linear_i32
+  integer :: allocated,pointee,storage(1)
   integer(8) :: address
   pointer(address,pointee)
 !$omp allocate(allocated)
@@ -161,7 +163,8 @@ program p
   array=1; matrix=0; where_value=2; forall_value=3
 !$omp parallel
   print *,array,ap,ptr,record,private_value,listed,eq1,eq2
-  print *,x_private_i32,y_firstprivate_i32,allocated,address,pointee,saved
+  print *,x_private_i32,y_firstprivate_i32,x_linear_i32
+  print *,allocated,address,pointee,saved
   print *,direct,abs(actual),[element],[character(extent)::]
   print *,t(field)
   call sink(actual)
@@ -182,6 +185,12 @@ program p
 !$omp parallel private(private_value,x) firstprivate(y)
   private_value=x+y
 !$omp end parallel
+  x=0
+!$omp simd linear(x)
+  do i=1,4
+    x=x+1
+  end do
+!$omp end simd
   serial_only=1
 contains
   subroutine sink(n)

>From 1fbba162b568543d175a4151e38450845b3c44e3 Mon Sep 17 00:00:00 2001
From: Sergey Shcherbinin <sscherbinin at nvidia.com>
Date: Mon, 9 Nov 2026 14:03:09 +0400
Subject: [PATCH 4/4] [flang][OpenMP] Refine MAIN scalar storage selection

Keep selected MAIN scalars in lowering-owned state so building a PFT
does not change semantic symbols. Complete storage selection before
constructing variable lists or classifying host associations.

Reuse the symbol visitor for direct references, user-call arguments and
supported intrinsic arguments. Keep the user's option intact when
applying the device DO CONCURRENT restriction.

Check storage and capture behavior through generated IR rather than
debug output, and keep the lowering tests focused on their relevant
compiler stage.
---
 clang/include/clang/Options/FlangOptions.td   |   4 +-
 flang/docs/ReleaseNotes.md                    |   2 +-
 flang/include/flang/Evaluate/tools.h          |   4 +-
 flang/include/flang/Lower/AbstractConverter.h |  12 +-
 flang/include/flang/Lower/HostAssociations.h  |  23 +-
 flang/include/flang/Lower/LoweringOptions.def |   4 +-
 flang/include/flang/Lower/LoweringOptions.h   |  22 +
 flang/include/flang/Lower/PFTBuilder.h        |  56 +-
 flang/include/flang/Lower/PFTDefs.h           |   6 +-
 flang/include/flang/Lower/Support/Utils.h     |   4 +
 .../flang/Optimizer/Builder/MIFCommon.h       |   1 -
 .../flang/Optimizer/Support/InternalNames.h   |   1 +
 flang/include/flang/Semantics/symbol.h        |   2 +-
 flang/lib/Frontend/CompilerInvocation.cpp     |  15 +-
 flang/lib/Frontend/ParserActions.cpp          |   5 +-
 flang/lib/Lower/Bridge.cpp                    |  83 ++-
 flang/lib/Lower/ConvertCall.cpp               |   3 +-
 flang/lib/Lower/ConvertVariable.cpp           |   6 +-
 flang/lib/Lower/HostAssociations.cpp          |  22 +-
 flang/lib/Lower/IO.cpp                        |   2 +-
 flang/lib/Lower/PFTBuilder.cpp                | 478 ++++++++++++------
 flang/lib/Lower/Support/Utils.cpp             |  16 +-
 .../Optimizer/Transforms/MIFOpConversion.cpp  |   6 +-
 .../OpenMP/main-program-globals-cost.f90      | 110 +---
 .../OpenMP/main-program-globals-fallback.f90  |  28 +-
 .../Lower/OpenMP/main-program-globals.f90     | 201 ++++----
 flang/tools/bbc/bbc.cpp                       |  16 +-
 .../Frontend/CompilerInstanceTest.cpp         |  11 +-
 .../unittests/Frontend/FrontendActionTest.cpp |  35 ++
 29 files changed, 680 insertions(+), 498 deletions(-)

diff --git a/clang/include/clang/Options/FlangOptions.td b/clang/include/clang/Options/FlangOptions.td
index d672380260a3f8..50b34e3fc2a183 100644
--- a/clang/include/clang/Options/FlangOptions.td
+++ b/clang/include/clang/Options/FlangOptions.td
@@ -440,8 +440,8 @@ let Visibility = [FC1Option] in {
 
 def fno_openmp_main_program_globals : Flag<["-"], "fno-openmp-main-program-globals">,
   Group<f_Group>,
-  HelpText<"Disable static storage selection for small main program scalars "
-           "used in explicit host OpenMP PARALLEL regions">;
+  HelpText<"Disable promotion to static storage for small main-program scalars "
+           "referenced directly in explicit host OpenMP PARALLEL regions">;
 
 def ffp_maxmin_behavior_EQ
     : Joined<["-"], "ffp-maxmin-behavior=">,
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index 98e6aa279acafe..69d02a85a4952d 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -88,7 +88,7 @@ page](https://llvm.org/releases/).
 ## Non-comprehensive list of changes in this release
 
 - With OpenMP enabled, eligible small main-program scalars used in explicit
-  host `PARALLEL` regions now use static storage by default.
+  host `PARALLEL` regions use static storage by default.
 
 - Added support for the OpenMP implementation-defined extension sentinels
   (OpenMP 5.2, section 3.1): `!$omx`, `c$omx` and `*$omx` in fixed source form
diff --git a/flang/include/flang/Evaluate/tools.h b/flang/include/flang/Evaluate/tools.h
index b001fcfb1de181..eaa308432baefd 100644
--- a/flang/include/flang/Evaluate/tools.h
+++ b/flang/include/flang/Evaluate/tools.h
@@ -1794,8 +1794,8 @@ bool IsObjectPointer(const Symbol *);
 bool IsAllocatableOrObjectPointer(const Symbol *);
 bool IsAutomatic(const Symbol &);
 bool IsSaved(const Symbol &); // saved implicitly or explicitly
-// Test the size boundary used by main-program storage policies. The argument
-// must already be an association root; this does not test SAVE or object kind.
+// True for a MAIN-program symbol whose storage size is at most 32 bytes.
+// Resolve USE/host associations first; SAVE and object kind are not tested.
 bool IsSmallMainProgramVariable(const Symbol &);
 bool IsDummy(const Symbol &);
 
diff --git a/flang/include/flang/Lower/AbstractConverter.h b/flang/include/flang/Lower/AbstractConverter.h
index 020e1b6240d6ad..1407b2a82b8f6b 100644
--- a/flang/include/flang/Lower/AbstractConverter.h
+++ b/flang/include/flang/Lower/AbstractConverter.h
@@ -65,6 +65,7 @@ namespace lower {
 class SymMap;
 struct SymbolBox;
 namespace pft {
+struct Program;
 struct Variable;
 struct FunctionLikeUnit;
 } // namespace pft
@@ -376,8 +377,10 @@ class AbstractConverter {
   genStatAndErrmsg(mlir::Location loc,
                    const std::list<Fortran::parser::StatOrErrmsg> &) = 0;
 
-  AbstractConverter(const Fortran::lower::LoweringOptions &loweringOptions)
-      : loweringOptions(loweringOptions) {}
+  /// The options and program must outlive the converter.
+  AbstractConverter(const Fortran::lower::LoweringOptions &loweringOptions,
+                    pft::Program &program)
+      : loweringOptions(loweringOptions), program(program) {}
   virtual ~AbstractConverter() = default;
 
   //===--------------------------------------------------------------------===//
@@ -396,6 +399,9 @@ class AbstractConverter {
   /// body is wrapped.
   virtual void genLoopBodyEvaluations(pft::Evaluation &loopEval) = 0;
 
+  /// Return the PFT being lowered.
+  pft::Program &getPft() { return program; }
+
   /// Return options controlling lowering behavior.
   const Fortran::lower::LoweringOptions &getLoweringOptions() const {
     return loweringOptions;
@@ -424,6 +430,8 @@ class AbstractConverter {
 private:
   /// Options controlling lowering behavior.
   const Fortran::lower::LoweringOptions &loweringOptions;
+  /// PFT owned by LoweringBridge::lower for this converter's entire lifetime.
+  pft::Program &program;
 };
 
 } // namespace lower
diff --git a/flang/include/flang/Lower/HostAssociations.h b/flang/include/flang/Lower/HostAssociations.h
index d865a719b3f243..2742c31eee28aa 100644
--- a/flang/include/flang/Lower/HostAssociations.h
+++ b/flang/include/flang/Lower/HostAssociations.h
@@ -11,7 +11,6 @@
 
 #include "mlir/IR/Types.h"
 #include "llvm/ADT/ArrayRef.h"
-#include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include <optional>
@@ -26,8 +25,9 @@ namespace lower {
 class AbstractConverter;
 class SymMap;
 namespace pft {
+struct Program;
 struct Variable;
-}
+} // namespace pft
 
 /// Internal procedures in Fortran may access variables declared in the host
 /// procedure directly. We bundle these variables together in a tuple and pass
@@ -41,14 +41,15 @@ class HostAssociations {
   /// an extra tuple argument.
   bool hasTupleAssociations() const { return !tupleSymbols.empty(); }
 
-  /// Adds a set of Symbols that will be the host associated bindings for this
-  /// host procedure. getVariables is invoked synchronously only for newly
-  /// promoted globals and must return Program's finalized, stable records.
+  /// Classify host variables as globals or tuple entries using Program.
+  /// Cached variable records are owned by Program.
   void addSymbolsToBind(
       const llvm::SetVector<const Fortran::semantics::Symbol *> &symbols,
-      const Fortran::semantics::Scope &hostScope,
-      llvm::function_ref<llvm::ArrayRef<pft::Variable>()> getVariables);
+      const Fortran::semantics::Scope &hostScope, pft::Program &program);
 
+  /// Return cached MAIN variables when a captured scalar is selected for static
+  /// storage, or std::nullopt otherwise. Program owns the list and must outlive
+  /// the returned view.
   std::optional<llvm::ArrayRef<pft::Variable>> getVariables() const {
     return variables;
   }
@@ -81,12 +82,10 @@ class HostAssociations {
   /// Canonical vector of host associated global symbols.
   llvm::SetVector<const Fortran::semantics::Symbol *> globalSymbols;
 
-  /// Ordered bindings selected once when newly promoted globals occur.
-  /// These records borrow Program's cached variable list; std::map nodes and
-  /// finalized variable lists remain stable through all child bindings.
+  /// Ordered records for captured globals and the storage backing their
+  /// aliases. The records refer to Program's cached lists and remain valid
+  /// until it is destroyed.
   llvm::SmallVector<const pft::Variable *> globalBindings;
-  /// Whole-scope records reused by the host's own lowering as well. Disengaged
-  /// when no newly promoted globals occur, preserving the legacy path.
   std::optional<llvm::ArrayRef<pft::Variable>> variables;
 
   /// The type of the extra argument to be added to each internal procedure.
diff --git a/flang/include/flang/Lower/LoweringOptions.def b/flang/include/flang/Lower/LoweringOptions.def
index f4a0d15c0bb42c..2e2773671ac235 100644
--- a/flang/include/flang/Lower/LoweringOptions.def
+++ b/flang/include/flang/Lower/LoweringOptions.def
@@ -49,8 +49,8 @@ ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
 /// If false, assume that the shapes/types/allocation-status match.
 ENUM_LOWERINGOPT(ReallocateLHS, unsigned, 1, 1)
 
-/// If true, place eligible small main program scalars used directly by explicit
-/// host OpenMP PARALLEL regions in static memory. On by default.
+/// If true, promote eligible small main-program scalars referenced directly in
+/// explicit host OpenMP PARALLEL regions to static storage. On by default.
 ENUM_LOWERINGOPT(OpenMPMainProgramGlobals, unsigned, 1, 1)
 
 /// If true, initialize globals without initialization to zero.
diff --git a/flang/include/flang/Lower/LoweringOptions.h b/flang/include/flang/Lower/LoweringOptions.h
index 63693c5806f0ed..b1598d2bf339ce 100644
--- a/flang/include/flang/Lower/LoweringOptions.h
+++ b/flang/include/flang/Lower/LoweringOptions.h
@@ -59,6 +59,25 @@ class LoweringOptions : public LoweringOptionsBase {
 
   LoweringOptions();
 
+  bool getDeviceDoConcurrent() const { return DeviceDoConcurrent; }
+  LoweringOptions &setDeviceDoConcurrent(bool value) {
+    DeviceDoConcurrent = value;
+    return *this;
+  }
+
+  /// Whether eligible implicit-SAVE MAIN scalars may use static storage in
+  /// OpenMP.
+  bool shouldPromoteOpenMPMainProgramScalars() const {
+    if (!getOpenMPMainProgramGlobals())
+      return false;
+    // Device DO CONCURRENT creates target regions after PFT analysis, so the
+    // analysis cannot apply its TARGET fallback. Device compilation disables
+    // promotion; host compilation must match its capture types.
+    // FIXME: Test promotion with device DO CONCURRENT and support matching
+    // host/device captures before removing this restriction.
+    return !getDeviceDoConcurrent();
+  }
+
   const Fortran::common::MathOptionsBase &getMathOptions() const {
     return MathOptions;
   }
@@ -77,6 +96,9 @@ class LoweringOptions : public LoweringOptionsBase {
   /// getInitLocalMode() == InitLocalKind::Hex.
   uint8_t InitLocalPattern = 0;
 
+  /// Whether DO CONCURRENT loops are mapped to OpenMP device regions.
+  bool DeviceDoConcurrent = false;
+
   /// Options for handling/optimizing mathematical computations.
   Fortran::common::MathOptionsBase MathOptions;
 };
diff --git a/flang/include/flang/Lower/PFTBuilder.h b/flang/include/flang/Lower/PFTBuilder.h
index dfa56ab3cddd5f..028681e259f267 100644
--- a/flang/include/flang/Lower/PFTBuilder.h
+++ b/flang/include/flang/Lower/PFTBuilder.h
@@ -26,6 +26,7 @@
 #include "flang/Semantics/scope.h"
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/symbol.h"
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
@@ -654,22 +655,15 @@ using VariableList = std::vector<Variable>;
 using ScopeVariableListMap =
     std::map<const Fortran::semantics::Scope *, VariableList>;
 
-/// Find or create an ordered list of the equivalence sets and variables that
-/// appear in \p scope. The result is cached in \p map.
-const VariableList &getScopeVariableList(const Fortran::semantics::Scope &scope,
-                                         ScopeVariableListMap &map);
-
 /// Create an ordered list of the equivalence sets and variables that appear in
 /// \p scope. The result is not cached.
-VariableList getScopeVariableList(const Fortran::semantics::Scope &scope);
+VariableList getScopeVariableList(const Fortran::semantics::Scope &scope,
+                                  const Program &program);
 
 /// Create an ordered list of the equivalence sets and variables that \p symbol
 /// depends on. \p symbol itself will be the last variable in the list.
-VariableList getDependentVariableList(const Fortran::semantics::Symbol &);
-
-/// Return the enclosing Program's stable variable-list cache.
-template <typename A>
-ScopeVariableListMap &getScopeVariableListMap(A &node);
+VariableList getDependentVariableList(const Fortran::semantics::Symbol &,
+                                      const Program &program);
 
 struct FunctionLikeUnit;
 /// Create an ordered list of equivalence sets and variables from host
@@ -752,14 +746,10 @@ struct FunctionLikeUnit : public ProgramUnit {
   // Host associations
   //===--------------------------------------------------------------------===//
 
+  /// Classify captures using the enclosing Program's finalized storage
+  /// selection.
   void setHostAssociatedSymbols(
-      const llvm::SetVector<const semantics::Symbol *> &symbols) {
-    hostAssociations.addSymbolsToBind(
-        symbols, getScope(), [&]() -> const VariableList & {
-          return getScopeVariableList(getScope(),
-                                      getScopeVariableListMap(*this));
-        });
-  }
+      const llvm::SetVector<const semantics::Symbol *> &symbols);
 
   /// Return the host associations, if any, from the parent (host) procedure.
   /// Crashes if the parent is not a procedure.
@@ -898,17 +888,33 @@ struct Program {
   const semantics::CommonBlockList &getCommonBlocks() const {
     return commonBlocks;
   }
-  ScopeVariableListMap &getScopeVariableListMap() {
-    return scopeVariableListMap;
+  /// Store the selected MAIN globals before constructing any variable lists.
+  /// The selection remains unchanged throughout lowering.
+  void setMainGlobals(llvm::DenseSet<const semantics::Symbol *> &&globals) {
+    assert(scopeVariableListMap.empty() &&
+           "storage selection must precede variable-list construction");
+    mainGlobals = std::move(globals);
+  }
+
+  /// True if a symbol returned by Symbol::GetUltimate() was selected for
+  /// static MAIN storage.
+  bool isMainGlobal(const semantics::Symbol &ultimate) const {
+    return mainGlobals.contains(&ultimate);
   }
 
+  /// Cache ordered variable records for this storage selection. They remain
+  /// valid until this Program is destroyed.
+  const VariableList &getScopeVariableList(const semantics::Scope &scope);
+
   /// LLVM dump method on a Program.
   LLVM_DUMP_METHOD void dump() const;
 
 private:
   std::list<Units> units;
   semantics::CommonBlockList commonBlocks;
-  /// Module/submodule scopes and MAIN hosts with newly promoted globals.
+  /// MAIN scalars chosen for static storage. Their symbols outlive the PFT.
+  llvm::DenseSet<const semantics::Symbol *> mainGlobals;
+  /// Variable records for module scopes and MAIN hosts with selected globals.
   ScopeVariableListMap scopeVariableListMap;
 };
 
@@ -929,14 +935,6 @@ ParentType *getAncestor(A &node) {
       [](auto &p) -> ParentType * { return getAncestor<ParentType>(p); }});
 }
 
-/// Get the "global" scopeVariableListMap, stored in the pft root node.
-template <typename A>
-ScopeVariableListMap &getScopeVariableListMap(A &node) {
-  Program *pftRoot = getAncestor<Program>(node);
-  assert(pftRoot && "pft must have a root");
-  return pftRoot->getScopeVariableListMap();
-}
-
 /// Call the provided \p callBack on all symbols that are referenced inside \p
 /// funit.
 void visitAllSymbols(const FunctionLikeUnit &funit,
diff --git a/flang/include/flang/Lower/PFTDefs.h b/flang/include/flang/Lower/PFTDefs.h
index c2d65320acc487..45716bca4f3edf 100644
--- a/flang/include/flang/Lower/PFTDefs.h
+++ b/flang/include/flang/Lower/PFTDefs.h
@@ -41,9 +41,13 @@ class Reference;
 }
 
 namespace lower {
+namespace pft {
+struct Program;
+}
 
 bool definedInCommonBlock(const semantics::Symbol &sym);
-bool symbolIsGlobal(const semantics::Symbol &sym);
+/// Classify storage using semantic properties and this PFT's storage selection.
+bool symbolIsGlobal(const semantics::Symbol &sym, const pft::Program &program);
 bool defaultRecursiveFunctionSetting();
 
 namespace pft {
diff --git a/flang/include/flang/Lower/Support/Utils.h b/flang/include/flang/Lower/Support/Utils.h
index bc2fb488f924a8..51b6353a77ceb2 100644
--- a/flang/include/flang/Lower/Support/Utils.h
+++ b/flang/include/flang/Lower/Support/Utils.h
@@ -86,6 +86,10 @@ inline Fortran::lower::SomeExpr ignoreEvConvert(
 }
 
 namespace Fortran::lower {
+/// Privatizer name suffixes, including the trailing '_' separator.
+inline constexpr llvm::StringLiteral privatizerSuffixes[] = {
+    "_private_", "_firstprivate_", "_linear_"};
+
 unsigned getHashValue(const Fortran::lower::SomeExpr *x);
 unsigned getHashValue(const Fortran::evaluate::Component *x);
 
diff --git a/flang/include/flang/Optimizer/Builder/MIFCommon.h b/flang/include/flang/Optimizer/Builder/MIFCommon.h
index 8c986dee0d179c..5957adab0a65ff 100644
--- a/flang/include/flang/Optimizer/Builder/MIFCommon.h
+++ b/flang/include/flang/Optimizer/Builder/MIFCommon.h
@@ -12,7 +12,6 @@
 #include "flang/Optimizer/Builder/FIRBuilder.h"
 #include "flang/Optimizer/Dialect/MIF/MIFOps.h"
 
-static constexpr llvm::StringRef coarrayHandleSuffix = "_coarray_handle";
 static constexpr llvm::StringRef mifSaveCoarraysAllocName =
     "__mif_save_coarrays_allocate";
 
diff --git a/flang/include/flang/Optimizer/Support/InternalNames.h b/flang/include/flang/Optimizer/Support/InternalNames.h
index d5da47818e1c06..bee5b0c9671df9 100644
--- a/flang/include/flang/Optimizer/Support/InternalNames.h
+++ b/flang/include/flang/Optimizer/Support/InternalNames.h
@@ -30,6 +30,7 @@ static constexpr llvm::StringRef kSpecialBindingSeparator = ".s.";
 static constexpr llvm::StringRef kBindingTableSeparator = ".v.";
 static constexpr llvm::StringRef boxprocSuffix = "UnboxProc";
 static constexpr llvm::StringRef kDerivedTypeInitSuffix = "DerivedInit";
+static constexpr llvm::StringRef coarrayHandleSuffix = "_coarray_handle";
 
 /// Internal name mangling of identifiers
 ///
diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index 23c5936a7be10b..d99e048f627717 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -973,7 +973,7 @@ class Symbol {
       OmpThreadprivate, OmpDeclareReduction, OmpFlushed, OmpCriticalLock,
       OmpIfSpecified, OmpNone, OmpPreDetermined, OmpExplicit, OmpImplicit,
       OmpDependObject, OmpInclusiveScan, OmpExclusiveScan, OmpInScanReduction,
-      OmpUniform, OmpAssumesNoOpenmp, OmpMainGlobal);
+      OmpUniform, OmpAssumesNoOpenmp);
   using Flags = common::EnumSet<Flag, Flag_enumSize>;
 
   const Scope &owner() const { return *owner_; }
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 3c1b2a7b111404..7727d6616208d0 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1910,12 +1910,8 @@ bool CompilerInvocation::createFromArgs(
   parsePreprocessorArgs(invoc.getPreprocessorOpts(), args);
   parseCodeGenArgs(invoc.getCodeGenOpts(), args, diags);
   success &= parseDoConcurrentMapping(invoc.getCodeGenOpts(), args, diags);
-  // Device DO CONCURRENT targets are generated after PFT construction. Keep
-  // host and device capture layouts identical when these targets are enabled.
   invoc.loweringOpts.setOpenMPMainProgramGlobals(
-      !args.hasArg(clang::options::OPT_fno_openmp_main_program_globals) &&
-      invoc.getCodeGenOpts().getDoConcurrentMapping() !=
-          CodeGenOptions::DoConcurrentMappingKind::DCMK_Device);
+      !args.hasArg(clang::options::OPT_fno_openmp_main_program_globals));
   success &= parseDebugArgs(invoc.getCodeGenOpts(), args, diags,
                             llvm::Triple(invoc.getTargetOpts().triple));
 
@@ -2188,11 +2184,10 @@ CompilerInvocation::getSemanticsCtx(
 void CompilerInvocation::setLoweringOptions() {
   const CodeGenOptions &codegenOpts = getCodeGenOpts();
 
-  // API clients may change the mapping after argument parsing. Reapply the
-  // device capture-layout restriction before constructing the PFT.
-  if (codegenOpts.getDoConcurrentMapping() ==
-      CodeGenOptions::DoConcurrentMappingKind::DCMK_Device)
-    loweringOpts.setOpenMPMainProgramGlobals(false);
+  // API clients may change the mapping after argument parsing.
+  loweringOpts.setDeviceDoConcurrent(
+      codegenOpts.getDoConcurrentMapping() ==
+      CodeGenOptions::DoConcurrentMappingKind::DCMK_Device);
 
   // Lower TRANSPOSE as a runtime call under -O0.
   loweringOpts.setOptimizeTranspose(codegenOpts.OptimizationLevel > 0);
diff --git a/flang/lib/Frontend/ParserActions.cpp b/flang/lib/Frontend/ParserActions.cpp
index cc54f565e9516b..284bc0bdccefd0 100644
--- a/flang/lib/Frontend/ParserActions.cpp
+++ b/flang/lib/Frontend/ParserActions.cpp
@@ -48,10 +48,9 @@ void dumpProvenance(CompilerInstance &ci) {
 void dumpPreFIRTree(CompilerInstance &ci) {
   auto &parseTree{*ci.getParsing().parseTree()};
 
-  // Use default lowering options, preserving the main-program globals setting.
+  // PFT dumps show control flow and do not use variable storage choices.
   lower::LoweringOptions loweringOptions{};
-  loweringOptions.setOpenMPMainProgramGlobals(
-      ci.getInvocation().getLoweringOpts().getOpenMPMainProgramGlobals());
+  loweringOptions.setOpenMPMainProgramGlobals(false);
   if (auto ast{lower::createPFT(parseTree, ci.getSemanticsContext(),
                                 loweringOptions)}) {
     lower::dumpPFT(llvm::outs(), *ast);
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index f6e46e9d706748..5abbedfd921d7b 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -556,14 +556,16 @@ namespace {
 /// Traverse the pre-FIR tree (PFT) to generate the FIR dialect of MLIR.
 class FirConverter : public Fortran::lower::AbstractConverter {
 public:
-  explicit FirConverter(Fortran::lower::LoweringBridge &bridge)
-      : Fortran::lower::AbstractConverter(bridge.getLoweringOptions()),
+  FirConverter(Fortran::lower::LoweringBridge &bridge,
+               Fortran::lower::pft::Program &program)
+      : Fortran::lower::AbstractConverter(bridge.getLoweringOptions(), program),
         bridge{bridge}, foldingContext{bridge.createFoldingContext()},
         mlirSymbolTable{bridge.getModule()} {}
   virtual ~FirConverter() = default;
 
   /// Convert the PFT to FIR.
-  void run(Fortran::lower::pft::Program &pft) {
+  void run() {
+    auto &pft = getPft();
     // Preliminary translation pass.
 
     // Lower common blocks, taking into account initialization and the largest
@@ -4691,7 +4693,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         scopeBlockIdMap.try_emplace(&scope, ++blockId);
         Fortran::lower::AggregateStoreMap storeMap;
         for (const Fortran::lower::pft::Variable &var :
-             Fortran::lower::pft::getScopeVariableList(scope)) {
+             Fortran::lower::pft::getScopeVariableList(scope, getPft())) {
           // Do no instantiate again variables from the block host
           // that appears in specification of block variables.
           if (!var.hasSymbol() || !lookupSymbol(var.getSymbol()))
@@ -6668,45 +6670,44 @@ class FirConverter : public Fortran::lower::AbstractConverter {
            Fortran::lower::pft::getHostModuleVariableList(funit))
         instantiateVar(var, storeMap);
 
-    // New global host associations already borrow a stable scope-variable plan.
-    // Reuse those records rather than recomputing them for the host itself.
-    auto variables = funit.hostAssociations.getVariables();
-    std::optional<Fortran::lower::pft::VariableList> localVariables;
-    if (!variables) {
-      variables = localVariables.emplace(
-          Fortran::lower::pft::getScopeVariableList(scope));
-    }
     // Map function equivalences and variables.
     mlir::Value primaryFuncResultStorage;
-    for (const Fortran::lower::pft::Variable &var : *variables) {
-      // Always instantiate aggregate storage blocks.
-      if (var.isAggregateStore()) {
-        instantiateVar(var, storeMap);
-        continue;
+    {
+      // Reuse the variable records cached while classifying host associations.
+      std::optional<Fortran::lower::pft::VariableList> localVariables;
+      auto variables = funit.hostAssociations.getVariables();
+      if (!variables) {
+        variables = localVariables.emplace(
+            Fortran::lower::pft::getScopeVariableList(scope, getPft()));
       }
-      const Fortran::semantics::Symbol &sym = var.getSymbol();
-      if (funit.parentHasHostAssoc()) {
-        // Never instantiate host associated variables, as they are already
-        // instantiated from an argument tuple. Instead, just bind the symbol
-        // to the host variable, which must be in the map.
-        const Fortran::semantics::Symbol &ultimate = sym.GetUltimate();
-        if (funit.parentHostAssoc().isAssociated(ultimate)) {
-          copySymbolBinding(ultimate, sym);
+      for (const Fortran::lower::pft::Variable &var : *variables) {
+        // Always instantiate aggregate storage blocks.
+        if (var.isAggregateStore()) {
+          instantiateVar(var, storeMap);
           continue;
         }
-      }
-      if (!sym.IsFuncResult() || !funit.primaryResult) {
-        instantiateVar(var, storeMap);
-      } else if (&sym == funit.primaryResult) {
-        instantiateVar(var, storeMap);
-        primaryFuncResultStorage = getSymbolAddress(sym);
-      } else {
-        deferredFuncResultList.push_back(var);
+        const Fortran::semantics::Symbol &sym = var.getSymbol();
+        if (funit.parentHasHostAssoc()) {
+          // Host-associated storage is already bound through a global address
+          // or the host tuple. Reuse it for the current procedure's symbol
+          // instead of allocating another variable.
+          const Fortran::semantics::Symbol &ultimate = sym.GetUltimate();
+          if (funit.parentHostAssoc().isAssociated(ultimate)) {
+            copySymbolBinding(ultimate, sym);
+            continue;
+          }
+        }
+        if (!sym.IsFuncResult() || !funit.primaryResult) {
+          instantiateVar(var, storeMap);
+        } else if (&sym == funit.primaryResult) {
+          instantiateVar(var, storeMap);
+          primaryFuncResultStorage = getSymbolAddress(sym);
+        } else {
+          deferredFuncResultList.push_back(var);
+        }
       }
     }
 
-    // Preserve the legacy temporary's lifetime when no cached plan exists.
-    localVariables.reset();
     // TODO: should use same mechanism as equivalence?
     // One blocking point is character entry returns that need special handling
     // since they are not locally allocated but come as argument. CHARACTER(*)
@@ -7025,7 +7026,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       if (modName != "__fortran_ieee_exceptions" &&
           modName != "iso_fortran_env")
         continue;
-      for (auto &var : Fortran::lower::pft::getScopeVariableList(scope)) {
+      for (auto &var :
+           Fortran::lower::pft::getScopeVariableList(scope, getPft())) {
         const Fortran::semantics::Symbol &sym = var.getSymbol();
         if (sym.test(Fortran::semantics::Symbol::Flag::CompilerCreated))
           continue;
@@ -7131,10 +7133,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   void lowerModuleDeclScope(Fortran::lower::pft::ModuleLikeUnit &mod) {
     setCurrentPosition(mod.getStartingSourceLoc());
     emitModuleDebugImports(*this, *builder, toLocation(), mod);
-    auto &scopeVariableListMap =
-        Fortran::lower::pft::getScopeVariableListMap(mod);
-    for (const auto &var : Fortran::lower::pft::getScopeVariableList(
-             mod.getScope(), scopeVariableListMap)) {
+    for (const auto &var : getPft().getScopeVariableList(mod.getScope())) {
 
       // Only define the variables owned by this module.
       const Fortran::semantics::Scope *owningScope = var.getOwningScope();
@@ -7408,8 +7407,8 @@ void Fortran::lower::LoweringBridge::lower(
     const Fortran::semantics::SemanticsContext &semanticsContext) {
   std::unique_ptr<Fortran::lower::pft::Program> pft =
       Fortran::lower::createPFT(prg, semanticsContext, getLoweringOptions());
-  FirConverter converter{*this};
-  converter.run(*pft);
+  FirConverter converter{*this, *pft};
+  converter.run();
 }
 
 void Fortran::lower::LoweringBridge::parseSourceFile(llvm::SourceMgr &srcMgr) {
diff --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp
index 8400743c0a30d9..a0aaeb242b4173 100644
--- a/flang/lib/Lower/ConvertCall.cpp
+++ b/flang/lib/Lower/ConvertCall.cpp
@@ -2853,7 +2853,8 @@ class ElementalUserCallBuilder
         requiredDummySymbols;
     const Fortran::semantics::Symbol &result = caller.getResultSymbol();
     for (Fortran::lower::pft::Variable var :
-         Fortran::lower::pft::getDependentVariableList(result))
+         Fortran::lower::pft::getDependentVariableList(result,
+                                                       converter.getPft()))
       if (var.hasSymbol()) {
         const Fortran::semantics::Symbol &sym = var.getSymbol();
         if (Fortran::semantics::IsDummy(sym) && sym.owner() == result.owner())
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 9e69b0723840b9..88236534f9ea8f 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -292,7 +292,8 @@ mlir::Value Fortran::lower::genInitialDataTarget(
     auto var = Fortran::lower::pft::Variable(sym, /*global=*/true);
     if (couldBeInEquivalence) {
       auto dependentVariableList =
-          Fortran::lower::pft::getDependentVariableList(sym);
+          Fortran::lower::pft::getDependentVariableList(sym,
+                                                        converter.getPft());
       for (Fortran::lower::pft::Variable var : dependentVariableList) {
         if (!var.isAggregateStore())
           break;
@@ -3610,7 +3611,8 @@ mapCallInterfaceSymbol(const Fortran::semantics::Symbol &interfaceSymbol,
                        Fortran::lower::SymMap &symMap) {
   Fortran::lower::AggregateStoreMap storeMap;
   for (Fortran::lower::pft::Variable var :
-       Fortran::lower::pft::getDependentVariableList(interfaceSymbol)) {
+       Fortran::lower::pft::getDependentVariableList(interfaceSymbol,
+                                                     converter.getPft())) {
     if (var.isAggregateStore()) {
       instantiateVariable(converter, var, symMap, storeMap);
       continue;
diff --git a/flang/lib/Lower/HostAssociations.cpp b/flang/lib/Lower/HostAssociations.cpp
index 6595c0b213f4fc..4b1b22f1cd8eea 100644
--- a/flang/lib/Lower/HostAssociations.cpp
+++ b/flang/lib/Lower/HostAssociations.cpp
@@ -24,7 +24,6 @@
 #include "flang/Semantics/tools.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
-#include "llvm/Support/Debug.h"
 #include <algorithm>
 #include <optional>
 
@@ -544,31 +543,30 @@ static mlir::Value genTupleCoor(fir::FirOpBuilder &builder, mlir::Location loc,
 
 void Fortran::lower::HostAssociations::addSymbolsToBind(
     const llvm::SetVector<const Fortran::semantics::Symbol *> &symbols,
-    const Fortran::semantics::Scope &hostScope,
-    llvm::function_ref<llvm::ArrayRef<pft::Variable>()> getVariables) {
+    const Fortran::semantics::Scope &hostScope, pft::Program &program) {
   assert(tupleSymbols.empty() && globalSymbols.empty() &&
          "must be initially empty");
   this->hostScope = &hostScope;
-  bool hasNewGlobals = false;
+  bool capturesMainGlobal = false;
   for (const auto *s : symbols)
     // GlobalOp are created for non-global threadprivate variable,
     //  so considering them as globals.
-    if (Fortran::lower::symbolIsGlobal(*s) ||
+    if (Fortran::lower::symbolIsGlobal(*s, program) ||
         (*s).test(Fortran::semantics::Symbol::Flag::OmpThreadprivate)) {
       // The ultimate symbol is stored here so that global symbols from the
       // host scope can later be searched in this set.
       const auto &ultimate = s->GetUltimate();
       globalSymbols.insert(&ultimate);
-      hasNewGlobals |= ultimate.test(semantics::Symbol::Flag::OmpMainGlobal);
+      capturesMainGlobal = capturesMainGlobal || program.isMainGlobal(ultimate);
     } else {
       tupleSymbols.insert(s);
     }
-  if (hasNewGlobals) {
-    variables = getVariables();
+  if (capturesMainGlobal) {
+    variables = program.getScopeVariableList(hostScope);
     // Aggregate stores precede their aliases. Select backwards so each chosen
     // alias requests its backing store using the already computed storage key.
-    // Resolve each USE/host association tail once, including rejected records.
-    // Direct objects require no cache lookup or allocation.
+    // Reuse shared parts of USE/host association chains, even for skipped
+    // records. Direct objects require no cache lookup or allocation.
     llvm::DenseMap<const semantics::Symbol *, const semantics::Symbol *> roots;
     auto root = [&](const semantics::Symbol &symbol) {
       const auto *original = &symbol;
@@ -587,7 +585,6 @@ void Fortran::lower::HostAssociations::addSymbolsToBind(
           break;
         }
         path.push_back(original);
-        LLVM_DEBUG(llvm::dbgs() << "main-global binding association\n");
         original = next;
       }
       for (const auto *entry : path)
@@ -660,7 +657,8 @@ void Fortran::lower::HostAssociations::internalProcedureBindings(
       for (const auto *variable : globalBindings)
         bind(*variable);
     } else {
-      for (const auto &variable : pft::getScopeVariableList(*hostScope))
+      for (const auto &variable :
+           pft::getScopeVariableList(*hostScope, converter.getPft()))
         if ((variable.isAggregateStore() && variable.isGlobal()) ||
             (variable.hasSymbol() &&
              globalSymbols.contains(&variable.getSymbol().GetUltimate())))
diff --git a/flang/lib/Lower/IO.cpp b/flang/lib/Lower/IO.cpp
index 43ba41b0aaec26..9278213d087282 100644
--- a/flang/lib/Lower/IO.cpp
+++ b/flang/lib/Lower/IO.cpp
@@ -394,7 +394,7 @@ getNamelistGroup(Fortran::lower::AbstractConverter &converter,
   stringAddress(symbol);
   for (const Fortran::semantics::Symbol &s : details.objects()) {
     stringAddress(s);
-    if (!Fortran::lower::symbolIsGlobal(s)) {
+    if (!Fortran::lower::symbolIsGlobal(s, converter.getPft())) {
       groupIsLocal = true;
       continue;
     }
diff --git a/flang/lib/Lower/PFTBuilder.cpp b/flang/lib/Lower/PFTBuilder.cpp
index 22d26584179300..7b0ff9dddb52eb 100644
--- a/flang/lib/Lower/PFTBuilder.cpp
+++ b/flang/lib/Lower/PFTBuilder.cpp
@@ -7,9 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Lower/PFTBuilder.h"
+#include "flang/Evaluate/intrinsics.h"
 #include "flang/Evaluate/traverse.h"
 #include "flang/Lower/LoweringOptions.h"
 #include "flang/Lower/Support/Utils.h"
+#include "flang/Optimizer/Support/InternalNames.h"
 #include "flang/Parser/dump-parse-tree.h"
 #include "flang/Parser/openmp-utils.h"
 #include "flang/Parser/parse-tree-visitor.h"
@@ -18,11 +20,13 @@
 #include "flang/Semantics/tools.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/IntervalMap.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include <algorithm>
 #include <limits>
+#include <variant>
 
 #define DEBUG_TYPE "flang-pft"
 
@@ -86,90 +90,130 @@ struct UnwrapStmt<parser::UnlabeledStatement<A>> {
 void dumpScope(const semantics::Scope *scope, int depth = -1);
 #endif
 
-/// Collect direct storage uses while retaining complex expressions on the
-/// existing storage path. Skipped nodes do not seed selection from their
-/// children.
-struct MainSymbolVisitor
-    : public evaluate::AllTraverse<MainSymbolVisitor, true, false> {
-  using Base = evaluate::AllTraverse<MainSymbolVisitor, true, false>;
-  /// Borrow the callback for one synchronous expression walk.
-  explicit MainSymbolVisitor(
-      llvm::function_ref<void(const semantics::Symbol &)> callback)
-      : Base{*this}, callback{callback} {}
+/// Names from DO CONCURRENT SHARED lists or references implied by a symbol.
+enum class SymbolVisitKind { SharedLocality, Dependencies };
+
+/// Nodes the caller can handle instead of the default symbol traversal.
+/// Pointers are valid only during the callback.
+using SymbolVisit =
+    std::variant<SymbolVisitKind, const lower::pft::SomeExpr *,
+                 const evaluate::Assignment *, const evaluate::ProcedureRef *>;
+
+/// Collect direct references and user-call arguments. Elemental intrinsics
+/// and both BESSEL_JN/YN forms contribute arguments used to compute values.
+/// Arguments used only for declared type, kind parameters or rank are skipped,
+/// as are other intrinsics, intrinsic-module procedures, constructors and
+/// coarray references.
+struct DirectSymbolVisitor
+    : public evaluate::AllTraverse<DirectSymbolVisitor, true> {
+  using Base = evaluate::AllTraverse<DirectSymbolVisitor, true>;
+  /// The callbacks and intrinsic table must remain valid throughout this
+  /// expression traversal.
+  explicit DirectSymbolVisitor(
+      llvm::function_ref<void(const semantics::Symbol &)> callback,
+      const evaluate::IntrinsicProcTable &intrinsics,
+      llvm::function_ref<bool(const semantics::Symbol &)> inIntrinsicModule)
+      : Base{*this}, callback{callback}, intrinsics{intrinsics},
+        inIntrinsicModule{inIntrinsicModule} {}
   using Base::operator();
   /// Stream each direct symbol occurrence without building subtree sets.
   bool operator()(const semantics::Symbol &symbol) const {
-    LLVM_DEBUG(llvm::dbgs() << "main-global symbol\n");
     callback(symbol);
     return true;
   }
-  /// TODO: Collect runtime dependencies of calls, constructors, PDT parameters
-  /// and coarray references, excluding metadata and folded payloads and
-  /// avoiding copies of selector expressions.
-  bool operator()(const semantics::DerivedTypeSpec &) const { return true; }
   /// Computed derived constants cannot add direct storage uses.
   bool operator()(const evaluate::Constant<evaluate::SomeDerived> &) const {
     return true;
   }
+  /// TODO: Collect runtime operands of coindexed references.
   bool operator()(const evaluate::CoarrayRef &) const { return true; }
-  bool operator()(const evaluate::ProcedureRef &) const { return true; }
-  /// Defined assignment is a call; ordinary assignment retains both operands.
-  bool operator()(const evaluate::Assignment &assignment) const {
-    if (std::holds_alternative<evaluate::ProcedureRef>(assignment.u))
+  /// Visit call arguments and a procedure-component base, not the procedure
+  /// name. Intrinsic arguments are filtered below.
+  bool operator()(const evaluate::ProcedureRef &call) const {
+    if (const auto *component = call.proc().GetComponent())
+      if (!(*this)(component->base()))
+        return false;
+    const auto &arguments = call.arguments();
+    if (arguments.empty())
       return true;
+    // TODO: Classify runtime arguments of other intrinsic procedures before
+    // traversing them.
+    if (const auto *intrinsic = call.proc().GetSpecificIntrinsic()) {
+      const auto &characteristics = intrinsic->characteristics.value();
+      // Classification is by name: the three-argument BESSEL_JN/YN forms are
+      // included with their elemental forms and need all three argument values.
+      if (!characteristics.IsFunction() ||
+          intrinsics.GetIntrinsicClass(intrinsic->name) !=
+              evaluate::IntrinsicClass::elementalFunction)
+        return true;
+      const auto &dummies = characteristics.dummyArguments;
+      auto isInquiryOnly = [](const auto &dummy) {
+        const auto *object =
+            std::get_if<evaluate::characteristics::DummyDataObject>(&dummy.u);
+        return object &&
+               object->attrs.test(evaluate::characteristics::DummyDataObject::
+                                      Attr::OnlyIntrinsicInquiry);
+      };
+      // Folding IZEXT/JZEXT into IAND adds a mask argument without updating
+      // the original parameter list, so argument counts can differ.
+      if (arguments.size() != dummies.size()) {
+        if (llvm::any_of(dummies, isInquiryOnly))
+          return true;
+        return (*this)(arguments);
+      }
+      // Resolved actuals follow dummy order, including keyword arguments.
+      for (auto [argument, dummy] : llvm::zip_equal(arguments, dummies)) {
+        if (isInquiryOnly(dummy))
+          continue;
+        if (!(*this)(argument))
+          return false;
+      }
+      return true;
+    } else if (const auto *symbol = call.proc().GetSymbol()) {
+      if (inIntrinsicModule(*symbol))
+        return true;
+    }
+    return (*this)(arguments);
+  }
+  /// Visit a defined assignment's arguments once, or both intrinsic operands.
+  bool operator()(const evaluate::Assignment &assignment) const {
+    if (const auto *call = std::get_if<evaluate::ProcedureRef>(&assignment.u))
+      return (*this)(*call);
     return Base::operator()(assignment);
   }
+  /// TODO: Collect component values and runtime derived-type parameters.
   bool operator()(const evaluate::StructureConstructor &) const { return true; }
+  /// TODO: Collect array elements and implied-DO bounds; CHARACTER length
+  /// expressions need an explicit visit.
   template <typename A>
   bool operator()(const evaluate::ArrayConstructor<A> &) const {
     return true;
   }
-  /// Avoid syntax collection after deliberately skipping resolved calls.
-  bool Pre(const parser::CallStmt &) { return false; }
-  /// SHARED names are metadata rather than accesses to object storage.
-  bool Pre(const parser::LocalitySpec::Shared &) { return false; }
-  template <typename A>
-  bool Pre(const A &node) {
-    if constexpr (parser::HasTypedExpr<A>::value) {
-      if (const auto *expr = semantics::GetExpr(nullptr, node)) {
-        collect(*expr);
-        return false;
-      }
-    } else if constexpr (parser::HasTypedAssignment<A>::value) {
-      if (node.typedAssignment && node.typedAssignment->v) {
-        collect(*node.typedAssignment->v);
-        return false;
-      }
-    }
-    return true;
-  }
-  bool Pre(const parser::Name &name) {
-    if (name.symbol)
-      callback(*name.symbol);
-    return false;
-  }
-  template <typename A>
-  void Post(const A &) {}
 
 private:
-  /// Traverse the outer typed expression once before returning to parser Walk.
-  template <typename A>
-  void collect(const A &expr) {
-    LLVM_DEBUG(llvm::dbgs() << "main-global expression\n");
-    (*this)(expr);
-  }
   llvm::function_ref<void(const semantics::Symbol &)> callback;
+  const evaluate::IntrinsicProcTable &intrinsics;
+  llvm::function_ref<bool(const semantics::Symbol &)> inIntrinsicModule;
 };
 
+/// Visit symbol references with an optional hook before entering subtrees.
+template <typename A>
+void visitSymbols(const A &node,
+                  llvm::function_ref<void(const semantics::Symbol &)> callback,
+                  llvm::function_ref<bool(SymbolVisit)> preVisit = {});
+
 /// Select small MAIN scalars directly used inside explicit host PARALLEL
 /// regions during PFT construction, before host associations are classified.
-/// Unsupported contexts retain the existing storage policy for the whole MAIN.
+/// Offload constructs and metadirectives conservatively disable selection
+/// for MAIN, so host and device procedures use identical capture layouts.
 class MainGlobalAnalysis {
 public:
-  /// Disabled analyses do not walk statements or semantic scopes.
-  explicit MainGlobalAnalysis(bool enabled) : enabled{enabled} {}
-  /// Determine membership once per procedure, including serially called
-  /// internal procedures that contain their own PARALLEL regions.
+  /// The intrinsic table must outlive the analysis. Disabled analyses skip
+  /// symbol collection and scope traversal.
+  MainGlobalAnalysis(bool enabled,
+                     const evaluate::IntrinsicProcTable &intrinsics)
+      : enabled{enabled}, intrinsics{intrinsics} {}
+  /// Track MAIN and all its internal procedures for scalar collection.
   void enterFunction(const lower::pft::FunctionLikeUnit &unit) {
     if (!enabled)
       return;
@@ -184,12 +228,11 @@ class MainGlobalAnalysis {
         return;
       }
     }
-    // Internal subprograms cannot themselves contain internal subprograms.
-    // Check only the immediate PFT parent, without semantic submodule ancestry.
+    // An internal procedure cannot contain another internal procedure, so
+    // only the immediate PFT parent can be MAIN.
     if (!relevant && mainScope)
       if (const auto *host =
               unit.parent.getIf<lower::pft::FunctionLikeUnit>()) {
-        LLVM_DEBUG(llvm::dbgs() << "main-global function parent\n");
         relevant = host->isMainProgram();
       }
     if (relevant && scope.symbol()) {
@@ -198,8 +241,8 @@ class MainGlobalAnalysis {
         unsupported = true;
     }
   }
-  /// Observe directive headers before their bodies for the whole-MAIN fallback.
-  /// TARGET leaves of combined constructs are checked by enterOpenMP().
+  /// Detect offload data directives, DECLARE TARGET and metadirectives.
+  /// TARGET constructs, standalone or combined, are handled by enterOpenMP().
   void directive(llvm::omp::Directive id) {
     if (!relevant || unsupported)
       return;
@@ -237,9 +280,9 @@ class MainGlobalAnalysis {
       parallels.pop_back();
     }
   }
-  /// Visit executable leaves in PARALLEL, including WHERE/FORALL statements
-  /// without PFT evaluations. Clauses do not seed selection; an IF action has
-  /// its own evaluation and must not be visited a second time.
+  /// Visit statements inside PARALLEL, including WHERE/FORALL statements
+  /// without separate PFT entries. OpenMP clause lists are skipped; IF actions
+  /// have their own PFT entries.
   template <typename A>
   void collect(const A &node) {
     if (!relevant || unsupported || !parallelDepth)
@@ -249,39 +292,62 @@ class MainGlobalAnalysis {
       if (&original->owner() == mainScope)
         objects.insert(original);
     };
-    MainSymbolVisitor visitor{collectSymbol};
+    auto inIntrinsicModule = [&](const semantics::Symbol &symbol) {
+      return isIntrinsicModuleProcedure(symbol);
+    };
+    DirectSymbolVisitor expressions{collectSymbol, intrinsics,
+                                    inIntrinsicModule};
+    // Use analyzed operands instead of their source names: the source walk
+    // would also collect operands that this analysis deliberately skips.
+    // SHARED locality lists and implicit dependencies do not select storage.
+    auto preVisit = [&](SymbolVisit node) {
+      return common::visit(
+          common::visitors{[](SymbolVisitKind) { return false; },
+                           [&](const auto *expr) {
+                             expressions(*expr);
+                             return false;
+                           }},
+          node);
+    };
     if constexpr (std::is_same_v<A, lower::pft::Evaluation>) {
       node.visit([&](const auto &stmt) {
         if constexpr (std::is_same_v<std::decay_t<decltype(stmt)>,
                                      parser::IfStmt>)
-          parser::Walk(std::get<parser::ScalarLogicalExpr>(stmt.t), visitor);
+          visitSymbols(std::get<parser::ScalarLogicalExpr>(stmt.t),
+                       collectSymbol, preVisit);
         else if constexpr (lower::pft::isActionStmt<
                                std::decay_t<decltype(stmt)>> ||
                            lower::pft::isConstructStmt<
                                std::decay_t<decltype(stmt)>>)
-          parser::Walk(stmt, visitor);
+          visitSymbols(stmt, collectSymbol, preVisit);
       });
     } else {
-      parser::Walk(node, visitor);
+      visitSymbols(node, collectSymbol, preVisit);
     }
   }
-  /// Filter whole original objects, then set storage flags. Set iteration
-  /// order cannot affect these independent decisions or generated declarations.
-  void finalize() {
+  /// Return the eligible MAIN scalars after applying storage restrictions.
+  /// Set iteration cannot affect independent decisions or declaration order.
+  llvm::DenseSet<const semantics::Symbol *>
+  finalize(const lower::pft::Program &program) {
     if (unsupported || objects.empty())
-      return;
-    objects.remove_if(
-        [](const semantics::Symbol *symbol) { return !canUseGlobal(*symbol); });
+      return {};
+    const auto candidateCount = objects.size();
+    objects.remove_if([&](const semantics::Symbol *symbol) {
+      return !canUseGlobal(*symbol, program);
+    });
     if (objects.empty())
-      return;
+      return {};
+    // EQUIVALENCE members share an allocation; this analysis does not move
+    // whole allocation groups.
     for (const auto &set : mainScope->equivalenceSets())
       for (const auto &object : set)
         if (objects.erase(&object.symbol) && objects.empty())
-          return;
-    // Retain legacy storage for the privatization/locality flags below and for
-    // NAMELIST members throughout MAIN, including unused internal procedures.
-    // This deliberately avoids analyzing individual copy bindings.
-    // LocalityReduce uses the original accumulator, so it remains eligible.
+          return {};
+    // Semantic analysis fixes OpenMP data sharing. This conservative policy
+    // excludes originals with private/locality copies anywhere in MAIN,
+    // including uncalled procedures, to avoid analyzing each copy's storage.
+    // Exclude NAMELIST members without analyzing their group descriptors.
+    // LocalityReduce uses the original accumulator and can use global storage.
     const semantics::Symbol::Flags excluded{
         semantics::Symbol::Flag::OmpPrivate,
         semantics::Symbol::Flag::OmpFirstPrivate,
@@ -298,64 +364,103 @@ class MainGlobalAnalysis {
     llvm::SmallVector<const semantics::Scope *> scopes{mainScope};
     while (!scopes.empty()) {
       const auto *scope = scopes.pop_back_val();
-      LLVM_DEBUG(llvm::dbgs() << "main-global scope\n");
       // This sweep only removes candidates; stop resolving after exhaustion.
       for (const auto &[name, symbol] : *scope)
         if ((symbol->flags() & excluded).any() &&
             objects.erase(root(*symbol)) && objects.empty())
-          return;
+          return {};
       for (const auto &child : scope->children())
         scopes.push_back(&child);
     }
-    for (const auto *symbol : objects)
-      const_cast<semantics::Symbol *>(symbol)->set(
-          semantics::Symbol::Flag::OmpMainGlobal);
+    // When most candidates were rejected, keep a compact table for the
+    // selection that lives throughout lowering.
+    if (objects.size() < candidateCount - objects.size()) {
+      llvm::DenseSet<const semantics::Symbol *> globals(objects.size());
+      globals.insert(objects.begin(), objects.end());
+      return globals;
+    }
+    return std::move(objects);
   }
 
 private:
-  /// Memoize every USE/host association tail across collection and filtering.
-  /// ASSOCIATE entities deliberately retain their own identity.
+  /// Follow USE/host associations, caching shared parts of their chains.
+  /// Return direct objects and ASSOCIATE entities without a cache lookup.
   const semantics::Symbol *root(const semantics::Symbol &symbol) {
     const auto *original = &symbol;
     llvm::SmallVector<const semantics::Symbol *> path;
     while (true) {
-      if (auto it = roots.find(original); it != roots.end()) {
-        original = it->second;
-        break;
-      }
-      path.push_back(original);
-      LLVM_DEBUG(llvm::dbgs() << "main-global association\n");
+      const semantics::Symbol *next;
       if (const auto *details =
               original->detailsIf<semantics::HostAssocDetails>())
-        original = &details->symbol();
+        next = &details->symbol();
       else if (const auto *details =
                    original->detailsIf<semantics::UseDetails>())
-        original = &details->symbol();
+        next = &details->symbol();
       else
         break;
+      if (auto it = roots.find(original); it != roots.end()) {
+        original = it->second;
+        break;
+      }
+      path.push_back(original);
+      original = next;
     }
     for (const auto *entry : path)
       roots.try_emplace(entry, original);
-    LLVM_DEBUG(llvm::dbgs() << "main-global reference: "
-                            << toStringRef(original->name()) << '\n');
     return original;
   }
-  /// Leave arrays, descriptors, derived types, automatic objects and special
-  /// runtime/device storage on their existing lowering paths.
-  static bool canUseGlobal(const semantics::Symbol &symbol) {
+  /// True for procedures owned by intrinsic modules. Cache scope ancestry so
+  /// repeated calls do not walk the same parent scopes again.
+  bool isIntrinsicModuleProcedure(const semantics::Symbol &symbol) {
+    const auto *scope = &root(symbol)->owner();
+    llvm::SmallVector<const semantics::Scope *> path;
+    bool intrinsic = false;
+    while (true) {
+      if (auto it = intrinsicScopes.find(scope); it != intrinsicScopes.end()) {
+        intrinsic = it->second;
+        break;
+      }
+      path.push_back(scope);
+      if (scope->IsModule()) {
+        intrinsic = scope->parent().IsIntrinsicModules();
+        break;
+      }
+      if (scope->IsGlobal() || scope->IsIntrinsicModules())
+        break;
+      scope = &scope->parent();
+    }
+    for (const auto *entry : path)
+      intrinsicScopes.try_emplace(entry, intrinsic);
+    return intrinsic;
+  }
+  /// Allow static storage for nonstatic intrinsic MAIN scalars of 1--32 bytes.
+  /// Descriptor-backed objects, Cray pointers and special allocation rules
+  /// require separate handling. Potential name collisions are excluded too.
+  static bool canUseGlobal(const semantics::Symbol &symbol,
+                           const lower::pft::Program &program) {
+    // FIXME: Teach AddDebugInfo to recognize globals behind fir.volatile_cast
+    // before promoting VOLATILE variables, to avoid duplicate local and global
+    // debug entries.
+    // See https://github.com/llvm/llvm-project/issues/238918.
+    if (symbol.attrs().test(semantics::Attr::VOLATILE))
+      return false;
+    const auto name = toStringRef(symbol.name());
     // Avoid collisions between user globals and generated privatizer names.
     // FIXME: Remove the privatizer exclusions once naming is fixed.
     // See https://github.com/llvm/llvm-project/issues/236705.
-    const auto name = toStringRef(symbol.name());
-    if (name.contains("_private_") || name.contains("_firstprivate_") ||
-        name.contains("_linear_") || name.ends_with("_coarray_handle"))
+    for (llvm::StringRef suffix : lower::privatizerSuffixes)
+      if (name.contains(suffix))
+        return false;
+    // A coarray named x creates a global handle named x_coarray_handle.
+    if (name.ends_with(fir::coarrayHandleSuffix))
       return false;
     const auto *object = symbol.detailsIf<semantics::ObjectEntityDetails>();
     const auto *type = symbol.GetType();
     return object && !object->IsArray() && type && type->AsIntrinsic() &&
            semantics::IsSmallMainProgramVariable(symbol) && symbol.size() &&
            !semantics::IsAllocatableOrPointer(symbol) &&
-           !semantics::IsAutomatic(symbol) && !lower::symbolIsGlobal(symbol) &&
+           !semantics::IsAutomatic(symbol) &&
+           !lower::symbolIsGlobal(symbol, program) &&
            !symbol.test(semantics::Symbol::Flag::OmpThreadprivate) &&
            !symbol.test(semantics::Symbol::Flag::CrayPointer) &&
            !symbol.test(semantics::Symbol::Flag::CrayPointee) &&
@@ -365,12 +470,14 @@ class MainGlobalAnalysis {
                semantics::Symbol::Flag::OmpExecutableAllocateDirective);
   }
   bool enabled;
+  const evaluate::IntrinsicProcTable &intrinsics;
   bool relevant = false;
   bool unsupported = false;
   const semantics::Scope *mainScope = nullptr;
   unsigned parallelDepth = 0;
   llvm::SmallVector<bool> parallels;
   llvm::DenseMap<const semantics::Symbol *, const semantics::Symbol *> roots;
+  llvm::DenseMap<const semantics::Scope *, bool> intrinsicScopes;
   llvm::DenseSet<const semantics::Symbol *> objects;
 };
 
@@ -384,28 +491,30 @@ class PFTBuilder {
       : pgm{std::make_unique<lower::pft::Program>(
             semanticsContext.GetCommonBlocks())},
         semanticsContext{semanticsContext}, loweringOptions{loweringOptions},
-        mainGlobals{loweringOptions.getOpenMPMainProgramGlobals() &&
-                    semanticsContext.languageFeatures().IsEnabled(
-                        common::LanguageFeature::OpenMP) &&
-                    !semanticsContext.langOptions().OpenMPSimd &&
-                    !semanticsContext.langOptions().OpenMPIsTargetDevice &&
-                    // TODO: Support host regions in OpenACC/CUDA units once
-                    // late routine targets and device captures are resolved.
-                    !semanticsContext.languageFeatures().IsEnabled(
-                        common::LanguageFeature::OpenACC) &&
-                    !semanticsContext.languageFeatures().IsEnabled(
-                        common::LanguageFeature::CUDA) &&
-                    !semanticsContext.languageFeatures().IsEnabled(
-                        common::LanguageFeature::SaveMainProgram) &&
-                    !semanticsContext.languageFeatures().IsEnabled(
-                        common::LanguageFeature::DefaultSave)} {
+        mainGlobals{
+            loweringOptions.shouldPromoteOpenMPMainProgramScalars() &&
+                semanticsContext.languageFeatures().IsEnabled(
+                    common::LanguageFeature::OpenMP) &&
+                !semanticsContext.langOptions().OpenMPSimd &&
+                !semanticsContext.langOptions().OpenMPIsTargetDevice &&
+                // TODO: Resolve OpenACC/CUDA device routines before choosing
+                // storage, so host and device procedures use matching captures.
+                !semanticsContext.languageFeatures().IsEnabled(
+                    common::LanguageFeature::OpenACC) &&
+                !semanticsContext.languageFeatures().IsEnabled(
+                    common::LanguageFeature::CUDA) &&
+                !semanticsContext.languageFeatures().IsEnabled(
+                    common::LanguageFeature::SaveMainProgram) &&
+                !semanticsContext.languageFeatures().IsEnabled(
+                    common::LanguageFeature::DefaultSave),
+            semanticsContext.intrinsics()} {
     lower::pft::PftNode pftRoot{*pgm.get()};
     pftParentStack.push_back(pftRoot);
   }
 
   /// Get the result
   std::unique_ptr<lower::pft::Program> result() {
-    mainGlobals.finalize();
+    pgm->setMainGlobals(mainGlobals.finalize(*pgm));
     return std::move(pgm);
   }
 
@@ -455,8 +564,8 @@ class PFTBuilder {
             stmt.unwrapped.u);
       } else if constexpr (std::is_same_v<T, parser::WhereStmt> ||
                            std::is_same_v<T, parser::ForallStmt>) {
-        // Nested single statements have no PFT evaluation. Keep PFT
-        // construction unchanged and collect their expressions here instead.
+        // Nested WHERE/FORALL statements have no separate PFT evaluation, so
+        // collect their expressions while visiting the parse tree.
         mainGlobals.collect(stmt.unwrapped);
       } else if constexpr (std::is_same_v<T, parser::UseStmt>) {
         // Process USE statement for debug info generation
@@ -2103,10 +2212,10 @@ bool Fortran::lower::definedInCommonBlock(const semantics::Symbol &sym) {
 }
 
 /// Is the symbol `sym` a global?
-bool Fortran::lower::symbolIsGlobal(const semantics::Symbol &sym) {
+bool Fortran::lower::symbolIsGlobal(const semantics::Symbol &sym,
+                                    const lower::pft::Program &program) {
   const auto &ultimate = sym.GetUltimate();
-  return ((semantics::IsSaved(ultimate) ||
-           ultimate.test(semantics::Symbol::Flag::OmpMainGlobal)) &&
+  return ((semantics::IsSaved(ultimate) || program.isMainGlobal(ultimate)) &&
           semantics::CanCUDASymbolBeGlobal(sym)) ||
          lower::definedInCommonBlock(sym) || semantics::IsNamedConstant(sym);
 }
@@ -2117,13 +2226,17 @@ namespace {
 /// preserves the order of the symbol table, which is sorted by name. This
 /// analysis may also be done for an individual symbol.
 struct SymbolDependenceAnalysis {
-  explicit SymbolDependenceAnalysis(const semantics::Scope &scope) {
+  explicit SymbolDependenceAnalysis(const semantics::Scope &scope,
+                                    const lower::pft::Program &program)
+      : program{program} {
     analyzeEquivalenceSets(scope);
     for (const auto &iter : scope)
       analyze(iter.second.get());
     finalize();
   }
-  explicit SymbolDependenceAnalysis(const semantics::Symbol &symbol) {
+  explicit SymbolDependenceAnalysis(const semantics::Symbol &symbol,
+                                    const lower::pft::Program &program)
+      : program{program} {
     analyzeEquivalenceSets(symbol.owner());
     analyze(symbol);
     finalize();
@@ -2131,7 +2244,9 @@ struct SymbolDependenceAnalysis {
   /// Analyze the dependencies of a set of module variables that are host
   /// associated (or use associated in host module scopes).
   explicit SymbolDependenceAnalysis(
-      const llvm::SetVector<const semantics::Symbol *> &moduleVariables) {
+      const llvm::SetVector<const semantics::Symbol *> &moduleVariables,
+      const lower::pft::Program &program)
+      : program{program} {
     for (const semantics::Symbol *sym : moduleVariables)
       analyzeLocalEquivalenceSets(sym->owner());
     // Add all aggregate stores to the front of the variable list.
@@ -2204,7 +2319,7 @@ struct SymbolDependenceAnalysis {
         if (sym.test(Fortran::semantics::Symbol::Flag::CompilerCreated)) {
           aggregateSym = &sym;
         } else {
-          isGlobal |= lower::symbolIsGlobal(sym);
+          isGlobal |= lower::symbolIsGlobal(sym, program);
           start = std::min(sym.offset(), start);
           end = std::max(sym.offset() + sym.size(), end);
           if (!namingSym || (sym.name() < namingSym->name()))
@@ -2313,7 +2428,7 @@ struct SymbolDependenceAnalysis {
       depth = std::max(
           analyze(Fortran::semantics::GetCrayPointer(ultimate)) + 1, depth);
     adjustSize(depth + 1);
-    bool global = lower::symbolIsGlobal(sym);
+    bool global = lower::symbolIsGlobal(sym, program);
     layeredVarList[depth].emplace_back(sym, global, depth);
     if (semantics::IsAllocatable(sym))
       layeredVarList[depth].back().setHeapAlloc();
@@ -2391,6 +2506,8 @@ struct SymbolDependenceAnalysis {
                                layeredVarList[i].end());
   }
 
+  /// The PFT owns the storage choices and must outlive this analysis.
+  const lower::pft::Program &program;
   llvm::SmallPtrSet<const semantics::Symbol *, 32> seen;
   std::vector<Fortran::lower::pft::VariableList> layeredVarList;
   llvm::SmallPtrSet<const semantics::Symbol *, 32> aliasSyms;
@@ -2455,6 +2572,13 @@ Fortran::lower::pft::FunctionLikeUnit::FunctionLikeUnit(
   scope = symbol->scope();
 }
 
+void Fortran::lower::pft::FunctionLikeUnit::setHostAssociatedSymbols(
+    const llvm::SetVector<const semantics::Symbol *> &symbols) {
+  Program *program = getAncestor<Program>(*this);
+  assert(program && "pft must have a root");
+  hostAssociations.addSymbolsToBind(symbols, getScope(), *program);
+}
+
 Fortran::lower::HostAssociations &
 Fortran::lower::pft::FunctionLikeUnit::parentHostAssoc() {
   if (auto *par = parent.getIf<FunctionLikeUnit>())
@@ -2623,22 +2747,19 @@ void Fortran::lower::pft::BlockDataUnit::dump() const {
   llvm::errs() << "block data {\n" << symTab << "\n}\n";
 }
 
-/// Find or create an ordered list of equivalences and variables in \p scope.
-/// The result is cached in \p map.
+/// Find or create ordered records using this Program's storage selection.
 const lower::pft::VariableList &
-lower::pft::getScopeVariableList(const semantics::Scope &scope,
-                                 ScopeVariableListMap &map) {
+lower::pft::Program::getScopeVariableList(const semantics::Scope &scope) {
   LLVM_DEBUG(
       llvm::dbgs() << "\ngetScopeVariableList of "
                    << (scope.kind() == semantics::Scope::Kind::MainProgram
                            ? "main program"
                            : "[sub]module")
                    << " scope <" << &scope << "> " << scope.GetName() << "\n");
-  auto iter = map.find(&scope);
-  if (iter == map.end()) {
-    SymbolDependenceAnalysis sda(scope);
-    map.emplace(&scope, sda.getVariableList());
-    iter = map.find(&scope);
+  auto iter = scopeVariableListMap.find(&scope);
+  if (iter == scopeVariableListMap.end()) {
+    SymbolDependenceAnalysis sda(scope, *this);
+    iter = scopeVariableListMap.emplace(&scope, sda.getVariableList()).first;
   }
   return iter->second;
 }
@@ -2646,21 +2767,23 @@ lower::pft::getScopeVariableList(const semantics::Scope &scope,
 /// Create an ordered list of equivalences and variables in \p scope.
 /// The result is not cached.
 lower::pft::VariableList
-lower::pft::getScopeVariableList(const semantics::Scope &scope) {
+lower::pft::getScopeVariableList(const semantics::Scope &scope,
+                                 const Program &program) {
   LLVM_DEBUG(
       llvm::dbgs() << "\ngetScopeVariableList of [sub]program|block scope <"
                    << &scope << "> " << scope.GetName() << "\n");
-  SymbolDependenceAnalysis sda(scope);
+  SymbolDependenceAnalysis sda(scope, program);
   return sda.getVariableList();
 }
 
 /// Create an ordered list of equivalences and variables that \p symbol
 /// depends on (no caching). Include \p symbol at the end of the list.
 lower::pft::VariableList
-lower::pft::getDependentVariableList(const semantics::Symbol &symbol) {
+lower::pft::getDependentVariableList(const semantics::Symbol &symbol,
+                                     const Program &program) {
   LLVM_DEBUG(llvm::dbgs() << "\ngetDependentVariableList of " << &symbol
                           << " - " << symbol << "\n");
-  SymbolDependenceAnalysis sda(symbol);
+  SymbolDependenceAnalysis sda(symbol, program);
   return sda.getVariableList();
 }
 
@@ -2715,25 +2838,56 @@ lower::pft::getHostModuleVariableList(const FunctionLikeUnit &funit) {
                           << funit.getScope().GetName() << "\n");
   llvm::SetVector<const semantics::Symbol *> moduleVariables;
   collectHostAssociatedModuleVariables(funit, moduleVariables);
-  SymbolDependenceAnalysis sda(moduleVariables);
+  const Program *program = getAncestor<Program>(funit);
+  assert(program && "pft must have a root");
+  SymbolDependenceAnalysis sda(moduleVariables, *program);
   return sda.getVariableList();
 }
 
 namespace {
-/// Helper class to find all the symbols referenced in a FunctionLikeUnit.
-/// It defines a parse tree visitor doing a deep visit in all nodes with
-/// symbols (including evaluate::Expr).
+
+/// Visit symbols in source syntax, analyzed expressions and their dependencies.
 struct SymbolVisitor {
+  /// Callbacks must remain valid during the walk. The hook may handle a node;
+  /// returning false skips its default traversal.
+  explicit SymbolVisitor(
+      llvm::function_ref<void(const semantics::Symbol &)> callback,
+      llvm::function_ref<bool(SymbolVisit)> preVisit = {})
+      : callback{callback}, preVisit{preVisit} {}
+
   template <typename A>
   bool Pre(const A &x) {
-    if constexpr (Fortran::parser::HasTypedExpr<A>::value)
+    if constexpr (Fortran::parser::HasTypedExpr<A>::value) {
       // Some parse tree Expr may legitimately be un-analyzed after semantics
       // (for instance PDT component initial value in the PDT definition body).
-      if (const auto *expr = Fortran::semantics::GetExpr(nullptr, x))
+      if (const auto *expr = Fortran::semantics::GetExpr(nullptr, x)) {
+        if (!enter(expr))
+          return false;
         visitExpr(*expr);
+      }
+    } else if constexpr (Fortran::parser::HasTypedAssignment<A>::value) {
+      // Let the hook handle the analyzed assignment. If it does, skip
+      // source operands, which may include names the hook excludes.
+      if (preVisit && x.typedAssignment && x.typedAssignment->v &&
+          !enter(&*x.typedAssignment->v))
+        return false;
+    }
     return true;
   }
 
+  /// With a hook, offer the analyzed call or skip it if analysis is absent.
+  bool Pre(const parser::CallStmt &call) {
+    if (preVisit && (!call.typedCall || !enter(&*call.typedCall)))
+      return false;
+    return Pre<parser::CallStmt>(call);
+  }
+
+  /// Let the hook skip SHARED locality names, which need no value evaluation.
+  bool Pre(const parser::LocalitySpec::Shared &shared) {
+    return enter(SymbolVisitKind::SharedLocality) &&
+           Pre<parser::LocalitySpec::Shared>(shared);
+  }
+
   bool Pre(const Fortran::parser::Name &name) {
     if (const semantics::Symbol *symbol = name.symbol)
       visitSymbol(*symbol);
@@ -2767,7 +2921,9 @@ struct SymbolVisitor {
   }
 
   void visitSymbol(const Fortran::semantics::Symbol &symbol) {
-    callBack(symbol);
+    callback(symbol);
+    if (!enter(SymbolVisitKind::Dependencies))
+      return;
     // - Visit statement function body since it will be inlined in lowering.
     // - Visit function results specification expressions because allocations
     //   happens on the caller side.
@@ -2817,25 +2973,37 @@ struct SymbolVisitor {
   template <typename A>
   constexpr void Post(const A &) {}
 
-  const std::function<void(const Fortran::semantics::Symbol &)> &callBack;
+private:
+  /// Allow default traversal unless the hook handles or rejects the node.
+  bool enter(SymbolVisit node) const { return !preVisit || preVisit(node); }
+
+  llvm::function_ref<void(const semantics::Symbol &)> callback;
+  llvm::function_ref<bool(SymbolVisit)> preVisit;
 };
+
+template <typename A>
+void visitSymbols(const A &node,
+                  llvm::function_ref<void(const semantics::Symbol &)> callback,
+                  llvm::function_ref<bool(SymbolVisit)> preVisit) {
+  SymbolVisitor visitor{callback, preVisit};
+  parser::Walk(node, visitor);
+}
+
 } // namespace
 
 void Fortran::lower::pft::visitAllSymbols(
     const Fortran::lower::pft::FunctionLikeUnit &funit,
     const std::function<void(const Fortran::semantics::Symbol &)> callBack) {
-  SymbolVisitor visitor{callBack};
   funit.visit([&](const auto &functionParserNode) {
-    parser::Walk(functionParserNode, visitor);
+    visitSymbols(functionParserNode, callBack);
   });
 }
 
 void Fortran::lower::pft::visitAllSymbols(
     const Fortran::lower::pft::Evaluation &eval,
     const std::function<void(const Fortran::semantics::Symbol &)> callBack) {
-  SymbolVisitor visitor{callBack};
   eval.visit([&](const auto &functionParserNode) {
-    parser::Walk(functionParserNode, visitor);
+    visitSymbols(functionParserNode, callBack);
   });
 }
 
diff --git a/flang/lib/Lower/Support/Utils.cpp b/flang/lib/Lower/Support/Utils.cpp
index fafa8cbe08ee60..ac85189e4cf13a 100644
--- a/flang/lib/Lower/Support/Utils.cpp
+++ b/flang/lib/Lower/Support/Utils.cpp
@@ -24,6 +24,7 @@
 #include "flang/Semantics/tools.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include <cstdint>
+#include <iterator>
 #include <optional>
 #include <type_traits>
 
@@ -776,11 +777,18 @@ void privatizeSymbol(
                              isDynamicallySizedBoxArrayType(allocType);
 
   OpType privatizerOp = [&]() {
+    // Privatization kind encoded in the generated name.
+    enum class PrivatizerKind { Private, FirstPrivate, Linear, Count };
+    static_assert(std::size(privatizerSuffixes) ==
+                  static_cast<unsigned>(PrivatizerKind::Count));
     auto moduleOp = firOpBuilder.getModule();
-    std::string privatizerPrefix =
-        converter.mangleName(*sym) +
-        (emitCopyRegion ? "_firstprivate"
-                        : (isLinear ? "_linear" : "_private"));
+    PrivatizerKind kind = emitCopyRegion ? PrivatizerKind::FirstPrivate
+                          : isLinear     ? PrivatizerKind::Linear
+                                         : PrivatizerKind::Private;
+    std::string privatizerPrefix = converter.mangleName(*sym);
+    llvm::StringRef suffix =
+        privatizerSuffixes[static_cast<unsigned>(kind)].drop_back();
+    privatizerPrefix.append(suffix.data(), suffix.size());
     if (forceHeapAllocation)
       privatizerPrefix += "_heap";
     auto uniquePrivatizerName = fir::getTypeAsString(
diff --git a/flang/lib/Optimizer/Transforms/MIFOpConversion.cpp b/flang/lib/Optimizer/Transforms/MIFOpConversion.cpp
index 9f3a701571f315..f9429c0cec9adb 100644
--- a/flang/lib/Optimizer/Transforms/MIFOpConversion.cpp
+++ b/flang/lib/Optimizer/Transforms/MIFOpConversion.cpp
@@ -123,7 +123,7 @@ mlir::Value getCoarrayHandle(fir::FirOpBuilder &builder, mlir::Location loc,
   mlir::Type handleTy = getCoarrayHandleType(builder, loc);
   std::string uniqName = mif::getFullUniqName(coarray);
   if (!uniqName.empty()) {
-    std::string globalName = uniqName + coarrayHandleSuffix.str();
+    std::string globalName = uniqName + fir::coarrayHandleSuffix.str();
     mlir::SymbolRefAttr symAttr =
         mlir::SymbolRefAttr::get(builder.getContext(), globalName);
     mlir::Value coarrayHandle = fir::AddrOfOp::create(
@@ -139,7 +139,7 @@ mlir::Value getCoarrayHandle(fir::FirOpBuilder &builder, mlir::Location loc,
 void storeCoarrayHandle(fir::FirOpBuilder &builder, mlir::Location loc,
                         mlir::Value coarrayHandle, std::string uniqName) {
   mlir::Type handleTy = getCoarrayHandleType(builder, loc);
-  std::string globalName = uniqName + coarrayHandleSuffix.str();
+  std::string globalName = uniqName + fir::coarrayHandleSuffix.str();
   fir::GlobalOp global = builder.getNamedGlobal(globalName);
   if (!global) {
     global = builder.createGlobal(loc, handleTy, globalName,
@@ -1681,7 +1681,7 @@ static void genCoarrayHandle(fir::FirOpBuilder &builder, mlir::ModuleOp mod,
     if (boxTy.isCoarray()) {
       mlir::Type handleTy = getCoarrayHandleType(builder, loc);
       std::string globalName =
-          op.getUniqName().str() + coarrayHandleSuffix.str();
+          op.getUniqName().str() + fir::coarrayHandleSuffix.str();
       if (builder.getNamedGlobal(globalName))
         return;
       fir::GlobalOp global = builder.createGlobal(
diff --git a/flang/test/Lower/OpenMP/main-program-globals-cost.f90 b/flang/test/Lower/OpenMP/main-program-globals-cost.f90
index f91cbe05344865..a7d51beda374a5 100644
--- a/flang/test/Lower/OpenMP/main-program-globals-cost.f90
+++ b/flang/test/Lower/OpenMP/main-program-globals-cost.f90
@@ -1,48 +1,10 @@
-! Check reuse of storage-selection and host-binding analysis results. Trace
-! checks require assertions; the IR checks also run without the asserts feature.
 ! REQUIRES: module-independent
 ! RUN: split-file %s %t
-! RUN: %if asserts %{ bbc -fopenmp -pft-test -debug-only=flang-pft %t/saved.f90 2>&1 | FileCheck %s --check-prefix=OFF %}
-! RUN: %flang_fc1 -fopenmp -fno-automatic -emit-hlfir %if asserts %{ -mllvm -debug-only=flang-pft %} %t/nested.f90 -o %t.auto > %t.auto-trace 2>&1
-! RUN: %if asserts %{ FileCheck %s --check-prefix=OFF --allow-empty < %t.auto-trace %}
-! RUN: FileCheck %s --check-prefix=SAVED < %t.auto
-! RUN: %if asserts %{ bbc -fopenmp -pft-test -debug-only=flang-pft %t/pruned.f90 2>&1 | FileCheck %s --check-prefix=PRUNED %}
-! RUN: %if asserts %{ bbc -fopenmp -pft-test -debug-only=flang-pft %t/nested.f90 2>&1 | FileCheck %s --check-prefix=EXPR %}
-! RUN: %if asserts %{ bbc -fopenmp -fno-openmp-main-program-globals -pft-test -debug-only=flang-pft %t/nested.f90 2>&1 | FileCheck %s --check-prefix=OFF %}
-! RUN: %flang_fc1 -fopenmp -emit-hlfir -module-dir %t %if asserts %{ -mllvm -debug-only=flang-lower-bridge,flang-lower-variable,flang-pft,flang-host-assoc %} %t/owner.f90 -o %t.owner > %t.trace 2>&1
+! RUN: %flang_fc1 -fopenmp -emit-hlfir -module-dir %t %t/owner.f90 -o %t.owner
 ! RUN: FileCheck %s --check-prefix=IR < %t.owner
 ! RUN: FileCheck %s --check-prefix=UNUSED < %t.owner
-! RUN: %if asserts %{ FileCheck %s --check-prefix=STORE < %t.trace %}
-! RUN: %if asserts %{ FileCheck %s --check-prefix=CACHE < %t.trace %}
-! RUN: %if asserts %{ FileCheck %s --check-prefix=ROOTS < %t.trace %}
-! RUN: %if asserts %{ FileCheck %s --check-prefix=TAILS < %t.trace %}
-! RUN: %flang_fc1 -fopenmp -fsave-main-program -emit-hlfir %if asserts %{ -mllvm -debug-only=flang-pft %} %t/nested.f90 -o %t.saved > %t.save-trace 2>&1
-! RUN: %if asserts %{ FileCheck %s --check-prefix=OFF --allow-empty < %t.save-trace %}
-! RUN: FileCheck %s --check-prefix=SAVED < %t.saved
-! SAVED-DAG: fir.global internal @_QFEsink : i32
-! SAVED-DAG: fir.global internal @_QFEx1 : i32
-! EXPR-COUNT-18: main-global symbol
-! EXPR-NOT: main-global symbol
-! EXPR: Program
-! EXPR-NOT: main-global symbol
-! OFF-NOT: {{^main-global }}
-! CACHE-NOT: getScopeVariableList of [sub]program|block scope {{.*}} P{{$}}
-! CACHE-NOT: getScopeVariableList of main program scope
-! CACHE: getScopeVariableList of main program scope
-! CACHE-NOT: getScopeVariableList of main program scope
-! CACHE-NOT: getScopeVariableList of [sub]program|block scope {{.*}} P{{$}}
-! STORE: [bridge - startNewFunction] worker
-! STORE-NOT: instantiateVariable: interval{{.*}} c,
-! STORE-DAG: instantiateVariable: interval{{.*}} a, global
-! STORE-DAG: instantiateVariable: interval{{.*}} alias, global
-! STORE-NOT: instantiateVariable: interval
-! STORE: [bridge - endNewFunction
-! STORE: [bridge - startNewFunction] other
-! STORE-NOT: instantiateVariable: interval{{.*}} c,
-! STORE-DAG: instantiateVariable: interval{{.*}} a, global
-! STORE-DAG: instantiateVariable: interval{{.*}} alias, global
-! STORE-NOT: instantiateVariable: interval
-! STORE: [bridge - endNewFunction
+! RUN: FileCheck %s --check-prefix=CALLS --implicit-check-not='fir.alloca tuple' < %t.owner
+! CALLS-COUNT-2: fir.global internal @_QFE{{(first|second)}} : i32
 ! IR-LABEL: func.func private @_QFPworker()
 ! IR-DAG: fir.address_of(@_QFEalias)
 ! IR-DAG: fir.address_of(@_QFEx)
@@ -50,77 +12,43 @@
 ! IR: return
 ! IR-LABEL: func.func private @_QFPother()
 ! IR-DAG: %[[MODULE:[^ ]+]] = fir.address_of(@_QMneededEa)
+! IR-DAG: %[[DEEP_ADDR:[^ ]+]] = fir.address_of(@_QMtail0Ecaptured)
+! IR-DAG: %[[DEEP:[^: ]+]]:2 = hlfir.declare %[[DEEP_ADDR]] {{.*}}uniq_name("_QMtail0Ecaptured")
 ! IR-DAG: %[[COMMON:[^ ]+]] = fir.address_of(@block_)
 ! IR-DAG: %[[USED:[^ ]+]]:2 = hlfir.declare {{.*}} storage(%[[MODULE]][0]) uniq_name("_QMneededEb")
 ! IR-DAG: %[[SHARED:[^ ]+]]:2 = hlfir.declare {{.*}} storage(%[[COMMON]][0]) uniq_name("_QFEshared")
 ! IR: omp.parallel
 ! IR-DAG: fir.load %[[USED]]#0 :
 ! IR-DAG: fir.load %[[SHARED]]#0 :
+! IR-DAG: fir.load %[[DEEP]]#0 :
 ! IR: return
 ! UNUSED-LABEL: func.func private @_QFPworker()
-! UNUSED-NOT: fir.address_of(@_QM{{(neededEc|tail0E)}}
+! UNUSED-NOT: fir.address_of(@_QM{{(neededEc|tail0Ev)}}
+! UNUSED-NOT: hlfir.declare {{.*}}uniq_name("_QMneededEa")
 ! UNUSED: return
 ! UNUSED-LABEL: func.func private @_QFPother()
-!--- nested.f90
-! Visit nested operands once; SAVE policies and the opt-out bypass selection.
-program p
-integer :: x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,sink
-!$omp parallel
-if (x1>0) sink=x1+(x2+(x3+(x4+(x5+(x6+(x7+(x8+( &
-     x9+(x10+(x11+(x12+(x13+(x14+(x15+x16))))))))))))))
-!$omp end parallel
-end program
 !--- owner.f90
-! Reuse the MAIN variable list and shared USE tails. Bind only the required
-! globals and EQUIVALENCE/COMMON stores in each internal procedure.
+! Internal procedures bind used globals and shared EQUIVALENCE/COMMON storage.
 module needed
   integer :: a,b,c,d; equivalence(a,b),(c,d)
+contains
+integer function id(n); integer :: n; id=n; end function
 end module
-module tail0; integer :: v; end module; module tail1; use tail0, only: v; end module
-module tail2; use tail1, only: v; end module; module tail3; use tail2, only: v; end module
+module tail0; integer :: v,captured; end module; module tail1; use tail0, only: v,captured; end module
+module tail2; use tail1, only: v,captured; end module; module tail3; use tail2, only: v,captured; end module
 program p
-  use needed, only: unused=>a,used=>b
-  use tail3, only: a1=>v,a2=>v,a3=>v,a4=>v
+  use needed, only: unused=>a,used=>b,id
+  use tail3, only: a1=>v,a2=>v,a3=>v,a4=>v,deep=>captured
   integer,save :: backing(2),alias; equivalence(backing(1),alias)
-  integer :: x,shared; common /block/ shared
-  alias=3; x=7; used=11; shared=13; call worker(); call other()
+  integer :: x,first,second,shared; common /block/ shared
+  alias=3; x=7; used=11; shared=13; first=1; second=2; deep=17; call worker(); call other()
 contains
   subroutine worker()
 !$omp parallel num_threads(1)
-    print *,x,alias
+    print *,x,alias,id(first),id(second),id(first)
 !$omp end parallel
   end subroutine; subroutine other()
 !$omp parallel num_threads(1)
-    print *,x,alias,used,shared
+    print *,x,alias,used,shared,deep
 !$omp end parallel
   end subroutine; end program
-! Four MAIN entities, one module object and six OpenMP proxies are resolved once.
-! ROOTS-COUNT-11: main-global association
-! ROOTS-NOT: main-global association
-! Four renames share three USE edges; two needed aliases add two links.
-! TAILS-COUNT-9: main-global binding association
-! TAILS-NOT: main-global binding association
-! PRUNED-NOT: main-global reference: image
-! PRUNED: main-global reference: direct
-! PRUNED-NOT: main-global reference: image
-! PRUNED: Program
-! PRUNED-NOT: main-global reference: image
-!--- pruned.f90
-! Do not select the coarray image selector; direct remains a candidate.
-program p; type t(k)
-    integer,kind :: k=4; integer :: payload=1
-  end type; type(t),parameter :: c=t()
-  integer :: direct,image,co[*]
-!$omp parallel
-  print *,direct,co[image],c
-!$omp end parallel
-end program
-!--- saved.f90
-! A bare SAVE makes the new storage-selection analysis unnecessary.
-program p
-  integer :: x
-  save
-!$omp parallel
-  x=1
-!$omp end parallel
-end program
diff --git a/flang/test/Lower/OpenMP/main-program-globals-fallback.f90 b/flang/test/Lower/OpenMP/main-program-globals-fallback.f90
index 7e4d0b40651750..94facece986f87 100644
--- a/flang/test/Lower/OpenMP/main-program-globals-fallback.f90
+++ b/flang/test/Lower/OpenMP/main-program-globals-fallback.f90
@@ -14,6 +14,8 @@
 ! RUN: %flang_fc1 -fopenmp -fdo-concurrent-to-openmp=device -fopenmp-is-target-device -fopenmp-targets=nvptx64-nvidia-cuda -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=DEVICE --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
 ! RUN: bbc -fopenmp -fdo-concurrent-to-openmp=device -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=DEVICE --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
 ! RUN: %flang_fc1 -fopenmp -fdo-concurrent-to-openmp=host -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=HOST
+! RUN: %flang_fc1 -fopenmp -cpp -DEARLY_TARGET -DDIRECTIVE=barrier -emit-hlfir %t/internal.f90 -o - | FileCheck %s --check-prefix=KEEP --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
+! RUN: bbc -fopenmp -fdo-concurrent-to-openmp=host -emit-fir %t/generated-target.f90 -o - | FileCheck %s --check-prefix=HOST
 ! KEEP: fir.alloca i32 {{.*}}bindc_name = "x"
 ! EXTERNAL-LABEL: func.func @_QQmain()
 ! EXTERNAL: fir.address_of(@_QFEx)
@@ -21,25 +23,31 @@
 ! EXTERNAL: omp.target_data
 ! GLOBAL: fir.global internal @_QFEx : i32
 !--- internal.f90
-! Each CPP offload variant independently preserves MAIN storage when the
-! directive is in an internal procedure. BARRIER is the host-only control.
+! Offload before or inside PARALLEL keeps x on the stack; BARRIER alone does not.
 program p
-  integer :: x
+  integer :: x,marker
+#ifdef EARLY_TARGET
+!$omp declare target(marker)
+#endif
 !$omp parallel
   print *,x
 !$omp end parallel
   call worker()
 contains
   subroutine worker()
+#ifdef REGION
+!$omp parallel
+#endif
 !$omp DIRECTIVE
 #ifdef REGION
     x=1
 !$omp END_DIRECTIVE
+!$omp end parallel
 #endif
   end subroutine
 end program
 !--- meta.f90
-! Even a metadirective selecting NOTHING currently preserves MAIN storage.
+! A metadirective selecting NOTHING keeps x on the stack.
 program p
   integer :: x
 !$omp parallel
@@ -68,15 +76,14 @@ subroutine outside()
   a=1
 !$omp end target data
 end subroutine
-! Device DO CONCURRENT keeps the host tuple and maps it into the target. Host
-! mapping still permits globals and removes the unused tuple argument from f.
+! Device mapping passes the host tuple to f; host mapping uses global x.
 ! DEVICE-LABEL: func.func @_QQmain()
 ! DEVICE: fir.alloca i32 {{.*}}bindc_name = "x"
 ! DEVICE: %[[HOST_TUPLE:[^ ]+]] = fir.alloca tuple<!fir.ref<i32>>
 ! DEVICE: %[[TUPLE_MAP:[^ ]+]] = omp.map.info var_ptr(%[[HOST_TUPLE]] : !fir.ref<tuple<!fir.ref<i32>>>
 ! DEVICE: omp.target
-! DEVICE-SAME: map_entries({{.*}}%[[TUPLE_MAP]] -> %[[MAPPED_TUPLE:[^, ]+]]{{.*}} : !fir.ref<index>, !fir.ref<index>, !fir.ref<index>, !fir.ref<i32>, !fir.ref<tuple<!fir.ref<i32>>>, !fir.ref<!fir.array<4xi32>>, !fir.ref<index>)
-! DEVICE: %[[TUPLE:[^ ]+]] = fir.declare %[[MAPPED_TUPLE]] uniq_name("") : (!fir.ref<tuple<!fir.ref<i32>>>)
+! DEVICE-SAME: map_entries({{.*}}%[[TUPLE_MAP]] -> %[[MAPPED_TUPLE:[^, )]+]]{{[, )]}}
+! DEVICE: %[[TUPLE:[^ ]+]] = fir.declare %[[MAPPED_TUPLE]] {{.*}} : (!fir.ref<tuple<!fir.ref<i32>>>)
 ! DEVICE: fir.call @_QFPf({{[^,]*}}, %[[TUPLE]])
 ! DEVICE-LABEL: func.func private @_QFPf(
 ! DEVICE-SAME: !fir.ref<tuple<!fir.ref<i32>>>
@@ -84,7 +91,7 @@ subroutine outside()
 ! HOST: fir.address_of(@_QFEx)
 ! HOST: fir.call @_QFPf({{[^,]*}})
 ! HOST-LABEL: func.func private @_QFPf(
-! HOST-SAME: !fir.ref<i32> {{.*}}) -> i32
+! HOST-SAME: %{{[^: ]+}}: !fir.ref<i32>{{( \{[^{}]*\})?}}) -> i32
 ! HOST: fir.global internal @_QFEx
 !--- generated-target.f90
 program p
@@ -102,12 +109,11 @@ program p
       a(i)=v
     end block
   end do
-  if(any(a/=[2,3,4,5])) stop 1
   call host_use()
 contains
   pure integer function f(j)
     integer,intent(in) :: j
-    f=j+1
+    f=j+x
   end function
   subroutine host_use()
     print *,x
diff --git a/flang/test/Lower/OpenMP/main-program-globals.f90 b/flang/test/Lower/OpenMP/main-program-globals.f90
index bbb1205c162db4..7e6d29209da740 100644
--- a/flang/test/Lower/OpenMP/main-program-globals.f90
+++ b/flang/test/Lower/OpenMP/main-program-globals.f90
@@ -1,55 +1,59 @@
 ! REQUIRES: module-independent
 ! RUN: split-file %s %t
+! RUN: %if flangrt-modules %{ %flang_fc1 -fopenmp -emit-hlfir %t/intrinsics.f90 -o - | FileCheck %s --check-prefix=INTRINSICS-ON %}
+! RUN: %if flangrt-modules %{ %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/intrinsics.f90 -o - | FileCheck %s --check-prefix=INTRINSICS-OFF --implicit-check-not='fir.global internal @_QFE' %}
 ! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/miniweather.f90 -o %t.on
-! RUN: FileCheck %s --check-prefix=ON < %t.on
+! RUN: FileCheck %s --check-prefixes=ON,ZERO < %t.on
+! RUN: %flang_fc1 -fopenmp -fno-init-global-zero -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefixes=ON,UNDEF
 ! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.alloca tuple' < %t.on
 ! RUN: bbc -fopenmp -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=ON
 ! RUN: bbc -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
 ! RUN: %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
 ! RUN: %flang_fc1 -fopenmp -fopenmp-is-target-device -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE'
-! RUN: %flang_fc1 -fopenmp-simd -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE'
+! RUN: %flang_fc1 -fopenmp-simd -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
 ! RUN: %flang_fc1 -fopenmp -fopenacc -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
 ! RUN: %flang_fc1 -fopenmp -x cuda -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
 ! RUN: bbc -fopenmp -fopenacc -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
 ! RUN: bbc -fopenmp -fcuda -emit-hlfir %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OFF --implicit-check-not='fir.global internal @_QFE'
-! RUN: %if aarch64-registered-target %{ %flang_fc1 -triple aarch64-unknown-linux-gnu -O3 -ffast-math -fopenmp -emit-llvm %t/miniweather.f90 -o - | FileCheck %s --check-prefix=OPT %}
-! RUN: %if asserts %{ %flang_fc1 -fopenmp -fdebug-dump-pft -mllvm -debug-only=flang-pft %t/miniweather.f90 2>&1 | FileCheck %s --check-prefix=PFT_ON %}
-! RUN: %if asserts %{ %flang_fc1 -fopenmp -fno-openmp-main-program-globals -fdebug-dump-pft -mllvm -debug-only=flang-pft %t/miniweather.f90 2>&1 | FileCheck %s --check-prefix=PFT_OFF %}
 ! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/objects.f90 -o %t.objects
 ! RUN: FileCheck %s --check-prefix=OBJECTS < %t.objects
-! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(array|matrix|storage|i|j|ap|ptr|record|private_value|listed|eq1|eq2|x|y|serial_only|x_private_i32|y_firstprivate_i32|x_linear_i32|allocated|address|pointee|actual|element|extent|rhs|field)[[:space:]]}}' < %t.objects
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(array|matrix|storage|i|j|ap|ptr|record|private_value|listed|eq1|eq2|x|y|serial_only|x_private_i32|y_firstprivate_i32|x_linear_i32|allocated|address|pointee|element|extent|field|lastprivate_value|local_value|init_value)[[:space:]]}}' < %t.objects
 ! RUN: %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/objects.f90 -o %t.off
 ! RUN: FileCheck %s --check-prefix=BOUNDARY < %t.off
 ! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(text|if_condition|if_action|where_value|forall_value)[[:space:]]}}' < %t.off
 ! RUN: FileCheck %s --check-prefix=EXPRESSIONS < %t.objects
-! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(actual|element|extent|rhs|field)[[:space:]]}}' < %t.objects
+! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(element|extent|field|clause_only|header_only|statement_only)[[:space:]]}}' < %t.objects
 ! RUN: %flang_fc1 -emit-hlfir %t/objects.f90 -o %t.serial
 ! RUN: FileCheck %s --check-prefix=BOUNDARY < %t.serial
 ! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFE{{(integer_value|real_value|complex_value|logical_value|text|if_condition|if_action|where_value|forall_value|direct)[[:space:]]}}' < %t.serial
-! RUN: %flang_fc1 -fopenmp -mllvm -strict-fir-volatile-verifier -emit-hlfir %t/mixed.f90 -o %t.mixed
+! RUN: %flang_fc1 -fopenmp -mmlir -strict-fir-volatile-verifier -emit-hlfir %t/mixed.f90 -o %t.mixed
 ! RUN: FileCheck %s --check-prefix=MIXED < %t.mixed
 ! RUN: FileCheck %s --check-prefix=PROPERTIES < %t.mixed
 ! RUN: %flang_fc1 -fopenmp -emit-hlfir %t/callbacks.f90 -o %t.callbacks
 ! RUN: FileCheck %s --check-prefix=CALLBACK < %t.callbacks
 ! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.alloca tuple' --implicit-check-not='fir.host_assoc' < %t.callbacks
 ! RUN: %flang_fc1 -fopenmp -fno-openmp-main-program-globals -emit-hlfir %t/callbacks.f90 -o - | FileCheck %s --check-prefix=BOX_OFF --implicit-check-not='fir.global internal @_QFEx{{[[:space:]]}}'
-! RUN: %flang_fc1 -fopenmp -emit-fir %t/callbacks.f90 -o - | fir-opt --flang-licm | FileCheck %s --check-prefix=LICM
 ! RUN: %flang_fc1 -fopenmp -fcoarray -emit-hlfir %t/coarray.f90 -o %t.on
 ! RUN: FileCheck %s --check-prefix=COARRAY < %t.on
 ! RUN: FileCheck %s --check-prefix=MODULE --implicit-check-not='fir.global internal @_QFEa_coarray_handle{{[[:space:]]}}' < %t.on
 ! RUN: %flang_fc1 -fopenmp -fcoarray -emit-llvm %t/coarray.f90 -o - | FileCheck %s --check-prefix=LLVM
 ! MODULE: module
 
-! Check globals and removal of the host tuple for MAIN scalars referenced in an
-! internal procedure's PARALLEL region. Disabled modes retain legacy storage;
-! OPT checks reciprocal computation before the outlined loop.
+! MAIN scalars used in worker's PARALLEL region use globals and need no host tuple.
 ! ON-LABEL: func.func @_QQmain()
+! ON-DAG: %[[MAIN_ADDR:[^ ]+]] = fir.address_of(@_QFEdx)
+! ON-DAG: %[[MAIN_X:[^: ]+]]:2 = hlfir.declare %[[MAIN_ADDR]] {{.*}}uniq_name("_QFEdx")
+! ON: fir.call @_FortranAioInputReal64(%{{[^,]+}}, %[[MAIN_X]]#0)
 ! ON: fir.call @_QFPworker()
 ! ON-LABEL: func.func private @_QFPworker()
-! ON-DAG: fir.address_of(@_QFEdx)
+! ON-DAG: %[[X_ADDR:[^ ]+]] = fir.address_of(@_QFEdx)
+! ON-DAG: %[[X:[^: ]+]]:2 = hlfir.declare %[[X_ADDR]] {{.*}}uniq_name("_QFEdx")
 ! ON-DAG: fir.address_of(@_QFEdz)
 ! ON: omp.parallel
+! ON: fir.load %[[X]]#0 : !fir.ref<f64>
 ! ON-DAG: fir.global internal @_QFEdx : f64
+! ZERO: fir.zero_bits f64
+! UNDEF: fir.undefined f64
 ! ON-DAG: fir.global internal @_QFEdz : f64
 ! OFF-LABEL: func.func @_QQmain()
 ! OFF-DAG: fir.alloca f64 {{.*}}bindc_name = "dx"
@@ -57,24 +61,6 @@
 ! OFF: fir.alloca tuple
 ! OFF-LABEL: func.func private @_QFPworker(
 ! OFF-SAME: fir.host_assoc
-! OPT-LABEL: define internal void @_QFPworker..omp_par(
-! OPT: omp_collapsed.body.lr.ph:
-! OPT-DAG: %[[DX:[^ ]+]] = load double, ptr @_QFEdx
-! OPT-DAG: %[[DZ:[^ ]+]] = load double, ptr @_QFEdz
-! OPT-DAG: %[[RX:[^ ]+]] = fdiv fast double 1.000000e+00, %[[DX]]
-! OPT-DAG: %[[RZ:[^ ]+]] = fdiv fast double 1.000000e+00, %[[DZ]]
-! OPT: br label %[[BODY:omp_collapsed.body]]
-! OPT: [[BODY]]:
-! OPT-NOT: fdiv
-! OPT: fmul fast double {{.*}}, %[[RX]]
-! OPT-NOT: fdiv
-! OPT: fmul fast double {{.*}}, %[[RZ]]
-! OPT-NOT: fdiv
-! OPT: br i1
-! PFT_ON: {{^main-global symbol$}}
-! PFT_OFF-NOT: {{^main-global }}
-! PFT_OFF: Program
-! PFT_OFF-NOT: {{^main-global }}
 !--- miniweather.f90
 program p
 integer :: nx,nz,i_beg,k_beg
@@ -101,13 +87,9 @@ subroutine worker()
 end subroutine
 end program
 
-! Select intrinsic scalars in nested regions and statement expressions while
-! preserving excluded objects and private copies. Generated privatizer names
-! must not collide with excluded scalars. Calls and constructors do not
-! seed selection. The 32/33-byte CHARACTER cases distinguish new selection from
-! existing static storage. serial_only checks collection stops at END PARALLEL.
+! Independent call operands select globals; metadata and callee bodies do not.
 ! OBJECTS-LABEL: func.func @_QQmain()
-! OBJECTS: fir.alloca i32 {{.*}}bindc_name = "serial_only"
+! OBJECTS-DAG: fir.alloca i32 {{.*}}bindc_name = "serial_only"
 ! OBJECTS: omp.parallel
 ! OBJECTS-DAG: fir.global internal @_QFEif_condition : i32
 ! OBJECTS-DAG: fir.global internal @_QFEif_action : i32
@@ -127,7 +109,7 @@ subroutine worker()
 ! EXPRESSIONS: omp.parallel
 ! EXPRESSIONS: fir.call @_QFPsink(
 ! EXPRESSIONS: fir.call @_QFPassign(
-! EXPRESSIONS: fir.global internal @_QFEdirect : i32
+! EXPRESSIONS-COUNT-17: fir.global internal @_QFE{{(direct|actual|rhs|lhs|call_arg|max_a|max_b|max_c|jn_lo|jn_hi|jn_arg|yn_lo|yn_hi|yn_arg|empty_index|argument_index|then_condition)}} :
 ! BOUNDARY-DAG: fir.global internal @_QFEsaved : i32
 !--- objects.f90
 program p
@@ -142,10 +124,15 @@ program p
     integer :: n
   end type
   type(t) :: record,value
+  type handler; procedure(), pointer, nopass :: proc=>null(); end type
+  type(handler) :: handlers(2)
   interface assignment(=)
-    procedure assign
+    procedure assign,extract
   end interface
-  integer :: direct,actual,element,extent,rhs,field
+  integer :: direct,actual,element,extent,rhs,field,lhs,call_arg
+  integer :: max_a,max_b,max_c,jn_lo,jn_hi,yn_lo,yn_hi,empty_index,argument_index
+  integer :: then_condition,clause_only,header_only,statement_only,sf,arg,lastprivate_value,local_value,init_value
+  real :: jn_arg,yn_arg
   integer :: array(2),matrix(2,2),private_value,listed,eq1,eq2,x,y
   integer :: if_condition,if_action,serial_only,where_value,forall_value,i,j
   integer :: x_private_i32,y_firstprivate_i32,x_linear_i32
@@ -157,18 +144,25 @@ program p
   integer, pointer :: ptr
   equivalence(eq1,eq2)
   namelist /group/ listed
+  sf(arg)=statement_only+arg
+  handlers(1)%proc=>no_arguments; handlers(2)%proc=>sink
+  empty_index=1; argument_index=2; header_only=1; init_value=0
   direct=1; actual=2; element=3
   extent=4; rhs=5; field=6
   saved=1; storage=1; address=loc(storage); if_condition=1
   array=1; matrix=0; where_value=2; forall_value=3
-!$omp parallel
+!$omp parallel shared(clause_only) num_threads(header_only)
   print *,array,ap,ptr,record,private_value,listed,eq1,eq2
   print *,x_private_i32,y_firstprivate_i32,x_linear_i32
   print *,allocated,address,pointee,saved
-  print *,direct,abs(actual),[element],[character(extent)::]
-  print *,t(field)
+  print *,direct,abs(actual),max(max_a,max_b,max_c),[element],[character(extent)::]
+  value=t(field); print *,sf(1),lastprivate_value,local_value,init_value
+  print *,bessel_jn(jn_lo,jn_hi,jn_arg),bessel_yn(yn_lo,yn_hi,yn_arg)
   call sink(actual)
-  value=rhs
+  if (then_condition>0) then
+    call handlers(argument_index)%proc(call_arg)
+  end if
+  value=rhs; lhs=value
   where(array>0)
     where(array<3) array=where_value
   end where
@@ -186,13 +180,20 @@ program p
   private_value=x+y
 !$omp end parallel
   x=0
-!$omp simd linear(x)
+!$omp simd linear(x) lastprivate(lastprivate_value)
   do i=1,4
-    x=x+1
+    x=x+1; lastprivate_value=x
   end do
 !$omp end simd
+!$omp teams distribute parallel do
+  do i=1,2
+    call handlers(empty_index)%proc()
+  end do
+  do concurrent(i=1:1) local(local_value) local_init(init_value) reduce(+:direct); local_value=0; direct=direct+i; end do
   serial_only=1
 contains
+  subroutine no_arguments(); end subroutine
+  subroutine extract(to,from); integer :: to; type(t) :: from; to=from%n; end subroutine
   subroutine sink(n)
     integer :: n
     print *,n
@@ -204,13 +205,11 @@ subroutine assign(to,from)
   end subroutine
 end program
 
-! Keep the array in the host tuple while promoted scalars use globals. Preserve
-! EQUIVALENCE offsets and DATA initialization, threadprivate bindings, and
-! VOLATILE/ASYNCHRONOUS attributes.
+! Mixed captures use globals for x and a tuple for the array and VOLATILE v.
 ! MIXED-LABEL: func.func @_QQmain()
 ! MIXED: %[[A:[^: ]+]]:2 = hlfir.declare {{.*}}uniq_name("_QFEa")
-! MIXED: %[[T:[^ ]+]] = fir.alloca tuple<!fir.box<!fir.array<2xi32>>>
-! MIXED: %[[S:[^ ]+]] = fir.coordinate_of %[[T]],
+! MIXED: %[[T:[^ ]+]] = fir.alloca tuple<{{.*}}!fir.box<!fir.array<2xi32>>{{.*}}>
+! MIXED: %[[S:[^ ]+]] = fir.coordinate_of %[[T]], {{.*}} -> !fir.ref<!fir.box<!fir.array<2xi32>>>
 ! MIXED: %[[B:[^ ]+]] = fir.embox %[[A]]#0
 ! MIXED: fir.store %[[B]] to %[[S]]
 ! MIXED: fir.call @_QFPworker(%[[T]])
@@ -225,7 +224,7 @@ subroutine assign(to,from)
 ! MIXED: %[[TLS:[^: ]+]]:2 = hlfir.declare {{.*}}uniq_name("_QFEtls")
 ! MIXED: %[[ADDR:[^ ]+]] = omp.threadprivate %[[TLS]]#0
 ! MIXED: %[[LOCAL:[^: ]+]]:2 = hlfir.declare %[[ADDR]] uniq_name("_QFEtls")
-! MIXED: %[[C:[^ ]+]] = fir.coordinate_of %[[H]],
+! MIXED: %[[C:[^ ]+]] = fir.coordinate_of %[[H]], {{.*}} -> !fir.ref<!fir.box<!fir.array<2xi32>>>
 ! MIXED: %[[BOX:[^ ]+]] = fir.load %[[C]]
 ! MIXED: %[[ARRAY:[^ ]+]] = fir.box_addr %[[BOX]]
 ! MIXED: hlfir.declare %[[ARRAY]]({{.*}}) uniq_name("_QFEa") fortran_attrs<host_assoc>
@@ -245,10 +244,10 @@ subroutine assign(to,from)
 ! MIXED: fir.has_value %[[SECOND]]
 ! PROPERTIES-LABEL: func.func private @_QFPworker(
 ! PROPERTIES-DAG: hlfir.declare {{.*}}uniq_name("_QFEav") fortran_attrs<asynchronous, internal_assoc>
-! PROPERTIES-DAG: hlfir.declare {{.*}}uniq_name("_QFEv") fortran_attrs<volatile, internal_assoc>
+! PROPERTIES-DAG: hlfir.declare {{.*}}uniq_name("_QFEv") fortran_attrs<volatile, host_assoc>
 !--- mixed.f90
 program p
-  integer :: x,a(2)
+  integer :: x,a(2),locality_only
   integer,volatile :: v
   integer,asynchronous :: av
   integer, save :: backing(2),alias,tls
@@ -260,6 +259,8 @@ program p
   call worker()
 contains
   subroutine worker()
+    integer :: i
+    do concurrent(i=1:1) shared(locality_only); continue; end do
     tls=tls+1
 !$omp parallel
     print *,x,a,alias,tls,v,av
@@ -267,8 +268,7 @@ subroutine worker()
   end subroutine
 end program
 
-! Removing the last host capture also updates procedure boxes and direct and
-! indirect calls. Loads must stay in loops that call a modifying sibling.
+! Global captures supply direct calls, procedure boxes and CHARACTER arguments.
 ! CALLBACK-LABEL: func.func @_QQmain()
 ! CALLBACK: %[[P:[^: ]+]]:2 = hlfir.declare {{.*}}uniq_name("_QFEcallback")
 ! CALLBACK: %[[WORKER:[^ ]+]] = fir.address_of(@_QFPworker) : () -> ()
@@ -282,7 +282,15 @@ subroutine worker()
 ! CALLBACK: %[[LOADED:[^ ]+]] = fir.load %[[P]]#0
 ! CALLBACK: %[[CODE:[^ ]+]] = fir.box_addr %[[LOADED]]
 ! CALLBACK: fir.call %[[CODE]]()
-! BOX_OFF: %[[HOST:[^ ]+]] = fir.alloca tuple<!fir.ref<i32>>
+! CALLBACK-LABEL: func.func private @_QFPworker()
+! CALLBACK: %[[TEXT_ADDR:[^ ]+]] = fir.address_of(@_QFEtext)
+! CALLBACK: %[[LEN:[^ ]+]] = arith.constant 4 : index
+! CALLBACK: %[[TEXT:[^: ]+]]:2 = hlfir.declare %[[TEXT_ADDR]] typeparams %[[LEN]]
+! CALLBACK: %[[BYTES:[^ ]+]] = fir.convert %[[TEXT]]#0
+! CALLBACK: %[[LENGTH:[^ ]+]] = fir.convert %[[LEN]] : (index) -> i64
+! CALLBACK: fir.call @_FortranAioOutputAscii(%{{[^,]+}}, %[[BYTES]], %[[LENGTH]])
+! CALLBACK-COUNT-2: fir.global internal @_QFE{{(text|result_len)}} :
+! BOX_OFF: %[[HOST:[^ ]+]] = fir.alloca tuple<{{.*}}>
 ! BOX_OFF: %[[PTR:[^ ]+]] = fir.address_of(@_QFPworker)
 ! BOX_OFF: %[[BOX:[^ ]+]] = fir.emboxproc %[[PTR]], %[[HOST]]
 ! BOX_OFF: fir.store %[[BOX]]
@@ -290,22 +298,6 @@ subroutine worker()
 ! BOX_OFF: %[[ARG:[^ ]+]] = fir.emboxproc %[[ACTUAL]], %[[HOST]]
 ! BOX_OFF: fir.call @_QPinvoke(%[[ARG]])
 ! BOX_OFF: func.func private @_QFPworker({{.*}}fir.host_assoc
-! LICM-LABEL: func.func @_QQmain()
-! LICM: %[[MAIN_X:[^ ]+]] = fir.declare {{.*}}uniq_name("_QFEx") fortran_attrs<internal_assoc>
-! LICM: fir.do_loop
-! LICM-NOT: {{^ *[}]}}
-! LICM: fir.call @_QFPbump(
-! LICM-NOT: {{^ *[}]}}
-! LICM: fir.load %[[MAIN_X]] :
-! LICM: {{^ *[}]}}
-! LICM-LABEL: func.func private @_QFPaccumulate(
-! LICM: %[[X:[^ ]+]] = fir.declare {{.*}}uniq_name("_QFEx") fortran_attrs<internal_assoc>
-! LICM: fir.do_loop
-! LICM-NOT: {{^ *[}]}}
-! LICM: fir.call @_QFPbump(
-! LICM-NOT: {{^ *[}]}}
-! LICM: fir.load %[[X]] :
-! LICM: {{^ *[}]}}
 !--- callbacks.f90
 program p
   interface
@@ -314,53 +306,32 @@ subroutine invoke(proc)
     end subroutine
   end interface
   procedure(), pointer :: callback
-  integer :: x,i,s
-  x=7
+  integer :: x,result_len
+  character(4) :: text
+  x=7; result_len=4; text="abcd"
   callback=>worker
   call invoke(worker)
 !$omp parallel num_threads(1)
-  print *,x
+  print *,x,text,result_len
   call worker()
   call callback()
 !$omp end parallel
-  x=0
-  call accumulate()
-  x=0
-  s=0
-  do i=1,3
-    call bump()
-    s=s+x
-  end do
-  print *,s
 contains
   subroutine worker()
-    print *,x
-  end subroutine
-  subroutine accumulate()
-    integer :: i,s
-    s=0
-    do i=1,3
-      call bump()
-      s=s+x
-    end do
-    print *,s
-  end subroutine
-  subroutine bump()
-    x=x+1
+    print *,x,text,make()
   end subroutine
+  function make() result(s); character(result_len) :: s; s=text; end function
 end program
 subroutine invoke(proc)
   external :: proc
   call proc()
 end subroutine
 
-! Preserve the scalar whose name would collide with a coarray handle; a longer
-! suffix and an ordinary scalar remain eligible for global storage.
-! COARRAY: fir.alloca i32 {{.*}}bindc_name = "a_coarray_handle"
+! A scalar matching a coarray handle name needs separate storage.
+! COARRAY-DAG: fir.alloca i32 {{.*}}bindc_name = "a_coarray_handle"
 ! COARRAY-DAG: fir.global internal @_QFEa_coarray_handle_extra : i32
 ! COARRAY-DAG: fir.global internal @_QFEordinary : i32
 ! LLVM: @_QFEa_coarray_handle = linkonce global %{{[^ ]*prif_coarray_handle_type}}
-! LLVM: call void @llvm.memmove{{.*}}(ptr @_QFEa_coarray_handle, ptr {{.*}}, i64 8, i1 false)
 !--- coarray.f90
 program p
   integer,allocatable :: a[:]
@@ -374,3 +345,29 @@ program p
 !$omp end parallel
   deallocate(a)
 end program
+
+! Independent arguments distinguish user calls, inquiry metadata and folded IZEXT/JZEXT.
+! INTRINSICS-ON-COUNT-5: fir.alloca {{.*}}bindc_name = "{{(meta|round_meta|keyword_meta|second_result|cpu_result)}}"
+! INTRINSICS-ON-COUNT-8: fir.global internal @_QFE{{(actual|intrinsic_result|user_result|choice|optional_choice|izext_arg|jzext_arg|zext_sum)}} :
+! INTRINSICS-OFF-COUNT-13: fir.alloca {{.*}}bindc_name = "{{(actual|intrinsic_result|user_result|choice|optional_choice|izext_arg|jzext_arg|zext_sum|meta|round_meta|keyword_meta|second_result|cpu_result)}}"
+!--- intrinsics.f90
+program p
+  use, intrinsic :: ieee_arithmetic, only: true_value=>ieee_value, &
+    ieee_positive_zero, rounding_support=>ieee_support_rounding, ieee_round_type, ieee_nearest, ieee_up
+  real :: meta,actual,intrinsic_result,user_result,round_meta,keyword_meta,second_result,cpu_result
+  integer(1) :: izext_arg,jzext_arg
+  integer :: zext_sum,choice,optional_choice
+  type(ieee_round_type) :: rounding,modes(2)
+  rounding=ieee_nearest; modes=[ieee_nearest,ieee_up]
+  meta=13; actual=6; round_meta=1; keyword_meta=2
+  izext_arg=-1; jzext_arg=-1; choice=1; optional_choice=2
+!$omp parallel num_threads(1)
+  intrinsic_result=true_value(meta,ieee_positive_zero); user_result=ieee_value(actual)
+  zext_sum=izext(izext_arg)+jzext(jzext_arg)
+  print *,rounding_support(modes(choice),round_meta), &
+    rounding_support(x=keyword_meta,round_value=rounding),rounding_support(modes(optional_choice))
+  call second(second_result); call cpu_time(cpu_result)
+!$omp end parallel
+contains
+  real function ieee_value(x); real :: x; ieee_value=x+1; end function
+end program
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index 9bbdb4306012f8..4a925741a83ab6 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -150,8 +150,9 @@ static llvm::cl::opt<bool> enableOpenMP("fopenmp",
 
 static llvm::cl::opt<bool> disableOpenMPMainProgramGlobals(
     "fno-openmp-main-program-globals",
-    llvm::cl::desc("Disable static storage selection for small main program "
-                   "scalars used by explicit host OpenMP PARALLEL regions"),
+    llvm::cl::desc(
+        "Disable promotion to static storage for small main-program "
+        "scalars referenced directly in explicit host OpenMP PARALLEL regions"),
     llvm::cl::init(false));
 
 static llvm::cl::opt<bool>
@@ -505,13 +506,10 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
     return mlir::success();
   }
 
-  // Generated device targets require the same capture layout on host and
-  // device.
   Fortran::lower::LoweringOptions loweringOptions{};
-  loweringOptions.setOpenMPMainProgramGlobals(
-      !disableOpenMPMainProgramGlobals &&
-      enableDoConcurrentToOpenMPConversion.getValue() != "device");
   if (pftDumpTest) {
+    // PFT dumps do not use variable storage choices.
+    loweringOptions.setOpenMPMainProgramGlobals(false);
     if (auto ast = Fortran::lower::createPFT(parseTree, semanticsContext,
                                              loweringOptions)) {
       Fortran::lower::dumpPFT(llvm::outs(), *ast);
@@ -521,6 +519,10 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
     return mlir::failure();
   }
 
+  loweringOptions.setOpenMPMainProgramGlobals(!disableOpenMPMainProgramGlobals);
+  loweringOptions.setDeviceDoConcurrent(
+      enableDoConcurrentToOpenMPConversion.getValue() == "device");
+
   // translate to FIR dialect of MLIR
   mlir::DialectRegistry registry;
   fir::support::registerNonCodegenDialects(registry);
diff --git a/flang/unittests/Frontend/CompilerInstanceTest.cpp b/flang/unittests/Frontend/CompilerInstanceTest.cpp
index ba31bf79df8bb4..5200d941692574 100644
--- a/flang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/flang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -132,9 +132,18 @@ TEST(CompilerInvocation, MainProgramGlobalsRespectLateDeviceMapping) {
   invocation.setLoweringOptions();
   EXPECT_TRUE(invocation.getLoweringOpts().getOpenMPMainProgramGlobals());
 
+  const auto &options = invocation.getLoweringOpts();
   invocation.getCodeGenOpts().setDoConcurrentMapping(Mapping::DCMK_Device);
   invocation.setLoweringOptions();
-  EXPECT_FALSE(invocation.getLoweringOpts().getOpenMPMainProgramGlobals());
+  EXPECT_FALSE(options.shouldPromoteOpenMPMainProgramScalars());
+  EXPECT_TRUE(options.getOpenMPMainProgramGlobals());
+  for (auto mapping :
+      {Mapping::DCMK_None, Mapping::DCMK_Device, Mapping::DCMK_Host}) {
+    invocation.getCodeGenOpts().setDoConcurrentMapping(mapping);
+    invocation.setLoweringOptions();
+    EXPECT_EQ(options.shouldPromoteOpenMPMainProgramScalars(),
+        mapping != Mapping::DCMK_Device);
+  }
 }
 
 TEST(CompilerInvocation, MainProgramGlobalsPreserveExplicitDisable) {
diff --git a/flang/unittests/Frontend/FrontendActionTest.cpp b/flang/unittests/Frontend/FrontendActionTest.cpp
index 01a3e0e5ae3cbe..386cf137660f44 100644
--- a/flang/unittests/Frontend/FrontendActionTest.cpp
+++ b/flang/unittests/Frontend/FrontendActionTest.cpp
@@ -10,6 +10,8 @@
 #include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Frontend/FrontendOptions.h"
 #include "flang/FrontendTool/Utils.h"
+#include "flang/Lower/LoweringOptions.h"
+#include "flang/Lower/PFTBuilder.h"
 #include "flang/Parser/dump-parse-tree.h"
 #include "flang/Parser/parse-tree-visitor.h"
 #include "flang/Parser/parsing.h"
@@ -355,6 +357,39 @@ TEST_F(FrontendActionTest, ModuleFileRecoveryIsRejected) {
   EXPECT_FALSE(parsing.parseTree().has_value());
 }
 
+// Building several PFTs must not change semantic symbols or another PFT.
+TEST_F(FrontendActionTest, MainProgramGlobalsBelongToPFT) {
+  *(inputFileOs) << "integer :: x,image,co[*]\n!$omp parallel\n"
+                 << "print *,x,co[image]\n!$omp end parallel\nend\n";
+  inputFileOs.reset();
+  compInst.getFrontendOpts().features.Enable(
+      Fortran::common::LanguageFeature::OpenMP);
+  compInst.getFrontendOpts().programAction = ParseSyntaxOnly;
+  ASSERT_TRUE(executeCompilerInvocation(&compInst));
+  auto &semantics = compInst.getSemanticsContext();
+  const auto &scope = semantics.globalScope().children().back();
+  const auto *x = scope.FindSymbol(Fortran::parser::CharBlock{"x"});
+  ASSERT_NE(x, nullptr);
+  const auto flags = x->flags();
+  const auto attrs = x->attrs();
+  auto build = [&](bool enabled) {
+    Fortran::lower::LoweringOptions options;
+    options.setOpenMPMainProgramGlobals(enabled);
+    return Fortran::lower::createPFT(
+        *compInst.getParsing().parseTree(), semantics, options);
+  };
+  auto on = build(true), off = build(false), again = build(true);
+  EXPECT_TRUE(x->flags() == flags && x->attrs() == attrs);
+  EXPECT_TRUE(on->isMainGlobal(*x));
+  EXPECT_FALSE(off->isMainGlobal(*x));
+  EXPECT_TRUE(again->isMainGlobal(*x));
+  EXPECT_FALSE(
+      on->isMainGlobal(*scope.FindSymbol(Fortran::parser::CharBlock{"image"})));
+  using Fortran::lower::pft::getDependentVariableList;
+  EXPECT_TRUE(getDependentVariableList(*x, *on).back().isGlobal());
+  EXPECT_FALSE(getDependentVariableList(*x, *off).back().isGlobal());
+}
+
 TEST_F(FrontendActionTest, EmitLLVM) {
   // Populate the input file with the pre-defined input and flush it.
   *(inputFileOs) << "end program";



More information about the cfe-commits mailing list