weval 0.4.1

The WebAssembly partial evaluator
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#pragma once

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/* ------------------------------------------------------------------------- */
/* partial-evaluation async requests and queues                              */
/* ------------------------------------------------------------------------- */

typedef void (*weval_func_t)();

typedef struct weval_req_t weval_req_t;
typedef struct weval_req_arg_t weval_req_arg_t;

/*
 * A weval "request": a record of a generic function and arguments,
 * and a place to put a function pointer containing a version of that
 * generic function specialized to those arguments.
 *
 * Note: the structure layout here is also hardcoded as a set of
 * offsets in `src/directive.rs`, where we read instances of this
 * struct from a snapshotted Wasm heap. Please keep both in sync!
 */
struct weval_req_t {
  weval_req_t* next;
  weval_req_t* prev;
  /* A user-provided ID of the weval'd function, for stability of
   * collected request bodies across relinkings: */
  uint32_t user_id;
  uint32_t
      num_globals; /* how many globals to specialize (prepended to arg list)? */
  weval_func_t func;
  uint8_t* argbuf;
  uint32_t arglen;
  weval_func_t* specialized;
};

typedef enum {
  weval_req_arg_i32 = 0,
  weval_req_arg_i64 = 1,
  weval_req_arg_f32 = 2,
  weval_req_arg_f64 = 3,
  weval_req_arg_buffer = 4,
  weval_req_arg_none = 255,
} weval_req_arg_type;

struct weval_req_arg_t {
  uint32_t specialize; /* is this argument specialized? */
  uint32_t
      ty; /* type of specialization value (`weval_req_arg_type` enum value). */
  /* The value to specialize on: */
  union {
    uint64_t raw;
    uint32_t i32;
    uint64_t i64;
    float f32;
    double f64;
    struct {
      /* A pointer to arbitrary memory with constant contents of the
       * given length; data follows. */
      uint32_t len;
      /* Size of buffer in data stream; next arg follows inline data. */
      uint32_t padded_len;
    } buffer;
  } u;
};

extern weval_req_t* weval_req_pending_head;
extern bool weval_is_wevaled;

#define WEVAL_DEFINE_GLOBALS()                                          \
  weval_req_t* weval_req_pending_head;                                  \
  __attribute__((export_name("weval.pending.head"))) weval_req_t**      \
  __weval_pending_head() {                                              \
    return &weval_req_pending_head;                                     \
  }                                                                     \
                                                                        \
  bool weval_is_wevaled;                                                \
  __attribute__((export_name("weval.is.wevaled"))) bool*                \
  __weval_is_wevaled() {                                                \
    return &weval_is_wevaled;                                           \
  }

