Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
d867ab3
[Clang][OpenMP] Capture mapped pointers on `target` by reference.
abhinavgaba Jun 23, 2025
97b2dff
Add another test, minor clang-format fix.
abhinavgaba Jun 24, 2025
a31ea2a
Update the comparator for sort, plus minor additions to offload tests.
abhinavgaba Jun 24, 2025
cc90643
[WIP] Use ATTACH maps for array-sections/subscripts on pointers.
abhinavgaba Jun 11, 2025
6ba75e1
[WIP] Commit some more non-debug print changes.
abhinavgaba Jun 25, 2025
001da09
[NFC][Clang] Refactor mapinfo generation for captured vars on target.
abhinavgaba Jul 2, 2025
1ebb7a9
Handle mapping of captured pointers before any dereference of them.
abhinavgaba Jul 2, 2025
7799d0a
No need to change the sorting since we now handle mapping of pointers…
abhinavgaba Jul 2, 2025
9f61909
[NFC][Clang] Refactor mapinfo generation for captured vars on target.
abhinavgaba Jul 2, 2025
5261578
Minor clang-format fix.
abhinavgaba Jul 3, 2025
145160b
[NFC][OpenMP] Add tests for mapping pointers and their dereferences.
abhinavgaba Jul 3, 2025
15e7d50
Minor clang-format fix.
abhinavgaba Jul 3, 2025
9a2fabb
Merge branch 'capture-info-refactor' into tgt-capture-mapped-ptrs-by-ref
abhinavgaba Jul 7, 2025
78fd5e5
Merge branch 'nfc-lit-test-updates-for-incoming-pr' into tgt-capture-…
abhinavgaba Jul 7, 2025
b1540b3
Minor NFC update to a test.
abhinavgaba Jul 7, 2025
f1746b6
Update tests with the current output.
abhinavgaba Jul 7, 2025
9408873
Merge branch 'tgt-capture-mapped-ptrs-by-ref' into map-ptr-array-sect…
abhinavgaba Jul 8, 2025
3ce181f
Fix findAttachComponent
abhinavgaba Jul 8, 2025
50c8d0e
Fix build issues, keep debug prints
abhinavgaba Jul 8, 2025
c23e01f
Fix handling of array-sections in findAttachComponent
abhinavgaba Jul 8, 2025
6c2df5c
Merge remote-tracking branch 'origin/main' into tgt-capture-mapped-pt…
abhinavgaba Jul 8, 2025
9adf9a8
Fix one test.
abhinavgaba Jul 8, 2025
ca0c381
Merge branch 'tgt-capture-mapped-ptrs-by-ref' into map-ptr-array-sect…
abhinavgaba Jul 8, 2025
36f6d9b
Fix star case. Update some tests.
abhinavgaba Jul 9, 2025
8349a49
Refactor AddAttachEntry
abhinavgaba Jul 9, 2025
eaf332f
Delay addition of attach when we populate PartialStruct.
abhinavgaba Jul 9, 2025
da55bf8
Update some more tests.
abhinavgaba Jul 16, 2025
5d72174
[WIP][Offload] Introduce ATTACH map-type support for pointer attachment.
abhinavgaba Jul 16, 2025
fcdc13a
More test updates
abhinavgaba Jul 16, 2025
c35d7a0
[WIP][Offload] Introduce ATTACH map-type support for pointer attachment.
abhinavgaba Jul 16, 2025
b645d58
Minor format/stylistic changes.
abhinavgaba Jul 16, 2025
13faca1
Minor formatting changes.
abhinavgaba Jul 16, 2025
54b2ae4
Remove debug prints.
abhinavgaba Jul 16, 2025
00b0767
Limit attach map-type generation to only map-entering constructs.
abhinavgaba Jul 16, 2025
baa9dbb
Minor clean-up
abhinavgaba Jul 16, 2025
c0d20e9
Minor NFC changes.
abhinavgaba Jul 20, 2025
a9b94d4
Use lb instead of base when attaching to a partialstruct.
abhinavgaba Jul 20, 2025
f1acc37
[NFC] Move computation of ptr/ptee addrs outside the loop.
abhinavgaba Jul 21, 2025
976470c
[NFC] Return Expr* from findAttachBasePtr.
abhinavgaba Jul 21, 2025
fd2d077
[NFC] Minor renaming/comment changes.
abhinavgaba Jul 21, 2025
fdced7d
Fix call to getAttachptrPteeAddrs.
abhinavgaba Jul 21, 2025
d791cb3
Remove unnecessary include.
abhinavgaba Jul 21, 2025
df62d2e
No need to compute the pointee address from scratch.
abhinavgaba Jul 22, 2025
63d2ca4
Update tests after avoiding creating pointee addr, and a minor commen…
abhinavgaba Jul 22, 2025
c9f8c0a
Move initialization of FinalLowestElem earlier.
abhinavgaba Jul 22, 2025
f044a56
Improve readability, add some more comments.
abhinavgaba Jul 23, 2025
de8d968
Merge remote-tracking branch 'upstream/main' into libomptarget-introd…
abhinavgaba Jul 27, 2025
168a2b8
add dataFence to plugin interface
adurang Jul 25, 2025
2370215
Clang-format fixes.
abhinavgaba Jul 27, 2025
2806389
Insert a data-fence before the first pointer-attachment.
abhinavgaba Jul 27, 2025
09c9cf3
Merge branch 'libomptarget-introduce-attach-support' into map-ptr-arr…
abhinavgaba Jul 27, 2025
e411f29
Update new upstream test.
abhinavgaba Jul 27, 2025
40320d2
Use unique_ptr, avoid c-style casts.
abhinavgaba Jul 30, 2025
fcdb2f5
Use unique_ptr, avoid c-style casts.
abhinavgaba Jul 30, 2025
39e4ab1
Add newline at end of file.
abhinavgaba Aug 1, 2025
9462953
Cache the attach-ptr-expr computation result, and reuse it if needed.
abhinavgaba Aug 1, 2025
371918e
Clean-up some debug prints.
abhinavgaba Aug 5, 2025
eb35a3e
Clean-up some debug prints.
abhinavgaba Aug 5, 2025
f1a4e80
[NFC][Offload] Add missing maps to OpenMP offloading tests.
abhinavgaba Aug 6, 2025
c15b2b9
[NFC][Clang][OpenMP] Extract collection of component-lists out of gen…
abhinavgaba Aug 6, 2025
4517cd2
Do pointee lookup and release its TPR before pointer lookup.
abhinavgaba Aug 8, 2025
d2e6a7c
Expand to remove the restriction to only support simple vars as attac…
abhinavgaba Aug 6, 2025
a78e9b0
Do pointee lookup and release its TPR before pointer lookup.
abhinavgaba Aug 8, 2025
4e3956c
Add an attach-ptr-expr comparator and use it to group components-list…
abhinavgaba Aug 9, 2025
3022b9a
Fix comparison of attach-ptrs, group component-lists by attach-ptr for
abhinavgaba Aug 10, 2025
788eb06
Fix matching of use_device_ptr/addr with existing map clauses.
abhinavgaba Aug 11, 2025
3dd11c1
Fix non-conforming tests.
abhinavgaba Aug 11, 2025
af72f97
Apply clang-format.
abhinavgaba Aug 11, 2025
2b31572
Add a fixme.
abhinavgaba Aug 12, 2025
dcd65fc
Remove debug-prints
abhinavgaba Aug 12, 2025
cb21ea4
Use a lambda to avoid uninitialized TgtPteeBegin.
abhinavgaba Aug 13, 2025
ac04da3
Merge remote-tracking branch 'upstream/main' into libomptarget-introd…
abhinavgaba Aug 13, 2025
63531f8
Remove debug prints. Update some tests.
abhinavgaba Aug 14, 2025
6c3def5
Merge branch 'libomptarget-introduce-attach-support' into map-ptr-arr…
abhinavgaba Aug 14, 2025
fb6a48c
Merge remote-tracking branch 'upstream/main' into map-ptr-array-secti…
abhinavgaba Aug 14, 2025
502dbb4
Update copy-gaps tests.
abhinavgaba Aug 14, 2025
9b1336c
Fix member-of field update, update some more tests.
abhinavgaba Aug 18, 2025
b84885a
Merge remote-tracking branch 'upstream/main' into map-ptr-array-secti…
abhinavgaba Aug 18, 2025
e5d22be
Fix use_device_ptr codegen and test.
abhinavgaba Aug 18, 2025
a4be53c
Re-add missing non-contiguous hanlding code, update target update tests.
abhinavgaba Aug 19, 2025
166e90e
Update use_device_ptr/addr handling, add an error for p[0] case, move…
abhinavgaba Aug 20, 2025
b86bd0a
[NFC][OpenMP] Add various combinations of use_device_ptr/addr tests.
abhinavgaba Aug 22, 2025
bf998da
Update use_device_ptr/addr tests.
abhinavgaba Aug 22, 2025
398a3a6
Clang-format fixes
abhinavgaba Aug 22, 2025
3e4d99b
Add two empty lines.
abhinavgaba Aug 22, 2025
a49143a
Clang-format renamed files as well.
abhinavgaba Aug 22, 2025
609a53c
Merge remote-tracking branch 'upstream/main' into add-use-device-ptr-…
abhinavgaba Aug 22, 2025
f7ef799
Merge branch 'add-use-device-ptr-addr-tests' into map-ptr-array-secti…
abhinavgaba Aug 23, 2025
b0170f7
Remove XFAIL from new uda/udp tests.
abhinavgaba Aug 24, 2025
63d7c8f
Add an implicit map of attach-ptr on a target construct.
abhinavgaba Aug 25, 2025
ba9b90c
Merge remote-tracking branch 'upstream/main' into map-ptr-array-secti…
abhinavgaba Aug 25, 2025
60c0ac2
Update header comment showing various maps in CGOpenMPRuntime.cpp
abhinavgaba Aug 25, 2025
38bf1a0
Minor NFC, comment changes.
abhinavgaba Aug 25, 2025
8b18577
Fix another obsolete test.
abhinavgaba Aug 26, 2025
84f1173
Remove some obsolete code, update some comments.
abhinavgaba Aug 27, 2025
6c0f62e
Only add implicit maps for base-ptr for 's.p[0:1]', not for 'ps->p[0:…
abhinavgaba Aug 27, 2025
c5a4be6
Update comment showing maps in CGOpenMPRuntime.
abhinavgaba Aug 27, 2025
087945a
Update test for addr error messages for array-section operands.
abhinavgaba Aug 27, 2025
707dfce
[Offload] Update LIBOMPTARGET_INFO text for "attach" map-type.
abhinavgaba Aug 26, 2025
2e9dde2
Fix pointer-type used to load from attach-ptr-addr.
abhinavgaba Aug 29, 2025
1cc6abf
Emit a single attach entry per attach-ptr group.
abhinavgaba Aug 29, 2025
a361e99
Apply clang-format
abhinavgaba Aug 29, 2025
3b11e7b
Minor comment change.
abhinavgaba Sep 6, 2025
1c93b72
Merge remote-tracking branch 'upstream/main' into map-ptr-array-secti…
abhinavgaba Sep 8, 2025
4ae1aa6
Update tests after merging upstream changes
abhinavgaba Sep 8, 2025
ff9ca6e
Add/update some test comments.
abhinavgaba Sep 8, 2025
f42f6a8
Remove some unnecessary braces
abhinavgaba Sep 8, 2025
9d5c26c
Use order of computation, instead of exprLocs for comparing attach-pt…
abhinavgaba Sep 22, 2025
936f0ec
Merge remote-tracking branch 'origin/main' into map-ptr-array-section…
abhinavgaba Sep 25, 2025
91f8432
Merge remote-tracking branch 'origin/main' into map-ptr-array-section…
abhinavgaba Sep 25, 2025
dc73180
Update ReleaseNotes.rst.
abhinavgaba Sep 25, 2025
9e4a7b5
Use sorted vectors instead of std::map
abhinavgaba Sep 29, 2025
6aa1cfe
Reuse the same comparator object for the same MEH.
abhinavgaba Sep 29, 2025
c7c3218
Use stable sort instead of llvm::sort
abhinavgaba Sep 29, 2025
245f43d
Minor NFC cleanup
abhinavgaba Sep 29, 2025
9e552ad
Use getTypeSizeInChars
abhinavgaba Sep 30, 2025
2f7b421
Merge remote-tracking branch 'upstream/main' into map-ptr-array-secti…
abhinavgaba Oct 6, 2025
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
12 changes: 12 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,18 @@ OpenMP Support

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

Additional Information
======================
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -11702,6 +11702,9 @@ def err_omp_expected_addressable_lvalue_or_array_item : Error<
"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">;
def err_omp_expected_named_var_member_or_array_expression: Error<
"expected expression containing only member accesses and/or array sections based on named variables">;
def err_omp_expected_base_pointer_var_name_member_expr
: Error<"base-pointer is not a variable name%select{| or data member of "
"current class}0">;
def err_omp_bit_fields_forbidden_in_clause : Error<
"bit fields cannot be used to specify storage in a '%0' clause">;
def err_array_section_does_not_specify_contiguous_storage : Error<
Expand Down
1,079 changes: 803 additions & 276 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp

Large diffs are not rendered by default.

129 changes: 107 additions & 22 deletions clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,7 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
// | ptr | n.a. | - | x | - | - | bycopy|
// | ptr | n.a. | x | - | - | - | null |
// | ptr | n.a. | - | - | - | x | byref |
// | ptr | n.a. | - | - | - | x, x[] | bycopy|
// | ptr | n.a. | - | - | - | x[] | bycopy|
// | ptr | n.a. | - | - | x | | bycopy|
// | ptr | n.a. | - | - | x | x | bycopy|
Expand All @@ -2242,18 +2243,22 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
// - For pointers mapped by value that have either an implicit map or an
// array section, the runtime library may pass the NULL value to the
// device instead of the value passed to it by the compiler.
// - If both a pointer an a dereference of it are mapped, then the pointer
// should be passed by reference.

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

// Locate map clauses and see if the variable being captured is referred to
// in any of those clauses. Here we only care about variables, not fields,
// because fields are part of aggregates.
// Locate map clauses and see if the variable being captured is mapped by
// itself, or referred to, in any of those clauses. Here we only care about
// variables, not fields, because fields are part of aggregates.
bool IsVariableAssociatedWithSection = false;
bool IsVariableItselfMapped = false;

DSAStack->checkMappableExprComponentListsForDeclAtLevel(
D, Level,
[&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
&IsVariableItselfMapped,
D](OMPClauseMappableExprCommon::MappableExprComponentListRef
MapExprComponents,
OpenMPClauseKind WhereFoundClauseKind) {
Expand All @@ -2269,8 +2274,19 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,

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

if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
if (isa<DeclRefExpr>(EI->getAssociatedExpression()) &&
EI->getAssociatedDeclaration() == D) {
IsVariableUsedInMapClause = true;

// If the component list has only one element, it's for mapping the
// variable itself, like map(p). This takes precedence in
// determining how it's captured, so we don't need to look further
// for any other maps that use the variable (like map(p[0]) etc.)
if (MapExprComponents.size() == 1) {
IsVariableItselfMapped = true;
return true;
}
}

++EI;
if (EI == EE)
Expand All @@ -2284,8 +2300,10 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
isa<MemberExpr>(EI->getAssociatedExpression()) ||
isa<OMPArrayShapingExpr>(Last->getAssociatedExpression())) {
IsVariableAssociatedWithSection = true;
// There is nothing more we need to know about this variable.
return true;
// We've found a case like map(p[0]) or map(p->a) or map(*p),
// so we are done with this particular map, but we need to keep
// looking in case we find a map(p).
return false;
}

// Keep looking for more map info.
Expand All @@ -2294,8 +2312,23 @@ bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,

if (IsVariableUsedInMapClause) {
// If variable is identified in a map clause it is always captured by
// reference except if it is a pointer that is dereferenced somehow.
IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
// reference except if it is a pointer that is dereferenced somehow, but
// not itself mapped.
//
// OpenMP 6.0, 7.1.1: Data sharing attribute rules, variables referenced
// in a construct::
// If a list item in a has_device_addr clause or in a map clause on the
// target construct has a base pointer, and the base pointer is a scalar
// variable *that is not a list item in a map clause on the construct*,
// the base pointer is firstprivate.
//
// OpenMP 4.5, 2.15.1.1: Data-sharing Attribute Rules for Variables
// Referenced in a Construct:
// If an array section is a list item in a map clause on the target
// construct and the array section is derived from a variable for which
// the type is pointer then that variable is firstprivate.
IsByRef = IsVariableItselfMapped ||
!(Ty->isPointerType() && IsVariableAssociatedWithSection);
} else {
// By default, all the data that has a scalar type is mapped by copy
// (except for reduction variables).
Expand Down Expand Up @@ -22838,8 +22871,10 @@ static void checkMappableExpressionList(
OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
ArrayRef<OpenMPMapModifierKind> Modifiers = {},
bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
// We only expect mappable expressions in 'to', 'from', and 'map' clauses.
assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
// We only expect mappable expressions in 'to', 'from', 'map', and
// 'use_device_addr' clauses.
assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from ||
CKind == OMPC_use_device_addr) &&
"Unexpected clause kind with mappable expressions!");
unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;

Expand Down Expand Up @@ -24543,17 +24578,67 @@ SemaOpenMP::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
// similar properties of a first private variable.
DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);

