@@ -35,12 +35,20 @@ FOREACH_OMPT_NOEMI_EVENT(defineOmptCallback)
3535FOREACH_OMPT_EMI_EVENT(defineOmptCallback)
3636#undef defineOmptCallback
3737
38- // / Thread local state for target region and associated metadata
39- thread_local llvm::omp::target::ompt::Interface OmptInterface ;
38+ // / Forward declaration
39+ class LibomptargetRtlFinalizer ;
4040
41- // / Define function pointers
42- ompt_get_task_data_t ompt_get_task_data_fn = nullptr ;
41+ // / Object that will maintain the RTL finalizer from the plugin
42+ LibomptargetRtlFinalizer *LibraryFinalizer = nullptr ;
43+
44+ thread_local Interface llvm::omp::target::ompt::RegionInterface;
45+
46+ bool llvm::omp::target::ompt::Initialized = false ;
47+
48+ ompt_get_callback_t llvm::omp::target::ompt::lookupCallbackByCode = nullptr ;
49+ ompt_function_lookup_t llvm::omp::target::ompt::lookupCallbackByName = nullptr ;
4350ompt_get_target_task_data_t ompt_get_target_task_data_fn = nullptr ;
51+ ompt_get_task_data_t ompt_get_task_data_fn = nullptr ;
4452
4553// / Unique correlation id
4654static std::atomic<uint64_t > IdCounter (1 );
@@ -51,14 +59,14 @@ static uint64_t createId() { return IdCounter.fetch_add(1); }
5159// / Create a new correlation id and update the operations id
5260static uint64_t createOpId () {
5361 uint64_t NewId = createId ();
54- OmptInterface .setHostOpId (NewId);
62+ RegionInterface .setHostOpId (NewId);
5563 return NewId;
5664}
5765
5866// / Create a new correlation id and update the target region id
5967static uint64_t createRegionId () {
6068 uint64_t NewId = createId ();
61- OmptInterface .setTargetDataValue (NewId);
69+ RegionInterface .setTargetDataValue (NewId);
6270 return NewId;
6371}
6472
@@ -68,18 +76,19 @@ void Interface::beginTargetDataAlloc(int64_t DeviceId, void *HstPtrBegin,
6876 if (ompt_callback_target_data_op_emi_fn) {
6977 // HostOpId will be set by the tool. Invoke the tool supplied data op EMI
7078 // callback
71- ompt_callback_target_data_op_emi_fn (ompt_scope_begin, TargetTaskData,
72- &TargetData, &TargetRegionOpId,
73- ompt_target_data_alloc, HstPtrBegin,
74- DeviceId , /* TgtPtrBegin */ nullptr ,
75- /* TgtDeviceNum */ 0 , Size, Code);
79+ ompt_callback_target_data_op_emi_fn (
80+ ompt_scope_begin, TargetTaskData, &TargetData, &TargetRegionOpId,
81+ ompt_target_data_alloc, HstPtrBegin,
82+ /* SrcDeviceNum */ omp_get_initial_device () , /* TgtPtrBegin */ nullptr ,
83+ /* TgtDeviceNum */ DeviceId , Size, Code);
7684 } else if (ompt_callback_target_data_op_fn) {
7785 // HostOpId is set by the runtime
7886 HostOpId = createOpId ();
7987 // Invoke the tool supplied data op callback
8088 ompt_callback_target_data_op_fn (
8189 TargetData.value , HostOpId, ompt_target_data_alloc, HstPtrBegin,
82- DeviceId, /* TgtPtrBegin */ nullptr , /* TgtDeviceNum */ 0 , Size, Code);
90+ /* SrcDeviceNum */ omp_get_initial_device (), /* TgtPtrBegin */ nullptr ,
91+ /* TgtDeviceNum */ DeviceId, Size, Code);
8392 }
8493}
8594
@@ -89,11 +98,11 @@ void Interface::endTargetDataAlloc(int64_t DeviceId, void *HstPtrBegin,
8998 if (ompt_callback_target_data_op_emi_fn) {
9099 // HostOpId will be set by the tool. Invoke the tool supplied data op EMI
91100 // callback
92- ompt_callback_target_data_op_emi_fn (ompt_scope_end, TargetTaskData,
93- &TargetData, &TargetRegionOpId,
94- ompt_target_data_alloc, HstPtrBegin,
95- DeviceId , /* TgtPtrBegin */ nullptr ,
96- /* TgtDeviceNum */ 0 , Size, Code);
101+ ompt_callback_target_data_op_emi_fn (
102+ ompt_scope_end, TargetTaskData, &TargetData, &TargetRegionOpId,
103+ ompt_target_data_alloc, HstPtrBegin,
104+ /* SrcDeviceNum */ omp_get_initial_device () , /* TgtPtrBegin */ nullptr ,
105+ /* TgtDeviceNum */ DeviceId , Size, Code);
97106 }
98107 endTargetDataOperation ();
99108}
@@ -108,14 +117,16 @@ void Interface::beginTargetDataSubmit(int64_t DeviceId, void *TgtPtrBegin,
108117 ompt_callback_target_data_op_emi_fn (
109118 ompt_scope_begin, TargetTaskData, &TargetData, &TargetRegionOpId,
110119 ompt_target_data_transfer_to_device, HstPtrBegin,
111- /* SrcDeviceNum */ 0 , TgtPtrBegin, DeviceId, Size, Code);
120+ /* SrcDeviceNum */ omp_get_initial_device (), TgtPtrBegin, DeviceId,
121+ Size, Code);
112122 } else if (ompt_callback_target_data_op_fn) {
113123 // HostOpId is set by the runtime
114124 HostOpId = createOpId ();
115125 // Invoke the tool supplied data op callback
116126 ompt_callback_target_data_op_fn (
117127 TargetData.value , HostOpId, ompt_target_data_transfer_to_device,
118- HstPtrBegin, /* SrcDeviceNum */ 0 , TgtPtrBegin, DeviceId, Size, Code);
128+ HstPtrBegin, /* SrcDeviceNum */ omp_get_initial_device (), TgtPtrBegin,
129+ DeviceId, Size, Code);
119130 }
120131}
121132
@@ -129,7 +140,8 @@ void Interface::endTargetDataSubmit(int64_t DeviceId, void *TgtPtrBegin,
129140 ompt_callback_target_data_op_emi_fn (
130141 ompt_scope_end, TargetTaskData, &TargetData, &TargetRegionOpId,
131142 ompt_target_data_transfer_to_device, HstPtrBegin,
132- /* SrcDeviceNum */ 0 , TgtPtrBegin, DeviceId, Size, Code);
143+ /* SrcDeviceNum */ omp_get_initial_device (), TgtPtrBegin, DeviceId,
144+ Size, Code);
133145 }
134146 endTargetDataOperation ();
135147}
@@ -143,15 +155,15 @@ void Interface::beginTargetDataDelete(int64_t DeviceId, void *TgtPtrBegin,
143155 ompt_callback_target_data_op_emi_fn (
144156 ompt_scope_begin, TargetTaskData, &TargetData, &TargetRegionOpId,
145157 ompt_target_data_delete, TgtPtrBegin, DeviceId,
146- /* TgtPtrBegin */ nullptr , /* TgtDeviceNum */ 0 , /* Bytes */ 0 , Code);
158+ /* TgtPtrBegin */ nullptr , /* TgtDeviceNum */ - 1 , /* Bytes */ 0 , Code);
147159 } else if (ompt_callback_target_data_op_fn) {
148160 // HostOpId is set by the runtime
149161 HostOpId = createOpId ();
150162 // Invoke the tool supplied data op callback
151163 ompt_callback_target_data_op_fn (TargetData.value , HostOpId,
152164 ompt_target_data_delete, TgtPtrBegin,
153165 DeviceId, /* TgtPtrBegin */ nullptr ,
154- /* TgtDeviceNum */ 0 , /* Bytes */ 0 , Code);
166+ /* TgtDeviceNum */ - 1 , /* Bytes */ 0 , Code);
155167 }
156168}
157169
@@ -164,7 +176,7 @@ void Interface::endTargetDataDelete(int64_t DeviceId, void *TgtPtrBegin,
164176 ompt_callback_target_data_op_emi_fn (
165177 ompt_scope_end, TargetTaskData, &TargetData, &TargetRegionOpId,
166178 ompt_target_data_delete, TgtPtrBegin, DeviceId,
167- /* TgtPtrBegin */ nullptr , /* TgtDeviceNum */ 0 , /* Bytes */ 0 , Code);
179+ /* TgtPtrBegin */ nullptr , /* TgtDeviceNum */ - 1 , /* Bytes */ 0 , Code);
168180 }
169181 endTargetDataOperation ();
170182}
@@ -176,19 +188,19 @@ void Interface::beginTargetDataRetrieve(int64_t DeviceId, void *HstPtrBegin,
176188 if (ompt_callback_target_data_op_emi_fn) {
177189 // HostOpId will be set by the tool. Invoke the tool supplied data op EMI
178190 // callback
179- ompt_callback_target_data_op_emi_fn (ompt_scope_begin, TargetTaskData,
180- &TargetData, &TargetRegionOpId,
181- ompt_target_data_transfer_from_device ,
182- TgtPtrBegin, DeviceId, HstPtrBegin,
183- /* TgtDeviceNum */ 0 , Size, Code);
191+ ompt_callback_target_data_op_emi_fn (
192+ ompt_scope_begin, TargetTaskData, &TargetData, &TargetRegionOpId,
193+ ompt_target_data_transfer_from_device, TgtPtrBegin, DeviceId ,
194+ HstPtrBegin,
195+ /* TgtDeviceNum */ omp_get_initial_device () , Size, Code);
184196 } else if (ompt_callback_target_data_op_fn) {
185197 // HostOpId is set by the runtime
186198 HostOpId = createOpId ();
187199 // Invoke the tool supplied data op callback
188- ompt_callback_target_data_op_fn (TargetData. value , HostOpId,
189- ompt_target_data_transfer_from_device,
190- TgtPtrBegin, DeviceId, HstPtrBegin,
191- /* TgtDeviceNum */ 0 , Size, Code);
200+ ompt_callback_target_data_op_fn (
201+ TargetData. value , HostOpId, ompt_target_data_transfer_from_device,
202+ TgtPtrBegin, DeviceId, HstPtrBegin,
203+ /* TgtDeviceNum */ omp_get_initial_device () , Size, Code);
192204 }
193205}
194206
@@ -199,11 +211,11 @@ void Interface::endTargetDataRetrieve(int64_t DeviceId, void *HstPtrBegin,
199211 if (ompt_callback_target_data_op_emi_fn) {
200212 // HostOpId will be set by the tool. Invoke the tool supplied data op EMI
201213 // callback
202- ompt_callback_target_data_op_emi_fn (ompt_scope_end, TargetTaskData,
203- &TargetData, &TargetRegionOpId,
204- ompt_target_data_transfer_from_device ,
205- TgtPtrBegin, DeviceId, HstPtrBegin,
206- /* TgtDeviceNum */ 0 , Size, Code);
214+ ompt_callback_target_data_op_emi_fn (
215+ ompt_scope_end, TargetTaskData, &TargetData, &TargetRegionOpId,
216+ ompt_target_data_transfer_from_device, TgtPtrBegin, DeviceId ,
217+ HstPtrBegin,
218+ /* TgtDeviceNum */ omp_get_initial_device () , Size, Code);
207219 }
208220 endTargetDataOperation ();
209221}
@@ -230,6 +242,7 @@ void Interface::endTargetSubmit(unsigned int numTeams) {
230242 numTeams);
231243 }
232244}
245+
233246void Interface::beginTargetDataEnter (int64_t DeviceId, void *Code) {
234247 beginTargetRegion ();
235248 if (ompt_callback_target_emi_fn) {
@@ -391,14 +404,6 @@ class LibomptargetRtlFinalizer {
391404 llvm::SmallVector<ompt_finalize_t > RtlFinalizationFunctions;
392405};
393406
394- // / Object that will maintain the RTL finalizer from the plugin
395- LibomptargetRtlFinalizer *LibraryFinalizer = nullptr ;
396-
397- bool llvm::omp::target::ompt::Initialized = false ;
398-
399- ompt_get_callback_t llvm::omp::target::ompt::lookupCallbackByCode = nullptr ;
400- ompt_function_lookup_t llvm::omp::target::ompt::lookupCallbackByName = nullptr ;
401-
402407int llvm::omp::target::ompt::initializeLibrary (ompt_function_lookup_t lookup,
403408 int initial_device_num,
404409 ompt_data_t *tool_data) {
@@ -418,6 +423,9 @@ int llvm::omp::target::ompt::initializeLibrary(ompt_function_lookup_t lookup,
418423
419424 assert (lookupCallbackByCode && " lookupCallbackByCode should be non-null" );
420425 assert (lookupCallbackByName && " lookupCallbackByName should be non-null" );
426+ assert (ompt_get_task_data_fn && " ompt_get_task_data_fn should be non-null" );
427+ assert (ompt_get_target_task_data_fn &&
428+ " ompt_get_target_task_data_fn should be non-null" );
421429 assert (LibraryFinalizer == nullptr &&
422430 " LibraryFinalizer should not be initialized yet" );
423431
@@ -434,6 +442,7 @@ void llvm::omp::target::ompt::finalizeLibrary(ompt_data_t *data) {
434442 // with this library
435443 LibraryFinalizer->finalize ();
436444 delete LibraryFinalizer;
445+ Initialized = false ;
437446}
438447
439448void llvm::omp::target::ompt::connectLibrary () {
0 commit comments