P4C
The P4 Compiler
Loading...
Searching...
No Matches
register_read_write.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_MIDEND_REGISTER_READ_WRITE_H_
20#define BACKENDS_TOFINO_BF_P4C_MIDEND_REGISTER_READ_WRITE_H_
21
22#include "backends/tofino/bf-p4c/midend/copy_header.h"
23#include "backends/tofino/bf-p4c/midend/type_checker.h"
24#include "frontends/common/resolveReferences/referenceMap.h"
25#include "frontends/p4/typeMap.h"
26#include "ir/ir.h"
27#include "table_mutex.h"
28
29namespace BFN {
30
32 IR::Expression *replace;
33 const IR::Expression *search;
34
35 IR::Node *preorder(IR::Expression *expr) {
36 if (expr->toString() == search->toString()) return replace;
37 return expr;
38 }
39
40 public:
41 SearchAndReplaceExpr(IR::Expression *replace, const IR::Expression *search)
42 : replace(replace), search(search) {}
43};
44
45// Action -> register -> read/write statements
46// the action is either a P4Action or a Declaration_Instance representing a RegisterAction extern
47typedef std::unordered_map<const IR::Declaration *,
48 std::unordered_map<const IR::Declaration_Instance *, // Register
49 // declaration
51 RegisterCallsByAction;
52typedef std::unordered_map<const IR::Declaration *,
53 std::unordered_map<const IR::Declaration_Instance *, // Register
54 // declaration
55 IR::MethodCallExpression *>>
56 RegisterExecuteCallByAction;
57typedef std::unordered_map<const IR::P4Control *, ordered_set<IR::Declaration_Instance *>>
58 RegisterActionsByControl;
59typedef std::unordered_map<const IR::Declaration *, // Register declaration
60 std::unordered_set<const IR::Declaration *>>
61 ActionsByRegister;
62
75 P4::ReferenceMap *refMap;
76 P4::TypeMap *typeMap;
77
78 RegisterCallsByAction action_register_calls;
79 RegisterExecuteCallByAction action_register_exec_calls;
80 RegisterActionsByControl control_register_actions;
81 ActionsByRegister generated_register_actions;
82 ActionsByRegister actions_using_register;
83 TableMutex table_mutex;
84
93 class CheckRegisterActions : public Inspector {
95 std::unordered_map<const IR::Declaration *, // Register -> RegisterAction
96 std::vector<const IR::Declaration *>>
97 all_register_actions;
98
99 public:
100 explicit CheckRegisterActions(RegisterReadWrite &self) : self(self) {}
101
102 bool preorder(const IR::Declaration_Instance *) override;
103 void end_apply() override;
104 };
105
113 class UpdateRegisterActionsAndExecuteCalls : public Transform {
114 RegisterReadWrite &self;
115 std::map<const IR::P4Control *, IR::Declaration_Instance *> register_actions;
116 IR::Node *preorder(IR::P4Action *) override;
117 IR::Node *preorder(IR::Declaration_Instance *) override;
118 IR::Node *postorder(IR::P4Control *ctrl) override;
119
120 bool processDeclaration(const IR::Declaration *action, const IR::BlockStatement *&body)
121 __attribute__((__warn_unused_result__));
122
123 public:
124 explicit UpdateRegisterActionsAndExecuteCalls(RegisterReadWrite &self) : self(self) {}
125 };
126
139 class AnalyzeActionWithRegisterCalls : public Inspector {
140 RegisterReadWrite &self;
141 struct RegActionInfo {
142 // infomation and IR about RegisterAction being created to implement a
143 // Register read+write
144 IR::Declaration_Instance *reg_action = nullptr;
145 IR::ParameterList *apply_params = nullptr;
146 IR::BlockStatement *apply_body = nullptr;
147 IR::MethodCallExpression *execute_call = nullptr;
148 const IR::Expression *read_expr = nullptr;
149 };
150 bool preorder(const IR::Declaration *) override;
151
152 void createRegisterExecute(RegActionInfo &reg_info, const IR::Statement *reg_stmt,
153 cstring action_name);
154 void createRegisterAction(RegActionInfo &reg_info, const IR::Statement *reg_stmt,
155 const IR::Declaration *act);
156
157 public:
158 explicit AnalyzeActionWithRegisterCalls(RegisterReadWrite &self) : self(self) {}
159 };
160
167 class CollectRegisterReadsWrites : public Inspector {
168 RegisterReadWrite &self;
169 bool preorder(const IR::MethodCallExpression *) override;
170 void end_apply() override;
171 void collectRegReadWrite(const IR::MethodCallExpression *, const IR::Declaration *);
172
173 public:
174 explicit CollectRegisterReadsWrites(RegisterReadWrite &self) : self(self) {}
175 };
176
189 class MoveRegisterParameters : public Modifier {
190 RegisterReadWrite &self;
191 bool preorder(IR::P4Control *c) override;
192
193 public:
194 explicit MoveRegisterParameters(RegisterReadWrite &self) : self(self) {}
195 };
196
197 public:
199 BFN::TypeChecking *typeChecking = nullptr)
200 : refMap(refMap), typeMap(typeMap) {
201 if (!typeChecking) typeChecking = new BFN::TypeChecking(refMap, typeMap);
202 addPasses({typeChecking, &table_mutex, new MoveRegisterParameters(*this),
203 new CollectRegisterReadsWrites(*this), new AnalyzeActionWithRegisterCalls(*this),
204 new UpdateRegisterActionsAndExecuteCalls(*this), new P4::ClearTypeMap(typeMap),
205 new CopyHeaders(refMap, typeMap, typeChecking), new P4::ClearTypeMap(typeMap),
206 typeChecking, new CheckRegisterActions(*this)});
207 }
208
209 static std::pair<const IR::MethodCallExpression * /*call*/,
210 const IR::Expression * /*read_expr*/>
211 extractRegisterReadWrite(const IR::Statement *reg_stmt);
212};
213
214} // namespace BFN
215
216#endif // BACKENDS_TOFINO_BF_P4C_MIDEND_REGISTER_READ_WRITE_H_
Definition copy_header.h:32
Definition register_read_write.h:31
Definition typeChecker.h:32
Definition node.h:94
Definition visitor.h:400
Definition visitor.h:372
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 ordered_set.h:32
Definition midend/table_mutex.h:30
static std::pair< const IR::MethodCallExpression *, const IR::Expression * > extractRegisterReadWrite(const IR::Statement *reg_stmt)
Definition midend/register_read_write.cpp:92
The pass replaces the Register.read/write() calls with register actions.
Definition register_read_write.h:74
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21