72 const MapFieldToExpr &fieldToExpr;
73 std::set<PHV::FieldRange> &mauInitFields;
74 std::vector<IR::Slice *> mauInitSlices;
75 bool ingress_inits =
false, egress_inits =
false;
79 if (!check_container)
return true;
88 if (alloc_sl1.container() == alloc_sl2.container()) {
99 auto rv = Transform::init_apply(root);
101 LOG3(
"AddInitTable: init_apply");
102 mauInitSlices.clear();
104 std::set<PHV::FieldRange> removeInitFields;
105 for (
const auto &mau_init : mauInitFields) {
107 if (
const auto *fld1 = phv.field(mau_init.field_name)) {
110 for (
auto &f_range : mau_init.conflicts) {
111 const auto *fld2 = phv.field(f_range.field_name);
114 ingress_inits = (fld1->gress == INGRESS);
115 egress_inits = (fld1->gress == EGRESS);
116 auto slice =
new IR::Slice(fieldToExpr.getExpr(fld1), mau_init.range.hi,
118 mauInitSlices.push_back(slice);
119 LOG4(
"\t Added slice: " << slice <<
" for " << mau_init.field_name <<
" "
122 removeInitFields.insert(mau_init);
128 for (
auto remInit : removeInitFields) {
129 mauInitFields.erase(remInit);
130 LOG5(
"\t Removing mau init " << remInit.field_name <<
" " << remInit.range);
136 IR::MAU::TableSeq *preorder(IR::MAU::TableSeq *tSeq)
override {
137 LOG3(
"AddInitTable: preorder TableSeq " << tSeq);
141 const IR::MAU::Table *init_table =
nullptr;
143 if (tSeq->tables.empty()) {
144 LOG3(
"AddInitTable: empty TableSeq - skipping");
147 LOG3(
"\t Table: " << tSeq->tables.front()->name);
148 seq_gress = tSeq->tables.front()->gress;
149 tableName =
"__mau_inits_table__" + toString(seq_gress);
150 actionName =
"__mau_inits_action__" + toString(seq_gress);
151 for (
auto *tbl : tSeq->tables) {
152 if (tbl->name == tableName) {
154 BUG_CHECK(tbl->actions.count(actionName),
"Table %1% does not have action %2%?",
155 tableName, actionName);
161 const IR::MAU::Table *parent_table =
nullptr;
162 parent_table = findContext<IR::MAU::Table>();
163 BUG_CHECK(!parent_table,
"This Table Sequence has parent table %1% ?", parent_table->name);
167 if (((seq_gress == INGRESS) && !ingress_inits) ||
168 ((seq_gress == EGRESS) && !egress_inits)) {
169 LOG3(
"AddInitTable: Table Sequence of gress "
170 << seq_gress <<
" without fields of that gress requiring init - nothing to do");
182 if (init_table)
return tSeq;
185 auto createDummyTableAndAction = [&]() -> std::pair<IR::MAU::Table *, IR::MAU::Action *> {
186 LOG3(
"AddInitTable: " <<
"new table name " << tableName);
187 auto table =
new IR::MAU::Table(tableName, seq_gress);
188 table->gress = seq_gress;
189 auto action =
new IR::MAU::Action(actionName);
190 table->match_table =
new IR::P4Table(tableName.c_str(),
new IR::TableProperties());
191 table->is_compiler_generated =
true;
192 action->init_default =
true;
193 action->default_allowed =
true;
195 for (
auto &slice : mauInitSlices) {
196 auto type = IR::Type_Bits::get(slice->getH() - slice->getL() + 1);
197 auto instr =
new IR::MAU::Instruction(
"set"_cs, {slice,
new IR::Constant(type, 0)});
198 LOG3(
"AddInitTable: " <<
"adding " << instr);
199 action->action.push_back(instr);
202 table->actions[action->name] = action;
204 return {table, action};
208 auto [table, action] = createDummyTableAndAction();
210 tSeq->tables.insert(tSeq->tables.begin(), table);
211 LOG3(
"AddInitTable: the first tableSeq now looks like this " << tSeq);
217 std::set<PHV::FieldRange> &to_be_inited,
bool container_info)
220 mauInitFields(to_be_inited),
221 check_container(container_info) {}