Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private async Task EnsureWasmAbiRulesAreFollowed(Configuration config, bool aot)
// FIXME: Not possible in in-process mode for some reason, even with verbosity at "diagnostic"
// Assert.Contains("Adding pinvoke signature FD for method 'Test.", output);

string pinvokeTableFileName = IsCoreClrRuntime ? "pinvoke-table.cpp" : "pinvoke-table.h";
string pinvokeTableFileName = IsCoreClrRuntime ? "callhelpers-pinvoke.cpp" : "pinvoke-table.h";
string pinvokeTable = File.ReadAllText(Path.Combine(objDir, pinvokeTableFileName));
// Verify that the invoke is in the pinvoke table. Under various circumstances we will silently skip it,
// for example if the module isn't found
Expand Down Expand Up @@ -367,7 +367,7 @@ public async Task EnsureWasmAbiRulesAreFollowedInAOT(Configuration config, bool

[Theory]
[BuildAndRun(aot: false)]
[TestCategory("native-mono")] // coreclr ActiveIssue: https://github.com/dotnet/runtime/issues/120897
[TestCategory("native-mono")]

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.

The native-mono runs only on mono. The native runs on both.

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.

@copilot create new PR and make this change

public async Task EnsureWasmAbiRulesAreFollowedInInterpreter(Configuration config, bool aot) =>
Comment thread
lewing marked this conversation as resolved.
await EnsureWasmAbiRulesAreFollowed(config, aot);

Expand Down
30 changes: 27 additions & 3 deletions src/tasks/WasmAppBuilder/coreclr/InterpToNativeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ private static void Emit(StreamWriter w, IEnumerable<string> cookies)
var signatures = cookies.OrderBy(c => c).Distinct().ToArray();
Array.Sort(signatures, StringComparer.Ordinal);

// Collect unique struct return sizes so we can emit typedefs
var structReturnSizes = new SortedSet<int>();
foreach (var sig in signatures)
{
var toks = SignatureMapper.ParseSignatureTokens(sig);
if (toks[0][0] == 'S' && toks[0].Length > 1)
structReturnSizes.Add(int.Parse(toks[0].Substring(1)));
}

w.Write(
"""
// Licensed to the .NET Foundation under one or more agreements.
Expand All @@ -85,6 +94,17 @@ private static void Emit(StreamWriter w, IEnumerable<string> cookies)
#define ARG_F32(i) (*(float*)ARG_ADDR(i))
#define ARG_F64(i) (*(double*)ARG_ADDR(i))

""");

// Emit typedefs for struct return types so emcc generates the correct sret ABI
foreach (var size in structReturnSizes)
{
w.WriteLine($"typedef struct {{ char d[{size}]; }} wasm_ret_S{size};");
}

w.Write(
"""

namespace
{
""");
Expand All @@ -104,7 +124,6 @@ private static void Emit(StreamWriter w, IEnumerable<string> cookies)
tokens.RemoveAt(tokens.Count - 1);
}
var args = Args(tokens);
var portabilityAssert = returnToken[0] == 'S' ? "PORTABILITY_ASSERT(\"Indirect struct return is not yet implemented.\");\n " : "";

var portableEntryPointComma = args.Count > 0 ? ", " : "";
var portableEntrypointDeclaration = isPortableEntryPointCall ? portableEntryPointComma + "PCODE" : "";
Expand All @@ -118,7 +137,7 @@ private static void Emit(StreamWriter w, IEnumerable<string> cookies)
{{(isPortableEntryPointCall ? "NOINLINE " : "")}}static void {{CallFuncName(args, SignatureMapper.TokenToNameType(returnToken), isPortableEntryPointCall)}}(PCODE {{(isPortableEntryPointCall ? "pPortableEntryPoint" : "pcode")}}, int8_t* pArgs, int8_t* pRet)
{{{(isPortableEntryPointCall ? "\n alignas(16) int framePointer = TERMINATE_R2R_STACK_WALK;" : "")}}
{{result.nativeType}} (*fptr)({{portableEntrypointStackDeclaration}}{{string.Join(", ", args.Select(static t => SignatureMapper.TokenToNativeType(t)))}}{{portableEntrypointDeclaration}}) = {{portableEntrypointPointerRD}}({{result.nativeType}} ({{portableEntrypointPointerRD}}*)({{portableEntrypointStackDeclaration}}{{string.Join(", ", args.Select(static t => SignatureMapper.TokenToNativeType(t)))}}{{portableEntrypointDeclaration}})){{(isPortableEntryPointCall ? "(pPortableEntryPoint)" : "pcode")}};
{{portabilityAssert}}{{(result.isVoid ? "" : "*" + "((" + result.nativeType + "*)pRet) = ")}}(*fptr)({{portableEntrypointStackParam}}{{string.Join(", ", ArgsWithSlotOffsets(args))}}{{portableEntrypointParam}});
{{(result.isVoid ? "" : "*" + "((" + result.nativeType + "*)pRet) = ")}}(*fptr)({{portableEntrypointStackParam}}{{string.Join(", ", ArgsWithSlotOffsets(args))}}{{portableEntrypointParam}});
}

""");
Expand Down Expand Up @@ -169,7 +188,12 @@ static List<string> ArgsWithSlotOffsets(List<string> args)
}

static (bool isVoid, string nativeType) Result(string returnToken)
=> new(returnToken == "v", SignatureMapper.TokenToNativeType(returnToken));
{
// For struct returns, use the typedef so emcc generates the correct sret ABI
if (returnToken[0] == 'S' && returnToken.Length > 1)
return (false, $"wasm_ret_{returnToken}");
return new(returnToken == "v", SignatureMapper.TokenToNativeType(returnToken));
}
Comment thread
lewing marked this conversation as resolved.

static bool IsPortableEntryPointCall(List<string> tokens)
{
Expand Down
Loading