19#ifndef BF_P4C_MAU_INSTRUCTION_SELECTION_H_
20#define BF_P4C_MAU_INSTRUCTION_SELECTION_H_
22#include "backends/tofino/bf-p4c/bf-p4c-options.h"
23#include "backends/tofino/bf-p4c/common/utils.h"
24#include "backends/tofino/bf-p4c/ir/tofino_write_context.h"
25#include "backends/tofino/bf-p4c/mau/mau_visitor.h"
26#include "backends/tofino/bf-p4c/mau/reduction_or.h"
27#include "backends/tofino/bf-p4c/phv/phv_fields.h"
28#include "ir/ir-generated.h"
38 bool preorder(
const IR::MAU::Primitive *prim)
override;
49 const IR::Expression *preorder(IR::Cast *)
override;
59 const IR::Expression *preorder(IR::Primitive *)
override;
77 auto rv = PassManager::init_apply(node);
78 hash_gen_injections.clear();
79 hash_dist_injections.clear();
88 std::vector<const IR::Expression *> per_prim_hge;
89 bool preorder(
const IR::MAU::StatefulAlu *)
override {
return false; }
90 bool preorder(
const IR::BFN::SignExtend *)
override;
91 bool preorder(
const IR::Concat *)
override;
92 bool preorder(
const IR::Expression *)
override;
93 bool preorder(
const IR::Constant *)
override;
94 bool preorder(
const IR::MAU::Primitive *)
override;
95 void postorder(
const IR::MAU::Primitive *)
override;
97 void check_for_symmetric(
const IR::Declaration_Instance *decl,
const IR::ListExpression *le,
99 bool isBetter(
const IR::Expression *dest,
const IR::Expression *a,
const IR::Expression *b);
102 explicit CreateHashGenExprs(
HashGenSetup &s) : self(s) {}
110 bool preorder(
const IR::MAU::StatefulAlu *)
override {
return false; }
111 bool preorder(
const IR::Expression *)
override;
122 const IR::Expression *postorder(IR::Expression *)
override;
131 {
new CreateHashGenExprs(*
this),
new ScanHashDists(*
this),
new UpdateHashDists(*
this)});
145 std::set<UniqueAttachedId> per_flow_enables;
146 std::map<UniqueAttachedId, IR::MAU::MeterType> meter_types;
150 const IR::MAU::Action *preorder(IR::MAU::Action *)
override;
151 const IR::Node *postorder(IR::MAU::Primitive *)
override;
152 const IR::MAU::Action *postorder(IR::MAU::Action *)
override;
154 void clear_action() {
156 per_flow_enables.clear();
181 IR::MAU::Action *af =
nullptr;
183 int synth_arg_num = 0;
184 const IR::MAU::TableSeq *ts =
nullptr;
188 const IR::MAU::HashDist *preorder(IR::MAU::HashDist *hd)
override {
192 const IR::GlobalRef *preorder(IR::GlobalRef *)
override;
193 const IR::MAU::TableSeq *postorder(IR::MAU::TableSeq *)
override;
194 const IR::MAU::Action *postorder(IR::MAU::Action *)
override;
195 const IR::MAU::Action *preorder(IR::MAU::Action *)
override;
196 const IR::MAU::SaluAction *preorder(IR::MAU::SaluAction *a)
override {
200 const IR::Expression *postorder(IR::Add *)
override;
201 const IR::Expression *postorder(IR::AddSat *)
override;
202 const IR::Expression *postorder(IR::Sub *)
override;
203 const IR::Expression *postorder(IR::SubSat *)
override;
204 const IR::Expression *postorder(IR::Neg *)
override;
205 const IR::Expression *postorder(IR::Shr *)
override;
206 const IR::Expression *postorder(IR::Shl *)
override;
207 const IR::Expression *postorder(IR::BAnd *)
override;
208 const IR::Expression *postorder(IR::BOr *)
override;
209 const IR::Expression *postorder(IR::BXor *)
override;
210 const IR::Expression *postorder(IR::Cmpl *)
override;
211 const IR::Expression *preorder(IR::BFN::SignExtend *)
override;
212 const IR::Expression *preorder(IR::Concat *)
override;
214 const IR::Expression *postorder(IR::Operation_Relation *)
override;
215 const IR::Expression *postorder(IR::Mux *)
override;
216 const IR::Slice *postorder(IR::Slice *)
override;
217 const IR::Expression *postorder(IR::BoolLiteral *)
override;
218 const IR::Node *postorder(IR::MAU::Primitive *)
override;
219 const IR::MAU::Instruction *postorder(IR::MAU::Instruction *i)
override {
return i; }
221 bool checkPHV(
const IR::Expression *);
222 bool checkActionBus(
const IR::Expression *e);
223 bool checkSrc1(
const IR::Expression *);
224 bool checkConst(
const IR::Expression *ex,
long &value);
225 bool equiv(
const IR::Expression *a,
const IR::Expression *b);
226 IR::Member *genIntrinsicMetadata(gress_t gress,
cstring header,
cstring field);
228 void limitWidth(
const IR::Expression *);
242 const IR::TempVar *saved_tempvar =
nullptr;
243 const IR::MAU::HashDist *saved_hashdist =
nullptr;
244 typedef std::pair<const IR::MAU::AttachedMemory *, const IR::MAU::Table *> HashDistKey;
245 typedef std::pair<const IR::MAU::StatefulCall *, const IR::MAU::Table *> StatefulCallKey;
252 typedef IR::MAU::StatefulUse use_t;
255 typedef std::pair<const IR::MAU::Synth2Port *, const IR::MAU::Table *> IndexCheck;
256 struct clear_info_t {
257 const IR::MAU::AttachedMemory *attached = 0;
259 uint32_t busy_value = 0;
268 remove_tempvars.clear();
269 copy_propagated_tempvars.clear();
270 remove_instr.clear();
271 stateful_alu_from_hash_dists.clear();
273 return PassManager::init_apply(root);
277 bool preorder(
const IR::MAU::Action *)
override;
278 bool preorder(
const IR::MAU::Instruction *)
override;
279 bool preorder(
const IR::TempVar *)
override;
280 bool preorder(
const IR::MAU::HashDist *)
override;
281 void postorder(
const IR::MAU::Instruction *)
override;
282 void postorder(
const IR::MAU::Primitive *)
override;
283 void postorder(
const IR::MAU::Table *)
override;
284 void setup_index_operand(
const IR::Expression *index_expr,
285 const IR::MAU::Synth2Port *synth2port,
const IR::MAU::Table *tbl,
286 const IR::MAU::StatefulCall *call);
287 void simpl_concat(std::vector<const IR::Expression *> &slices,
const IR::Concat *concat);
294 const IR::MAU::StatefulCall *preorder(IR::MAU::StatefulCall *)
override;
295 const IR::MAU::BackendAttached *preorder(IR::MAU::BackendAttached *ba)
override;
296 const IR::MAU::StatefulAlu *preorder(IR::MAU::StatefulAlu *salu)
override;
297 const IR::MAU::Instruction *preorder(IR::MAU::Instruction *sp)
override;
301 dontForwardChildrenBeforePreorder =
true;
305 const IR::MAU::HashDist *find_hash_dist(
const IR::Expression *expr,
306 const IR::MAU::Primitive *prim);
307 IR::MAU::HashDist *create_hash_dist(
const IR::Expression *e,
const IR::MAU::Primitive *prim);
311 addPasses({
new Scan(*
this),
new Update(*
this)});
316 bool preorder(IR::MAU::StatefulCall *sc)
override;
329 const IR::Expression *read;
332 FieldImpact(
le_bitrange db,
const IR::Expression *r) : dest_bits(db), read(r) {}
333 const IR::Expression *getSlice(
bool isSalu,
le_bitrange bits);
336 bool elem_copy_propagated =
false;
343 const IR::Node *preorder(IR::MAU::Instruction *i)
override;
344 const IR::MAU::StatefulCall *preorder(IR::MAU::StatefulCall *sc)
override {
348 const IR::MAU::Action *preorder(IR::MAU::Action *a)
override;
349 const IR::Expression *preorder(IR::Expression *a)
override;
350 void update(
const IR::MAU::Instruction *instr,
const IR::Expression *e);
351 const IR::Expression *propagate(
const IR::MAU::Instruction *instr,
const IR::Expression *e);
352 const IR::MAU::Action *postorder(IR::MAU::Action *)
override;
370 bool preorder(
const IR::MAU::Action *)
override;
371 void postorder(
const IR::MAU::Instruction *)
override;
372 bool preorder(
const IR::MAU::StatefulCall *)
override {
return false; }
373 bool preorder(
const IR::MAU::BackendAttached *)
override {
return false; }
398 bool preorder(
const IR::MAU::Action *)
override;
399 bool preorder(
const IR::MAU::Instruction *)
override;
400 bool preorder(
const IR::MAU::StatefulCall *)
override {
return false; }
401 bool preorder(
const IR::MAU::BackendAttached *)
override {
return false; }
402 void postorder(
const IR::MAU::Action *)
override;
410 const IR::MAU::Action *current_af =
nullptr;
411 const IR::MAU::Action *preorder(IR::MAU::Action *)
override;
412 const IR::Node *preorder(IR::MAU::Instruction *)
override;
413 const IR::MAU::StatefulCall *preorder(IR::MAU::StatefulCall *sc)
override {
417 const IR::MAU::BackendAttached *preorder(IR::MAU::BackendAttached *ba)
override {
428 addPasses({
new Scan(*
this),
new Update(*
this)});
504 std::map<const PHV::Field *, int> comp_adj_field_width_map;
505 std::map<const cstring, int> comp_adj_name_width_map;
506 std::map<const IR::MAU::Action *, std::set<const PHV::Field *>> comp_adj_fields_per_action_map;
511 std::map<const PHV::Field *, bitvec> comp_or_set_zero_writes;
513 std::set<const PHV::Field *> other_targets;
515 bool preorder(
const IR::MAU::Action *)
override;
516 bool preorder(
const IR::MAU::Instruction *)
override;
517 bool preorder(
const IR::MAU::StatefulCall *)
override {
return false; }
518 bool preorder(
const IR::MAU::BackendAttached *)
override {
return false; }
519 void postorder(
const IR::MAU::Action *)
override;
528 const IR::MAU::Action *current_af =
nullptr;
529 std::set<const PHV::Field *> comp_adj_fields;
531 const IR::MAU::Action *preorder(IR::MAU::Action *)
override;
532 const IR::MAU::Instruction *preorder(IR::MAU::Instruction *)
override;
533 const IR::Metadata *preorder(IR::Metadata *)
override;
534 const IR::ConcreteHeaderRef *preorder(IR::ConcreteHeaderRef *)
override;
535 const IR::Node *preorder(IR::Member *)
override;
536 const IR::Type_StructLike *adjust_type_struct(
const IR::Type_StructLike *,
cstring name);
537 const IR::MAU::StatefulCall *preorder(IR::MAU::StatefulCall *sc)
override {
541 const IR::MAU::BackendAttached *preorder(IR::MAU::BackendAttached *ba)
override {
545 const IR::MAU::Action *postorder(IR::MAU::Action *)
override;
551 bool is_compare(
cstring name) {
552 return name ==
"gtequ" || name ==
"gteqs" || name ==
"ltu" || name ==
"lts" ||
553 name ==
"lequ" || name ==
"leqs" || name ==
"gtu" || name ==
"gts" || name ==
"eq" ||
554 name ==
"neq" || name ==
"eq64" || name ==
"neq64";
559 addPasses({
new Scan(*
this),
new Update(*
this)});
580 void find_input(
const IR::MAU::Primitive *);
581 void find_pre_color(
const IR::MAU::Primitive *);
582 const IR::Expression *convert_cast_to_slice(
const IR::Expression *);
584 bool preorder(
const IR::MAU::Instruction *)
override;
585 bool preorder(
const IR::MAU::Primitive *)
override;
588 explicit Scan(
MeterSetup &self) : self(self) {}
593 void update_input(IR::MAU::Meter *);
594 void update_pre_color(IR::MAU::Meter *);
595 bool preorder(IR::MAU::Meter *)
override;
596 bool preorder(IR::MAU::Action *)
override;
599 explicit Update(
MeterSetup &self) : self(self) {}
604 addPasses({
new Scan(*
this),
new Update(*
this)});
609 struct AttachedActionCoord {
610 bool all_per_flow_enabled =
true;
611 bool all_same_meter_type =
true;
612 bool meter_type_set =
false;
613 IR::MAU::MeterType meter_type = IR::MAU::MeterType::UNUSED;
614 const IR::MAU::AttachedMemory *am =
nullptr;
617 using AttachedInfo = std::map<UniqueAttachedId, AttachedActionCoord>;
625 bool preorder(
const IR::MAU::BackendAttached *)
override;
633 bool preorder(
const IR::MAU::Table *)
override;
641 bool preorder(
const IR::MAU::BackendAttached *)
override;
649 void simple_attached(IR::MAU::BackendAttached *);
650 bool preorder(IR::MAU::BackendAttached *)
override;
658 addPasses({
new InitializeAttachedInfo(*
this),
new ScanTables(*
this),
659 new VerifyAttached(*
this),
new UpdateAttached(*
this)});
664 const IR::Node *preorder(IR::Slice *)
override;
674 auto rv = PassManager::init_apply(node);
675 hash_dist_instrs.clear();
681 bool contains_hash_dist =
false;
683 bool preorder(
const IR::MAU::BackendAttached *)
override {
return false; }
684 bool preorder(
const IR::MAU::StatefulCall *)
override {
return false; }
685 bool preorder(
const IR::MAU::Instruction *)
override;
686 bool preorder(
const IR::MAU::HashDist *)
override;
687 void postorder(
const IR::MAU::Instruction *)
override;
696 const IR::Node *postorder(IR::MAU::Instruction *)
override;
709 const IR::Node *postorder(IR::Mux *prim)
override;
Definition instruction_selection.h:500
Definition bf-p4c-options.h:28
Definition instruction_selection.h:325
Definition instruction_selection.h:58
Definition instruction_selection.cpp:621
Definition instruction_selection.h:179
Definition instruction_selection.h:382
Definition instruction_selection.h:670
Definition instruction_selection.h:70
Definition instruction_selection.h:712
InstructionSelection(const BFN_Options &, PhvInfo &, const ReductionOrInfo &)
Definition instruction_selection.cpp:3246
Definition mau_visitor.h:29
Definition mau_visitor.h:45
Definition instruction_selection.h:568
Definition instruction_selection.h:315
Definition ltbitmatrix.h:25
Definition ir/pass_manager.h:40
Definition ordered_map.h:32
Definition ordered_set.h:32
Definition safe_vector.h:27
Definition phv_fields.h:1095
Definition instruction_selection.h:663
Definition instruction_selection.h:608
Definition instruction_selection.h:706
Definition instruction_selection.h:240
Definition instruction_selection.h:143
Definition instruction_selection.h:48
Definition tofino_write_context.h:24
Definition instruction_selection.h:37
Definition instruction_selection.h:362
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition hash_function.h:28
Definition reduction_or.h:47