Repository: tetratelabs/wazero
Pull request #1972 (2024-01-29 18:24:33)

wazevo(amd64): set up spectests


Patch:
diff --git a/cmd/wazero/wazero.go b/cmd/wazero/wazero.go
index 6bb6bb0e..45cfc5f1 100644
--- a/cmd/wazero/wazero.go
+++ b/cmd/wazero/wazero.go
@@ -275,6 +275,9 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer) int {
 	if useInterpreter {
 		rtc = wazero.NewRuntimeConfigInterpreter()
 	} else if useOptimizingCompiler {
+		if runtime.GOARCH != "arm64" {
+			panic("UseOptimizingCompiler is only supported on arm64")
+		}
 		rtc = opt.NewRuntimeConfigOptimizingCompiler()
 	} else {
 		rtc = wazero.NewRuntimeConfig()
diff --git a/experimental/opt/opt.go b/experimental/opt/opt.go
index ecffb001..fa796731 100644
--- a/experimental/opt/opt.go
+++ b/experimental/opt/opt.go
@@ -1,8 +1,6 @@
 package opt
 
 import (
-	"runtime"
-
 	"github.com/tetratelabs/wazero"
 )
 
@@ -14,9 +12,6 @@ type enabler interface {
 
 // 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
diff --git a/internal/engine/wazevo/wazevoapi/debug_options.go b/internal/engine/wazevo/wazevoapi/debug_options.go
index 73b69c0d..1a5c0bc3 100644
--- a/internal/engine/wazevo/wazevoapi/debug_options.go
+++ b/internal/engine/wazevo/wazevoapi/debug_options.go
@@ -196,18 +196,3 @@ func GetCurrentFunctionIndex(ctx context.Context) int {
 	ret, _ := ctx.Value(currentFunctionIndexKey{}).(int)
 	return ret
 }
-
-// ----- High Register Pressure -----
-
-type highRegisterPressureContextKey struct{}
-
-// EnableHighRegisterPressure enables the high register pressure mode.
-func EnableHighRegisterPressure(ctx context.Context) context.Context {
-	ctx = context.WithValue(ctx, highRegisterPressureContextKey{}, true)
-	return ctx
-}
-
-// IsHighRegisterPressure returns true if the current compilation is under high register pressure.
-func IsHighRegisterPressure(ctx context.Context) bool {
-	return ctx.Value(highRegisterPressureContextKey{}) != nil
-}


Test patch:
diff --git a/internal/engine/wazevo/e2e_test.go b/internal/engine/wazevo/e2e_test.go
index 29f786a2..67472c7f 100644
--- a/internal/engine/wazevo/e2e_test.go
+++ b/internal/engine/wazevo/e2e_test.go
@@ -13,7 +13,11 @@ 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/internal/engine/wazevo/testcases"
+	"github.com/tetratelabs/wazero/internal/integration_test/spectest"
+	v1 "github.com/tetratelabs/wazero/internal/integration_test/spectest/v1"
+	v2 "github.com/tetratelabs/wazero/internal/integration_test/spectest/v2"
 	"github.com/tetratelabs/wazero/internal/leb128"
 	"github.com/tetratelabs/wazero/internal/testing/binaryencoding"
 	"github.com/tetratelabs/wazero/internal/testing/dwarftestdata"
@@ -544,7 +548,7 @@ func TestE2E(t *testing.T) {
 				t.Run(name, func(t *testing.T) {
 					cache, err := wazero.NewCompilationCacheWithDir(tmp)
 					require.NoError(t, err)
-					config := newRuntimeConfigOptimizingCompiler().WithCompilationCache(cache)
+					config := opt.NewRuntimeConfigOptimizingCompiler().WithCompilationCache(cache)
 
 					ctx := context.Background()
 					r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -609,7 +613,7 @@ func TestE2E_host_functions(t *testing.T) {
 		t.Run(tc.name, func(t *testing.T) {
 			ctx := tc.ctx
 
-			config := newRuntimeConfigOptimizingCompiler()
+			config := opt.NewRuntimeConfigOptimizingCompiler()
 
 			r := wazero.NewRuntimeWithConfig(ctx, config)
 			defer func() {
@@ -691,7 +695,7 @@ func TestE2E_host_functions(t *testing.T) {
 }
 
 func TestE2E_stores(t *testing.T) {
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 
 	ctx := context.Background()
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -778,7 +782,7 @@ func TestE2E_reexported_memory(t *testing.T) {
 		CodeSection:       []wasm.Code{{Body: []byte{wasm.OpcodeI32Const, 10, wasm.OpcodeMemoryGrow, 0, wasm.OpcodeEnd}}},
 	}
 
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 
 	ctx := context.Background()
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -827,7 +831,7 @@ func TestStackUnwind_panic_in_host(t *testing.T) {
 		},
 	}
 
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 
 	ctx := context.Background()
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -877,7 +881,7 @@ func TestStackUnwind_unreachable(t *testing.T) {
 		},
 	}
 
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 	ctx := context.Background()
 	r := wazero.NewRuntimeWithConfig(ctx, config)
 	defer func() {
@@ -899,7 +903,7 @@ wasm stack trace:
 
 func TestListener_local(t *testing.T) {
 	var buf bytes.Buffer
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))
 
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -927,7 +931,7 @@ func TestListener_local(t *testing.T) {
 
 func TestListener_imported(t *testing.T) {
 	var buf bytes.Buffer
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))
 
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -978,7 +982,7 @@ func TestListener_long(t *testing.T) {
 	})
 
 	var buf bytes.Buffer
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))
 
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -1028,7 +1032,7 @@ func TestListener_long_as_is(t *testing.T) {
 	})
 
 	var buf bytes.Buffer
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))
 
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -1077,7 +1081,7 @@ func TestListener_long_many_consts(t *testing.T) {
 	})
 
 	var buf bytes.Buffer
-	config := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&buf))
 
 	r := wazero.NewRuntimeWithConfig(ctx, config)
