[Openmp-commits] [clang] [llvm] [openmp] [Clang][OpenMP] Add `#pragma omp flatten` loop directive + `depth` clause (PR #206977)

Alexey Bataev via Openmp-commits openmp-commits at lists.llvm.org
Thu Aug 20 10:51:54 PDT 2026


================
@@ -16528,6 +16534,277 @@ StmtResult SemaOpenMP::ActOnOpenMPInterchangeDirective(
                                          buildPreInits(Context, PreInits));
 }
 
+StmtResult
+SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
+                                        Stmt *AStmt, SourceLocation StartLoc,
+                                        SourceLocation EndLoc) {
+  ASTContext &Context = getASTContext();
+  DeclContext *CurContext = SemaRef.CurContext;
+  Scope *CurScope = SemaRef.getCurScope();
+
+  // Empty statement should only be possible if there already was an error.
+  if (!AStmt)
+    return StmtError();
+
+  // flatten without 'depth' clause combines two loops; 'depth(k)' selects k.
+  unsigned NumLoops = 2;
+  bool DepthIsDependent = false;
+  const auto *DepthClause =
+      OMPExecutableDirective::getSingleClause<OMPDepthClause>(Clauses);
+  if (DepthClause) {
+    Expr *DepthExpr = DepthClause->getDepth();
+    if (DepthExpr && DepthExpr->isInstantiationDependent()) {
+      DepthIsDependent = true;
+    } else if (DepthExpr) {
+      Expr::EvalResult EvalResult;
+      if (DepthExpr->EvaluateAsInt(EvalResult, Context))
+        NumLoops = EvalResult.Val.getInt().getLimitedValue();
----------------
alexey-bataev wrote:

```suggestion
        NumLoops = EvalResult.Val.getInt().getLimitedValue(std::numeric_limits<unsigned>::max());
```

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


More information about the Openmp-commits mailing list