Skip to content

Commit e4eaf9d

Browse files
jhuber6jhuber-ornl
authored andcommitted
[OpenMP] Add support for mapping names in mapper API
Summary: The custom mapper API did not previously support the mapping names added previously. This means they were not present if a user requested debugging information while using the mapper functions. This adds basic support for passing the mapped names to the runtime library. Reviewers: jdoerfert Differential Revision: https://reviews.llvm.org/D94806
1 parent 20566a2 commit e4eaf9d

File tree

10 files changed

+112
-70
lines changed

10 files changed

+112
-70
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9502,7 +9502,8 @@ getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) {
95029502
/// \code
95039503
/// void .omp_mapper.<type_name>.<mapper_id>.(void *rt_mapper_handle,
95049504
/// void *base, void *begin,
9505-
/// int64_t size, int64_t type) {
9505+
/// int64_t size, int64_t type,
9506+
/// void *name = nullptr) {
95069507
/// // Allocate space for an array section first.
95079508
/// if (size > 1 && !maptype.IsDelete)
95089509
/// __tgt_push_mapper_component(rt_mapper_handle, base, begin,
@@ -9513,10 +9514,11 @@ getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) {
95139514
/// for (auto c : all_components) {
95149515
/// if (c.hasMapper())
95159516
/// (*c.Mapper())(rt_mapper_handle, c.arg_base, c.arg_begin, c.arg_size,
9516-
/// c.arg_type);
9517+
/// c.arg_type, c.arg_name);
95179518
/// else
95189519
/// __tgt_push_mapper_component(rt_mapper_handle, c.arg_base,
9519-
/// c.arg_begin, c.arg_size, c.arg_type);
9520+
/// c.arg_begin, c.arg_size, c.arg_type,
9521+
/// c.arg_name);
95209522
/// }
95219523
/// }
95229524
/// // Delete the array section.
@@ -9549,12 +9551,15 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
95499551
ImplicitParamDecl::Other);
95509552
ImplicitParamDecl TypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int64Ty,
95519553
ImplicitParamDecl::Other);
9554+
ImplicitParamDecl NameArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
9555+
ImplicitParamDecl::Other);
95529556
FunctionArgList Args;
95539557
Args.push_back(&HandleArg);
95549558
Args.push_back(&BaseArg);
95559559
Args.push_back(&BeginArg);
95569560
Args.push_back(&SizeArg);
95579561
Args.push_back(&TypeArg);
9562+
Args.push_back(&NameArg);
95589563
const CGFunctionInfo &FnInfo =
95599564
CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
95609565
llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
@@ -9654,6 +9659,10 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
96549659
llvm::Value *CurBeginArg = MapperCGF.Builder.CreateBitCast(
96559660
Info.Pointers[I], CGM.getTypes().ConvertTypeForMem(C.VoidPtrTy));
96569661
llvm::Value *CurSizeArg = Info.Sizes[I];
9662+
llvm::Value *CurNameArg =
9663+
(CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo)
9664+
? llvm::ConstantPointerNull::get(CGM.VoidPtrTy)
9665+
: emitMappingInformation(MapperCGF, OMPBuilder, Info.Exprs[I]);
96579666

96589667
// Extract the MEMBER_OF field from the map type.
96599668
llvm::BasicBlock *MemberBB = MapperCGF.createBasicBlock("omp.member");
@@ -9742,8 +9751,8 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
97429751
CurMapType->addIncoming(FromMapType, FromBB);
97439752
CurMapType->addIncoming(MemberMapType, ToElseBB);
97449753