// Create a mappable component for the list item. List items in this clause
// only need a component.
MVLI.VarBaseDeclarations.push_back(D);
MVLI.VarComponents.emplace_back();
Expr *Component = SimpleRefExpr;
if (VD && (isa<ArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) ||
isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts())))
Component =
SemaRef.DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get();
MVLI.VarComponents.back().emplace_back(Component, D,
/*IsNonContiguous=*/false);
// Use the map-like approach to fully populate VarComponents
OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;

const Expr *BE = checkMapClauseExpressionBase(
SemaRef, RefExpr, CurComponents, OMPC_use_device_addr,
DSAStack->getCurrentDirective(),
/*NoDiagnose=*/false);

if (!BE)
continue;

assert(!CurComponents.empty() &&
"use_device_addr clause expression with no components!");

// OpenMP use_device_addr: If a list item is an array section, the array
// base must be a base language identifier. We caught the cases where
// the array-section has a base-variable in getPrivateItem. e.g.
// struct S {
// int a[10];
// }; S s1;
// ... use_device_addr(s1.a[0]) // not ok, caught already
//
// But we still neeed to verify that the base-pointer is also a
// base-language identifier, and catch cases like:
// int *pa[10]; *p;
// ... use_device_addr(pa[1][2]) // not ok, base-pointer is pa[1]
// ... use_device_addr(p[1]) // ok
// ... use_device_addr(this->p[1]) // ok
auto AttachPtrResult = OMPClauseMappableExprCommon::findAttachPtrExpr(
CurComponents, DSAStack->getCurrentDirective());
const Expr *AttachPtrExpr = AttachPtrResult.first;

