Skip to content

Conversation

Azoy
Copy link
Contributor

@Azoy Azoy commented Oct 2, 2025

@Azoy Azoy requested a review from rjmccall as a code owner October 2, 2025 17:28
@Azoy
Copy link
Contributor Author

Azoy commented Oct 2, 2025

@swift-ci please smoke test

value = value.sextOrTrunc(IGM.SizeTy->getBitWidth());
} else {
value = value.zextOrTrunc(IGM.SizeTy->getBitWidth());
}
Copy link
Contributor

@rjmccall rjmccall Oct 2, 2025

Choose a reason for hiding this comment

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

This is exactly the same as just calling sextOrTrunc.

IntegerType::getValue() should probably document the value it returns, which appears to be that (1) it faithfully represents the written argument with arbitrary precision and (2) the bit width is the minimum necessary to represent the value correctly.

Also, since this has a trunc component, where (if anywhere) do we actually diagnose an integer generic argument that's out of the expressible range of Int?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, but in a theoretical future where we support UInt as values, then we'd need to come back here and use zextOrTrunc no?

Copy link
Contributor

@rjmccall rjmccall Oct 2, 2025

Choose a reason for hiding this comment

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

Yeah, but in a theoretical future where we support UInt as values, then we'd need to come back here and use zextOrTrunc no?

No, not in this case.

You need to use sextOrTrunc or zextOrTrunc appropriately according to the signedness of the source value, not its sign. Signedness is not something that APInt carries directly; isNegative() is just returning whether the high bit is set and should generally not be used if you actually have a fixed-width unsigned value. But here, IntegerType::getValue() is always returning an exact, arbitrary-width value with the correct sign bit for its mathematical value. Having a correct sign bit means it is a signed value, and it is always correct to use sextOrTrunc on it.

@Azoy
Copy link
Contributor Author

Azoy commented Oct 2, 2025

@swift-ci please smoke test

@Azoy Azoy merged commit 89052a7 into swiftlang:main Oct 4, 2025
3 checks passed
@Azoy Azoy deleted the negative-nancy branch October 4, 2025 16:18
Azoy added a commit to Azoy/swift that referenced this pull request Oct 7, 2025
[IRGen] Correctly sext instead of zext for negative integer types
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.

3 participants