[clang] [llvm] [openmp] [Clang][OpenMP] Add `#pragma omp flatten` loop directive + `depth` clause (PR #206977)
Amit Tiwari via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 28 05:05:15 PDT 2026
================
@@ -16528,6 +16535,305 @@ 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(
+ std::numeric_limits<unsigned>::max());
+ }
+ }
+
+ // Count perfectly nested loops with doForAllLoops. When 'depth' is present,
+ // walk NumLoops iterations to diagnose an insufficient nest. When it is
+ // omitted, walk one extra loop (3 total) so we can warn that default
+ // flatten only combines 2 of a deeper nest.
+ if (!DepthIsDependent) {
+ unsigned WalkLimit = DepthClause ? NumLoops : 3;
+ unsigned Found = 0;
+ bool Enough = OMPLoopBasedDirective::doForAllLoops(
+ AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false,
----------------
loopacino wrote:
The walk counts syntactic ForStmt, not canonical loops. So, the triangular `k` does still warn despite being non-flattenable. `k` is not flatten-affected unless `depth(3)` is used, where it correctly errors. Didn't change Sema here. Added For actionable part: aded a note + fix-it that inserts `depth(2)`
https://github.com/llvm/llvm-project/pull/206977
More information about the llvm-commits
mailing list