P4C
The P4 Compiler
Loading...
Searching...
No Matches
mutex_overlay.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_PHV_ANALYSIS_MUTEX_OVERLAY_H_
20#define BACKENDS_TOFINO_BF_P4C_PHV_ANALYSIS_MUTEX_OVERLAY_H_
21
22#include <iostream>
23
24#include "backends/tofino/bf-p4c/ir/tofino_write_context.h"
25#include "backends/tofino/bf-p4c/mau/mau_visitor.h"
26#include "backends/tofino/bf-p4c/parde/parde_visitor.h"
27#include "backends/tofino/bf-p4c/parde/parser_info.h"
28#include "backends/tofino/bf-p4c/phv/analysis/build_mutex.h"
29#include "backends/tofino/bf-p4c/phv/analysis/header_mutex.h"
30#include "backends/tofino/bf-p4c/phv/phv_fields.h"
31#include "backends/tofino/bf-p4c/phv/pragma/pa_no_pack.h"
32#include "ir/ir.h"
33#include "ir/visitor.h"
34
35namespace PHV {
36class Field;
37} // namespace PHV
38
39class PhvInfo;
40
41/* Produces a SymBitMatrix where keys are PHV::Field ids and values
42 * indicate whether two fields are mutually exclusive, based on analyzing the
43 * structure of the parse graph to identify fields that can never appear in the
44 * same packet. @see BuildMutex.
45 */
48 static bool ignore_field(const PHV::Field *f) { return !f || f->pov || f->metadata; }
49
50 profile_t init_apply(const IR::Node *root) override;
51 bool preorder(const IR::MAU::TableSeq *) override { return false; }
52 bool preorder(const IR::BFN::Deparser *) override { return false; }
53
54 public:
55 BuildParserOverlay(PhvInfo &phv, const bitvec &neverOverlay, const PragmaNoOverlay &pragma)
56 : BuildMutex(phv, neverOverlay, pragma, ignore_field) {}
57 BuildParserOverlay *clone() const override { return new BuildParserOverlay(*this); }
58};
59
61 PhvInfo &phv;
62 const MapFieldToParserStates &fieldToStates;
63 const CollectParserInfo &parserInfo;
64
65 private:
66 const IR::Node *apply_visitor(const IR::Node *root, const char *) override;
67
68 bool is_loop_reachable(const ordered_set<const IR::BFN::ParserState *> &k,
70
71 public:
73 const CollectParserInfo &pi)
74 : phv(phv), fieldToStates(fs), parserInfo(pi) {}
75};
76
77/* Produces a SymBitMatrix where keys are PHV::Field ids and values indicate
78 * whether two fields are mutually exclusive, based on analyzing the structure
79 * of the MAU pipeline to identify metadata fields that are only used in
80 * mutually exclusive tables/actions. @see BuildMutex.
81 */
83 private:
85 static bool ignore_field(const PHV::Field *f) { return !f || f->pov || !f->metadata; }
86
87 profile_t init_apply(const IR::Node *root) override;
88 bool preorder(const IR::BFN::Deparser *) override { return false; }
89
90 public:
91 BuildMetadataOverlay(PhvInfo &phv, const bitvec &neverOverlay, const PragmaNoOverlay &pragma)
92 : BuildMutex(phv, neverOverlay, pragma, ignore_field) {}
93 BuildMetadataOverlay *clone() const override { return new BuildMetadataOverlay(*this); }
94};
95
100 PhvInfo &phv;
101 bitvec &neverOverlay;
102
103 bool preorder(const IR::BFN::AliasMember *alias) override {
104 excludeAliasedField(alias);
105 return true;
106 }
107
108 bool preorder(const IR::BFN::AliasSlice *alias) override {
109 excludeAliasedField(alias);
110 return true;
111 }
112
113 void excludeAliasedField(const IR::Expression *alias);
114
115 public:
116 ExcludeAliasedHeaderFields(PhvInfo &phv, bitvec &neverOverlay)
117 : phv(phv), neverOverlay(neverOverlay) {}
118};
119
125 PhvInfo &phv;
126 bitvec &neverOverlay;
127
128 profile_t init_apply(const IR::Node *root) override {
129 profile_t rv = Inspector::init_apply(root);
130 neverOverlay.clear();
131 return rv;
132 }
133
134 void end_apply() override;
135
136 public:
138 : phv(phv), neverOverlay(neverOverlay) {}
139};
140
145 bitvec &neverOverlay;
146 const PragmaNoOverlay &pragma;
147
148 void end_apply() override;
149
150 public:
152 : neverOverlay(neverOverlay), pragma(p) {}
153};
154
170 private:
171 PhvInfo &phv;
172 bitvec rv;
173
174 profile_t init_apply(const IR::Node *root) override {
175 rv.clear();
176 return Inspector::init_apply(root);
177 }
178 bool preorder(const IR::MAU::Primitive *prim) override;
179 void markFields(const IR::HeaderRef *hr);
180
181 public:
182 explicit FindAddedHeaderFields(PhvInfo &phv) : phv(phv) {}
183 bool isAddedInMAU(const PHV::Field *field) const { return rv[field->id]; }
184};
185
187 public:
189
190 private:
191 PhvInfo &phv;
192 const FindAddedHeaderFields &addedFields;
193
194 ActionToFieldsMap actionToWrites;
195 ActionToFieldsMap actionToReads;
196
197 profile_t init_apply(const IR::Node *root) override {
198 actionToWrites.clear();
199 actionToReads.clear();
200 return Inspector::init_apply(root);
201 }
202
203 bool preorder(const IR::MAU::Table *tbl) override;
204 bool preorder(const IR::MAU::Instruction *inst) override;
205 void end_apply() override;
206
209 void markNonMutex(const ActionToFieldsMap &arg);
210
211 public:
213 : phv(p), addedFields(a) {}
214};
215
219 private:
220 PhvInfo &phv;
222
223 profile_t init_apply(const IR::Node *root) override {
224 povToFieldsMap.clear();
225 return Inspector::init_apply(root);
226 }
227
228 bool preorder(const IR::BFN::EmitField *emit) override;
229 void end_apply() override;
230
231 public:
232 explicit ExcludeDeparserOverlays(PhvInfo &p) : phv(p) {}
233};
234
250 private:
251 PhvInfo &phv;
252 const FindAddedHeaderFields &addedFields;
253 const PhvUse &use;
254 bool preorder(const IR::BFN::EmitChecksum *emitChecksum) override;
255
256 public:
257 explicit ExcludeCsumOverlays(PhvInfo &p, const FindAddedHeaderFields &a, const PhvUse &u)
258 : phv(p), addedFields(a), use(u) {}
259};
260
269 private:
270 PhvInfo &phv;
271 const FindAddedHeaderFields &addedFields;
272 const PhvUse &use;
273 bool preorder(const IR::BFN::EmitChecksum *emitChecksum) override;
274
275 public:
276 explicit ExcludeCsumOverlaysPOV(PhvInfo &p, const FindAddedHeaderFields &a, const PhvUse &u)
277 : phv(p), addedFields(a), use(u) {}
278};
279
281 private:
282 PhvInfo &phv;
283 const PragmaMutuallyExclusive &pragma;
284
285 profile_t init_apply(const IR::Node *root) override;
286
287 public:
289 : phv(p), pragma(pr) {}
290};
291
293class MutexOverlay : public PassManager {
294 private:
296 bitvec neverOverlay;
297 FindAddedHeaderFields addedFields;
298 CollectParserInfo parserInfo;
299 MapFieldToParserStates fieldToParserStates;
300
301 public:
302 MutexOverlay(PhvInfo &phv, const PHV::Pragmas &pragmas, const PhvUse &use)
303 : addedFields(phv), fieldToParserStates(phv) {
304 Visitor *exclude_csum_overlays = nullptr;
305 if (Device::currentDevice() == Device::TOFINO) {
306 exclude_csum_overlays = new ExcludeCsumOverlays(phv, addedFields, use);
307 } else if (Device::currentDevice() == Device::JBAY) {
308 exclude_csum_overlays = new ExcludeCsumOverlaysPOV(phv, addedFields, use);
309 }
310
311 addPasses({new ExcludeDeparsedIntrinsicMetadata(phv, neverOverlay),
312 new ExcludePragmaNoOverlayFields(neverOverlay, pragmas.pa_no_overlay()),
313 &addedFields, new ExcludeAliasedHeaderFields(phv, neverOverlay),
314 new BuildParserOverlay(phv, neverOverlay, pragmas.pa_no_overlay()),
315 new BuildMetadataOverlay(phv, neverOverlay, pragmas.pa_no_overlay()),
316 &parserInfo, &fieldToParserStates,
317 new ExcludeParserLoopReachableFields(phv, fieldToParserStates, parserInfo),
318 exclude_csum_overlays, new ExcludeMAUOverlays(phv, addedFields),
319 new ExcludeDeparserOverlays(phv), new HeaderMutex(phv, neverOverlay, pragmas),
320 new MarkMutexPragmaFields(phv, pragmas.pa_mutually_exclusive())});
321 }
322};
323
324#endif /* BACKENDS_TOFINO_BF_P4C_PHV_ANALYSIS_MUTEX_OVERLAY_H_ */
Definition mutex_overlay.h:82
Definition build_mutex.h:63
Definition mutex_overlay.h:46
Definition mutex_overlay.h:99
Definition mutex_overlay.h:249
Definition mutex_overlay.h:268
Definition mutex_overlay.h:124
Definition mutex_overlay.h:218
Definition mutex_overlay.h:186
Definition mutex_overlay.h:60
Definition mutex_overlay.h:144
Definition mutex_overlay.h:169
After identifying which headers in the P4 program are mutually exclusive in the parser,...
Definition header_mutex.h:400
Definition mutex_overlay.h:280
Definition mau_visitor.h:29
Definition mutex_overlay.h:293
Definition node.h:94
Definition visitor.h:400
Definition ir/pass_manager.h:40
Definition visitor.h:78
Definition visitor.h:75
Definition bitvec.h:120
Definition ordered_set.h:32
Definition phv_fields.h:154
int id
Unique field ID.
Definition phv_fields.h:164
bool pov
True if this Field is a validity bit.
Definition phv_fields.h:253
bool metadata
True if this Field is metadata.
Definition phv_fields.h:209
Definition phv_pragmas.h:46
Definition phv_fields.h:1095
Definition phv_parde_mau_use.h:154
Definition pa_mutually_exclusive.h:37
Definition pa_no_overlay.h:33
The namespace encapsulating PHV-related stuff.
Definition gateway.h:32
Map field to the parser states in which they are extracted or assigned from checksums.
Definition phv_fields.h:1755