Skip to content

Bind WASM ReadyToRun imports to declared modules - #138664

Open
jkoritzinsky wants to merge 9 commits into
dotnet:mainfrom
jkoritzinsky:dev/jkoritzinsky/wasm-r2r-direct-pinvoke-symbols
Open

jkoritzinsky wants to merge 9 commits into
dotnet:mainfrom
jkoritzinsky:dev/jkoritzinsky/wasm-r2r-direct-pinvoke-symbols

Conversation

@jkoritzinsky

@jkoritzinsky jkoritzinsky commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 023f5ece48de8f79e38ed34e0563b8e445324e41

Dimension Assessment Rationale
Change confidence 8/10 ↑ Final browser build and WASM ReadyToRun suite passed, including import and fixup coverage.
↓ Browser end-to-end execution was not rerun after the final changes.

There was no mechanism to provide module imports for user-owned [WasmImportLinkage] P/Invokes, so add that support.

WebAssembly ReadyToRun images can more efficiently bind [WasmImportLinkage] P/Invokes to their declared WebAssembly imports rather than resolving them through the generated P/Invoke table.

  • Emit module-qualified direct function imports for [WasmImportLinkage] and reuse host-provided imports when instantiating R2R Webcil images.
  • Allow direct ReadyToRun function symbols through the common WASM call lowering path
  • Align FreeGCHandle with its native void return type.
  • Add coverage for declared import modules, QCall and configured-module fixups, and WASM function index spaces.

Validation

  • .\build.cmd clr+libs -os browser -configuration Release /p:NuGetAudit=false - passed.
  • .\build.cmd -s clr.r2rtests -os browser -arch wasm -lc Release -c Release -test /p:NuGetAudit=false - passed: 399 passed, 43 skipped, 0 failed.
  • Browser end-to-end execution was not rerun after the final changes.

Note

This description was generated with GitHub Copilot.

jkoritzinsky and others added 9 commits December 1, 2026 16:56
Direct P/Invokes in WASM ReadyToRun images are now emitted as 'webcil'
function imports called directly, instead of going through the build-time
generated P/Invoke lookup table. The host resolves these imports against
the native runtime's wasmExports, and crossgen2's call-helper generator
emits callhelpers-exports.txt so link steps export the needed symbols.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
R2R WASM images now import [WasmImportLinkage] P/Invoke targets as module.entryPoint instead of routing them through the webcil exports or the generated P/Invoke table. The browser and corerun hosts resolve these imports from the runtime module's imports, or install throwing stubs to preserve lazy binding failures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
P/Invokes that are direct only because their module is listed in DirectPInvokes
go back to the PInvokeTarget fixup resolved through the generated table.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Remove the lazy-resolution Proxy import objects and throw-on-call stubs. The hosts now pass the runtime module's exports (webcil) and imports (WasmImportLinkage modules) directly, so missing imports fail at instantiation like any WASM import.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The browser and WASI corerun links listed every QCall export inline, which
exceeds the Windows command-line limit. Emscripten reads EXPORTED_FUNCTIONS
from a file and clang reads the WASI export options from a response file.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- Link Node corerun with WASM_BIGINT so QCall exports keep i64 signatures.
- Resolve JS-implemented QCalls from the runtime's env imports.
- Match FreeGCHandleForTypeHandle's managed return type to its native void.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Remove QCall export plumbing and QCall/FCall-specific Webcil imports while preserving plain WasmImportLinkage imports and configured direct P/Invoke behavior.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

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

@jkotas jkotas added the arch-wasm WebAssembly architecture label Dec 3, 2026
@jkoritzinsky
jkoritzinsky requested review from davidwrighton and a balanced review from Copilot December 3, 2026 01:14

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.

🟡 Changes recommended

Distinct signatures sharing a module and entrypoint get identical symbols and collapse into one import, causing WebAssembly type-validation failures for incompatible calls.

1 open issue
What changed in this PR

Adds direct binding of [WasmImportLinkage] P/Invokes to their declared modules in CoreCLR WebAssembly ReadyToRun images.

Changes:

  • Emits module-qualified imports and reuses host-provided imports during loading.
  • Unifies direct-call lowering and corrects FreeGCHandle’s return type.
  • Adds import, fixup, and function-index coverage.
File Description
src/​native/​libs/​Common/​JavaScript/​host/​assets.ts Reuses host imports for ReadyToRun loading.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun/​JitInterface/​CorInfoImpl.ReadyToRun.cs Selects direct imports for attributed P/Invokes.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun/​Compiler/​DependencyAnalysis/​ReadyToRunSymbolNodeFactory.cs Creates module-qualified import symbols.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun.Tests/​TestCasesRunner/​WasmR2RAssert.cs Accounts for imported functions in assertions.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun.Tests/​TestCases/​Webcil/​WasmImportLinkagePInvoke.cs Adds representative P/Invoke test declarations.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun.Tests/​TestCases/​R2RTestSuites.cs Tests declared modules, fixups, and indices.
src/​coreclr/​System.Private.CoreLib/​src/​System/​RuntimeHandles.cs Matches native FreeGCHandle’s void return.
src/​coreclr/​jit/​lowerwasm.cpp Allows direct ReadyToRun function symbols.
src/​coreclr/​jit/​lower.cpp Removes the redundant ReadyToRun special case.
src/​coreclr/​hosts/​corerun/​wasm/​libCorerun.js Supplies Emscripten imports to Webcil images.

Review effort: Balanced

private NodeCache<(string ImportModule, string ImportName, MethodSignature Signature), ExternFunctionSymbolNode> _wasmImportLinkagePInvokeTargets =
new NodeCache<(string ImportModule, string ImportName, MethodSignature Signature), ExternFunctionSymbolNode>(key =>
{
Utf8String symbolName = new Utf8String($"{key.ImportModule}#{key.ImportName}");
@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.

@pavelsavara

Copy link
Copy Markdown
Member

Does the [WasmImportLinkage][DllImport] comply with WASM ABI and LLVM extensions ?

Shall we add [WasmExportLinkage][UnmanagedCallersOnly] ?

cc @lewing

This branch has not been deployed

No deployments
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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants