Skip to content

[Clang][OpenMP] Fix mapping of arrays of structs with members with mappers #142511

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,9 @@ OpenMP Support
open parenthesis. (#GH139665)
- An error is now emitted when OpenMP ``collapse`` and ``ordered`` clauses have
an argument larger than what can fit within a 64-bit integer.
- Fixed mapping of arrays of structs containing nested structs with user defined
mappers, by using compiler-generated default mappers for the outer structs for
such maps.

Improvements
^^^^^^^^^^^^
Expand Down
38 changes: 26 additions & 12 deletions clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22060,20 +22060,34 @@ static void checkMappableExpressionList(
Type.getCanonicalType(), UnresolvedMapper);
if (ER.isInvalid())
continue;
if (!ER.get() && isa<ArraySectionExpr>(VE)) {
// Create implicit mapper as needed.
QualType BaseType = VE->getType().getCanonicalType();
if (BaseType->isSpecificBuiltinType(BuiltinType::ArraySection)) {
const auto *OASE = cast<ArraySectionExpr>(VE->IgnoreParenImpCasts());
QualType BType = ArraySectionExpr::getBaseOriginalType(OASE->getBase());
QualType ElemType;
if (const auto *ATy = BType->getAsArrayTypeUnsafe())
ElemType = ATy->getElementType();
else
ElemType = BType->getPointeeType();

// If no user-defined mapper is found, we need to create an implicit one for
// arrays/array-sections on structs that have members that have
// user-defined mappers. This is needed to ensure that the mapper for the
// member is invoked when mapping each element of the array/array-section.
if (!ER.get()) {
QualType BaseType;

if (isa<ArraySectionExpr>(VE)) {
BaseType = VE->getType().getCanonicalType();
if (BaseType->isSpecificBuiltinType(BuiltinType::ArraySection)) {
const auto *OASE = cast<ArraySectionExpr>(VE->IgnoreParenImpCasts());
QualType BType =
ArraySectionExpr::getBaseOriginalType(OASE->getBase());
QualType ElemType;
if (const auto *ATy = BType->getAsArrayTypeUnsafe())
ElemType = ATy->getElementType();
else
ElemType = BType->getPointeeType();
BaseType = ElemType.getCanonicalType();
}
} else if (VE->getType()->isArrayType()) {
const ArrayType *AT = VE->getType()->getAsArrayTypeUnsafe();
const QualType ElemType = AT->getElementType();
BaseType = ElemType.getCanonicalType();
}
if (BaseType->getAsRecordDecl() &&

if (!BaseType.isNull() && BaseType->getAsRecordDecl() &&
isImplicitMapperNeeded(SemaRef, DSAS, BaseType, VE)) {
ER = buildImplicitMapper(SemaRef, BaseType, DSAS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fopenmp -ast-dump %s | FileCheck %s --check-prefix=DUM

typedef struct {
int a;
} C;
#pragma omp declare mapper(C s) map(to : s.a)

typedef struct {
int e;
C f;
int h;
} D;

void foo() {
D sa[10];
sa[1].e = 111;
sa[1].f.a = 222;

#pragma omp target map(tofrom : sa)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is identical to the array-section version, with the only difference being this line:

#pragma omp target map(tofrom : sa [0:2])

{
sa[0].e = 333;
sa[1].f.a = 444;
}
}

// DUM: -OMPDeclareMapperDecl{{.*}}<<invalid sloc>> <invalid sloc>
// DUM-NEXT: |-OMPMapClause {{.*}}<<invalid sloc>> <implicit>
// DUM-NEXT: | |-MemberExpr {{.*}}<line:9:3> 'int' lvalue .e
// DUM-NEXT: | | `-DeclRefExpr {{.*}}<<invalid sloc>> 'D' lvalue Var {{.*}} '_s' 'D'
// DUM-NEXT: | |-MemberExpr {{.*}}<line:10:3> 'C' lvalue .f {{.*}}
// DUM-NEXT: | | `-DeclRefExpr {{.*}}<<invalid sloc>> 'D' lvalue Var {{.*}} '_s' 'D'
// DUM-NEXT: | `-MemberExpr {{.*}}<line:11:3> 'int' lvalue .h {{.*}}
// DUM-NEXT: | `-DeclRefExpr {{.*}}<<invalid sloc>> 'D' lvalue Var {{.*}} '_s' 'D'
// DUM-NEXT: `-VarDecl {{.*}} <line:12:1> col:1 implicit used _s 'D'
Loading
Loading