Skip to content

[wip][Wasm] Emit R2R branch hints from profile data - #133795

Closed
lewing wants to merge 1 commit into
mainfrom
lewing-wasm-r2r-branch-hinting
Closed

lewing wants to merge 1 commit into
mainfrom
lewing-wasm-r2r-branch-hinting

Conversation

@lewing

@lewing lewing commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

Experimental implementation of #133794: emit the standard WebAssembly metadata.code.branch_hint custom section from CoreCLR ReadyToRun when static PGO shows a strongly biased branch. Engines that don't consume branch hints ignore the section.

  • RyuJIT selects hints after final wasm layout, requiring trustworthy static PGO and an 80/20 likelihood, and reports function-relative br_if offsets through the JIT metadata channel (reset on internal retries).
  • crossgen2 keeps hints as per-method side data; the object writer resolves final function indices, emits one sorted section before the code section, and adjusts offsets when relocation LEBs shrink.
  • Relocatable output stays unsupported. JitWasmBranchHintStress enables deterministic tests with synthesized profiles.

The section adds 32 bytes to the test module. Before this can merge it needs end-to-end measurements on engines that consume hints: startup and steady-state time, engine compile time, native code size, and R2R image size.

Note

This pull request description was drafted with GitHub Copilot.

Co-authored-by: Copilot App <[email protected]>
@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: @dotnet/crossgen-contrib
See info in area-owners.md if you want to be subscribed.

@lewing lewing added the arch-wasm WebAssembly architecture label Sep 12, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@lewing lewing changed the title [Wasm] Emit R2R branch hints from profile data [wip][Wasm] Emit R2R branch hints from profile data Sep 12, 2026
@lewing lewing added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Sep 12, 2026
@lewing

lewing commented Sep 12, 2026

Copy link
Copy Markdown
Member Author

I'm exploring tooling changes against this prototype.

}
metadata[8] = branchHint == WasmBranchHint::LikelyTrue ? 1 : 0;

JitMetadata::report(m_compiler, JitMetadata::WasmBranchHint, metadata, sizeof(metadata));

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.

We don't want to call report here, it will get called automatically when the jit is done with the method. Instead, increment the metric.

Comment thread src/coreclr/jit/emit.cpp
instrDesc* prevId = nullptr;
#endif // defined(DEBUG) && defined(TARGET_ARM64)

#ifdef TARGET_WASM

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.

Omit this (see below).

If you want to know what % of branches got hinted it will take more work.

assert(trueTarget != block->Next());

WasmBranchHint branchHint = WasmBranchHint::None;
bool const hasStaticProfile =

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.

I would just call fgHaveProfileWeights here and not worry about the source or quantity.

WasmBranchHint branchHint = WasmBranchHint::None;
bool const hasStaticProfile =
(m_compiler->fgPgoSource == ICorJitInfo::PgoSource::Static) && m_compiler->fgHaveSufficientProfileWeights();
if (hasStaticProfile || (JitConfig.JitWasmBranchHintStress() != 0))

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.

Instead of forcing on hints then you could force them off here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-ReadyToRun NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants