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

via flang-commits flang-commits at lists.llvm.org
Fri Jul 3 02:00:13 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()) {
----------------
jeanPerier wrote:

Note that this helper is also used when taking the function address to pass it as a dummy function argument or to associate it to a function pointer.

In other words, when you have code like the following, you will now also change the name of the function used on the fir.address_of genetrated to pass the function to `do_something`:

```
subroutine test_indirect
  !$omp parallel
  call do_something(base)
  !$omp end parallel
end subroutine
```

That may be fine, I have no idea what the OpenMP spec says about it, just bringing it to your attention to check that is what you want.

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


More information about the flang-commits mailing list