Repository: tetratelabs/wazero
Pull request #1993 (2024-01-31 22:24:22)

wazevo(amd64): fixes sat conversions


Patch:
diff --git a/internal/engine/wazevo/backend/isa/amd64/machine.go b/internal/engine/wazevo/backend/isa/amd64/machine.go
index 81627836..effb62e2 100644
--- a/internal/engine/wazevo/backend/isa/amd64/machine.go
+++ b/internal/engine/wazevo/backend/isa/amd64/machine.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"encoding/binary"
 	"fmt"
+	"math"
 	"strings"
 
 	"github.com/tetratelabs/wazero/internal/engine/wazevo/backend"
@@ -353,14 +354,14 @@ func (m *machine) LowerInstr(instr *ssa.Instruction) {
 		cnt := m.allocateInstr()
 		cnt.asXmmUnaryRmR(sseOpcodeCvtsd2ss, rn, rd)
 		m.insert(cnt)
-	case ssa.OpcodeFcvtToSint:
+	case ssa.OpcodeFcvtToSint, ssa.OpcodeFcvtToSintSat:
 		x, ctx := instr.Arg2()
 		rn := m.getOperand_Reg(m.c.ValueDefinition(x))
 		rd := newOperandReg(m.c.VRegOf(instr.Return()))
 		ctxVReg := m.c.VRegOf(ctx)
-		m.lowerFcvtToSint(ctxVReg, rn, rd, true, x.Type() == ssa.TypeF64,
+		m.lowerFcvtToSint(ctxVReg, rn, rd, x.Type() == ssa.TypeF64,
 			instr.Return().Type().Bits() == 64, op == ssa.OpcodeFcvtToSintSat)
-	case ssa.OpcodeFcvtToUint:
+	case ssa.OpcodeFcvtToUint, ssa.OpcodeFcvtToUintSat:
 		x, ctx := instr.Arg2()
 		rn := m.getOperand_Reg(m.c.ValueDefinition(x))
 		rd := newOperandReg(m.c.VRegOf(instr.Return()))
@@ -1765,7 +1766,7 @@ func (m *machine) lowerBitcast(instr *ssa.Instruction) {
 	}
 }
 
-func (m *machine) lowerFcvtToSint(ctxVReg regalloc.VReg, rn, rd operand, signed, src64, dst64, sat bool) {
+func (m *machine) lowerFcvtToSint(ctxVReg regalloc.VReg, rn, rd operand, src64, dst64, sat bool) {
 	var tmp regalloc.VReg
 	if dst64 {
 		tmp = m.c.AllocateVReg(ssa.TypeF64)
@@ -1832,14 +1833,14 @@ func (m *machine) lowerFcvtToSint(ctxVReg regalloc.VReg, rn, rd operand, signed,
 
 		// if >= jump to end.
 		jmpEnd2 := m.allocateInstr()
-		jmpEnd.asJmp(newOperandLabel(done))
+		jmpEnd2.asJmpIf(condB, newOperandLabel(done))
 		m.insert(jmpEnd2)
 
 		// Otherwise, saturate to INT_MAX.
-		if src64 {
-			m.lowerIconst(tmpDst, 0x7fffffffffffffff, dst64)
+		if dst64 {
+			m.lowerIconst(tmpDst, math.MaxInt64, dst64)
 		} else {
-			m.lowerIconst(tmpDst, 0x7fffffff, dst64)
+			m.lowerIconst(tmpDst, math.MaxInt32, dst64)
 		}
 
 	} else {
@@ -2029,9 +2030,9 @@ func (m *machine) lowerFcvtToUint(ctxVReg regalloc.VReg, rn, rd operand, src64,
 		// overflow is because the input was too large: saturate to the max value.
 		var maxInt uint64
 		if dst64 {
-			maxInt = 0x7fffffffffffffff
+			maxInt = math.MaxUint64
 		} else {
-			maxInt = 0x7fffffff
+			maxInt = math.MaxUint32
 		}
 		m.lowerIconst(tmpDst, maxInt, dst64)
 


Test patch:
diff --git a/internal/engine/wazevo/e2e_test.go b/internal/engine/wazevo/e2e_test.go
index 4dd81b4b..123e7ce1 100644
--- a/internal/engine/wazevo/e2e_test.go
+++ b/internal/engine/wazevo/e2e_test.go
@@ -1284,7 +1284,7 @@ func TestSpectestV2(t *testing.T) {
 		{"br"},
 		{"call"},
 		{"call_indirect"},
-		//{"conversions"},
+		{"conversions"},
 		{"global"},
 		{"if"},
 		{"linking"},
diff --git a/internal/integration_test/filecache/filecache_test.go b/internal/integration_test/filecache/filecache_test.go
index f1bf16da..88bb0950 100644
--- a/internal/integration_test/filecache/filecache_test.go
+++ b/internal/integration_test/filecache/filecache_test.go
@@ -6,7 +6,6 @@ import (
 	"fmt"
 	"os"
 	"os/exec"
-	"runtime"
 	"strings"
 	"testing"
 	"time"
@@ -24,15 +23,15 @@ import (
 	"github.com/tetratelabs/wazero/internal/wasm"
 )
 
-func TestFileCacheSpecTest_compiler(t *testing.T) {
+func TestFileCache_compiler(t *testing.T) {
 	if !platform.CompilerSupported() {
 		return
 	}
 	runAllFileCacheTests(t, wazero.NewRuntimeConfigCompiler())
 }
 
-func TestFileCacheSpecTest_wazevo(t *testing.T) {
-	if runtime.GOARCH != "arm64" {
+func TestFileCache_wazevo(t *testing.T) {
+	if !platform.CompilerSupported() {
 		return
 	}
 	config := opt.NewRuntimeConfigOptimizingCompiler()