Skip to content

Commit

Permalink
[wasm] reduce MethodImplOptions.NoInlining (#79457)
Browse files Browse the repository at this point in the history
* reduce MethodImplOptions.NoInlining
  • Loading branch information
pavelsavara authored Dec 12, 2022
1 parent d8a4271 commit fda0b35
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 65 deletions.
3 changes: 3 additions & 0 deletions src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

internal static partial class Interop
{
// WARNING: until https://github.com/dotnet/runtime/issues/37955 is fixed
// make sure that the native side always sets the out parameters
// otherwise out parameters could stay un-initialized, when the method is used in inlined context
internal static unsafe partial class Runtime
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace System.Runtime.InteropServices.JavaScript
// the public methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction
internal static unsafe partial class JavaScriptExports
{
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// Task<int>? CallEntrypoint(MonoMethod* entrypointPtr, string[] args)
public static void CallEntrypoint(JSMarshalerArgument* arguments_buffer)
Expand Down Expand Up @@ -95,7 +94,6 @@ public static void CallEntrypoint(JSMarshalerArgument* arguments_buffer)

// The JS layer invokes this method when the JS wrapper for a JS owned object
// has been collected by the JS garbage collector
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// void ReleaseJSOwnedObjectByGCHandle(GCHandle gcHandle)
public static void ReleaseJSOwnedObjectByGCHandle(JSMarshalerArgument* arguments_buffer)
Expand All @@ -118,7 +116,6 @@ public static void ReleaseJSOwnedObjectByGCHandle(JSMarshalerArgument* arguments
}
}

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// GCHandle CreateTaskCallback()
public static void CreateTaskCallback(JSMarshalerArgument* arguments_buffer)
Expand All @@ -137,7 +134,6 @@ public static void CreateTaskCallback(JSMarshalerArgument* arguments_buffer)
}
}

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// TRes? CallDelegate<T1,T2,T3TRes>(GCHandle callback, T1? arg1, T2? arg2, T3? arg3)
public static void CallDelegate(JSMarshalerArgument* arguments_buffer)
Expand Down Expand Up @@ -167,7 +163,6 @@ public static void CallDelegate(JSMarshalerArgument* arguments_buffer)
}
}

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// void CompleteTask<T>(GCHandle holder, Exception? exceptionResult, T? result)
public static void CompleteTask(JSMarshalerArgument* arguments_buffer)
Expand Down Expand Up @@ -195,7 +190,6 @@ public static void CompleteTask(JSMarshalerArgument* arguments_buffer)
}
}

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// string GetManagedStackTrace(GCHandle exception)
public static void GetManagedStackTrace(JSMarshalerArgument* arguments_buffer)
Expand Down Expand Up @@ -223,7 +217,6 @@ public static void GetManagedStackTrace(JSMarshalerArgument* arguments_buffer)

