Skip to content

Add WASM PerfMap support: OS/arch constants and correct R2R offsets - #132693

Merged
pavelsavara merged 4 commits into
dotnet:mainfrom
pavelsavara:PerfMap_wasm
Aug 26, 2026
Merged

Add WASM PerfMap support: OS/arch constants and correct R2R offsets#132693
pavelsavara merged 4 commits into
dotnet:mainfrom
pavelsavara:PerfMap_wasm

Conversation

@pavelsavara

@pavelsavarapavelsavara commented Aug 24, 2026

Copy link
Copy Markdown
Member

Summary

Enables the ReadyToRun perfmap (.ni.r2rmap) for browser-wasm / wasi-wasm targets. Two sequential crashes were blocking it when PublishReadyToRunPerfmapFormatVersion was set:

  1. NotImplementedException in PerfMapWriter.TranslateTargetDetailsToPerfmapConstantsPerfMapOSToken had no Browser/Wasi members and PerfMapArchitectureToken had no Wasm member, so the switch expressions hit their throwing default arms.
  2. ArgumentOutOfRangeException in OutputInfoBuilder.EnumerateMethods (_sections[node.SectionIndex]) — the wasm object writers never populated _outputSectionLayout, unlike COFF/ELF/Mach-O/PE, so method nodes carried a valid SectionIndex but there were no OutputSections to index.

Changes

PerfMap constants

  • Add Browser = 8 and Wasi = 9 to PerfMapOSToken, and Wasm = 7 to PerfMapArchitectureToken (appended after the highest existing value to preserve the numeric contract consumed by symbol-upload / PerfView tooling).
  • Map TargetOS.Browser/TargetOS.Wasi and TargetArchitecture.Wasm32 in the PerfMapWriter switches.
  • Update docs/design/coreclr/botr/r2r-perfmap-format.md, also filling in the already-shipped OpenBSD/RiscV64/LoongArch64 rows.

Correct wasm R2R offsets

  • Populate _outputSectionLayout in WebCilObjectWriter.EmitObjectFile (index-aligned with the wasm section table); the code section records the module-file offset where its function-body content lands.
  • The code section is LEB128-shrunk during final emission (ResolveCodeRelocations), so each method's recorded (pre-shrink) offset no longer matches the final module. Build a pre→post entry-boundary offset map and remap every method node's offset and length to its post-shrink position, so a method's RVA + Length equals the next code entry's offset.
  • Document the perfmap offset scheme in docs/design/mono/webcil.md.

Address space / consumers

For wasm, a method's perfmap RVA is the byte offset from the start of the .wasm module to its function entry in the Code section — a file offset into the module, not a virtual address or a Webcil payload RVA.

Note

The only meaningful consumer of these wasm file offsets is a native-wasm profiler that keys frames by a code offset within the module (e.g. a wasm sampling profiler / DevTools-style correlation). EventPipe- and dotnet-trace-based symbolication identifies managed methods by module MVID + metadata token via method-load events and does not rely on these perfmap offsets. The perfmap is opt-in diagnostics; nothing in the runtime reads it.

Validation

  • ILCompiler.ReadyToRun builds clean (Release).
  • Ran the rebuilt wasm crossgen2 on the IL System.Private.CoreLib.dll with --perfmap --perfmap-format-version:1 --obj-format:wasm --targetarch:wasm --targetos:browser: exit 0 (previously ArgumentOutOfRangeException), produced a .ni.r2rmap with 47,731 methods, header tokens OS=8 (Browser) / arch=7 (Wasm) / version 1, and contiguous, non-overlapping method RVAs (confirming both offset and length are post-shrink correct).

Note

This PR description and the code changes were generated with the assistance of GitHub Copilot.

Crossgen2 R2R compilation for browser-wasm and wasi-wasm targets threw
System.NotImplementedException from PerfMapWriter.TranslateTargetDetailsToPerfmapConstants
because PerfMapOSToken had no Browser/Wasi members and PerfMapArchitectureToken
had no Wasm member, so the switch expressions hit their throwing default arms
when PublishReadyToRunPerfmapFormatVersion was set.
Add Browser/Wasi to PerfMapOSToken, Wasm to PerfMapArchitectureToken, map the
corresponding TargetOS/TargetArchitecture values, and update the r2r-perfmap-format
doc to match (also filling in the already-shipped OpenBSD/RiscV64/LoongArch64 rows).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CopilotAI lite review requested due to automatic review settings August 24, 2026 11:14
@pavelsavarapavelsavara self-assigned this Aug 24, 2026
@pavelsavarapavelsavara added the arch-wasm WebAssembly architecture label Aug 24, 2026
@pavelsavarapavelsavara added this to the 12.0.0 milestone Aug 24, 2026
@azure-pipelines

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

