P4C
The P4 Compiler
Loading...
Searching...
No Matches
phv_spec.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_PHV_PHV_SPEC_H_
20#define BACKENDS_TOFINO_BF_P4C_PHV_PHV_SPEC_H_
21
22#include <optional>
23#include <vector>
24
25#include "backends/tofino/bf-p4c/phv/phv.h"
26#include "lib/bitvec.h"
27#include "lib/json.h"
28#include "lib/ordered_map.h"
29
30namespace P4 {
31class cstring;
32}
33
34namespace P4 {
35namespace IR {
36class Annotation;
37} // end namespace IR
38} // end namespace P4
39
40using namespace P4;
41
42class PhvSpec {
43 public:
44 // We should reuse the definition from arch/arch.h, however, because of circular includes
45 // (arch.h, phv_spec.h, device.h) that is not currently workable
46 enum ArchBlockType_t { PARSER, MAU, DEPARSER };
47
52 struct RangeSpec {
53 unsigned start;
54 unsigned blocks;
55 unsigned blockSize;
56 unsigned incr;
57 unsigned shl;
58 unsigned shr;
59 };
60 using AddressSpec = std::map<PHV::Type, RangeSpec>;
61
62 protected:
63 // All cache fields
64 mutable bitvec physical_containers_i;
65 mutable std::map<PHV::Size, std::vector<bitvec>> mau_groups_i;
66 mutable bitvec ingress_only_containers_i;
67 mutable bitvec egress_only_containers_i;
68 mutable std::vector<bitvec> tagalong_collections_i;
69 mutable bitvec individually_assigned_containers_i;
70
72 std::vector<PHV::Type> definedTypes;
74 std::set<PHV::Size> definedSizes;
76 std::set<PHV::Kind> definedKinds;
77
79
80 std::map<PHV::Size, std::set<PHV::Type>> sizeToTypeMap;
81
84 struct MauGroupType {
85 unsigned numGroups; // Number of MAU groups
86 std::map<PHV::Type, unsigned> types;
87 // Type and number of containers in each group
88
89 explicit MauGroupType(unsigned n, std::map<PHV::Type, unsigned> t)
90 : numGroups(n), types(t) {}
91
92 explicit MauGroupType(unsigned n) : numGroups(n) {}
93
94 MauGroupType() : numGroups(0) {}
95
96 void addType(PHV::Type t, unsigned n) { types.emplace(t, n); }
97 };
98
107 std::map<PHV::Size, MauGroupType> mauGroupSpec;
108
111 unsigned containersPerGroup = 0;
112
113 std::map<PHV::Size, std::vector<unsigned>> ingressOnlyMauGroupIds;
114
115 std::map<PHV::Size, std::vector<unsigned>> egressOnlyMauGroupIds;
116
117 std::map<PHV::Type, unsigned> tagalongCollectionSpec;
118
119 unsigned numTagalongCollections = 0;
120
121 std::map<PHV::Type, unsigned> deparserGroupSize;
122
123 std::map<PHV::Type, std::pair<unsigned, unsigned>> deparserGroupSpec;
124
125 unsigned numPovBits = 0;
126
131 void addType(PHV::Type t);
132
134 unsigned getContainersPerGroup(
135 const std::map<PHV::Size, unsigned> &numContainersPerGroup) const;
136
137 public:
139 const std::vector<PHV::Type> &containerTypes() const;
140
142 const std::set<PHV::Size> &containerSizes() const;
143
145 const std::set<PHV::Kind> &containerKinds() const;
146
148 bool hasContainerKind(PHV::Kind kind) const;
149
151 const std::map<PHV::Size, std::set<PHV::Type>> groupsToTypes() const;
152
155 unsigned numContainerTypes() const;
156
159 unsigned numContainersInGroup() const { return containersPerGroup; }
160
164 PHV::Type idToContainerType(unsigned id) const;
165
170 unsigned containerTypeToId(PHV::Type type) const;
171
175 PHV::Container idToContainer(unsigned id) const;
176
180 unsigned containerToId(PHV::Container container) const;
181
183 bitvec filterContainerSet(const bitvec &set, PHV::Kind kind) const;
184 bitvec filterContainerSet(const bitvec &set, PHV::Type type) const;
185
187 cstring containerSetToString(const bitvec &set) const;
188
200 virtual bitvec parserGroup(unsigned id) const = 0;
201
205 virtual bool hasParserExtractGroups() const = 0;
206
212 virtual bitvec parserExtractGroup(unsigned id) const = 0;
213
215 virtual unsigned parserGroupId(const PHV::Container &c) const = 0;
216
219 bitvec deparserGroup(unsigned id) const;
220
222 virtual unsigned deparserGroupId(const PHV::Container &c) const = 0;
223
235 bitvec range(PHV::Type t, unsigned start, unsigned length) const;
236
239 const std::map<PHV::Size, std::vector<unsigned>> &gressOnlyMauGroupIds) const;
240
242 const bitvec &ingressOnly() const;
243
245 const bitvec &egressOnly() const;
246
248 const std::vector<bitvec> &mauGroups(PHV::Size sz) const;
249
251 const std::map<PHV::Size, std::vector<bitvec>> &mauGroups() const;
252
255 bitvec mauGroup(unsigned container_id) const;
256
258 virtual unsigned mauGroupId(const PHV::Container &c) const = 0;
259
262 const std::pair<int, int> mauGroupNumAndSize(const PHV::Type t) const;
263
266 const std::pair<int, int> deparserGroupNumAndSize(const PHV::Type t) const;
267
269 unsigned getTagalongCollectionId(PHV::Container c) const;
270
271 const std::map<PHV::Type, unsigned> getTagalongCollectionSpec() const {
272 return tagalongCollectionSpec;
273 }
274
275 unsigned getNumTagalongCollections() const { return numTagalongCollections; }
276
278 const std::vector<bitvec> &tagalongCollections() const;
279
282 bitvec tagalongCollection(unsigned container_id) const;
283
285 unsigned getNumPovBits() const { return numPovBits; }
286
289 virtual const bitvec &individuallyAssignedContainers() const = 0;
290
293 const bitvec &physicalContainers() const;
294
297 virtual unsigned physicalAddress(unsigned container_id, ArchBlockType_t interface) const = 0;
298
301 unsigned physicalAddress(const PHV::Container &c, ArchBlockType_t interface) const;
302
304 virtual AddressSpec &physicalAddressSpec(ArchBlockType_t interface) const = 0;
305
308 std::optional<PHV::Container> physicalAddressToContainer(unsigned address,
309 ArchBlockType_t interface) const;
310
312 void applyGlobalPragmas(const std::vector<const IR::Annotation *> &global_pragmas) const;
313};
314
315class TofinoPhvSpec : public PhvSpec {
316 public:
318
320 bitvec parserGroup(unsigned id) const override;
321
323 bool hasParserExtractGroups() const override;
324
326 bitvec parserExtractGroup(unsigned id) const override;
327
329 unsigned parserGroupId(const PHV::Container &c) const override;
330
332 unsigned mauGroupId(const PHV::Container &c) const override;
333
335 unsigned deparserGroupId(const PHV::Container &c) const override;
336
337 const bitvec &individuallyAssignedContainers() const override;
338
341 unsigned physicalAddress(unsigned container_id, ArchBlockType_t /*interface*/) const override;
342
345 AddressSpec &physicalAddressSpec(ArchBlockType_t /* interface */) const override {
346 return _physicalAddresses;
347 }
348
349 private:
350 static AddressSpec _physicalAddresses;
351};
352
353class JBayPhvSpec : public PhvSpec {
354 public:
355 JBayPhvSpec();
356
358 bitvec parserGroup(unsigned id) const override;
359
361 bool hasParserExtractGroups() const override;
362
364 bitvec parserExtractGroup(unsigned id) const override;
365
367 unsigned parserGroupId(const PHV::Container &c) const override;
368
370 unsigned mauGroupId(const PHV::Container &c) const override;
371
373 unsigned deparserGroupId(const PHV::Container &c) const override;
374
375 const bitvec &individuallyAssignedContainers() const override;
376
379 unsigned physicalAddress(unsigned container_id, ArchBlockType_t interface) const override;
380
382 AddressSpec &physicalAddressSpec(ArchBlockType_t interface) const override {
383 switch (interface) {
384 case PhvSpec::MAU:
385 return _physicalMauAddresses;
386 case PhvSpec::PARSER:
387 return _physicalParserAddresses;
388 case PhvSpec::DEPARSER:
389 return _physicalDeparserAddresses;
390 default:
391 BUG("Invalid interface");
392 }
393 }
394
395 private:
396 static AddressSpec _physicalMauAddresses;
397 static AddressSpec _physicalParserAddresses;
398 static AddressSpec _physicalDeparserAddresses;
399};
400
401#endif /* BACKENDS_TOFINO_BF_P4C_PHV_PHV_SPEC_H_ */
Definition phv_spec.h:353
AddressSpec & physicalAddressSpec(ArchBlockType_t interface) const override
Definition phv_spec.h:382
bitvec parserGroup(unsigned id) const override
Definition phv_spec.cpp:593
unsigned physicalAddress(unsigned container_id, ArchBlockType_t interface) const override
Definition phv_spec.cpp:686
bitvec parserExtractGroup(unsigned id) const override
Definition phv_spec.cpp:606
unsigned parserGroupId(const PHV::Container &c) const override
Definition phv_spec.cpp:608
unsigned mauGroupId(const PHV::Container &c) const override
Definition phv_spec.cpp:638
bool hasParserExtractGroups() const override
Definition phv_spec.cpp:604
unsigned deparserGroupId(const PHV::Container &c) const override
Definition phv_spec.cpp:677
const bitvec & individuallyAssignedContainers() const override
Definition phv_spec.cpp:682
Definition bitvec.h:120
Definition cstring.h:85
Definition ordered_map.h:32
Definition phv.h:176
Definition phv.h:114
Definition phv_spec.h:42
bitvec tagalongCollection(unsigned container_id) const
Definition phv_spec.cpp:266
void addType(PHV::Type t)
Definition phv_spec.cpp:31
virtual unsigned physicalAddress(unsigned container_id, ArchBlockType_t interface) const =0
const std::vector< PHV::Type > & containerTypes() const
Definition phv_spec.cpp:39
unsigned getNumPovBits() const
Definition phv_spec.h:285
std::set< PHV::Size > definedSizes
All sizes of containers supported by the device.
Definition phv_spec.h:74
virtual bitvec parserExtractGroup(unsigned id) const =0
const bitvec & physicalContainers() const
Definition phv_spec.cpp:150
bitvec deparserGroup(unsigned id) const
Definition phv_spec.cpp:301
std::map< PHV::Size, MauGroupType > mauGroupSpec
Definition phv_spec.h:107
PHV::Type idToContainerType(unsigned id) const
Definition phv_spec.cpp:53
void applyGlobalPragmas(const std::vector< const IR::Annotation * > &global_pragmas) const
apply global pragmas to cached info about available PHV containers
Definition phv_spec.cpp:717
bitvec filterContainerSet(const bitvec &set, PHV::Kind kind) const
Filters a set of containers for a single container kind or type.
Definition phv_spec.cpp:75
virtual const bitvec & individuallyAssignedContainers() const =0
unsigned getTagalongCollectionId(PHV::Container c) const
Definition phv_spec.cpp:239
unsigned containerTypeToId(PHV::Type type) const
Definition phv_spec.cpp:58
virtual bitvec parserGroup(unsigned id) const =0
const std::pair< int, int > deparserGroupNumAndSize(const PHV::Type t) const
Definition phv_spec.cpp:296
const std::set< PHV::Size > & containerSizes() const
Definition phv_spec.cpp:41
virtual AddressSpec & physicalAddressSpec(ArchBlockType_t interface) const =0
bitvec ingressOrEgressOnlyContainers(const std::map< PHV::Size, std::vector< unsigned > > &gressOnlyMauGroupIds) const
Definition phv_spec.cpp:211
unsigned getContainersPerGroup(const std::map< PHV::Size, unsigned > &numContainersPerGroup) const
Return the number of containers in an MAU group.
Definition phv_spec.cpp:325
std::set< PHV::Kind > definedKinds
All kinds of containers supported by the device.
Definition phv_spec.h:76
unsigned numContainersInGroup() const
Definition phv_spec.h:159
const std::map< PHV::Size, std::vector< bitvec > > & mauGroups() const
Definition phv_spec.cpp:179
const std::vector< bitvec > & tagalongCollections() const
Definition phv_spec.cpp:248
const std::pair< int, int > mauGroupNumAndSize(const PHV::Type t) const
Definition phv_spec.cpp:285
const std::map< PHV::Size, std::set< PHV::Type > > groupsToTypes() const
Definition phv_spec.cpp:47
virtual unsigned mauGroupId(const PHV::Container &c) const =0
const std::set< PHV::Kind > & containerKinds() const
Definition phv_spec.cpp:43
bitvec range(PHV::Type t, unsigned start, unsigned length) const
Definition phv_spec.cpp:143
unsigned containerToId(PHV::Container container) const
Definition phv_spec.cpp:70
virtual unsigned deparserGroupId(const PHV::Container &c) const =0
unsigned numContainerTypes() const
Definition phv_spec.cpp:51
virtual unsigned parserGroupId(const PHV::Container &c) const =0
bool hasContainerKind(PHV::Kind kind) const
Determines whether the device has the given kind of PHV container.
Definition phv_spec.cpp:45
std::optional< PHV::Container > physicalAddressToContainer(unsigned address, ArchBlockType_t interface) const
Definition phv_spec.cpp:345
const bitvec & egressOnly() const
Definition phv_spec.cpp:233
PHV::Container idToContainer(unsigned id) const
Definition phv_spec.cpp:60
cstring containerSetToString(const bitvec &set) const
Definition phv_spec.cpp:99
virtual bool hasParserExtractGroups() const =0
bitvec mauGroup(unsigned container_id) const
Definition phv_spec.cpp:160
std::vector< PHV::Type > definedTypes
All types of containers supported by the device.
Definition phv_spec.h:72
unsigned containersPerGroup
Definition phv_spec.h:111
const bitvec & ingressOnly() const
Definition phv_spec.cpp:227
Definition phv_spec.h:52
Definition phv_spec.h:315
bool hasParserExtractGroups() const override
Definition phv_spec.cpp:452
bitvec parserGroup(unsigned id) const override
Definition phv_spec.cpp:450
unsigned mauGroupId(const PHV::Container &c) const override
Definition phv_spec.cpp:461
unsigned parserGroupId(const PHV::Container &c) const override
Definition phv_spec.cpp:456
unsigned deparserGroupId(const PHV::Container &c) const override
Definition phv_spec.cpp:481
const bitvec & individuallyAssignedContainers() const override
Definition phv_spec.cpp:486
bitvec parserExtractGroup(unsigned id) const override
Definition phv_spec.cpp:454
unsigned physicalAddress(unsigned container_id, ArchBlockType_t) const override
Definition phv_spec.cpp:494
AddressSpec & physicalAddressSpec(ArchBlockType_t) const override
Definition phv_spec.h:345
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Size
all possible PHV container sizes in BFN devices
Definition phv.h:110
Kind
Definition phv.h:44
Definition phv_spec.h:84