P4C
The P4 Compiler
Loading...
Searching...
No Matches
dynhash.h
1
19#ifndef BF_P4C_MAU_DYNHASH_H_
20#define BF_P4C_MAU_DYNHASH_H_
21
22#include <vector>
23
24#include <boost/range/irange.hpp>
25
26#include "backends/tofino/bf-p4c/mau/input_xbar.h"
27#include "backends/tofino/bf-p4c/mau/tofino/input_xbar.h"
28#include "backends/tofino/bf-p4c/phv/phv_fields.h"
29#include "ir/ir.h"
30#include "lib/cstring.h"
31#include "lib/json.h"
32#include "lib/ordered_map.h"
33#include "mau_visitor.h"
34
35using namespace P4;
36
38 int stage;
39 int hash_group;
40
41 bool operator<(const HashFuncLoc &hfl) const {
42 if (stage != hfl.stage) return stage < hfl.stage;
43 return hash_group < hfl.hash_group;
44 }
45
46 bool operator==(const HashFuncLoc &hfl) const {
47 return stage == hfl.stage && hash_group != hfl.hash_group;
48 }
49
50 bool operator!=(const HashFuncLoc &hfl) const { return !(*this == hfl); }
51
52 HashFuncLoc(int s, int hg) : stage(s), hash_group(hg) {}
53};
54
55using NameToHashGen = std::map<cstring, const IR::MAU::HashGenExpression *>;
56using HashGenToAlloc =
57 std::map<cstring, safe_vector<std::pair<HashFuncLoc, const Tofino::IXBar::HashDistIRUse *>>>;
58using AllocToHashUse = std::map<HashFuncLoc, safe_vector<const Tofino::IXBar::HashDistIRUse *>>;
59
61 NameToHashGen &verify_hash_gen;
62
63 profile_t init_apply(const IR::Node *node) override {
64 auto rv = MauInspector::init_apply(node);
65 verify_hash_gen.clear();
66 return rv;
67 }
68
69 bool preorder(const IR::MAU::HashGenExpression *) override;
70
71 public:
72 explicit VerifyUniqueDynamicHash(NameToHashGen &vhg) : verify_hash_gen(vhg) {}
73};
74
76 const NameToHashGen &verify_hash_gen;
77 HashGenToAlloc &hash_gen_alloc;
78
79 profile_t init_apply(const IR::Node *node) override {
80 auto rv = MauInspector::init_apply(node);
81 hash_gen_alloc.clear();
82 return rv;
83 }
84
85 bool preorder(const IR::MAU::Table *) override;
86
87 public:
88 GatherDynamicHashAlloc(const NameToHashGen &vhg, HashGenToAlloc &hga)
89 : verify_hash_gen(vhg), hash_gen_alloc(hga) {}
90};
91
92namespace BFN {
93
94static const unsigned fieldListHandleBase = (0x21 << 24);
95// dynamic hash handle base is 0x8, not 0x22
96static const unsigned dynHashHandleBase = (0x8 << 24);
97static const unsigned algoHandleBase = (0x23 << 24);
98extern unsigned fieldListHandle;
99extern unsigned dynHashHandle;
100extern unsigned algoHandle;
101static std::map<cstring, unsigned> algoHandles;
103 private:
104 const PhvInfo &phv;
105 Util::JsonArray *_dynHashNode;
106 const NameToHashGen &verify_hash_gen;
107 const HashGenToAlloc &hash_gen_alloc;
108 bool all_placed = true;
109
110 bool preorder(const IR::MAU::Table *tbl) override;
111 void gen_ixbar_json(const IXBar::Use *ixbar_use, Util::JsonObject *_dhc, int stage,
112 const cstring field_list_name, const IR::NameList *algorithms,
113 int hash_width = -1);
114
115 void end_apply() override;
116
117 void gen_ixbar_bytes_json(Util::JsonArray *_xbar_cfgs, int stage,
118 const std::map<cstring, cstring> &fieldNames,
119 const IXBar::Use &ixbar_use);
120 void gen_algo_json(Util::JsonObject *_dhc, const IR::MAU::HashGenExpression *hge);
121 void gen_single_algo_json(Util::JsonArray *_algos, const IR::MAU::HashFunction *alg,
122 cstring alg_name, bool &is_default);
123 void gen_hash_json(Util::JsonArray *_hash_cfgs, int stage, Tofino::IXBar::Use &ixbar_use,
124 int &hash_bit_width);
125 void gen_field_list_json(Util::JsonObject *_field_list, const IR::MAU::HashGenExpression *hge,
126 std::map<cstring, cstring> &fieldNames);
127 void gen_hash_dist_json(cstring dyn_hash_name);
128
129 public:
130 GenerateDynamicHashJson(const PhvInfo &p, Util::JsonArray *_dhn, const NameToHashGen &vhg,
131 const HashGenToAlloc &hga)
132 : phv(p), _dynHashNode(_dhn), verify_hash_gen(vhg), hash_gen_alloc(hga) {}
134};
135
137 const PhvInfo &phv;
138 Util::JsonArray *_dynHashNode = nullptr;
139 NameToHashGen verify_hash_gen;
140 HashGenToAlloc hash_gen_alloc;
141
142 friend std::ostream &operator<<(std::ostream &out, const DynamicHashJson &dyn);
143
144 public:
145 explicit DynamicHashJson(const PhvInfo &p) : phv(p) {
146 _dynHashNode = new Util::JsonArray();
147 addPasses(
148 {new VerifyUniqueDynamicHash(verify_hash_gen),
149 new GatherDynamicHashAlloc(verify_hash_gen, hash_gen_alloc),
150 new GenerateDynamicHashJson(phv, _dynHashNode, verify_hash_gen, hash_gen_alloc)});
151 }
152};
153
154} // namespace BFN
155
156#endif /* BF_P4C_MAU_DYNHASH_H_ */
Definition dynhash.h:136
Definition dynhash.h:102
Definition dynhash.h:75
Definition mau_visitor.h:29
Definition node.h:94
Definition ir/pass_manager.h:40
Definition json.h:115
Definition json.h:164
Definition visitor.h:78
Definition cstring.h:85
Definition phv_fields.h:1095
Definition dynhash.h:60
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition dynhash.h:37
Definition input_xbar.h:191
Definition hash_function.h:28
Definition tofino/input_xbar.h:192