Skip to content

[Clang][OpenMP] Non-contiguous strided update #144635

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
35 changes: 34 additions & 1 deletion clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7384,7 +7384,40 @@ class MappableExprsHandler {
// dimension.
uint64_t DimSize = 1;

bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous;
// Detects non-contiguous updates due to strided accesses.
// Sets the 'IsNonContiguous' flag so that the 'MapType' bits are set
// correctly when generating information to be passed to the runtime. The
// flag is set to true if any array section has a stride not equal to 1, or
// if the stride is not a constant expression (conservatively assumed
// non-contiguous).
bool IsNonContiguous = false;
for (const auto &Component : Components) {
const auto *OASE =
dyn_cast<ArraySectionExpr>(Component.getAssociatedExpression());
if (OASE) {
const Expr *StrideExpr = OASE->getStride();
if (StrideExpr) {
// Check if the stride is a constant integer expression
if (StrideExpr->isIntegerConstantExpr(CGF.getContext())) {
Copy link
Member

Choose a reason for hiding this comment

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

Do you need this check before calling getIntegerConstantExpr?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, because stride can be a variable/complex expression that is determined only at runtime, so conservatively treating it as non-contiguous.

Copy link
Member

Choose a reason for hiding this comment

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

You're answering wrong question. I'm asking if this check is required and is not covered by StrideExpr->getIntegerConstantExpr( already?

if (auto Constant =
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (auto Constant =
if (const auto Constant =

StrideExpr->getIntegerConstantExpr(CGF.getContext())) {
int64_t StrideVal = Constant->getExtValue();
if (StrideVal != 1) {
Comment on lines +7404 to +7405
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
int64_t StrideVal = Constant->getExtValue();
if (StrideVal != 1) {
if (!Constant->isOne()) {

// Set flag if stride is not 1 (i.e., non-contiguous update)
IsNonContiguous = true;
break;
}
}
} else {
// If stride is not a constant, conservatively treat as
// non-contiguous
IsNonContiguous = true;
break;
}
}
}
}
Comment on lines +7394 to +7419
Copy link
Member

Choose a reason for hiding this comment

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

Please, convert it to lambda or a separate function


bool IsPrevMemberReference = false;

bool IsPartialMapped =
Expand Down
51 changes: 51 additions & 0 deletions offload/test/offloading/strided_update.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Checks that "update from" clause in OpenMP is supported when the elements are updated in a non-contiguous manner.
Copy link
Member

Choose a reason for hiding this comment

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

Also, need a clang unit test

// RUN: %libomptarget-compile-run-and-check-generic
#include <omp.h>
#include <stdio.h>

int main() {
int len = 8;
double data[len];
#pragma omp target map(tofrom: len, data[0:len])
{
for (int i = 0; i < len; i++) {
data[i] = i;
}
}
// initial values
printf("original host array values:\n");
for (int i = 0; i < len; i++)
printf("%f\n", data[i]);
printf("\n");

#pragma omp target data map(to: len, data[0:len])
{
#pragma omp target
for (int i = 0; i < len; i++) {
data[i] += i ;
}

#pragma omp target update from(data[0:8:2])
}
// from results
// CHECK: 0.000000
// CHECK: 1.000000
// CHECK: 4.000000
// CHECK: 3.000000
// CHECK: 8.000000
// CHECK: 5.000000
// CHECK: 12.000000
// CHECK: 7.000000
// CHECK-NOT: 2.000000
// CHECK-NOT: 6.000000
// CHECK-NOT: 10.000000
// CHECK-NOT: 14.000000

printf("from target array results:\n");
for (int i = 0; i < len; i++)
printf("%f\n", data[i]);
printf("\n");

return 0;
}