#define WEVAL_DEFINE_TARGET(index, func)             \
  __attribute__((export_name("weval.func." #index))) \
      weval_func_t __weval_func_##index() {          \
    return (weval_func_t) & (func);                  \
  }

static inline void weval_request(weval_req_t* req) {
  if (weval_is_wevaled) {
      /* nothing! */
  } else {
    req->next = weval_req_pending_head;
    req->prev = NULL;
    if (weval_req_pending_head) {
      weval_req_pending_head->prev = req;
    }
    weval_req_pending_head = req;
  }
}

static inline void weval_free(weval_req_t* req) {
  if (req->prev) {
    req->prev->next = req->next;
  } else if (weval_req_pending_head == req) {
    weval_req_pending_head = req->next;
  }
  if (req->next) {
    req->next->prev = req->prev;
  }
  if (req->argbuf) {
    free(req->argbuf);
  }
  free(req);
}

/* ------------------------------------------------------------------------- */
/* intrinsics                                                                */
/* ------------------------------------------------------------------------- */

#ifdef __cplusplus
extern "C" {
#endif

#define WEVAL_WASM_IMPORT(name) \
  __attribute__((__import_module__("weval"), __import_name__(name)))

/* Core intrinsics for interpreter loops: contexts, registers, value
 * specialization */
    
void weval_push_context(uint32_t pc) WEVAL_WASM_IMPORT("push.context");
void weval_pop_context() WEVAL_WASM_IMPORT("pop.context");
void weval_update_context(uint32_t pc) WEVAL_WASM_IMPORT("update.context");
uint64_t weval_read_reg(uint64_t idx) WEVAL_WASM_IMPORT("read.reg");
void weval_write_reg(uint64_t idx, uint64_t value)
    WEVAL_WASM_IMPORT("write.reg");
uint32_t weval_specialize_value(uint32_t value, uint32_t lo, uint32_t hi)
    WEVAL_WASM_IMPORT("specialize.value");
uint64_t weval_read_specialization_global(uint32_t index)
    WEVAL_WASM_IMPORT("read.specialization.global");

/* Operand-stack virtualization */

/*
 * The stack is tracked abstractly as part of block specialization
 * context, and has entries of the form:
 *
 * /// Some value pushed onto stack. Not actually stored until
 * /// state is synced.
 * struct StackEntry {
 *     /// The address at which the value is stored.
 *     stackptr: Value,
 *     /// The value to store (and return from pops/stack-reads).
 *     value: Value,
 * }
 */

/* Push a value on the abstract stack; does not yet actually store
 * memory until sync'd. */
void weval_push_stack(uint64_t* ptr, uint64_t value)
    WEVAL_WASM_IMPORT("push.stack");
/* Synchronize all stack entries to the actual stack. */
void weval_sync_stack() WEVAL_WASM_IMPORT("sync.stack");
/* Read an entry from the virtual stack if available (index 0 is
 * just-pushed, 1 is one push before that, etc.) Loads from the
 * pointer if that index is not available. */
uint64_t weval_read_stack(uint64_t* ptr, uint32_t index)
    WEVAL_WASM_IMPORT("read.stack");
/* Write an entry at an existing stack index */
void weval_write_stack(uint64_t* ptr, uint32_t index, uint64_t value)
    WEVAL_WASM_IMPORT("write.stack");
/* Pops an entry from the stack, canceling its store if any (the
 * effect never occurs). */
uint64_t weval_pop_stack(uint64_t* ptr) WEVAL_WASM_IMPORT("pop.stack");

/* Locals virtualization; locals are also flushed when the stack is
 * flushed */

uint64_t weval_read_local(const uint64_t* ptr, uint32_t index)
    WEVAL_WASM_IMPORT("read.local");
void weval_write_local(uint64_t* ptr, uint32_t index, uint64_t value)
    WEVAL_WASM_IMPORT("write.local");

/* Debugging and stats intrinsics */
    
void weval_trace_line(uint32_t line_number) WEVAL_WASM_IMPORT("trace.line");
void weval_abort_specialization(uint32_t line_number, uint32_t fatal)
    WEVAL_WASM_IMPORT("abort.specialization");
void weval_assert_const32(uint32_t value, uint32_t line_no)
    WEVAL_WASM_IMPORT("assert.const32");
void weval_print(const char* message, uint32_t line, uint32_t val)
    WEVAL_WASM_IMPORT("print");
void weval_context_bucket(uint32_t bucket) WEVAL_WASM_IMPORT("context.bucket");

#undef WEVAL_WASM_IMPORT

#ifdef __cplusplus
}  // extern "C"
#endif

#ifdef __cplusplus
namespace weval {
static inline void push_context(uint32_t pc) { weval_push_context(pc); }
static inline void pop_context() { weval_pop_context(); }
static inline void update_context(uint32_t pc) { weval_update_context(pc); }
}  // namespace weval
#endif  // __cplusplus

/* ------------------------------------------------------------------------- */
/* C++ type-safe wrapper for partial evaluation of functions                 */
/* ------------------------------------------------------------------------- */

#ifdef __cplusplus
namespace weval {

struct ArgWriter {
  static const size_t MAX = 1024 * 1024;

  uint8_t* buffer;
  size_t len;
  size_t cap;

  ArgWriter() : buffer(nullptr), len(0), cap(0) {}

  uint8_t* alloc(size_t bytes) {
    if (bytes + len > MAX) {
      return nullptr;
    }
    if (bytes + len > cap) {
      size_t desired_cap = (cap == 0) ? 1024 : cap;
      while (desired_cap < (len + bytes)) {
        desired_cap *= 2;
      }
      buffer = reinterpret_cast<uint8_t*>(realloc(buffer, desired_cap));
      if (!buffer) {
        return nullptr;
      }
      cap = desired_cap;
    }
    uint8_t* ret = buffer + len;
    len += bytes;
    return ret;
  }

  template <typename T>
  bool write(T t) {
    uint8_t* mem = alloc(sizeof(T));
    if (!mem) {
      return false;
    }
    memcpy(mem, reinterpret_cast<uint8_t*>(&t), sizeof(T));
    return true;
  }

  uint8_t* take() {
    uint8_t* ret = buffer;
    buffer = nullptr;
    len = 0;
    cap = 0;
    return ret;
  }
};

template <typename T>
struct ArgSpec {};

template <typename T>
struct RuntimeArg : ArgSpec<T> {};

template <typename T>
RuntimeArg<T> Runtime() {
  return RuntimeArg<T>{};
}

template <typename T>
struct Specialize : ArgSpec<T> {
  T value;
  explicit Specialize(T value_) : value(value_) {}
};

template <typename T>
struct SpecializeMemory : ArgSpec<T> {
  T ptr;
  uint32_t len;
  SpecializeMemory(T ptr_, uint32_t len_) : ptr(ptr_), len(len_) {}
  SpecializeMemory(const SpecializeMemory& other) = default;
};

namespace impl {
template <typename Ret, typename... Args>
using FuncPtr = Ret (*)(Args...);

template <typename T>
struct StoreArg;

template <>
struct StoreArg<uint32_t> {
  bool operator()(ArgWriter& args, uint32_t value) {
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_i32;
    arg.u.raw = 0;
    arg.u.i32 = value;
    return args.write(arg);
  }
};
template <>
struct StoreArg<bool> {
  bool operator()(ArgWriter& args, bool value) {
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_i32;
    arg.u.raw = 0;
    arg.u.i32 = value ? 1 : 0;
    return args.write(arg);
  }
};
template <>
struct StoreArg<uint64_t> {
  bool operator()(ArgWriter& args, uint64_t value) {
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_i64;
    arg.u.raw = 0;
    arg.u.i64 = value;
    return args.write(arg);
  }
};
template <>
struct StoreArg<float> {
  bool operator()(ArgWriter& args, float value) {
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_f32;
    arg.u.raw = 0;
    arg.u.f32 = value;
    return args.write(arg);
  }
};
template <>
struct StoreArg<double> {
  bool operator()(ArgWriter& args, double value) {
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_f64;
    arg.u.raw = 0;
    arg.u.f64 = value;
    return args.write(arg);
  }
};
template <typename T>
struct StoreArg<T*> {
  bool operator()(ArgWriter& args, T* value) {
    static_assert(sizeof(T*) == 4, "Only 32-bit Wasm supported");
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_i32;
    arg.u.raw = 0;
    arg.u.i32 = reinterpret_cast<uint32_t>(value);
    return args.write(arg);
  }
};
template <typename T>
struct StoreArg<T&> {
  bool operator()(ArgWriter& args, T& value) {
    return StoreArg<T*>(args, &value);
  }
};
template <typename T>
struct StoreArg<const T*> {
  bool operator()(ArgWriter& args, const T* value) {
    static_assert(sizeof(const T*) == 4, "Only 32-bit Wasm supported");
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_i32;
    arg.u.raw = 0;
    arg.u.i32 = reinterpret_cast<uint32_t>(value);
    return args.write(arg);
  }
};

template <typename... Args>
struct StoreArgs {};

template <>
struct StoreArgs<> {
  bool operator()(ArgWriter& args) { return true; }
};

template <typename T, typename... Rest>
struct StoreArgs<Specialize<T>, Rest...> {
  bool operator()(ArgWriter& args, Specialize<T> arg0, Rest... rest) {
    if (!StoreArg<T>()(args, arg0.value)) {
      return false;
    }
    return StoreArgs<Rest...>()(args, rest...);
  }
};

template <typename T, typename... Rest>
struct StoreArgs<SpecializeMemory<T>, Rest...> {
  bool operator()(ArgWriter& args, SpecializeMemory<T> arg0, Rest... rest) {
    weval_req_arg_t arg;
    arg.specialize = 1;
    arg.ty = weval_req_arg_buffer;
    arg.u.raw = 0;
    arg.u.buffer.len = arg0.len;
    arg.u.buffer.padded_len = (arg0.len + 7) & ~7;  // Align to 8-byte boundary.
    if (!args.write(arg)) {
      return false;
    }
    const uint8_t* src = reinterpret_cast<const uint8_t*>(arg0.ptr);
    uint8_t* dst = args.alloc(arg.u.buffer.padded_len);
    if (!dst) {
      return false;
    }
    memcpy(dst, src, arg0.len);
    if (arg.u.buffer.padded_len > arg.u.buffer.len) {
      // Ensure deterministic (zeroed) padding bytes.
      memset(dst + arg.u.buffer.len, 0,
             arg.u.buffer.padded_len - arg.u.buffer.len);
    }
    return StoreArgs<Rest...>()(args, rest...);
  }
};

template <typename T, typename... Rest>
struct StoreArgs<RuntimeArg<T>, Rest...> {
  bool operator()(ArgWriter& args, RuntimeArg<T> arg0, Rest... rest) {
    weval_req_arg_t arg;
    arg.specialize = 0;
    arg.ty = weval_req_arg_none;
    arg.u.raw = 0;
    if (!args.write(arg)) {
      return false;
    }
    return StoreArgs<Rest...>()(args, rest...);
  }
};

}  // namespace impl

template <typename Ret, typename... Args, typename... WrappedArgs>
weval_req_t* weval(impl::FuncPtr<Ret, Args...>* dest,
                   impl::FuncPtr<Ret, Args...> generic, uint32_t func_id,
                   uint32_t num_globals,
                   WrappedArgs... args) {
  weval_req_t* req = (weval_req_t*)malloc(sizeof(weval_req_t));
  if (!req) {
    return nullptr;
  }
  ArgWriter writer;
  if (!impl::StoreArgs<WrappedArgs...>()(writer, args...)) {
    return nullptr;
  }

  req->num_globals = num_globals;
  req->func = (weval_func_t)generic;
  req->arglen = writer.len;
  req->argbuf = writer.take();
  req->specialized = (weval_func_t*)dest;

  weval_request(req);

  return req;
}

inline void free(weval_req_t* req) { weval_free(req); }

}  // namespace weval

#endif  // __cplusplus