Skip to content

[Clang][Unittest] Support for target update directive and from clause in clang unittests #150580

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

@amitamd7 amitamd7 commented Jul 25, 2025

Added support for detecting OMP Target Update Directive and OMP From Clause in Clang Unit Test Framework

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 offload labels Jul 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2025

@llvm/pr-subscribers-offload

@llvm/pr-subscribers-clang

Author: Amit Tiwari (amitamd7)

Changes

Added support for detecting OMP Target Directive and OMP From Clause in Clang Unit Test Framework


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

4 Files Affected:

  • (modified) clang/include/clang/ASTMatchers/ASTMatchers.h (+27)
  • (modified) clang/lib/ASTMatchers/ASTMatchersInternal.cpp (+4)
  • (modified) clang/lib/ASTMatchers/Dynamic/Registry.cpp (+2)
  • (added) offload/test/offloading/strided_update.c (+50)
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 08c898f7758ec..5ca075528add5 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -8735,6 +8735,21 @@ 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 ``default`` clause.
 ///
 /// Given
@@ -8848,6 +8863,18 @@ AST_MATCHER_P(OMPExecutableDirective, isAllowedToContainClauseKind,
       Finder->getASTContext().getLangOpts().OpenMP);
 }
 
+/// Matches OpenMP ``from`` clause.
+///
+/// Given
+///
+/// \code
+///   #pragma omp target update from(a)
+/// \endcode
+///
+/// ``ompFromClause()`` matches ``from(a)``.
+extern const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPFromClause>
+    ompFromClause;
+
 //----------------------------------------------------------------------------//
 // End OpenMP handling.
 //----------------------------------------------------------------------------//
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 80dc888811657..7580cee3d8aed 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -1125,8 +1125,12 @@ AST_TYPELOC_TRAVERSE_MATCHER_DEF(
 
 const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
     ompExecutableDirective;
+const internal::VariadicDynCastAllOfMatcher<Stmt, OMPTargetUpdateDirective>
+    ompTargetUpdateDirective;
 const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPDefaultClause>
     ompDefaultClause;
+const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPFromClause>
+    ompFromClause;
 const internal::VariadicDynCastAllOfMatcher<Decl, CXXDeductionGuideDecl>
     cxxDeductionGuideDecl;
 
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 562df715e08ae..e74fae3c50029 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -531,7 +531,9 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(ofClass);
   REGISTER_MATCHER(ofKind);
   REGISTER_MATCHER(ompDefaultClause);
+  REGISTER_MATCHER(ompFromClause);
   REGISTER_MATCHER(ompExecutableDirective);
+  REGISTER_MATCHER(ompTargetUpdateDirective);
   REGISTER_MATCHER(on);
   REGISTER_MATCHER(onImplicitObjectArgument);
   REGISTER_MATCHER(opaqueValueExpr);
diff --git a/offload/test/offloading/strided_update.c b/offload/test/offloading/strided_update.c
new file mode 100644
index 0000000000000..271a00de5f7a1
--- /dev/null
+++ b/offload/test/offloading/strided_update.c
@@ -0,0 +1,50 @@
+// 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;  
+}  

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 -- offload/test/offloading/strided_update.c clang/include/clang/ASTMatchers/ASTMatchers.h clang/lib/ASTMatchers/ASTMatchersInternal.cpp clang/lib/ASTMatchers/Dynamic/Registry.cpp
View the diff from clang-format here.
diff --git a/offload/test/offloading/strided_update.c b/offload/test/offloading/strided_update.c
index 271a00de5..c223e159c 100644
--- a/offload/test/offloading/strided_update.c
+++ b/offload/test/offloading/strided_update.c
@@ -1,33 +1,34 @@
-// 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  
+// 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
@@ -41,10 +42,10 @@ int main() {
   // 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;  
-}  
+  printf("from target array results:\n");
+  for (int i = 0; i < len; i++)
+    printf("%f\n", data[i]);
+  printf("\n");
+
+  return 0;
+}

@amitamd7 amitamd7 force-pushed the tiwari_support_target_directive_clang_unittest branch from 85653b4 to 4b0be55 Compare July 25, 2025 09:17
@amitamd7 amitamd7 changed the title [Clang][Unittest] Support for target directive and from clause in clang unittests [Clang][Unittest] Support for target update directive and from clause in clang unittests Jul 25, 2025
Copy link
Contributor

@shiltian shiltian left a comment

Choose a reason for hiding this comment

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

it doesn't seem to be tested?

@amitamd7 amitamd7 force-pushed the tiwari_support_target_directive_clang_unittest branch from 4b0be55 to d7d3652 Compare July 28, 2025 11:44
@amitamd7 amitamd7 force-pushed the tiwari_support_target_directive_clang_unittest branch from d7d3652 to 615209b Compare July 28, 2025 11:49
@amitamd7
Copy link
Author

it doesn't seem to be tested?

I have added them now.

@saiislam saiislam requested a review from alexey-bataev July 29, 2025 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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