P4C
The P4 Compiler
Loading...
Searching...
No Matches
add_initialization.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_PHV_ADD_INITIALIZATION_H_
20#define BACKENDS_TOFINO_BF_P4C_PHV_ADD_INITIALIZATION_H_
21
22#include "backends/tofino/bf-p4c/common/field_defuse.h"
23#include "backends/tofino/bf-p4c/common/map_tables_to_actions.h"
24#include "backends/tofino/bf-p4c/mau/add_always_run.h"
25#include "backends/tofino/bf-p4c/mau/table_dependency_graph.h"
26#include "backends/tofino/bf-p4c/mau/table_mutex.h"
27#include "backends/tofino/bf-p4c/phv/analysis/meta_live_range.h"
28#include "backends/tofino/bf-p4c/phv/finalize_stage_allocation.h"
29#include "backends/tofino/bf-p4c/phv/phv_fields.h"
30
35class MapFieldToExpr : public Inspector {
36 private:
37 const PhvInfo &phv;
39
40 profile_t init_apply(const IR::Node *root) override {
41 fieldExpressions.clear();
42 return Inspector::init_apply(root);
43 }
44
49 bool preorder(const IR::Expression *expr) override;
50
51 public:
52 explicit MapFieldToExpr(const PhvInfo &p) : phv(p) {}
53
54 const IR::Expression *getExpr(const PHV::Field *field) const {
55 BUG_CHECK(fieldExpressions.count(field->id), "Missing IR::Expression mapping of %1%",
56 field->name);
57 return fieldExpressions.at(field->id)->clone();
58 }
59
61 const IR::MAU::Instruction *generateInitInstruction(const PHV::AllocSlice &slice) const;
62
63 const IR::MAU::Instruction *generateInitInstruction(const PHV::AllocSlice &dest,
64 const PHV::AllocSlice &source) const;
65};
66
68 private:
69 const PhvInfo &phv;
70
71 // Map of action names to the set of fields that must be initialized at that action.
73
74 // Map of all fields that must be initialized (across all actions).
75 ordered_set<PHV::FieldSlice> fieldsForInit;
76
77 const IR::Node *apply_visitor(const IR::Node *root, const char * = nullptr) override;
78
79 public:
80 explicit ComputeFieldsRequiringInit(const PhvInfo &p) : phv(p) {}
81
82 // @returns the set of all fields that must be initialized across all actions.
83 const ordered_set<PHV::FieldSlice> &getSlicesRequiringInitialization() const {
84 return fieldsForInit;
85 }
86
87 // @returns the map of all actions and the metadata fields to be initialized in those actions.
89 const {
90 return actionInits;
91 }
92
93 // @returns the set of fields that must be initialized at action @act.
94 const std::vector<PHV::AllocSlice> getInitsForAction(const IR::MAU::Action *act) const {
95 std::vector<PHV::AllocSlice> empty;
96 if (!actionInits.count(act)) return empty;
97 return actionInits.at(act);
98 }
99};
100
102 private:
103 PhvInfo &phv;
104 const MapTablesToActions &tableActionsMap;
105 const MapFieldToExpr &fieldToExpr;
106 const DependencyGraph &dg;
107
110
111 void computeInitInstruction(const PHV::AllocSlice &slice, const IR::MAU::Action *act);
112
113 // Calculate the minStage for the ARA table moving a slice between
114 // normal/dark PHVs
115 // Ths will need to be updated with more detailed analysis when
116 // the initialization injection stops using the dominator analysis
117 int calcMinStage(const PHV::AllocSlice &sl_prev, const PHV::AllocSlice &sl_current,
118 int prior_max_stage, int post_min_stage, bool init_from_zero);
119
120 // Check dest and src containers if they match with containers in
121 // darkInitToARA AllocSlice's
122 bool use_same_containers(PHV::AllocSlice alloc_sl, IR::MAU::Table *&ara_tbl);
123
124 // Create new MOVE instruction and place it into new Action which
125 // is added into new AlwaysRunAction Table. Use prior and post
126 // Tables from @alloc_sl to set the minStage for the new AlwaysRunAction Table
127 void createAlwaysRunTable(PHV::AllocSlice &alloc_sl);
128
129 cstring getKey(const IR::MAU::Table *tbl, const IR::MAU::Action *act) const {
130 return (tbl->name + "." + act->name);
131 }
132
133 profile_t init_apply(const IR::Node *root) override;
134 void end_apply() override;
135
136 public:
137 // Increasing integer value used in AlwaysRunAction Table name
138 static int ARA_table_id;
140 const MapFieldToExpr &e, const DependencyGraph &g)
141 : phv(p), tableActionsMap(m), fieldToExpr(e), dg(g) {}
142
143 const ordered_set<const IR::MAU::Primitive *> getInitializationInstructions(
144 const IR::MAU::Table *tbl, const IR::MAU::Action *act) const;
145};
146
160 private:
164
165 PhvInfo &phv;
166 const DependencyGraph &dg;
167 const MapTablesToActions &actionsMap;
168 const ComputeFieldsRequiringInit &fieldsForInit;
169 const FieldDefUse &defuse;
170 const MetadataLiveRange &liverange;
171 const TablesMutuallyExclusive &tableMutex;
172
173 ordered_set<cstring> initTableNames;
174
175 profile_t init_apply(const IR::Node *root) override;
176 void end_apply() override;
177
178 // @fields: Map of a field being initialized to the overlapping fields.
179 // @inits: Map of a field being initialized to the tables where the initialization is inserted.
180 // Note down all the dependencies that would be induced because of the initialization.
181 void noteDependencies(
184
185 void addDepsForDarkInitialization();
186 void addDepsForSetsOfAllocSlices(const std::vector<PHV::AllocSlice> &alloc_slices,
187 const StageFieldUse &fieldWrites,
188 const StageFieldUse &fieldReads, bool checkBitsOverlap = true);
189 // Given a @min_stage, a @max_stage, and a map @uses of the uses/defs of slice @alloc,
190 // populate @tables with all the tables using that slice between the min and max stage
191 // range.
192 void accountUses(int min_stage, int max_stage, const PHV::AllocSlice &alloc,
193 const StageFieldUse &uses, ordered_set<const IR::MAU::Table *> &tables) const;
194 void summarizeDarkInits(StageFieldUse &fieldWrites, StageFieldUse &fieldReads);
195
196 public:
198 const ComputeFieldsRequiringInit &i, const FieldDefUse &d,
200 : phv(p), dg(g), actionsMap(a), fieldsForInit(i), defuse(d), liverange(r), tableMutex(m) {}
201
202 bool isInitTable(const IR::MAU::Table *tbl) const { return initTableNames.count(tbl->name); }
203};
204
210 const ComputeDependencies &dep;
211
212 IR::Node *preorder(IR::MAU::Table *tbl) override {
213 if (dep.isInitTable(tbl)) {
214 tbl->has_dark_init = true;
215 LOG2("\tDark initialization table: " << tbl->name);
216 }
217 return tbl;
218 }
219
220 public:
221 explicit MarkDarkInitTables(const ComputeDependencies &d) : dep(d) {}
222};
223
229 private:
230 TablesMutuallyExclusive tableMutex;
231 MapTablesToActions actionsMap;
232 MapFieldToExpr fieldToExpr;
235 ComputeDarkInitialization computeDarkInit;
236
237 public:
239 const MetadataLiveRange &r);
240};
241
242#endif /* BACKENDS_TOFINO_BF_P4C_PHV_ADD_INITIALIZATION_H_ */
Definition add_initialization.h:228
Definition add_initialization.h:101
Definition add_initialization.h:159
Definition add_initialization.h:67
Definition add_initialization.h:35
const IR::MAU::Instruction * generateInitInstruction(const PHV::AllocSlice &slice) const
Definition add_initialization.cpp:31
Definition map_tables_to_actions.h:29
Definition add_initialization.h:209
Definition meta_live_range.h:41
Definition node.h:94
Definition visitor.h:400
Definition ir/pass_manager.h:40
Definition visitor.h:424
Definition visitor.h:78
Definition visitor.h:75
Definition cstring.h:85
Definition ordered_map.h:32
Definition ordered_set.h:32
Definition slice_alloc.h:136
Definition phv_fields.h:154
int id
Unique field ID.
Definition phv_fields.h:164
cstring name
Definition phv_fields.h:161
Definition phv_fields.h:1095
Definition mau/table_mutex.h:110
Definition field_defuse.h:77
Definition table_dependency_graph.h:52