P4C
The P4 Compiler
Loading...
Searching...
No Matches
ping_pong_generation.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_MIDEND_PING_PONG_GENERATION_H_
20#define BACKENDS_TOFINO_BF_P4C_MIDEND_PING_PONG_GENERATION_H_
21
22#include "backends/tofino/bf-p4c/midend/type_checker.h"
23#include "frontends/common/resolveReferences/referenceMap.h"
24#include "frontends/p4/methodInstance.h"
25#include "frontends/p4/typeMap.h"
26#include "ir/gress.h"
27#include "ir/ir.h"
28#include "midend/collect_pipelines.h"
29
30namespace BFN {
31
47 // CONSTANTS ----------------------------------------------------------------------------------
51 static const cstring ID_PING_PONG_SUFFIX;
55 static const cstring GMD_STRUCTURE_NAME;
59 static const cstring PING_PONG_FIELD_NAME;
60
61 // VARIABLES ----------------------------------------------------------------------------------
62 // These variables serve as a state/storage between different passes
66 const IR::Type_Header *ghost_meta_struct = nullptr;
67
68 // Basic maps
69 P4::ReferenceMap *refMap;
70 P4::TypeMap *typeMap;
71
72 // Local maps for storing information
73 using RegisterSet = std::set<const IR::Declaration_Instance *>;
75
76 template <typename Key>
78
79 template <typename Key, typename Value>
80 using TnaControlPairMap = std::map<std::pair<const IR::BFN::TnaControl *, const Key>, Value,
82
83 template <typename Value>
84 using TnaControlMap = std::map<const IR::BFN::TnaControl *, Value, TnaControlLess>;
85
86 using RegisterToRegisterActionMap =
87 TnaControlPairMap<const IR::Declaration_Instance *, RegisterSet>;
88 using RegisterToP4ActionMap =
89 TnaControlPairMap<const IR::Declaration_Instance *, std::set<const IR::P4Action *>>;
90 using P4ActionToRegisterMap =
91 TnaControlPairMap<const IR::P4Action *, const IR::Declaration_Instance *>;
92 using P4ActionToP4TableMap = TnaControlPairMap<const IR::P4Action *, const IR::P4Table *>;
93 using P4TableToRegisterMap =
94 TnaControlPairMap<const IR::P4Table *, const IR::Declaration_Instance *>;
95 using P4TableToP4TableMap = std::unordered_map<const IR::P4Table *, const IR::P4Table *>;
96 using ControlToRegisterMap = TnaControlMap<RegisterSet>;
100 TnaControlMap<cstring> ghost_meta_name;
101
102 RegisterToRegisterActionMap registerToRegisterAction;
103 RegisterToP4ActionMap registerToP4Action;
104 P4ActionToRegisterMap p4ActionToRegister;
105 P4ActionToP4TableMap p4ActionToP4Table;
106 P4TableToRegisterMap p4TableToRegister;
107 ControlToRegisterMap ppIfCoveredRegisters;
108 ControlToRegisterMap controlToRegister;
109 P4TableToP4TableMap p4TableToDuplicateTable;
110
112
113 // HELPER FUNCTIONS ---------------------------------------------------------------------------
114 inline IR::Path *appendPingPongSuffix(IR::Path *, std::set<cstring> &);
115 inline void duplicateNodeDeclaration(const IR::Declaration *, IR::BFN::TnaControl *,
116 std::set<cstring> &);
117
120 std::set<const IR::Declaration_Instance *> allRegisters() const;
121 bool shouldTransform(); // FIXME const;
122
125 bool isPingPongValid(const IR::Declaration_Instance *reg);
126
130 bool isPingPongValid(const IR::BFN::TnaControl *gress, const IR::Declaration_Instance *reg,
131 bool calculatingCovered = false); // FIXME const;
132
135 bool isPingPongValid(const CollectPipelines::Pipe &pipe, const IR::Declaration_Instance *reg,
136 bool calculatingCovered = false); // FIXME const;
137
138 // HELPER VISITORS ----------------------------------------------------------------------------
139 // Finds a ghost_metadata.ping_pong field reference in a subtree
141 // Base class for chaning declarations
142 class DeclarationChanger;
143 // This visitor changes specific references in new cloned register action
144 class RegActionChanger;
145 // This visitor changes specific references in new cloned P4 action
146 class P4ActionChanger;
147 // This visitor changes specific references in new cloned P4 table
148 class P4TableChanger;
149 // This visitor changes P4 table references in cloned MethodCallStatement
150 class ApplyMCSChanger;
151
152 // MAIN VISITORS ------------------------------------------------------------------------------
156 class GetAllRegisters : public Inspector {
157 PingPongGeneration &self;
158
159 bool preorder(const IR::MethodCallExpression *) override;
160
161 public:
162 explicit GetAllRegisters(PingPongGeneration &self) : self(self) {}
163 };
164
168 class AddAllTables : public Inspector {
169 PingPongGeneration &self;
170
171 bool preorder(const IR::ActionListElement *) override;
172
173 public:
174 explicit AddAllTables(PingPongGeneration &self) : self(self) {}
175 };
176
182 class CheckPingPongTables : public Inspector {
183 PingPongGeneration &self;
184 unsigned pipes = 0;
185
186 // Finds ghost_metadata structure presence
187 bool preorder(const IR::Type_Header *) override;
188 // Finds tables applied under ping_pong
189 bool preorder(const IR::PathExpression *) override;
190 // Gets the identifier of ghost metadata
191 bool preorder(const IR::Parameter *) override;
192
193 public:
194 explicit CheckPingPongTables(PingPongGeneration &self) : self(self) {}
195 };
196
200 class GeneratePingPongMechanismDeclarations : public Transform {
201 PingPongGeneration &self;
202
203 IR::Node *preorder(IR::P4Program *);
204 IR::Node *preorder(IR::BFN::TnaControl *);
205
206 public:
207 explicit GeneratePingPongMechanismDeclarations(PingPongGeneration &self) : self(self) {}
208 };
209
213 class GeneratePingPongMechanism : public Transform {
214 PingPongGeneration &self;
215
216 IR::Node *postorder(IR::MethodCallStatement *);
217
218 public:
219 explicit GeneratePingPongMechanism(PingPongGeneration &self) : self(self) {}
220 };
221
222 public:
227 : refMap(refMap), typeMap(typeMap) {
228 setName("Automatic ping-pong generation");
229 addPasses({new CollectPipelines(refMap, &pipelines),
230 new TypeChecking(refMap, typeMap, true), new GetAllRegisters(*this),
231 new AddAllTables(*this), new CheckPingPongTables(*this),
232 new PassIf([this]() { return shouldTransform(); },
233 {new GeneratePingPongMechanismDeclarations(*this),
234 // Update ref and type map, bacause the IR might have changed
235 new TypeChecking(refMap, typeMap, true),
236 new GeneratePingPongMechanism(*this)})});
237 }
238};
239
240} // namespace BFN
241
242#endif // BACKENDS_TOFINO_BF_P4C_MIDEND_PING_PONG_GENERATION_H_
Definition collect_pipelines.h:58
Definition collect_pipelines.h:80
This visitor changes P4 table references in cloned MethodCallStatement.
Definition ping_pong_generation.cpp:143
Base visitor class for other classes that change declarations.
Definition ping_pong_generation.cpp:42
This visitor changes specific references in new cloned P4 action.
Definition ping_pong_generation.cpp:93
This visitor changes specific references in new cloned P4 table.
Definition ping_pong_generation.cpp:115
Finds a ghost_metadata.ping_pong field reference in a subtree.
Definition ping_pong_generation.cpp:202
This visitor changes specific references in new cloned register action.
Definition ping_pong_generation.cpp:72
Definition cmp.h:97
Definition node.h:94
Definition visitor.h:400
Definition ir/pass_manager.h:172
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition cstring.h:85
Definition cmp.h:113
Definition decaf.h:117
PingPongGeneration(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
Definition ping_pong_generation.h:226
Inspector pass that collects information about all pipelines declared in the Switch....
Definition collect_pipelines.h:42
PassManager that adds the ping pong mechanism for ghost thread.
Definition ping_pong_generation.h:46
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21