P4C
The P4 Compiler
Loading...
Searching...
No Matches
utils_v2.h
1
18
19#ifndef BACKENDS_TOFINO_BF_P4C_PHV_V2_UTILS_V2_H_
20#define BACKENDS_TOFINO_BF_P4C_PHV_V2_UTILS_V2_H_
21
22#include <iterator>
23#include <optional>
24#include <ostream>
25#include <sstream>
26#include <vector>
27
28#include "backends/tofino/bf-p4c/phv/phv.h"
29#include "backends/tofino/bf-p4c/phv/phv_fields.h"
30#include "backends/tofino/bf-p4c/phv/utils/utils.h"
31#include "backends/tofino/bf-p4c/phv/v2/tx_score.h"
32#include "backends/tofino/bf-p4c/phv/v2/types.h"
33#include "lib/cstring.h"
34#include "lib/exceptions.h"
35
36namespace PHV {
37namespace v2 {
38
41enum class ErrorCode {
45 NOT_ENOUGH_SPACE,
53 ACTION_CANNOT_BE_SYNTHESIZED,
55 CONTAINER_TYPE_MISMATCH,
57 CONTAINER_GRESS_MISMATCH,
59 CONTAINER_PARSER_WRITE_MODE_MISMATCH,
61 CONTAINER_PARSER_PACKING_INVALID,
63 FIELD_MAX_CONTAINER_BYTES_EXCEEDED,
65 ALIGNED_CLUSTER_NO_VALID_START,
67 ALIGNED_CLUSTER_CANNOT_BE_ALLOCATED,
69 NO_VALID_CONTAINER_SIZE,
71 NO_VALID_SC_ALLOC_ALIGNMENT,
73 WIDE_ARITH_ALLOC_FAILED,
75 NO_SLICING_FOUND,
78 INVALID_ALLOC_FOUND_BY_COPACKER,
82 CANNOT_APPLY_REQUIRED_COPACK_HINTS,
83};
84
86cstring to_str(const ErrorCode &e);
87
92struct AllocError {
93 ErrorCode code;
94 cstring msg;
97 const std::vector<AllocSlice> *invalid_packing = nullptr;
103 explicit AllocError(ErrorCode code) : code(code), msg(cstring::empty) {}
104 AllocError(ErrorCode code, cstring msg) : code(code), msg(msg) {}
105 std::string str() const;
106};
107
108template <class T>
109AllocError &operator<<(AllocError &e, const T &v) {
110 std::stringstream ss;
111 ss << v;
112 e.msg += ss.str();
113 return e;
114}
115
116std::ostream &operator<<(std::ostream &out, const AllocError &e);
117
120struct AllocResult {
121 const AllocError *err = nullptr;
122 std::optional<PHV::Transaction> tx;
123 explicit AllocResult(const AllocError *err) : err(err) {}
124 explicit AllocResult(const Transaction &tx) : tx(tx) {}
125 bool ok() const { return err == nullptr; }
126 std::string err_str() const;
127 std::string tx_str(cstring prefix = ""_cs) const;
128 static std::string pretty_print_tx(const PHV::Transaction &tx, cstring prefix = cstring::empty);
129 bool operator==(const AllocResult &other) const;
130};
131
138 std::optional<ScAllocAlignment> merge(const ScAllocAlignment &other) const;
143 bool ok(const AlignedCluster *aligned, int start) const {
144 return !cluster_starts.count(aligned) || cluster_starts.at(aligned) == start;
145 }
146
147 bool empty() const { return cluster_starts.empty(); }
148};
149
155std::vector<ScAllocAlignment> make_sc_alloc_alignment(
156 const SuperCluster *sc, const PHV::Size width, const int max_n,
157 const std::list<const SuperCluster::SliceList *> *sl_order = nullptr);
158
176
178class ScoreContext {
180 const SuperCluster *sc_i = nullptr;
181
183 const ContainerGroup *cont_group_i = nullptr;
184
186 const std::vector<const SuperCluster::SliceList *> *alloc_order_i = {};
187
189 const ScAllocAlignment *alloc_alignment_i = nullptr;
190
192 const TxScoreMaker *score_i = nullptr;
193
195 int t_i = 0;
196
198 const SearchConfig *search_config_i = new SearchConfig();
199
200 private:
201 static constexpr const char *tab_table[] = {
202 "", " ", " ", " ", " ", " ", " ", " ", " ",
203 };
204 static constexpr int max_log_level = sizeof(tab_table) / sizeof(tab_table[0]) - 1;
205
206 public:
207 ScoreContext() {}
208
209 const SuperCluster *sc() const {
210 BUG_CHECK(sc_i, "sc not added in ctx.");
211 return sc_i;
212 }
213 ScoreContext with_sc(const SuperCluster *sc) const {
214 auto cloned = *this;
215 cloned.sc_i = sc;
216 return cloned;
217 }
218
219 const ContainerGroup *cont_group() const {
220 BUG_CHECK(cont_group_i, "container group not added in ctx.");
221 return cont_group_i;
222 }
223 ScoreContext with_cont_group(const ContainerGroup *cont_group) const {
224 auto cloned = *this;
225 cloned.cont_group_i = cont_group;
226 return cloned;
227 }
228
229 const std::vector<const SuperCluster::SliceList *> *alloc_order() const {
230 BUG_CHECK(alloc_order_i, "sl alloc order not added in ctx.");
231 return alloc_order_i;
232 }
233 ScoreContext with_alloc_order(const std::vector<const SuperCluster::SliceList *> *order) const {
234 auto cloned = *this;
235 cloned.alloc_order_i = order;
236 return cloned;
237 }
238
239 const ScAllocAlignment *alloc_alignment() const {
240 BUG_CHECK(alloc_alignment_i, "alloc alignment not added in ctx.");
241 return alloc_alignment_i;
242 }
243 ScoreContext with_alloc_alignment(const ScAllocAlignment *alignment) const {
244 auto cloned = *this;
245 cloned.alloc_alignment_i = alignment;
246 return cloned;
247 }
248
249 const TxScoreMaker *score() const {
250 BUG_CHECK(score_i, "score not added in ctx.");
251 return score_i;
252 }
253 ScoreContext with_score(const TxScoreMaker *score) const {
254 auto cloned = *this;
255 cloned.score_i = score;
256 return cloned;
257 }
258
259 int t() const { return t_i; }
260 std::string t_tabs() const { return tab_table[t_i]; }
261 ScoreContext with_t(int t) const {
262 if (t > max_log_level) t = max_log_level;
263 auto cloned = *this;
264 cloned.t_i = t;
265 return cloned;
266 }
267
268 const SearchConfig *search_config() const { return search_config_i; }
269 ScoreContext with_search_config(const SearchConfig *c) const {
270 auto cloned = *this;
271 cloned.search_config_i = c;
272 return cloned;
273 }
274};
275
276} // namespace v2
277} // namespace PHV
278
279#endif /* BACKENDS_TOFINO_BF_P4C_PHV_V2_UTILS_V2_H_ */
Definition cstring.h:85
Definition ordered_map.h:32
Definition ordered_set.h:32
Definition phv/utils/utils.h:789
Definition phv/utils/utils.h:37
Definition phv/utils/utils.h:1049
Definition phv/utils/utils.h:561
TxScoreMaker is the interface of the factory class of TxScore.
Definition tx_score.h:35
The namespace encapsulating PHV-related stuff.
Definition bf-p4c/mau/gateway.h:32
Size
all possible PHV container sizes in BFN devices
Definition bf-p4c/phv/phv.h:110
Definition utils_v2.h:92
const ordered_set< const SuperCluster::SliceList * > * reslice_required
Definition utils_v2.h:102
const std::vector< AllocSlice > * invalid_packing
Definition utils_v2.h:97
Definition utils_v2.h:134
std::optional< ScAllocAlignment > merge(const ScAllocAlignment &other) const
Definition utils_v2.cpp:163
cstring pretty_print(cstring prefix, const SuperCluster *sc) const
Definition utils_v2.cpp:237
ordered_map< const AlignedCluster *, int > cluster_starts
a cluster_alignment maps aligned cluster to start bit location in a container.
Definition utils_v2.h:136
bool empty() const
Definition utils_v2.h:147
bool ok(const AlignedCluster *aligned, int start) const
Definition utils_v2.h:143
a collection of allocation configurations that balances speed and performance of allocation.
Definition utils_v2.h:160
bool stop_first_succ_empty_normal_container
Definition utils_v2.h:171
int n_best_of_sc_alloc
Allocator will find the best score out of this number of allocation for a super cluster.
Definition utils_v2.h:167
bool try_byte_aligned_starts_first_for_table_keys
For table match key fields, try byte-aligned container starts first.
Definition utils_v2.h:174
int n_dfs_steps_sc_alloc
Definition utils_v2.h:164
Definition common/field_defuse.cpp:590
Definition register_reference.h:28