P4C
The P4 Compiler
Loading...
Searching...
No Matches
manifest.h
1
19#ifndef _BACKENDS_TOFINO_BF_P4C_LOGGING_MANIFEST_H_
20#define _BACKENDS_TOFINO_BF_P4C_LOGGING_MANIFEST_H_
21
22#include <rapidjson/document.h>
23#include <rapidjson/prettywriter.h>
24
25#include <cstdarg>
26#include <fstream>
27#include <map>
28#include <set>
29#include <string>
30#include <utility>
31
32#include "bf-p4c/arch/arch.h"
33#include "bf-p4c/bf-p4c-options.h"
34#include "bf-p4c/ir/gress.h"
35#include "lib/cstring.h"
36
37namespace Logging {
38
45class Manifest : public Inspector {
46 using Writer = rapidjson::PrettyWriter<rapidjson::StringBuffer>;
47 using PathAndType = std::pair<cstring, cstring>;
48
49 // to use PathAndType as std::set
50 struct PathCmp {
51 bool operator()(const PathAndType &lhs, const PathAndType &rhs) const;
52 };
53
54 private:
56 struct InputFiles {
57 cstring m_rootPath;
58 cstring m_sourceInfo; // path to source.json relative to manifest.json
59 std::set<cstring> m_includePaths;
60 std::set<cstring> m_defines;
61
62 explicit InputFiles(const BFN_Options &);
63 void serialize(Writer &) const;
64 };
65
67 struct GraphOutput {
68 cstring m_path;
69 gress_t m_gress;
70 cstring m_type;
71 cstring m_format;
72 GraphOutput(const cstring path_in, const gress_t gress_in, const cstring type_in,
73 const cstring format_in = ".dot"_cs)
74 : m_path(path_in), m_gress(gress_in), m_type(type_in), m_format(format_in) {}
75
76 void serialize(Writer &) const;
77
78 cstring getHash() const { return m_path + toString(m_gress) + m_type + m_format; }
79 };
80
81 struct GraphOutputCmp {
82 bool operator()(const GraphOutput &lhs, const GraphOutput &rhs) const {
83 return std::less<const char *>()(lhs.getHash().c_str(), rhs.getHash().c_str());
84 }
85 };
86
88 struct OutputFiles {
89 cstring m_context; // path to the context file
90 cstring m_binary; // path to the binary file
91 // pairs of path and resource type
92 std::set<PathAndType, PathCmp> m_resources;
93 // pairs of path and log type
94 std::set<PathAndType, PathCmp> m_logs;
95 std::set<GraphOutput, GraphOutputCmp> m_graphs;
96
97 void serialize(Writer &) const;
98 };
99
100 const BFN_Options &m_options;
101 // pairs of <pipe_id, pipe_name>
102 std::map<unsigned int, cstring> m_pipes;
104 std::map<unsigned int, OutputFiles *> m_pipeOutputs;
105 InputFiles m_programInputs;
106 cstring m_eventLogPath, m_frontendIrLogPath;
108 BFN::ProgramPipelines m_pipelines;
109 int m_pipeId = -1;
111 P4::ReferenceMap *m_refMap = nullptr;
112 std::ofstream m_manifestStream;
114 bool m_success = false;
115
116 private:
117 Manifest();
118 ~Manifest();
119
120 OutputFiles *getPipeOutputs(unsigned int pipe);
121
122 public:
123 static Manifest &getManifest(); // singleton
124
126 void postorder(const IR::BFN::TnaParser *parser) override;
127 void postorder(const IR::BFN::TnaControl *control) override;
130 void setRefMap(/* intentionally no "const" here */ P4::ReferenceMap *const refMap_in) {
131 m_refMap = refMap_in;
132 }
133
134 void setPipe(int pipe_ID, const cstring pipe_name);
135
136 void setSuccess(const bool success) { m_success = success; }
137
138 void addContext(const int pipe, const cstring path) { getPipeOutputs(pipe)->m_context = path; }
139 void addResources(const int pipe, const cstring path, const cstring type = "resources"_cs) {
140 getPipeOutputs(pipe)->m_resources.insert(PathAndType(path, type));
141 }
142 void addGraph(int pipe, const cstring graphType, const cstring graphName, const gress_t gress,
143 const cstring extension_including_the_leading_period = ".dot"_cs);
144 void addLog(int pipe, const cstring logType, const cstring logName);
145 void addArchitecture(const BFN::ProgramPipelines &pipelines_in) { m_pipelines = pipelines_in; }
146 void setSourceInfo(const cstring sourceInfo_in) {
147 BUG_CHECK(m_programInputs.m_sourceInfo.size() == 0,
148 "Trying to redefine path to source info!");
149 m_programInputs.m_sourceInfo = sourceInfo_in;
150 }
151 void setEventLog(const cstring eventLogPath_in) {
152 BUG_CHECK(m_eventLogPath.size() == 0, "Trying to redefine path to source info!");
153 m_eventLogPath = eventLogPath_in;
154 }
155 void setFrontendIrLog(const cstring frontendIrLogPath_in) {
156 BUG_CHECK(m_frontendIrLogPath.size() == 0, "Trying to redefine path to frontend IR!");
157 m_frontendIrLogPath = frontendIrLogPath_in;
158 }
159
161 virtual void serialize();
162
163 private:
164 void serialize_target_data(Writer &);
165
167 void serializePipes(Writer &);
168
169 void sendTo(Writer &, int pipe, gress_t);
170};
171} // end namespace Logging
172
173#endif /* _BACKENDS_TOFINO_BF_P4C_LOGGING_MANIFEST_H_ */
Definition arch.h:294
Definition bf-p4c-options.h:28
Definition manifest.h:45
virtual void serialize()
serialize the entire manifest
Definition manifest.cpp:127
void postorder(const IR::BFN::TnaParser *parser) override
Visitor methods to generate graphs.
Definition manifest.cpp:37
static Manifest & getManifest()
Return the singleton object.
Definition manifest.cpp:414
void setRefMap(P4::ReferenceMap *const refMap_in)
Definition manifest.h:130
Definition visitor.h:400
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition cstring.h:85
Definition filelog.h:32