[clang] Fix for codegen Crash in Clang when using locator omp_all_memory with depobj construct (PR #114221)

CHANDRA GHALE via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 10 23:52:19 PST 2024


https://github.com/chandraghale updated https://github.com/llvm/llvm-project/pull/114221

>From 4e6d8c1edb73fe08659519d8798cab162875ebc0 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe31.hpc.amslabs.hpecorp.net>
Date: Wed, 30 Oct 2024 07:18:06 -0500
Subject: [PATCH 1/4] Fix for codegen Crash in Clang when using locator
 omp_all_memory with depobj

---
 clang/lib/CodeGen/CGStmtOpenMP.cpp   | 10 ++++++----
 clang/test/OpenMP/depobj_codegen.cpp |  2 ++
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 1c32a675380c7f..5125044b2aa629 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -5556,12 +5556,14 @@ void CodeGenFunction::EmitOMPDepobjDirective(const OMPDepobjDirective &S) {
   const auto *DO = S.getSingleClause<OMPDepobjClause>();
   LValue DOLVal = EmitLValue(DO->getDepobj());
   if (const auto *DC = S.getSingleClause<OMPDependClause>()) {
-    OMPTaskDataTy::DependData Dependencies(DC->getDependencyKind(),
-                                           DC->getModifier());
-    Dependencies.DepExprs.append(DC->varlist_begin(), DC->varlist_end());
+    // Build list and emit dependences
+    OMPTaskDataTy Data;
+    buildDependences(S, Data);
+    for (auto &Dep : Data.Dependences) {
     Address DepAddr = CGM.getOpenMPRuntime().emitDepobjDependClause(
-        *this, Dependencies, DC->getBeginLoc());
+        *this, Dep, DC->getBeginLoc());
     EmitStoreOfScalar(DepAddr.emitRawPointer(*this), DOLVal);
+    }
     return;
   }
   if (const auto *DC = S.getSingleClause<OMPDestroyClause>()) {
diff --git a/clang/test/OpenMP/depobj_codegen.cpp b/clang/test/OpenMP/depobj_codegen.cpp
index 92751ac44b8c78..00cf43f54d6695 100644
--- a/clang/test/OpenMP/depobj_codegen.cpp
+++ b/clang/test/OpenMP/depobj_codegen.cpp
@@ -36,6 +36,8 @@ int main(int argc, char **argv) {
 #pragma omp depobj(b) update(mutexinoutset)
 #pragma omp depobj(a) depend(iterator(char *p = argv[argc]:argv[0]:-1), out: p[0])
   (void)tmain(a), tmain(b);
+   omp_depend_t obj;
+#pragma omp depobj(obj) depend(inout: omp_all_memory)
   return 0;
 }
 

>From 30eb82552f431604695ac55e584bb2bfcf6a927d Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe31.hpc.amslabs.hpecorp.net>
Date: Mon, 4 Nov 2024 02:13:38 -0600
Subject: [PATCH 2/4] clang-format fix

---
 clang/lib/CodeGen/CGStmtOpenMP.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 5125044b2aa629..390516fea38498 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -5560,9 +5560,9 @@ void CodeGenFunction::EmitOMPDepobjDirective(const OMPDepobjDirective &S) {
     OMPTaskDataTy Data;
     buildDependences(S, Data);
     for (auto &Dep : Data.Dependences) {
-    Address DepAddr = CGM.getOpenMPRuntime().emitDepobjDependClause(
-        *this, Dep, DC->getBeginLoc());
-    EmitStoreOfScalar(DepAddr.emitRawPointer(*this), DOLVal);
+      Address DepAddr = CGM.getOpenMPRuntime().emitDepobjDependClause(
+          *this, Dep, DC->getBeginLoc());
+      EmitStoreOfScalar(DepAddr.emitRawPointer(*this), DOLVal);
     }
     return;
   }

>From a35fe7e37fff62ca06a838a91f0c7b1149ccb165 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe31.hpc.amslabs.hpecorp.net>
Date: Thu, 7 Nov 2024 04:30:06 -0600
Subject: [PATCH 3/4] updated with codegen checks

---
 clang/test/OpenMP/depobj_codegen.cpp | 32 +++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/clang/test/OpenMP/depobj_codegen.cpp b/clang/test/OpenMP/depobj_codegen.cpp
index 00cf43f54d6695..8c7dce8d734060 100644
--- a/clang/test/OpenMP/depobj_codegen.cpp
+++ b/clang/test/OpenMP/depobj_codegen.cpp
@@ -17,6 +17,15 @@
 typedef void *omp_depend_t;
 
 void foo() {}
+void tmainc(){
+   omp_depend_t obj;
+#pragma omp depobj(obj) depend(inout: omp_all_memory)
+{
+   volatile omp_depend_t temp = obj;
+    char* char_ptr = reinterpret_cast<char*>(temp);
+    char_ptr[0] = 1;
+}
+}
 
 template <class T>
 T tmain(T argc) {
@@ -36,11 +45,24 @@ int main(int argc, char **argv) {
 #pragma omp depobj(b) update(mutexinoutset)
 #pragma omp depobj(a) depend(iterator(char *p = argv[argc]:argv[0]:-1), out: p[0])
   (void)tmain(a), tmain(b);
-   omp_depend_t obj;
-#pragma omp depobj(obj) depend(inout: omp_all_memory)
+ tmainc();
   return 0;
 }
-
+// CHECK-LABEL: tmainc
+// CHECK: [[D_ADDR:%obj]] = alloca ptr,
+// CHECK: [[GTID:%.+]] = call i32 @__kmpc_global_thread_num(
+// CHECK: [[DEP_ADDR_ADDR2:%.+]] = call ptr @__kmpc_alloc(i32 [[GTID]], i64 48, ptr null)
+// CHECK: [[SZ_DEOOBJ:%.+]] = getelementptr inbounds nuw %struct.kmp_depend_info, ptr [[DEP_ADDR_ADDR2]], i{{.+}} 0, i{{.+}} 0
+// CHECK: store i64 1, ptr [[SZ_DEOOBJ]], align 8
+// CHECK: [[DEPOBJ_BASE_ADDR:%.+]] = getelementptr %struct.kmp_depend_info, ptr [[DEP_ADDR_ADDR2]], i{{.+}} 1
+// CHECK: [[ADDR_ONE:%.+]] = getelementptr inbounds nuw %struct.kmp_depend_info, ptr [[DEPOBJ_BASE_ADDR]], i{{.+}} 0, i{{.+}} 0
+// CHECK: store i64 0, ptr [[ADDR_ONE]], align 8
+// CHECK: [[SZ_ADDR:%.+]] = getelementptr inbounds nuw %struct.kmp_depend_info, ptr [[DEPOBJ_BASE_ADDR]], i{{.+}} 0, i{{.+}} 1
+// CHECK: store i64 0, ptr [[SZ_ADDR]], align 8
+// CHECK: [[SZ_ADDR_NEW:%.+]] = getelementptr inbounds nuw %struct.kmp_depend_info, ptr [[DEPOBJ_BASE_ADDR]], i{{.+}} 0, i{{.+}} 2
+// CHECK: store {{i[0-9]+}} {{-?[0-9]+}}, ptr [[SZ_ADDR_NEW]], align 8
+// CHECK: [[DEP_NEW:%.+]] = getelementptr   %struct.kmp_depend_info, ptr [[DEP_ADDR_ADDR2]], i{{.+}} 1
+// CHECK: store ptr [[DEP_NEW]], ptr [[D_ADDR]], align 8
 // CHECK-LABEL: @main
 // CHECK: [[B_ADDR:%b]] = alloca ptr,
 // CHECK: [[GTID:%.+]] = call i32 @__kmpc_global_thread_num(
@@ -71,6 +93,8 @@ int main(int argc, char **argv) {
 // CHECK: [[NUMDEPS_ADDR:%.+]] = getelementptr inbounds nuw %struct.kmp_depend_info, ptr [[NUMDEPS_BASE]], i{{.+}} 0, i{{.+}} 0
 // CHECK: [[NUMDEPS:%.+]] = load i64, ptr [[NUMDEPS_ADDR]], align 8
 // CHECK: [[END:%.+]] = getelementptr %struct.kmp_depend_info, ptr [[B_BASE]], i64 [[NUMDEPS]]
+
+
 // CHECK: br label %[[BODY:.+]]
 // CHECK: [[BODY]]:
 // CHECK: [[EL:%.+]] = phi ptr [ [[B_BASE]], %{{.+}} ], [ [[EL_NEXT:%.+]], %[[BODY]] ]
@@ -230,6 +254,8 @@ int main(int argc, char **argv) {
 // CHECK: [[EL_NEXT]] = getelementptr %struct.kmp_depend_info, ptr [[EL]], i{{.+}} 1
 // CHECK: [[IS_DONE:%.+]] = icmp eq ptr [[EL_NEXT]], [[END]]
 // CHECK: br i1 [[IS_DONE]], label %[[DONE:.+]], label %[[BODY]]
+
 // CHECK: [[DONE]]:
 
+
 #endif

>From ae42197017501d8fffad3bde6a6bb94e90f0803c Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe31.hpc.amslabs.hpecorp.net>
Date: Thu, 7 Nov 2024 09:45:49 -0600
Subject: [PATCH 4/4] code formatting

---
 clang/test/OpenMP/depobj_codegen.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/test/OpenMP/depobj_codegen.cpp b/clang/test/OpenMP/depobj_codegen.cpp
index 8c7dce8d734060..b3bcd76e237dc8 100644
--- a/clang/test/OpenMP/depobj_codegen.cpp
+++ b/clang/test/OpenMP/depobj_codegen.cpp
@@ -22,8 +22,8 @@ void tmainc(){
 #pragma omp depobj(obj) depend(inout: omp_all_memory)
 {
    volatile omp_depend_t temp = obj;
-    char* char_ptr = reinterpret_cast<char*>(temp);
-    char_ptr[0] = 1;
+   char* char_ptr = reinterpret_cast<char*>(temp);
+   char_ptr[0] = 1;
 }
 }
 
@@ -44,7 +44,7 @@ int main(int argc, char **argv) {
 #pragma omp depobj(b) destroy
 #pragma omp depobj(b) update(mutexinoutset)
 #pragma omp depobj(a) depend(iterator(char *p = argv[argc]:argv[0]:-1), out: p[0])
-  (void)tmain(a), tmain(b);
+ (void)tmain(a), tmain(b);
  tmainc();
   return 0;
 }



More information about the cfe-commits mailing list