Skip to content

Commit 1fbf33c

Browse files
abhinavgabaadurang
andauthored
[OpenMP][Clang] Use ATTACH map-type for list-items with base-pointers. (llvm#153683)
This adds support for using `ATTACH` map-type for proper pointer-attachment when mapping list-items that have base-pointers. For example, for the following: ```c int *p; #pragma omp target enter data map(p[1:10]) ``` The following maps are now emitted by clang: ``` (A) &p[0], &p[1], 10 * sizeof(p[1]), TO | FROM &p, &p[1], sizeof(p), ATTACH ``` Previously, the two possible maps emitted by clang were: ``` (B) &p[0], &p[1], 10 * sizeof(p[1]), TO | FROM (C) &p, &p[1], 10 * sizeof(p[1]), TO | FROM | PTR_AND_OBJ ```` (B) does not perform any pointer attachment, while (C) also maps the pointer p, both of which are incorrect. ----- With this change, we are using ATTACH-style maps, like `(A)`, for cases where the expression has a base-pointer. For example: ```cpp int *p, **pp; S *ps, **pps; ... map(p[0]) ... map(p[10:20]) ... map(*p) ... map(([20])p) ... map(ps->a) ... map(pps->p->a) ... map(pp[0][0]) ... map(*(pp + 10)[0]) ``` #### Grouping of maps based on attach base-pointers We also group mapping of clauses with the same base decl in the order of the increasing complexity of their base-pointers, e.g. for something like: ``` S **spp; map(spp[0][0], spp[0][0].a), // attach-ptr: spp[0] map(spp[0]), // attach-ptr: spp map(spp), // attach-ptr: N/A ``` We first map `spp`, then `spp[0]` then `spp[0][0]` and `spp[0][0].a`. This allows us to also group "struct" allocation based on their attach pointers. This resolves the issues of us always mapping everything from the beginning of the symbol `spp`. Each group is mapped independently, and at the same level, like `spp[0][0]` and its member `spp[0][0].a`, we still get map them together as part of the same contiguous struct `spp[0][0]`. This resolves issue llvm#141042. #### use_device_ptr/addr fixes The handling of `use_device_ptr/addr` was updated to use the attach-ptr information, and works for many cases that were failing before. It has to be done as part of this series because otherwise, the switch from ptr_to_obj to attach-style mapping would have caused regressions in existing use_device_ptr/addr tests. #### Handling of attach-pointers that are members of implicitly mapped structs: * When a struct member-pointer, like `p` below, is a base-pointer in a `map` clause on a target construct (like `map(p[0:1])`, and the base of that struct is either the `this` pointer (implicitly or explicitly), or a struct that is implicitly mapped on that construct, we add an implicit `map(p)` so that we don't implicitly map the full struct. ```c struct S { int *p; void f1() { #pragma omp target map(p[0:1]) // Implicitly map this->p, to ensure // that the implicit map of `this[:]` does // not map the full struct printf("%p %p\n", &p, p); } ``` #### Scope for improvement: * We may be able to compute attach-ptr expr while collecting component-lists in Sema. * But we cache the computation results already, and `findAttachPtrExpr` is fairly simple, and fast. * There may be a better way to implement semantic expr comparison. #### Needs future work: * Attach-style maps not yet emitted for declare mappers. * Mapping of class member references: We are still using PTR_AND_OBJ maps for them. We will likely need to change that to handle `ref_ptr/ref_ptee`, and `attach` map-type-modifier on them. * Implicit capturing of "this" needs to map the full `this[0:1]` unless there is an explicit map on one of the members, or a map with a member as its base-pointer. * Implicit map added for capturing a class member pointer needs to also add a zero-length-array-section map. * `use_device_addr` on array-sections-on-pointers need further improvements (documented using FIXMEs) #### Why a large PR While it's unfortunate that this PR has gotten large and difficult to review, the issue is that all the functional changes have to be made together, to prevent regressions from partially implemented changes. For example, the changes to capturing were previously done separately (llvm#145454), but they would still cause stability issues in absence of full attach-mapping. And attach-mapping needs those changes to be able to launch kernels. We extracted the utilities and functions, like those for finding attach-ptrs, or comparing exprs, out as a separate NFC PR that doesn't call those functions, just adds them (llvm#155625). Maybe the change that adds a new error message for use_device_addr on array-sections with non-var base-pointers could have been extracted out too (but that would have had to be a follow-up change in that case, and we would get comp-fails with this PR when the erroneous case was not caught/diagnosed). --------- Co-authored-by: Alex Duran <[email protected]>
1 parent 8af59b2 commit 1fbf33c

File tree

71 files changed

+5457
-3970
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+5457
-3970
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,18 @@ OpenMP Support
843843

844844
Improvements
845845
^^^^^^^^^^^^
846+
- Mapping of expressions that have base-pointers now conforms to the OpenMP's
847+
conditional pointer-attachment based on both pointee and poitner being
848+
present, and one being new. This also lays the foundation of supporting
849+
OpenMP 6.1's attach map-type modifier.
850+
- Several improvements were made to the handling of maps on list items involving
851+
multiple levels of pointer dereferences, including not mapping intermediate
852+
expressions, and grouping the items that share the same base-pointer, as
853+
belonging to the same containing structure.
854+
- Support of array-sections on ``use_device_addr`` was made more robust,
855+
including diagnosing when the array-section's base is not a named-variable.
856+
- Handling of ``use_device_addr`` and ``use_device_ptr`` in the presence of
857+
other maps with the same base-pointer/variable, was improved.
846858

847859
Additional Information
848860
======================

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11805,6 +11805,9 @@ def err_omp_expected_addressable_lvalue_or_array_item : Error<
1180511805
"expected addressable lvalue expression, array element%select{ or array section|, array section or array shaping expression}0%select{| of non 'omp_depend_t' type}1">;
1180611806
def err_omp_expected_named_var_member_or_array_expression: Error<
1180711807
"expected expression containing only member accesses and/or array sections based on named variables">;
11808+
def err_omp_expected_base_pointer_var_name_member_expr
11809+
: Error<"base-pointer is not a variable name%select{| or data member of "
11810+
"current class}0">;
1180811811
def err_omp_bit_fields_forbidden_in_clause : Error<
1180911812
"bit fields cannot be used to specify storage in a '%0' clause">;
1181011813
def err_array_section_does_not_specify_contiguous_storage : Error<

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 775 additions & 338 deletions
Large diffs are not rendered by default.

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 107 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,7 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
22062206
// | ptr | n.a. | - | x | - | - | bycopy|
22072207
// | ptr | n.a. | x | - | - | - | null |
22082208
// | ptr | n.a. | - | - | - | x | byref |
2209+
// | ptr | n.a. | - | - | - | x, x[] | bycopy|
22092210
// | ptr | n.a. | - | - | - | x[] | bycopy|
22102211
// | ptr | n.a. | - | - | x | | bycopy|
22112212
// | ptr | n.a. | - | - | x | x | bycopy|
@@ -2231,18 +2232,22 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
22312232
// - For pointers mapped by value that have either an implicit map or an
22322233
// array section, the runtime library may pass the NULL value to the
22332234
// device instead of the value passed to it by the compiler.
2235+
// - If both a pointer and a dereference of it are mapped, then the pointer
2236+
// should be passed by reference.
22342237

22352238
if (Ty->isReferenceType())
22362239
Ty = Ty->castAs<ReferenceType>()->getPointeeType();
22372240

2238-
// Locate map clauses and see if the variable being captured is referred to
2239-
// in any of those clauses. Here we only care about variables, not fields,
2240-
// because fields are part of aggregates.
2241+
// Locate map clauses and see if the variable being captured is mapped by
2242+
// itself, or referred to, in any of those clauses. Here we only care about
2243+
// variables, not fields, because fields are part of aggregates.
22412244
bool IsVariableAssociatedWithSection = false;
2245+
bool IsVariableItselfMapped = false;
22422246

22432247
DSAStack->checkMappableExprComponentListsForDeclAtLevel(
22442248
D, Level,
22452249
[&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
2250+
&IsVariableItselfMapped,
22462251
D](OMPClauseMappableExprCommon::MappableExprComponentListRef
22472252
MapExprComponents,
22482253
OpenMPClauseKind WhereFoundClauseKind) {
@@ -2258,8 +2263,19 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
22582263

22592264
assert(EI != EE && "Invalid map expression!");
22602265

2261-
if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
2262-
IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
2266+
if (isa<DeclRefExpr>(EI->getAssociatedExpression()) &&
2267+
EI->getAssociatedDeclaration() == D) {
2268+
IsVariableUsedInMapClause = true;
2269+
2270+
// If the component list has only one element, it's for mapping the
2271+
// variable itself, like map(p). This takes precedence in
2272+
// determining how it's captured, so we don't need to look further
2273+
// for any other maps that use the variable (like map(p[0]) etc.)
2274+
if (MapExprComponents.size() == 1) {
2275+
IsVariableItselfMapped = true;
2276+
return true;
2277+
}
2278+
}
22632279

22642280
++EI;
22652281
if (EI == EE)
@@ -2273,8 +2289,10 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
22732289
isa<MemberExpr>(EI->getAssociatedExpression()) ||
22742290
isa<OMPArrayShapingExpr>(Last->getAssociatedExpression())) {
22752291
IsVariableAssociatedWithSection = true;
2276-
// There is nothing more we need to know about this variable.
2277-
return true;
2292+
// We've found a case like map(p[0]) or map(p->a) or map(*p),
2293+
// so we are done with this particular map, but we need to keep
2294+
// looking in case we find a map(p).
2295+
return false;
22782296
}
22792297

22802298
// Keep looking for more map info.
@@ -2283,8 +2301,23 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
22832301

22842302
if (IsVariableUsedInMapClause) {
22852303
// If variable is identified in a map clause it is always captured by
2286-
// reference except if it is a pointer that is dereferenced somehow.
2287-
IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2304+
// reference except if it is a pointer that is dereferenced somehow, but
2305+
// not itself mapped.
2306+
//
2307+
// OpenMP 6.0, 7.1.1: Data sharing attribute rules, variables referenced
2308+
// in a construct::
2309+
// If a list item in a has_device_addr clause or in a map clause on the
2310+
// target construct has a base pointer, and the base pointer is a scalar
2311+
// variable *that is not a list item in a map clause on the construct*,
2312+
// the base pointer is firstprivate.
2313+
//
2314+
// OpenMP 4.5, 2.15.1.1: Data-sharing Attribute Rules for Variables
2315+
// Referenced in a Construct:
2316+
// If an array section is a list item in a map clause on the target
2317+
// construct and the array section is derived from a variable for which
2318+
// the type is pointer then that variable is firstprivate.
2319+
IsByRef = IsVariableItselfMapped ||
2320+
!(Ty->isPointerType() && IsVariableAssociatedWithSection);
22882321
} else {
22892322
// By default, all the data that has a scalar type is mapped by copy
22902323
// (except for reduction variables).
@@ -22944,8 +22977,10 @@ static void checkMappableExpressionList(
2294422977
OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
2294522978
ArrayRef<OpenMPMapModifierKind> Modifiers = {},
2294622979
bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
22947-
// We only expect mappable expressions in 'to', 'from', and 'map' clauses.
22948-
assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
22980+
// We only expect mappable expressions in 'to', 'from', 'map', and
22981+
// 'use_device_addr' clauses.
22982+
assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from ||
22983+
CKind == OMPC_use_device_addr) &&
2294922984
"Unexpected clause kind with mappable expressions!");
2295022985
unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
2295122986

@@ -24659,17 +24694,67 @@ SemaOpenMP::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
2465924694
// similar properties of a first private variable.
2466024695
DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
2466124696

24662-
// Create a mappable component for the list item. List items in this clause
24663-
// only need a component.
24664-
MVLI.VarBaseDeclarations.push_back(D);
24665-
MVLI.VarComponents.emplace_back();
24666-
Expr *Component = SimpleRefExpr;
24667-
if (VD && (isa<ArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) ||
24668-
isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts())))
24669-
Component =
24670-
SemaRef.DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get();
24671-
MVLI.VarComponents.back().emplace_back(Component, D,
24672-
/*IsNonContiguous=*/false);
24697+
// Use the map-like approach to fully populate VarComponents
24698+
OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
24699+
24700+
const Expr *BE = checkMapClauseExpressionBase(
24701+
SemaRef, RefExpr, CurComponents, OMPC_use_device_addr,
24702+
DSAStack->getCurrentDirective(),
24703+
/*NoDiagnose=*/false);
24704+
24705+
if (!BE)
24706+
continue;
24707+
24708+
assert(!CurComponents.empty() &&
24709+
"use_device_addr clause expression with no components!");
24710+
24711+
// OpenMP use_device_addr: If a list item is an array section, the array
24712+
// base must be a base language identifier. We caught the cases where
24713+
// the array-section has a base-variable in getPrivateItem. e.g.
24714+
// struct S {
24715+
// int a[10];
24716+
// }; S s1;
24717+
// ... use_device_addr(s1.a[0]) // not ok, caught already
24718+
//
24719+
// But we still neeed to verify that the base-pointer is also a
24720+
// base-language identifier, and catch cases like:
24721+
// int *pa[10]; *p;
24722+
// ... use_device_addr(pa[1][2]) // not ok, base-pointer is pa[1]
24723+
// ... use_device_addr(p[1]) // ok
24724+
// ... use_device_addr(this->p[1]) // ok
24725+
auto AttachPtrResult = OMPClauseMappableExprCommon::findAttachPtrExpr(
24726+
CurComponents, DSAStack->getCurrentDirective());
24727+
const Expr *AttachPtrExpr = AttachPtrResult.first;
24728+
24729+
if (AttachPtrExpr) {
24730+
const Expr *BaseExpr = AttachPtrExpr->IgnoreParenImpCasts();
24731+
bool IsValidBase = false;
24732+
24733+
if (isa<DeclRefExpr>(BaseExpr))
24734+
IsValidBase = true;
24735+
else if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr);
24736+
ME && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
24737+
IsValidBase = true;
24738+
24739+
if (!IsValidBase) {
24740+
SemaRef.Diag(ELoc,
24741+
diag::err_omp_expected_base_pointer_var_name_member_expr)
24742+
<< (SemaRef.getCurrentThisType().isNull() ? 0 : 1)
24743+
<< AttachPtrExpr->getSourceRange();
24744+
continue;
24745+
}
24746+
}
24747+
24748+
// Get the declaration from the components
24749+
ValueDecl *CurDeclaration = CurComponents.back().getAssociatedDeclaration();
24750+
assert(isa<CXXThisExpr>(BE) ||
24751+
CurDeclaration &&
24752+
"Unexpected null decl for use_device_addr clause.");
24753+
24754+
MVLI.VarBaseDeclarations.push_back(CurDeclaration);
24755+
MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
24756+
MVLI.VarComponents.back().append(CurComponents.begin(),
24757+
CurComponents.end());
2467324758
}
2467424759

2467524760
if (MVLI.ProcessedVarList.empty())

clang/test/OpenMP/bug59160.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ void zoo(void) {
88
xp[1] = &x[0];
99
short **xpp = &xp[0];
1010
x[1] = 111;
11-
#pragma omp target data map(tofrom: xpp[1][1]) use_device_addr(xpp[1][1])
11+
12+
// NOTE: use_device_addr on xpp[1][1] is non-compliant, as the base-pointer
13+
// is xpp[1], which is not a base-language identifier.
14+
#pragma omp target data map(tofrom: xpp[1][1]) //use_device_addr(xpp[1][1])
15+
// FIXME: The assumption that xpp should not be mapped is incorrect.
16+
// The base-pointer of the array-section is xpp[1], not xpp, so the implicit
17+
// clause on xpp, i.e. a zero-length array-section amp, should still be emitted.
1218
#pragma omp target has_device_addr(xpp[1][1])
1319
{
1420
xpp[1][1] = 222;

0 commit comments

Comments
 (0)