if (AttachPtrExpr) {
const Expr *BaseExpr = AttachPtrExpr->IgnoreParenImpCasts();
bool IsValidBase = false;

if (isa<DeclRefExpr>(BaseExpr))
IsValidBase = true;
else if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr);
ME && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
IsValidBase = true;

if (!IsValidBase) {
SemaRef.Diag(ELoc,
diag::err_omp_expected_base_pointer_var_name_member_expr)
<< (SemaRef.getCurrentThisType().isNull() ? 0 : 1)
<< AttachPtrExpr->getSourceRange();
continue;
}
}

// Get the declaration from the components
ValueDecl *CurDeclaration = CurComponents.back().getAssociatedDeclaration();
assert(isa<CXXThisExpr>(BE) ||
CurDeclaration &&
"Unexpected null decl for use_device_addr clause.");

MVLI.VarBaseDeclarations.push_back(CurDeclaration);
MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
MVLI.VarComponents.back().append(CurComponents.begin(),
CurComponents.end());
}

if (MVLI.ProcessedVarList.empty())
Expand Down
8 changes: 7 additions & 1 deletion clang/test/OpenMP/bug59160.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ void zoo(void) {
xp[1] = &x[0];
short **xpp = &xp[0];
x[1] = 111;
#pragma omp target data map(tofrom: xpp[1][1]) use_device_addr(xpp[1][1])

// NOTE: use_device_addr on xpp[1][1] is non-compliant, as the base-pointer
// is xpp[1], which is not a base-language identifier.
#pragma omp target data map(tofrom: xpp[1][1]) //use_device_addr(xpp[1][1])
// FIXME: The assumption that xpp should not be mapped is incorrect.
// The base-pointer of the array-section is xpp[1], not xpp, so the implicit
// clause on xpp, i.e. a zero-length array-section amp, should still be emitted.
#pragma omp target has_device_addr(xpp[1][1])
{
xpp[1][1] = 222;
Expand Down
Loading