Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces the new optimizing compiler (wazevo) in experimental pkg #1869

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/gojs"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/experimental/sock"
"github.com/tetratelabs/wazero/experimental/sysfs"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
Expand Down Expand Up @@ -161,6 +162,10 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer) int {
flags.BoolVar(&useInterpreter, "interpreter", false,
"Interprets WebAssembly modules instead of compiling them into native code.")

var useOptimizingCompiler bool
flags.BoolVar(&useOptimizingCompiler, "optimizing-compiler", false,
"[Experimental] Compiles WebAssembly modules using the optimizing compiler.")

var envs sliceFlag
flags.Var(&envs, "env", "key=value pair of environment variable to expose to the binary. "+
"Can be specified multiple times.")
Expand Down Expand Up @@ -269,6 +274,8 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer) int {
var rtc wazero.RuntimeConfig
if useInterpreter {
rtc = wazero.NewRuntimeConfigInterpreter()
} else if useOptimizingCompiler {
rtc = opt.NewRuntimeConfigOptimizingCompiler()
} else {
rtc = wazero.NewRuntimeConfig()
}
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/engine/compiler"
"github.com/tetratelabs/wazero/internal/engine/interpreter"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/internal/filecache"
"github.com/tetratelabs/wazero/internal/internalapi"
"github.com/tetratelabs/wazero/internal/platform"
Expand Down Expand Up @@ -190,6 +191,11 @@ type runtimeConfig struct {
ensureTermination bool
}

// EnableOptimizingCompiler implements experimental/opt/enabler.EnableOptimizingCompiler.
func (c *runtimeConfig) EnableOptimizingCompiler() {
c.newEngine = wazevo.NewEngine
}

// engineLessConfig helps avoid copy/pasting the wrong defaults.
var engineLessConfig = &runtimeConfig{
enabledFeatures: api.CoreFeaturesV2,
Expand Down
23 changes: 23 additions & 0 deletions experimental/opt/opt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package opt

import (
"runtime"

"github.com/tetratelabs/wazero"
)

type enabler interface {
// EnableOptimizingCompiler enables the optimizing compiler.
// This is only implemented the internal type of wazero.runtimeConfig.
EnableOptimizingCompiler()
}

// NewRuntimeConfigOptimizingCompiler returns a new RuntimeConfig with the optimizing compiler enabled.
func NewRuntimeConfigOptimizingCompiler() wazero.RuntimeConfig {
if runtime.GOARCH != "arm64" {
panic("UseOptimizingCompiler is only supported on arm64")
}
c := wazero.NewRuntimeConfig()
c.(enabler).EnableOptimizingCompiler()
return c
}
20 changes: 20 additions & 0 deletions experimental/opt/opt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package opt_test

import (
"context"
"runtime"
"testing"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/internal/testing/require"
)

func TestUseOptimizingCompiler(t *testing.T) {
if runtime.GOARCH != "arm64" {
return
}
c := opt.NewRuntimeConfigOptimizingCompiler()
r := wazero.NewRuntimeWithConfig(context.Background(), c)
require.NoError(t, r.Close(context.Background()))
}
42 changes: 0 additions & 42 deletions internal/engine/wazevo/config.go

This file was deleted.

63 changes: 13 additions & 50 deletions internal/engine/wazevo/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/internal/engine/wazevo/testcases"
"github.com/tetratelabs/wazero/internal/leb128"
"github.com/tetratelabs/wazero/internal/testing/binaryencoding"
Expand Down Expand Up @@ -308,10 +308,7 @@ func TestE2E(t *testing.T) {
t.Run(name, func(t *testing.T) {
cache, err := wazero.NewCompilationCacheWithDir(tmp)
require.NoError(t, err)
config := wazero.NewRuntimeConfigCompiler().WithCompilationCache(cache)

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler().WithCompilationCache(cache)

ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx, config)
Expand Down Expand Up @@ -377,10 +374,7 @@ func TestE2E_host_functions(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctx := tc.ctx

config := wazero.NewRuntimeConfigCompiler()

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler()

r := wazero.NewRuntimeWithConfig(ctx, config)
defer func() {
Expand Down Expand Up @@ -462,10 +456,7 @@ func TestE2E_host_functions(t *testing.T) {
}

func TestE2E_stores(t *testing.T) {
config := wazero.NewRuntimeConfigCompiler()

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler()

ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx, config)
Expand Down Expand Up @@ -552,10 +543,7 @@ func TestE2E_reexported_memory(t *testing.T) {
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeI32Const, 10, wasm.OpcodeMemoryGrow, 0, wasm.OpcodeEnd}}},
}

config := wazero.NewRuntimeConfigCompiler()

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler()

ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx, config)
Expand Down Expand Up @@ -604,10 +592,7 @@ func TestStackUnwind_panic_in_host(t *testing.T) {
},
}

config := wazero.NewRuntimeConfigCompiler()

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler()

ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx, config)
Expand Down Expand Up @@ -657,11 +642,7 @@ func TestStackUnwind_unreachable(t *testing.T) {
},
}

config := wazero.NewRuntimeConfigCompiler()

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)

