-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcilk2c_inlined.c
408 lines (354 loc) · 15.1 KB
/
cilk2c_inlined.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// =============================================================================
// This file contains the compiler-runtime ABI. This file is compiled to LLVM
// bitcode, which the compiler then includes and inlines when it compiles a Cilk
// program.
// =============================================================================
#include <stdatomic.h>
#include <stdio.h>
#include <unwind.h>
#include "cilk-internal.h"
#include "cilk2c.h"
#include "debug.h"
#include "fiber.h"
#include "fiber-header.h"
#include "frame.h"
#include "global.h"
#include "init.h"
#include "local-reducer-api.h"
#include "scheduler.h"
#include "pedigree_ext.c"
#include "worker.h"
// This variable encodes the alignment of a __cilkrts_stack_frame, both in its
// value and in its own alignment. Because LLVM IR does not associate
// alignments with types, this variable communicates the desired alignment to
// the compiler instead.
_Alignas(__cilkrts_stack_frame)
size_t __cilkrts_stack_frame_align = __alignof__(__cilkrts_stack_frame);
__attribute__((always_inline)) unsigned __cilkrts_get_nworkers(void) {
return __cilkrts_nproc;
}
// Internal method to get the Cilk worker ID. Intended for debugging purposes.
//
// TODO: Figure out how we want to support worker-local storage.
__attribute__((always_inline))
unsigned __cilkrts_get_worker_number(void) {
__cilkrts_worker *w = __cilkrts_get_tls_worker();
if (w)
return w->self;
// If the worker structure is not yet initialized, pretend we're worker 0.
return 0;
}
void *__cilkrts_reducer_lookup(void *key, size_t size,
void *identity_ptr, void *reduce_ptr) {
// If we're outside a cilkified region, then the key is the view.
if (__cilkrts_need_to_cilkify)
return key;
struct local_hyper_table *table = get_hyper_table();
struct bucket *b = find_hyperobject(table, (uintptr_t)key);
if (__builtin_expect(!!b, true)) {
// Return the existing view.
return b->value.view;
}
return __cilkrts_insert_new_view(table, (uintptr_t)key, size,
(__cilk_identity_fn)identity_ptr,
(__cilk_reduce_fn)reduce_ptr);
}
// Begin a Cilkified region. The routine runs on a Cilkifying thread to
// transfer the execution of this function to the workers in global_state g.
// This routine must be inlined for correctness.
static inline __attribute__((always_inline)) void
cilkify(__cilkrts_stack_frame *sf) {
// After inlining, the setjmp saves the processor state, including the frame
// pointer, of the Cilk function.
if (__builtin_setjmp(sf->ctx) == 0) {
sysdep_save_fp_ctrl_state(sf);
__cilkrts_internal_invoke_cilkified_root(sf);
} else {
sanitizer_finish_switch_fiber();
}
}
// End a Cilkified region. This routine runs on one worker in global_state g
// who finished executing the Cilkified region, in order to transfer control
// back to the original thread that began the Cilkified region. This routine
// must be inlined for correctness.
static inline __attribute__((always_inline)) void
uncilkify(global_state *g, __cilkrts_stack_frame *sf) {
// The setjmp will save the processor state at the end of the Cilkified
// region. The Cilkifying thread will longjmp to this point.
if (__builtin_setjmp(sf->ctx) == 0) {
sysdep_save_fp_ctrl_state(sf);
// Finish this Cilkified region, and transfer control back to the
// original thread that performed cilkify.
__cilkrts_internal_exit_cilkified_root(g, sf);
} else {
sanitizer_finish_switch_fiber();
}
}
// Enter a new Cilk function, i.e., a function that contains a cilk_spawn. This
// function must be inlined for correctness.
__attribute__((always_inline)) void
__cilkrts_enter_frame(__cilkrts_stack_frame *sf) {
sf->flags = 0;
if (__cilkrts_need_to_cilkify) {
cilkify(sf);
}
cilkrts_alert(CFRAME, "__cilkrts_enter_frame %p", (void *)sf);
sf->magic = frame_magic;
struct cilk_fiber *fh = __cilkrts_current_fh;
sf->fh = fh;
sf->call_parent = fh->current_stack_frame;
fh->current_stack_frame = sf;
// WHEN_CILK_DEBUG(sf->magic = CILK_STACKFRAME_MAGIC);
}
// Enter a spawn helper, i.e., a fucntion containing code that was cilk_spawn'd.
// This function initializes worker and stack_frame structures. Because this
// routine will always be executed by a Cilk worker, it is optimized compared to
// its counterpart, __cilkrts_enter_frame.
__attribute__((always_inline)) void
__cilkrts_enter_frame_helper(__cilkrts_stack_frame *sf,
__cilkrts_stack_frame *parent, bool spawner) {
cilkrts_alert(CFRAME, "__cilkrts_enter_frame_helper %p", (void *)sf);
sf->flags = 0;
sf->magic = frame_magic;
struct cilk_fiber *fh = parent->fh;
sf->fh = fh;
if (spawner) {
sf->call_parent = parent;
fh->current_stack_frame = sf;
}
}
__attribute__((always_inline)) int
__cilk_prepare_spawn(__cilkrts_stack_frame *sf) {
sysdep_save_fp_ctrl_state(sf);
int res = __builtin_setjmp(sf->ctx);
if (res != 0) {
sanitizer_finish_switch_fiber();
}
return res;
}
// Detach the given Cilk stack frame, allowing other Cilk workers to steal the
// parent frame.
__attribute__((always_inline)) void
__cilkrts_detach(__cilkrts_stack_frame *sf, __cilkrts_stack_frame *parent) {
__cilkrts_worker *w = get_worker_from_stack(sf);
cilkrts_alert(CFRAME, "__cilkrts_detach %p", (void *)sf);
CILK_ASSERT(CHECK_CILK_FRAME_MAGIC(w->g, sf));
if (USE_EXTENSION) {
__cilkrts_extend_spawn(w, &parent->extension, &w->extension);
}
sf->flags |= CILK_FRAME_DETACHED;
struct __cilkrts_stack_frame **tail =
atomic_load_explicit(&w->tail, memory_order_relaxed);
CILK_ASSERT((tail + 1) < w->ltq_limit);
// store parent at *tail, and then increment tail
*tail++ = parent;
/* Release ordering ensures the two preceding stores are visible. */
atomic_store_explicit(&w->tail, tail, memory_order_release);
}
__attribute__((always_inline)) void __cilk_sync(__cilkrts_stack_frame *sf) {
if (sf->flags & CILK_FRAME_UNSYNCHED || USE_EXTENSION) {
if (sf->flags & CILK_FRAME_UNSYNCHED) {
if (__builtin_setjmp(sf->ctx) == 0) {
sysdep_save_fp_ctrl_state(sf);
__cilkrts_sync(sf);
} else {
sanitizer_finish_switch_fiber();
if (sf->flags & CILK_FRAME_EXCEPTION_PENDING) {
__cilkrts_check_exception_raise(sf);
}
}
}
if (USE_EXTENSION) {
__cilkrts_worker *w = get_worker_from_stack(sf);
__cilkrts_extend_sync(&w->extension);
}
}
}
__attribute__((always_inline)) void
__cilk_sync_nothrow(__cilkrts_stack_frame *sf) {
if (sf->flags & CILK_FRAME_UNSYNCHED || USE_EXTENSION) {
if (sf->flags & CILK_FRAME_UNSYNCHED) {
if (__builtin_setjmp(sf->ctx) == 0) {
sysdep_save_fp_ctrl_state(sf);
__cilkrts_sync(sf);
} else {
sanitizer_finish_switch_fiber();
}
}
if (USE_EXTENSION) {
__cilkrts_worker *w = get_worker_from_stack(sf);
__cilkrts_extend_sync(&w->extension);
}
}
}
__attribute__((always_inline)) void
__cilkrts_leave_frame(__cilkrts_stack_frame *sf) {
// TODO: Move load of worker pointer out of fast path.
__cilkrts_worker *w = get_worker_from_stack(sf);
cilkrts_alert(CFRAME, "__cilkrts_leave_frame %p", (void *)sf);
CILK_ASSERT(CHECK_CILK_FRAME_MAGIC(w->g, sf));
// WHEN_CILK_DEBUG(sf->magic = ~CILK_STACKFRAME_MAGIC);
__cilkrts_stack_frame *parent = sf->call_parent;
// Pop this frame off the cactus stack. This logic used to be in
// __cilkrts_pop_frame, but has been manually inlined to avoid reloading the
// worker unnecessarily.
sf->fh->current_stack_frame = parent;
sf->call_parent = NULL;
// Check if sf is the final stack frame, and if so, terminate the Cilkified
// region.
uint32_t flags = sf->flags;
if (flags & CILK_FRAME_LAST) {
uncilkify(w->g, sf);
flags = sf->flags;
}
if (flags == 0) {
return;
}
CILK_ASSERT(!(flags & CILK_FRAME_DETACHED));
// A detached frame would never need to call Cilk_set_return, which performs
// the return protocol of a full frame back to its parent when the full
// frame is called (not spawned). A spawned full frame returning is done
// via a different protocol, which is triggered in Cilk_exception_handler.
if (flags & CILK_FRAME_STOLEN) { // if this frame has a full frame
cilkrts_alert(RETURN,
"__cilkrts_leave_frame parent is call_parent!");
// leaving a full frame; need to get the full frame of its call
// parent back onto the deque
Cilk_set_return(w);
CILK_ASSERT(CHECK_CILK_FRAME_MAGIC(w->g, sf));
}
}
__attribute__((always_inline)) void
__cilkrts_leave_frame_helper(__cilkrts_stack_frame *sf,
__cilkrts_stack_frame *parent, bool spawner) {
__cilkrts_worker *w = get_worker_from_stack(sf);
cilkrts_alert(CFRAME, "__cilkrts_leave_frame_helper %p", (void *)sf);
CILK_ASSERT(CHECK_CILK_FRAME_MAGIC(w->g, sf));
// WHEN_CILK_DEBUG(sf->magic = ~CILK_STACKFRAME_MAGIC);
// Pop this frame off the cactus stack. This logic used to be in
// __cilkrts_pop_frame, but has been manually inlined to avoid reloading the
// worker unnecessarily.
if (spawner)
sf->fh->current_stack_frame = parent;
if (USE_EXTENSION) {
__cilkrts_extend_return_from_spawn(w, &w->extension);
w->extension = parent->extension;
}
sf->call_parent = NULL;
CILK_ASSERT(sf->flags & CILK_FRAME_DETACHED);
__cilkrts_stack_frame **tail =
atomic_load_explicit(&w->tail, memory_order_relaxed);
--tail;
/* The store of tail must precede the load of exc in global order. See
comment in do_dekker_on. */
atomic_store_explicit(&w->tail, tail, memory_order_seq_cst);
__cilkrts_stack_frame **exc =
atomic_load_explicit(&w->exc, memory_order_seq_cst);
/* Currently no other modifications of flags are atomic so this one isn't
either. If the thief wins it may run in parallel with the clear of
DETACHED. Does it modify flags too? */
sf->flags &= ~CILK_FRAME_DETACHED;
if (__builtin_expect(exc > tail, false)) {
Cilk_exception_handler(w, NULL);
// If Cilk_exception_handler returns this thread won the race and can
// return to the parent function.
}
}
__attribute__((always_inline)) void
__cilk_parent_epilogue(__cilkrts_stack_frame *sf) {
__cilkrts_leave_frame(sf);
}
__attribute__((always_inline)) void
__cilk_helper_epilogue(__cilkrts_stack_frame *sf, __cilkrts_stack_frame *parent,
bool spawner) {
__cilkrts_leave_frame_helper(sf, parent, spawner);
}
__attribute__((always_inline))
void __cilkrts_enter_landingpad(__cilkrts_stack_frame *sf, int32_t sel) {
if (__cilkrts_need_to_cilkify)
return;
sf->fh->current_stack_frame = sf;
// Don't do anything special during cleanups.
if (sel == 0)
return;
if (0 == __builtin_setjmp(sf->ctx))
__cilkrts_cleanup_fiber(sf, sel);
}
__attribute__((always_inline)) void
__cilkrts_pause_frame(__cilkrts_stack_frame *sf, __cilkrts_stack_frame *parent,
char *exn, bool spawner) {
if (0 == __builtin_setjmp(sf->ctx))
__cilkrts_cleanup_fiber(sf, 1);
__cilkrts_worker *w = get_worker_from_stack(sf);
cilkrts_alert(CFRAME, "__cilkrts_pause_frame %p", (void *)sf);
CILK_ASSERT(CHECK_CILK_FRAME_MAGIC(w->g, sf));
// Pop this frame off the cactus stack. This logic used to be in
// __cilkrts_pop_frame, but has been manually inlined to avoid reloading the
// worker unnecessarily.
if (spawner)
sf->fh->current_stack_frame = parent;
sf->call_parent = NULL;
// A __cilkrts_pause_frame may be reached before the spawn-helper frame has
// detached. In that case, THE is not required.
if (sf->flags & CILK_FRAME_DETACHED) {
if (USE_EXTENSION) {
__cilkrts_extend_return_from_spawn(w, &w->extension);
w->extension = parent->extension;
}
__cilkrts_stack_frame **tail =
atomic_load_explicit(&w->tail, memory_order_relaxed);
--tail;
/* The store of tail must precede the load of exc in global order.
See comment in do_dekker_on. */
atomic_store_explicit(&w->tail, tail, memory_order_seq_cst);
__cilkrts_stack_frame **exc =
atomic_load_explicit(&w->exc, memory_order_seq_cst);
/* Currently no other modifications of flags are atomic so this
one isn't either. If the thief wins it may run in parallel
with the clear of DETACHED. Does it modify flags too? */
sf->flags &= ~CILK_FRAME_DETACHED;
if (__builtin_expect(exc > tail, false)) {
Cilk_exception_handler(w, exn);
// If Cilk_exception_handler returns this thread won
// the race and can return to the parent function.
}
}
}
__attribute__((always_inline)) void
__cilk_helper_epilogue_exn(__cilkrts_stack_frame *sf,
__cilkrts_stack_frame *parent, char *exn,
bool spawner) {
__cilkrts_pause_frame(sf, parent, exn, spawner);
}
// Internal helper function to ensure the __cilkrts_stack_frame type is present
// in the bitcode file. Does not end up in compiled Cilk code nor the OpenCilk
// runtime library.
CHEETAH_INTERNAL __cilkrts_stack_frame
__internal_preserve_stack_frame_type_helper(void) {
__cilkrts_stack_frame sf;
__cilkrts_enter_frame(&sf);
return sf;
}
/// Computes a grainsize for a cilk_for loop, using the following equation:
///
/// grainsize = min(2048, ceil(n / (8 * nworkers)))
#define __cilkrts_grainsize_fn_impl(NAME, INT_T) \
__attribute__((always_inline)) INT_T NAME(INT_T n) { \
INT_T small_loop_grainsize = n / (8 * __cilkrts_nproc); \
if (small_loop_grainsize <= 1) \
return 1; \
INT_T large_loop_grainsize = 2048; \
return large_loop_grainsize < small_loop_grainsize \
? large_loop_grainsize \
: small_loop_grainsize; \
}
#define __cilkrts_grainsize_fn(SZ) \
__cilkrts_grainsize_fn_impl(__cilkrts_cilk_for_grainsize_##SZ, uint##SZ##_t)
__attribute__((always_inline)) uint8_t
__cilkrts_cilk_for_grainsize_8(uint8_t n) {
uint8_t small_loop_grainsize = n / (8 * __cilkrts_nproc);
if (small_loop_grainsize <= 1)
return 1;
return small_loop_grainsize;
}
__cilkrts_grainsize_fn(16) __cilkrts_grainsize_fn(32) __cilkrts_grainsize_fn(64)