Skip to content

JIT: Fix profiled memcmp specialization for inline candidates - #134437

Merged
EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:jit-fix-memcmp-value-profiling
Sep 23, 2026
Merged

EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:jit-fix-memcmp-value-profiling

Conversation

@EgorBo

@EgorBo EgorBo commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Since #134160 led to 69 benchmarks improved, I decided to do the same for memcmp idiom.

Benchmark

using BenchmarkDotNet.Attributes;

public class Bench
{
    private byte[] _bytes1 = null!;
    private byte[] _bytes2 = null!;

    [Params(1, 2, 5, 8, 16, 20, 32, 50, 64, 128)]
    public int Length { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        _bytes1 = new byte[Length];
        _bytes2 = new byte[Length];
    }

    [Benchmark]
    public bool Bytes() => _bytes1.AsSpan().SequenceEqual(_bytes2);
}

Results: EgorBot/Benchmarks#612 (arm64 only handles up to 32 bytes today).

Try length specialization before inline-candidate wrapping and clear candidate metadata only when a usable profile is selected.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e9c37cab-1590-4d11-a950-db4e7f065a3d
Copilot AI lite review requested due to automatic review settings September 22, 2026 16:55
@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 Sep 22, 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

Copy link
Copy Markdown
Contributor

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

This comment was marked as resolved.

@EgorBo

This comment was marked as outdated.

Match profiling to the lowering ISA limit rather than preferred vector width, and avoid specializing unsupported targets.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e9c37cab-1590-4d11-a950-db4e7f065a3d
Copilot AI review requested due to automatic review settings September 22, 2026 18:09
@EgorBo

This comment was marked as resolved.

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

Address the fallback-threshold regression and add TieredPGO regression coverage.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

Comment thread src/coreclr/jit/compiler.h
@EgorBo

EgorBo commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

@EgorBot -macos_arm -linux_amd -linux_arm

using BenchmarkDotNet.Attributes;

public class Bench
{
    private byte[] _bytes1 = null!;
    private byte[] _bytes2 = null!;

    [Params(1, 2, 5, 8, 16, 20, 32, 50, 64, 128)]
    public int Length { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        _bytes1 = new byte[Length];
        _bytes2 = new byte[Length];
    }

    [Benchmark]
    public bool Bytes() => _bytes1.AsSpan().SequenceEqual(_bytes2);
}

@EgorBo

EgorBo commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

PTAL @AndyAyersMS follow up to #134160 with improvements for SequenceEqual this time.

@EgorBo
EgorBo requested a review from AndyAyersMS September 22, 2026 20:02
@AndyAyersMS

Copy link
Copy Markdown
Member

I wonder how often this actually kicks in for realistic code. Do we have any benchmarks that run a mixture of lengths? Have you considered multi-guess for things like this?

@EgorBo

EgorBo commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

I wonder how often this actually kicks in for realistic code. Do we have any benchmarks that run a mixture of lengths? Have you considered multi-guess for things like this?

I expect it to be pretty common. It's basically what all string comparisons/startswith/endswith call under the hood. Because we can instrument inlined code now, this one actually manages to keep profile per call-site (acts as context-oriented).

Basically, SequenceEqual is a work horse for all comparisons of arrays/spans of anything (blittable)

@EgorBo

EgorBo commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

/ba-g osx-arm64 stuck

@EgorBo
EgorBo merged commit 4f4b95c into dotnet:main Sep 23, 2026
135 of 138 checks passed
@EgorBo
EgorBo deleted the jit-fix-memcmp-value-profiling branch September 23, 2026 11:12
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 24, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants