Skip to content

JIT: Fold SequenceEqual self-comparisons with constant length - #135307

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
aw0lid:fix/sequenceequal-constant-length-fold
Oct 7, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
aw0lid:fix/sequenceequal-constant-length-fold

Conversation

@aw0lid

@aw0lid aw0lid commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

The existing equal-VN optimization for SequenceEqual is only reached when the length is not a constant. As a result, self-comparisons with a constant length are not folded to true.

Move the equal-VN check before the constant-length check so the existing optimization applies regardless of whether the length is constant.

Closes #134431

@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 Oct 6, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 6, 2026
@EgorBo
EgorBo requested a balanced review from Copilot October 6, 2026 20:37

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.

Copilot review overview

🟡 Changes recommended

The codegen fix needs a targeted disassembly regression test.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Moves the existing equal-value-number check so constant-length SequenceEqual self-comparisons fold to true.

Changes:

  • Applies self-comparison folding before constant-length handling.
  • Preserves argument side effects while eliminating redundant comparisons.
File Description
src/​coreclr/​jit/​assertionprop.cpp Hoists the equal-VN optimization.

Comment thread src/coreclr/jit/assertionprop.cpp

@EgorBo EgorBo 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. Although, I suspect there is a pre-existing bug that may fold SequenceEqual(null, null, x) as true without side-effects 🤔

@EgorBo
EgorBo enabled auto-merge (squash) October 6, 2026 23:33
@EgorBo
EgorBo merged commit aa595bc into dotnet:main Oct 7, 2026
140 of 142 checks passed
@aw0lid

aw0lid commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@EgorBo I agree with your concern, if I understood it correctly.

With (arg1VN != ValueNumStore::NoVN) && (arg1VN == arg2VN), we fold the call to true, so the original SequenceEqual call is not executed. This means that any exception that would have been thrown inside the call is skipped.

I think we should avoid this optimization when either argument is known to be null and let the original call execute instead:

if (vnStore->IsVNNull(arg1VN) || vnStore->IsVNNull(arg2VN))
{
    return nullptr;
}

I'll verify this when I have some time.

@aw0lid
aw0lid deleted the fix/sequenceequal-constant-length-fold branch October 7, 2026 20:03
@aw0lid

aw0lid commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Just a small personal note: this was my first contribution to the JIT, so seeing it merged means a lot to me. Thanks for the review!

@EgorBo

EgorBo commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

I think we should avoid this optimization when either argument is known to be null and let the original call execute instead:

I'd just wrap 0/1 with explicit nullchecks for addresses - I'll file a PR later.

Just a small personal note: this was my first contribution to the JIT, so seeing it merged means a lot to me. Thanks for the review!

Haha, well, thanks for not trying to land a lot of changes at once like other first-time contributors try to do sometimes 🙂

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: x.SequenceEqual(x) is only folded to true when the length is *not* a constant

3 participants