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

Strength reduction should not remove some casts #3226

Merged
merged 4 commits into from
Apr 20, 2022

Conversation

mihaibudiu
Copy link
Contributor

Signed-off-by: Mihai Budiu mbudiu@vmware.com

Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
@mihaibudiu
Copy link
Contributor Author

Fixed #3225

if (expr->getH() < size_t(cast->expr->type->width_bits())) {
expr->e0 = cast->expr;
if (auto tb = cast->expr->type->to<IR::Type_Bits>()) {
auto type = expr->type->to<IR::Type_Bits>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use checkedTo here?

if (auto tb = cast->expr->type->to<IR::Type_Bits>()) {
auto type = expr->type->to<IR::Type_Bits>();
CHECK_NULL(type);
if (tb->isSigned != type->isSigned)
Copy link
Collaborator

@fruffy fruffy Apr 19, 2022

Choose a reason for hiding this comment

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

I always assumed that slices of signed bit strings will produce an unsigned bit string. This should probably ensure that a cast to int<W> is converted into a bit<W>. A cast from int<W> to bit<W> should be acceptable though?

This is what the spec says for slicing int<W>:

The result is an unsigned bit-string of width m - l + 1, including the bits numbered from l (which becomes the least significant bit of the result) to m (the most significant bit of the result) from the source operand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added another test case which fails without this change; it's in the test file:
h.h.b = ((bit<1>)h.h.c)[0:0];

Copy link
Collaborator

@fruffy fruffy Apr 19, 2022

Choose a reason for hiding this comment

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

But
((bit<1>)h.h.c)[0:0];
to
((bit<1>)h.h.c);
should be acceptable, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The previous result was just h.h.c, which failed to typecheck.

@fruffy
Copy link
Collaborator

fruffy commented Apr 19, 2022

The only thing is to to add a new member d to the header and assign c to that one. Otherwise, the assignment is optimized away.

Mihai Budiu added 2 commits April 19, 2022 15:39
Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
@fruffy
Copy link
Collaborator

fruffy commented Apr 20, 2022

Is this ready to be merged?

@mihaibudiu mihaibudiu merged commit 094f962 into p4lang:main Apr 20, 2022
@mihaibudiu mihaibudiu deleted the issue3225 branch April 20, 2022 22:16
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.

2 participants