[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-21 Thread Joseph Huber via cfe-commits

jhuber6 wrote:

> This fails for me on the host and the AMD GPU: GPU:
> 
> ```
> # | :217:1: note: possible intended match here
> # | dat.datum[dat.arr[0][0]] = 5
> ```
> 
> X86:
> 
> ```
> # | :134:1: note: possible intended match here
> # | dat.datum[dat.arr[0][0]] = 5461
> ```
> 
> The location that is printed (datum[1]) is uninitialized.

I see the same but forgot to say anything.

https://github.com/llvm/llvm-project/pull/75642
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-21 Thread Johannes Doerfert via cfe-commits

jdoerfert wrote:

This fails for me on the host and the AMD GPU:
GPU:
# | :217:1: note: possible intended match here
# | dat.datum[dat.arr[0][0]] = 5
X86:
# | :134:1: note: possible intended match here
# | dat.datum[dat.arr[0][0]] = 5461

The location that is printed (datum[1]) is uninitialized.


https://github.com/llvm/llvm-project/pull/75642
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-19 Thread Gheorghe-Teodor Bercea via cfe-commits

doru1004 wrote:

> The newly added test `offloading/struct_mapping_with_pointers.cpp` fails on 
> NVIDIA GPUs as well.
> 
> ```
>  TEST 'libomptarget :: nvptx64-nvidia-cuda :: 
> offloading/struct_mapping_with_pointers.cpp' FAILED 
> Exit Code: 1
> 
> Command Output (stdout):
> --
> # RUN: at line 2
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/clang++ -fopenmp 
> -pthread   -I 
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test 
> -I /gpfs/jlse
> -fs0/users/ac.shilei.tian/build/openmp/release/runtime/src -L 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget -L 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/ll
> vm/release/./lib -L 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src  
> -Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget
>  -Wl,-rpa
> th,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src 
> -Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/./lib 
> -Wl,-rpath,/soft/compilers/cuda/cud
> a-11.8.0/targets/x86_64-linux/lib 
> --libomptarget-nvptx-bc-path=/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/DeviceRTL
>  -fopenmp-targets=nvptx64-nvidia-cuda
>  
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp
>  -o /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/releas
> e/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_pointers.cpp.tmp
>  
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/libomptarget.d
> evicertl.a && env LIBOMPTARGET_DEBUG=1 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_pointer
> s.cpp.tmp 2>&1 | 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/FileCheck 
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct
> _mapping_with_pointers.cpp
> # executed command: 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/clang++ -fopenmp 
> -pthread -I 
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/
> test -I /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src 
> -L /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget -L 
> /gpfs/jlse-fs0/users/ac.sh
> ilei.tian/build/llvm/release/./lib -L 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src 
> -Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libo
> mptarget 
> -Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src
>  -Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/./lib 
> -Wl,-rpath,/soft/c
> ompilers/cuda/cuda-11.8.0/targets/x86_64-linux/lib 
> --libomptarget-nvptx-bc-path=/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/DeviceRTL
>  -fopenmp-targets=nv
> ptx64-nvidia-cuda 
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp
>  -o /gpfs/jlse-fs0/users/ac.shilei.tian/bu
> ild/openmp/release/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_pointers.cpp.tmp
>  /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarg
> et/libomptarget.devicertl.a
> # executed command: env LIBOMPTARGET_DEBUG=1 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_p
> ointers.cpp.tmp
> # executed command: 
> /gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/FileCheck 
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/str
> uct_mapping_with_pointers.cpp
> # .---command stderr
> # | 
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp:106:12:
>  error: CHECK: expected string not found in inpu
> t
> # |  // CHECK: dat.datum[dat.arr[0][0]] = 0
> # |^
> # | :124:24: note: scanning from here
> # | dat.val_more_datum = 18
> # |^
> # | :125:1: note: possible intended match here
> # | dat.datum[dat.arr[0][0]] = 32542
> # | ^
> # |
> # | Input file: 
> # | Check file: 
> /home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp
> # |
> # | -dump-input=help explains the following input dump.
> # |
> # | Input was:
> # | <<
> # |  .
> # |  .
> # |  .
> # |119: omptarget --> Done unregistering library!
> # |120: omptarget --> Deinit offload library!
> # |121: TARGET CUDA RTL --> Missing 2 resources to be returned
> # |122: dat.xi = 4
> # |123: dat.val_datum = 8
> # |124: dat.val_more_datum = 18
> # | check:106'0  

[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-18 Thread Shilei Tian via cfe-commits

shiltian wrote:

The newly added test `offloading/struct_mapping_with_pointers.cpp` fails on 
NVIDIA GPUs as well.
```
 TEST 'libomptarget :: nvptx64-nvidia-cuda :: 
offloading/struct_mapping_with_pointers.cpp' FAILED 
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 2
/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/clang++ -fopenmp 
-pthread   -I 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test -I 
/gpfs/jlse
-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src -L 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget -L 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/ll
vm/release/./lib -L 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src  
-Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget
 -Wl,-rpa
th,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src 
-Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/./lib 
-Wl,-rpath,/soft/compilers/cuda/cud
a-11.8.0/targets/x86_64-linux/lib 
--libomptarget-nvptx-bc-path=/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/DeviceRTL
 -fopenmp-targets=nvptx64-nvidia-cuda
 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp
 -o /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/releas
e/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_pointers.cpp.tmp
 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/libomptarget.d
evicertl.a && env LIBOMPTARGET_DEBUG=1 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_pointer
s.cpp.tmp 2>&1 | 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/FileCheck 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct
_mapping_with_pointers.cpp
# executed command: 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/clang++ -fopenmp 
-pthread -I 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/
test -I /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src -L 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget -L 
/gpfs/jlse-fs0/users/ac.sh
ilei.tian/build/llvm/release/./lib -L 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src 
-Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libo
mptarget 
-Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/runtime/src 
-Wl,-rpath,/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/./lib 
-Wl,-rpath,/soft/c
ompilers/cuda/cuda-11.8.0/targets/x86_64-linux/lib 
--libomptarget-nvptx-bc-path=/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/DeviceRTL
 -fopenmp-targets=nv
ptx64-nvidia-cuda 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp
 -o /gpfs/jlse-fs0/users/ac.shilei.tian/bu
ild/openmp/release/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_pointers.cpp.tmp
 /gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarg
et/libomptarget.devicertl.a
# executed command: env LIBOMPTARGET_DEBUG=1 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/openmp/release/libomptarget/test/nvptx64-nvidia-cuda/offloading/Output/struct_mapping_with_p
ointers.cpp.tmp
# executed command: 
/gpfs/jlse-fs0/users/ac.shilei.tian/build/llvm/release/bin/FileCheck 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/str
uct_mapping_with_pointers.cpp
# .---command stderr
# | 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp:106:12:
 error: CHECK: expected string not found in inpu
t
# |  // CHECK: dat.datum[dat.arr[0][0]] = 0
# |^
# | :124:24: note: scanning from here
# | dat.val_more_datum = 18
# |^
# | :125:1: note: possible intended match here
# | dat.datum[dat.arr[0][0]] = 32542
# | ^
# |
# | Input file: 
# | Check file: 
/home/ac.shilei.tian/Documents/vscode/llvm-project/openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp
# |
# | -dump-input=help explains the following input dump.
# |
# | Input was:
# | <<
# |  .
# |  .
# |  .
# |119: omptarget --> Done unregistering library!
# |120: omptarget --> Deinit offload library!
# |121: TARGET CUDA RTL --> Missing 2 resources to be returned
# |122: dat.xi = 4
# |123: dat.val_datum = 8
# |124: dat.val_more_datum = 18
# | check:106'0X error: no match found
# |125: dat.datum[dat.arr[0][0]] = 32542
# | check:106'0 ~
# | check:106'1 ?   

[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-15 Thread Gheorghe-Teodor Bercea via cfe-commits

doru1004 wrote:

@alexey-bataev I have reworked the previous patch with your advice in mind. The 
emitCombinedEntry function was not changed since eliminating the combined entry 
has many ramifications which would need to be handled in a separate patch. For 
now this fixes the immediate error in a way that allows us to later get rid of 
the combined entry later on if we want to.

https://github.com/llvm/llvm-project/pull/75642
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-15 Thread Gheorghe-Teodor Bercea via cfe-commits

https://github.com/doru1004 updated 
https://github.com/llvm/llvm-project/pull/75642

>From 32454489d4e77f22ab935827dffe0febbb7b0626 Mon Sep 17 00:00:00 2001
From: Doru Bercea 
Date: Fri, 15 Dec 2023 10:22:38 -0500
Subject: [PATCH] Fix mapping of structs to device.

---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp | 148 +++
 clang/test/OpenMP/map_struct_ordering.cpp | 172 ++
 .../struct_mapping_with_pointers.cpp  | 114 
 3 files changed, 401 insertions(+), 33 deletions(-)
 create mode 100644 clang/test/OpenMP/map_struct_ordering.cpp
 create mode 100644 
openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 7f7e6f53066644..ea6645a39e8321 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6811,8 +6811,10 @@ class MappableExprsHandler {
   OpenMPMapClauseKind MapType, ArrayRef 
MapModifiers,
   ArrayRef MotionModifiers,
   OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
-  MapCombinedInfoTy &CombinedInfo, StructRangeInfoTy &PartialStruct,
-  bool IsFirstComponentList, bool IsImplicit,
+  MapCombinedInfoTy &CombinedInfo,
+  MapCombinedInfoTy &StructBaseCombinedInfo,
+  StructRangeInfoTy &PartialStruct, bool IsFirstComponentList,
+  bool IsImplicit, bool GenerateAllInfoForClauses,
   const ValueDecl *Mapper = nullptr, bool ForDeviceAddr = false,
   const ValueDecl *BaseDecl = nullptr, const Expr *MapExpr = nullptr,
   ArrayRef
@@ -7098,6 +7100,25 @@ class MappableExprsHandler {
 bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous;
 bool IsPrevMemberReference = false;
 
+// We need to check if we will be encountering any MEs. If we do not
+// encounter any ME expression it means we will be mapping the whole 
struct.
+// In that case we need to skip adding an entry for the struct to the
+// CombinedInfo list and instead add an entry to the StructBaseCombinedInfo
+// list only when generating all info for clauses.
+bool IsMappingWholeStruct = true;
+if (!GenerateAllInfoForClauses) {
+  IsMappingWholeStruct = false;
+} else {
+  for (auto TempI = I; TempI != CE; ++TempI) {
+const MemberExpr *PossibleME =
+dyn_cast(TempI->getAssociatedExpression());
+if (PossibleME) {
+  IsMappingWholeStruct = false;
+  break;
+}
+  }
+}
+
 for (; I != CE; ++I) {
   // If the current component is member of a struct (parent struct) mark 
it.
   if (!EncounteredME) {
@@ -7317,21 +7338,41 @@ class MappableExprsHandler {
   break;
 }
 llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
+// Skip adding an entry in the CurInfo of this combined entry if the
+// whole struct is currently being mapped. The struct needs to be added
+// in the first position before any data internal to the struct is 
being
+// mapped.
 if (!IsMemberPointerOrAddr ||
 (Next == CE && MapType != OMPC_MAP_unknown)) {
-  CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
-  CombinedInfo.BasePointers.push_back(BP.getPointer());
-  CombinedInfo.DevicePtrDecls.push_back(nullptr);
-  CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
-  CombinedInfo.Pointers.push_back(LB.getPointer());
-  CombinedInfo.Sizes.push_back(
-  CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
-  CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
-: 1);
+  if (!IsMappingWholeStruct) {
+CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+CombinedInfo.BasePointers.push_back(BP.getPointer());
+CombinedInfo.DevicePtrDecls.push_back(nullptr);
+CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+CombinedInfo.Pointers.push_back(LB.getPointer());
+CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
+  : 1);
+  } else {
+StructBaseCombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+StructBaseCombinedInfo.BasePointers.push_back(BP.getPointer());
+StructBaseCombinedInfo.DevicePtrDecls.push_back(nullptr);
+
StructBaseCombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+StructBaseCombinedInfo.Pointers.push_back(LB.getPointer());
+StructBaseCombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+ 

[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-15 Thread Gheorghe-Teodor Bercea via cfe-commits

https://github.com/doru1004 updated 
https://github.com/llvm/llvm-project/pull/75642

>From e0e1f5e7bb2f95f2568b5dd647b883f4740bcafd Mon Sep 17 00:00:00 2001
From: Doru Bercea 
Date: Fri, 15 Dec 2023 10:22:38 -0500
Subject: [PATCH] Fix mapping of structs to device.

---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp | 146 +++
 clang/test/OpenMP/map_struct_ordering.cpp | 172 ++
 .../struct_mapping_with_pointers.cpp  | 114 
 3 files changed, 399 insertions(+), 33 deletions(-)
 create mode 100644 clang/test/OpenMP/map_struct_ordering.cpp
 create mode 100644 
openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 7f7e6f53066644..350e7108b8d5a7 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6811,8 +6811,10 @@ class MappableExprsHandler {
   OpenMPMapClauseKind MapType, ArrayRef 
MapModifiers,
   ArrayRef MotionModifiers,
   OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
-  MapCombinedInfoTy &CombinedInfo, StructRangeInfoTy &PartialStruct,
-  bool IsFirstComponentList, bool IsImplicit,
+  MapCombinedInfoTy &CombinedInfo,
+  MapCombinedInfoTy &StructBaseCombinedInfo,
+  StructRangeInfoTy &PartialStruct, bool IsFirstComponentList,
+  bool IsImplicit, bool GenerateAllInfoForClauses,
   const ValueDecl *Mapper = nullptr, bool ForDeviceAddr = false,
   const ValueDecl *BaseDecl = nullptr, const Expr *MapExpr = nullptr,
   ArrayRef
@@ -7098,6 +7100,25 @@ class MappableExprsHandler {
 bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous;
 bool IsPrevMemberReference = false;
 
+// We need to check if we will be encountering any MEs. If we do not
+// encounter any ME expression it means we will be mapping the whole 
struct.
+// In that case we need to skip adding an entry for the struct to the
+// CombinedInfo list and instead add an entry to the StructBaseCombinedInfo
+// list only when generating all info for clauses.
+bool IsMappingWholeStruct = true;
+if (!GenerateAllInfoForClauses) {
+  IsMappingWholeStruct = false;
+} else {
+  for (auto TempI = I; TempI != CE; ++TempI) {
+const MemberExpr *PossibleME =
+dyn_cast(TempI->getAssociatedExpression());
+if (PossibleME) {
+  IsMappingWholeStruct = false;
+  break;
+}
+  }
+}
+
 for (; I != CE; ++I) {
   // If the current component is member of a struct (parent struct) mark 
it.
   if (!EncounteredME) {
@@ -7317,21 +7338,41 @@ class MappableExprsHandler {
   break;
 }
 llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
+// Skip adding an entry in the CurInfo of this combined entry if the
+// whole struct is currently being mapped. The struct needs to be added
+// in the first position before any data internal to the struct is 
being
+// mapped.
 if (!IsMemberPointerOrAddr ||
 (Next == CE && MapType != OMPC_MAP_unknown)) {
-  CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
-  CombinedInfo.BasePointers.push_back(BP.getPointer());
-  CombinedInfo.DevicePtrDecls.push_back(nullptr);
-  CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
-  CombinedInfo.Pointers.push_back(LB.getPointer());
-  CombinedInfo.Sizes.push_back(
-  CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
-  CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
-: 1);
+  if (!IsMappingWholeStruct) {
+CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+CombinedInfo.BasePointers.push_back(BP.getPointer());
+CombinedInfo.DevicePtrDecls.push_back(nullptr);
+CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+CombinedInfo.Pointers.push_back(LB.getPointer());
+CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
+  : 1);
+  } else {
+StructBaseCombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+StructBaseCombinedInfo.BasePointers.push_back(BP.getPointer());
+StructBaseCombinedInfo.DevicePtrDecls.push_back(nullptr);
+
StructBaseCombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+StructBaseCombinedInfo.Pointers.push_back(LB.getPointer());
+StructBaseCombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+ 

[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-15 Thread Gheorghe-Teodor Bercea via cfe-commits

https://github.com/doru1004 updated 
https://github.com/llvm/llvm-project/pull/75642

>From ae6cf04a149f00f52c1da8e7b9c1ca3af5393f99 Mon Sep 17 00:00:00 2001
From: Doru Bercea 
Date: Fri, 15 Dec 2023 10:22:38 -0500
Subject: [PATCH] Fix mapping of structs to device.

---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp | 147 +++
 clang/test/OpenMP/map_struct_ordering.cpp | 172 ++
 .../struct_mapping_with_pointers.cpp  | 114 
 3 files changed, 400 insertions(+), 33 deletions(-)
 create mode 100644 clang/test/OpenMP/map_struct_ordering.cpp
 create mode 100644 
openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 7f7e6f53066644..02f5d8fca7090c 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6811,8 +6811,10 @@ class MappableExprsHandler {
   OpenMPMapClauseKind MapType, ArrayRef 
MapModifiers,
   ArrayRef MotionModifiers,
   OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
-  MapCombinedInfoTy &CombinedInfo, StructRangeInfoTy &PartialStruct,
-  bool IsFirstComponentList, bool IsImplicit,
+  MapCombinedInfoTy &CombinedInfo,
+  MapCombinedInfoTy &StructBaseCombinedInfo,
+  StructRangeInfoTy &PartialStruct, bool IsFirstComponentList,
+  bool IsImplicit, bool GenerateAllInfoForClauses,
   const ValueDecl *Mapper = nullptr, bool ForDeviceAddr = false,
   const ValueDecl *BaseDecl = nullptr, const Expr *MapExpr = nullptr,
   ArrayRef
@@ -7098,6 +7100,25 @@ class MappableExprsHandler {
 bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous;
 bool IsPrevMemberReference = false;
 
+// We need to check if we will be encountering any MEs. If we do not
+// encounter any ME expression it means we will be mapping the whole 
struct.
+// In that case we need to skip adding an entry for the struct to the
+// CombinedInfo list and instead add an entry to the StructBaseCombinedInfo
+// list only when generating all info for clauses.
+bool IsMappingWholeStruct = true;
+if (!GenerateAllInfoForClauses) {
+  IsMappingWholeStruct = false;
+} else {
+  for (auto TempI = I; TempI != CE; ++TempI) {
+const MemberExpr *PossibleME =
+dyn_cast(TempI->getAssociatedExpression());
+if (PossibleME) {
+  IsMappingWholeStruct = false;
+  break;
+}
+  }
+}
+
 for (; I != CE; ++I) {
   // If the current component is member of a struct (parent struct) mark 
it.
   if (!EncounteredME) {
@@ -7317,21 +7338,41 @@ class MappableExprsHandler {
   break;
 }
 llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
+// Skip adding an entry in the CurInfo of this combined entry if the
+// whole struct is currently being mapped. The struct needs to be added
+// in the first position before any data internal to the struct is 
being
+// mapped.
 if (!IsMemberPointerOrAddr ||
 (Next == CE && MapType != OMPC_MAP_unknown)) {
-  CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
-  CombinedInfo.BasePointers.push_back(BP.getPointer());
-  CombinedInfo.DevicePtrDecls.push_back(nullptr);
-  CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
-  CombinedInfo.Pointers.push_back(LB.getPointer());
-  CombinedInfo.Sizes.push_back(
-  CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
-  CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
-: 1);
+  if (!IsMappingWholeStruct) {
+CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+CombinedInfo.BasePointers.push_back(BP.getPointer());
+CombinedInfo.DevicePtrDecls.push_back(nullptr);
+CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+CombinedInfo.Pointers.push_back(LB.getPointer());
+CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
+  : 1);
+  } else {
+StructBaseCombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+StructBaseCombinedInfo.BasePointers.push_back(BP.getPointer());
+StructBaseCombinedInfo.DevicePtrDecls.push_back(nullptr);
+
StructBaseCombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+StructBaseCombinedInfo.Pointers.push_back(LB.getPointer());
+StructBaseCombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+ 

[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-15 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Gheorghe-Teodor Bercea (doru1004)


Changes

Fix mapping of structs to device.

The following example fails:

```
#include 
#include 

struct Descriptor {
  int *datum;
  long int x;
  int xi;
  long int arr[1][30];
};

int main() {
  Descriptor dat = Descriptor();
  dat.datum = (int *)malloc(sizeof(int)*10);
  dat.xi = 3;
  dat.arr[0][0] = 1;

  #pragma omp target enter data map(to: dat.datum[:10]) map(to: dat)

  #pragma omp target
  {
dat.xi = 4;
dat.datum[dat.arr[0][0]] = dat.xi;
  }

  #pragma omp target exit data map(from: dat)

 return 0;
}
```

This is a rework of the previous attempt: 
https://github.com/llvm/llvm-project/pull/72410

---

Patch is 30.55 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/75642.diff


3 Files Affected:

- (modified) clang/lib/CodeGen/CGOpenMPRuntime.cpp (+114-33) 
- (added) clang/test/OpenMP/map_struct_ordering.cpp (+172) 
- (added) openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp 
(+115) 


``diff
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 7f7e6f53066644..02f5d8fca7090c 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6811,8 +6811,10 @@ class MappableExprsHandler {
   OpenMPMapClauseKind MapType, ArrayRef 
MapModifiers,
   ArrayRef MotionModifiers,
   OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
-  MapCombinedInfoTy &CombinedInfo, StructRangeInfoTy &PartialStruct,
-  bool IsFirstComponentList, bool IsImplicit,
+  MapCombinedInfoTy &CombinedInfo,
+  MapCombinedInfoTy &StructBaseCombinedInfo,
+  StructRangeInfoTy &PartialStruct, bool IsFirstComponentList,
+  bool IsImplicit, bool GenerateAllInfoForClauses,
   const ValueDecl *Mapper = nullptr, bool ForDeviceAddr = false,
   const ValueDecl *BaseDecl = nullptr, const Expr *MapExpr = nullptr,
   ArrayRef
@@ -7098,6 +7100,25 @@ class MappableExprsHandler {
 bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous;
 bool IsPrevMemberReference = false;
 
+// We need to check if we will be encountering any MEs. If we do not
+// encounter any ME expression it means we will be mapping the whole 
struct.
+// In that case we need to skip adding an entry for the struct to the
+// CombinedInfo list and instead add an entry to the StructBaseCombinedInfo
+// list only when generating all info for clauses.
+bool IsMappingWholeStruct = true;
+if (!GenerateAllInfoForClauses) {
+  IsMappingWholeStruct = false;
+} else {
+  for (auto TempI = I; TempI != CE; ++TempI) {
+const MemberExpr *PossibleME =
+dyn_cast(TempI->getAssociatedExpression());
+if (PossibleME) {
+  IsMappingWholeStruct = false;
+  break;
+}
+  }
+}
+
 for (; I != CE; ++I) {
   // If the current component is member of a struct (parent struct) mark 
it.
   if (!EncounteredME) {
@@ -7317,21 +7338,41 @@ class MappableExprsHandler {
   break;
 }
 llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
+// Skip adding an entry in the CurInfo of this combined entry if the
+// whole struct is currently being mapped. The struct needs to be added
+// in the first position before any data internal to the struct is 
being
+// mapped.
 if (!IsMemberPointerOrAddr ||
 (Next == CE && MapType != OMPC_MAP_unknown)) {
-  CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
-  CombinedInfo.BasePointers.push_back(BP.getPointer());
-  CombinedInfo.DevicePtrDecls.push_back(nullptr);
-  CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
-  CombinedInfo.Pointers.push_back(LB.getPointer());
-  CombinedInfo.Sizes.push_back(
-  CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
-  CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
-: 1);
+  if (!IsMappingWholeStruct) {
+CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+CombinedInfo.BasePointers.push_back(BP.getPointer());
+CombinedInfo.DevicePtrDecls.push_back(nullptr);
+CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+CombinedInfo.Pointers.push_back(LB.getPointer());
+CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
+  : 1);
+  } else {
+StructBaseCombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+StructBas

[clang] [openmp] [Clang][OpenMP] Fix mapping of structs to device (PR #75642)

2023-12-15 Thread Gheorghe-Teodor Bercea via cfe-commits

https://github.com/doru1004 created 
https://github.com/llvm/llvm-project/pull/75642

Fix mapping of structs to device.

The following example fails:

```
#include 
#include 

struct Descriptor {
  int *datum;
  long int x;
  int xi;
  long int arr[1][30];
};

int main() {
  Descriptor dat = Descriptor();
  dat.datum = (int *)malloc(sizeof(int)*10);
  dat.xi = 3;
  dat.arr[0][0] = 1;

  #pragma omp target enter data map(to: dat.datum[:10]) map(to: dat)

  #pragma omp target
  {
dat.xi = 4;
dat.datum[dat.arr[0][0]] = dat.xi;
  }

  #pragma omp target exit data map(from: dat)

 return 0;
}
```

This is a rework of the previous attempt: 
https://github.com/llvm/llvm-project/pull/72410

>From 2dc40b67e55985de4e9e89758d6c65eb73faac02 Mon Sep 17 00:00:00 2001
From: Doru Bercea 
Date: Fri, 15 Dec 2023 10:22:38 -0500
Subject: [PATCH] Fix mapping of structs to device.

---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp | 147 +++
 clang/test/OpenMP/map_struct_ordering.cpp | 172 ++
 .../struct_mapping_with_pointers.cpp  | 115 
 3 files changed, 401 insertions(+), 33 deletions(-)
 create mode 100644 clang/test/OpenMP/map_struct_ordering.cpp
 create mode 100644 
openmp/libomptarget/test/offloading/struct_mapping_with_pointers.cpp

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 7f7e6f53066644..02f5d8fca7090c 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6811,8 +6811,10 @@ class MappableExprsHandler {
   OpenMPMapClauseKind MapType, ArrayRef 
MapModifiers,
   ArrayRef MotionModifiers,
   OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
-  MapCombinedInfoTy &CombinedInfo, StructRangeInfoTy &PartialStruct,
-  bool IsFirstComponentList, bool IsImplicit,
+  MapCombinedInfoTy &CombinedInfo,
+  MapCombinedInfoTy &StructBaseCombinedInfo,
+  StructRangeInfoTy &PartialStruct, bool IsFirstComponentList,
+  bool IsImplicit, bool GenerateAllInfoForClauses,
   const ValueDecl *Mapper = nullptr, bool ForDeviceAddr = false,
   const ValueDecl *BaseDecl = nullptr, const Expr *MapExpr = nullptr,
   ArrayRef
@@ -7098,6 +7100,25 @@ class MappableExprsHandler {
 bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous;
 bool IsPrevMemberReference = false;
 
+// We need to check if we will be encountering any MEs. If we do not
+// encounter any ME expression it means we will be mapping the whole 
struct.
+// In that case we need to skip adding an entry for the struct to the
+// CombinedInfo list and instead add an entry to the StructBaseCombinedInfo
+// list only when generating all info for clauses.
+bool IsMappingWholeStruct = true;
+if (!GenerateAllInfoForClauses) {
+  IsMappingWholeStruct = false;
+} else {
+  for (auto TempI = I; TempI != CE; ++TempI) {
+const MemberExpr *PossibleME =
+dyn_cast(TempI->getAssociatedExpression());
+if (PossibleME) {
+  IsMappingWholeStruct = false;
+  break;
+}
+  }
+}
+
 for (; I != CE; ++I) {
   // If the current component is member of a struct (parent struct) mark 
it.
   if (!EncounteredME) {
@@ -7317,21 +7338,41 @@ class MappableExprsHandler {
   break;
 }
 llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
+// Skip adding an entry in the CurInfo of this combined entry if the
+// whole struct is currently being mapped. The struct needs to be added
+// in the first position before any data internal to the struct is 
being
+// mapped.
 if (!IsMemberPointerOrAddr ||
 (Next == CE && MapType != OMPC_MAP_unknown)) {
-  CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
-  CombinedInfo.BasePointers.push_back(BP.getPointer());
-  CombinedInfo.DevicePtrDecls.push_back(nullptr);
-  CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
-  CombinedInfo.Pointers.push_back(LB.getPointer());
-  CombinedInfo.Sizes.push_back(
-  CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
-  CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
-: 1);
+  if (!IsMappingWholeStruct) {
+CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
+CombinedInfo.BasePointers.push_back(BP.getPointer());
+CombinedInfo.DevicePtrDecls.push_back(nullptr);
+CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
+CombinedInfo.Pointers.push_back(LB.getPointer());
+CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
+Size, CGF.Int64Ty, /*isSigned=*/true));
+CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize