Skip to content

Commit 9554152

Browse files
kparzysztru
authored andcommitted
[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 (cherry picked from commit 8fcbd06)
1 parent 704f542 commit 9554152

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"
@@ -4117,21 +4116,26 @@ void OmpStructureChecker::CheckArraySection(
41174116
// Detect this by looking for array accesses on character variables which are
41184117
// not arrays.
41194118
bool isSubstring{false};
4120-
evaluate::ExpressionAnalyzer ea{context_};
4121-
if (MaybeExpr expr = ea.Analyze(arrayElement.base)) {
4122-
std::optional<evaluate::Shape> shape = evaluate::GetShape(expr);
4123-
// Not an array: rank 0
4124-
if (shape && shape->size() == 0) {
4125-
if (std::optional<evaluate::DynamicType> type = expr->GetType()) {
4126-
if (type->category() == evaluate::TypeCategory::Character) {
4127-
// Substrings are explicitly denied by the standard [6.0:163:9-11].
4128-
// This is supported as an extension. This restriction was added in
4129-
// OpenMP 5.2.
4130-
isSubstring = true;
4131-
context_.Say(GetContext().clauseSource,
4132-
"The use of substrings in OpenMP argument lists has been disallowed since OpenMP 5.2."_port_en_US);
4133-
} else {
4134-
llvm_unreachable("Array indexing on a variable that isn't an array");
4119+
// Cannot analyze a base of an assumed-size array on its own. If we know
4120+
// this is an array (assumed-size or not) we can ignore it, since we're
4121+
// looking for strings.
4122+
if (!IsAssumedSizeArray(*name.symbol)) {
4123+
evaluate::ExpressionAnalyzer ea{context_};
4124+
if (MaybeExpr expr = ea.Analyze(arrayElement.base)) {
4125+
if (expr->Rank() == 0) {
4126+
// Not an array: rank 0
4127+
if (std::optional<evaluate::DynamicType> type = expr->GetType()) {
4128+
if (type->category() == evaluate::TypeCategory::Character) {
4129+
// Substrings are explicitly denied by the standard [6.0:163:9-11].
4130+
// This is supported as an extension. This restriction was added in
4131+
// OpenMP 5.2.
4132+
isSubstring = true;
4133+
context_.Say(GetContext().clauseSource,
4134+
"The use of substrings in OpenMP argument lists has been disallowed since OpenMP 5.2."_port_en_US);
4135+
} else {
4136+
llvm_unreachable(
4137+
"Array indexing on a variable that isn't an array");
4138+
}
41354139
}
41364140
}
41374141
}
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)