9745-
llvm::Value *OffloadingArgs[] = {Handle, CurBaseArg, CurBeginArg,
9746-
CurSizeArg, CurMapType};
9754+
llvm::Value *OffloadingArgs[] = {Handle, CurBaseArg, CurBeginArg,
9755+
CurSizeArg, CurMapType, CurNameArg};
97479756
if (Info.Mappers[I]) {
97489757
// Call the corresponding mapper function.
97499758
llvm::Function *MapperFunc = getOrCreateUserDefinedMapperFunc(
@@ -9833,9 +9842,12 @@ void CGOpenMPRuntime::emitUDMapperArrayInitOrDel(
98339842
MapType,
98349843
MapperCGF.Builder.getInt64(~(MappableExprsHandler::OMP_MAP_TO |
98359844
MappableExprsHandler::OMP_MAP_FROM)));
9845+
llvm::Value *MapNameArg = llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
9846+
98369847
// Call the runtime API __tgt_push_mapper_component to fill up the runtime
98379848
// data structure.
9838-
llvm::Value *OffloadingArgs[] = {Handle, Base, Begin, ArraySize, MapTypeArg};
9849+
llvm::Value *OffloadingArgs[] = {Handle, Base, Begin,
9850+
ArraySize, MapTypeArg, MapNameArg};
98399851
MapperCGF.EmitRuntimeCall(
98409852
OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
98419853
OMPRTL___tgt_push_mapper_component),

clang/test/OpenMP/declare_mapper_codegen.cpp

Lines changed: 36 additions & 36 deletions
Large diffs are not rendered by default.

clang/test/OpenMP/target_depend_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int foo(int n) {
130130
// CHECK-DAG: [[CPADDR2:%.+]] = bitcast i8** [[PADDR2]] to [[STRUCT_TT]]**
131131
// CHECK-DAG: store [[STRUCT_TT]]* [[D_ADDR:%.+]], [[STRUCT_TT]]** [[CBPADDR2]]
132132
// CHECK-DAG: store [[STRUCT_TT]]* [[D_ADDR]], [[STRUCT_TT]]** [[CPADDR2]]
133-
// CHECK-DAG: store i8* bitcast (void (i8*, i8*, i8*, i64, i64)* [[MAPPER_ID:@.+]] to i8*), i8** [[MADDR2]],
133+
// CHECK-DAG: store i8* bitcast (void (i8*, i8*, i8*, i64, i64, i8*)* [[MAPPER_ID:@.+]] to i8*), i8** [[MADDR2]],
134134

135135
// CHECK-DAG: [[BP_START:%.+]] = getelementptr inbounds [3 x i8*], [3 x i8*]* [[BP]], i32 0, i32 0
136136
// CHECK-DAG: [[P_START:%.+]] = getelementptr inbounds [3 x i8*], [3 x i8*]* [[P]], i32 0, i32 0

clang/test/OpenMP/target_map_names.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ void baz() {
167167
#pragma omp target update to(t) nowait
168168
}
169169

170+
struct S3 {
171+
double Z[64];
172+
};
173+
174+
#pragma omp declare mapper(id: S3 s) map(s.Z[0:64])
175+
176+
void qux() {
177+
S3 s;
178+
#pragma omp target map(mapper(id), to:s)
179+
{ }
180+
}
181+
182+
// DEBUG: @{{[0-9]+}} = private unnamed_addr constant [{{[0-9]+}} x i8] c";s.Z[0:64];{{.*}}.cpp;{{[0-9]+}};{{[0-9]+}};;\00"
183+
170184
// DEBUG: %{{.+}} = call i32 @__tgt_target_mapper(%struct.ident_t* @{{.+}}, i64 -1, i8* @{{.+}}, i32 1, i8** %{{.+}}, i8** %{{.+}}, i64* {{.+}}, i64* {{.+}}, i8** getelementptr inbounds ([{{[0-9]+}} x i8*], [{{[0-9]+}} x i8*]* @.offload_mapnames{{.*}}, i32 0, i32 0), i8** {{.+}})
171185
// DEBUG: %{{.+}} = call i32 @__tgt_target_teams_mapper(%struct.ident_t* @{{.+}}, i64 -1, i8* @{{.+}}, i32 1, i8** %{{.+}}, i8** %{{.+}}, i64* {{.+}}, i64* {{.+}}, i8** getelementptr inbounds ([{{[0-9]+}} x i8*], [{{[0-9]+}} x i8*]* @.offload_mapnames{{.*}}, i32 0, i32 0), i8** {{.+}}, i32 {{.+}}, i32 {{.+}})
172186
// DEBUG: call void @__tgt_target_data_begin_mapper(%struct.ident_t* @{{.+}}, i64 -1, i32 1, i8** %{{.+}}, i8** %{{.+}}, i64* {{.+}}, i64* {{.+}}, i8** getelementptr inbounds ([{{[0-9]+}} x i8*], [{{[0-9]+}} x i8*]* @.offload_mapnames{{.*}}, i32 0, i32 0), i8** {{.+}})

llvm/include/llvm/Frontend/OpenMP/OMPKinds.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ __OMP_RTL(__tgt_target_data_update_nowait_mapper, false, Void, IdentPtr, Int64,
402402
VoidPtrPtr, VoidPtrPtr, Int64Ptr, Int64Ptr, VoidPtrPtr, VoidPtrPtr)
403403
__OMP_RTL(__tgt_mapper_num_components, false, Int64, VoidPtr)
404404
__OMP_RTL(__tgt_push_mapper_component, false, Void, VoidPtr, VoidPtr, VoidPtr,
405-
Int64, Int64)
405+
Int64, Int64, VoidPtr)
406406
__OMP_RTL(__kmpc_task_allow_completion_event, false, VoidPtr, IdentPtr,
407407
/* Int */ Int32, /* kmp_task_t */ VoidPtr)
408408

llvm/test/Transforms/OpenMP/add_attributes.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ declare void @__tgt_target_data_update_nowait_mapper(%struct.ident_t*, i64, i32,
657657

658658
declare i64 @__tgt_mapper_num_components(i8*)
659659

660-
declare void @__tgt_push_mapper_component(i8*, i8*, i8*, i64, i64)
660+
declare void @__tgt_push_mapper_component(i8*, i8*, i8*, i64, i64, i8*)
661661

662662
declare i8* @__kmpc_task_allow_completion_event(%struct.ident_t*, i32, i8*)
663663

@@ -1189,7 +1189,7 @@ declare void @__kmpc_proxy_task_completed_ooo(i8*)
11891189
; CHECK-NEXT: declare i64 @__tgt_mapper_num_components(i8*)
11901190

11911191
; CHECK: ; Function Attrs: nounwind
1192-
; CHECK-NEXT: declare void @__tgt_push_mapper_component(i8*, i8*, i8*, i64, i64)
1192+
; CHECK-NEXT: declare void @__tgt_push_mapper_component(i8*, i8*, i8*, i64, i64, i8*)
11931193

11941194
; CHECK: ; Function Attrs: nounwind
11951195
; CHECK-NEXT: declare i8* @__kmpc_task_allow_completion_event(%struct.ident_t*, i32, i8*)
@@ -1714,7 +1714,7 @@ declare void @__kmpc_proxy_task_completed_ooo(i8*)
17141714
; OPTIMISTIC-NEXT: declare i64 @__tgt_mapper_num_components(i8*)
17151715

17161716
; OPTIMISTIC: ; Function Attrs: nounwind
1717-
; OPTIMISTIC-NEXT: declare void @__tgt_push_mapper_component(i8*, i8*, i8*, i64, i64)
1717+
; OPTIMISTIC-NEXT: declare void @__tgt_push_mapper_component(i8*, i8*, i8*, i64, i64, i8*)
17181718

17191719
; OPTIMISTIC: ; Function Attrs: nofree nosync nounwind willreturn
17201720
; OPTIMISTIC-NEXT: declare noalias i8* @__kmpc_task_allow_completion_event(%struct.ident_t* nocapture nofree readonly, i32, i8*)

openmp/libomptarget/src/interface.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,17 @@ EXTERN int64_t __tgt_mapper_num_components(void *rt_mapper_handle) {
487487

488488
// Push back one component for a user-defined mapper.
489489
EXTERN void __tgt_push_mapper_component(void *rt_mapper_handle, void *base,
490-
void *begin, int64_t size,
491-
int64_t type) {
490+
void *begin, int64_t size, int64_t type,
491+
void *name) {
492492
TIMESCOPE();
493493
DP("__tgt_push_mapper_component(Handle=" DPxMOD
494494
") adds an entry (Base=" DPxMOD ", Begin=" DPxMOD ", Size=%" PRId64
495-
", Type=0x%" PRIx64 ").\n",
496-
DPxPTR(rt_mapper_handle), DPxPTR(base), DPxPTR(begin), size, type);
495+
", Type=0x%" PRIx64 ", Name=%s).\n",
496+
DPxPTR(rt_mapper_handle), DPxPTR(base), DPxPTR(begin), size, type,
497+
(name) ? getNameFromMapping(name).c_str() : "unknown");
497498
auto *MapperComponentsPtr = (struct MapperComponentsTy *)rt_mapper_handle;
498499
MapperComponentsPtr->Components.push_back(
499-
MapComponentInfoTy(base, begin, size, type));
500+
MapComponentInfoTy(base, begin, size, type, name));
500501
}
501502

502503
EXTERN void __kmpc_push_target_tripcount(ident_t *loc, int64_t device_id,

openmp/libomptarget/src/omptarget.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,16 @@ static int32_t getParentIndex(int64_t type) {
209209
/// Call the user-defined mapper function followed by the appropriate
210210
// target_data_* function (target_data_{begin,end,update}).
211211
int targetDataMapper(DeviceTy &Device, void *arg_base, void *arg,
212-
int64_t arg_size, int64_t arg_type, void *arg_mapper,
212+
int64_t arg_size, int64_t arg_type,
213+
map_var_info_t arg_names, void *arg_mapper,
213214
TargetDataFuncPtrTy target_data_function) {
214215
DP("Calling the mapper function " DPxMOD "\n", DPxPTR(arg_mapper));
215216

216217
// The mapper function fills up Components.
217218
MapperComponentsTy MapperComponents;
218219
MapperFuncPtrTy MapperFuncPtr = (MapperFuncPtrTy)(arg_mapper);
219-
(*MapperFuncPtr)((void *)&MapperComponents, arg_base, arg, arg_size,
220-
arg_type);
220+
(*MapperFuncPtr)((void *)&MapperComponents, arg_base, arg, arg_size, arg_type,
221+
arg_names);
221222

222223
// Construct new arrays for args_base, args, arg_sizes and arg_types
223224
// using the information in MapperComponents and call the corresponding
@@ -226,6 +227,7 @@ int targetDataMapper(DeviceTy &Device, void *arg_base, void *arg,
226227
std::vector<void *> MapperArgs(MapperComponents.Components.size());
227228
std::vector<int64_t> MapperArgSizes(MapperComponents.Components.size());
228229
std::vector<int64_t> MapperArgTypes(MapperComponents.Components.size());
230+
std::vector<void *> MapperArgNames(MapperComponents.Components.size());
229231

230232
for (unsigned I = 0, E = MapperComponents.Components.size(); I < E; ++I) {
231233
auto &C =
@@ -235,12 +237,13 @@ int targetDataMapper(DeviceTy &Device, void *arg_base, void *arg,
235237
MapperArgs[I] = C.Begin;
236238
MapperArgSizes[I] = C.Size;
237239
MapperArgTypes[I] = C.Type;
240+
MapperArgNames[I] = C.Name;
238241
}
239242

240243
int rc = target_data_function(Device, MapperComponents.Components.size(),
241244
MapperArgsBase.data(), MapperArgs.data(),
242245
MapperArgSizes.data(), MapperArgTypes.data(),
243-
/*arg_names*/ nullptr, /*arg_mappers*/ nullptr,
246+
MapperArgNames.data(), /*arg_mappers*/ nullptr,
244247
/*__tgt_async_info*/ nullptr);
245248

246249
return rc;
@@ -264,8 +267,10 @@ int targetDataBegin(DeviceTy &Device, int32_t arg_num, void **args_base,
264267
// with new arguments.
265268
DP("Calling targetDataMapper for the %dth argument\n", i);
266269

270+
map_var_info_t arg_name = (!arg_names) ? nullptr : arg_names[i];
267271
int rc = targetDataMapper(Device, args_base[i], args[i], arg_sizes[i],
268-
arg_types[i], arg_mappers[i], targetDataBegin);
272+
arg_types[i], arg_name, arg_mappers[i],
273+
targetDataBegin);
269274

270275
if (rc != OFFLOAD_SUCCESS) {
271276
REPORT("Call to targetDataBegin via targetDataMapper for custom mapper"
@@ -329,7 +334,7 @@ int targetDataBegin(DeviceTy &Device, int32_t arg_num, void **args_base,
329334
// PTR_AND_OBJ entry is handled below, and so the allocation might fail
330335
// when HasPresentModifier.
331336
PointerTgtPtrBegin = Device.getOrAllocTgtPtr(
332-
HstPtrBase, HstPtrBase, sizeof(void *), HstPtrName, Pointer_IsNew,
337+
HstPtrBase, HstPtrBase, sizeof(void *), nullptr, Pointer_IsNew,
333338
IsHostPtr, IsImplicit, UpdateRef, HasCloseModifier,
334339
HasPresentModifier);
335340
if (!PointerTgtPtrBegin) {
@@ -464,8 +469,10 @@ int targetDataEnd(DeviceTy &Device, int32_t ArgNum, void **ArgBases,
464469
// with new arguments.
465470
DP("Calling targetDataMapper for the %dth argument\n", I);
466471

467-
Ret = targetDataMapper(Device, ArgBases[I], Args[I], ArgSizes[I],
468-
ArgTypes[I], ArgMappers[I], targetDataEnd);
472+
map_var_info_t ArgName = (!ArgNames) ? nullptr : ArgNames[I];
473+
Ret =
474+
targetDataMapper(Device, ArgBases[I], Args[I], ArgSizes[I],
475+
ArgTypes[I], ArgName, ArgMappers[I], targetDataEnd);
469476

470477
if (Ret != OFFLOAD_SUCCESS) {
471478
REPORT("Call to targetDataEnd via targetDataMapper for custom mapper"
@@ -785,8 +792,10 @@ int targetDataUpdate(DeviceTy &Device, int32_t ArgNum, void **ArgsBase,
785792
// with new arguments.
786793
DP("Calling targetDataMapper for the %dth argument\n", I);
787794

795+
map_var_info_t ArgName = (!ArgNames) ? nullptr : ArgNames[I];
788796
int Ret = targetDataMapper(Device, ArgsBase[I], Args[I], ArgSizes[I],
789-
ArgTypes[I], ArgMappers[I], targetDataUpdate);
797+
ArgTypes[I], ArgName, ArgMappers[I],
798+
targetDataUpdate);
790799

791800
if (Ret != OFFLOAD_SUCCESS) {
792801
REPORT("Call to targetDataUpdate via targetDataMapper for custom mapper"

openmp/libomptarget/src/private.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ struct MapComponentInfoTy {
4848
void *Begin;
4949
int64_t Size;
5050
int64_t Type;
51+
void *Name;
5152
MapComponentInfoTy() = default;
52-
MapComponentInfoTy(void *Base, void *Begin, int64_t Size, int64_t Type)
53-
: Base(Base), Begin(Begin), Size(Size), Type(Type) {}
53+
MapComponentInfoTy(void *Base, void *Begin, int64_t Size, int64_t Type,
54+
void *Name)
55+
: Base(Base), Begin(Begin), Size(Size), Type(Type), Name(Name) {}
5456
};
5557

5658
// This structure stores all components of a user-defined mapper. The number of
@@ -64,8 +66,10 @@ struct MapperComponentsTy {
6466
// The mapper function pointer type. It follows the signature below:
6567
// void .omp_mapper.<type_name>.<mapper_id>.(void *rt_mapper_handle,
6668
// void *base, void *begin,
67-
// size_t size, int64_t type);
68-
typedef void (*MapperFuncPtrTy)(void *, void *, void *, int64_t, int64_t);
69+
// size_t size, int64_t type,
70+
// void * name);
71+
typedef void (*MapperFuncPtrTy)(void *, void *, void *, int64_t, int64_t,
72+
void *);
6973

7074
// Function pointer type for target_data_* functions (targetDataBegin,
7175
// targetDataEnd and targetDataUpdate).

openmp/libomptarget/test/mapping/declare_mapper_api.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ struct MapComponentInfoTy {
1515
void *Begin;
1616
int64_t Size;
1717
int64_t Type;
18+
void *Name;
1819
MapComponentInfoTy() = default;
19-
MapComponentInfoTy(void *Base, void *Begin, int64_t Size, int64_t Type)
20-
: Base(Base), Begin(Begin), Size(Size), Type(Type) {}
20+
MapComponentInfoTy(void *Base, void *Begin, int64_t Size, int64_t Type, void *Name)
21+
: Base(Base), Begin(Begin), Size(Size), Type(Type), Name(Name) {}
2122
};
2223

2324
struct MapperComponentsTy {
@@ -30,7 +31,8 @@ extern "C" {
3031
#endif
3132
int64_t __tgt_mapper_num_components(void *rt_mapper_handle);
3233
void __tgt_push_mapper_component(void *rt_mapper_handle, void *base,
33-
void *begin, int64_t size, int64_t type);
34+
void *begin, int64_t size, int64_t type,
35+
void *name);
3436
#ifdef __cplusplus
3537
}
3638
#endif
@@ -40,8 +42,8 @@ int main(int argc, char *argv[]) {
4042
void *base, *begin;
4143
int64_t size, type;
4244
// Push 2 elements into MC.
43-
__tgt_push_mapper_component((void *)&MC, base, begin, size, type);
44-
__tgt_push_mapper_component((void *)&MC, base, begin, size, type);
45+
__tgt_push_mapper_component((void *)&MC, base, begin, size, type, nullptr);
46+
__tgt_push_mapper_component((void *)&MC, base, begin, size, type, nullptr);
4547
int64_t num = __tgt_mapper_num_components((void *)&MC);
4648
// CHECK: num=2
4749
printf("num=%" PRId64 "\n", num);

0 commit comments

Comments
 (0)