-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
270 lines (232 loc) · 8.65 KB
/
Copy pathProgram.cs
File metadata and controls
270 lines (232 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using Wacs.Core;
using Wacs.Core.Runtime;
using MirrorVM;
MirrorBuilder.CompileLogFile = File.Open("compile_log.txt", FileMode.Create);
string module_name = "./rust_bench/target/wasm32-unknown-unknown/release/rust_bench.wasm";
string[] benchmarks = ["hashes", "image", "json", "prospero_compile", "prospero_eval", "rand_sort", "rapier", "regex", "zip"];
//benchmarks = ["rapier"];
var module = new WasmModule(new MemoryStream(File.ReadAllBytes(module_name)), null);
var instance = new WasmInstance(module);
// MirrorVM benchmark
if (true)
{
List<string> result_table = [];
var frame = new MirrorVM.Frame(1);
foreach (var name in benchmarks)
{
string func_name = "bench_" + name;
Console.WriteLine("function " + func_name);
if (module.Exports.TryGetValue(func_name, out object item))
{
var func = item as WasmFunction;
if (func != null)
{
var callable = func.GetBody().Compile();
//long[] func_args = [100_000_000];
List<TimeSpan> times = [];
for (int i = 0; i < 10; i++)
{
var start = Stopwatch.StartNew();
callable.Call(frame, instance);
times.Add(start.Elapsed);
Console.WriteLine("> " + frame.GetReturnLong());
}
times.Sort();
Console.WriteLine("min = " + times[0]);
Console.WriteLine("max = " + times[times.Count - 1]);
result_table.Add(name + "," + times[0].TotalSeconds);
}
}
else
{
throw new Exception("failed to find function: " + func_name);
}
}
Console.WriteLine("count reg = " + IRBuilder.TOTAL_REG);
Console.WriteLine("count frame = " + IRBuilder.TOTAL_FRAME);
for (int i = 0; i < IRBuilder.FRAME_INDICES.Length; i++)
{
int n = IRBuilder.FRAME_INDICES[i];
if (n > 0)
{
Console.WriteLine("count frame[" + i + "] = " + n);
}
}
Console.WriteLine("==================");
foreach (var line in result_table)
{
Console.WriteLine(line);
}
Console.WriteLine("==================");
MirrorBuilder.DumpRecordedConstants();
}
// WACS benchmark
if (false)
{
List<string> result_table = [];
var runtime = new WasmRuntime();
runtime.TranspileModules = true;
var stream = new FileStream(module_name, FileMode.Open);
var wacs_module = BinaryModuleParser.ParseWasm(stream);
var modInst = runtime.InstantiateModule(wacs_module);
runtime.RegisterModule("bench", modInst);
foreach (var name in benchmarks)
{
if (runtime.TryGetExportedFunction(("bench", "bench_" + name), out var func_addr))
{
var func_invoker = runtime.CreateInvokerFunc<Value>(func_addr);
List<TimeSpan> times = [];
for (int i = 0; i < 3; i++)
{
var start = Stopwatch.StartNew();
int n = func_invoker();
times.Add(start.Elapsed);
Console.WriteLine("> " + n);
}
times.Sort();
Console.WriteLine("min = " + times[0]);
Console.WriteLine("max = " + times[times.Count - 1]);
result_table.Add(name + "," + times[0].TotalSeconds);
}
}
Console.WriteLine("==================");
foreach (var line in result_table)
{
Console.WriteLine(line);
}
Console.WriteLine("==================");
}
// prospero eval
if (false)
{
int SIZE = 256;
Bitmap image = new Bitmap(SIZE, SIZE);
//Graphics gfx = Graphics.FromImage(image);
{
if (module.Exports.TryGetValue("bench_rapier", out object item))
{
var func = item as WasmFunction;
if (func != null)
{
var callable = func.GetBody().Compile();
callable.Call([], instance);
}
}
}
{
if (module.Exports.TryGetValue("physics_test", out object item))
{
var func = item as WasmFunction;
if (func != null)
{
var callable = func.GetBody().Compile();
for (int y = 0; y < SIZE; y++)
{
for (int x = 0; x < SIZE; x++)
{
float scale = 20.0f;
float x_f = (float)x / SIZE * 2 - 1;
float y_f = -(float)y / SIZE * 2 + 1;
callable.Call([
BitConverter.SingleToInt32Bits(x_f * scale),
BitConverter.SingleToInt32Bits(y_f * scale),
], instance);
/*if (res > 0)
{
image.SetPixel(x, y, Color.Red);
}*/
//Console.WriteLine("-> " + x_f + " " + y_f + " " + res_f);
}
}
}
}
}
image.Save("physics.png", ImageFormat.Png);
}
//TestBarriers.Run("funky");
return;
// barrier tests
if (false)
{
TestBarriers.Run("local_set");
TestBarriers.Run("local_tee");
TestBarriers.Run("global");
TestBarriers.Run("memory");
}
TestCommands.RunFile("address");
TestCommands.RunFile("align");
//TestCommands.RunFile("binary"); no exec tests
//TestCommands.RunFile("binary-leb128"); no exec tests
TestCommands.RunFile("block");
TestCommands.RunFile("br");
TestCommands.RunFile("br_if");
TestCommands.RunFile("br_table", [1067, 1068, 1069, 1070, 1071, 1072, 1073, 1074]); // skip br_table with thousands of options
// todo bulk
TestCommands.RunFile("call");
TestCommands.RunFile("call_indirect");
//TestCommands.RunFile("comments"); no exec tests
TestCommands.RunFile("const");
TestCommands.RunFile("conversions");
//TestCommands.RunFile("custom"); no exec tests
//TestCommands.RunFile("data"); no exec tests
//TestCommands.RunFile("elem"); a bunch of junk I don't want to support, including multi-module tests
TestCommands.RunFile("endianness");
//TestCommands.RunFile("exports"); didn't bother
TestCommands.RunFile("f32");
TestCommands.RunFile("f32_bitwise");
TestCommands.RunFile("f32_cmp");
TestCommands.RunFile("f64");
TestCommands.RunFile("f64_bitwise");
TestCommands.RunFile("f64_cmp");
TestCommands.RunFile("fac");
TestCommands.RunFile("float_exprs",[2403,2405,2407,2409,2411,2413,2415]); // canonicalization
TestCommands.RunFile("float_literals");
TestCommands.RunFile("float_memory",[21,22,71,73,74]); // canonicalization crap, possibly worth looking into?
TestCommands.RunFile("float_misc");
TestCommands.RunFile("forward");
TestCommands.RunFile("func");
TestCommands.RunFile("func_ptrs");
TestCommands.RunFile("global");
TestCommands.RunFile("i32");
TestCommands.RunFile("i64");
TestCommands.RunFile("if");
//TestCommands.RunFile("imports"); didn't bother
//TestCommands.RunFile("inline-module"); no exec tests
TestCommands.RunFile("int_exprs");
TestCommands.RunFile("int_literals");
TestCommands.RunFile("labels");
TestCommands.RunFile("left-to-right");
//TestCommands.RunFile("linking"); didn't bother
TestCommands.RunFile("load");
TestCommands.RunFile("local_get");
TestCommands.RunFile("local_set");
TestCommands.RunFile("local_tee");
TestCommands.RunFile("loop");
TestCommands.RunFile("memory");
//TestCommands.RunFile("memory_copy"); todo bulk memory
//TestCommands.RunFile("memory_grow"); multi-module tests
//TestCommands.RunFile("memory_init"); fairly certain something is wrong with data section handling
TestCommands.RunFile("memory_redundancy");
TestCommands.RunFile("memory_size");
TestCommands.RunFile("memory_trap");
//TestCommands.RunFile("names"); mostly just breaks my debug output
TestCommands.RunFile("nop");
//TestCommands.RunFile("obsolete-keywords"); no exec tests
//TestCommands.RunFile("ref_func"); didn't bother
//TestCommands.RunFile("ref_is_null"); didn't bother
//TestCommands.RunFile("ref_null"); didn't bother
TestCommands.RunFile("return");
//TestCommands.RunFile("select"); don't want to deal with annotated select
// skip simd
//TestCommands.RunFile("skip-stack-guard-page"); no exec tests / there's no way this is relevant
TestCommands.RunFile("stack");
//TestCommands.RunFile("start"); don't bother
TestCommands.RunFile("store");
TestCommands.RunFile("switch");
// skip table
Console.WriteLine();
//var cmds = JsonSerializer.Deserialize<TestCommands>(File.ReadAllText("tests/int_exprs.json"));
//cmds.Run();