1 // RUN: %libomp-compile-and-run
2 // The runtime currently does not get dependency information from GCC.
3 // UNSUPPORTED: gcc
4 
5 // Very flaky on openmp-clang-x86_64-linux-debian.
6 // https://bugs.llvm.org/show_bug.cgi?id=45397
7 // UNSUPPORTED: linux
8 
9 #include <stdio.h>
10 #include <omp.h>
11 #include <pthread.h>
12 #include "omp_my_sleep.h"
13 
14 /*
15  With task dependencies one can generate proxy tasks from an explicit task
16  being executed by a serial task team. The OpenMP runtime library didn't
17  expect that and tries to free the explicit task that is the parent of the
18  proxy task still working in background. It therefore has incomplete children
19  which triggers a debugging assertion.
20 */
21 
22 // Compiler-generated code (emulation)
23 typedef long kmp_intptr_t;
24 typedef int kmp_int32;
25 
26 typedef char bool;
27 
28 typedef struct ident {
29     kmp_int32 reserved_1;   /**<  might be used in Fortran; see above  */
30     kmp_int32 flags;        /**<  also f.flags; KMP_IDENT_xxx flags; KMP_IDENT_KMPC identifies this union member  */
31     kmp_int32 reserved_2;   /**<  not really used in Fortran any more; see above */
32 #if USE_ITT_BUILD
33                             /*  but currently used for storing region-specific ITT */
34                             /*  contextual information. */
35 #endif /* USE_ITT_BUILD */
36     kmp_int32 reserved_3;   /**< source[4] in Fortran, do not use for C++  */
37     char const *psource;    /**< String describing the source location.
38                             The string is composed of semi-colon separated fields which describe the source file,
39                             the function and a pair of line numbers that delimit the construct.
40                              */
41 } ident_t;
42 
43 typedef struct kmp_depend_info {
44      kmp_intptr_t               base_addr;
45      size_t                     len;
46      struct {
47          bool                   in:1;
48          bool                   out:1;
49      } flags;
50 } kmp_depend_info_t;
51 
52 struct kmp_task;
53 typedef kmp_int32 (* kmp_routine_entry_t)( kmp_int32, struct kmp_task * );
54 
55 typedef struct kmp_task {                   /* GEH: Shouldn't this be aligned somehow? */
56     void *              shareds;            /**< pointer to block of pointers to shared vars   */
57     kmp_routine_entry_t routine;            /**< pointer to routine to call for executing task */
58     kmp_int32           part_id;            /**< part id for the task                          */
59 } kmp_task_t;
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 kmp_int32  __kmpc_global_thread_num  ( ident_t * );
65 kmp_task_t*
66 __kmpc_omp_task_alloc( ident_t *loc_ref, kmp_int32 gtid, kmp_int32 flags,
67                        size_t sizeof_kmp_task_t, size_t sizeof_shareds,
68                        kmp_routine_entry_t task_entry );
69 void __kmpc_proxy_task_completed_ooo ( kmp_task_t *ptask );
70 kmp_int32 __kmpc_omp_task_with_deps ( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * new_task,
71                                       kmp_int32 ndeps, kmp_depend_info_t *dep_list,
72                                       kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list );
73 kmp_int32
74 __kmpc_omp_task( ident_t *loc_ref, kmp_int32 gtid, kmp_task_t * new_task );
75 #ifdef __cplusplus
76 }
77 #endif
78 
target(void * task)79 void *target(void *task)
80 {
81     my_sleep( 0.1 );
82     __kmpc_proxy_task_completed_ooo((kmp_task_t*) task);
83     return NULL;
84 }
85 
86 pthread_t target_thread;
87 
88 // User's code
task_entry(kmp_int32 gtid,kmp_task_t * task)89 int task_entry(kmp_int32 gtid, kmp_task_t *task)
90 {
91     pthread_create(&target_thread, NULL, &target, task);
92     return 0;
93 }
94 
main()95 int main()
96 {
97     int dep;
98 
99 #pragma omp taskgroup
100 {
101 /*
102  *  Corresponds to:
103     #pragma omp target nowait depend(out: dep)
104     {
105         my_sleep( 0.1 );
106     }
107 */
108     kmp_depend_info_t dep_info;
109     dep_info.base_addr = (long) &dep;
110     dep_info.len = sizeof(int);
111     // out = inout per spec and runtime expects this
112     dep_info.flags.in = 1;
113     dep_info.flags.out = 1;
114 
115     kmp_int32 gtid = __kmpc_global_thread_num(NULL);
116     kmp_task_t *proxy_task = __kmpc_omp_task_alloc(NULL,gtid,17,sizeof(kmp_task_t),0,&task_entry);
117     __kmpc_omp_task_with_deps(NULL,gtid,proxy_task,1,&dep_info,0,NULL);
118 
119     #pragma omp task depend(in: dep)
120     {
121 /*
122  *      Corresponds to:
123         #pragma omp target nowait
124         {
125             my_sleep( 0.1 );
126         }
127 */
128         kmp_task_t *nested_proxy_task = __kmpc_omp_task_alloc(NULL,gtid,17,sizeof(kmp_task_t),0,&task_entry);
129         __kmpc_omp_task(NULL,gtid,nested_proxy_task);
130     }
131 }
132 
133     // only check that it didn't crash
134     return 0;
135 }
136