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 1 commit 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
27 changes: 27 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8713,6 +8713,33 @@ AST_MATCHER_P(OMPExecutableDirective, hasAnyClause,
Builder) != Clauses.end();
}

/// Matches any ``#pragma omp target update`` executable directive.
///
/// Given
///
/// \code
/// #pragma omp target update from(a)
/// #pragma omp target update to(b)
/// \endcode
///
/// ``ompTargetUpdateDirective()`` matches both ``omp target update from(a)``
/// and ``omp target update to(b)``.
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
OMPTargetUpdateDirective>
ompTargetUpdateDirective;

/// Matches OpenMP ``from`` clause.
///
/// Given
///
/// \code
/// #pragma omp target update from(a)
/// \endcode
///
/// ``ompFromClause()`` matches ``from(a)``.
extern const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPFromClause>
ompFromClause;

/// Matches OpenMP ``default`` clause.
///
/// Given
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,10 @@ AST_TYPELOC_TRAVERSE_MATCHER_DEF(

const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
ompExecutableDirective;
const internal::VariadicDynCastAllOfMatcher<Stmt, OMPTargetUpdateDirective>
ompTargetUpdateDirective;
const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPFromClause>
ompFromClause;
const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPDefaultClause>
ompDefaultClause;
const internal::VariadicDynCastAllOfMatcher<Decl, CXXDeductionGuideDecl>
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/ASTMatchers/Dynamic/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(ofKind);
REGISTER_MATCHER(ompDefaultClause);
REGISTER_MATCHER(ompExecutableDirective);
REGISTER_MATCHER(ompTargetUpdateDirective);
REGISTER_MATCHER(ompFromClause);
REGISTER_MATCHER(on);
REGISTER_MATCHER(onImplicitObjectArgument);
REGISTER_MATCHER(opaqueValueExpr);
Expand Down
26 changes: 25 additions & 1 deletion clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7378,7 +7378,31 @@ 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 = [&]() -> bool {
for (const auto &Component : Components) {
const auto *OASE =
dyn_cast<ArraySectionExpr>(Component.getAssociatedExpression());
if (OASE) {
const Expr *StrideExpr = OASE->getStride();
if (StrideExpr) {
if (const auto Constant =
StrideExpr->getIntegerConstantExpr(CGF.getContext())) {
if (!Constant->isOne()) {
return true;
}
}
}
}
}
return false;
}();

bool IsPrevMemberReference = false;

bool IsPartialMapped =
Expand Down
59 changes: 59 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4724,6 +4724,65 @@ void x() {
EXPECT_TRUE(matchesWithOpenMP(Source8, Matcher));
}

TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsStandaloneDirective) {
auto Matcher = ompTargetUpdateDirective(isStandaloneDirective());

StringRef Source0 = R"(
void foo() {
int arr[8];
#pragma omp target update from(arr[0:8:2])
;
}
)";
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
}

TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_HasStructuredBlock) {
StringRef Source0 = R"(
void foo() {
int arr[8];
#pragma omp target update from(arr[0:8:2])
;
}
)";
EXPECT_TRUE(notMatchesWithOpenMP(
Source0, ompTargetUpdateDirective(hasStructuredBlock(nullStmt()))));
}

TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_HasClause) {
auto Matcher = ompTargetUpdateDirective(hasAnyClause(anything()));

StringRef Source0 = R"(
void foo() {
int arr[8];
#pragma omp target update from(arr[0:8:2])
;
}
)";
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
}

TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsAllowedToContainClauseKind) {
auto Matcher = ompTargetUpdateDirective(
isAllowedToContainClauseKind(llvm::omp::OMPC_from));

StringRef Source0 = R"(
void x() {
;
}
)";
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher));

StringRef Source1 = R"(
void foo() {
int arr[8];
#pragma omp target update from(arr[0:8:2])
;
}
)";
EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher));
}

