P4C
The P4 Compiler
Loading...
Searching...
No Matches
resource_estimate.h
1
19#ifndef BF_P4C_MAU_RESOURCE_ESTIMATE_H_
20#define BF_P4C_MAU_RESOURCE_ESTIMATE_H_
21
22#include "bf-p4c/mau/attached_entries.h"
23#include "bf-p4c/mau/table_layout.h"
24#include "lib/algorithm.h"
25#include "lib/safe_vector.h"
26
27using namespace P4;
28
30 static constexpr int MIN_WAYS = 1;
31 static constexpr int MAX_WAYS = 8;
32 static constexpr int MAX_METER_ALUS = 4;
33 static constexpr int MAX_STATS_ALUS = 4;
34 static constexpr int MAX_LOCAL_TINDS = 16;
35 // FIXME: This is a quick workaround that will need to change as the tables need to expand
36 static constexpr int MAX_DLEFT_HASH_SIZE = 23;
37 static constexpr int COMPILER_DEFAULT_SELECTOR_POOLS = 4;
38 static constexpr int SINGLE_RAMLINE_POOL_SIZE = 120;
39 static constexpr int MAX_MOD = 31;
40 static constexpr int MAX_MOD_SHIFT = 5;
41 static constexpr int MAX_POOL_RAMLINES = MAX_MOD << MAX_MOD_SHIFT;
42 static constexpr int MOD_INPUT_BITS = 10;
43
44 int logical_ids = 0;
45 int srams = 0;
46 int tcams = 0;
47 int local_tinds = 0;
48 int maprams = 0;
49 int exact_ixbar_bytes = 0;
50 int ternary_ixbar_groups = 0;
51 int meter_alus = 0;
52 int stats_alus = 0;
53 // Number of hash bits masked out by the @hash_mask() annotation.
54 int hash_bits_masked = 0;
55
56 // This struct is a confusing mix. The fields above the comment are the real
57 // "stage use estimate"; the estimate of resources needed for one or more table in
58 // a stage. The fields below this comment are really the "table layout"; the choices
59 // about how to split and lay out a table in memory that influences the resources
60 // needed. These really are two not-really related things and should be in different
61 // objects. Their combination here makes a lot of things confusing. Some places
62 // use this object really as just the estimate and ignore the table layout related
63 // fields. Other places depend greatly on the table layout.
64
65 ActionData::FormatType_t format_type;
66 safe_vector<LayoutOption> layout_options;
68 MeterALU::Format::Use meter_format;
69 size_t preferred_index = 0; // into layout_options
71 StageUseEstimate &operator+=(const StageUseEstimate &a) {
72 logical_ids += a.logical_ids;
73 srams += a.srams;
74 tcams += a.tcams;
75 local_tinds += a.local_tinds;
76 maprams += a.maprams;
77 exact_ixbar_bytes += a.exact_ixbar_bytes;
78 ternary_ixbar_groups += a.ternary_ixbar_groups;
79 meter_alus += a.meter_alus;
80 stats_alus += a.stats_alus;
81 return *this;
82 }
83 StageUseEstimate(const IR::MAU::Table *, int &, attached_entries_t &, LayoutChoices *lc,
84 bool prev_placed, bool gateway_attached, bool disable_split, PhvInfo &phv);
85
86 StageUseEstimate operator+(const StageUseEstimate &a) const {
87 StageUseEstimate rv = *this;
88 rv += a;
89 return rv;
90 }
91 static StageUseEstimate max() {
93 rv.logical_ids = StageUse::MAX_LOGICAL_IDS;
94 rv.srams = StageUse::MAX_SRAMS;
95 rv.tcams = StageUse::MAX_TCAMS;
96 rv.local_tinds = MAX_LOCAL_TINDS;
97 rv.maprams = StageUse::MAX_MAPRAMS;
98 rv.exact_ixbar_bytes = StageUse::MAX_IXBAR_BYTES;
99 rv.ternary_ixbar_groups = StageUse::MAX_TERNARY_GROUPS;
100 rv.meter_alus = MAX_METER_ALUS;
101 rv.stats_alus = MAX_STATS_ALUS;
102 return rv;
103 }
104 bool operator<=(const StageUseEstimate &a) {
105 return logical_ids <= a.logical_ids && srams <= a.srams && tcams <= a.tcams &&
106 maprams <= a.maprams && exact_ixbar_bytes <= a.exact_ixbar_bytes &&
107 ternary_ixbar_groups <= a.ternary_ixbar_groups && meter_alus < a.meter_alus &&
108 stats_alus <= a.stats_alus && local_tinds <= a.local_tinds;
109 }
110 void clear() {
111 logical_ids = 0;
112 srams = 0;
113 tcams = 0;
114 maprams = 0;
115 exact_ixbar_bytes = 0;
116 ternary_ixbar_groups = 0;
117 meter_alus = 0;
118 stats_alus = 0;
119 local_tinds = 0;
120 }
121 cstring ran_out() const;
122
123 void options_to_ways(const IR::MAU::Table *tbl, int entries);
124 void options_to_rams(const IR::MAU::Table *tbl, const attached_entries_t &att_entries);
125 void select_best_option(const IR::MAU::Table *tbl);
126 void options_to_ternary_entries(const IR::MAU::Table *tbl, int entries);
127 void select_best_option_ternary();
128 void options_to_atcam_entries(const IR::MAU::Table *tbl, int entries);
129 void options_to_dleft_entries(const IR::MAU::Table *tbl, const attached_entries_t &att_entries);
130 void calculate_attached_rams(const IR::MAU::Table *tbl, const attached_entries_t &att_entries,
131 LayoutOption *lo);
132 void fill_estimate_from_option(int &entries);
133 void remove_invalid_option() {
134 erase_if(layout_options, [](const LayoutOption &lo) { return lo.entries == 0; });
135 }
136 const LayoutOption *preferred() const {
137 if (layout_options.empty())
138 return nullptr;
139 else
140 return &layout_options[preferred_index];
141 }
142
143 const ActionData::Format::Use *preferred_action_format() const {
144 auto option = preferred();
145 if (option == nullptr) return nullptr;
146 return &action_formats[option->action_format_index];
147 }
148
149 const MeterALU::Format::Use *preferred_meter_format() const { return &meter_format; }
150
151 void determine_initial_layout_option(const IR::MAU::Table *tbl, int &entries,
153 bool adjust_choices(const IR::MAU::Table *tbl, int &entries, attached_entries_t &);
154
155 bool calculate_for_leftover_srams(const IR::MAU::Table *tbl, int &srams_left, int &entries,
157 void calculate_for_leftover_tcams(const IR::MAU::Table *tbl, int srams_left, int tcams_left,
158 int &entries, attached_entries_t &);
159 void calculate_for_leftover_atcams(const IR::MAU::Table *tbl, int srams_left, int &entries,
161 void shrink_preferred_srams_lo(const IR::MAU::Table *tbl, int &entries,
162 attached_entries_t &attached_entries);
163 void shrink_preferred_tcams_lo(const IR::MAU::Table *tbl, int &entries,
164 attached_entries_t &attached_entries);
165 void shrink_preferred_atcams_lo(const IR::MAU::Table *tbl, int &entries,
166 attached_entries_t &attached_entries);
167 void known_srams_needed(const IR::MAU::Table *tbl, const attached_entries_t &,
168 LayoutOption *lo);
169 void unknown_srams_needed(const IR::MAU::Table *tbl, LayoutOption *lo, int srams_left);
170 void unknown_tcams_needed(const IR::MAU::Table *tbl, LayoutOption *lo, int tcams_left,
171 int srams_left);
172 void unknown_atcams_needed(const IR::MAU::Table *tbl, LayoutOption *lo, int srams_left);
173 bool can_be_identity_hash(const IR::MAU::Table *tbl, LayoutOption *lo, int &calculated_depth);
174 void calculate_way_sizes(const IR::MAU::Table *tbl, LayoutOption *lo, int &calculated_depth);
175 void calculate_partition_sizes(const IR::MAU::Table *tbl, LayoutOption *lo, int ram_depth);
176 bool ways_provided(const IR::MAU::Table *tbl, LayoutOption *lo, int &calculated_depth);
177 void srams_left_best_option(int srams_left);
178 void max_entries_best_option();
179 void tcams_left_best_option();
180 struct RAM_counter {
181 int per_word;
182 int width;
183 bool need_srams;
184 bool need_maprams;
185 RAM_counter() : per_word(0), width(0), need_srams(false), need_maprams(false) {}
186 RAM_counter(int p, int w, bool ns, bool nm)
187 : per_word(p), width(w), need_srams(ns), need_maprams(nm) {}
188 };
189 void calculate_per_row_vector(safe_vector<RAM_counter> &per_word_and_width,
190 const IR::MAU::Table *tbl, LayoutOption *lo);
191
192 int stages_required() const;
193};
194
195int CounterPerWord(const IR::MAU::Counter *ctr);
196int CounterWidth(const IR::MAU::Counter *ctr);
197int RegisterPerWord(const IR::MAU::StatefulAlu *reg);
198int ActionDataPerWord(const IR::MAU::Table::Layout *layout, int *width);
199int ActionDataHuffmanVPNBits(const IR::MAU::Table::Layout *layout);
200int ActionDataVPNStartPosition(const IR::MAU::Table::Layout *layout);
201int ActionDataVPNIncrement(const IR::MAU::Table::Layout *layout);
202int LocalTernaryIndirectPerWord(const IR::MAU::Table::Layout *layout, const IR::MAU::Table *tbl);
203int TernaryIndirectPerWord(const IR::MAU::Table::Layout *layout, const IR::MAU::Table *tbl);
204int IdleTimePerWord(const IR::MAU::IdleTime *idletime);
205int SelectorRAMLinesPerEntry(const IR::MAU::Selector *sel);
206int SelectorModBits(const IR::MAU::Selector *sel);
207int SelectorShiftBits(const IR::MAU::Selector *sel);
208int SelectorHashModBits(const IR::MAU::Selector *sel);
209int SelectorLengthShiftBits(const IR::MAU::Selector *sel);
210int SelectorLengthBits(const IR::MAU::Selector *sel);
211
213 static constexpr int MULTIRANGE_DISTRIBUTION_LIMIT = 8;
214 static constexpr int RANGE_ENTRY_PERCENTAGE = 25;
215
216 private:
217 const PhvInfo &phv;
218 int table_entries;
219 int total_TCAM_lines = 0;
220 int max_entry_TCAM_lines = 1;
221
222 bool preorder(const IR::MAU::TableSeq *) override { return false; }
223 bool preorder(const IR::MAU::Action *) override { return false; }
224 bool preorder(const IR::MAU::BackendAttached *) override { return false; }
225 bool preorder(const IR::MAU::TableKey *) override;
226 void postorder(const IR::MAU::Table *) override;
227
228 public:
229 int TCAM_lines() { return total_TCAM_lines; }
230 RangeEntries(const PhvInfo &p, int te) : phv(p), table_entries(te) {}
231};
232
233std::ostream &operator<<(std::ostream &, const StageUseEstimate &);
234
235#endif /* BF_P4C_MAU_RESOURCE_ESTIMATE_H_ */
Definition attached_info.h:32
Definition table_layout.h:83
Definition table_layout.h:34
Definition mau_visitor.h:29
Definition cstring.h:85
Definition safe_vector.h:27
Definition phv_fields.h:1095
Definition resource_estimate.h:212
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition action_format.h:979
Definition attached_output.h:42
Definition resource_estimate.h:180
Definition resource_estimate.h:29
void calculate_way_sizes(const IR::MAU::Table *tbl, LayoutOption *lo, int &calculated_depth)
Definition resource_estimate.cpp:571
void options_to_dleft_entries(const IR::MAU::Table *tbl, const attached_entries_t &att_entries)
Definition resource_estimate.cpp:773
void calculate_partition_sizes(const IR::MAU::Table *tbl, LayoutOption *lo, int ram_depth)
Definition resource_estimate.cpp:727
void options_to_atcam_entries(const IR::MAU::Table *tbl, int entries)
Definition resource_estimate.cpp:750
void unknown_atcams_needed(const IR::MAU::Table *tbl, LayoutOption *lo, int srams_left)
Definition resource_estimate.cpp:1445
bool ways_provided(const IR::MAU::Table *tbl, LayoutOption *lo, int &calculated_depth)
Definition resource_estimate.cpp:383
bool can_be_identity_hash(const IR::MAU::Table *tbl, LayoutOption *lo, int &calculated_depth)
Definition resource_estimate.cpp:482