Skip to content

Conversation

@nikic
Copy link
Contributor

@nikic nikic commented Dec 9, 2025

Disable implicit truncation in the ConstantInt constructor by default. This means that it needs to be passed a signed/unsigned (depending on the IsSigned flag) value matching the bit width.

The intention is to prevent the recurring bug where people write something like ConstantInt::get(Ty, -1), and this "works" until Ty is larger than 64-bit and then the value is incorrect due to missing type extension.

This is the continuation of #112670, which originally allowed implicit truncation in this constructor to reduce initial scope of the change.

This PR is WIP. Most parts will be landed separately.

nikic added a commit that referenced this pull request Dec 12, 2025
Explicitly cast the value to (int) before negating, so it gets properly
sign extended. Otherwise we end up with a large unsigned value instead
of a negative value for large bit widths.

This was found while working on
#171456.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Dec 12, 2025
Explicitly cast the value to (int) before negating, so it gets properly
sign extended. Otherwise we end up with a large unsigned value instead
of a negative value for large bit widths.

This was found while working on
llvm/llvm-project#171456.
anonymouspc pushed a commit to anonymouspc/llvm that referenced this pull request Dec 15, 2025
Explicitly cast the value to (int) before negating, so it gets properly
sign extended. Otherwise we end up with a large unsigned value instead
of a negative value for large bit widths.

This was found while working on
llvm#171456.
nikic added a commit that referenced this pull request Dec 16, 2025
Use getSigned() to create the 1 or -1 constant, so it gets properly sign
extended.

This miscompile was found while working on
#171456.
nikic added 24 commits December 16, 2025 09:51
For now, we should implicitly truncate overflowing allocation
sizes when expanding GEPs, like we do in other places.
It's okay if the pattern value gets truncated here, it's a splat.
For non-fragile this is a negative value.
This may be -1 for incompete array types.
@nikic nikic force-pushed the constantint-implicit-trunc-false branch from 0eb2003 to 9e2cace Compare December 16, 2025 08:52
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Dec 16, 2025
…172301)

Use getSigned() to create the 1 or -1 constant, so it gets properly sign
extended.

This miscompile was found while working on
llvm/llvm-project#171456.
@github-actions
Copy link

github-actions bot commented Dec 16, 2025

🐧 Linux x64 Test Results

  • 194158 tests passed
  • 6293 tests skipped

✅ The build succeeded and all tests passed.

The value is always stored as an unsigned number, even if the
char type is signed, so we have to allow truncation here.
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.

1 participant