P4C
The P4 Compiler
Loading...
Searching...
No Matches
collect_strided_headers.h
1
19#ifndef BF_P4C_PHV_COLLECT_STRIDED_HEADERS_H_
20#define BF_P4C_PHV_COLLECT_STRIDED_HEADERS_H_
21
22#include <map>
23
24#include "backends/tofino/bf-p4c/phv/phv_fields.h"
25#include "lib/cstring.h"
26
29 PhvInfo &phv;
30
31 std::map<cstring, std::vector<ordered_set<const PHV::Field *>>> stride_groups;
32
33 explicit CollectStridedHeaders(PhvInfo &p) : phv(p) {}
34
35 bool preorder(const IR::HeaderStack *hs) {
36 auto state = findContext<IR::BFN::ParserState>();
37
38 if (state && state->stride) {
39 LOG4(state->name << " needs strided allocation (stride size = " << hs->size << ")");
40
41 for (auto f : hs->type->fields) {
43
44 for (int i = 0; i < hs->size; i++) {
45 cstring name = hs->name + "[" + cstring::to_cstring(i) + "]." + f->name;
46 auto field = phv.field(name);
47 stride_group.push_back(field);
48 }
49
50 stride_groups[hs->name].push_back(stride_group);
51 }
52 }
53
54 return false;
55 }
56
57 const ordered_set<const PHV::Field *> *get_strided_group(const PHV::Field *f) const {
58 for (auto &[_, groups] : stride_groups) {
59 for (auto &group : groups) {
60 if (group.count(f)) return &group;
61 }
62 }
63
64 return nullptr;
65 }
66
67 profile_t init_apply(const IR::Node *root) override {
68 profile_t rv = Inspector::init_apply(root);
69 stride_groups.clear();
70 return rv;
71 }
72};
73
74#endif /* BF_P4C_PHV_COLLECT_STRIDED_HEADERS_H_ */
Definition node.h:94
Definition visitor.h:400
Definition visitor.h:78
Definition cstring.h:85
Definition ordered_set.h:32
Definition phv_fields.h:154
Definition phv_fields.h:1095
Collects header stacks that require strided allocation (in a parser loop).
Definition collect_strided_headers.h:28