Fix BMv2 variable-offset slice compilation (#5558) - #5716
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses BMv2 compilation failures for variable-offset +: slices (e.g., value[offset+:width]) by adding an opt-in strength-reduction transform that lowers these expressions into a form BMv2 can codegen: (value >> offset)[width-1:0]. The transform is enabled for BMv2 midends and includes targeted unit and regression coverage.
Changes:
- Add a
StrengthReductionPolicyoption to lower variable-offsetIR::PlusSliceintoShr+ constantSlice, while preserving write-context behavior and handling signed sources. - Enable the new transform in BMv2 midend pipelines (simple_switch, psa_switch, pna_nic).
- Add gtest coverage and a new sample/regression program with expected outputs.
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| frontends/p4/strengthReduction.h | Extends StrengthReductionPolicy and updates DoStrengthReduction to support write-context awareness. |
| frontends/p4/strengthReduction.cpp | Implements the opt-in lowering of variable-offset PlusSlice into >> plus constant Slice. |
| backends/bmv2/simple_switch/midend.cpp | Enables the new strength-reduction policy for the simple_switch BMv2 midend pipeline. |
| backends/bmv2/psa_switch/midend.cpp | Enables the new strength-reduction policy for the PSA BMv2 midend pipeline. |
| backends/bmv2/pna_nic/midend.cpp | Enables the new strength-reduction policy for the PNA NIC BMv2 midend pipeline. |
| test/gtest/strength_reduction.cpp | Adds unit tests verifying default-off behavior, enabled lowering, write-context preservation, and signed-source handling. |
| testdata/p4_16_samples/issue5558-bmv2.p4 | Adds a regression P4 program reproducing issue #5558 for BMv2. |
| testdata/p4_16_samples_outputs/issue5558-bmv2.p4.p4info.txtpb | Expected P4Info output for the new BMv2 regression sample. |
| testdata/p4_16_samples_outputs/issue5558-bmv2.p4.entries.txtpb | Expected P4Runtime entries output for the new BMv2 regression sample. |
| testdata/p4_16_samples_outputs/issue5558-bmv2.p4-stderr | Expected stderr output for the new BMv2 regression sample. |
| testdata/p4_16_samples_outputs/issue5558-bmv2.p4 | Expected post-parse P4 output for the new regression sample. |
| testdata/p4_16_samples_outputs/issue5558-bmv2-first.p4 | Expected early-frontend dump output for the new regression sample. |
| testdata/p4_16_samples_outputs/issue5558-bmv2-frontend.p4 | Expected end-of-frontend dump output for the new regression sample. |
| testdata/p4_16_samples_outputs/issue5558-bmv2-midend.p4 | Expected end-of-midend dump output for the new regression sample. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (sourceType == nullptr) return expr; | ||
| if (sourceType->isSigned) { | ||
| auto *unsignedType = IR::Type_Bits::get(sourceType->size, false); | ||
| source = new IR::Cast(source->srcInfo, unsignedType, source); |
There was a problem hiding this comment.
Adding a cast here seems wrong -- if the value is negative and the slice goes off the top, the sign bit should be replicated. That's what happens with a signed shift.
There was a problem hiding this comment.
@ChrisDodd thanks for the review. Removed the unsigned cast to preserve signed right-shift semantics and updated the test
186720f to
850b0a1
Compare
Signed-off-by: Artur Novotarskyi <artur.novotarskyi@gmail.com>
850b0a1 to
02af277
Compare
Signed-off-by: Artur Novotarskyi <artur.novotarskyi@gmail.com>
|
The failing container test is fixed in #5715. For P4Testgen add the failures to xfails and file an issue, they might have to be fixed separately. |
| @@ -0,0 +1,51 @@ | |||
| #include <core.p4> | |||
There was a problem hiding this comment.
Consider adding an stf test to ensure the transformation works correctly.
Signed-off-by: Artur Novotarskyi <artur.novotarskyi@gmail.com>
Fixes #5558.
Adds an opt-in strength-reduction transform that lowers variable-offset slices such as value[offset+:width] into (value >> offset)[width-1:0], which BMv2 can generate code for. The transform is enabled for all BMv2 targets and preserves write-context behavior.
Includes unit tests and a BMv2 regression test.