Skip to content

Commit a2068b9

Browse files
kgBrzVlad
andauthored
[wasm] Intrinsics for blazor (#102670)
Attempt to improve interp startup by adding intrinsics: * Add IsNullRef/NullRef intrinsics * Add Unsafe.Add intrinsic Co-authored-by: Vlad Brezae <[email protected]>
1 parent c6779ba commit a2068b9

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

src/mono/mono/mini/interp/transform.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,56 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas
22522252
#endif
22532253
} else if (!strcmp (tm, "InitBlockUnaligned") || !strcmp (tm, "InitBlock")) {
22542254
*op = MINT_INITBLK;
2255+
} else if (!strcmp (tm, "IsNullRef")) {
2256+
#if SIZEOF_VOID_P == 4
2257+
*op = MINT_CEQ0_I4;
2258+
#else
2259+
// FIXME: No CEQ0_I8
2260+
#endif
2261+
} else if (!strcmp (tm, "NullRef")) {
2262+
#if SIZEOF_VOID_P == 4
2263+
*op = MINT_LDC_I4_0;
2264+
#else
2265+
*op = MINT_LDC_I8_0;
2266+
#endif
2267+
} else if (!strcmp (tm, "Add")) {
2268+
MonoGenericContext *ctx = mono_method_get_context (target_method);
2269+
g_assert (ctx);
2270+
g_assert (ctx->method_inst);
2271+
g_assert (ctx->method_inst->type_argc == 1);
2272+
MonoType *t = ctx->method_inst->type_argv [0];
2273+
2274+
int base_var = td->sp [-2].var,
2275+
offset_var = td->sp [-1].var,
2276+
align, esize;
2277+
2278+
#if SIZEOF_VOID_P == 8
2279+
if (td->sp [-1].type == STACK_TYPE_I4) {
2280+
interp_add_ins (td, MINT_CONV_I8_I4);
2281+
interp_ins_set_sreg (td->last_ins, offset_var);
2282+
interp_ins_set_dreg (td->last_ins, offset_var);
2283+
}
2284+
#endif
2285+
2286+
td->sp -= 2;
2287+
2288+
esize = mono_type_size (t, &align);
2289+
if (esize != 1) {
2290+
g_assert (esize <= 32767);
2291+
interp_add_ins (td, MINT_MUL_P_IMM);
2292+
td->last_ins->data [0] = (gint16)esize;
2293+
interp_ins_set_sreg (td->last_ins, offset_var);
2294+
interp_ins_set_dreg (td->last_ins, offset_var);
2295+
}
2296+
2297+
interp_add_ins (td, MINT_ADD_P);
2298+
interp_ins_set_sregs2 (td->last_ins, base_var, offset_var);
2299+
push_simple_type (td, STACK_TYPE_MP);
2300+
interp_ins_set_dreg (td->last_ins, td->sp [-1].var);
2301+
2302+
td->ip += 5;
2303+
2304+
return TRUE;
22552305
}
22562306
} else if (in_corlib && !strcmp (klass_name_space, "System.Runtime.CompilerServices") && !strcmp (klass_name, "RuntimeHelpers")) {
22572307
if (!strcmp (tm, "get_OffsetToStringData")) {

src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/InterpPgoTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public InterpPgoTests(ITestOutputHelper output, SharedBuildPerTestClassFixture b
2828
[InlineData("Release")]
2929
public async Task FirstRunGeneratesTableAndSecondRunLoadsIt(string config)
3030
{
31-
// We need to invoke Greeting enough times to cause BCL code to tier so we can exercise interpreter PGO
31+
// We need to invoke Random enough times to cause BCL code to tier so we can exercise interpreter PGO
3232
// Invoking it too many times makes the test meaningfully slower.
33-
const int iterationCount = 70;
33+
const int iterationCount = 50;
3434

3535
_testOutput.WriteLine("/// Creating project");
3636
CopyTestAsset("WasmBasicTestApp", "InterpPgoTest", "App");
@@ -61,13 +61,13 @@ public async Task FirstRunGeneratesTableAndSecondRunLoadsIt(string config)
6161
lock (runner.OutputLines)
6262
output = string.Join(Environment.NewLine, runner.OutputLines);
6363

64-
Assert.Contains("Hello, World!", output);
64+
Assert.Contains("I filled a buffer with random items", output);
6565
// Verify that no PGO table was located in cache
6666
Assert.Contains("Failed to load interp_pgo table", output);
6767
// Verify that the table was saved after the app ran
6868
Assert.Contains("Saved interp_pgo table", output);
6969
// Verify that a specific method was tiered by the Greeting calls and recorded by PGO
70-
Assert.Contains("added System.Runtime.CompilerServices.Unsafe:Add<byte> (byte&,int) to table", output);
70+
Assert.Contains(" System.Random:Next () to table", output);
7171
}
7272

7373
{
@@ -81,7 +81,7 @@ public async Task FirstRunGeneratesTableAndSecondRunLoadsIt(string config)
8181
lock (runner.OutputLines)
8282
output = string.Join(Environment.NewLine, runner.OutputLines);
8383

84-
Assert.Contains("Hello, World!", output);
84+
Assert.Contains("I filled a buffer with random items", output);
8585
// Verify that table data was loaded from cache
8686
// if this breaks, it could be caused by change in config which affects the config hash and the cache storage hash key
8787
Assert.Contains(" bytes of interp_pgo data (table size == ", output);
@@ -90,7 +90,7 @@ public async Task FirstRunGeneratesTableAndSecondRunLoadsIt(string config)
9090
// Verify that method(s) were found in the table and eagerly tiered
9191
Assert.Contains("because it was in the interp_pgo table", output);
9292
// Verify that a specific method was tiered by the Greeting calls and recorded by PGO
93-
Assert.Contains("added System.Runtime.CompilerServices.Unsafe:Add<byte> (byte&,int) to table", output);
93+
Assert.Contains(" System.Random:Next () to table", output);
9494
}
9595

9696
_testOutput.WriteLine("/// Done");

src/mono/wasm/testassets/WasmBasicTestApp/App/InterpPgoTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ public partial class InterpPgoTest
1212
internal static partial string GetHRef();
1313

1414
[JSExport]
15-
internal static string Greeting()
15+
internal static void TryToTier(int iterationCount)
1616
{
17-
var text = $"Hello, World! Greetings from {GetHRef()}";
17+
var buffer = new int[4096];
18+
var random = new Random();
19+
for (int i = 0; i < iterationCount; i++) {
20+
for (int j = 0; j < buffer.Length; j++)
21+
buffer[j] = random.Next();
22+
}
23+
var text = $"Greetings from {GetHRef()}. I filled a buffer with random items {iterationCount} times.";
1824
Console.WriteLine(text);
19-
return text;
2025
}
2126
}

src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ try {
139139
}
140140
}
141141
});
142-
const iterationCount = params.get("iterationCount") ?? 70;
143-
for (let i = 0; i < iterationCount; i++) {
144-
exports.InterpPgoTest.Greeting();
145-
};
142+
const iterationCount = params.get("iterationCount") ?? "70";
143+
exports.InterpPgoTest.TryToTier(parseInt(iterationCount));
146144
await INTERNAL.interp_pgo_save_data();
147145
exit(0);
148146
break;

0 commit comments

Comments
 (0)