Skip to content

Commit 8391bb3

Browse files
authored
[OpenMP][NFC] Move more declarations out of private.h (#73823)
1 parent 04b1853 commit 8391bb3

File tree

5 files changed

+128
-116
lines changed

5 files changed

+128
-116
lines changed

openmp/libomptarget/include/OpenMP/InternalTypes.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,52 @@ typedef struct kmp_depend_info {
2929
} flags;
3030
} kmp_depend_info_t;
3131

32+
typedef struct kmp_tasking_flags { /* Total struct must be exactly 32 bits */
33+
/* Compiler flags */ /* Total compiler flags must be 16 bits */
34+
unsigned tiedness : 1; /* task is either tied (1) or untied (0) */
35+
unsigned final : 1; /* task is final(1) so execute immediately */
36+
unsigned merged_if0 : 1; /* no __kmpc_task_{begin/complete}_if0 calls in if0
37+
code path */
38+
unsigned destructors_thunk : 1; /* set if the compiler creates a thunk to
39+
invoke destructors from the runtime */
40+
unsigned proxy : 1; /* task is a proxy task (it will be executed outside the
41+
context of the RTL) */
42+
unsigned priority_specified : 1; /* set if the compiler provides priority
43+
setting for the task */
44+
unsigned detachable : 1; /* 1 == can detach */
45+
unsigned hidden_helper : 1; /* 1 == hidden helper task */
46+
unsigned reserved : 8; /* reserved for compiler use */
47+
48+
/* Library flags */ /* Total library flags must be 16 bits */
49+
unsigned tasktype : 1; /* task is either explicit(1) or implicit (0) */
50+
unsigned task_serial : 1; // task is executed immediately (1) or deferred (0)
51+
unsigned tasking_ser : 1; // all tasks in team are either executed immediately
52+
// (1) or may be deferred (0)
53+
unsigned team_serial : 1; // entire team is serial (1) [1 thread] or parallel
54+
// (0) [>= 2 threads]
55+
/* If either team_serial or tasking_ser is set, task team may be NULL */
56+
/* Task State Flags: */
57+
unsigned started : 1; /* 1==started, 0==not started */
58+
unsigned executing : 1; /* 1==executing, 0==not executing */
59+
unsigned complete : 1; /* 1==complete, 0==not complete */
60+
unsigned freed : 1; /* 1==freed, 0==allocated */
61+
unsigned native : 1; /* 1==gcc-compiled task, 0==intel */
62+
unsigned reserved31 : 7; /* reserved for library use */
63+
} kmp_tasking_flags_t;
64+
65+
struct kmp_task;
66+
typedef int32_t (*kmp_routine_entry_t)(int32_t, struct kmp_task *);
67+
typedef struct kmp_task {
68+
void *shareds;
69+
kmp_routine_entry_t routine;
70+
int32_t part_id;
71+
} kmp_task_t;
72+
73+
int32_t __kmpc_global_thread_num(void *) __attribute__((weak));
74+
bool __kmpc_omp_has_task_team(int32_t gtid) __attribute__((weak));
75+
void **__kmpc_omp_get_target_async_handle_ptr(int32_t gtid)
76+
__attribute__((weak));
77+
3278
} // extern "C"
3379

3480
#endif // OMPTARGET_OPENMP_INTERNAL_TYPES_H

openmp/libomptarget/include/OpenMP/omp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,20 @@
3030

