[clang] [OpenMP] Clang Codegen Interop : Accept multiple init (PR #82604)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 28 10:30:45 PST 2024


https://github.com/SunilKuravinakop updated https://github.com/llvm/llvm-project/pull/82604

>From 6444ef3f60a9f8ed8b3f413997259fe5006396b7 Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Tue, 20 Feb 2024 11:29:49 -0600
Subject: [PATCH 1/6] Accept multiple init clauses in interop directive.

  Changes to be committed:
 	modified:   clang/lib/CodeGen/CGStmtOpenMP.cpp
---
 clang/lib/CodeGen/CGStmtOpenMP.cpp | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 8fd74697de3c0f..c512b0bd851f7a 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -7023,19 +7023,25 @@ void CodeGenFunction::EmitOMPInteropDirective(const OMPInteropDirective &S) {
                                      S.getSingleClause<OMPUseClause>())) &&
          "OMPNowaitClause clause is used separately in OMPInteropDirective.");
 
-  if (const auto *C = S.getSingleClause<OMPInitClause>()) {
-    llvm::Value *InteropvarPtr =
-        EmitLValue(C->getInteropVar()).getPointer(*this);
-    llvm::omp::OMPInteropType InteropType = llvm::omp::OMPInteropType::Unknown;
-    if (C->getIsTarget()) {
-      InteropType = llvm::omp::OMPInteropType::Target;
-    } else {
-      assert(C->getIsTargetSync() && "Expected interop-type target/targetsync");
-      InteropType = llvm::omp::OMPInteropType::TargetSync;
+  auto It = S.getClausesOfKind<OMPInitClause>();
+  if (!It.empty()) {
+    // Look at the multiple init clauses
+    for (auto C : It) {
+      llvm::Value *InteropvarPtr =
+          EmitLValue(C->getInteropVar()).getPointer(*this);
+      llvm::omp::OMPInteropType InteropType =
+          llvm::omp::OMPInteropType::Unknown;
+      if (C->getIsTarget()) {
+        InteropType = llvm::omp::OMPInteropType::Target;
+      } else {
+        assert(C->getIsTargetSync() &&
+               "Expected interop-type target/targetsync");
+        InteropType = llvm::omp::OMPInteropType::TargetSync;
+      }
+      OMPBuilder.createOMPInteropInit(Builder, InteropvarPtr, InteropType,
+                                      Device, NumDependences, DependenceList,
+                                      Data.HasNowaitClause);
     }
-    OMPBuilder.createOMPInteropInit(Builder, InteropvarPtr, InteropType, Device,
-                                    NumDependences, DependenceList,
-                                    Data.HasNowaitClause);
   } else if (const auto *C = S.getSingleClause<OMPDestroyClause>()) {
     llvm::Value *InteropvarPtr =
         EmitLValue(C->getInteropVar()).getPointer(*this);

>From f4779f0dfd0fbf3a9e851f3df276840f254d87a7 Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Sat, 24 Feb 2024 00:41:03 -0600
Subject: [PATCH 2/6] Adding a testcase for interop_codegen.cpp to check "init"
 clause being provided twice. This check is being done only for AMD GPU.

  Changes to be committed:
 	new file:   clang/test/OpenMP/interop_codegen.cpp
---
 clang/test/OpenMP/interop_codegen.cpp | 30 +++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 clang/test/OpenMP/interop_codegen.cpp

diff --git a/clang/test/OpenMP/interop_codegen.cpp b/clang/test/OpenMP/interop_codegen.cpp
new file mode 100644
index 00000000000000..6277db27bd4da8
--- /dev/null
+++ b/clang/test/OpenMP/interop_codegen.cpp
@@ -0,0 +1,30 @@
+// expected-no-diagnostics
+// REQUIRES: amdgpu-registered-target
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -o - | FileCheck %s
+
+#ifndef HEADER
+#define HEADER
+
+typedef void *omp_interop_t;
+#define omp_interop_none 0
+#define omp_ipr_fr_id -1
+typedef long omp_intptr_t;
+#define NULL 0
+
+extern omp_intptr_t omp_get_interop_int(const omp_interop_t, int, int *);
+
+int main() {
+  omp_interop_t i1 = omp_interop_none;
+  omp_interop_t obj = omp_interop_none;
+
+  #pragma omp interop init(targetsync: i1) init(targetsync: obj)
+  int id = (int )omp_get_interop_int(obj, omp_ipr_fr_id, NULL);
+  int id1 = (int )omp_get_interop_int(i1, omp_ipr_fr_id, NULL);
+
+
+}
+#endif
+
+// CHECK-LABEL: define {{.+}}main{{.+}} 
+// CHECK: call {{.+}}__tgt_interop_init({{.+}}i1{{.*}})
+// CHECK: call {{.+}}__tgt_interop_init({{.+}}obj{{.*}})

>From f7ddf6eb5df9ec9a1f84e5918f05854465ca74bc Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Sat, 24 Feb 2024 05:27:19 -0600
Subject: [PATCH 3/6] Added -fopenmp-targets=nvptx64-nvidia-cuda &
 -fopenmp-targets=powerpc64le-ibm-linux-gnu.   Changes to be committed:  
 modified:   clang/test/OpenMP/interop_codegen.cpp

---
 clang/test/OpenMP/interop_codegen.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/test/OpenMP/interop_codegen.cpp b/clang/test/OpenMP/interop_codegen.cpp
index 6277db27bd4da8..e814fd9b23d47c 100644
--- a/clang/test/OpenMP/interop_codegen.cpp
+++ b/clang/test/OpenMP/interop_codegen.cpp
@@ -1,6 +1,7 @@
 // expected-no-diagnostics
-// REQUIRES: amdgpu-registered-target
 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1  -verify -fopenmp -x c++ -std=c++11 -triple powerpc64le-unknown-unknown -fopenmp-targets=powerpc64le-ibm-linux-gnu -emit-llvm %s -o - | FileCheck %s
 
 #ifndef HEADER
 #define HEADER

>From fd26d6590d8bb3d86c02e52de536c3e4436a9820 Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Sat, 24 Feb 2024 12:46:01 -0600
Subject: [PATCH 4/6] Changing from "for (auto C : It)" to "for (const class
 OMPInitClause *C : It)".   Changes to be committed:  	modified:  
 clang/lib/CodeGen/CGStmtOpenMP.cpp

---
 clang/lib/CodeGen/CGStmtOpenMP.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index c512b0bd851f7a..f5a4e31b15b901 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -7026,7 +7026,7 @@ void CodeGenFunction::EmitOMPInteropDirective(const OMPInteropDirective &S) {
   auto It = S.getClausesOfKind<OMPInitClause>();
   if (!It.empty()) {
     // Look at the multiple init clauses
-    for (auto C : It) {
+    for (const class OMPInitClause *C : It) {
       llvm::Value *InteropvarPtr =
           EmitLValue(C->getInteropVar()).getPointer(*this);
       llvm::omp::OMPInteropType InteropType =

>From 51fbefbcf6c43ed51f29e703ff0c7cf6a4a34e98 Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Wed, 28 Feb 2024 05:48:27 -0600
Subject: [PATCH 5/6] Generation of llvm-IR for multiple init, use and destroy
 clauses.

  Changes to be committed:
 	modified:   clang/lib/CodeGen/CGStmtOpenMP.cpp
---
 clang/lib/CodeGen/CGStmtOpenMP.cpp | 40 +++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index f5a4e31b15b901..b018ffed072681 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -7023,10 +7023,10 @@ void CodeGenFunction::EmitOMPInteropDirective(const OMPInteropDirective &S) {
                                      S.getSingleClause<OMPUseClause>())) &&
          "OMPNowaitClause clause is used separately in OMPInteropDirective.");
 
-  auto It = S.getClausesOfKind<OMPInitClause>();
-  if (!It.empty()) {
+  auto ItOMPInitClause = S.getClausesOfKind<OMPInitClause>();
+  if (!ItOMPInitClause.empty()) {
     // Look at the multiple init clauses
-    for (const class OMPInitClause *C : It) {
+    for (const class OMPInitClause *C : ItOMPInitClause) {
       llvm::Value *InteropvarPtr =
           EmitLValue(C->getInteropVar()).getPointer(*this);
       llvm::omp::OMPInteropType InteropType =
@@ -7042,18 +7042,28 @@ void CodeGenFunction::EmitOMPInteropDirective(const OMPInteropDirective &S) {
                                       Device, NumDependences, DependenceList,
                                       Data.HasNowaitClause);
     }
-  } else if (const auto *C = S.getSingleClause<OMPDestroyClause>()) {
-    llvm::Value *InteropvarPtr =
-        EmitLValue(C->getInteropVar()).getPointer(*this);
-    OMPBuilder.createOMPInteropDestroy(Builder, InteropvarPtr, Device,
-                                       NumDependences, DependenceList,
-                                       Data.HasNowaitClause);
-  } else if (const auto *C = S.getSingleClause<OMPUseClause>()) {
-    llvm::Value *InteropvarPtr =
-        EmitLValue(C->getInteropVar()).getPointer(*this);
-    OMPBuilder.createOMPInteropUse(Builder, InteropvarPtr, Device,
-                                   NumDependences, DependenceList,
-                                   Data.HasNowaitClause);
+  }
+  auto ItOMPDestroyClause = S.getClausesOfKind<OMPDestroyClause>();
+  if (!ItOMPDestroyClause.empty()) {
+    // Look at the multiple destroy clauses
+    for (const class OMPDestroyClause *C : ItOMPDestroyClause) {
+      llvm::Value *InteropvarPtr =
+          EmitLValue(C->getInteropVar()).getPointer(*this);
+      OMPBuilder.createOMPInteropDestroy(Builder, InteropvarPtr, Device,
+                                         NumDependences, DependenceList,
+                                         Data.HasNowaitClause);
+    }
+  }
+  auto ItOMPUseClause = S.getClausesOfKind<OMPUseClause>();
+  if (!ItOMPUseClause.empty()) {
+    // Look at the multiple use clauses
+    for (const class OMPUseClause *C : ItOMPUseClause) {
+      llvm::Value *InteropvarPtr =
+          EmitLValue(C->getInteropVar()).getPointer(*this);
+      OMPBuilder.createOMPInteropUse(Builder, InteropvarPtr, Device,
+                                     NumDependences, DependenceList,
+                                     Data.HasNowaitClause);
+    }
   }
 }
 

>From 84edc81be33e9736a0aa302799579ec5209da7ec Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Wed, 28 Feb 2024 11:25:19 -0600
Subject: [PATCH 6/6] Adding code for multiple occurences of "use" and
 "destroy" clause.   Changes to be committed:  	modified:  
 clang/lib/CodeGen/CGStmtOpenMP.cpp  	modified:  
 clang/test/OpenMP/interop_codegen.cpp

---
 clang/lib/CodeGen/CGStmtOpenMP.cpp    |  6 +++---
 clang/test/OpenMP/interop_codegen.cpp | 12 ++++++++++--
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index b018ffed072681..3fbd2e03eb61df 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -7026,7 +7026,7 @@ void CodeGenFunction::EmitOMPInteropDirective(const OMPInteropDirective &S) {
   auto ItOMPInitClause = S.getClausesOfKind<OMPInitClause>();
   if (!ItOMPInitClause.empty()) {
     // Look at the multiple init clauses
-    for (const class OMPInitClause *C : ItOMPInitClause) {
+    for (const OMPInitClause *C : ItOMPInitClause) {
       llvm::Value *InteropvarPtr =
           EmitLValue(C->getInteropVar()).getPointer(*this);
       llvm::omp::OMPInteropType InteropType =
@@ -7046,7 +7046,7 @@ void CodeGenFunction::EmitOMPInteropDirective(const OMPInteropDirective &S) {
   auto ItOMPDestroyClause = S.getClausesOfKind<OMPDestroyClause>();
   if (!ItOMPDestroyClause.empty()) {
     // Look at the multiple destroy clauses
-    for (const class OMPDestroyClause *C : ItOMPDestroyClause) {
+    for (const OMPDestroyClause *C : ItOMPDestroyClause) {
       llvm::Value *InteropvarPtr =
           EmitLValue(C->getInteropVar()).getPointer(*this);
       OMPBuilder.createOMPInteropDestroy(Builder, InteropvarPtr, Device,
@@ -7057,7 +7057,7 @@ void CodeGenFunction::EmitOMPInteropDirective(const OMPInteropDirective &S) {
   auto ItOMPUseClause = S.getClausesOfKind<OMPUseClause>();
   if (!ItOMPUseClause.empty()) {
     // Look at the multiple use clauses
-    for (const class OMPUseClause *C : ItOMPUseClause) {
+    for (const OMPUseClause *C : ItOMPUseClause) {
       llvm::Value *InteropvarPtr =
           EmitLValue(C->getInteropVar()).getPointer(*this);
       OMPBuilder.createOMPInteropUse(Builder, InteropvarPtr, Device,
diff --git a/clang/test/OpenMP/interop_codegen.cpp b/clang/test/OpenMP/interop_codegen.cpp
index e814fd9b23d47c..9fefd88728c7e6 100644
--- a/clang/test/OpenMP/interop_codegen.cpp
+++ b/clang/test/OpenMP/interop_codegen.cpp
@@ -15,10 +15,14 @@ typedef long omp_intptr_t;
 extern omp_intptr_t omp_get_interop_int(const omp_interop_t, int, int *);
 
 int main() {
-  omp_interop_t i1 = omp_interop_none;
   omp_interop_t obj = omp_interop_none;
+  omp_interop_t i1 = omp_interop_none;
+  omp_interop_t i2 = omp_interop_none;
+  omp_interop_t i3 = omp_interop_none;
+  omp_interop_t i4 = omp_interop_none;
+  omp_interop_t i5 = omp_interop_none;
 
-  #pragma omp interop init(targetsync: i1) init(targetsync: obj)
+  #pragma omp interop init(targetsync: i1) init(targetsync: obj) use(i2) use(i3) destroy(i4) destroy(i5)
   int id = (int )omp_get_interop_int(obj, omp_ipr_fr_id, NULL);
   int id1 = (int )omp_get_interop_int(i1, omp_ipr_fr_id, NULL);
 
@@ -29,3 +33,7 @@ int main() {
 // CHECK-LABEL: define {{.+}}main{{.+}} 
 // CHECK: call {{.+}}__tgt_interop_init({{.+}}i1{{.*}})
 // CHECK: call {{.+}}__tgt_interop_init({{.+}}obj{{.*}})
+// CHECK: call {{.+}}__tgt_interop_destroy({{.+}}i4{{.*}})
+// CHECK: call {{.+}}__tgt_interop_destroy({{.+}}i5{{.*}})
+// CHECK: call {{.+}}__tgt_interop_use({{.+}}i2{{.*}})
+// CHECK: call {{.+}}__tgt_interop_use({{.+}}i3{{.*}})



More information about the cfe-commits mailing list