Skip to content

Fix incorrect CoercedConstantValue usage in IsVNIntegralConstant - #128770

Merged
EgorBo merged 4 commits into
mainfrom
copilot/fix-coerced-constant-value-usages
Jun 2, 2026
Merged

EgorBo merged 4 commits into
mainfrom
copilot/fix-coerced-constant-value-usages

Conversation

Copilot AI commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

On 32-bit targets ssize_t is effectively int32_t, so CoercedConstantValue<ssize_t> truncates TYP_LONG values. In IsVNIntegralConstant, this truncation occurs before the FitsIn<T> check, causing arbitrary TYP_LONG constants to be incorrectly reported as fitting into smaller integral types (e.g. int32).

Changes

  • src/coreclr/jit/valuenum.h: read the constant as int64_t instead of ssize_t in IsVNIntegralConstant, preserving the full 64-bit value before the FitsIn<T> check.
// before
ssize_t val = CoercedConstantValue<ssize_t>(vn);
// after
int64_t val = CoercedConstantValue<int64_t>(vn);

TYP_INT constants are sign-extended to int64_t and TYP_LONG constants are preserved without truncation, so FitsIn<T> now evaluates the true value on all architectures.

Copilot AI review requested due to automatic review settings May 29, 2026 14:05
Copilot AI removed the request for review from Copilot May 29, 2026 14:05
Copilot AI linked an issue May 29, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot May 29, 2026 14:09
Copilot AI changed the title [WIP] Fix incorrect CoercedConstantValue<ssize_t> usages Fix incorrect CoercedConstantValue usage in IsVNIntegralConstant May 29, 2026
Copilot finished work on behalf of EgorBo May 29, 2026 14:11
Copilot AI requested a review from EgorBo May 29, 2026 14:11
Comment thread src/coreclr/jit/valuenum.h Resolved
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 29, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@EgorBo
EgorBo marked this pull request as ready for review May 29, 2026 15:39
Copilot AI review requested due to automatic review settings May 29, 2026 15:39

Copilot AI left a comment

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.

Pull request overview

This PR fixes IsVNIntegralConstant so JIT value-number constants are checked for integral fit after preserving the full 64-bit value, avoiding 32-bit host truncation of TYP_LONG constants.

Changes:

  • Reads VN integral constants as int64_t before applying FitsIn<T>.
  • Preserves existing behavior for TYP_INT while correctly rejecting oversized TYP_LONG values.

Comment thread src/coreclr/jit/valuenum.h Resolved

@tannergooding tannergooding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

I think the same issue might exist for ConstantValue<T>, looking at the flow

Copilot AI review requested due to automatic review settings June 1, 2026 12:13

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/coreclr/jit/valuenum.h:1324

  • This change fixes a 32-bit truncation bug that can affect value numbering and assertion propagation (e.g., long constants like 0x1_0000_0000L being incorrectly treated as fitting in a 32-bit type). Since this can potentially lead to incorrect optimization/miscompilation on 32-bit targets, please add a targeted JIT regression test that fails on x86 without the fix and passes with it (for example, a nested if where the outer compare is x < 0x1_0000_0000L and the inner branch checks x >= 0, validating that x=1 takes the non-negative path).
        int64_t val = CoercedConstantValue<int64_t>(vn);
        if (FitsIn<T>(val))
        {
            *value = static_cast<T>(val);
            return true;

@EgorBo
EgorBo merged commit a1b9d08 into main Jun 2, 2026
138 of 140 checks passed
@EgorBo
EgorBo deleted the copilot/fix-coerced-constant-value-usages branch June 2, 2026 23:35
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 3, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix incorrect CoercedConstantValue<ssize_t> usages

4 participants