#if FEATURE_WASM_THREADS

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// void InstallSynchronizationContext()
public static void InstallSynchronizationContext (JSMarshalerArgument* arguments_buffer) {
Expand All @@ -240,13 +233,13 @@ public static void InstallSynchronizationContext (JSMarshalerArgument* arguments

#endif

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
[MethodImpl(MethodImplOptions.NoInlining)] // profiler needs to find it executed under this name
public static void StopProfile()
{
}

// Called by the AOT profiler to save profile data into INTERNAL.aotProfileData
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
[MethodImpl(MethodImplOptions.NoInlining)] // profiler needs to find it executed under this name
public static unsafe void DumpAotProfileData(ref byte buf, int len, string extraArg)
{
if (len == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace System.Runtime.InteropServices.JavaScript
{
internal static unsafe partial class JavaScriptImports
{
[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void MarshalPromise(Span<JSMarshalerArgument> arguments)
{
fixed (JSMarshalerArgument* ptr = arguments)
Expand All @@ -23,7 +22,6 @@ public static void MarshalPromise(Span<JSMarshalerArgument> arguments)

#region legacy

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static object GetGlobalObject(string? str = null)
{
int exception;
Expand All @@ -36,7 +34,6 @@ public static object GetGlobalObject(string? str = null)
return jsObj;
}

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static IntPtr CreateCSOwnedObject(string typeName, object[] parms)
{
Interop.Runtime.CreateCSOwnedObjectRef(typeName, parms, out int exception, out object res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace System.Runtime.InteropServices.JavaScript
// the public methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction
internal static unsafe partial class LegacyExports
{
[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void GetCSOwnedObjectByJSHandleRef(nint jsHandle, int shouldAddInflight, out JSObject? result)
{
lock (JSHostImplementation.s_csOwnedObjects)
Expand All @@ -32,7 +31,6 @@ public static void GetCSOwnedObjectByJSHandleRef(nint jsHandle, int shouldAddInf
result = null;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static IntPtr GetCSOwnedObjectJSHandleRef(in JSObject jsObject, int shouldAddInflight)
{
jsObject.AssertNotDisposed();
Expand All @@ -44,7 +42,6 @@ public static IntPtr GetCSOwnedObjectJSHandleRef(in JSObject jsObject, int shoul
return jsObject.JSHandle;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static IntPtr TryGetCSOwnedObjectJSHandleRef(in object rawObj, int shouldAddInflight)
{
JSObject? jsObject = rawObj as JSObject;
Expand All @@ -55,7 +52,6 @@ public static IntPtr TryGetCSOwnedObjectJSHandleRef(in object rawObj, int should
return jsObject?.JSHandle ?? IntPtr.Zero;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void CreateCSOwnedProxyRef(nint jsHandle, LegacyHostImplementation.MappedType mappedType, int shouldAddInflight, out JSObject jsObject)
{
JSObject? res = null;
Expand Down Expand Up @@ -88,27 +84,23 @@ public static void CreateCSOwnedProxyRef(nint jsHandle, LegacyHostImplementation
jsObject = res;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void GetJSOwnedObjectByGCHandleRef(int gcHandle, out object result)
{
GCHandle h = (GCHandle)(IntPtr)gcHandle;
result = h.Target!;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static IntPtr GetJSOwnedObjectGCHandleRef(in object obj)
{
return JSHostImplementation.GetJSOwnedObjectGCHandle(obj, GCHandleType.Normal);
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static IntPtr CreateTaskSource()
{
var tcs = new TaskCompletionSource<object>();
return GetJSOwnedObjectGCHandleRef(tcs);
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void SetTaskSourceResultRef(int tcsGCHandle, in object result)
{
GCHandle handle = (GCHandle)(IntPtr)tcsGCHandle;
Expand All @@ -117,7 +109,6 @@ public static void SetTaskSourceResultRef(int tcsGCHandle, in object result)
tcs.SetResult(result);
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void SetTaskSourceFailure(int tcsGCHandle, string reason)
{
GCHandle handle = (GCHandle)(IntPtr)tcsGCHandle;
Expand All @@ -126,7 +117,6 @@ public static void SetTaskSourceFailure(int tcsGCHandle, string reason)
tcs.SetException(new JSException(reason));
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void GetTaskSourceTaskRef(int tcsGCHandle, out object result)
{
GCHandle handle = (GCHandle)(IntPtr)tcsGCHandle;
Expand All @@ -135,7 +125,6 @@ public static void GetTaskSourceTaskRef(int tcsGCHandle, out object result)
result = tcs.Task;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void SetupJSContinuationRef(in Task _task, JSObject continuationObj)
{
// HACK: Attempting to use the in-param will produce CS1628, so we make a temporary copy
Expand Down Expand Up @@ -182,13 +171,11 @@ void Complete()
}
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static string ObjectToStringRef(ref object o)
{
return o.ToString() ?? string.Empty;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static double GetDateValueRef(ref object dtv)
{
ArgumentNullException.ThrowIfNull(dtv);
Expand All @@ -204,7 +191,6 @@ public static double GetDateValueRef(ref object dtv)

// HACK: We need to implicitly box by using an 'object' out-param.
// Note that the return value would have been boxed on the C#->JS transition anyway.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void CreateDateTimeRef(double ticks, out object result)
{
DateTimeOffset unixTime = DateTimeOffset.FromUnixTimeMilliseconds((long)ticks);
Expand All @@ -218,7 +204,6 @@ public static void CreateDateTimeRef(double ticks, out object result)
// System.Private.Uri is ~80KB large assembly so it's worth trimming
private static Type? uriType;

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
[Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2077", Justification = "Done on purpose, see comment above.")]
[Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2057", Justification = "Done on purpose, see comment above.")]
public static void CreateUriRef(string uri, out object? result)
Expand All @@ -241,13 +226,11 @@ public static void CreateUriRef(string uri, out object? result)
}
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static bool IsSimpleArrayRef(ref object a)
{
return a is System.Array arr && arr.Rank == 1 && arr.GetLowerBound(0) == 0;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static string GetCallSignatureRef(IntPtr _methodHandle, in object objForRuntimeType)
{
var methodHandle = JSHostImplementation.GetMethodHandleFromIntPtr(_methodHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ internal static unsafe void InvokeImportImpl(IntPtr fnHandle, Span<JSMarshalerAr
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
internal static unsafe JSFunctionBinding BindJSFunctionImpl(string functionName, string moduleName, ReadOnlySpan<JSMarshalerType> signatures)
{
var signature = JSHostImplementation.GetMethodSignature(signatures);
Expand All @@ -212,7 +211,6 @@ internal static unsafe JSFunctionBinding BindJSFunctionImpl(string functionName,
return signature;
}

[MethodImpl(MethodImplOptions.NoInlining)]
internal static unsafe JSFunctionBinding BindManagedFunctionImpl(string fullyQualifiedName, int signatureHash, ReadOnlySpan<JSMarshalerType> signatures)
{
var signature = JSHostImplementation.GetMethodSignature(signatures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ internal Array(IntPtr jsHandle) : base(jsHandle)
/// <param name="i">The index.</param>
public object this[int i]
{
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
get
{
this.AssertNotDisposed();
Expand All @@ -95,7 +94,6 @@ public object this[int i]
JSHostImplementation.ReleaseInFlight(indexValue);
return indexValue;
}
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
set
{
this.AssertNotDisposed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static object GetGlobalObject(string str)
/// valuews.
/// </para>
/// </returns>
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static object Invoke(this JSObject self, string method, params object?[] args)
{
ArgumentNullException.ThrowIfNull(self);
Expand Down Expand Up @@ -67,7 +66,6 @@ public static object Invoke(this JSObject self, string method, params object?[]
/// valuews.
/// </para>
/// </returns>
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static object GetObjectProperty(this JSObject self, string name)
{
ArgumentNullException.ThrowIfNull(self);
Expand All @@ -92,7 +90,6 @@ public static object GetObjectProperty(this JSObject self, string name)
/// float[], double[]) </param>
/// <param name="createIfNotExists">Defaults to <see langword="true"/> and creates the property on the javascript object if not found, if set to <see langword="false"/> it will not create the property if it does not exist. If the property exists, the value is updated with the provided value.</param>
/// <param name="hasOwnProperty"></param>
[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void SetObjectProperty(this JSObject self, string name, object? value, bool createIfNotExists = true, bool hasOwnProperty = false)
{
ArgumentNullException.ThrowIfNull(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public int Length

public static implicit operator Uint8Array(Span<byte> span) => From(span);

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public byte[] ToArray()
{
this.AssertNotDisposed();
Expand All @@ -48,7 +47,6 @@ public byte[] ToArray()
return (byte[])res;
}

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static unsafe Uint8Array From(ReadOnlySpan<byte> span)
{
// source has to be instantiated.
Expand Down
21 changes: 11 additions & 10 deletions src/mono/wasm/runtime/corebindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,25 @@ extern void mono_wasm_marshal_promise(void *data);

void core_initialize_internals (void)
{
mono_add_internal_call ("Interop/Runtime::InvokeJSWithArgsRef", mono_wasm_invoke_js_with_args_ref);
mono_add_internal_call ("Interop/Runtime::GetObjectPropertyRef", mono_wasm_get_object_property_ref);
mono_add_internal_call ("Interop/Runtime::GetByIndexRef", mono_wasm_get_by_index_ref);
mono_add_internal_call ("Interop/Runtime::SetObjectPropertyRef", mono_wasm_set_object_property_ref);
mono_add_internal_call ("Interop/Runtime::SetByIndexRef", mono_wasm_set_by_index_ref);
mono_add_internal_call ("Interop/Runtime::GetGlobalObjectRef", mono_wasm_get_global_object_ref);
mono_add_internal_call ("Interop/Runtime::CreateCSOwnedObjectRef", mono_wasm_create_cs_owned_object_ref);
mono_add_internal_call ("Interop/Runtime::ReleaseCSOwnedObject", mono_wasm_release_cs_owned_object);
mono_add_internal_call ("Interop/Runtime::TypedArrayToArrayRef", mono_wasm_typed_array_to_array_ref);
mono_add_internal_call ("Interop/Runtime::TypedArrayFromRef", mono_wasm_typed_array_from_ref);

mono_add_internal_call ("Interop/Runtime::BindJSFunction", mono_wasm_bind_js_function);
mono_add_internal_call ("Interop/Runtime::InvokeJSFunction", mono_wasm_invoke_bound_function);
mono_add_internal_call ("Interop/Runtime::InvokeImport", mono_wasm_invoke_import);
mono_add_internal_call ("Interop/Runtime::BindCSFunction", mono_wasm_bind_cs_function);
mono_add_internal_call ("Interop/Runtime::MarshalPromise", mono_wasm_marshal_promise);
mono_add_internal_call ("Interop/Runtime::RegisterGCRoot", mono_wasm_register_root);
mono_add_internal_call ("Interop/Runtime::DeregisterGCRoot", mono_wasm_deregister_root);

// legacy
mono_add_internal_call ("Interop/Runtime::InvokeJSWithArgsRef", mono_wasm_invoke_js_with_args_ref);
mono_add_internal_call ("Interop/Runtime::GetObjectPropertyRef", mono_wasm_get_object_property_ref);
mono_add_internal_call ("Interop/Runtime::SetObjectPropertyRef", mono_wasm_set_object_property_ref);
mono_add_internal_call ("Interop/Runtime::GetByIndexRef", mono_wasm_get_by_index_ref);
mono_add_internal_call ("Interop/Runtime::SetByIndexRef", mono_wasm_set_by_index_ref);
mono_add_internal_call ("Interop/Runtime::GetGlobalObjectRef", mono_wasm_get_global_object_ref);
mono_add_internal_call ("Interop/Runtime::TypedArrayToArrayRef", mono_wasm_typed_array_to_array_ref);
mono_add_internal_call ("Interop/Runtime::CreateCSOwnedObjectRef", mono_wasm_create_cs_owned_object_ref);
mono_add_internal_call ("Interop/Runtime::TypedArrayFromRef", mono_wasm_typed_array_from_ref);
}

// Int8Array | int8_t | byte or SByte (signed byte)
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/runtime/invoke-cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { mono_assert, MonoObjectRef, MonoStringRef, MonoString, MonoObject, Mono
import { Int32Ptr } from "./types/emscripten";
import cwraps from "./cwraps";
import { assembly_load } from "./class-loader";
import { wrap_error_root } from "./invoke-js";
import { wrap_error_root, wrap_no_error_root } from "./invoke-js";
import { startMeasure, MeasuredBlock, endMeasure } from "./profiler";

export function mono_wasm_bind_cs_function(fully_qualified_name: MonoStringRef, signature_hash: number, signature: JSFunctionSignature, is_exception: Int32Ptr, result_address: MonoObjectRef): void {
Expand Down Expand Up @@ -88,6 +88,7 @@ export function mono_wasm_bind_cs_function(fully_qualified_name: MonoStringRef,

_walk_exports_to_set_function(assembly, namespace, classname, methodname, signature_hash, bound_fn);
endMeasure(mark, MeasuredBlock.bindCsFunction, js_fqn);
wrap_no_error_root(is_exception, resultRoot);
}
catch (ex: any) {
Module.printErr(ex.toString());
Expand Down
Loading

0 comments on commit fda0b35

Please sign in to comment.