Skip to content

Fix BMv2 variable-offset slice compilation (#5558) - #5716

Open
anovotarsk wants to merge 4 commits into
p4lang:mainfrom
anovotarsk:anovotarsk/bmv2_variable_slice
Open

Fix BMv2 variable-offset slice compilation (#5558)#5716
anovotarsk wants to merge 4 commits into
p4lang:mainfrom
anovotarsk:anovotarsk/bmv2_variable_slice

Conversation

@anovotarsk

Copy link
Copy Markdown

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.

Copilot AI review requested due to automatic review settings July 28, 2026 12:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 StrengthReductionPolicy option to lower variable-offset IR::PlusSlice into Shr + constant Slice, 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.

Comment thread backends/bmv2/simple_switch/midend.cpp
Comment thread backends/bmv2/psa_switch/midend.cpp
Comment thread backends/bmv2/pna_nic/midend.cpp
Comment thread frontends/p4/strengthReduction.cpp Outdated
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@ChrisDodd thanks for the review. Removed the unsigned cast to preserve signed right-shift semantics and updated the test

@anovotarsk
anovotarsk marked this pull request as draft July 28, 2026 12:35
@anovotarsk
anovotarsk force-pushed the anovotarsk/bmv2_variable_slice branch 3 times, most recently from 186720f to 850b0a1 Compare July 28, 2026 13:09
@anovotarsk
anovotarsk marked this pull request as ready for review July 28, 2026 13:13
@anovotarsk
anovotarsk requested a review from ChrisDodd July 28, 2026 13:13
Signed-off-by: Artur Novotarskyi <artur.novotarskyi@gmail.com>
@anovotarsk
anovotarsk force-pushed the anovotarsk/bmv2_variable_slice branch 2 times, most recently from 850b0a1 to 02af277 Compare July 28, 2026 13:15
Signed-off-by: Artur Novotarskyi <artur.novotarskyi@gmail.com>
@fruffy

fruffy commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

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>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider adding an stf test to ensure the transformation works correctly.

Comment thread frontends/p4/strengthReduction.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variable offset in +: slicing causes compiler error

4 participants