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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

amitamd7
Copy link

This patch handles the strided update in the #pragma omp target update from(data[a:b:c]) directive where 'c' represents the strided access leading to non-contiguous update in the data array when the offloaded execution returns the control back to host from device using the from clause.

Issue: Clang CodeGen where info is generated for the particular MapType (to, from, etc), it was failing to detect the strided access. Because of this, the MapType bits were incorrect when passed to runtime. This led to incorrect execution (contiguous) in the libomptarget runtime code.

Added a minimal testcase that verifies the working of the patch.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. clang:openmp OpenMP related changes to Clang offload labels Jun 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2025

@llvm/pr-subscribers-offload
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Amit Tiwari (amitamd7)

Changes

This patch handles the strided update in the #pragma omp target update from(data[a:b:c]) directive where 'c' represents the strided access leading to non-contiguous update in the data array when the offloaded execution returns the control back to host from device using the from clause.

Issue: Clang CodeGen where info is generated for the particular MapType (to, from, etc), it was failing to detect the strided access. Because of this, the MapType bits were incorrect when passed to runtime. This led to incorrect execution (contiguous) in the libomptarget runtime code.

Added a minimal testcase that verifies the working of the patch.


Full diff: https://github.com/llvm/llvm-project/pull/144635.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGOpenMPRuntime.cpp (+34-1)
  • (added) offload/test/offloading/strided_update.c (+51)
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 4173355491fd4..81a2dd0fae5c9 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -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())) {
+            if (auto Constant =
+                    StrideExpr->getIntegerConstantExpr(CGF.getContext())) {
+              int64_t StrideVal = Constant->getExtValue();
+              if (StrideVal != 1) {
+                // 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;
+          }
+        }
+      }
+    }
+
     bool IsPrevMemberReference = false;
 
     bool IsPartialMapped =
diff --git a/offload/test/offloading/strided_update.c b/offload/test/offloading/strided_update.c
new file mode 100644
index 0000000000000..fc47216fb5684
--- /dev/null
+++ b/offload/test/offloading/strided_update.c
@@ -0,0 +1,51 @@
+// Checks that "update from" clause in OpenMP is supported when the elements are updated in a non-contiguous manner.
+// 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;  
+}  
+

@amitamd7 amitamd7 marked this pull request as draft June 18, 2025 05:57
Comment on lines 7394 to 7419
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())) {
if (auto Constant =
StrideExpr->getIntegerConstantExpr(CGF.getContext())) {
int64_t StrideVal = Constant->getExtValue();
if (StrideVal != 1) {
// 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;
}
}
}
}
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

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?

Copy link
Author

Choose a reason for hiding this comment

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

Okay, got it. No, it does not implicitly check if it is an integer.

Copy link
Member

Choose a reason for hiding this comment

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

I think getIntegerConstantExpr calls isIntegerConstantExpr, so you can skip it.