TEST_P(ASTMatchersTest, HasAnyBase_DirectBase) {
if (!GetParam().isCXX()) {
return;
Expand Down
26 changes: 26 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,32 @@ void x() {
EXPECT_TRUE(notMatchesWithOpenMP(Source2, Matcher));
}

TEST(ASTMatchersTestOpenMP, OMPTargetUpdateDirective) {
auto Matcher = stmt(ompTargetUpdateDirective());

StringRef Source0 = R"(
void foo() {
int arr[8];
#pragma omp target update from(arr[0:8:2])
;
}
)";
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
}

TEST(ASTMatchersTestOpenMP, OMPFromClause) {
auto Matcher = ompTargetUpdateDirective(hasAnyClause(ompFromClause()));

StringRef Source0 = R"(
void foo() {
int arr[8];
#pragma omp target update from(arr[0:8:2])
;
}
)";
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
}

TEST(ASTMatchersTestOpenMP, OMPDefaultClause) {
auto Matcher = ompExecutableDirective(hasAnyClause(ompDefaultClause()));

Expand Down
62 changes: 62 additions & 0 deletions offload/test/offloading/strided_multiple_update.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This test checks that #pragma omp target update from(data1[0:3:4],
// data2[0:2:5]) correctly updates disjoint strided sections of multiple arrays
// from the device to the host.

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

int main() {
int len = 12;
double data1[len], data2[len];

// Initial values
#pragma omp target map(tofrom : data1[0 : len], data2[0 : len])
{
for (int i = 0; i < len; i++) {
data1[i] = i;
data2[i] = i * 10;
}
}

printf("original host array values:\n");
printf("data1: ");
for (int i = 0; i < len; i++)
printf("%.1f ", data1[i]);
printf("\ndata2: ");
for (int i = 0; i < len; i++)
printf("%.1f ", data2[i]);
printf("\n\n");

#pragma omp target data map(to : data1[0 : len], data2[0 : len])
{
// Modify arrays on device
#pragma omp target
{
for (int i = 0; i < len; i++)
data1[i] += i;
for (int i = 0; i < len; i++)
data2[i] += 100;
}

// data1[0:3:4] // indices 0,4,8
// data2[0:2:5] // indices 0,5
#pragma omp target update from(data1[0 : 3 : 4], data2[0 : 2 : 5])
}

printf("device array values after update from:\n");
printf("data1: ");
for (int i = 0; i < len; i++)
printf("%.1f ", data1[i]);
printf("\ndata2: ");
for (int i = 0; i < len; i++)
printf("%.1f ", data2[i]);
printf("\n\n");

// CHECK: data1: 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0
// CHECK: data2: 0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0 110.0

// CHECK: data1: 0.0 1.0 2.0 3.0 8.0 5.0 6.0 7.0 16.0 9.0 10.0 11.0
// CHECK: data2: 100.0 10.0 20.0 30.0 40.0 150.0 60.0 70.0 80.0 90.0 100.0
// 110.0
}
63 changes: 63 additions & 0 deletions offload/test/offloading/strided_partial_update.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This test checks that #pragma omp target update from(data[0:4:3]) correctly
// updates every third element (stride 3) from the device to the host, partially
// across the array

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

int main() {
int len = 11;
double data[len];

#pragma omp target map(tofrom : 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 : data[0 : len])
{
// Modify arrays on device
#pragma omp target
for (int i = 0; i < len; i++)
data[i] += i;

#pragma omp target update from(data[0 : 4 : 3]) // indices 0,3,6,9
}

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

// CHECK: 0.000000
// CHECK: 1.000000
// CHECK: 2.000000
// CHECK: 3.000000
// CHECK: 4.000000
// CHECK: 5.000000
// CHECK: 6.000000
// CHECK: 7.000000
// CHECK: 8.000000
// CHECK: 9.000000
// CHECK: 10.000000

// CHECK: 0.000000
// CHECK: 1.000000
// CHECK: 2.000000
// CHECK: 6.000000
// CHECK: 4.000000
// CHECK: 5.000000
// CHECK: 12.000000
// CHECK: 7.000000
// CHECK: 8.000000
// CHECK: 18.000000
// CHECK: 10.000000
}
54 changes: 54 additions & 0 deletions offload/test/offloading/strided_update.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This test checks that "update from" clause in OpenMP is supported when the
// elements are updated in a non-contiguous manner. This test checks that
// #pragma omp target update from(data[0:4:2]) correctly updates only every
// other element (stride 2) from the device to the host

// 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])
{
// Modify arrays on device
#pragma omp target
for (int i = 0; i < len; i++) {
data[i] += i;
}

#pragma omp target update from(data[0 : 4 : 2])
}
// 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;
}