Skip to content

Commit 8fcbd06

Browse files
authored
[flang][OpenMP] Avoid analyzing assumed-size array bases (#150324)
A check for character substrings masquerading as array sections was using expression analyzer on the array base. When this array happened to be an assumed-size array, the analyzer emitted a semantic error that did not correspond to any issue with the source code. To avoid that, check whether the object is an assumed-size array before using the expression analyzer on it. While at it, replace the call to GetShape with a simple check for rank, since that's the only information needed. Fixes #150297
1 parent 588845d commit 8fcbd06

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "flang/Common/idioms.h"
1717
#include "flang/Common/indirection.h"
1818
#include "flang/Common/visit.h"
19-
#include "flang/Evaluate/shape.h"
2019
#include "flang/Evaluate/tools.h"
2120
#include "flang/Evaluate/type.h"
2221
#include "flang/Parser/char-block.h"
@@ -4151,21 +4150,26 @@ void OmpStructureChecker::CheckArraySection(
41514150
// Detect this by looking for array accesses on character variables which are
41524151
// not arrays.
41534152
bool isSubstring{false};
4154-
evaluate::ExpressionAnalyzer ea{context_};
4155-
if (MaybeExpr expr = ea.Analyze(arrayElement.base)) {
4156-
std::optional<evaluate::Shape> shape = evaluate::GetShape(expr);
4157-
// Not an array: rank 0
4158-
if (shape && shape->size() == 0) {
4159-
if (std::optional<evaluate::DynamicType> type = expr->GetType()) {
4160-
if (type->category() == evaluate::TypeCategory::Character) {
4161-
// Substrings are explicitly denied by the standard [6.0:163:9-11].
4162-
// This is supported as an extension. This restriction was added in
4163-
// OpenMP 5.2.
4164-
isSubstring = true;
4165-
context_.Say(GetContext().clauseSource,
4166-
"The use of substrings in OpenMP argument lists has been disallowed since OpenMP 5.2."_port_en_US);
4167-
} else {
4168-
llvm_unreachable("Array indexing on a variable that isn't an array");
4153+
// Cannot analyze a base of an assumed-size array on its own. If we know
4154+
// this is an array (assumed-size or not) we can ignore it, since we're
4155+
// looking for strings.
4156+
if (!IsAssumedSizeArray(*name.symbol)) {
4157+
evaluate::ExpressionAnalyzer ea{context_};
4158+
if (MaybeExpr expr = ea.Analyze(arrayElement.base)) {
4159+
if (expr->Rank() == 0) {
4160+
// Not an array: rank 0
4161+
if (std::optional<evaluate::DynamicType> type = expr->GetType()) {
4162+
if (type->category() == evaluate::TypeCategory::Character) {
4163+
// Substrings are explicitly denied by the standard [6.0:163:9-11].
4164+
// This is supported as an extension. This restriction was added in
4165+
// OpenMP 5.2.
4166+
isSubstring = true;
4167+
context_.Say(GetContext().clauseSource,
4168+
"The use of substrings in OpenMP argument lists has been disallowed since OpenMP 5.2."_port_en_US);
4169+
} else {
4170+
llvm_unreachable(
4171+
"Array indexing on a variable that isn't an array");
4172+
}
41694173
}
41704174
}
41714175
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
2+
3+
! This should compile without errors. Check for a symptom of a reasonable
4+
! output.
5+
6+
!CHECK: omp.task depend
7+
8+
subroutine omp_task_depend_reproducer(work, myid, shift)
9+
implicit none
10+
integer, intent(in) :: myid, shift
11+
real, intent(inout) :: work(*)
12+
13+
!$omp parallel shared(work, myid, shift)
14+
!$omp single
15+
!$omp task depend(in:work(myid+shift-1)) depend(in:work(myid-1)) depend(out:work(myid))
16+
call dummy_kernel(work(myid))
17+
!$omp end task
18+
!$omp end single
19+
!$omp end parallel
20+
contains
21+
subroutine dummy_kernel(x)
22+
real :: x
23+
x = x + 1.0
24+
end subroutine dummy_kernel
25+
end subroutine omp_task_depend_reproducer

0 commit comments

Comments
 (0)