3131
extern "C" {
3232

33+
/// Type declarations
34+
///{
35+
36+
typedef void *omp_depend_t;
37+
38+
///}
39+
40+
/// API declarations
41+
///{
42+
3343
int omp_get_default_device(void) __attribute__((weak));
3444

45+
///}
46+
3547
/// InteropAPI
3648
///
3749
///{

openmp/libomptarget/include/omptarget.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "Shared/Environment.h"
1818
#include "Shared/SourceInfo.h"
1919

20+
#include "OpenMP/InternalTypes.h"
21+
2022
#include <cstdint>
2123
#include <deque>
2224
#include <functional>
@@ -291,6 +293,72 @@ class AsyncInfoTy {
291293
bool isQueueEmpty() const;
292294
};
293295

296+
// Wrapper for task stored async info objects.
297+
class TaskAsyncInfoWrapperTy {
298+
// Invalid GTID as defined by libomp; keep in sync
299+
static constexpr int KMP_GTID_DNE = -2;
300+
301+
const int ExecThreadID = KMP_GTID_DNE;
302+
AsyncInfoTy LocalAsyncInfo;
303+
AsyncInfoTy *AsyncInfo = &LocalAsyncInfo;
304+
void **TaskAsyncInfoPtr = nullptr;
305+
306+
public:
307+
TaskAsyncInfoWrapperTy(DeviceTy &Device)
308+
: ExecThreadID(__kmpc_global_thread_num(NULL)), LocalAsyncInfo(Device) {
309+
// If we failed to acquired the current global thread id, we cannot
310+
// re-enqueue the current task. Thus we should use the local blocking async
311+
// info.
312+
if (ExecThreadID == KMP_GTID_DNE)
313+
return;
314+
315+
// Only tasks with an assigned task team can be re-enqueue and thus can
316+
// use the non-blocking synchronization scheme. Thus we should use the local
317+
// blocking async info, if we don´t have one.
318+
if (!__kmpc_omp_has_task_team(ExecThreadID))
319+
return;
320+
321+
// Acquire a pointer to the AsyncInfo stored inside the current task being
322+
// executed.
323+
TaskAsyncInfoPtr = __kmpc_omp_get_target_async_handle_ptr(ExecThreadID);
324+
325+
// If we cannot acquire such pointer, fallback to using the local blocking
326+
// async info.
327+
if (!TaskAsyncInfoPtr)
328+
return;
329+
330+
// When creating a new task async info, the task handle must always be
331+
// invalid. We must never overwrite any task async handle and there should
332+
// never be any valid handle store inside the task at this point.
333+
assert((*TaskAsyncInfoPtr) == nullptr &&
334+
"Task async handle is not empty when dispatching new device "
335+
"operations. The handle was not cleared properly or "
336+
"__tgt_target_nowait_query should have been called!");
337+
338+
// If no valid async handle is present, a new AsyncInfo will be allocated
339+
// and stored in the current task.
340+
AsyncInfo = new AsyncInfoTy(Device, AsyncInfoTy::SyncTy::NON_BLOCKING);
341+
*TaskAsyncInfoPtr = (void *)AsyncInfo;
342+
}
343+
344+
~TaskAsyncInfoWrapperTy() {
345+
// Local async info destruction is automatically handled by ~AsyncInfoTy.
346+
if (AsyncInfo == &LocalAsyncInfo)
347+
return;
348+
349+
// If the are device operations still pending, return immediately without
350+
// deallocating the handle.
351+
if (!AsyncInfo->isDone())
352+
return;
353+
354+
// Delete the handle and unset it from the OpenMP task data.
355+
delete AsyncInfo;
356+
*TaskAsyncInfoPtr = nullptr;
357+
}
358+
359+
operator AsyncInfoTy &() { return *AsyncInfo; }
360+
};
361+
294362
/// This struct is a record of non-contiguous information
295363
struct __tgt_target_non_contig {
296364
uint64_t Offset;

openmp/libomptarget/src/api.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "private.h"
1616
#include "rtl.h"
1717

18+
#include "OpenMP/omp.h"
1819
#include "Shared/Profile.h"
1920

2021
#include "llvm/ADT/SmallVector.h"

openmp/libomptarget/src/private.h

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -105,58 +105,8 @@ typedef int (*TargetDataFuncPtrTy)(ident_t *, DeviceTy &, int32_t, void **,
105105
#ifdef __cplusplus
106106
extern "C" {
107107
#endif
108-
/*!
109-
* The ident structure that describes a source location.
110-
* The struct is identical to the one in the kmp.h file.
111-
* We maintain the same data structure for compatibility.
112-
*/
113-
typedef void *omp_depend_t;
114-
struct kmp_task;
115-
typedef int32_t (*kmp_routine_entry_t)(int32_t, struct kmp_task *);
116-
typedef struct kmp_task {
117-
void *shareds;
118-
kmp_routine_entry_t routine;
119-
int32_t part_id;
120-
} kmp_task_t;
121-
122-
typedef struct kmp_tasking_flags { /* Total struct must be exactly 32 bits */
123-
/* Compiler flags */ /* Total compiler flags must be 16 bits */
124-
unsigned tiedness : 1; /* task is either tied (1) or untied (0) */
125-
unsigned final : 1; /* task is final(1) so execute immediately */
126-
unsigned merged_if0 : 1; /* no __kmpc_task_{begin/complete}_if0 calls in if0
127-
code path */
128-
unsigned destructors_thunk : 1; /* set if the compiler creates a thunk to
129-
invoke destructors from the runtime */
130-
unsigned proxy : 1; /* task is a proxy task (it will be executed outside the
131-
context of the RTL) */
132-
unsigned priority_specified : 1; /* set if the compiler provides priority
133-
setting for the task */
134-
unsigned detachable : 1; /* 1 == can detach */
135-
unsigned hidden_helper : 1; /* 1 == hidden helper task */
136-
unsigned reserved : 8; /* reserved for compiler use */
137-
138-
/* Library flags */ /* Total library flags must be 16 bits */
139-
unsigned tasktype : 1; /* task is either explicit(1) or implicit (0) */
140-
unsigned task_serial : 1; // task is executed immediately (1) or deferred (0)
141-
unsigned tasking_ser : 1; // all tasks in team are either executed immediately
142-
// (1) or may be deferred (0)
143-
unsigned team_serial : 1; // entire team is serial (1) [1 thread] or parallel
144-
// (0) [>= 2 threads]
145-
/* If either team_serial or tasking_ser is set, task team may be NULL */
146-
/* Task State Flags: */
147-
unsigned started : 1; /* 1==started, 0==not started */
148-
unsigned executing : 1; /* 1==executing, 0==not executing */
149-
unsigned complete : 1; /* 1==complete, 0==not complete */
150-
unsigned freed : 1; /* 1==freed, 0==allocated */
151-
unsigned native : 1; /* 1==gcc-compiled task, 0==intel */
152-
unsigned reserved31 : 7; /* reserved for library use */
153-
} kmp_tasking_flags_t;
154-
155-
int32_t __kmpc_global_thread_num(void *) __attribute__((weak));
108+
156109
int __kmpc_get_target_offload(void) __attribute__((weak));
157-
void **__kmpc_omp_get_target_async_handle_ptr(int32_t gtid)
158-
__attribute__((weak));
159-
bool __kmpc_omp_has_task_team(int32_t gtid) __attribute__((weak));
160110
kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, int32_t gtid, int32_t flags,
161111
size_t sizeof_kmp_task_t,
162112
size_t sizeof_shareds,
@@ -248,8 +198,6 @@ struct TargetMemsetArgsTy {
248198
// no constructors defined, because this is a PoD
249199
};
250200

251-
// Invalid GTID as defined by libomp; keep in sync
252-
#define KMP_GTID_DNE (-2)
253201
#ifdef __cplusplus
254202
}
255203
#endif
@@ -319,67 +267,4 @@ printKernelArguments(const ident_t *Loc, const int64_t DeviceId,
319267
}
320268
}
321269

322-
// Wrapper for task stored async info objects.
323-
class TaskAsyncInfoWrapperTy {
324-
const int ExecThreadID = KMP_GTID_DNE;
325-
AsyncInfoTy LocalAsyncInfo;
326-
AsyncInfoTy *AsyncInfo = &LocalAsyncInfo;
327-
void **TaskAsyncInfoPtr = nullptr;
328-
329-
public:
330-
TaskAsyncInfoWrapperTy(DeviceTy &Device)
331-
: ExecThreadID(__kmpc_global_thread_num(NULL)), LocalAsyncInfo(Device) {
332-
// If we failed to acquired the current global thread id, we cannot
333-
// re-enqueue the current task. Thus we should use the local blocking async
334-
// info.
335-
if (ExecThreadID == KMP_GTID_DNE)
336-
return;
337-
338-
// Only tasks with an assigned task team can be re-enqueue and thus can
339-
// use the non-blocking synchronization scheme. Thus we should use the local
340-
// blocking async info, if we don´t have one.
341-
if (!__kmpc_omp_has_task_team(ExecThreadID))
342-
return;
343-
344-
// Acquire a pointer to the AsyncInfo stored inside the current task being
345-
// executed.
346-
TaskAsyncInfoPtr = __kmpc_omp_get_target_async_handle_ptr(ExecThreadID);
347-
348-
// If we cannot acquire such pointer, fallback to using the local blocking
349-
// async info.
350-
if (!TaskAsyncInfoPtr)
351-
return;
352-
353-
// When creating a new task async info, the task handle must always be
354-
// invalid. We must never overwrite any task async handle and there should
355-
// never be any valid handle store inside the task at this point.
356-
assert((*TaskAsyncInfoPtr) == nullptr &&
357-
"Task async handle is not empty when dispatching new device "
358-
"operations. The handle was not cleared properly or "
359-
"__tgt_target_nowait_query should have been called!");
360-
361-
// If no valid async handle is present, a new AsyncInfo will be allocated
362-
// and stored in the current task.
363-
AsyncInfo = new AsyncInfoTy(Device, AsyncInfoTy::SyncTy::NON_BLOCKING);
364-
*TaskAsyncInfoPtr = (void *)AsyncInfo;
365-
}
366-
367-
~TaskAsyncInfoWrapperTy() {
368-
// Local async info destruction is automatically handled by ~AsyncInfoTy.
369-
if (AsyncInfo == &LocalAsyncInfo)
370-
return;
371-
372-
// If the are device operations still pending, return immediately without
373-
// deallocating the handle.
374-
if (!AsyncInfo->isDone())
375-
return;
376-
377-
// Delete the handle and unset it from the OpenMP task data.
378-
delete AsyncInfo;
379-
*TaskAsyncInfoPtr = nullptr;
380-
}
381-
382-
operator AsyncInfoTy &() { return *AsyncInfo; }
383-
};
384-
385270
#endif

0 commit comments

Comments
 (0)