@@ -1102,7 +1106,7 @@ 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 := newRuntimeConfigOptimizingCompiler()
+	config := opt.NewRuntimeConfigOptimizingCompiler()
 	ctx := context.Background()
 
 	bin := dwarftestdata.ZigWasm
@@ -1135,11 +1139,191 @@ func TestDWARF(t *testing.T) {
 	require.NoError(t, err)
 }
 
-// newRuntimeConfigOptimizingCompiler is the same as opt.NewRuntimeConfigOptimizingCompiler
-// except that it doesn't check if it is on arm64 or not. Use this in tests until amd64 is completely supported.
-func newRuntimeConfigOptimizingCompiler() wazero.RuntimeConfig {
-	type enabler interface{ EnableOptimizingCompiler() }
-	c := wazero.NewRuntimeConfig()
-	c.(enabler).EnableOptimizingCompiler()
-	return c
+// TODO: delete this and rune them in internal/integration_test/spectest/v1/spec_test.go after amd64 is done.
+func TestSpectestV1(t *testing.T) {
+	if runtime.GOARCH != "amd64" {
+		return
+	}
+
+	config := opt.NewRuntimeConfigOptimizingCompiler()
+
+	for _, tc := range []struct {
+		name string
+	}{
+		{name: "address"},
+		{name: "align"},
+		{name: "br"},
+		//{name: "br_if"},
+		{name: "br_table"},
+		{name: "break-drop"},
+		//{name: "block"},
+		{name: "binary"},
+		{name: "binary-leb128"},
+		//{name: "call"},
+		//{name: "call_indirect"},
+		{name: "comments"},
+		{name: "custom"},
+		//{name: "conversions"},
+		{name: "const"},
+		{name: "data"},
+		//{name: "elem"},
+		//{name: "endianness"},
+		{name: "exports"},
+		//{name: "f32"},
+		//{name: "f32_bitwise"},
+		//{name: "f32_cmp"},
+		//{name: "f64"},
+		//{name: "f64_bitwise"},
+		//{name: "f64_cmp"},
+		//{name: "fac"},
+		//{name: "float_exprs"},
+		//{name: "float_literals"},
+		{name: "float_memory"},
+		//{name: "float_misc"},
+		//{name: "func"},
+		{name: "func_ptrs"},
+		{name: "forward"},
+		//{name: "globals"},
+		//{name: "if"},
+		//{name: "imports"},
+		{name: "inline-module"},
+		//{name: "i32"},
+		//{name: "i64"},
+		//{name: "int_exprs"},
+		{name: "int_literals"},
+		{name: "labels"},
+		//{name: "left-to-right"},
+		//{name: "linking"},
+		//{name: "load"},
+		//{name: "loop"},
+		//{name: "local_get"},
+		//{name: "local_set"},
+		//{name: "local_tee"},
+		//{name: "memory"},
+		{name: "memory_size"},
+		//{name: "memory_grow"},
+		{name: "memory_redundancy"},
+		{name: "memory_trap"},
+		{name: "names"},
+		//{name: "nop"},
+		{name: "return"},
+		//{name: "select"},
+		{name: "start"},
+		{name: "stack"},
+		{name: "store"},
+		//{name: "switch"},
+		{name: "skip-stack-guard-page"},
+		{name: "token"},
+		//{name: "type"},
+		//{name: "traps"},
+		{name: "unreachable"},
+		//{name: "unreached-invalid"},
+		//{name: "unwind"},
+	} {
+		t.Run(tc.name, func(t *testing.T) {
+			spectest.RunCase(t, v1.Testcases, tc.name, context.Background(), config,
+				-1, 0, math.MaxInt)
+		})
+	}
+}
+
+// TODO: delete this and rune them in internal/integration_test/spectest/v2/spec_test.go after amd64 is done.
+func TestSpectestV2(t *testing.T) {
+	if runtime.GOARCH != "amd64" {
+		return
+	}
+
+	config := opt.NewRuntimeConfigOptimizingCompiler()
+
+	for _, tc := range []struct {
+		name string
+	}{
+		//{"block"},
+		//{"bulk"},
+		//{"i32"},
+		//{"i64"},
+		{"br"},
+		//{"call"},
+		//{"call_indirect"},
+		//{"conversions"},
+		//{"global"},
+		//{"if"},
+		//{"linking"},
+		//{"loop"},
+		//{"memory_copy"},
+		//{"memory_fill"},
+		//{"memory_init"},
+		{"memory_trap"},
+		{"ref_null"},
+		//{"ref_is_null"},
+		//{"ref_func"},
+		//{"table_copy"},
+		//{"table_fill"},
+		//{"table_get"},
+		//{"table_init"},
+		//{"table_set"},
+		{"table_size"},
+		//{"select"},
+		//{"simd_boolean"},
+		//{"simd_bit_shift"},
+		//{"simd_bitwise"},
+		//{"simd_const"},
+		//{"simd_conversions"},
+		//{"simd_i8x16_arith"},
+		//{"simd_i8x16_arith2"},
+		//{"simd_i8x16_sat_arith"},
+		//{"simd_i16x8_arith"},
+		//{"simd_i16x8_arith2"},
+		//{"simd_i16x8_sat_arith"},
+		//{"simd_i32x4_arith"},
+		//{"simd_i32x4_arith2"},
+		//{"simd_i64x2_arith"},
+		//{"simd_i64x2_arith2"},
+		//{"simd_i8x16_cmp"},
+		//{"simd_i16x8_cmp"},
+		//{"simd_i32x4_cmp"},
+		//{"simd_i64x2_cmp"},
+		//{"simd_f32x4"},
+		//{"simd_f64x2"},
+		//{"simd_f32x4_arith"},
+		//{"simd_f64x2_arith"},
+		//{"simd_f32x4_cmp"},
+		//{"simd_f64x2_cmp"},
+		//{"simd_f32x4_rounding"},
+		//{"simd_f64x2_rounding"},
+		//{"simd_f32x4_pmin_pmax"},
+		//{"simd_f64x2_pmin_pmax"},
+		//{"simd_i32x4_trunc_sat_f32x4"},
+		//{"simd_i32x4_trunc_sat_f64x2"},
+		//{"simd_i32x4_dot_i16x8"},
+		//{"simd_i16x8_extmul_i8x16"},
+		//{"simd_i64x2_extmul_i32x4"},
+		//{"simd_i32x4_extmul_i16x8"},
+		//{"simd_i16x8_extadd_pairwise_i8x16"},
+		//{"simd_i32x4_extadd_pairwise_i16x8"},
+		//{"simd_i16x8_q15mulr_sat_s"},
+		//{"simd_int_to_int_extend"},
+		//{"simd_load"},
+		//{"simd_load_extend"},
+		//{"simd_load_splat"},
+		//{"simd_load_zero"},
+		//{"simd_load8_lane"},
+		//{"simd_load16_lane"},
+		//{"simd_load32_lane"},
+		//{"simd_load64_lane"},
+		//{"simd_lane"},
+		//{"simd_linking"},
+		//{"simd_splat"},
+		//{"simd_store"},
+		//{"simd_store8_lane"},
+		//{"simd_store16_lane"},
+		//{"simd_store32_lane"},
+		//{"simd_store64_lane"},
+	} {
+		t.Run(tc.name, func(t *testing.T) {
+			ctx := context.Background()
+			spectest.RunCase(t, v2.Testcases, tc.name, ctx, config,
+				-1, 0, math.MaxInt)
+		})
+	}
 }