github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/engine/compiler/RATIONALE.md (about)

     1  # Compiler engine
     2  
     3  This package implements the Compiler engine for WebAssembly *purely written in Go*.
     4  In this README, we describe the background, technical difficulties and some design choices.
     5  
     6  ## General limitations on pure Go Compiler engines
     7  
     8  In Go program, each Goroutine manages its own stack, and each item on Goroutine
     9  stack is managed by Go runtime for garbage collection, etc.
    10  
    11  These impose some difficulties on compiler engine purely written in Go because
    12  we *cannot* use native push/pop instructions to save/restore temporary
    13  variables spilling from registers. This results in making it impossible for us
    14  to invoke Go functions from compiled native codes with the native `call`
    15  instruction since it involves stack manipulations.
    16  
    17  *TODO: maybe it is possible to hack the runtime to make it possible to achieve
    18  function calls with `call`.*
    19  
    20  ## How to generate native codes
    21  
    22  wazero uses its own assembler, implemented from scratch in the
    23  [`internal/asm`](../../asm/) package. The primary rationale are wazero's zero
    24  dependency policy, and to enable concurrent compilation (a feature the
    25  WebAssembly binary format optimizes for).
    26  
    27  Before this, wazero used [`twitchyliquid64/golang-asm`](https://github.com/twitchyliquid64/golang-asm).
    28  However, this was not only a dependency (one of our goals is to have zero
    29  dependencies), but also a large one (several megabytes added to the binary).
    30  Moreover, any copy of golang-asm is not thread-safe, so can't be used for
    31  concurrent compilation (See [#233](https://github.com/tetratelabs/wazero/issues/233)).
    32  
    33  The assembled native codes are represented as `[]byte` and the slice region is
    34  marked as executable via mmap system call.
    35  
    36  ## How to enter native codes
    37  
    38  Assuming that we have a native code as `[]byte`, it is straightforward to enter
    39  the native code region via Go assembly code. In this package, we have the
    40  function without body called `nativecall`
    41  
    42  ```go
    43  func nativecall(codeSegment, engine, memory uintptr)
    44  ```
    45  
    46  where we pass `codeSegment uintptr` as a first argument. This pointer is to the
    47  first instruction to be executed. The pointer can be easily derived from
    48  `[]byte` via `unsafe.Pointer`:
    49  
    50  ```go
    51  code := []byte{}
    52  /* ...Compilation ...*/
    53  codeSegment := uintptr(unsafe.Pointer(&code[0]))
    54  nativecall(codeSegment, ...)
    55  ```
    56  
    57  And `nativecall` is actually implemented in [arch_amd64.s](./arch_amd64.s)
    58  as a convenience layer to comply with the Go's official calling convention.
    59  We delegate the task to jump into the code segment to the Go assembler code.
    60  
    61  
    62  ## Why it's safe to execute runtime-generated machine codes against async Goroutine preemption
    63  
    64  Goroutine preemption is the mechanism of the Go runtime to switch goroutines contexts on an OS thread.
    65  There are two types of preemption: cooperative preemption and async preemption. The former happens, for example,
    66  when making a function call, and it is not an issue for our runtime-generated functions as they do not make
    67  direct function calls to Go-implemented functions. On the other hand, the latter, async preemption, can be problematic
    68  since it tries to interrupt the execution of Goroutine at any point of function, and manipulates CPU register states.
    69  
    70  Fortunately, our runtime-generated machine codes do not need to take the async preemption into account.
    71  All the assembly codes are entered via the trampoline implemented as Go Assembler Function (e.g. [arch_amd64.s](./arch_amd64.s)),
    72  and as of Go 1.20, these assembler functions are considered as _unsafe_ for async preemption:
    73  - https://github.com/golang/go/blob/go1.20rc1/src/runtime/preempt.go#L406-L407
    74  - https://github.com/golang/go/blob/9f0234214473dfb785a5ad84a8fc62a6a395cbc3/src/runtime/traceback.go#L227
    75  
    76  From the Go runtime point of view, the execution of runtime-generated machine codes is considered as a part of
    77  that trampoline function. Therefore, runtime-generated machine code is also correctly considered unsafe for async preemption.
    78  
    79  ## Why context cancellation is handled in Go code rather than native code
    80  
    81  Since [wazero v1.0.0-pre.9](https://github.com/tetratelabs/wazero/releases/tag/v1.0.0-pre.9), the runtime
    82  supports integration with Go contexts to interrupt execution after a timeout, or in response to explicit cancellation.
    83  This support is internally implemented as a special opcode `builtinFunctionCheckExitCode` that triggers the execution of
    84  a Go function (`ModuleInstance.FailIfClosed`) that atomically checks a sentinel value at strategic points in the code
    85  (e.g. [within loops][checkexitcode_loop]).
    86  
    87  [It _is indeed_ possible to check the sentinel value directly, without leaving the native world][native_check], thus sparing some cycles;
    88  however, because native code never preempts (see section above), this may lead to a state where the other goroutines
    89  never get the chance to run, and thus never get the chance to set the sentinel value; effectively preventing
    90  cancellation from taking place.
    91  
    92  [checkexitcode_loop]: https://github.com/tetratelabs/wazero/blob/86444c67a37dbf9e693ae5b365901f64968d9025/internal/wazeroir/compiler.go#L467-L476
    93  [native_check]: https://github.com/tetratelabs/wazero/issues/1409
    94  
    95  ## Source Offset Mapping
    96  
    97  When translating code from WebAssembly to the wazero IR, and compiling to native
    98  binary, wazero keeps track of two indexes to correlate native program counters
    99  to the original source offset that they were generated from.
   100  
   101  Source offset maps are useful for debugging, but holding indexes in memory for
   102  all instructions can have a significant overhead. To reduce the memory footprint
   103  of the compiled modules, wazero uses data structures inspired by
   104  [frame-of-reference and delta encoding][FOR].
   105  
   106  Because wazero does not reorder instructions, the source offsets are naturally
   107  sorted during compilation, and the distance between two consecutive offsets is
   108  usually small. Encoding deltas instead of the absolute values allows most of
   109  the indexes to store offsets with an overhead of 8 bits per instruction, instead
   110  of recording 64 bits integers for absolute code positions.
   111  
   112  [FOR]: https://lemire.me/blog/2012/02/08/effective-compression-using-frame-of-reference-and-delta-coding/