P4C
The P4 Compiler
Loading...
Searching...
No Matches
hash_mask_annotations.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_MAU_HASH_MASK_ANNOTATIONS_H_
20#define BACKENDS_TOFINO_BF_P4C_MAU_HASH_MASK_ANNOTATIONS_H_
21
22#include "backends/tofino/bf-p4c/phv/phv_fields.h"
23#include "ir/ir.h"
24#include "lib/bitvec.h"
25
26using namespace P4;
27
30 public:
31 HashMaskAnnotations(const IR::MAU::Table *tbl, const PhvInfo &phv) {
32 key_hash_bits_masked = 0;
33 for (auto &table_key : tbl->match_key) {
34 for (auto &a : table_key->annotations->annotations) {
35 if (a->name == "hash_mask") {
36 bitvec hash_mask = getBitvec(a);
37
38 le_bitrange bits = {0, 0};
39 const PHV::Field *field = phv.field(table_key->expr, &bits);
40 if (field) {
41 int masked = hash_mask.popcount();
42 if (masked > bits.size()) masked = bits.size();
43 key_hash_bits_masked += (bits.size() - masked);
44 }
45
46 const IR::Member *m = table_key->expr->to<IR::Member>();
47 if (m != nullptr) key_hash_masks[m->toString()] = hash_mask;
48 break;
49 }
50 }
51 }
52 if (LOGGING(5)) {
53 LOG5("Hash mask annotations for table " << tbl->name << ":");
54 for (auto &m : key_hash_masks) LOG5(" " << m.first << " : 0x" << std::hex << m.second);
55 }
56 }
57
58 std::map<cstring, bitvec> hash_masks() { return key_hash_masks; }
59
60 int hash_bits_masked() { return key_hash_bits_masked; }
61
62 private:
63 bitvec getBitvec(const IR::Annotation *annotation) {
64 bitvec rv;
65 if (annotation->expr.size() != 1) {
66 error("%1% should contain a constant", annotation);
67 return rv;
68 }
69 auto constant = annotation->expr[0]->to<IR::Constant>();
70 if (constant == nullptr) {
71 error("%1% should contain a constant", annotation);
72 return rv;
73 }
74 int64_t c = constant->asUint64();
75 rv.setraw(c);
76 return rv;
77 }
78
79 // Vector of match keys annotated with @hash_mask()
80 // along with their associated mask.
81 std::map<cstring, bitvec> key_hash_masks;
82
83 // Number of bits masked out with @hash_mask() throughout
84 // all match keys in the table.
85 int key_hash_bits_masked;
86};
87
88#endif /* BACKENDS_TOFINO_BF_P4C_MAU_HASH_MASK_ANNOTATIONS_H_ */
Helper class to handle the @hash_mask() annotation.
Definition hash_mask_annotations.h:29
Definition bitvec.h:120
Definition phv_fields.h:154
Definition phv_fields.h:1095
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
ssize_t size() const
Definition lib/bitrange.h:539