[clang] [llvm] [openmp] [Clang][OpenMP] Add `#pragma omp flatten` loop directive + `depth` clause (PR #206977)
Michael Kruse via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 26 04:18:37 PDT 2026
================
@@ -0,0 +1,128 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -std=c++20 -fopenmp -fopenmp-version=60 -fsyntax-only -Wuninitialized -verify %s
+
+extern "C" void body(...);
+
+void func(int n) {
+
+ // The associated statement must be a for loop.
+ // expected-error at +2 {{statement after '#pragma omp flatten' must be a for loop}}
+ #pragma omp flatten
+ ;
+
+ // A non-loop statement is rejected as well.
+ // expected-error at +2 {{statement after '#pragma omp flatten' must be a for loop}}
+ #pragma omp flatten
+ int b = 0;
+
+ // A single loop is not enough: flatten combines two perfectly nested loops,
+ // so the body of the outer loop must itself be a for loop.
+ #pragma omp flatten
+ for (int i = 0; i < 7; ++i)
+ // expected-error at +1 {{statement after '#pragma omp flatten' must be a for loop}}
+ ;
+
+ // The associated statement of a directive is not a for loop.
+ // expected-error at +2 {{statement after '#pragma omp flatten' must be a for loop}}
+ #pragma omp flatten
+ #pragma omp for
+ for (int i = 0; i < 7; ++i)
+ for (int j = 0; j < 7; ++j)
+ body(i, j);
+
+ {
+ // expected-error at +2 {{expected statement}}
+ #pragma omp flatten
+ }
+
+ // The loops must be perfectly nested: no code is allowed between them.
+ #pragma omp flatten
+ for (int i = 0; i < n; ++i) {
+ int x = 0;
+ // expected-error at -2 {{statement after '#pragma omp flatten' must be a for loop}}
+ for (int j = 0; j < n; ++j)
+ body(i, j, x);
+ }
+
+ // Each affected loop must be in OpenMP canonical form.
+ #pragma omp flatten
+ for (int i = 0; i < n; ++i)
+ // expected-error at +1 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', '>=', or '!=') of loop variable 'j'}}
+ for (int j = 0; j / 3 < n; ++j)
+ body(i, j);
+
+ // The affected loops must be rectangular: an inner bound may not depend on an
+ // outer loop counter.
----------------
Meinersbur wrote:
[nit] According to the spec, those are allowed, but support non-rectangular loops not added by this PR
https://github.com/llvm/llvm-project/pull/206977
More information about the llvm-commits
mailing list