[flang-commits] [flang] [flang][OpenMP] Lowering support for declare variant. (PR #206988)

Abid Qadeer via flang-commits flang-commits at lists.llvm.org
Fri Jul 3 08:36:12 PDT 2026


================
@@ -61,13 +62,39 @@ bool Fortran::lower::CallerInterface::hasAlternateReturns() const {
   return procRef.hasAlternateReturns();
 }
 
+/// If \p proc refers to a base procedure that carries OpenMP DECLARE VARIANT
+/// entries, return the variant selected for the enclosing OpenMP context: a
+/// direct call to the base is then lowered as a call to that variant. Returns
+/// nullptr when \p proc is not such a base call, or when no variant matches the
+/// context, so the base procedure is used as usual.
+static const Fortran::semantics::Symbol *
+getOmpDeclareVariantCallee(const Fortran::evaluate::ProcedureDesignator &proc,
+                           Fortran::lower::AbstractConverter &converter) {
+  const Fortran::semantics::Symbol *symbol = proc.GetSymbol();
+  if (!symbol)
+    return nullptr;
+  const Fortran::semantics::Symbol &ultimate{symbol->GetUltimate()};
+  // Only pay the cost of declare-variant resolution when the callee actually
+  // carries variant entries; this avoids overhead on every other call.
+  const auto *details =
+      ultimate.detailsIf<Fortran::semantics::SubprogramDetails>();
+  if (!details || details->ompDeclareVariants().empty())
+    return nullptr;
+  return Fortran::lower::omp::resolveDeclareVariantCallee(ultimate, converter);
+}
+
 /// Return the binding label (from BIND(C...)) or the mangled name of the
 /// symbol.
 static std::string
 getProcMangledName(const Fortran::evaluate::ProcedureDesignator &proc,
                    Fortran::lower::AbstractConverter &converter) {
-  if (const Fortran::semantics::Symbol *symbol = proc.GetSymbol())
+  if (const Fortran::semantics::Symbol *symbol = proc.GetSymbol()) {
----------------
abidh wrote:

Thanks again for catching this. The OpenMP spec (6.0, section 9.6) frames replacement around direct calls: "The OpenMP context for a direct call to a given base function is defined according to Section 9.1." 

It also states that "For indirect function calls that can be determined to call a particular base function, whether variant substitution occurs is unspecified." I think it is safest to substitute only at direct calls, and I've modified the code accordingly. There are two new tests confirming that an indirect call and a base passed as an argument are not replaced.

https://github.com/llvm/llvm-project/pull/206988


More information about the flang-commits mailing list