if (StrideExpr) {
// Check if the stride is a constant integer expression
if (StrideExpr->isIntegerConstantExpr(CGF.getContext())) {
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 =

@@ -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

Comment on lines 7404 to 7405
int64_t StrideVal = Constant->getExtValue();
if (StrideVal != 1) {
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()) {

@amitamd7 amitamd7 force-pushed the tiwari_target_non-contiguous_strided_update branch from 31b83e2 to 1383c0e Compare June 26, 2025 12:06
@amitamd7
Copy link
Author

amitamd7 commented Jun 26, 2025

Please ignore the indentation changes. I'll fix them soon in the revised version.

@amitamd7 amitamd7 force-pushed the tiwari_target_non-contiguous_strided_update branch from 1383c0e to bdff136 Compare July 2, 2025 11:03
@amitamd7 amitamd7 marked this pull request as ready for review July 15, 2025 10:18
@amitamd7 amitamd7 requested a review from alexey-bataev July 15, 2025 10:18
@amitamd7
Copy link
Author

The PR has incorporated the revised changes. Please, have a look.

for (const auto &Component : Components) {
const auto *OASE =
dyn_cast<ArraySectionExpr>(Component.getAssociatedExpression());
if (OASE) {
Copy link
Member

Choose a reason for hiding this comment

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

if (!OASE)
  continue

Comment on lines 7393 to 7398
if (StrideExpr) {
if (const auto Constant =
StrideExpr->getIntegerConstantExpr(CGF.getContext())) {
if (!Constant->isOne()) {
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Use early exits to reduce strutured complexity

Copy link
Author

Choose a reason for hiding this comment

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

Sure.

Comment on lines 8727 to 8729
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
OMPTargetUpdateDirective>
ompTargetUpdateDirective;
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need this?

Copy link
Author

Choose a reason for hiding this comment

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

It specifically checks the omp target update directive. ompExecutableDirective would work too but its use case is generic.

Copy link
Member

Choose a reason for hiding this comment

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

The question is why do you need it in this patch?

Copy link
Member

Choose a reason for hiding this comment

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

Please, remove it here

Copy link
Author

Choose a reason for hiding this comment

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

Done

@@ -4724,6 +4724,65 @@ void x() {
EXPECT_TRUE(matchesWithOpenMP(Source8, Matcher));
}

TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsStandaloneDirective) {
Copy link
Member

Choose a reason for hiding this comment

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

Clang tests are required

Copy link
Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

These tests better to move a separate patch, if really needed

Copy link
Author

Choose a reason for hiding this comment

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

The tests are moved to a separate patch: #150580

@alexey-bataev
Copy link
Member

Also, update OpenMPSupport.rst and release notes

@amitamd7 amitamd7 marked this pull request as draft July 21, 2025 15:32
@amitamd7 amitamd7 marked this pull request as ready for review July 22, 2025 10:41
@amitamd7 amitamd7 force-pushed the tiwari_target_non-contiguous_strided_update branch from bdff136 to 7f897ff Compare July 23, 2025 11:45
@amitamd7
Copy link
Author

Also, update OpenMPSupport.rst and release notes

@amitamd7 amitamd7 closed this Jul 23, 2025
@amitamd7 amitamd7 reopened this Jul 23, 2025
Comment on lines 8727 to 8729
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
OMPTargetUpdateDirective>
ompTargetUpdateDirective;
Copy link
Member

Choose a reason for hiding this comment

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

The question is why do you need it in this patch?

Comment on lines 7391 to 7393
if (!OASE) {
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

Drop braces

Comment on lines 7395 to 7397
if (!StrideExpr) {
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

Drop braces

Comment on lines 7400 to 7405
if (!Constant) {
continue;
}
if (!Constant->isOne()) {
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Drop extra braces

// 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 {
Copy link
Member

Choose a reason for hiding this comment

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

bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous || ... to avoid doing some extra work if CombinedInfo.NonContigInfo.IsNonContiguous is already set

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense!

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp,c -- clang/test/OpenMP/target_update_strided_messages.c clang/test/OpenMP/target_update_strided_multiple_messages.c clang/test/OpenMP/target_update_strided_partial_messages.c offload/test/offloading/strided_multiple_update.c offload/test/offloading/strided_partial_update.c offload/test/offloading/strided_update.c clang/include/clang/ASTMatchers/ASTMatchers.h clang/lib/ASTMatchers/ASTMatchersInternal.cpp clang/lib/ASTMatchers/Dynamic/Registry.cpp clang/lib/CodeGen/CGOpenMPRuntime.cpp clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
View the diff from clang-format here.
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index fee3f952b..e3d086b0a 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -7409,7 +7409,7 @@ private:
           return true;
         }
       }
-      return CombinedInfo.NonContigInfo.IsNonContiguous; 
+      return CombinedInfo.NonContigInfo.IsNonContiguous;
     }();
 
     bool IsPrevMemberReference = false;

@amitamd7 amitamd7 force-pushed the tiwari_target_non-contiguous_strided_update branch from 7f897ff to 9afb5a6 Compare July 25, 2025 11:58
@amitamd7
Copy link
Author

@alexey-bataev I think OMP target update directive and from clause declaration in clang unit-tests is independent from this patch. I have initiated a new PR for the same.

#150580

Comment on lines 7388 to 7408
CombinedInfo.NonContigInfo.IsNonContiguous || [&]() -> bool {
for (const auto &Component : Components) {
const auto *OASE =
dyn_cast<ArraySectionExpr>(Component.getAssociatedExpression());
if (!OASE)
continue;

const Expr *StrideExpr = OASE->getStride();
if (!StrideExpr)
continue;

const auto Constant =
StrideExpr->getIntegerConstantExpr(CGF.getContext());
if (!Constant)
continue;

if (!Constant->isOne())
return true;
}
return false;
}();
Copy link
Member

Choose a reason for hiding this comment

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

.. || any_of(Components, [&](const auto &Component) {...};

@@ -4724,6 +4724,65 @@ void x() {
EXPECT_TRUE(matchesWithOpenMP(Source8, Matcher));
}

TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsStandaloneDirective) {
Copy link
Member

Choose a reason for hiding this comment

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

These tests better to move a separate patch, if really needed

@amitamd7 amitamd7 force-pushed the tiwari_target_non-contiguous_strided_update branch from 9afb5a6 to 2f4de4e Compare July 28, 2025 15:16
@amitamd7 amitamd7 force-pushed the tiwari_target_non-contiguous_strided_update branch from b95523d to 940d6dc Compare July 29, 2025 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category offload
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants