================
@@ -16382,6 +16395,277 @@ StmtResult
SemaOpenMP::ActOnOpenMPInterchangeDirective(
buildPreInits(Context, PreInits));
}
+/// Counts the perfectly nested canonical loop depth at \p AStmt. Returns
+/// std::nullopt if a loop-generating transformation prevents a static count.
+static std::optional<unsigned> getCanonicalLoopNestDepth(Stmt *AStmt) {
+ unsigned Depth = 0;
+ Stmt *CurStmt = AStmt ? AStmt->IgnoreContainers() : nullptr;
+ while (CurStmt) {
+ if (isa<OMPLoopTransformationDirective>(CurStmt))
+ return std::nullopt;
+ if (auto *CanonLoop = dyn_cast<OMPCanonicalLoop>(CurStmt))
+ CurStmt = CanonLoop->getLoopStmt();
+ Stmt *Body = nullptr;
+ if (auto *For = dyn_cast<ForStmt>(CurStmt))
+ Body = For->getBody();
+ else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(CurStmt))
+ Body = RangeFor->getBody();
+ else
+ break;
+ ++Depth;
+ CurStmt = Body ? Body->IgnoreContainers() : nullptr;
+ }
+ return Depth;
+}
+
+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 DepthIsValueDependent = false;
+ const auto *DepthClause =
+ OMPExecutableDirective::getSingleClause<OMPDepthClause>(Clauses);
+ if (DepthClause) {
+ Expr *DepthExpr = DepthClause->getDepth();
+ if (DepthExpr && DepthExpr->isValueDependent()) {
+ DepthIsValueDependent = true;
+ } else if (DepthExpr) {
+ Expr::EvalResult EvalResult;
+ if (DepthExpr->EvaluateAsInt(EvalResult, Context))
+ NumLoops = EvalResult.Val.getInt().getZExtValue();
----------------
alexey-bataev wrote:
```suggestion
NumLoops = EvalResult.Val.getInt(). getLimitedValue();
```
https://github.com/llvm/llvm-project/pull/206977
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits