[llvm-branch-commits] [flang] [flang][OpenMP] Use the LoopSequence-based checks (PR #185300)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Mar 8 08:43:59 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
Author: Krzysztof Parzyszek (kparzysz)
<details>
<summary>Changes</summary>
Turn on the new loop-construct checks. Remove some checks from resolve-directives.cpp which are now done in the semantic analysis. Update tests.
---
Patch is 30.69 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/185300.diff
18 Files Affected:
- (modified) flang/include/flang/Semantics/openmp-utils.h (+3)
- (modified) flang/lib/Semantics/check-omp-loop.cpp (+57-26)
- (modified) flang/lib/Semantics/check-omp-structure.h (-1)
- (modified) flang/lib/Semantics/openmp-utils.cpp (+9)
- (modified) flang/lib/Semantics/resolve-directives.cpp (-52)
- (modified) flang/test/Semantics/OpenMP/do-collapse.f90 (+8-3)
- (modified) flang/test/Semantics/OpenMP/do-concurrent-collapse.f90 (+4)
- (modified) flang/test/Semantics/OpenMP/do-ordered.f90 (+10-5)
- (modified) flang/test/Semantics/OpenMP/do08.f90 (+12-6)
- (modified) flang/test/Semantics/OpenMP/do10.f90 (+2-1)
- (modified) flang/test/Semantics/OpenMP/do13.f90 (+12-6)
- (modified) flang/test/Semantics/OpenMP/do15.f90 (+10-8)
- (modified) flang/test/Semantics/OpenMP/do16.f90 (+4-2)
- (modified) flang/test/Semantics/OpenMP/do22.f90 (+4-2)
- (modified) flang/test/Semantics/OpenMP/fuse1.f90 (+2-1)
- (modified) flang/test/Semantics/OpenMP/loop-transformation-clauses01.f90 (+2-1)
- (modified) flang/test/Semantics/OpenMP/tile05.f90 (+2-1)
- (modified) flang/test/Semantics/OpenMP/tile07.f90 (+4-2)
``````````diff
diff --git a/flang/include/flang/Semantics/openmp-utils.h b/flang/include/flang/Semantics/openmp-utils.h
index a9801cf5b5753..bf02bc4f0252e 100644
--- a/flang/include/flang/Semantics/openmp-utils.h
+++ b/flang/include/flang/Semantics/openmp-utils.h
@@ -133,6 +133,9 @@ static std::string format(const char *fmt, Ts... values) {
return os.str();
}
+parser::Message &SayReason(
+ parser::Messages &msgs, const std::string &why, parser::CharBlock source);
+
std::pair<std::optional<int64_t>, Reason> GetArgumentValueWithReason(
const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
unsigned version);
diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index b17ece42f475a..36d7984227c6b 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -243,6 +243,7 @@ void OmpStructureChecker::CheckSIMDNest(const parser::OpenMPConstruct &c) {
void OmpStructureChecker::CheckNestedConstruct(
const parser::OpenMPLoopConstruct &x) {
const parser::OmpDirectiveSpecification &beginSpec{x.BeginDir()};
+ llvm::omp::Directive dir{beginSpec.DirId()};
unsigned version{context_.langOptions().OpenMPVersion};
// End-directive is not allowed in such cases:
@@ -266,8 +267,8 @@ void OmpStructureChecker::CheckNestedConstruct(
// Check constructs contained in the body of the loop construct.
auto &body{std::get<parser::Block>(x.t)};
for (auto &stmt : BlockRange(body, BlockRange::Step::Over)) {
- if (auto *dir{parser::Unwrap<parser::CompilerDirective>(stmt)}) {
- context_.Say(dir->source,
+ if (auto *d{parser::Unwrap<parser::CompilerDirective>(stmt)}) {
+ context_.Say(d->source,
"Compiler directives are not allowed inside OpenMP loop constructs"_warn_en_US);
} else if (auto *omp{parser::Unwrap<parser::OpenMPLoopConstruct>(stmt)}) {
if (!IsLoopTransforming(omp->BeginDir().DirId())) {
@@ -289,6 +290,9 @@ void OmpStructureChecker::CheckNestedConstruct(
// Check if a loop-nest-associated construct has only one top-level loop
// in it.
+ auto [needFirst, needCount, rangeReason]{
+ GetAffectedLoopRangeWithReason(x, version)};
+
if (std::optional<int64_t> numLoops{sequence.length()}) {
if (*numLoops == 0) {
context_.Say(beginSpec.DirName().source,
@@ -300,7 +304,58 @@ void OmpStructureChecker::CheckNestedConstruct(
"This construct applies to a loop nest, but has a loop sequence of length %ld"_err_en_US,
*numLoops);
}
+ if (assoc == llvm::omp::Association::LoopSeq) {
+ if (auto requiredCount{GetRequiredCount(needFirst, needCount)}) {
+ if (*requiredCount > 0 && *numLoops < *requiredCount) {
+ auto &msg{context_.Say(beginSpec.DirName().source,
+ "This construct requires a sequence of %ld loops, but the loop sequence has a length of %ld"_err_en_US,
+ *requiredCount, *numLoops)};
+ if (rangeReason) {
+ parser::Messages reasons;
+ SayReason(reasons, rangeReason.text, rangeReason.source);
+ reasons.AttachTo(msg);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // Check requirements on nest depth.
+ auto [needDepth, needPerfect, depthReason]{
+ GetAffectedNestDepthWithReason(x, version)};
+ auto [haveSema, havePerf]{sequence.depth()};
+
+ if (dir != llvm::omp::Directive::OMPD_fuse) {
+ auto haveDepth = needPerfect ? havePerf : haveSema;
+ // If the present depth is 0, it's likely that the construct doesn't
+ // have any loops in it, which would be diagnosed above.
+ if (needDepth && haveDepth && *haveDepth > 0) {
+ if (*needDepth > *haveDepth) {
+ if (needPerfect) {
+ auto &msg{context_.Say(beginSpec.DirName().source,
+ "This construct requires a perfect nest of depth %ld, but the associated nest is a perfect nest of depth %ld"_err_en_US,
+ *needDepth, *haveDepth)};
+ if (depthReason) {
+ parser::Messages reasons;
+ SayReason(reasons, depthReason.text, depthReason.source);
+ reasons.AttachTo(msg);
+ }
+ } else {
+ auto &msg{context_.Say(beginSpec.DirName().source,
+ "This construct requires a nest of depth %ld, but the associated nest has a depth of %ld"_err_en_US,
+ *needDepth, *haveDepth)};
+ if (depthReason) {
+ parser::Messages reasons;
+ SayReason(reasons, depthReason.text, depthReason.source);
+ reasons.AttachTo(msg);
+ }
+ }
+ }
}
+ } else {
+ // FUSE requires a sequence of perfect nests.
+ // TODO: Defer this check for now.
}
}
@@ -501,27 +556,6 @@ void OmpStructureChecker::CheckDistLinear(
}
}
-void OmpStructureChecker::CheckLooprangeBounds(
- const parser::OpenMPLoopConstruct &x) {
- unsigned version{context_.langOptions().OpenMPVersion};
- if (auto *clause{parser::omp::FindClause(
- x.BeginDir(), llvm::omp::Clause::OMPC_looprange)}) {
- auto *lrClause{parser::Unwrap<parser::OmpLooprangeClause>(clause)};
- auto first{GetIntValue(std::get<0>(lrClause->t))};
- auto count{GetIntValue(std::get<1>(lrClause->t))};
- if (auto requiredCount{GetRequiredCount(first, count)}) {
- LoopSequence sequence(std::get<parser::Block>(x.t), version, true);
- if (auto loopCount{sequence.length()}) {
- if (*loopCount < *requiredCount) {
- context_.Say(clause->source,
- "The specified loop range requires %ld loops, but the loop sequence has a length of %ld"_err_en_US,
- *requiredCount, *loopCount);
- }
- }
- }
- }
-}
-
void OmpStructureChecker::CheckScanModifier(
const parser::OmpClause::Reduction &x) {
using ReductionModifier = parser::OmpReductionModifier;
@@ -570,9 +604,6 @@ void OmpStructureChecker::Leave(const parser::OpenMPLoopConstruct &x) {
CheckScanModifier(*reduction);
}
}
- if (beginSpec.DirName().v == llvm::omp::Directive::OMPD_fuse) {
- CheckLooprangeBounds(x);
- }
if (llvm::omp::allSimdSet.test(beginSpec.DirName().v)) {
ExitDirectiveNest(SIMDNest);
}
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index cd98334ad8662..dc84c9d9ae9d8 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -326,7 +326,6 @@ class OmpStructureChecker : public OmpStructureCheckerBase {
void CheckAtomicUpdate(const parser::OpenMPAtomicConstruct &x);
void CheckScanModifier(const parser::OmpClause::Reduction &x);
- void CheckLooprangeBounds(const parser::OpenMPLoopConstruct &x);
void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
void CheckSIMDNest(const parser::OpenMPConstruct &x);
void CheckNestedConstruct(const parser::OpenMPLoopConstruct &x);
diff --git a/flang/lib/Semantics/openmp-utils.cpp b/flang/lib/Semantics/openmp-utils.cpp
index 63cd9b42e651c..19c793f7dd274 100644
--- a/flang/lib/Semantics/openmp-utils.cpp
+++ b/flang/lib/Semantics/openmp-utils.cpp
@@ -528,6 +528,15 @@ MaybeExpr MakeEvaluateExpr(const parser::OmpStylizedInstance &inp) {
instance.u);
}
+parser::Message &SayReason(
+ parser::Messages &msgs, const std::string &why, parser::CharBlock source) {
+ parser::MessageFixedText msg(
+ why.c_str(), why.size(), parser::Severity::Because);
+ // Use MessageFormattedText because it owns the storage for the message
+ // text.
+ return msgs.Say(source, parser::MessageFormattedText(msg));
+}
+
std::pair<std::optional<int64_t>, Reason> GetArgumentValueWithReason(
const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
unsigned version) {
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index c8ffa22d6bb5f..861fb9f2af2bc 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1123,7 +1123,6 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
void CheckDataCopyingClause(
const parser::Name &, const Symbol &, Symbol::Flag);
- void CheckAssocLoopLevel(std::int64_t level, const parser::OmpClause *clause);
void CheckObjectIsPrivatizable(
const parser::Name &, const Symbol &, Symbol::Flag);
void CheckSourceLabel(const parser::Label &);
@@ -2145,11 +2144,6 @@ std::int64_t OmpAttributeVisitor::SetAssociatedMaxClause(
const parser::OmpClause *maxClause = nullptr;
for (auto [level, clause] : llvm::zip_equal(levels, clauses)) {
if (tileLevel > 0 && tileLevel < level) {
- context_.Say(clause->source,
- "The value of the parameter in the COLLAPSE clause must"
- " not be larger than the number of the number of tiled loops"
- " because collapse currently is limited to independent loop"
- " iterations."_err_en_US);
return 1;
}
@@ -2285,41 +2279,14 @@ void OmpAttributeVisitor::CheckPerfectNestAndRectangularLoop(
// Recurse into nested loop
const auto &block{std::get<parser::Block>(loop->t)};
if (block.empty()) {
- // Insufficient number of nested loops already reported by
- // CheckAssocLoopLevel()
break;
}
loop = GetDoConstructIf(block.front());
if (!loop) {
- // Insufficient number of nested loops already reported by
- // CheckAssocLoopLevel()
break;
}
- auto checkPerfectNest = [&, this]() {
- if (block.empty())
- return;
- auto last = block.end();
- --last;
-
- // A trailing CONTINUE is not considered part of the loop body
- if (parser::Unwrap<parser::ContinueStmt>(*last))
- --last;
-
- // In a perfectly nested loop, the nested loop must be the only
- // statement
- if (last == block.begin())
- return;
-
- // Non-perfectly nested loop
- // TODO: Point to non-DO statement, directiveSource as a note
- context_.Say(dirContext.directiveSource,
- "Canonical loop nest must be perfectly nested."_err_en_US);
- };
-
- checkPerfectNest();
-
++curLevel;
}
}
@@ -2393,25 +2360,6 @@ void OmpAttributeVisitor::PrivatizeAssociatedLoopIndexAndCheckLoopLevel(
loop = it != block.end() ? GetDoConstructIf(*it) : nullptr;
}
}
- CheckAssocLoopLevel(level, GetAssociatedClause());
- }
- }
-}
-
-void OmpAttributeVisitor::CheckAssocLoopLevel(
- std::int64_t level, const parser::OmpClause *clause) {
- if (clause && level != 0) {
- switch (clause->Id()) {
- case llvm::omp::OMPC_sizes:
- context_.Say(clause->source,
- "The SIZES clause has more entries than there are nested canonical loops."_err_en_US);
- break;
- default:
- context_.Say(clause->source,
- "The value of the parameter in the COLLAPSE or ORDERED clause must"
- " not be larger than the number of nested loops"
- " following the construct."_err_en_US);
- break;
}
}
}
diff --git a/flang/test/Semantics/OpenMP/do-collapse.f90 b/flang/test/Semantics/OpenMP/do-collapse.f90
index ec6a3bdad3686..70a84c333236a 100644
--- a/flang/test/Semantics/OpenMP/do-collapse.f90
+++ b/flang/test/Semantics/OpenMP/do-collapse.f90
@@ -3,7 +3,8 @@
! 2.7.1 Collapse Clause
program omp_doCollapse
integer:: i,j
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 3, but the associated nest is a perfect nest of depth 2
+ !BECAUSE: COLLAPSE clause was specified with argument 3
!$omp do collapse(3)
do i = 1,10
do j = 1, 10
@@ -14,7 +15,8 @@ program omp_doCollapse
do i = 1,10
do j = 1, 10
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: COLLAPSE clause was specified with argument 2
!$omp do collapse(2)
do k = 1, 10
print *, "hello"
@@ -23,6 +25,8 @@ program omp_doCollapse
end do
end do
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: COLLAPSE clause was specified with argument 2
!$omp parallel do collapse(2)
do i = 1, 3
!ERROR: Loop control is not present in the DO LOOP
@@ -31,7 +35,8 @@ program omp_doCollapse
end do
end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: COLLAPSE clause was specified with argument 2
!ERROR: At most one COLLAPSE clause can appear on the SIMD directive
!$omp simd collapse(2) collapse(1)
do i = 1, 4
diff --git a/flang/test/Semantics/OpenMP/do-concurrent-collapse.f90 b/flang/test/Semantics/OpenMP/do-concurrent-collapse.f90
index 355626f6e73b9..3e382eb3cc4d4 100644
--- a/flang/test/Semantics/OpenMP/do-concurrent-collapse.f90
+++ b/flang/test/Semantics/OpenMP/do-concurrent-collapse.f90
@@ -1,6 +1,8 @@
!RUN: %python %S/../test_errors.py %s %flang -fopenmp
integer :: i, j
+! ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+! BECAUSE: COLLAPSE clause was specified with argument 2
! ERROR: DO CONCURRENT loops cannot be used with the COLLAPSE clause.
!$omp parallel do collapse(2)
do i = 1, 1
@@ -30,6 +32,8 @@
print *, j
end do
+! ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+! BECAUSE: COLLAPSE clause was specified with argument 2
! ERROR: DO CONCURRENT loops cannot be used with the COLLAPSE clause.
!$omp loop collapse(2)
do i = 1, 1
diff --git a/flang/test/Semantics/OpenMP/do-ordered.f90 b/flang/test/Semantics/OpenMP/do-ordered.f90
index 79ded3e1b6fe6..00fdd2dc966b5 100644
--- a/flang/test/Semantics/OpenMP/do-ordered.f90
+++ b/flang/test/Semantics/OpenMP/do-ordered.f90
@@ -4,7 +4,8 @@
program omp_doOrdered
integer:: i,j
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 3, but the associated nest is a perfect nest of depth 2
+ !BECAUSE: ORDERED clause was specified with argument 3
!$omp do ordered(3)
do i = 1,10
do j = 1, 10
@@ -15,7 +16,8 @@ program omp_doOrdered
do i = 1,10
do j = 1, 10
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: ORDERED clause was specified with argument 2
!$omp do ordered(2)
do k = 1, 10
print *, "hello"
@@ -24,7 +26,8 @@ program omp_doOrdered
end do
end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: ORDERED clause was specified with argument 2
!$omp do ordered(2)
do i = 1,10
!ERROR: An ORDERED directive without the DEPEND clause must be closely nested in a worksharing-loop (or worksharing-loop SIMD) region with ORDERED clause without the parameter
@@ -36,7 +39,8 @@ program omp_doOrdered
end do
!$omp end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 3, but the associated nest is a perfect nest of depth 2
+ !BECAUSE: ORDERED clause was specified with argument 3
!$omp do collapse(1) ordered(3)
do i = 1,10
do j = 1, 10
@@ -46,7 +50,8 @@ program omp_doOrdered
!$omp end do
!$omp parallel num_threads(4)
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: ORDERED clause was specified with argument 2
!$omp do ordered(2) collapse(1)
do i = 1,10
!ERROR: An ORDERED directive without the DEPEND clause must be closely nested in a worksharing-loop (or worksharing-loop SIMD) region with ORDERED clause without the parameter
diff --git a/flang/test/Semantics/OpenMP/do08.f90 b/flang/test/Semantics/OpenMP/do08.f90
index 5143dff0dd315..8ab02a0d9acbf 100644
--- a/flang/test/Semantics/OpenMP/do08.f90
+++ b/flang/test/Semantics/OpenMP/do08.f90
@@ -7,7 +7,8 @@ program omp
logical cond(10,10,10)
cond = .false.
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 3, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: COLLAPSE clause was specified with argument 3
!$omp do collapse(3)
do i = 0, 10
!ERROR: CYCLE statement to non-innermost associated loop of an OpenMP DO construct
@@ -20,7 +21,8 @@ program omp
end do
!$omp end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 3, but the associated nest is a perfect nest of depth 2
+ !BECAUSE: COLLAPSE clause was specified with argument 3
!$omp do collapse(3)
do i = 0, 10
do j = 0, 10
@@ -33,7 +35,8 @@ program omp
end do
!$omp end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: COLLAPSE clause was specified with argument 2
!$omp do collapse(2)
do i = 0, 10
!ERROR: CYCLE statement to non-innermost associated loop of an OpenMP DO construct
@@ -47,7 +50,8 @@ program omp
!$omp end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 2, but the associated nest is a perfect nest of depth 1
+ !BECAUSE: COLLAPSE clause was specified with argument 2
!$omp do collapse(2)
foo: do i = 0, 10
!ERROR: CYCLE statement to non-innermost associated loop of an OpenMP DO construct
@@ -61,7 +65,8 @@ program omp
!$omp end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 3, but the associated nest is a perfect nest of depth 2
+ !BECAUSE: COLLAPSE clause was specified with argument 3
!$omp do collapse(3)
do 60 i=2,200,2
do j=1,10
@@ -124,7 +129,8 @@ program omp
end do foo
!$omp end do
- !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
+ !ERROR: This construct requires a perfect nest of depth 3, but the associated nest is a perfect nest of depth 2
+ !BECAUSE: ORDERED clause was specified with argument 3
!$omp do collapse(2) ordered(3)
foo: do i = 0, 10
foo1: do j = 0, 10
diff --git a/flang/test/Semantics/OpenMP/do10.f90 b/flang/test/Semantics/OpenMP/do10.f90
index 936d94d591369..b609567c4d93d 100644
--- a/flang/test/Semantics/OpenMP/do10.f90
+++ b/flang/test/Semant...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/185300
More information about the llvm-branch-commits
mailing list