Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dpdk: Eliminate unused metadata fields #3096

Merged
merged 1 commit into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backends/dpdk/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ void DpdkBackend::convert(const IR::ToplevelBlock *tlb) {
};
simplify.addDebugHook(hook, true);
program = program->apply(simplify);

ordered_set<cstring> used_fields;
dpdk_program = convertToDpdk->getDpdkProgram();
if (!dpdk_program)
return;
PassManager post_code_gen = {
new EliminateUnusedAction(),
new DpdkAsmOptimization,
new CollectUsedMetadataField(used_fields),
new RemoveUnusedMetadataFields(used_fields),
};

dpdk_program = dpdk_program->apply(post_code_gen)->to<IR::DpdkAsmProgram>();
Expand Down
31 changes: 31 additions & 0 deletions backends/dpdk/dpdkAsmOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,35 @@ const IR::IndexedVector<IR::DpdkAsmStatement> *RemoveLabelAfterLabel::removeLabe
}
return new_l;
}

const IR::Node* RemoveUnusedMetadataFields::preorder(IR::DpdkAsmProgram *p) {
IR::IndexedVector<IR::DpdkStructType> usedStruct;
bool isMetadataStruct = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this "foundMetadataStruct".
Perhaps you can change it in a future PR.

for (auto st : p->structType) {
if (!isMetadataStruct) {
for (auto anno : st->annotations->annotations) {
if (anno->name == "__metadata__") {
isMetadataStruct = true;
IR::IndexedVector<IR::StructField> usedMetadataFields;
for (auto field : st->fields) {
if (used_fields.count(field->name.name)) {
usedMetadataFields.push_back(field);
}
}
auto newSt = new IR::DpdkStructType(st->srcInfo, st->name,
st->annotations, usedMetadataFields);
usedStruct.push_back(newSt);
}
}
if (!isMetadataStruct) {
usedStruct.push_back(st);
}
} else {
usedStruct.push_back(st);
}
}
p->structType = usedStruct;
return p;
}

} // namespace DPDK
24 changes: 24 additions & 0 deletions backends/dpdk/dpdkAsmOpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ class RemoveLabelAfterLabel : public Transform {
}
};


// This pass Collects all metadata struct member used in program
class CollectUsedMetadataField : public Inspector {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, if this pass is used only within dpdkAsmOpt.cpp, it does not need to have a declaration in the header, it can be local to the file (within an empty namespace to avoid name clashes).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it's used in backend.cpp

ordered_set<cstring>& used_fields;
public:
explicit CollectUsedMetadataField(ordered_set<cstring>& used_fields)
: used_fields(used_fields) {}
bool preorder(const IR::Member *m) override {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessarily a field of the metadata structure, but I guess it will include all the fields. So you may keep some extra fields here if they happen to show up in other structures.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now only insert if it's metadata's member.

// metadata struct field used like m.<field_name> in expressions
if (m->expr->toString() == "m")
used_fields.insert(m->member.toString());
return true;
}
};

// This pass removes all unused fields from metadata struct
class RemoveUnusedMetadataFields : public Transform {
ordered_set<cstring>& used_fields;
public:
explicit RemoveUnusedMetadataFields(ordered_set<cstring>& used_fields)
: used_fields(used_fields) {}
const IR::Node* preorder(IR::DpdkAsmProgram *p) override;
};

