-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renames JIT to Compiler and notes it is AOT (#564)
This notably changes NewRuntimeJIT to NewRuntimeCompiler as well renames packages from jit to compiler. This clarifies the implementation is AOT, not JIT, at least when clarified to where it occurs (Runtime.CompileModule). In doing so, we reduce any concern that compilation will happen during function execution. We also free ourselves to create a JIT option without confusion in the future via CompileConfig or otherwise. Fixes #560 Signed-off-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
1 parent
dc5d928
commit c815060
Showing
65 changed files
with
606 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners | ||
|
||
/internal/asm/ @mathetake | ||
/internal/wasm/jit/ @mathetake | ||
/internal/engine/compiler/ @mathetake | ||
|
||
* @codefromthecrypt @mathetake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Compiler engine | ||
|
||
This package implements the Compiler engine for WebAssembly *purely written in Go*. | ||
In this README, we describe the background, technical difficulties and some design choices. | ||
|
||
## General limitations on pure Go Compiler engines | ||
|
||
In Go program, each Goroutine manages its own stack, and each item on Goroutine | ||
stack is managed by Go runtime for garbage collection, etc. | ||
|
||
These impose some difficulties on compiler engine purely written in Go because | ||
we *cannot* use native push/pop instructions to save/restore temporary | ||
variables spilling from registers. This results in making it impossible for us | ||
to invoke Go functions from compiled native codes with the native `call` | ||
instruction since it involves stack manipulations. | ||
|
||
*TODO: maybe it is possible to hack the runtime to make it possible to achieve | ||
function calls with `call`.* | ||
|
||
## How to generate native codes | ||
|
||
wazero uses its own assembler, implemented from scratch in the | ||
[`internal/asm`](../../asm/) package. The primary rationale are wazero's zero | ||
dependency policy, and to enable concurrent compilation (a feature the | ||
WebAssembly binary format optimizes for). | ||
|
||
Before this, wazero used [`twitchyliquid64/golang-asm`](https://github.com/twitchyliquid64/golang-asm). | ||
However, this was not only a dependency (one of our goals is to have zero | ||
dependencies), but also a large one (several megabytes added to the binary). | ||
Moreover, any copy of golang-asm is not thread-safe, so can't be used for | ||
concurrent compilation (See [#233](https://github.com/tetratelabs/wazero/issues/233)). | ||
|
||
The assembled native codes are represented as `[]byte` and the slice region is | ||
marked as executable via mmap system call. | ||
|
||
## How to enter native codes | ||
|
||
Assuming that we have a native code as `[]byte`, it is straightforward to enter | ||
the native code region via Go assembly code. In this package, we have the | ||
function without body called `compilercall` | ||
|
||
```go | ||
func compilercall(codeSegment, engine, memory uintptr) | ||
``` | ||
|
||
where we pass `codeSegment uintptr` as a first argument. This pointer is to the | ||
first instruction to be executed. The pointer can be easily derived from | ||
`[]byte` via `unsafe.Pointer`: | ||
|
||
```go | ||
code := []byte{} | ||
/* ...Compilation ...*/ | ||
codeSegment := uintptr(unsafe.Pointer(&code[0])) | ||
compilercall(codeSegment, ...) | ||
``` | ||
|
||
And `compilercall` is actually implemented in [arch_amd64.s](./arch_amd64.s) | ||
as a convenience layer to comply with the Go's official calling convention. | ||
We delegate the task to jump into the code segment to the Go assembler code. | ||
|
||
## How to achieve function calls | ||
|
||
Given that we cannot use `call` instruction at all in native code, here's how | ||
we achieve the function calls back and forth among Go and (compiled) Wasm | ||
native functions. | ||
|
||
TODO: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package jit | ||
package compiler | ||
|
||
var ( | ||
// newArchContext returns a new archContext which is architecture-specific type to be embedded in callEngine. | ||
// This must be initialized in init() function in architecture-specific arch_*.go file which is guarded by build tag. | ||
newArchContext func() archContext | ||
) | ||
|
||
// jitcall is used by callEngine.execWasmFunction and the entrypoint to enter the JITed native code. | ||
// compilercall is used by callEngine.execWasmFunction and the entrypoint to enter the compiled native code. | ||
// codeSegment is the pointer to the initial instruction of the compiled native code. | ||
// ce is "*callEngine" as uintptr. | ||
// | ||
// Note: this is implemented in per-arch Go assembler file. For example, arch_amd64.s implements this for amd64. | ||
func jitcall(codeSegment, ce uintptr, moduleInstanceAddress uintptr) | ||
func compilercall(codeSegment, ce uintptr, moduleInstanceAddress uintptr) |
2 changes: 1 addition & 1 deletion
2
internal/wasm/jit/arch_amd64.go → internal/engine/compiler/arch_amd64.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package jit | ||
package compiler | ||
|
||
import ( | ||
"github.com/tetratelabs/wazero/internal/wazeroir" | ||
|
4 changes: 2 additions & 2 deletions
4
internal/wasm/jit/arch_amd64.s → internal/engine/compiler/arch_amd64.s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
internal/wasm/jit/arch_arm64.s → internal/engine/compiler/arch_arm64.s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.