[flang-commits] [flang] [flang][OpenMP] Add structure checks for DECLARE VARIANT (PR #198799)

Abid Qadeer via flang-commits flang-commits at lists.llvm.org
Fri May 29 08:27:45 PDT 2026


================
@@ -0,0 +1,183 @@
+//===-- lib/Semantics/check-omp-declare-variant.cpp -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Structure checks for DECLARE VARIANT.
+//
+//===----------------------------------------------------------------------===//
+
+#include "check-omp-structure.h"
+
+#include "flang/Common/idioms.h"
+#include "flang/Common/visit.h"
+#include "flang/Evaluate/check-expression.h"
+#include "flang/Parser/parse-tree.h"
+#include "flang/Semantics/openmp-utils.h"
+#include "flang/Semantics/symbol.h"
+#include "flang/Semantics/tools.h"
+
+#include "llvm/Frontend/OpenMP/OMP.h"
+
+namespace Fortran::semantics {
+
+using namespace Fortran::semantics::omp;
+
+static const parser::traits::OmpContextSelectorSpecification *
+getMatchClauseContextSelector(const parser::OmpDirectiveSpecification &spec) {
+  for (const parser::OmpClause &clause : spec.Clauses().v) {
+    if (clause.Id() == llvm::omp::Clause::OMPC_match)
+      return &std::get<parser::OmpClause::Match>(clause.u).v.v;
+  }
+  return nullptr;
+}
+
+void OmpStructureChecker::CheckDeclareVariantUserConditions(
+    const parser::OmpContextSelector &ctx) {
+  using SetName = parser::OmpTraitSetSelectorName;
+  using TraitName = parser::OmpTraitSelectorName;
+
+  for (const parser::OmpTraitSetSelector &traitSet : ctx.v) {
+    if (std::get<SetName>(traitSet.t).v != SetName::Value::User) {
+      continue;
+    }
+    for (const parser::OmpTraitSelector &trait :
+        std::get<std::list<parser::OmpTraitSelector>>(traitSet.t)) {
+      const auto &traitName{std::get<TraitName>(trait.t)};
+      if (!std::holds_alternative<TraitName::Value>(traitName.u) ||
+          std::get<TraitName::Value>(traitName.u) !=
+              TraitName::Value::Condition) {
+        continue;
+      }
+      const auto &maybeProps{
+          std::get<std::optional<parser::OmpTraitSelector::Properties>>(
+              trait.t)};
+      if (!maybeProps) {
+        continue;
+      }
+      const auto &properties{
+          std::get<std::list<parser::OmpTraitProperty>>(maybeProps->t)};
+      if (properties.size() != 1) {
+        continue;
+      }
+      const parser::OmpTraitProperty &property{properties.front()};
+      const parser::ScalarExpr &scalarExpr{
+          std::get<parser::ScalarExpr>(property.u)};
+      auto maybeType{GetDynamicType(scalarExpr.thing.value())};
+      if (!maybeType || maybeType->category() != TypeCategory::Logical) {
+        continue;
+      }
+      if (const auto *expr{GetExpr(scalarExpr)}) {
+        if (!IsConstantExpr(*expr, &context_.foldingContext())) {
+          context_.Say(property.source,
+              "USER condition in the MATCH clause must be a constant expression"_err_en_US);
----------------
abidh wrote:

Thanks. Yes, I meant it as stop gap until I implement the dynamic condition support. I have updated the error message.

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


More information about the flang-commits mailing list