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

Support for [lsb+:width] slices #4917

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ChrisDodd
Copy link
Contributor

Initial support for Verilog-style [lsb+:width] slices, allowing variable index slices with a constant width.

Most backends will probably require that these be strength-reduced to constant (normal) indexes, which will happen with eg. unrolled loops where the index depends on the loop index.

@ChrisDodd ChrisDodd force-pushed the cdodd-slice branch 2 times, most recently from 4a72192 to 422673f Compare September 17, 2024 01:34
@fruffy fruffy added the p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). label Sep 17, 2024
@ChrisDodd ChrisDodd force-pushed the cdodd-slice branch 5 times, most recently from 379c843 to 7ad797d Compare September 20, 2024 01:48
- allows for non-const lsb slices (width must still be const)

Signed-off-by: Chris Dodd <[email protected]>
static Expression make(Expression a, unsigned hi, unsigned lo);
}

class AbsSlice : Slice {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The name AbsSlice is a little unclear. I would write it out. Or change it to "ConstantSlice"?

Copy link
Contributor Author

@ChrisDodd ChrisDodd Sep 23, 2024

Choose a reason for hiding this comment

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

Abs here is short for Absolute, which seems a better, more descriptive name than Constant to me. I could make it AbsoluteSlice intead, but that just seems excessively wordy. Another possibility is something like FixedSlice. However, none of these names seem 100% correct as "this is obvious what it means"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I have begun to prefer explicit naming over abbreviations because it reduces the cognitive load slightly when dealing with a new code base.

So it looks like they are called Variable Slices in SystemVerilog. How about naming the base class AbstractSlice, Slice, VariableSlice? Or AbstractSlice Constant/Fixed/AbsoluteSlice VariableSlice.

Copy link
Contributor

Choose a reason for hiding this comment

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

AbstractSlice, Slice, VariableSlice

I'd lean towards this naming if possible

Copy link
Contributor Author

@ChrisDodd ChrisDodd Sep 24, 2024

Choose a reason for hiding this comment

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

I was avoiding renaming the abstract base class (Slice) so as to minimize changes to other parts of the code/backends that don't really care about the details of what kind of slice it is. I also used PlusSlice in case we also wanted to add MinusSlice for [msb-:width] slices in the future.

Slice(Util::SourceInfo si, Expression a, int hi, int lo)
: Operation_Ternary(si, IR::Type::Bits::get(hi-lo+1), a, new Constant(hi), new Constant(lo)) {}
Slice {
unsigned getH() const override { return e1->to<IR::Constant>()->asUnsigned(); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be a checked cast?

: Operation_Ternary(si, IR::Type::Bits::get(hi-lo+1), a, new Constant(hi), new Constant(lo)) {}
Slice {
unsigned getH() const override { return e1->to<IR::Constant>()->asUnsigned(); }
unsigned getL() const override { return e2->to<IR::Constant>()->asUnsigned(); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, checked cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These weren't checked casts before, so I was just keeping the same behavior.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, but this seems to lead a nullpointer access if the cast does not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which has the same net result -- an internal compiler error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

True, but checkedTo adds some more output to the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants