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