Skip to content

JIT: Bypass bulk write barrier wrapper for small struct copies - #134345

Merged
EgorBo merged 7 commits into
dotnet:mainfrom
EgorBo:jit-small-bulk-writebarrier
Sep 22, 2026
Merged

EgorBo merged 7 commits into
dotnet:mainfrom
EgorBo:jit-small-bulk-writebarrier

Conversation

@EgorBo

@EgorBo EgorBo commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Bypass the managed bulk-write-barrier wrapper for GC-containing struct copies up to 128 bytes in JIT, ReadyToRun, and NativeAOT. Larger copies retain the existing helper.

        mov      r8d, 40
-       call     [CORINFO_HELP_BULK_WRITEBARRIER]
+       call     CORINFO_HELP_BULK_WRITEBARRIER_SMALL
        nop

        add      rsp, 40
        ret

Benchmark - shows 5-15% improvement for 5 refs struct.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d2554ff3-cb18-47b0-9789-b44b8d03f59f
Copilot AI lite review requested due to automatic review settings September 21, 2026 15:38
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging @dotnet/jit-contrib for JIT-EE GUID update

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

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
10 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.

@EgorBo

EgorBo commented Sep 21, 2026 •

Copy link
Copy Markdown
Member Author

@jkotas does this make sense as a micro-opt?

The current Bulk WB is calling a managed impl (very likely via an indirect call). the managed impl does a size check and calls FCall. Given JIT always knows the size of the layout and in most cases it's not too big, we can bypass that transition.

An additional version can be made for "definitely on the heap (aka unchecked)" to avoid the "is on the heap" check, but that is probably more churn for less benefits.

PS: not sure we need to abstract BULK_WRITEBARRIER_SMALL_SIZE under a JIT-EE. Presumably it's not a correctness issue if they don't match with whatever is used in the managed impl. It's just have to be not too big.

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

🔵 Needs a closer look

Preserve GC polling for repeated small copies and add regression coverage for the 512-byte and overlapping-copy cases.

Review effort: Lite
Findings: None

What changed in this PR

Adds a raw write-barrier path for small GC-containing struct copies while preserving existing fallbacks.

Changes:

  • Registers and versions the new helper.
  • Routes copies up to 512 bytes through the raw worker where supported.
  • Updates CoreCLR, NativeAOT, ReadyToRun, and WASM mappings.
File Reviewed changes
src/​coreclr/​vm/​corelib.h Registers the internal buffer helper.
src/​coreclr/​tools/​Common/​JitInterface/​CorInfoHelpFunc.cs Adds the helper enum entry.
src/​coreclr/​tools/​aot/​ILCompiler.RyuJit/​JitInterface/​CorInfoImpl.RyuJit.cs Uses the raw worker on 64-bit NativeAOT.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun/​JitInterface/​CorInfoImpl.ReadyToRun.cs Preserves the wrapper fallback.
src/​coreclr/​jit/​utils.cpp Marks the helper as heap-mutating.
src/​coreclr/​jit/​lower.cpp Selects the raw helper for small copies.
src/​coreclr/​jit/​gentree.cpp Handles VEX state for the helper.
src/​coreclr/​jit/​codegenwasm.cpp Adds the WASM helper signature.
src/​coreclr/​jit/​codegencommon.cpp Includes the helper in debug queries.
src/​coreclr/​inc/​jithelpers.h Registers the dynamic helper.
src/​coreclr/​inc/​jiteeversionguid.h Updates interface versioning.
src/​coreclr/​inc/​corinfo.h Adds the native helper entry and contract.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d2554ff3-cb18-47b0-9789-b44b8d03f59f
Copilot AI review requested due to automatic review settings September 21, 2026 17:14
Comment thread src/coreclr/jit/lower.cpp Outdated

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

lower.cpp has unresolved critical WASM GC-polling and moderate DEBUG GC-stress issues.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)

Comment thread src/coreclr/jit/lower.cpp
Comment thread src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs Outdated
Limit direct copies to 128 bytes and add the R2R helper ID, versioning, and NativeAOT Wasm ABI mapping.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d2554ff3-cb18-47b0-9789-b44b8d03f59f
Copilot AI review requested due to automatic review settings September 21, 2026 18:33

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

CoreLib ReadyToRun rooting, NativeAOT WASM entry-point, and no-GC metadata issues remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 High severity

Open (2)
Resolved since last review (1)

Comment thread src/coreclr/inc/jithelpers.h
Comment thread src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/JitHelper.cs Outdated
Comment thread src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/JitHelper.cs Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d2554ff3-cb18-47b0-9789-b44b8d03f59f
Copilot AI review requested due to automatic review settings September 21, 2026 18:52

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 helper’s GC-safepoint classification requires correction, and boundary plus AOT/ReadyToRun regression coverage is missing.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (2)

Comment thread src/coreclr/jit/utils.cpp Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 21, 2026 19:05

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

🔵 Needs a closer look

Three unresolved moderate findings remain: incomplete ReadyToRun rooting, missing safepoint polling, and missing cutoff GC-stress coverage.

Review effort: Lite
Findings: None

Resolved since last review (1)

Copilot AI review requested due to automatic review settings September 21, 2026 19:23

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

🔵 Needs a closer look

Address the missing boundary coverage and preserve a GC poll for repeated small copies.

Review effort: Lite
Findings: None

Previously missed (1)

In code that hasn't changed since last review

Medium severity Add boundary and AOT coverage for 128-byte helper mapping

src/​coreclr/​jit/​lower.cpp:10402

This introduces a new 128-byte boundary and deliberately removes the GC poll for the <=128-byte path, but there is no targeted coverage for the exact 128/129 split or for the new helper through ReadyToRun/NativeAOT. Please add a focused runtime/codegen test covering both sides of the boundary so an off-by-one or incomplete helper mapping cannot silently change GC safepoint behavior.

@EgorBo
EgorBo enabled auto-merge (squash) September 21, 2026 23:05
@EgorBo
EgorBo merged commit 30ef944 into dotnet:main Sep 22, 2026
125 of 126 checks passed
@EgorBo
EgorBo deleted the jit-small-bulk-writebarrier branch September 22, 2026 11:49
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 23, 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