-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[wasm][coreclr] First part of the call generator, interp to native #121491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
radekdoulik
merged 11 commits into
dotnet:main
from
radekdoulik:clr-interp-wasm-callhelpers-generator-1st-part
Nov 18, 2025
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a8486e5
[wasm][coreclr] First part of the call generator, interp to native
radekdoulik b21949f
Feedback
radekdoulik 8e966ad
Feedback
radekdoulik ddecf73
Feedback
radekdoulik 497ef22
Feedback
radekdoulik 68882f1
Feedback
radekdoulik b25f206
Feedback
radekdoulik 9771385
Feedback
radekdoulik 5bfe1a2
Feedback
radekdoulik 03b4cc6
Merge remote-tracking branch 'remotes/origin/main' into clr-interp-wa…
radekdoulik 880e5e1
Feedback
radekdoulik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
419 changes: 142 additions & 277 deletions
419
src/coreclr/vm/wasm/callhelpers.cpp → ...vm/wasm/callhelpers-interp-to-managed.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // | ||
|
|
||
| #include <callhelpers.hpp> | ||
|
|
||
| // Define reverse thunks here | ||
|
|
||
| // Entry point for interpreted method execution from unmanaged code | ||
| class MethodDesc; | ||
|
|
||
| // WASM-TODO: The method lookup would ideally be fully qualified assembly and then methodDef token. | ||
| // The current approach has limitations with overloaded methods. | ||
| extern "C" void LookupMethodByName(const char* fullQualifiedTypeName, const char* methodName, MethodDesc** ppMD); | ||
| extern "C" void ExecuteInterpretedMethodFromUnmanaged(MethodDesc* pMD, int8_t* args, size_t argSize, int8_t* ret); | ||
|
|
||
| static MethodDesc* MD_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler_Void_RetVoid = nullptr; | ||
| static void Call_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler() | ||
| { | ||
| // Lazy lookup of MethodDesc for the function export scenario. | ||
| if (!MD_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler_Void_RetVoid) | ||
| { | ||
| LookupMethodByName("System.Threading.ThreadPool, System.Private.CoreLib", "BackgroundJobHandler", &MD_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler_Void_RetVoid); | ||
| } | ||
| ExecuteInterpretedMethodFromUnmanaged(MD_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler_Void_RetVoid, nullptr, 0, nullptr); | ||
| } | ||
|
|
||
| extern "C" void SystemJS_ExecuteBackgroundJobCallback() | ||
| { | ||
| Call_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler(); | ||
| } | ||
|
|
||
| static MethodDesc* MD_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler_Void_RetVoid = nullptr; | ||
| static void Call_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler() | ||
| { | ||
| // Lazy lookup of MethodDesc for the function export scenario. | ||
| if (!MD_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler_Void_RetVoid) | ||
| { | ||
| LookupMethodByName("System.Threading.TimerQueue, System.Private.CoreLib", "TimerHandler", &MD_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler_Void_RetVoid); | ||
| } | ||
| ExecuteInterpretedMethodFromUnmanaged(MD_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler_Void_RetVoid, nullptr, 0, nullptr); | ||
| } | ||
|
|
||
| extern "C" void SystemJS_ExecuteTimerCallback() | ||
| { | ||
| Call_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler(); | ||
| } | ||
|
|
||
| extern const ReverseThunkMapEntry g_ReverseThunks[] = | ||
| { | ||
| { 100678287, { &MD_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler_Void_RetVoid, (void*)&Call_System_Private_CoreLib_System_Threading_ThreadPool_BackgroundJobHandler } }, | ||
| { 100678363, { &MD_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler_Void_RetVoid, (void*)&Call_System_Private_CoreLib_System_Threading_TimerQueue_TimerHandler } }, | ||
| }; | ||
|
|
||
| const size_t g_ReverseThunksCount = sizeof(g_ReverseThunks) / sizeof(g_ReverseThunks[0]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/tasks/WasmAppBuilder/coreclr/InterpToNativeGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using Microsoft.Build.Utilities; | ||
| using Microsoft.Build.Framework; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using WasmAppBuilder; | ||
|
|
||
| using JoinedString; | ||
| // | ||
| // This class generates the g_wasmThunks array and CallFunc_* functions used by the CoreCLR interpreter to call native code on WASM. | ||
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // The generated code should be kept in sync with the corresponding CoreCLR runtime code that consumes these thunks and call functions. | ||
| // | ||
|
|
||
| #nullable enable | ||
|
|
||
| internal sealed class InterpToNativeGenerator | ||
| { | ||
| private LogAdapter Log { get; set; } | ||
|
|
||
| public InterpToNativeGenerator(LogAdapter log) => Log = log; | ||
|
|
||
| public void Generate(IEnumerable<string> cookies, string outputPath) | ||
| { | ||
| using TempFileName tmpFileName = new(); | ||
| using (var w = File.CreateText(tmpFileName.Path)) | ||
| { | ||
| Emit(w, cookies); | ||
| } | ||
|
|
||
| if (Utils.CopyIfDifferent(tmpFileName.Path, outputPath, useHash: false)) | ||
| Log.LogMessage(MessageImportance.Low, $"Generating managed2native table to '{outputPath}'."); | ||
| else | ||
| Log.LogMessage(MessageImportance.Low, $"Managed2native table in {outputPath} is unchanged."); | ||
| } | ||
|
|
||
| private static string SignatureToArguments(string signature) | ||
| { | ||
| if (signature.Length <= 1) | ||
| return "void"; | ||
|
|
||
| return string.Join(", ", signature.Skip(1).Select(static c => SignatureMapper.CharToNativeType(c))); | ||
| } | ||
|
|
||
| private static string CallFuncName(IEnumerable<char> args, string result) | ||
| { | ||
| var paramTypes = args.Any() ? args.Join("_", (p, i) => SignatureMapper.CharToNameType(p)).ToString() : "Void"; | ||
|
|
||
| return $"CallFunc_{paramTypes}_Ret{result}"; | ||
| } | ||
|
|
||
| private static void Emit(StreamWriter w, IEnumerable<string> cookies) | ||
| { | ||
| // Use OrderBy because Order() is not available on .NET Framework | ||
| var signatures = cookies.OrderBy(c => c).Distinct().ToArray(); | ||
| Array.Sort(signatures, StringComparer.Ordinal); | ||
|
|
||
| w.Write( | ||
| """ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // | ||
|
|
||
| // | ||
| // GENERATED FILE, DON'T EDIT | ||
| // Generated by coreclr InterpToNativeGenerator | ||
| // | ||
|
|
||
| #include <callhelpers.hpp> | ||
|
|
||
| // Arguments are passed on the stack with each argument aligned to INTERP_STACK_SLOT_SIZE. | ||
| #define ARG_ADDR(i) (pArgs + (i * INTERP_STACK_SLOT_SIZE)) | ||
| #define ARG_IND(i) ((int32_t)((int32_t*)ARG_ADDR(i))) | ||
radekdoulik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #define ARG_I32(i) (*(int32_t*)ARG_ADDR(i)) | ||
| #define ARG_I64(i) (*(int64_t*)ARG_ADDR(i)) | ||
| #define ARG_F32(i) (*(float*)ARG_ADDR(i)) | ||
| #define ARG_F64(i) (*(double*)ARG_ADDR(i)) | ||
|
|
||
| namespace | ||
| { | ||
| """); | ||
|
|
||
| foreach (var signature in signatures) | ||
| { | ||
| try | ||
| { | ||
| var result = Result(signature); | ||
| var args = Args(signature); | ||
| var portabilityAssert = signature[0] == 'n' ? "PORTABILITY_ASSERT(\"Indirect struct return is not yet implemented.\");\n " : ""; | ||
| w.Write( | ||
| $$""" | ||
|
|
||
| static void {{CallFuncName(args, SignatureMapper.CharToNameType(signature[0]))}}(PCODE pcode, int8_t* pArgs, int8_t* pRet) | ||
| { | ||
| {{result.nativeType}} (*fptr)({{args.Join(", ", (p, i) => SignatureMapper.CharToNativeType(p))}}) = ({{result.nativeType}} (*)({{args.Join(", ", (p, i) => SignatureMapper.CharToNativeType(p))}}))pcode; | ||
| {{portabilityAssert}}{{(result.isVoid ? "" : "*" + "((" + result.nativeType + "*)pRet) = ")}}(*fptr)({{args.Join(", ", (p, i) => $"{SignatureMapper.CharToArgType(p)}({i})")}}); | ||
| } | ||
|
|
||
| """); | ||
| } | ||
| catch (InvalidSignatureCharException e) | ||
| { | ||
| throw new LogAsErrorException($"Element '{e.Char}' of signature '{signature}' can't be handled by managed2native generator"); | ||
| } | ||
| } | ||
|
|
||
| w.Write( | ||
| $$""" | ||
| } | ||
|
|
||
| const StringToWasmSigThunk g_wasmThunks[] = { | ||
| {{signatures.Join($",{w.NewLine}", signature => | ||
| $" {{ \"{signature}\", (void*)&{CallFuncName(Args(signature), SignatureMapper.CharToNameType(signature[0]))} }}")}} | ||
| }; | ||
|
|
||
| const size_t g_wasmThunksCount = sizeof(g_wasmThunks) / sizeof(g_wasmThunks[0]); | ||
|
|
||
| """); | ||
|
|
||
| static IEnumerable<char> Args(string signature) | ||
| { | ||
| for (int i = 1; i < signature.Length; ++i) | ||
| yield return signature[i]; | ||
| } | ||
|
|
||
| static (bool isVoid, string nativeType) Result(string signature) | ||
| => new(SignatureMapper.IsVoidSignature(signature), SignatureMapper.CharToNativeType(signature[0])); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.