P4C
The P4 Compiler
Loading...
Searching...
No Matches
gateway.h
1
19#ifndef BF_P4C_MAU_GATEWAY_H_
20#define BF_P4C_MAU_GATEWAY_H_
21
22#include <set>
23
24#include "backends/tofino/bf-p4c/device.h"
25#include "backends/tofino/bf-p4c/mau/input_xbar.h"
26#include "backends/tofino/bf-p4c/mau/mau_visitor.h"
27#include "backends/tofino/bf-p4c/mau/tofino/input_xbar.h"
28#include "lib/safe_vector.h"
29
30using namespace P4;
31
32namespace PHV {
33class Field;
34} // namespace PHV
35
36class PhvInfo;
37
39 int PhvBytes;
40 int HashBits;
41 int PredicateBits;
42 int MaxRows;
43 bool SupportXor;
44 bool SupportRange;
45 int ExactShifts;
46 bool ByteSwizzle; // is the a byte swizzle between ixbar and gateway
47 int PerByteMatch; // lower bytes are shared per row, with 1 bit match per row
48 unsigned XorByteSlots;
49};
50
52 public:
53 CanonGatewayExpr() : _debugIndent(6) {}
54
55 private:
56 IR::MAU::Action *preorder(IR::MAU::Action *af) override {
57 prune();
58 return af;
59 }
60 IR::P4Table *preorder(IR::P4Table *t) override {
61 prune();
62 return t;
63 }
64 IR::Attached *preorder(IR::Attached *a) override {
65 prune();
66 return a;
67 }
68 IR::Node *preorder(IR::MAU::Table *) override;
69
72 const IR::Expression *preorder(IR::LAnd *) override;
73 const IR::Expression *preorder(IR::LOr *) override;
74 const IR::Expression *preorder(IR::LNot *) override;
75
76 const IR::Expression *postorder(IR::Operation::Relation *) override;
77 const IR::Expression *postorder(IR::Leq *) override;
78 const IR::Expression *postorder(IR::Lss *) override;
79 const IR::Expression *postorder(IR::Geq *) override;
80 const IR::Expression *postorder(IR::Grt *) override;
81 const IR::Expression *postorder(IR::LAnd *) override;
82 const IR::Expression *postorder(IR::LOr *) override;
83 const IR::Expression *postorder(IR::LNot *) override;
84 const IR::Expression *postorder(IR::BAnd *) override;
85 const IR::Expression *postorder(IR::BOr *) override;
86 const IR::Expression *postorder(IR::MAU::TypedPrimitive *) override;
87 const IR::Node *postorder(IR::MAU::Table *) override;
88 // helper functions
89 using GWRow_t = std::pair<const IR::Expression *, cstring>;
90 void removeUnusedRows(IR::MAU::Table *, bool isCanon);
91 void sortGatewayRows(safe_vector<GWRow_t> &gateway_rows);
92 void splitGatewayRows(safe_vector<GWRow_t> &gateway_rows);
93 void removeNotEquals(safe_vector<GWRow_t> &gateway_rows);
94 class NeedNegate;
95
96 private:
98 indent_t _debugIndent;
99};
100
102 const PhvInfo &phv;
103 const IXBar::Use *ixbar = nullptr;
104 const IR::MAU::Table *tbl = nullptr;
105 unsigned row_limit = ~0U; // FIXME -- needed? only use by SplitComplexGateways
106 PHV::FieldSlice xor_match;
107 bool preorder(const IR::MAU::Table *tbl) override;
108 bool preorder(const IR::Expression *) override;
109 bool preorder(const IR::MAU::TypedPrimitive *) override;
110 void postorder(const IR::Literal *) override;
111 void postorder(const IR::Operation::Relation *) override { xor_match = {}; }
112
113 public:
114 struct info_t {
115 ordered_set<PHV::FieldSlice> xor_with; // {x: x ==/!= this field in gateway }
116 bool const_eq = false; // bits compared ==/!= const
117 bool need_range = false; // bits needed in range compares
118 bool valid_bit = false; // TOFINO1-ONLY: implicit container valid bit
121 };
124 bool need_range = false;
125 int bytes = 0, bits = 0;
126 explicit CollectGatewayFields(const PhvInfo &phv, const IXBar::Use *ix = nullptr)
127 : phv(phv), ixbar(ix) {}
128 CollectGatewayFields(const PhvInfo &phv, unsigned rl) : phv(phv), row_limit(rl) {}
129 bool compute_offsets();
130 friend std::ostream &operator<<(std::ostream &, const info_t &);
131 friend std::ostream &operator<<(std::ostream &, const CollectGatewayFields &);
132};
133
135 bool fail;
136 bool preorder(const IR::MAU::Table *tbl) override;
137
138 public:
139 explicit CollectMatchFieldsAsGateway(const PhvInfo &phv, const IXBar::Use *ixb = nullptr)
140 : CollectGatewayFields(phv, ixb), fail(false) {}
141 explicit operator bool() { return !fail; }
142};
143
145 const PhvInfo &phv;
146 void postorder(IR::MAU::Table *) override;
147 // optimization -- prune parts of the tree we know can't contain MAU::Tables
148 bool preorder(IR::MAU::Action *) override { return false; }
149 bool preorder(IR::P4Table *) override { return false; }
150 bool preorder(IR::Attached *) override { return false; }
151 class SetupRanges;
152
153 public:
154 explicit GatewayRangeMatch(const PhvInfo &phv) : phv(phv) {}
155};
156
158 const PhvInfo &phv;
159 bool needConstOperand(const IR::Operation::Binary *);
160 bool preorder(const IR::MAU::Table *tbl) override;
161 bool preorder(const IR::MAU::Action *) override { return false; }
162 bool preorder(const IR::MAU::TableKey *) override { return false; }
163 bool preorder(const IR::P4Table *) override { return false; }
164 bool preorder(const IR::Attached *) override { return false; }
165 bool preorder(const IR::MAU::BackendAttached *) override { return false; }
166 bool preorder(const IR::Equ *) override { return true; }
167 bool preorder(const IR::Neq *) override { return true; }
168 bool preorder(const IR::BAnd *e) override { return needConstOperand(e); }
169 bool preorder(const IR::BOr *e) override { return needConstOperand(e); }
170 bool preorder(const IR::LAnd *) override { return true; }
171 bool preorder(const IR::LOr *) override { return true; }
172 bool preorder(const IR::LNot *) override { return true; }
173 bool preorder(const IR::Literal *) override { return true; }
174 bool preorder(const IR::RangeMatch *) override { return true; }
175 bool preorder(const IR::Expression *e) override;
176 bool preorder(const IR::Operation::Relation *rel) override { return needConstOperand(rel); }
177
178 public:
179 explicit CheckGatewayExpr(const PhvInfo &phv) : phv(phv) {}
180};
181
183 const PhvInfo &phv;
184 CollectGatewayFields &fields;
185 match_t match;
186 safe_vector<int> range_match;
187 std::map<int, match_t> byte_matches;
188 profile_t init_apply(const IR::Node *root) override;
189 bool preorder(const IR::Expression *) override;
190 bool preorder(const IR::MAU::TypedPrimitive *) override;
191 bool preorder(const IR::LAnd *) override { return true; }
192 bool preorder(const IR::LNot *) override { return true; }
193 bool preorder(const IR::BAnd *) override { return true; }
194 bool preorder(const IR::BOr *) override { return true; }
195 void constant(big_int val);
196 bool preorder(const IR::Constant *c) override {
197 constant(c->value);
198 return true;
199 }
200 bool preorder(const IR::BoolLiteral *c) override {
201 constant(c->value);
202 return true;
203 }
204 bool preorder(const IR::Equ *) override;
205 bool preorder(const IR::Neq *) override;
206 bool preorder(const IR::RangeMatch *) override;
207 friend std::ostream &operator<<(std::ostream &, const BuildGatewayMatch &);
208 PHV::FieldSlice match_field;
209 big_int andmask = 0, ormask = 0, cmplmask = 0;
210 int shift = 0, maxbit = 0;
211 bool check_per_byte_match(const std::pair<int, le_bitrange> &byte, big_int mask, big_int val);
212
213 public:
215};
216
217class GatewayOpt : public PassManager {
218 public:
219 explicit GatewayOpt(const PhvInfo &);
220};
221
222#endif /* BF_P4C_MAU_GATEWAY_H_ */
Definition gateway.h:182
Definition gateway.cpp:64
Definition gateway.h:51
Definition gateway.h:157
Definition gateway.h:101
Definition gateway.h:114
Definition gateway.h:134
Definition gateway.h:38
Definition gateway.h:217
Definition gateway.cpp:1029
Definition gateway.h:144
Definition mau_visitor.h:29
Definition mau_visitor.h:45
Definition mau_visitor.h:55
Definition node.h:94
Definition visitor.h:400
Definition ir/pass_manager.h:40
Definition visitor.h:78
Definition indent.h:26
Definition ordered_map.h:32
Definition ordered_set.h:32
Definition safe_vector.h:27
Definition phv_fields.h:154
Definition phv_fields.h:898
Definition phv_fields.h:1095
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
The namespace encapsulating PHV-related stuff.
Definition gateway.h:32
Definition input_xbar.h:191
Definition match.h:36