Skip to content

Absolute/global addressing changes for native AOT/WASM - #135768

Merged
MichalStrehovsky merged 2 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-wasm-absolute-addresses
Oct 19, 2026
Merged

MichalStrehovsky merged 2 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-wasm-absolute-addresses

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Part of a series of pull requests that enable compiling, linking, and running a Console.WriteLine hello world on WASI/native AOT.

Summary

Use linker-relocated absolute addresses for NativeAOT Wasm instead of CoreCLR's image-base-relative model:

  • Emit absolute signed address constants and JIT-data addresses without adding an image-base global.
  • Fold scalar global loads/stores into absolute memarg relocations, using zero as the address operand.
  • Preserve function-pointer materialization by mapping function-targeted WASM_MEMORY_ADDR_SLEB relocations to table-index relocations.

emitImageBase() remains R2R-only, and CoreCLR's image-relative paths are unchanged. This matches the WASI SDK clang non-PIC global-access model.

Validation

  • .\src\coreclr\build-runtime.cmd -checked -x64 -component wasmjit - passed.
  • Local verify-global-codegen.ps1 - 10/10 methods passed compilation, object/executable validation, and Wasmtime execution; 30 value comparisons passed across integer, long, double, address-taking, and nonzero field-offset cases.
  • Local interface-codegen controls - 6/6 Wasm cases and 6/6 Windows x64 compilation cases passed.
  • Both real-CoreLib Hello variants were recompiled after the R2R-only image-base cleanup, linked, and executed successfully with the complete series.

These results are from the combined local bringup tree, including prerequisite NativeAOT/Wasm enablement, not a fresh build of this isolated main-based branch. The probe scripts are local fixtures, not tests added by this PR.

Note

This PR description was generated by GitHub Copilot.

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

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

@MichalStrehovsky MichalStrehovsky added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed area-NativeAOT-coreclr labels Oct 14, 2026
@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.

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.

🔵 Needs a closer look

Cross-layer codegen and relocation changes need maintainer review and reproducible validation beyond the reported combined-tree probes; no builds or tests were run here.

1 open finding
What changed in this PR

Advances NativeAOT/WASI support by using linker-relocated absolute addresses while preserving CoreCLR’s image-relative addressing.

Changes:

  • Selects absolute relocations for NativeAOT addresses and JIT data.
  • Folds global memory accesses into relocated offsets.
  • Maps function-address relocations to table indices.
File Description
src/​coreclr/​tools/​Common/​Compiler/​ObjectWriter/​WasmRelocatableObjectWriter.cs Maps absolute function relocations to table indices.
src/​coreclr/​jit/​lowerwasm.cpp Enables NativeAOT global-address folding.
src/​coreclr/​jit/​lir.h Documents expanded FoldedAddr semantics.
src/​coreclr/​jit/​emitwasm.cpp Selects absolute or image-relative address emission.
src/​coreclr/​jit/​codegenwasm.cpp Emits zero-base folded global accesses.

🧠 Review effort: Balanced

Comment thread src/coreclr/jit/emitwasm.cpp Resolved
This is so we can emit proper relocs, not offsets from__image_base.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c78266f3-08e3-4918-b2e5-9fe0ddf2ca17
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib could someone have a look? this is blocking progress for native AOT WASM. We cannot start testing anything in the CI if memory access doesn't work and native AOT WASM already got broken twice since I started working on it.

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

Looking now! This one was on my list.

@adamperlin

adamperlin commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

@MichalStrehovsky I'm not sure we need to use LIR::Flags::FoldedAddr here. I think for NativeAOT address codegen can just be a relocated constant for the absolute address like:

i32.const <reloc> // codegen for addr
...
i32.{load,store} offset=0 // codegen for parent load/store

If there's no base/offset split in the addr then I don't think we save any instructions by using the FoldedAddr flag.

Materialize relocated absolute addresses directly and use the ordinary zero-offset store path. Keep FoldedAddr limited to local frame addresses.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c78266f3-08e3-4918-b2e5-9fe0ddf2ca17
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

@MichalStrehovsky I'm not sure we need to use LIR::Flags::FoldedAddr here. I think for NativeAOT address codegen can just be a relocated constant for the absolute address like:

Yeah, the form in the PR matches clang codegen but there doesn't seem to be an advantage in that.

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

This LGTM now!

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/ba-g lots of mutex failures that are known

@MichalStrehovsky
MichalStrehovsky enabled auto-merge (squash) October 19, 2026 00:02
@MichalStrehovsky
MichalStrehovsky merged commit e0e9922 into dotnet:main Oct 19, 2026
126 of 129 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the nativeaot-wasm-absolute-addresses branch October 19, 2026 00:02
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture 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