action: add get_return_value()
[cdsspec-compiler.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <vector>
5
6 #include "model.h"
7 #include "action.h"
8 #include "clockvector.h"
9 #include "common.h"
10 #include "threads-model.h"
11 #include "nodestack.h"
12
13 #define ACTION_INITIAL_CLOCK 0
14
15 /**
16  * @brief Construct a new ModelAction
17  *
18  * @param type The type of action
19  * @param order The memory order of this action. A "don't care" for non-ATOMIC
20  * actions (e.g., THREAD_* or MODEL_* actions).
21  * @param loc The location that this action acts upon
22  * @param value (optional) A value associated with the action (e.g., the value
23  * read or written). Defaults to a given macro constant, for debugging purposes.
24  * @param thread (optional) The Thread in which this action occurred. If NULL
25  * (default), then a Thread is assigned according to the scheduler.
26  */
27 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
28                 uint64_t value, Thread *thread) :
29         type(type),
30         order(order),
31         location(loc),
32         value(value),
33         reads_from(NULL),
34         reads_from_promise(NULL),
35         last_fence_release(NULL),
36         node(NULL),
37         seq_number(ACTION_INITIAL_CLOCK),
38         cv(NULL),
39         sleep_flag(false)
40 {
41         /* References to NULL atomic variables can end up here */
42         ASSERT(loc || type == ATOMIC_FENCE || type == MODEL_FIXUP_RELSEQ);
43
44         Thread *t = thread ? thread : thread_current();
45         this->tid = t->get_id();
46 }
47
48 /** @brief ModelAction destructor */
49 ModelAction::~ModelAction()
50 {
51         /**
52          * We can't free the clock vector:
53          * Clock vectors are snapshotting state. When we delete model actions,
54          * they are at the end of the node list and have invalid old clock
55          * vectors which have already been rolled back to an unallocated state.
56          */
57
58         /*
59          if (cv)
60                 delete cv; */
61 }
62
63 void ModelAction::copy_from_new(ModelAction *newaction)
64 {
65         seq_number = newaction->seq_number;
66 }
67
68 void ModelAction::set_seq_number(modelclock_t num)
69 {
70         /* ATOMIC_UNINIT actions should never have non-zero clock */
71         ASSERT(!is_uninitialized());
72         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
73         seq_number = num;
74 }
75
76 bool ModelAction::is_thread_start() const
77 {
78         return type == THREAD_START;
79 }
80
81 bool ModelAction::is_relseq_fixup() const
82 {
83         return type == MODEL_FIXUP_RELSEQ;
84 }
85
86 bool ModelAction::is_mutex_op() const
87 {
88         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
89 }
90
91 bool ModelAction::is_lock() const
92 {
93         return type == ATOMIC_LOCK;
94 }
95
96 bool ModelAction::is_wait() const {
97         return type == ATOMIC_WAIT;
98 }
99
100 bool ModelAction::is_notify() const {
101         return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
102 }
103
104 bool ModelAction::is_notify_one() const {
105         return type == ATOMIC_NOTIFY_ONE;
106 }
107
108 bool ModelAction::is_unlock() const
109 {
110         return type == ATOMIC_UNLOCK;
111 }
112
113 bool ModelAction::is_trylock() const
114 {
115         return type == ATOMIC_TRYLOCK;
116 }
117
118 bool ModelAction::is_success_lock() const
119 {
120         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
121 }
122
123 bool ModelAction::is_failed_trylock() const
124 {
125         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
126 }
127
128 /** @return True if this operation is performed on a C/C++ atomic variable */
129 bool ModelAction::is_atomic_var() const
130 {
131         return is_read() || could_be_write();
132 }
133
134 bool ModelAction::is_uninitialized() const
135 {
136         return type == ATOMIC_UNINIT;
137 }
138
139 bool ModelAction::is_read() const
140 {
141         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
142 }
143
144 bool ModelAction::is_write() const
145 {
146         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
147 }
148
149 bool ModelAction::could_be_write() const
150 {
151         return is_write() || is_rmwr();
152 }
153
154 bool ModelAction::is_rmwr() const
155 {
156         return type == ATOMIC_RMWR;
157 }
158
159 bool ModelAction::is_rmw() const
160 {
161         return type == ATOMIC_RMW;
162 }
163
164 bool ModelAction::is_rmwc() const
165 {
166         return type == ATOMIC_RMWC;
167 }
168
169 bool ModelAction::is_fence() const
170 {
171         return type == ATOMIC_FENCE;
172 }
173
174 bool ModelAction::is_initialization() const
175 {
176         return type == ATOMIC_INIT;
177 }
178
179 bool ModelAction::is_relaxed() const
180 {
181         return order == std::memory_order_relaxed;
182 }
183
184 bool ModelAction::is_acquire() const
185 {
186         switch (order) {
187         case std::memory_order_acquire:
188         case std::memory_order_acq_rel:
189         case std::memory_order_seq_cst:
190                 return true;
191         default:
192                 return false;
193         }
194 }
195
196 bool ModelAction::is_release() const
197 {
198         switch (order) {
199         case std::memory_order_release:
200         case std::memory_order_acq_rel:
201         case std::memory_order_seq_cst:
202                 return true;
203         default:
204                 return false;
205         }
206 }
207
208 bool ModelAction::is_seqcst() const
209 {
210         return order == std::memory_order_seq_cst;
211 }
212
213 bool ModelAction::same_var(const ModelAction *act) const
214 {
215         if (act->is_wait() || is_wait()) {
216                 if (act->is_wait() && is_wait()) {
217                         if (((void *)value) == ((void *)act->value))
218                                 return true;
219                 } else if (is_wait()) {
220                         if (((void *)value) == act->location)
221                                 return true;
222                 } else if (act->is_wait()) {
223                         if (location == ((void *)act->value))
224                                 return true;
225                 }
226         }
227
228         return location == act->location;
229 }
230
231 bool ModelAction::same_thread(const ModelAction *act) const
232 {
233         return tid == act->tid;
234 }
235
236 void ModelAction::copy_typeandorder(ModelAction * act)
237 {
238         this->type = act->type;
239         this->order = act->order;
240 }
241
242 /**
243  * Get the Thread which is the operand of this action. This is only valid for
244  * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
245  * that this provides a central place for determining the conventions of Thread
246  * storage in ModelAction, where we generally aren't very type-safe (e.g., we
247  * store object references in a (void *) address.
248  *
249  * For THREAD_CREATE, this yields the Thread which is created.
250  * For THREAD_JOIN, this yields the Thread we are joining with.
251  *
252  * @return The Thread which this action acts on, if exists; otherwise NULL
253  */
254 Thread * ModelAction::get_thread_operand() const
255 {
256         if (type == THREAD_CREATE) {
257                 /* THREAD_CREATE stores its (Thread *) in a thrd_t::priv */
258                 thrd_t *thrd = (thrd_t *)get_location();
259                 return thrd->priv;
260         } else if (type == THREAD_JOIN)
261                 /* THREAD_JOIN uses (Thread *) for location */
262                 return (Thread *)get_location();
263         else
264                 return NULL;
265 }
266
267 /** This method changes an existing read part of an RMW action into either:
268  *  (1) a full RMW action in case of the completed write or
269  *  (2) a READ action in case a failed action.
270  * @todo  If the memory_order changes, we may potentially need to update our
271  * clock vector.
272  */
273 void ModelAction::process_rmw(ModelAction *act)
274 {
275         this->order = act->order;
276         if (act->is_rmwc())
277                 this->type = ATOMIC_READ;
278         else if (act->is_rmw()) {
279                 this->type = ATOMIC_RMW;
280                 this->value = act->value;
281         }
282 }
283
284 /** The is_synchronizing method should only explore interleavings if:
285  *  (1) the operations are seq_cst and don't commute or
286  *  (2) the reordering may establish or break a synchronization relation.
287  *  Other memory operations will be dealt with by using the reads_from
288  *  relation.
289  *
290  *  @param act is the action to consider exploring a reordering.
291  *  @return tells whether we have to explore a reordering.
292  */
293 bool ModelAction::could_synchronize_with(const ModelAction *act) const
294 {
295         // Same thread can't be reordered
296         if (same_thread(act))
297                 return false;
298
299         // Different locations commute
300         if (!same_var(act))
301                 return false;
302
303         // Explore interleavings of seqcst writes/fences to guarantee total
304         // order of seq_cst operations that don't commute
305         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
306                 return true;
307
308         // Explore synchronizing read/write pairs
309         if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
310                 return true;
311
312         // lock just released...we can grab lock
313         if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
314                 return true;
315
316         // lock just acquired...we can fail to grab lock
317         if (is_trylock() && act->is_success_lock())
318                 return true;
319
320         // other thread stalling on lock...we can release lock
321         if (is_unlock() && (act->is_trylock() || act->is_lock()))
322                 return true;
323
324         if (is_trylock() && (act->is_unlock() || act->is_wait()))
325                 return true;
326
327         if (is_notify() && act->is_wait())
328                 return true;
329
330         if (is_wait() && act->is_notify())
331                 return true;
332
333         // Otherwise handle by reads_from relation
334         return false;
335 }
336
337 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
338 {
339         // Must be different threads to reorder
340         if (same_thread(act))
341                 return false;
342
343         // Try to reorder a lock past a successful lock
344         if (act->is_success_lock())
345                 return true;
346
347         // Try to push a successful trylock past an unlock
348         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
349                 return true;
350
351         // Try to push a successful trylock past a wait
352         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
353                 return true;
354
355         return false;
356 }
357
358 /**
359  * Create a new clock vector for this action. Note that this function allows a
360  * user to clobber (and leak) a ModelAction's existing clock vector. A user
361  * should ensure that the vector has already either been rolled back
362  * (effectively "freed") or freed.
363  *
364  * @param parent A ModelAction from which to inherit a ClockVector
365  */
366 void ModelAction::create_cv(const ModelAction *parent)
367 {
368         if (parent)
369                 cv = new ClockVector(parent->cv, this);
370         else
371                 cv = new ClockVector(NULL, this);
372 }
373
374 void ModelAction::set_try_lock(bool obtainedlock) {
375         if (obtainedlock)
376                 value = VALUE_TRYSUCCESS;
377         else
378                 value = VALUE_TRYFAILED;
379 }
380
381 /**
382  * @brief Get the value read by this load
383  *
384  * We differentiate this function from ModelAction::get_write_value and
385  * ModelAction::get_value for the purpose of RMW's, which may have both a
386  * 'read' and a 'write' value.
387  *
388  * Note: 'this' must be a load.
389  *
390  * @return The value read by this load
391  */
392 uint64_t ModelAction::get_reads_from_value() const
393 {
394         ASSERT(is_read());
395         if (reads_from)
396                 return reads_from->get_write_value();
397         else if (reads_from_promise)
398                 return reads_from_promise->get_value();
399         return VALUE_NONE; /* Only for new actions with no reads-from */
400 }
401
402 /**
403  * @brief Get the value written by this store
404  *
405  * We differentiate this function from ModelAction::get_reads_from_value and
406  * ModelAction::get_value for the purpose of RMW's, which may have both a
407  * 'read' and a 'write' value.
408  *
409  * Note: 'this' must be a store.
410  *
411  * @return The value written by this store
412  */
413 uint64_t ModelAction::get_write_value() const
414 {
415         ASSERT(is_write());
416         return value;
417 }
418
419 /**
420  * @brief Get the value returned by this action
421  *
422  * For atomic reads (including RMW), an operation returns the value it read.
423  * For atomic writes, an operation returns the value it wrote. For other
424  * operations, the return value varies (sometimes is a "don't care"), but the
425  * value is simply stored in the "value" field.
426  *
427  * @return This action's return value
428  */
429 uint64_t ModelAction::get_return_value() const
430 {
431         if (is_read())
432                 return get_reads_from_value();
433         else if (is_write())
434                 return get_write_value();
435         else
436                 return value;
437 }
438
439 /** @return The Node associated with this ModelAction */
440 Node * ModelAction::get_node() const
441 {
442         /* UNINIT actions do not have a Node */
443         ASSERT(!is_uninitialized());
444         return node;
445 }
446
447 /**
448  * Update the model action's read_from action
449  * @param act The action to read from; should be a write
450  */
451 void ModelAction::set_read_from(const ModelAction *act)
452 {
453         ASSERT(act);
454         reads_from = act;
455         reads_from_promise = NULL;
456         if (act->is_uninitialized())
457                 model->assert_bug("May read from uninitialized atomic\n");
458 }
459
460 /**
461  * Set this action's read-from promise
462  * @param promise The promise to read from
463  */
464 void ModelAction::set_read_from_promise(Promise *promise)
465 {
466         ASSERT(is_read());
467         reads_from_promise = promise;
468         reads_from = NULL;
469 }
470
471 /**
472  * Synchronize the current thread with the thread corresponding to the
473  * ModelAction parameter.
474  * @param act The ModelAction to synchronize with
475  * @return True if this is a valid synchronization; false otherwise
476  */
477 bool ModelAction::synchronize_with(const ModelAction *act)
478 {
479         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
480                 return false;
481         model->check_promises(act->get_tid(), cv, act->cv);
482         cv->merge(act->cv);
483         return true;
484 }
485
486 bool ModelAction::has_synchronized_with(const ModelAction *act) const
487 {
488         return cv->synchronized_since(act);
489 }
490
491 /**
492  * Check whether 'this' happens before act, according to the memory-model's
493  * happens before relation. This is checked via the ClockVector constructs.
494  * @return true if this action's thread has synchronized with act's thread
495  * since the execution of act, false otherwise.
496  */
497 bool ModelAction::happens_before(const ModelAction *act) const
498 {
499         return act->cv->synchronized_since(this);
500 }
501
502 /** @brief Print nicely-formatted info about this ModelAction */
503 void ModelAction::print() const
504 {
505         const char *type_str, *mo_str;
506         switch (this->type) {
507         case MODEL_FIXUP_RELSEQ:
508                 type_str = "relseq fixup";
509                 break;
510         case THREAD_CREATE:
511                 type_str = "thread create";
512                 break;
513         case THREAD_START:
514                 type_str = "thread start";
515                 break;
516         case THREAD_YIELD:
517                 type_str = "thread yield";
518                 break;
519         case THREAD_JOIN:
520                 type_str = "thread join";
521                 break;
522         case THREAD_FINISH:
523                 type_str = "thread finish";
524                 break;
525         case ATOMIC_UNINIT:
526                 type_str = "uninitialized";
527                 break;
528         case ATOMIC_READ:
529                 type_str = "atomic read";
530                 break;
531         case ATOMIC_WRITE:
532                 type_str = "atomic write";
533                 break;
534         case ATOMIC_RMW:
535                 type_str = "atomic rmw";
536                 break;
537         case ATOMIC_FENCE:
538                 type_str = "fence";
539                 break;
540         case ATOMIC_RMWR:
541                 type_str = "atomic rmwr";
542                 break;
543         case ATOMIC_RMWC:
544                 type_str = "atomic rmwc";
545                 break;
546         case ATOMIC_INIT:
547                 type_str = "init atomic";
548                 break;
549         case ATOMIC_LOCK:
550                 type_str = "lock";
551                 break;
552         case ATOMIC_UNLOCK:
553                 type_str = "unlock";
554                 break;
555         case ATOMIC_TRYLOCK:
556                 type_str = "trylock";
557                 break;
558         case ATOMIC_WAIT:
559                 type_str = "wait";
560                 break;
561         case ATOMIC_NOTIFY_ONE:
562                 type_str = "notify one";
563                 break;
564         case ATOMIC_NOTIFY_ALL:
565                 type_str = "notify all";
566                 break;
567         default:
568                 type_str = "unknown type";
569         }
570
571         switch (this->order) {
572         case std::memory_order_relaxed:
573                 mo_str = "relaxed";
574                 break;
575         case std::memory_order_acquire:
576                 mo_str = "acquire";
577                 break;
578         case std::memory_order_release:
579                 mo_str = "release";
580                 break;
581         case std::memory_order_acq_rel:
582                 mo_str = "acq_rel";
583                 break;
584         case std::memory_order_seq_cst:
585                 mo_str = "seq_cst";
586                 break;
587         default:
588                 mo_str = "unknown";
589                 break;
590         }
591
592         model_print("(%4d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p   Value: %-#18" PRIx64,
593                         seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
594         if (is_read()) {
595                 if (reads_from)
596                         model_print("  Rf: %-3d", reads_from->get_seq_number());
597                 else if (reads_from_promise) {
598                         int idx = model->get_promise_number(reads_from_promise);
599                         if (idx >= 0)
600                                 model_print("  Rf: P%-2d", idx);
601                         else
602                                 model_print("  RF: P? ");
603                 } else
604                         model_print("  Rf: ?  ");
605         }
606         if (cv) {
607                 if (is_read())
608                         model_print(" ");
609                 else
610                         model_print("          ");
611                 cv->print();
612         } else
613                 model_print("\n");
614 }
615
616 /** @brief Get a (likely) unique hash for this ModelAction */
617 unsigned int ModelAction::hash() const
618 {
619         unsigned int hash = (unsigned int)this->type;
620         hash ^= ((unsigned int)this->order) << 3;
621         hash ^= seq_number << 5;
622         hash ^= id_to_int(tid) << 6;
623
624         if (is_read() && reads_from)
625                 hash ^= reads_from->get_seq_number();
626         return hash;
627 }
628
629 /**
630  * @brief Checks the NodeStack to see if a ModelAction is in our may-read-from set
631  * @param write The ModelAction to check for
632  * @return True if the ModelAction is found; false otherwise
633  */
634 bool ModelAction::may_read_from(const ModelAction *write) const
635 {
636         for (int i = 0; i < node->get_read_from_past_size(); i++)
637                 if (node->get_read_from_past(i) == write)
638                         return true;
639         return false;
640 }
641
642 /**
643  * @brief Checks the NodeStack to see if a Promise is in our may-read-from set
644  * @param promise The Promise to check for
645  * @return True if the Promise is found; false otherwise
646  */
647 bool ModelAction::may_read_from(const Promise *promise) const
648 {
649         for (int i = 0; i < node->get_read_from_promise_size(); i++)
650                 if (node->get_read_from_promise(i) == promise)
651                         return true;
652         return false;
653 }