config := opt.NewRuntimeConfigOptimizingCompiler()
ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx, config)
defer func() {
Expand All @@ -683,12 +664,9 @@ wasm stack trace:

func TestListener_local(t *testing.T) {
var buf bytes.Buffer
config := wazero.NewRuntimeConfigCompiler()
config := opt.NewRuntimeConfigOptimizingCompiler()
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)

r := wazero.NewRuntimeWithConfig(ctx, config)
defer func() {
require.NoError(t, r.Close(ctx))
Expand All @@ -714,12 +692,9 @@ func TestListener_local(t *testing.T) {

func TestListener_imported(t *testing.T) {
var buf bytes.Buffer
config := wazero.NewRuntimeConfigCompiler()
config := opt.NewRuntimeConfigOptimizingCompiler()
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)

r := wazero.NewRuntimeWithConfig(ctx, config)
defer func() {
require.NoError(t, r.Close(ctx))
Expand Down Expand Up @@ -768,12 +743,9 @@ func TestListener_long(t *testing.T) {
})

var buf bytes.Buffer
config := wazero.NewRuntimeConfigCompiler()
config := opt.NewRuntimeConfigOptimizingCompiler()
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)

r := wazero.NewRuntimeWithConfig(ctx, config)
defer func() {
require.NoError(t, r.Close(ctx))
Expand Down Expand Up @@ -821,12 +793,9 @@ func TestListener_long_as_is(t *testing.T) {
})

var buf bytes.Buffer
config := wazero.NewRuntimeConfigCompiler()
config := opt.NewRuntimeConfigOptimizingCompiler()
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)

r := wazero.NewRuntimeWithConfig(ctx, config)
defer func() {
require.NoError(t, r.Close(ctx))
Expand Down Expand Up @@ -873,12 +842,9 @@ func TestListener_long_many_consts(t *testing.T) {
})

var buf bytes.Buffer
config := wazero.NewRuntimeConfigCompiler()
config := opt.NewRuntimeConfigOptimizingCompiler()
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)

r := wazero.NewRuntimeWithConfig(ctx, config)
defer func() {
require.NoError(t, r.Close(ctx))
Expand All @@ -901,12 +867,9 @@ func TestListener_long_many_consts(t *testing.T) {

// TestDWARF verifies that the DWARF based stack traces work as expected before/after compilation cache.
func TestDWARF(t *testing.T) {
config := wazero.NewRuntimeConfigCompiler()
config := opt.NewRuntimeConfigOptimizingCompiler()
ctx := context.Background()

// Configure the new optimizing backend!
wazevo.ConfigureWazevo(config)

bin := dwarftestdata.ZigWasm

dir := t.TempDir()
Expand Down
5 changes: 2 additions & 3 deletions internal/integration_test/engine/adhoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/experimental/table"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/internal/leb128"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/binaryencoding"
Expand Down Expand Up @@ -96,8 +96,7 @@ func TestEngineWazevo(t *testing.T) {
if runtime.GOARCH != "arm64" {
t.Skip()
}
config := wazero.NewRuntimeConfigInterpreter()
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler()
runAllTests(t, tests, config.WithCloseOnContextDone(true), true)
}

Expand Down
5 changes: 2 additions & 3 deletions internal/integration_test/engine/dwarf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"testing"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/dwarftestdata"
"github.com/tetratelabs/wazero/internal/testing/require"
Expand Down Expand Up @@ -37,8 +37,7 @@ func TestEngineWazevo_DWARF(t *testing.T) {
if runtime.GOARCH != "arm64" {
t.Skip()
}
config := wazero.NewRuntimeConfigInterpreter()
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler()
runAllTests(t, dwarfTests, config, true)
}

Expand Down
5 changes: 2 additions & 3 deletions internal/integration_test/engine/hammer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/hammer"
"github.com/tetratelabs/wazero/internal/testing/require"
Expand Down Expand Up @@ -36,8 +36,7 @@ func TestEngineWazevo_hammer(t *testing.T) {
if runtime.GOARCH != "arm64" {
t.Skip()
}
c := wazero.NewRuntimeConfigInterpreter()
wazevo.ConfigureWazevo(c)
c := opt.NewRuntimeConfigOptimizingCompiler()
runAllTests(t, hammers, c, true)
}

Expand Down
5 changes: 2 additions & 3 deletions internal/integration_test/engine/memleak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/experimental/opt"
)

func TestMemoryLeak(t *testing.T) {
Expand Down Expand Up @@ -61,8 +61,7 @@ func testMemoryLeakInstantiateRuntimeAndModule(isWazevo bool) error {

var r wazero.Runtime
if isWazevo {
c := wazero.NewRuntimeConfigInterpreter()
wazevo.ConfigureWazevo(c)
c := opt.NewRuntimeConfigOptimizingCompiler()
r = wazero.NewRuntimeWithConfig(ctx, c)
} else {
r = wazero.NewRuntime(ctx)
Expand Down
5 changes: 2 additions & 3 deletions internal/integration_test/filecache/filecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/internal/engine/wazevo"
"github.com/tetratelabs/wazero/experimental/opt"
"github.com/tetratelabs/wazero/internal/integration_test/spectest"
v1 "github.com/tetratelabs/wazero/internal/integration_test/spectest/v1"
"github.com/tetratelabs/wazero/internal/platform"
Expand All @@ -35,8 +35,7 @@ func TestFileCacheSpecTest_wazevo(t *testing.T) {
if runtime.GOARCH != "arm64" {
return
}
config := wazero.NewRuntimeConfigCompiler()
wazevo.ConfigureWazevo(config)
config := opt.NewRuntimeConfigOptimizingCompiler()
runAllFileCacheTests(t, config)
}

Expand Down
Loading
Loading