71 const MapFieldToExpr &fieldToExpr;
72 std::set<PHV::FieldRange> &mauInitFields;
73 std::vector<IR::Slice *> mauInitSlices;
74 bool ingress_inits =
false, egress_inits =
false;
78 if (!check_container)
return true;
87 if (alloc_sl1.container() == alloc_sl2.container()) {
98 auto rv = Transform::init_apply(root);
100 LOG3(
"AddInitTable: init_apply");
101 mauInitSlices.clear();
103 std::set<PHV::FieldRange> removeInitFields;
104 for (
const auto &mau_init : mauInitFields) {
106 if (
const auto *fld1 = phv.field(mau_init.field_name)) {
109 for (
auto &f_range : mau_init.conflicts) {
110 const auto *fld2 = phv.field(f_range.field_name);
113 ingress_inits = (fld1->gress == INGRESS);
114 egress_inits = (fld1->gress == EGRESS);
115 auto slice =
new IR::Slice(fieldToExpr.getExpr(fld1), mau_init.range.hi,
117 mauInitSlices.push_back(slice);
118 LOG4(
"\t Added slice: " << slice <<
" for " << mau_init.field_name <<
" "
121 removeInitFields.insert(mau_init);
127 for (
auto remInit : removeInitFields) {
128 mauInitFields.erase(remInit);
129 LOG5(
"\t Removing mau init " << remInit.field_name <<
" " << remInit.range);
135 IR::MAU::TableSeq *preorder(IR::MAU::TableSeq *tSeq)
override {
136 LOG3(
"AddInitTable: preorder TableSeq " << tSeq);
140 const IR::MAU::Table *init_table =
nullptr;
142 if (tSeq->tables.empty()) {
143 LOG3(
"AddInitTable: empty TableSeq - skipping");
146 LOG3(
"\t Table: " << tSeq->tables.front()->name);
147 seq_gress = tSeq->tables.front()->gress;
148 tableName =
"__mau_inits_table__" + toString(seq_gress);
149 actionName =
"__mau_inits_action__" + toString(seq_gress);
150 for (
auto *tbl : tSeq->tables) {
151 if (tbl->name == tableName) {
153 BUG_CHECK(tbl->actions.count(actionName),
"Table %1% does not have action %2%?",
154 tableName, actionName);
160 const IR::MAU::Table *parent_table =
nullptr;
161 parent_table = findContext<IR::MAU::Table>();
162 BUG_CHECK(!parent_table,
"This Table Sequence has parent table %1% ?", parent_table->name);
166 if (((seq_gress == INGRESS) && !ingress_inits) ||
167 ((seq_gress == EGRESS) && !egress_inits)) {
168 LOG3(
"AddInitTable: Table Sequence of gress "
169 << seq_gress <<
" without fields of that gress requiring init - nothing to do");
181 if (init_table)
return tSeq;
184 auto createDummyTableAndAction = [&]() -> std::pair<IR::MAU::Table *, IR::MAU::Action *> {
185 LOG3(
"AddInitTable: " <<
"new table name " << tableName);
186 auto table =
new IR::MAU::Table(tableName, seq_gress);
187 table->gress = seq_gress;
188 auto action =
new IR::MAU::Action(actionName);
189 table->match_table =
new IR::P4Table(tableName.c_str(),
new IR::TableProperties());
190 table->is_compiler_generated =
true;
191 action->init_default =
true;
192 action->default_allowed =
true;
194 for (
auto &slice : mauInitSlices) {
195 auto type = IR::Type_Bits::get(slice->getH() - slice->getL() + 1);
196 auto instr =
new IR::MAU::Instruction(
"set"_cs, {slice,
new IR::Constant(type, 0)});
197 LOG3(
"AddInitTable: " <<
"adding " << instr);
198 action->action.push_back(instr);
201 table->actions[action->name] = action;
203 return {table, action};
207 auto [table, action] = createDummyTableAndAction();
209 tSeq->tables.insert(tSeq->tables.begin(), table);
210 LOG3(
"AddInitTable: the first tableSeq now looks like this " << tSeq);
215 explicit AddInitTable(
PhvInfo &p, MapFieldToExpr &f2exp,
216 std::set<PHV::FieldRange> &to_be_inited,
bool container_info)
219 mauInitFields(to_be_inited),
220 check_container(container_info) {}