// Instructions can only appear in actions and apply block of .spec file.
// All these individual passes work on the actions and apply block of .spec file.
class DpdkAsmOptimization : public PassRepeated {
Expand Down
13 changes: 2 additions & 11 deletions backends/dpdk/dpdkProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ limitations under the License.

namespace DPDK {
/* Insert the metadata structure updated with tmp variables created during parser conversion
Add annotations to metadata and header structures and add all the structures to DPDK
structtype.
Add all the structures to DPDK structtype.
*/
IR::IndexedVector<IR::DpdkStructType> ConvertToDpdkProgram::UpdateHeaderMetadata
(IR::P4Program *prog, IR::Type_Struct *metadata) {
Expand All @@ -34,13 +33,9 @@ IR::IndexedVector<IR::DpdkStructType> ConvertToDpdkProgram::UpdateHeaderMetadata
for (auto obj : prog->objects) {
if (auto s = obj->to<IR::Type_Struct>()) {
if (s->name.name == structure->local_metadata_type) {
auto *annotations = new IR::Annotations(
{new IR::Annotation(IR::ID("__metadata__"), {})});
for (auto anno : s->annotations->annotations)
annotations->add(anno);
new_objs->push_back(metadata);
auto st = new IR::DpdkStructType(s->srcInfo, s->name,
annotations, metadata->fields);
s->annotations, metadata->fields);
structType.push_back(st);
} else {
if (structure->args_struct_map.find(s->name.name) !=
Expand All @@ -49,10 +44,6 @@ IR::IndexedVector<IR::DpdkStructType> ConvertToDpdkProgram::UpdateHeaderMetadata
s->annotations, s->fields);
structType.push_back(st);
} else if (s->name.name == structure->header_type) {
auto *annotations = new IR::Annotations(
{new IR::Annotation(IR::ID("__packet_data__"), {})});
for (auto anno : s->annotations->annotations)
annotations->add(anno);
auto st = new IR::DpdkStructType(s->srcInfo, s->name,
s->annotations, s->fields);
structType.push_back(st);
Expand Down
19 changes: 0 additions & 19 deletions testdata/p4_16_samples_outputs/pna-action-selector.p4.spec
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,7 @@ struct tbl_set_group_id_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<16> local_metadata_data
bit<32> pna_main_output_metadata_output_port
bit<32> MainControlT_as_group_id
Expand Down
19 changes: 0 additions & 19 deletions testdata/p4_16_samples_outputs/pna-add-on-miss.p4.spec
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,7 @@ struct next_hop_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> pna_main_output_metadata_output_port
bit<32> MainControlT_tmp
bit<32> MainControlT_tmp_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,8 @@ struct next_hop_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> pna_main_output_metadata_output_port
bit<32> MainControlT_key
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,8 @@ struct next_hop_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> pna_main_output_metadata_output_port
bit<32> MainControlT_tmp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,8 @@ struct forward_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> local_metadata_meta
bit<32> pna_main_output_metadata_output_port
bit<32> MainControlT_addr
Expand Down
19 changes: 0 additions & 19 deletions testdata/p4_16_samples_outputs/pna-example-dpdk-varbit.p4.spec
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,7 @@ struct a2_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> pna_main_output_metadata_output_port
bit<32> MainParserT_parser_tmp
bit<32> MainParserT_parser_tmp_0
Expand Down
19 changes: 0 additions & 19 deletions testdata/p4_16_samples_outputs/pna-example-template.p4.spec
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,7 @@ struct next_hop_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> pna_main_output_metadata_output_port
}
metadata instanceof main_metadata_t
Expand Down
19 changes: 0 additions & 19 deletions testdata/p4_16_samples_outputs/pna-extract-local-header.p4.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,7 @@ struct my_header_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> pna_main_output_metadata_output_port
}
metadata instanceof main_metadata_t
Expand Down
19 changes: 0 additions & 19 deletions testdata/p4_16_samples_outputs/pna-issue3041.p4.spec
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,7 @@ struct a2_arg_t {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<32> pna_main_output_metadata_output_port
bit<32> MainParserT_parser_tmp
bit<32> MainParserT_parser_tmp_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,7 @@ struct lookahead_tmp_hdr {
}

struct main_metadata_t {
bit<32> pna_pre_input_metadata_input_port
bit<16> pna_pre_input_metadata_parser_error
bit<32> pna_pre_input_metadata_direction
bit<3> pna_pre_input_metadata_pass
bit<8> pna_pre_input_metadata_loopedback
bit<8> pna_pre_output_metadata_decrypt
bit<32> pna_pre_output_metadata_said
bit<16> pna_pre_output_metadata_decrypt_start_offset
bit<32> pna_main_parser_input_metadata_direction
bit<3> pna_main_parser_input_metadata_pass
bit<8> pna_main_parser_input_metadata_loopedback
bit<32> pna_main_parser_input_metadata_input_port
bit<32> pna_main_input_metadata_direction
bit<3> pna_main_input_metadata_pass
bit<8> pna_main_input_metadata_loopedback
bit<64> pna_main_input_metadata_timestamp
bit<16> pna_main_input_metadata_parser_error
bit<8> pna_main_input_metadata_class_of_service
bit<32> pna_main_input_metadata_input_port
bit<8> pna_main_output_metadata_class_of_service
bit<8> local_metadata_f1
bit<32> pna_main_output_metadata_output_port
}
Expand Down
Loading