Clang error: the user condition in the OpenMP context selector needs to be constant; A is not (err_omp_declare_variant_user_condition_not_constant)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: the user condition in the OpenMP context selector needs to be constant; A is not (since 11.0)
Type Error
Category OpenMP Issue (since 11.0)
Internal Id err_omp_declare_variant_user_condition_not_constant (since 11.0)
Internal Message the user condition in the OpenMP context selector needs to be constant; %0 is not (since 11.0)
Regular Expression (?:error|fatal error)\: the user condition in the OpenMP context selector needs to be constant; (.*?) is not
First Commit 2019-12-20 1228d42ddab8 [OpenMP][Part 2] Use reusable OpenMP context/traits handling

Description

The error is issued by the Clang compiler when encountering a user-defined condition within an OpenMP context selector that is not constant. OpenMP, a parallel programming model, allows the specification of context selectors to direct the compiler on which variant of a function or method should be used based on certain traits or conditions at compile time. A user-defined condition is one such trait that can be specified through the use of the match clause in #pragma omp declare variant. According to the OpenMP specification, these user conditions are required to be evaluable to a constant expression at compile time so that the compiler can make a determinate choice on which variant to select. This error surfaces when the expression specified as a user condition does not meet this criterion, indicating that the compiler is unable to resolve the variant selection as intended due to the dynamism introduced by the non-constant expression.  
AI Generated

Example

In the following example, the Clang compiler issues an error when attempting to compile a C++ program that utilizes the OpenMP directive declare variant to specify a function variant based on a user-defined condition. The program defines a function f and tries to create a variant of this function that should be used when the condition x meets certain criteria, as indicated in the match clause. However, the variable x is not constant, which violates the OpenMP specification requiring that user conditions in context selectors be constant expressions evaluatable at compile time. Consequently, the compiler cannot determine at compile time which variant of the function f to use, leading to the error stating that the user condition must be a constant expression, which x is not.  
AI Generated


Flags -xc -fopenmp

[Try out in Compiler Explorer]

Source
#pragma omp declare variant(f) match(user={condition(x)})
int f(int x) { return x * 2; }
int x; // Non-constant variable
int main() {}
Compiler Output
<source>:1:54: error: the user condition in the OpenMP context selector needs to be constant; x is not


Clang Internals (17.0.6)

Git Commit Message

[OpenMP][Part 2] Use reusable OpenMP context/traits handling

This patch implements an almost complete handling of OpenMP
contexts/traits such that we can reuse most of the logic in Flang
through the OMPContext.{h,cpp} in llvm/Frontend/OpenMP.

All but construct SIMD specifiers, e.g., inbranch, and the device ISA
selector are define in `llvm/lib/Frontend/OpenMP/OMPKinds.def`. From
these definitions we generate the enum classes `TraitSet`,
`TraitSelector`, and `TraitProperty` as well as conversion and helper
functions in `llvm/lib/Frontend/OpenMP/OMPContext.{h,cpp}`.

The above enum classes are used in the parser, sema, and the AST
attribute. The latter is not a collection of multiple primitive variant
arguments that contain encodings via numbers and strings but instead a
tree that mirrors the `match` clause (see `struct OpenMPTraitInfo`).

The changes to the parser make it more forgiving when wrong syntax is
read and they also resulted in more specialized diagnostics. The tests
are updated and the core issues are detected as before. Here and
elsewhere this patch tries to be generic, thus we do not distinguish
what selector set, selector, or property is parsed except if they do
behave exceptionally, as for example `user={condition(EXPR)}` does.

The sema logic changed in two ways: First, the OMPDeclareVariantAttr
representation changed, as mentioned above, and the sema was adjusted to
work with the new `OpenMPTraitInfo`. Second, the matching and scoring
logic moved into `OMPContext.{h,cpp}`. It is implemented on a flat
representation of the `match` clause that is not tied to clang.
`OpenMPTraitInfo` provides a method to generate this flat structure (see
`struct VariantMatchInfo`) by computing integer score values and boolean
user conditions from the `clang::Expr` we keep for them.

The OpenMP context is now an explicit object (see `struct OMPContext`).
This is in anticipation of construct traits that need to be tracked. The
OpenMP context, as well as the `VariantMatchInfo`, are basically made up
of a set of active or respectively required traits, e.g., 'host', and an
ordered container of constructs which allows duplication. Matching and
scoring is kept as generic as possible to allow easy extension in the
future.

---

Test changes:

The messages checked in `OpenMP/declare_variant_messages.{c,cpp}` have
been auto generated to match the new warnings and notes of the parser.
The "subset" checks were reversed causing the wrong version to be
picked. The tests have been adjusted to correct this.
We do not print scores if the user did not provide one.
We print spaces to make lists in the `match` clause more legible.

Reviewers: kiranchandramohan, ABataev, RaviNarayanaswamy, gtbercea, grokos, sdmitriev, JonChesterfield, hfinkel, fghanim

Subscribers: merge_guards_bot, rampitec, mgorny, hiraditya, aheejin, fedor.sergeev, simoncook, bollu, guansong, dexonsmith, jfb, s.egerton, llvm-commits, cfe-commits

Tags: #clang, #llvm

Differential Revision: https://reviews.llvm.org/D71830

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaOpenMP.cpp (line 7430)

std::optional<std::pair<FunctionDecl *, Expr *>> Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, Expr *VariantRef, OMPTraitInfo &TI, unsigned NumAppendArgs, SourceRange SR) {
  // ...
  // Deal with non-constant score and user condition expressions.
  auto HandleNonConstantScoresAndConditions = [this](Expr *&E, bool IsScore) -> bool {
    // ...
    if (IsScore) {
    // ...
    } else {
      // ...
      Diag(E->getExprLoc(), diag::err_omp_declare_variant_user_condition_not_constant) << E;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/OpenMP/declare_variant_messages.c

  • clang/test/OpenMP/declare_variant_messages.c:57:58: error: the user condition in the OpenMP context selector needs to be constant; foo() is not
  • clang/test/OpenMP/declare_variant_messages.c:56:58: error: the user condition in the OpenMP context selector needs to be constant; foo is not