Debian Codegraph @

openmp/tools/omptest/include/InternalEventCommon.h

raw
1 //===- InternalEventCommon.h - Common internal event basics -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Provides event types, and class/operator declaration macros.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #ifndef OPENMP_TOOLS_OMPTEST_INCLUDE_INTERNALEVENTCOMMON_H
15 #define OPENMP_TOOLS_OMPTEST_INCLUDE_INTERNALEVENTCOMMON_H
16
17 #include "omp-tools.h"
18
19 #include <cassert>
20 #include <string>
21
22 namespace omptest {
23
24 namespace internal {
25 /// Enum values are used for comparison of observed and asserted events
26 /// List is based on OpenMP 5.2 specification, table 19.2 (page 447)
27 enum class EventTy {
28 None, // not part of OpenMP spec, used for implementation
29 AssertionSyncPoint, // not part of OpenMP spec, used for implementation
30 AssertionSuspend, // not part of OpenMP spec, used for implementation
31 BufferRecord, // not part of OpenMP spec, used for implementation
32 BufferRecordDeallocation, // not part of OpenMP spec, used for implementation
33 ThreadBegin,
34 ThreadEnd,
35 ParallelBegin,
36 ParallelEnd,
37 Work,
38 Dispatch,
39 TaskCreate, // TODO: Implement
40 Dependences, // TODO: Implement
41 TaskDependence, // TODO: Implement
42 TaskSchedule, // TODO: Implement
43 ImplicitTask, // TODO: Implement
44 Masked, // TODO: Implement
45 SyncRegion,
46 MutexAcquire, // TODO: Implement
47 Mutex, // TODO: Implement
48 NestLock, // TODO: Implement
49 Flush, // TODO: Implement
50 Cancel, // TODO: Implement
51 DeviceInitialize,
52 DeviceFinalize,
53 DeviceLoad,
54 DeviceUnload,
55 BufferRequest,
56 BufferComplete,
57 TargetDataOp,
58 TargetDataOpEmi,
59 Target,
60 TargetEmi,
61 TargetSubmit,
62 TargetSubmitEmi,
63 ControlTool
64 };
65
66 /// Base event class
67 /// Offers default CTOR, DTOR and CTOR which assigns the actual event type.
68 struct InternalEvent {
69 InternalEvent() : Type(EventTy::None) {}
70 InternalEvent(EventTy T) : Type(T) {}
71 virtual ~InternalEvent() = default;
72
73 virtual bool equals(const InternalEvent *o) const {
74 assert(false && "Base class implementation");
75 return false;
76 };
77
78 virtual std::string toString() const {
79 std::string S{"InternalEvent: Type="};
80 S.append(std::to_string((uint32_t)Type));
81 return S;
82 }
83
84 /// Identifying event type
85 EventTy Type;
86 };
87
88 /// Specialize EventType member for each derived internal event type.
89 /// Effectively selecting an event type as initialization value.
90 template <typename EventType> struct EventTypeOf;
91
92 /// Actual definition macro for EventTypeOf.
93 #define event_type_trait(EvTy) \
94 template <> struct EventTypeOf<EvTy> { \
95 static constexpr EventTy Value = EventTy::EvTy; \
96 };
97
98 /// CRTP (Curiously Recurring Template Pattern) intermediate class
99 /// Adding a new event type can be achieved by inheriting from an EventBase
100 /// template instantiation of the new class' name, like this:
101 /// struct NewEventType : public EventBase<NewEventType>
102 template <typename Derived> class EventBase : public InternalEvent {
103 public:
104 static constexpr EventTy EventType = EventTypeOf<Derived>::Value;
105 EventBase() : InternalEvent(EventType) {}
106 virtual ~EventBase() = default;
107
108 /// Equals method to cast and dispatch to the specific class operator==
109 virtual bool equals(const InternalEvent *o) const override {
110 // Note: When the if-condition evaluates to true, the event types are
111 // trivially identical. Otherwise, a cast to the Derived pointer would have
112 // been impossible.
113 if (const auto Other = dynamic_cast<const Derived *>(o))
114 return operator==(*static_cast<const Derived *>(this), *Other);
115 return false;
116 }
117
118 /// Basic toString method, which may be overridden with own implementations.
119 virtual std::string toString() const override {
120 std::string S{"EventBase: Type="};
121 S.append(std::to_string((uint32_t)Type));
122 return S;
123 }
124 };
125
126 } // namespace internal
127
128 } // namespace omptest
129
130 #endif