P4C
The P4 Compiler
Loading...
Searching...
No Matches
utils_v2.h
1
19#ifndef BF_P4C_PHV_V2_UTILS_V2_H_
20#define 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 "bf-p4c/phv/phv.h"
29#include "bf-p4c/phv/phv_fields.h"
30#include "bf-p4c/phv/utils/utils.h"
31#include "bf-p4c/phv/v2/tx_score.h"
32#include "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
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;
140 cstring pretty_print(cstring prefix, const SuperCluster *sc) const;
143 bool ok(const AlignedCluster *aligned, int start) const {
144 return !cluster_starts.count(aligned) || cluster_starts.at(aligned) == start;
145 }
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
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
201 std::ostream *tracer = nullptr;
202
203 private:
204 static constexpr const char *tab_table[] = {
205 "", " ", " ", " ", " ", " ", " ", " ", " ",
206 };
207 static constexpr int max_log_level = sizeof(tab_table) / sizeof(tab_table[0]) - 1;
208
209 public:
210 ScoreContext() {}
211
212 const SuperCluster *sc() const {
213 BUG_CHECK(sc_i, "sc not added in ctx.");
214 return sc_i;
215 }
216 ScoreContext with_sc(const SuperCluster *sc) const {
217 auto cloned = *this;
218 cloned.sc_i = sc;
219 return cloned;
220 }
221
222 const ContainerGroup *cont_group() const {
223 BUG_CHECK(cont_group_i, "container group not added in ctx.");
224 return cont_group_i;
225 }
226 ScoreContext with_cont_group(const ContainerGroup *cont_group) const {
227 auto cloned = *this;
228 cloned.cont_group_i = cont_group;
229 return cloned;
230 }
231
232 const std::vector<const SuperCluster::SliceList *> *alloc_order() const {
233 BUG_CHECK(alloc_order_i, "sl alloc order not added in ctx.");
234 return alloc_order_i;
235 }
236 ScoreContext with_alloc_order(const std::vector<const SuperCluster::SliceList *> *order) const {
237 auto cloned = *this;
238 cloned.alloc_order_i = order;
239 return cloned;
240 }
241
242 const ScAllocAlignment *alloc_alignment() const {
243 BUG_CHECK(alloc_alignment_i, "alloc alignment not added in ctx.");
244 return alloc_alignment_i;
245 }
246 ScoreContext with_alloc_alignment(const ScAllocAlignment *alignment) const {
247 auto cloned = *this;
248 cloned.alloc_alignment_i = alignment;
249 return cloned;
250 }
251
252 const TxScoreMaker *score() const {
253 BUG_CHECK(score_i, "score not added in ctx.");
254 return score_i;
255 }
256 ScoreContext with_score(const TxScoreMaker *score) const {
257 auto cloned = *this;
258 cloned.score_i = score;
259 return cloned;
260 }
261
262 int t() const { return t_i; }
263 std::string t_tabs() const { return tab_table[t_i]; }
264 ScoreContext with_t(int t) const {
265 if (t > max_log_level) t = max_log_level;
266 auto cloned = *this;
267 cloned.t_i = t;
268 return cloned;
269 }
270
271 const SearchConfig *search_config() const { return search_config_i; }
272 ScoreContext with_search_config(const SearchConfig *c) const {
273 auto cloned = *this;
274 cloned.search_config_i = c;
275 return cloned;
276 }
277};
278
279} // namespace v2
280} // namespace PHV
281
282#endif /* 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
ScoreContext is the allocation context that is updated and passed down during allocation.
Definition utils_v2.h:178
TxScoreMaker is the interface of the factory class of TxScore.
Definition tx_score.h:35
The namespace encapsulating PHV-related stuff.
Definition gateway.h:32
Size
all possible PHV container sizes in BFN devices
Definition phv.h:110
ErrorCode
ErrorCode is the specific reason of failure of function invoke.
Definition backends/tofino/bf-p4c/phv/error.h:27
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:120
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