CopilotAI 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

Adds missing PerfMap v1 target OS/architecture tokens and wiring so Crossgen2 ReadyToRun perfmap emission can support WebAssembly targets (Browser/WASI) without falling into the “unknown target” throwing paths. Also updates the design doc to reflect the expanded token sets.

Changes:

  • Extend PerfMapOSToken with Browser/Wasi and PerfMapArchitectureToken with Wasm.
  • Map TargetOS.Browser/TargetOS.Wasi and TargetArchitecture.Wasm32 to the new tokens in PerfMapWriter.
  • Update r2r-perfmap-format.md to include the current token tables.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

FileDescription
src/coreclr/tools/aot/ILCompiler.Diagnostics/ReadyToRunDiagnosticsConstants.csAdds new perfmap OS/arch enum values used as the on-disk perfmap contract.
src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.csExtends target-to-token translation to cover Browser/WASI and Wasm32.
docs/design/coreclr/botr/r2r-perfmap-format.mdDocuments the updated token values for perfmap v1 headers.

@pavelsavara
pavelsavara marked this pull request as draft August 24, 2026 16:43
Populate the output section layout for the wasm/webcil object writer so OutputInfoBuilder.EnumerateMethods can resolve each method node's section; it previously threw ArgumentOutOfRangeException because the wasm writers never populated _outputSectionLayout. Remap each method node's offset and length to its post-shrink position (the code section is LEB128-shrunk during final emission) so perfmap entries point at the method's real file offset in the emitted module. Document the perfmap offset scheme in webcil.md.
@pavelsavarapavelsavara changed the title Add PerfMap OS/arch constants for wasm and wasi targetsAdd wasm/wasi PerfMap support: OS/arch constants and correct R2R offsetsAug 24, 2026
@pavelsavara
pavelsavara marked this pull request as ready for review August 24, 2026 21:30
CopilotAI review requested due to automatic review settings August 24, 2026 21:30
@azure-pipelines

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

CopilotAI 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 6 out of 6 changed files in this pull request and generated 2 comments.

@pavelsavarapavelsavara changed the title Add wasm/wasi PerfMap support: OS/arch constants and correct R2R offsetsAdd WASM PerfMap support: OS/arch constants and correct R2R offsetsAug 24, 2026

@adamperlinadamperlin 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.

Overall looks good to me, I just had a suggestion to clarify a comment.

Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/WebCilObjectWriter.cs Outdated
…ter.cs
Co-authored-by: Adam Perlin <adamp@nanosoft.com>
CopilotAI review requested due to automatic review settings August 26, 2026 08:42

CopilotAI 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 6 out of 6 changed files in this pull request and generated no new comments.

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

src/coreclr/tools/Common/Compiler/ObjectWriter/OutputInfoBuilder.cs:185

  • RemapMethodNodeOffsets updates OutputNode.Offset/Length, but leaves OutputSymbol.Offset unchanged. This makes the node/symbol lists inconsistent, and MapFileBuilder’s FindSymbol(node, ...) binary search can stop finding the corresponding symbol for code-section nodes once node offsets are remapped (affecting --map / --mapcsv output for wasm). Also consider checked when recomputing node.Offset + node.Length / casting the new length back to int to avoid silent wrap/truncation on unexpectedly large sections.
 internal void RemapMethodNodeOffsets(int sectionIndex, IReadOnlyDictionary<ulong, ulong> offsetMap)
{
foreach (OutputNode node in _nodes)
{
if (node.SectionIndex == sectionIndex
&& offsetMap.TryGetValue(node.Offset, out ulong postStart)
&& offsetMap.TryGetValue(node.Offset + (ulong)node.Length, out ulong postEnd))
{

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

Labels

arch-wasmWebAssembly architecturearea-ReadyToRun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@pavelsavara@lewing@adamperlin