Skip to content

Fix warnings on signed integer initialization - #4309

Open
AndrzejSawula wants to merge 2 commits into
p4lang:mainfrom
AndrzejSawula:lang-issue1269-fix-int-overflow-warning
Open

Fix warnings on signed integer initialization#4309
AndrzejSawula wants to merge 2 commits into
p4lang:mainfrom
AndrzejSawula:lang-issue1269-fix-int-overflow-warning

Conversation

@AndrzejSawula

@AndrzejSawula AndrzejSawula commented Dec 22, 2023

Copy link
Copy Markdown

Remove misleading overflow warning for correct-width signed integer literals with MSBit set, i.e. representing negative numbers. This is actually the only way integer literals of explicitly-declared width can be specified (as per language specs, Section 6.3.3.2).

As these warnings are mentioned in the language specs, Section 7.1.6.6, this change is accompanied with one to the specs content, PR #1270.

Comment thread ir/expression.cpp
big_int mask = Util::mask(width);

if (tb->isSigned) {
auto masked_value = value;

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.

The logic here looks fairly complex. Possible to add some tests to https://github.com/p4lang/p4c/blob/main/test/gtest/constant_expr_test.cpp which exercise this code?

Comment thread ir/expression.cpp
masked_value = value & mask;
}
}
if (is_overflow) {

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.

This big block of code seems to be equivalent to just changing one line

big_int max = (one << width) -1;

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.

Wouldn't this need to be adjusted based on signedness (value >= 0, value <0)?

@asawulaINTC Can you comment?

@AndrzejSawula
AndrzejSawula force-pushed the lang-issue1269-fix-int-overflow-warning branch from eec5c29 to 16a7248 Compare January 9, 2024 14:55
@AndrzejSawula
AndrzejSawula force-pushed the lang-issue1269-fix-int-overflow-warning branch from 16a7248 to 6b94bd7 Compare January 9, 2024 15:33
@AndrzejSawula
AndrzejSawula requested a review from fruffy January 10, 2024 08:29
EXPECT_EQ(c5.asUnsigned(), 5461);

// overflow, but this should not even generate a warning under regular settings
auto c6 = IR::Constant(&t13s, 0x3555); // 0b1_0101_0101_0101

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.

It should be possible to get the number of warnings triggered with diagnosticCount().

@fruffy fruffy left a comment

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.

Thanks for the tests.

@jonathan-dilorenzo

Copy link
Copy Markdown
Contributor

It looks like this didn't come up yet in the corresponding spec change, but, based on discussions in the LDWG, I think we'd prefer to keep the warnings on signed integer initialization unless you have a use-case that doesn't fit our imagination in mind. Let me add a comment to the specification issue: github.com/p4lang/p4-spec/issues/1269

@ChrisDodd

Copy link
Copy Markdown
Contributor

I've been experimenting a bit with this and another implementation issue I see is that it does not appear to be possible to stop specific warnings with explicit casts -- that is, something like (bit<8>)10w256 will give a warning about 256 being out of range for a bit<8>. This seems wrong (and will cause problems when using -Werror if you have code that wants the expected modulo behavior for the code). This should probably be a separate bug filed against p4c.

@fruffy fruffy added the p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). label Feb 25, 2024
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.

4 participants