Poplar and PopLibs
DebugContext.hpp
1// Copyright (c) 2020 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_DebugContext_hpp
4#define poplar_DebugContext_hpp
5
6#include "ProfileValue.hpp"
7#include "SerializationFormat.hpp"
8#include <cstdint>
9#include <string>
10
11namespace poplar {
12
13#if defined(__clang__)
14#define SUPPORTS_LOCATION_BUILTINS \
15 (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
16 __has_builtin(__builtin_LINE))
17#elif __GNUC__ >= 7
18#define SUPPORTS_LOCATION_BUILTINS 1
19#else
20#define SUPPORTS_LOCATION_BUILTINS 0
21#endif
22
27 const char *functionName{""};
28 const char *fileName{""};
29 unsigned lineNumber{};
30 bool valid{false};
31
32public:
33 SourceLocation() = default;
34 constexpr SourceLocation(const char *functionName, const char *fileName,
35 unsigned lineNumber)
36 : functionName{functionName}, fileName{fileName},
37 lineNumber{lineNumber}, valid{true} {}
38 constexpr const char *getFunctionName() const { return functionName; }
39 constexpr const char *getFileName() const { return fileName; }
40 constexpr unsigned getLineNumber() const { return lineNumber; }
41 constexpr bool isValid() const { return valid; }
42#if SUPPORTS_LOCATION_BUILTINS
43 static SourceLocation Current(const char *functionName = __builtin_FUNCTION(),
44 const char *fileName = __builtin_FILE(),
45 unsigned lineNumber = __builtin_LINE()) {
46 return {functionName, fileName, lineNumber};
47 }
48#else
49 static SourceLocation Current() { return {}; }
50#endif
51};
52
54 JSON,
55 CBOR,
56};
57
59using DebugId = std::uint64_t;
60
61class DebugContext;
62namespace core {
63class DebugInfo;
64}
65
91class DebugInfo {
92 std::unique_ptr<core::DebugInfo> impl;
93
94public:
101 DebugInfo(const DebugContext &debugContext, std::string layer);
102 DebugInfo &operator=(const DebugInfo &) = delete;
103 DebugInfo(const DebugInfo &) = delete;
104 virtual ~DebugInfo();
107 DebugId getId() const;
110 std::string getPathName() const;
111 core::DebugInfo &getImpl() const { return *impl; }
119 const std::string &fileName,
125 static void closeStreamer();
126
132 bool setValue(std::string name, ProfileValue value);
133};
134
135namespace core {
136class DebugNameAndId;
137}
138
143 std::unique_ptr<core::DebugNameAndId> impl;
144
145public:
146 DebugNameAndId(std::string name = "", DebugId debugId = {},
147 std::string parentPath = "");
148 DebugNameAndId(const char *name);
149 DebugNameAndId(DebugId debugId);
150 DebugNameAndId(const DebugInfo &debugInfo, std::string name = "");
151 DebugNameAndId(const DebugNameAndId &DebugNameAndId, StringRef name = "");
152 DebugNameAndId &operator=(const DebugNameAndId &other);
158 std::string getPathName() const;
159 core::DebugNameAndId &getImpl() const { return *impl; }
160};
161
167std::ostream &operator<<(std::ostream &os, const DebugNameAndId &dnai);
168
169namespace core {
170struct DebugContext;
171}
172
222 std::unique_ptr<core::DebugContext> impl;
223
224public:
225 DebugContext(SourceLocation loc = SourceLocation::Current());
226 DebugContext(const char *name,
227 SourceLocation loc = SourceLocation::Current());
228 DebugContext(StringRef name, SourceLocation loc = SourceLocation::Current());
229 DebugContext(std::string name,
230 SourceLocation loc = SourceLocation::Current());
231 DebugContext(const DebugInfo &debugInfo, std::string name = "",
232 SourceLocation loc = SourceLocation::Current());
233 DebugContext(const DebugNameAndId &debugNameAndId, std::string name = "",
234 SourceLocation loc = SourceLocation::Current());
235 DebugContext(const DebugContext &debugContext, StringRef name = "");
236 DebugContext(const DebugContext &debugContext, SourceLocation loc);
237 DebugContext(DebugContext &&) noexcept;
243 std::string getPathName() const;
244 core::DebugContext &getImpl() const { return *impl; }
245};
246
252std::ostream &operator<<(std::ostream &os, const DebugContext &dc);
253
254} // end namespace poplar
255
256#endif // poplar_DebugContext_hpp
DebugContext gathers the common external parameters of the context of an operation.
Definition: DebugContext.hpp:221
std::string getPathName() const
Gets the pathname of this object as the concatenation of the parent name received in the constructor ...
DebugInfo stores and persists a set of data that describes the context of an operation.
Definition: DebugContext.hpp:91
bool setValue(std::string name, ProfileValue value)
Adds custom data to this object if "name" is not already set.
DebugId getId() const
Gets the unique identifier of this DebugInfo object.
std::string getPathName() const
Gets the pathname of this object (as received from DebugContext).
static void closeStreamer()
Closes the streamer: all data is flushed to disk and the file is ready to be read.
static void initializeStreamer(const std::string &fileName, const DebugSerializationFormat &format=DebugSerializationFormat::CBOR)
Initializes the streamer, unless it is already initialized (for example through env variables).
DebugInfo(const DebugContext &debugContext, std::string layer)
Constructor.
DebugNameAndId bundles a name and a DebugId to facilitate their propagation through function calls.
Definition: DebugContext.hpp:142
std::string getPathName() const
Gets the pathname of this object as the concatenation of the parent name received in the constructor ...
ProfileValue represents a read-only JSON-like tree of values that are used to store the output of the...
Definition: ProfileValue.hpp:39
This class mimics std::source_location that is unavailable as we don't yet support C++20.
Definition: DebugContext.hpp:26
Poplar classes and functions.
Definition: ArrayRef.hpp:14
std::ostream & operator<<(std::ostream &os, const DebugNameAndId &dnai)
Display the path name of the DebugNameAndId.
std::uint64_t DebugId
A unique identifier for the debug context.
Definition: DebugContext.hpp:59
DebugSerializationFormat
Definition: DebugContext.hpp:53
@ JSON
Serialise in JSON format.
@ CBOR
Serialise in CBOR format.