Skip to content

[flang][OpenMP][Semantics] improve semantic checks for array sections #132230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5304,27 +5304,37 @@ void OmpStructureChecker::CheckArraySection(
if (const auto *triplet{
std::get_if<parser::SubscriptTriplet>(&subscript.u)}) {
if (std::get<0>(triplet->t) && std::get<1>(triplet->t)) {
std::optional<int64_t> strideVal{std::nullopt};
if (const auto &strideExpr = std::get<2>(triplet->t)) {
// OpenMP 6.0 Section 5.2.5: Array Sections
// Restrictions: if a stride expression is specified it must be
// positive. A stride of 0 doesn't make sense.
strideVal = GetIntValue(strideExpr);
if (strideVal && *strideVal < 1) {
context_.Say(GetContext().clauseSource,
"'%s' in %s clause must have a positive stride"_err_en_US,
name.ToString(),
parser::ToUpperCaseLetters(getClauseName(clause).str()));
}
}
const auto &lower{std::get<0>(triplet->t)};
const auto &upper{std::get<1>(triplet->t)};
if (lower && upper) {
const auto lval{GetIntValue(lower)};
const auto uval{GetIntValue(upper)};
if (lval && uval && *uval < *lval) {
context_.Say(GetContext().clauseSource,
"'%s' in %s clause"
" is a zero size array section"_err_en_US,
name.ToString(),
parser::ToUpperCaseLetters(getClauseName(clause).str()));
break;
} else if (std::get<2>(triplet->t)) {
const auto &strideExpr{std::get<2>(triplet->t)};
if (strideExpr) {
if (clause == llvm::omp::Clause::OMPC_depend) {
context_.Say(GetContext().clauseSource,
"Stride should not be specified for array section in "
"DEPEND "
"clause"_err_en_US);
}
if (lval && uval) {
int64_t sectionLen = *uval - *lval;
if (strideVal) {
sectionLen = sectionLen / *strideVal;
}

if (sectionLen < 1) {
context_.Say(GetContext().clauseSource,
"'%s' in %s clause"
" is a zero size array section"_err_en_US,
name.ToString(),
parser::ToUpperCaseLetters(getClauseName(clause).str()));
break;
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions flang/test/Semantics/OpenMP/depend01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ program omp_depend
a(2:1) = b(2, 2)
!$omp end task

!ERROR: Stride should not be specified for array section in DEPEND clause
!$omp task shared(x) depend(in: a(5:10:1))
!ERROR: 'a' in DEPEND clause must have a positive stride
!ERROR: 'b' in DEPEND clause must have a positive stride
!ERROR: 'b' in DEPEND clause is a zero size array section
!$omp task shared(x) depend(in: a(10:5:-1)) depend(in: b(5:10:-1))
print *, a(5:10), b
!$omp end task

!ERROR: 'a' in DEPEND clause is a zero size array section
!$omp task shared(x) depend(in: a(1:5:10))
print *, a(5:10), b
!$omp end task

Expand Down