[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-10 Thread via llvm-branch-commits

agozillon wrote:

I made a bit of a mistake not using SPR to land this PR series, so i've ended 
up having to directly commit the PR, the associated commit for this PR can be 
found here: 
https://github.com/llvm/llvm-project/commit/462435f089b7ca67ed1eed60f369e16b71e76b74
 

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-10 Thread via llvm-branch-commits

https://github.com/agozillon closed 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-10 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-10 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-07 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak approved this pull request.

Thank you Andrew for working on my suggestions. LGTM, I just have some minimal 
nits, but there's no need for another review from me before merging this PR.

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-07 Thread Sergio Afonso via llvm-branch-commits


@@ -2122,6 +2124,66 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end() &&
+ "MapInfoOp for member not found in MapData, cannot return index");
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  mlir::DenseIntElementsAttr indexAttr = mapInfo.getMembersIndexAttr();
+
+  // Only 1 member has been mapped, we can return it.
+  if (indexAttr.size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  llvm::ArrayRef shape = indexAttr.getShapedType().getShape();
+  std::vector indices(shape[0]);

skatrak wrote:

Nit: Wouldn't `SmallVector` be preferred in this case?
```suggestion
  llvm::SmallVector indices(shape[0]);
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-07 Thread Sergio Afonso via llvm-branch-commits


@@ -0,0 +1,63 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// This test checks the offload sizes, map types and base pointers and pointers
+// provided to the OpenMP kernel argument structure are correct when lowering 
+// to LLVM-IR from MLIR when performing explicit member mapping of a recrod 
type
+// (C++/C class/structure, Fortran derived type) where only members of the 
record 
+// type are mapped.
+
+module attributes {omp.is_target_device = false} {
+llvm.func @_QQmain() {
+%0 = llvm.mlir.constant(10 : index) : i64
+%1 = llvm.mlir.constant(4 : index) : i64
+%2 = llvm.mlir.constant(1 : index) : i64
+%3 = llvm.mlir.constant(1 : i64) : i64
+%4 = llvm.alloca %3 x !llvm.struct<(f32, array<10 x i32>, i32)> : (i64) -> 
!llvm.ptr
+%5 = llvm.mlir.constant(2 : i32) : i32
+%6 = llvm.getelementptr %4[0, 2] : (!llvm.ptr) -> !llvm.ptr, 
!llvm.struct<(f32, array<10 x i32>, i32)>
+%7 = omp.map.info var_ptr(%6 : !llvm.ptr, i32) map_clauses(tofrom) 
capture(ByRef) -> !llvm.ptr
+%8 = llvm.mlir.constant(1 : i32) : i32
+%9 = llvm.getelementptr %4[0, 1] : (!llvm.ptr) -> !llvm.ptr, 
!llvm.struct<(f32, array<10 x i32>, i32)>
+%10 = omp.map.bounds lower_bound(%2 : i64) upper_bound(%1 : i64) extent(%0 
: i64) stride(%2 : i64) start_idx(%2 : i64)
+%11 = omp.map.info var_ptr(%9 : !llvm.ptr, !llvm.array<10 x i32>) 
map_clauses(tofrom) capture(ByRef) bounds(%10) -> !llvm.ptr
+%12 = omp.map.info var_ptr(%4 : !llvm.ptr, !llvm.struct<(f32, array<10 x 
i32>, i32)>) map_clauses(tofrom) capture(ByRef) members(%7, %11 : [2], [1] : 
!llvm.ptr, !llvm.ptr) -> !llvm.ptr {partial_map = true}
+omp.target map_entries(%7 -> %arg0, %11 -> %arg1, %12 -> %arg2 : 
!llvm.ptr, !llvm.ptr, !llvm.ptr) {
+^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: !llvm.ptr):
+  omp.terminator
+}
+llvm.return
+  }
+}
+
+// CHECK: @.offload_sizes = private unnamed_addr constant [3 x i64] [i64 0, 
i64 4, i64 16]
+// CHECK: @.offload_maptypes = private unnamed_addr constant [3 x i64] [i64 
32, i64 281474976710659, i64 281474976710659]
+
+// CHECK: define void @_QQmain()
+// CHECK: %[[ALLOCA:.*]] = alloca { float, [10 x i32], i32 }, i64 1, align 8
+// CHECK: %[[MEMBER_ACCESS_1:.*]] = getelementptr { float, [10 x i32], i32 }, 
ptr %[[ALLOCA]], i32 0, i32 2
+// CHECK: %[[MEMBER_ACCESS_2:.*]] = getelementptr { float, [10 x i32], i32 }, 
ptr %[[ALLOCA]], i32 0, i32 1
+
+// CHECK: %[[LAST_MEMBER:.*]] = getelementptr inbounds [10 x i32], ptr 
%[[MEMBER_ACCESS_2]], i64 0, i64 1
+// CHECK: %[[FIRST_MEMBER:.*]] = getelementptr i32, ptr %[[MEMBER_ACCESS_1]], 
i64 1
+// CHECK: %[[FIRST_MEMBER_OFF:.*]] = ptrtoint ptr %[[FIRST_MEMBER]] to i64
+// CHECK: %[[SECOND_MEMBER_OFF:.*]] = ptrtoint ptr %[[LAST_MEMBER]] to i64
+// CHECK: %[[MEMBER_DIFF:.*]] = sub i64 %[[FIRST_MEMBER_OFF]], 
%[[SECOND_MEMBER_OFF]]
+// CHECK: %[[OFFLOAD_SIZE:.*]] = sdiv exact i64 %[[MEMBER_DIFF]], ptrtoint 
(ptr getelementptr (i8, ptr null, i32 1) to i64)
+
+// CHECK: %[[BASE_PTR_ARR:.*]] = getelementptr inbounds [3 x ptr], ptr 
%.offload_baseptrs, i32 0, i32 0
+// CHECK: store ptr %[[ALLOCA]], ptr %[[BASE_PTR_ARR]], align 8
+// CHECK: %[[PTR_ARR:.*]] = getelementptr inbounds [3 x ptr], ptr 
%.offload_ptrs, i32 0, i32 0
+// CHECK: store ptr %[[LAST_MEMBER]], ptr %[[PTR_ARR]], align 8
+// CHECK: %[[SIZE_ARR:.*]] = getelementptr inbounds [3 x i64], ptr 
%.offload_sizes, i32 0, i32 0
+// CHECK: store i64 %[[OFFLOAD_SIZE]], ptr %[[SIZE_ARR]], align 8
+
+// CHECK: %[[BASE_PTR_ARR_2:.*]] = getelementptr inbounds [3 x ptr], ptr 
%.offload_baseptrs, i32 0, i32 1
+// CHECK: store ptr %[[ALLOCA]], ptr %[[BASE_PTR_ARR_2]], align 8
+// CHECK: %[[PTR_ARR_2:.*]] = getelementptr inbounds [3 x ptr], ptr 
%.offload_ptrs, i32 0, i32 1
+// CHECK: store ptr %[[MEMBER_ACCESS_1]], ptr %[[PTR_ARR_2]], align 8
+  
+// CHECK: %[[BASE_PTR_ARR_3:.*]] = getelementptr inbounds [3 x ptr], ptr 
%.offload_baseptrs, i32 0, i32 2
+// CHECK: store ptr %[[ALLOCA]], ptr %[[BASE_PTR_ARR_3]], align 8
+// CHECK: %[[PTR_ARR_3:.*]] = getelementptr inbounds [3 x ptr], ptr 
%.offload_ptrs, i32 0, i32 2
+// CHECK: store ptr %[[LAST_MEMBER]], ptr %[[PTR_ARR_3]], align 8
+ 

skatrak wrote:

Still there! 

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-07 Thread Sergio Afonso via llvm-branch-commits


@@ -2122,6 +2124,66 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end() &&
+ "MapInfoOp for member not found in MapData, cannot return index");
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  mlir::DenseIntElementsAttr indexAttr = mapInfo.getMembersIndexAttr();
+
+  // Only 1 member has been mapped, we can return it.
+  if (indexAttr.size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  llvm::ArrayRef shape = indexAttr.getShapedType().getShape();
+  std::vector indices(shape[0]);
+  std::iota(indices.begin(), indices.end(), 0);
+
+  llvm::sort(
+  indices.begin(), indices.end(), [&](const size_t a, const size_t b) {
+for (int i = 0;
+ i < shape[1];
+ ++i) {
+  int aIndex = indexAttr.getValues()[a * shape[1] + i];
+  int bIndex = indexAttr.getValues()[b * shape[1] + i];

skatrak wrote:

Nit: Extract `indexAttr.getValues()` out of the lambda.
```suggestion
  int aIndex = indexAttrValues[a * shape[1] + i];
  int bIndex = indexAttrValues[b * shape[1] + i];
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-07 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak edited 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-07 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak deleted 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-03 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-03 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-01 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-05-01 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-24 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-24 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-24 Thread via llvm-branch-commits


@@ -2283,16 +2386,12 @@ static void processMapMembersWithParent(
   for (auto mappedMembers : parentClause.getMembers()) {
 auto memberClause =
 mlir::dyn_cast(mappedMembers.getDefiningOp());

agozillon wrote:

should be fine to fix it in this PR as we're altering the file in any case! 
But, I can perhaps do a follow up PR to adjust these cases, I have a bad habit 
of using dyn_cast as opposed to cast, usually I check the result, but it seems 
I've done that infrequently in this PR at the least! 

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-24 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak edited 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-24 Thread Sergio Afonso via llvm-branch-commits


@@ -2081,6 +2083,79 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end());
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  // Only 1 member has been mapped, we can return it.
+  if (mapInfo.getMembersIndex()->size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  std::vector indices(
+  mapInfo.getMembersIndexAttr().getShapedType().getShape()[0]);
+  std::iota(indices.begin(), indices.end(), 0);

skatrak wrote:

Disregard this suggestion, it's not the same. Sorry about that!

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Jan Leyonberg via llvm-branch-commits

https://github.com/jsjodin approved this pull request.

Other than fixing some nits I think this looks good.


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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Jan Leyonberg via llvm-branch-commits

https://github.com/jsjodin edited 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Jan Leyonberg via llvm-branch-commits

https://github.com/jsjodin edited 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Jan Leyonberg via llvm-branch-commits


@@ -2283,16 +2386,12 @@ static void processMapMembersWithParent(
   for (auto mappedMembers : parentClause.getMembers()) {
 auto memberClause =
 mlir::dyn_cast(mappedMembers.getDefiningOp());

jsjodin wrote:

Another dyn_cast here that could be a cast.

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -33,6 +33,8 @@
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 
 #include 
+#include 
+#include 

skatrak wrote:

Maybe not needed if `std::iota` call is removed?

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2081,6 +2083,79 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end());
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  // Only 1 member has been mapped, we can return it.
+  if (mapInfo.getMembersIndex()->size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  std::vector indices(
+  mapInfo.getMembersIndexAttr().getShapedType().getShape()[0]);
+  std::iota(indices.begin(), indices.end(), 0);
+
+  llvm::sort(
+  indices.begin(), indices.end(), [&](const size_t a, const size_t b) {
+for (int i = 0;
+ i < mapInfo.getMembersIndexAttr().getShapedType().getShape()[1];
+ ++i) {
+  int aIndex =
+  mapInfo.getMembersIndexAttr()
+  .getValues()[a * mapInfo.getMembersIndexAttr()
+.getShapedType()
+.getShape()[1] +
+i];
+  int bIndex =
+  mapInfo.getMembersIndexAttr()
+  .getValues()[b * mapInfo.getMembersIndexAttr()
+.getShapedType()
+.getShape()[1] +
+i];
+
+  // As we have iterated to a stage where both indices are invalid
+  // we likely have the same member index, possibly the same member
+  // being mapped, return the first.
+  if (aIndex == -1 && bIndex == -1)
+return true;
+
+  if (aIndex == -1)
+return true;
+
+  if (bIndex == -1)
+return false;

skatrak wrote:

Redundant check, if I'm not wrong.
```suggestion
  if (aIndex == -1)
return true;

  if (bIndex == -1)
return false;
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2210,42 +2287,68 @@ static llvm::omp::OpenMPOffloadMappingFlags 
mapParentWithMembers(
   // Fortran pointers and allocatables, the mapping of the pointed to
   // data by the descriptor (which itself, is a structure containing
   // runtime information on the dynamically allocated data).
-  llvm::Value *lowAddr = builder.CreatePointerCast(
-  mapData.Pointers[mapDataIndex], builder.getPtrTy());
-  llvm::Value *highAddr = builder.CreatePointerCast(
-  builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
- mapData.Pointers[mapDataIndex], 1),
-  builder.getPtrTy());
+  auto parentClause =
+  mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+
+  llvm::Value *lowAddr, *highAddr;
+  if (!parentClause.getPartialMap()) {
+lowAddr = builder.CreatePointerCast(mapData.Pointers[mapDataIndex],
+builder.getPtrTy());
+highAddr = builder.CreatePointerCast(
+builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
+   mapData.Pointers[mapDataIndex], 1),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIndex]);
+  } else {
+auto mapOp =
+mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+int firstMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, true));
+lowAddr = builder.CreatePointerCast(mapData.Pointers[firstMemberIdx],
+builder.getPtrTy());
+int lastMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, false));
+highAddr = builder.CreatePointerCast(
+builder.CreateGEP(mapData.BaseType[lastMemberIdx],
+  mapData.Pointers[lastMemberIdx], 
builder.getInt64(1)),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[firstMemberIdx]);
+  }
+
   llvm::Value *size = builder.CreateIntCast(
   builder.CreatePtrDiff(builder.getInt8Ty(), highAddr, lowAddr),
   builder.getInt64Ty(),
   /*isSigned=*/false);
   combinedInfo.Sizes.push_back(size);
 
-  // This creates the initial MEMBER_OF mapping that consists of
-  // the parent/top level container (same as above effectively, except
-  // with a fixed initial compile time size and seperate maptype which
-  // indicates the true mape type (tofrom etc.) and that it is a part
-  // of a larger mapping and indicating the link between it and it's
-  // members that are also explicitly mapped).
+  // TODO: This will need expanded to include the whole host of logic for the
+  // map flags that Clang currently supports (e.g. it hsould take the map flag
+  // of the parent map flag, remove the OMP_MAP_TARGET_PARAM and do some 
further
+  // case specific flag modifications), for the moment it handles what we

skatrak wrote:

```suggestion
  // case-specific flag modifications). For the moment, it handles what we
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2081,6 +2083,79 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end());
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  // Only 1 member has been mapped, we can return it.
+  if (mapInfo.getMembersIndex()->size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  std::vector indices(
+  mapInfo.getMembersIndexAttr().getShapedType().getShape()[0]);

skatrak wrote:

Nit: Maybe store the output of `mapInfo.getMembersIndexAttr()` and a reference 
to the result of calling   `getShapedType().getShape()` on it to reduce 
verbosity below, since there are many uses of these.

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2306,18 +2405,81 @@ static void processMapMembersWithParent(
 llvm::OpenMPIRBuilder::DeviceInfoTy::None);
 combinedInfo.Names.emplace_back(
 LLVM::createMappingInformation(memberClause.getLoc(), ompBuilder));
-
-
combinedInfo.BasePointers.emplace_back(mapData.BasePointers[memberDataIdx]);
+combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIndex]);
 combinedInfo.Pointers.emplace_back(mapData.Pointers[memberDataIdx]);
 combinedInfo.Sizes.emplace_back(mapData.Sizes[memberDataIdx]);
   }
 }
 
+static void
+processIndividualMap(MapInfoData , size_t mapDataIdx,
+ llvm::OpenMPIRBuilder::MapInfosTy ,
+ bool isTargetParams, int mapDataParentIdx = -1) {
+  // Declare Target Mappings are excluded from being marked as
+  // OMP_MAP_TARGET_PARAM as they are not passed as parameters, they're
+  // marked with OMP_MAP_PTR_AND_OBJ instead.
+  auto mapFlag = mapData.Types[mapDataIdx];
+  auto mapInfoOp =
+  dyn_cast(mapData.MapClause[mapDataIdx]);

skatrak wrote:

```suggestion
  cast(mapData.MapClause[mapDataIdx]);
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2306,18 +2405,81 @@ static void processMapMembersWithParent(
 llvm::OpenMPIRBuilder::DeviceInfoTy::None);
 combinedInfo.Names.emplace_back(
 LLVM::createMappingInformation(memberClause.getLoc(), ompBuilder));
-
-
combinedInfo.BasePointers.emplace_back(mapData.BasePointers[memberDataIdx]);
+combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIndex]);
 combinedInfo.Pointers.emplace_back(mapData.Pointers[memberDataIdx]);
 combinedInfo.Sizes.emplace_back(mapData.Sizes[memberDataIdx]);
   }
 }
 
+static void
+processIndividualMap(MapInfoData , size_t mapDataIdx,
+ llvm::OpenMPIRBuilder::MapInfosTy ,
+ bool isTargetParams, int mapDataParentIdx = -1) {
+  // Declare Target Mappings are excluded from being marked as
+  // OMP_MAP_TARGET_PARAM as they are not passed as parameters, they're
+  // marked with OMP_MAP_PTR_AND_OBJ instead.
+  auto mapFlag = mapData.Types[mapDataIdx];
+  auto mapInfoOp =
+  dyn_cast(mapData.MapClause[mapDataIdx]);
+
+  bool isPtrTy = checkIfPointerMap(mapInfoOp);
+  if (isPtrTy)
+mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_PTR_AND_OBJ;
+
+  if (isTargetParams && !mapData.IsDeclareTarget[mapDataIdx])
+mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TARGET_PARAM;
+
+  if (mapInfoOp.getMapCaptureType().value() ==
+  mlir::omp::VariableCaptureKind::ByCopy &&
+  !isPtrTy)
+mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_LITERAL;
+
+  // if we're provided a mapDataParentIdx, then the data being mapped is
+  // part of a larger object (in a parent <-> member mapping) and in this
+  // case our BasePointer should be the parent.
+  if (mapDataParentIdx >= 0)
+combinedInfo.BasePointers.emplace_back(
+mapData.BasePointers[mapDataParentIdx]);
+  else
+combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIdx]);
+
+  combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIdx]);
+  combinedInfo.DevicePointers.emplace_back(mapData.DevicePointers[mapDataIdx]);
+  combinedInfo.Names.emplace_back(mapData.Names[mapDataIdx]);
+  combinedInfo.Types.emplace_back(mapFlag);
+  combinedInfo.Sizes.emplace_back(mapData.Sizes[mapDataIdx]);
+}
+
 static void processMapWithMembersOf(
 LLVM::ModuleTranslation , llvm::IRBuilderBase ,
 llvm::OpenMPIRBuilder , DataLayout ,
 llvm::OpenMPIRBuilder::MapInfosTy , MapInfoData ,
 uint64_t mapDataIndex, bool isTargetParams) {
+  auto parentClause =
+  mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+
+  // If we have a partial map (no parent referneced in the map clauses of the
+  // directive, only members) and only a single member, we do not need to bind
+  // the map of the member to the parent, we can pass the member seperately.
+  if (parentClause.getMembers().size() == 1 && parentClause.getPartialMap()) {
+auto memberClause = mlir::dyn_cast(

skatrak wrote:

```suggestion
auto memberClause = mlir::cast(
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2283,16 +2386,12 @@ static void processMapMembersWithParent(
   for (auto mappedMembers : parentClause.getMembers()) {
 auto memberClause =
 mlir::dyn_cast(mappedMembers.getDefiningOp());
-int memberDataIdx = -1;
-for (size_t i = 0; i < mapData.MapClause.size(); ++i) {
-  if (mapData.MapClause[i] == memberClause)
-memberDataIdx = i;
-}
+int memberDataIdx = getMapDataMemberIdx(mapData, memberClause);
 
 assert(memberDataIdx >= 0 && "could not find mapped member of structure");
 
 // Same MemberOfFlag to indicate its link with parent and other members
-// of, and we flag that it's part of a pointer and object coupling.
+// of

skatrak wrote:

```suggestion
// of.
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2210,42 +2287,68 @@ static llvm::omp::OpenMPOffloadMappingFlags 
mapParentWithMembers(
   // Fortran pointers and allocatables, the mapping of the pointed to
   // data by the descriptor (which itself, is a structure containing
   // runtime information on the dynamically allocated data).
-  llvm::Value *lowAddr = builder.CreatePointerCast(
-  mapData.Pointers[mapDataIndex], builder.getPtrTy());
-  llvm::Value *highAddr = builder.CreatePointerCast(
-  builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
- mapData.Pointers[mapDataIndex], 1),
-  builder.getPtrTy());
+  auto parentClause =
+  mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+
+  llvm::Value *lowAddr, *highAddr;
+  if (!parentClause.getPartialMap()) {
+lowAddr = builder.CreatePointerCast(mapData.Pointers[mapDataIndex],
+builder.getPtrTy());
+highAddr = builder.CreatePointerCast(
+builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
+   mapData.Pointers[mapDataIndex], 1),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIndex]);
+  } else {
+auto mapOp =
+mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+int firstMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, true));
+lowAddr = builder.CreatePointerCast(mapData.Pointers[firstMemberIdx],
+builder.getPtrTy());
+int lastMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, false));
+highAddr = builder.CreatePointerCast(
+builder.CreateGEP(mapData.BaseType[lastMemberIdx],
+  mapData.Pointers[lastMemberIdx], 
builder.getInt64(1)),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[firstMemberIdx]);
+  }
+
   llvm::Value *size = builder.CreateIntCast(
   builder.CreatePtrDiff(builder.getInt8Ty(), highAddr, lowAddr),
   builder.getInt64Ty(),
   /*isSigned=*/false);
   combinedInfo.Sizes.push_back(size);
 
-  // This creates the initial MEMBER_OF mapping that consists of
-  // the parent/top level container (same as above effectively, except
-  // with a fixed initial compile time size and seperate maptype which
-  // indicates the true mape type (tofrom etc.) and that it is a part
-  // of a larger mapping and indicating the link between it and it's
-  // members that are also explicitly mapped).
+  // TODO: This will need expanded to include the whole host of logic for the
+  // map flags that Clang currently supports (e.g. it hsould take the map flag
+  // of the parent map flag, remove the OMP_MAP_TARGET_PARAM and do some 
further
+  // case specific flag modifications), for the moment it handles what we
+  // support as expected.
   llvm::omp::OpenMPOffloadMappingFlags mapFlag =
   llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO;
-  if (isTargetParams)
-mapFlag &= ~llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TARGET_PARAM;
 
   llvm::omp::OpenMPOffloadMappingFlags memberOfFlag =
   ompBuilder.getMemberOfFlag(combinedInfo.BasePointers.size() - 1);
   ompBuilder.setCorrectMemberOfFlag(mapFlag, memberOfFlag);
 
-  combinedInfo.Types.emplace_back(mapFlag);
-  combinedInfo.DevicePointers.emplace_back(
-  llvm::OpenMPIRBuilder::DeviceInfoTy::None);
-  combinedInfo.Names.emplace_back(LLVM::createMappingInformation(
-  mapData.MapClause[mapDataIndex]->getLoc(), ompBuilder));
-  combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIndex]);
-  combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIndex]);
-  combinedInfo.Sizes.emplace_back(mapData.Sizes[mapDataIndex]);
-
+  // This creates the initial MEMBER_OF mapping that consists of
+  // the parent/top level container (same as above effectively, except
+  // with a fixed initial compile time size and seperate maptype which
+  // indicates the true mape type (tofrom etc.). This parent mapping is
+  // only relevant if the structure in it's totality is being mapped,

skatrak wrote:

```suggestion
  // only relevant if the structure in its totality is being mapped,
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2210,42 +2287,68 @@ static llvm::omp::OpenMPOffloadMappingFlags 
mapParentWithMembers(
   // Fortran pointers and allocatables, the mapping of the pointed to
   // data by the descriptor (which itself, is a structure containing
   // runtime information on the dynamically allocated data).
-  llvm::Value *lowAddr = builder.CreatePointerCast(
-  mapData.Pointers[mapDataIndex], builder.getPtrTy());
-  llvm::Value *highAddr = builder.CreatePointerCast(
-  builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
- mapData.Pointers[mapDataIndex], 1),
-  builder.getPtrTy());
+  auto parentClause =
+  mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+
+  llvm::Value *lowAddr, *highAddr;
+  if (!parentClause.getPartialMap()) {
+lowAddr = builder.CreatePointerCast(mapData.Pointers[mapDataIndex],
+builder.getPtrTy());
+highAddr = builder.CreatePointerCast(
+builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
+   mapData.Pointers[mapDataIndex], 1),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIndex]);
+  } else {
+auto mapOp =
+mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+int firstMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, true));
+lowAddr = builder.CreatePointerCast(mapData.Pointers[firstMemberIdx],
+builder.getPtrTy());
+int lastMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, false));
+highAddr = builder.CreatePointerCast(
+builder.CreateGEP(mapData.BaseType[lastMemberIdx],
+  mapData.Pointers[lastMemberIdx], 
builder.getInt64(1)),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[firstMemberIdx]);
+  }
+
   llvm::Value *size = builder.CreateIntCast(
   builder.CreatePtrDiff(builder.getInt8Ty(), highAddr, lowAddr),
   builder.getInt64Ty(),
   /*isSigned=*/false);
   combinedInfo.Sizes.push_back(size);
 
-  // This creates the initial MEMBER_OF mapping that consists of
-  // the parent/top level container (same as above effectively, except
-  // with a fixed initial compile time size and seperate maptype which
-  // indicates the true mape type (tofrom etc.) and that it is a part
-  // of a larger mapping and indicating the link between it and it's
-  // members that are also explicitly mapped).
+  // TODO: This will need expanded to include the whole host of logic for the

skatrak wrote:

```suggestion
  // TODO: This will need to be expanded to include the whole host of logic for 
the
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2081,6 +2083,79 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end());
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  // Only 1 member has been mapped, we can return it.
+  if (mapInfo.getMembersIndex()->size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  std::vector indices(
+  mapInfo.getMembersIndexAttr().getShapedType().getShape()[0]);
+  std::iota(indices.begin(), indices.end(), 0);

skatrak wrote:

```suggestion
  llvm::SmallVector indices(
  mapInfo.getMembersIndexAttr().getShapedType().getShape()[0], 0);
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2210,42 +2287,68 @@ static llvm::omp::OpenMPOffloadMappingFlags 
mapParentWithMembers(
   // Fortran pointers and allocatables, the mapping of the pointed to
   // data by the descriptor (which itself, is a structure containing
   // runtime information on the dynamically allocated data).
-  llvm::Value *lowAddr = builder.CreatePointerCast(
-  mapData.Pointers[mapDataIndex], builder.getPtrTy());
-  llvm::Value *highAddr = builder.CreatePointerCast(
-  builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
- mapData.Pointers[mapDataIndex], 1),
-  builder.getPtrTy());
+  auto parentClause =
+  mlir::dyn_cast(mapData.MapClause[mapDataIndex]);

skatrak wrote:

Nit: It's assumed that it returned a valid object by never checking it, so it's 
better to use `cast` to at least assert this is the case.
```suggestion
  llvm::cast(mapData.MapClause[mapDataIndex]);
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2081,6 +2083,79 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end());
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  // Only 1 member has been mapped, we can return it.
+  if (mapInfo.getMembersIndex()->size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  std::vector indices(
+  mapInfo.getMembersIndexAttr().getShapedType().getShape()[0]);
+  std::iota(indices.begin(), indices.end(), 0);
+
+  llvm::sort(
+  indices.begin(), indices.end(), [&](const size_t a, const size_t b) {
+for (int i = 0;
+ i < mapInfo.getMembersIndexAttr().getShapedType().getShape()[1];
+ ++i) {
+  int aIndex =
+  mapInfo.getMembersIndexAttr()
+  .getValues()[a * mapInfo.getMembersIndexAttr()
+.getShapedType()
+.getShape()[1] +
+i];
+  int bIndex =
+  mapInfo.getMembersIndexAttr()
+  .getValues()[b * mapInfo.getMembersIndexAttr()
+.getShapedType()
+.getShape()[1] +
+i];
+
+  // As we have iterated to a stage where both indices are invalid
+  // we likely have the same member index, possibly the same member
+  // being mapped, return the first.
+  if (aIndex == -1 && bIndex == -1)
+return true;
+
+  if (aIndex == -1)
+return true;
+
+  if (bIndex == -1)
+return false;
+
+  // A is earlier in the record type layout than B
+  if (aIndex < bIndex)
+return true;
+
+  if (bIndex < aIndex)
+return false;
+}
+
+// iterated the entire list and couldn't make a decision, all elements
+// were likely the same, return true for now similar to reaching the 
end
+// of both and finding invalid indices.
+return true;
+  });
+
+  if (auto mapOp = mlir::dyn_cast(
+  mapInfo.getMembers()[((first) ? indices.front() : indices.back())]
+  .getDefiningOp()))
+return mapOp;
+
+  assert(false && "getFirstOrLastMappedMemberPtr could not find approproaite "
+  "map information");
+  return {};

skatrak wrote:

Nit: Replace with `llvm_unreachable()` for a custom failure message or `return 
llvm::cast(...)`, since that already asserts.

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2306,18 +2405,81 @@ static void processMapMembersWithParent(
 llvm::OpenMPIRBuilder::DeviceInfoTy::None);
 combinedInfo.Names.emplace_back(
 LLVM::createMappingInformation(memberClause.getLoc(), ompBuilder));
-
-
combinedInfo.BasePointers.emplace_back(mapData.BasePointers[memberDataIdx]);
+combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIndex]);
 combinedInfo.Pointers.emplace_back(mapData.Pointers[memberDataIdx]);
 combinedInfo.Sizes.emplace_back(mapData.Sizes[memberDataIdx]);
   }
 }
 
+static void
+processIndividualMap(MapInfoData , size_t mapDataIdx,
+ llvm::OpenMPIRBuilder::MapInfosTy ,
+ bool isTargetParams, int mapDataParentIdx = -1) {
+  // Declare Target Mappings are excluded from being marked as
+  // OMP_MAP_TARGET_PARAM as they are not passed as parameters, they're
+  // marked with OMP_MAP_PTR_AND_OBJ instead.
+  auto mapFlag = mapData.Types[mapDataIdx];
+  auto mapInfoOp =
+  dyn_cast(mapData.MapClause[mapDataIdx]);
+
+  bool isPtrTy = checkIfPointerMap(mapInfoOp);
+  if (isPtrTy)
+mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_PTR_AND_OBJ;
+
+  if (isTargetParams && !mapData.IsDeclareTarget[mapDataIdx])
+mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TARGET_PARAM;
+
+  if (mapInfoOp.getMapCaptureType().value() ==
+  mlir::omp::VariableCaptureKind::ByCopy &&
+  !isPtrTy)
+mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_LITERAL;
+
+  // if we're provided a mapDataParentIdx, then the data being mapped is
+  // part of a larger object (in a parent <-> member mapping) and in this
+  // case our BasePointer should be the parent.
+  if (mapDataParentIdx >= 0)
+combinedInfo.BasePointers.emplace_back(
+mapData.BasePointers[mapDataParentIdx]);
+  else
+combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIdx]);
+
+  combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIdx]);
+  combinedInfo.DevicePointers.emplace_back(mapData.DevicePointers[mapDataIdx]);
+  combinedInfo.Names.emplace_back(mapData.Names[mapDataIdx]);
+  combinedInfo.Types.emplace_back(mapFlag);
+  combinedInfo.Sizes.emplace_back(mapData.Sizes[mapDataIdx]);
+}
+
 static void processMapWithMembersOf(
 LLVM::ModuleTranslation , llvm::IRBuilderBase ,
 llvm::OpenMPIRBuilder , DataLayout ,
 llvm::OpenMPIRBuilder::MapInfosTy , MapInfoData ,
 uint64_t mapDataIndex, bool isTargetParams) {
+  auto parentClause =
+  mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+
+  // If we have a partial map (no parent referneced in the map clauses of the

skatrak wrote:

```suggestion
  // If we have a partial map (no parent referenced in the map clauses of the
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2186,6 +2261,9 @@ calculateBoundsOffset(LLVM::ModuleTranslation 
,
 // which is utilised in subsequent member mappings (by modifying there map type
 // with it) to indicate that a member is part of this parent and should be
 // treated by the runtime as such. Important to achieve the correct mapping.
+//
+// This function borrows a lot from it's Clang parallel function
+// emitCombinedEntry inside of CGOpenMPRuntime.cpp

skatrak wrote:

```suggestion
// This function borrows a lot from Clang's emitCombinedEntry function
// inside of CGOpenMPRuntime.cpp
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2210,42 +2287,68 @@ static llvm::omp::OpenMPOffloadMappingFlags 
mapParentWithMembers(
   // Fortran pointers and allocatables, the mapping of the pointed to
   // data by the descriptor (which itself, is a structure containing
   // runtime information on the dynamically allocated data).
-  llvm::Value *lowAddr = builder.CreatePointerCast(
-  mapData.Pointers[mapDataIndex], builder.getPtrTy());
-  llvm::Value *highAddr = builder.CreatePointerCast(
-  builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
- mapData.Pointers[mapDataIndex], 1),
-  builder.getPtrTy());
+  auto parentClause =
+  mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+
+  llvm::Value *lowAddr, *highAddr;
+  if (!parentClause.getPartialMap()) {
+lowAddr = builder.CreatePointerCast(mapData.Pointers[mapDataIndex],
+builder.getPtrTy());
+highAddr = builder.CreatePointerCast(
+builder.CreateConstGEP1_32(mapData.BaseType[mapDataIndex],
+   mapData.Pointers[mapDataIndex], 1),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIndex]);
+  } else {
+auto mapOp =
+mlir::dyn_cast(mapData.MapClause[mapDataIndex]);
+int firstMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, true));
+lowAddr = builder.CreatePointerCast(mapData.Pointers[firstMemberIdx],
+builder.getPtrTy());
+int lastMemberIdx = getMapDataMemberIdx(
+mapData, getFirstOrLastMappedMemberPtr(mapOp, false));
+highAddr = builder.CreatePointerCast(
+builder.CreateGEP(mapData.BaseType[lastMemberIdx],
+  mapData.Pointers[lastMemberIdx], 
builder.getInt64(1)),
+builder.getPtrTy());
+combinedInfo.Pointers.emplace_back(mapData.Pointers[firstMemberIdx]);
+  }
+
   llvm::Value *size = builder.CreateIntCast(
   builder.CreatePtrDiff(builder.getInt8Ty(), highAddr, lowAddr),
   builder.getInt64Ty(),
   /*isSigned=*/false);
   combinedInfo.Sizes.push_back(size);
 
-  // This creates the initial MEMBER_OF mapping that consists of
-  // the parent/top level container (same as above effectively, except
-  // with a fixed initial compile time size and seperate maptype which
-  // indicates the true mape type (tofrom etc.) and that it is a part
-  // of a larger mapping and indicating the link between it and it's
-  // members that are also explicitly mapped).
+  // TODO: This will need expanded to include the whole host of logic for the
+  // map flags that Clang currently supports (e.g. it hsould take the map flag

skatrak wrote:

```suggestion
  // map flags that Clang currently supports (e.g. it should take the map flag
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak edited 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak commented:

Thank you Andrew, as usual I have a lot of nits but the overall approach seems 
reasonable to me.

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-23 Thread Sergio Afonso via llvm-branch-commits


@@ -2081,6 +2083,79 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end());

skatrak wrote:

Nit: Add a message as well.
```suggestion
  assert(res != mapData.MapClause.end() && "...");
```

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-19 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-04-19 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-03-22 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-03-22 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-03-15 Thread via llvm-branch-commits

https://github.com/agozillon edited 
https://github.com/llvm/llvm-project/pull/82852
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-03-15 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-03-15 Thread via llvm-branch-commits

https://github.com/agozillon updated 
https://github.com/llvm/llvm-project/pull/82852


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-03-05 Thread via llvm-branch-commits

agozillon wrote:

Small ping for attention on this PR please, to start nudging it towards the 
finish line if there is one in its future. I am sorry ahead of time for 
stealing some of a reviewers precious time. 

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


[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-02-23 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir-llvm

Author: None (agozillon)


Changes

This patch seeks to refactor slightly and extend the current record type map
support that was put in place for Fortran's descriptor types to handle explicit
member mapping for record types at a single level of depth (the case of explicit
mapping of nested record types is currently unsupported).

This patch seeks to support this by extending the OpenMPToLLVMIRTranslation 
phase
to more generally support record types, building on the prior groundwork in the
Fortran allocatables/pointers patch. It now supports different kinds of record 
type
mapping, in this case full record type mapping and then explicit member mapping
in which there is a special case for certain types when mapped individually to 
not
require any parent map link in the kernel argument structure. To facilitate this
required:
   *  The movement of the setting of the map flag type "ptr_and_obj" to 
respective
 frontends, now supporting it as a possible flag that can be read and 
printed
 in mlir form. Some minor changes to declare target map type setting was
 neccesary for this.
   * The addition of a member index array operand, which tracks the position
   of the member in the parent, required for caclulating the appropriate 
size
   to offload to the target, alongside the parents offload pointer (always 
the
   first member currently being mapped).
   * A partial mapping attribute operand, to indicate if the entire record type 
is
   being mapped or just member components, aiding the ability to lower
   record types in the different manners that are possible.
   * Refactoring bounds calculation for record types and general arrays to one
   location (as well as load/store generation prior to assigning to the 
kernel
   argument structure), as a side affect enter/exit/update/data mapping
   should now be more correct and fully support bounds mapping, previously
   this would have only worked for target.


---

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


4 Files Affected:

- (modified) 
mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp (+345-206) 
- (modified) 
mlir/test/Target/LLVMIR/omptarget-fortran-allocatable-types-host.mlir (+14-16) 
- (modified) mlir/test/Target/LLVMIR/omptarget-llvm.mlir (+27-22) 
- (added) mlir/test/Target/LLVMIR/omptarget-record-type-mapping-host.mlir (+63) 


``diff
diff --git 
a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp 
b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index 6e53d801a0d2f0..f234fcee388e30 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -33,6 +33,7 @@
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 
 #include 
+#include 
 #include 
 #include 
 
@@ -1750,9 +1751,9 @@ void collectMapDataFromMapOperands(MapInfoData ,
 
   mapData.BaseType.push_back(
   moduleTranslation.convertType(mapOp.getVarType()));
-  mapData.Sizes.push_back(getSizeInBytes(
-  dl, mapOp.getVarType(), mapOp, mapData.BasePointers.back(),
-  mapData.BaseType.back(), builder, moduleTranslation));
+  mapData.Sizes.push_back(
+  getSizeInBytes(dl, mapOp.getVarType(), mapOp, 
mapData.Pointers.back(),
+ mapData.BaseType.back(), builder, moduleTranslation));
   mapData.MapClause.push_back(mapOp.getOperation());
   mapData.Types.push_back(
   llvm::omp::OpenMPOffloadMappingFlags(mapOp.getMapType().value()));
@@ -1783,6 +1784,134 @@ void collectMapDataFromMapOperands(MapInfoData ,
   }
 }
 
+static int getMapDataMemberIdx(MapInfoData ,
+   mlir::omp::MapInfoOp memberOp) {
+  auto *res = llvm::find(mapData.MapClause, memberOp);
+  assert(res != mapData.MapClause.end());
+  return std::distance(mapData.MapClause.begin(), res);
+}
+
+static mlir::omp::MapInfoOp
+getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
+  // Only 1 member has been mapped, we can return it.
+  if (mapInfo.getMembersIndex()->size() == 1)
+if (auto mapOp = mlir::dyn_cast(
+mapInfo.getMembers()[0].getDefiningOp()))
+  return mapOp;
+
+  std::vector indices(mapInfo.getMembersIndexAttr().size());
+  std::iota(indices.begin(), indices.end(), 0);
+  llvm::sort(indices.begin(), indices.end(),
+ [&](const size_t a, const size_t b) {
+   return mapInfo.getMembersIndexAttr()[a]
+  .cast()
+  .getInt() < mapInfo.getMembersIndexAttr()[b]
+  .cast()
+  .getInt();
+ });
+
+  if (auto mapOp = mlir::dyn_cast(
+  mapInfo.getMembers()[((first) ? indices.front() : 

[llvm-branch-commits] [OpenMP][MLIR] Extend record member map support for omp dialect to LLVM-IR (PR #82852)

2024-02-23 Thread via llvm-branch-commits

https://github.com/agozillon created 
https://github.com/llvm/llvm-project/pull/82852

This patch seeks to refactor slightly and extend the current record type map
support that was put in place for Fortran's descriptor types to handle explicit
member mapping for record types at a single level of depth (the case of explicit
mapping of nested record types is currently unsupported).

This patch seeks to support this by extending the OpenMPToLLVMIRTranslation 
phase
to more generally support record types, building on the prior groundwork in the
Fortran allocatables/pointers patch. It now supports different kinds of record 
type
mapping, in this case full record type mapping and then explicit member mapping
in which there is a special case for certain types when mapped individually to 
not
require any parent map link in the kernel argument structure. To facilitate this
required:
   *  The movement of the setting of the map flag type "ptr_and_obj" to 
respective
 frontends, now supporting it as a possible flag that can be read and 
printed
 in mlir form. Some minor changes to declare target map type setting was
 neccesary for this.
   * The addition of a member index array operand, which tracks the position
   of the member in the parent, required for caclulating the appropriate 
size
   to offload to the target, alongside the parents offload pointer (always 
the
   first member currently being mapped).
   * A partial mapping attribute operand, to indicate if the entire record type 
is
   being mapped or just member components, aiding the ability to lower
   record types in the different manners that are possible.
   * Refactoring bounds calculation for record types and general arrays to one
   location (as well as load/store generation prior to assigning to the 
kernel
   argument structure), as a side affect enter/exit/update/data mapping
   should now be more correct and fully support bounds mapping, previously
   this would have only worked for target.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits