Skip to content

Commit 8d81028

Browse files
committed
cmd/compile: make wasm match other platforms for FP->int32/64 conversions
this change is for overflows, so that all the platforms agree. Change-Id: I9f459353615bf24ef8a5de641063d9ce34986241 Reviewed-on: https://go-review.googlesource.com/c/go/+/708358 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent b9f3acc commit 8d81028

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

src/cmd/compile/internal/ssa/_gen/Wasm.rules

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@
7676
(Cvt32Uto(64|32)F x) => (F(64|32)ConvertI64U (ZeroExt32to64 x))
7777
(Cvt64Uto(64|32)F ...) => (F(64|32)ConvertI64U ...)
7878

79-
(Cvt32Fto32 ...) => (I64TruncSatF32S ...)
79+
(Cvt32Fto32 ...) => (I32TruncSatF32S ...)
8080
(Cvt32Fto64 ...) => (I64TruncSatF32S ...)
81-
(Cvt64Fto32 ...) => (I64TruncSatF64S ...)
81+
(Cvt64Fto32 ...) => (I32TruncSatF64S ...)
8282
(Cvt64Fto64 ...) => (I64TruncSatF64S ...)
83-
(Cvt32Fto32U ...) => (I64TruncSatF32U ...)
83+
84+
(Cvt32Fto32U ...) => (I32TruncSatF32U ...)
8485
(Cvt32Fto64U ...) => (I64TruncSatF32U ...)
85-
(Cvt64Fto32U ...) => (I64TruncSatF64U ...)
86+
(Cvt64Fto32U ...) => (I32TruncSatF64U ...)
8687
(Cvt64Fto64U ...) => (I64TruncSatF64U ...)
8788

8889
(Cvt32Fto64F ...) => (F64PromoteF32 ...)

src/cmd/compile/internal/ssa/_gen/WasmOps.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,19 @@ func init() {
222222
{name: "F64Mul", asm: "F64Mul", argLength: 2, reg: fp64_21, typ: "Float64"}, // arg0 * arg1
223223
{name: "F64Div", asm: "F64Div", argLength: 2, reg: fp64_21, typ: "Float64"}, // arg0 / arg1
224224

225-
{name: "I64TruncSatF64S", asm: "I64TruncSatF64S", argLength: 1, reg: regInfo{inputs: []regMask{fp64}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to a signed integer (saturating)
226-
{name: "I64TruncSatF64U", asm: "I64TruncSatF64U", argLength: 1, reg: regInfo{inputs: []regMask{fp64}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to an unsigned integer (saturating)
227-
{name: "I64TruncSatF32S", asm: "I64TruncSatF32S", argLength: 1, reg: regInfo{inputs: []regMask{fp32}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to a signed integer (saturating)
228-
{name: "I64TruncSatF32U", asm: "I64TruncSatF32U", argLength: 1, reg: regInfo{inputs: []regMask{fp32}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to an unsigned integer (saturating)
225+
{name: "I64TruncSatF64S", asm: "I64TruncSatF64S", argLength: 1, reg: regInfo{inputs: []regMask{fp64}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to a signed integer (saturating)
226+
{name: "I64TruncSatF64U", asm: "I64TruncSatF64U", argLength: 1, reg: regInfo{inputs: []regMask{fp64}, outputs: []regMask{gp}}, typ: "Uint64"}, // truncates the float arg0 to an unsigned integer (saturating)
227+
{name: "I64TruncSatF32S", asm: "I64TruncSatF32S", argLength: 1, reg: regInfo{inputs: []regMask{fp32}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to a signed integer (saturating)
228+
{name: "I64TruncSatF32U", asm: "I64TruncSatF32U", argLength: 1, reg: regInfo{inputs: []regMask{fp32}, outputs: []regMask{gp}}, typ: "Uint64"}, // truncates the float arg0 to an unsigned integer (saturating)
229+
230+
// It appears to be wasm convention that everything lands in a 64-bit register;
231+
// the WASM instructions for these operations produce 32-bit width results, but
232+
// wasm/ssa.go widens them appropriately to 64-bit results.
233+
{name: "I32TruncSatF64S", asm: "I32TruncSatF64S", argLength: 1, reg: regInfo{inputs: []regMask{fp64}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to a signed integer (saturating)
234+
{name: "I32TruncSatF64U", asm: "I32TruncSatF64U", argLength: 1, reg: regInfo{inputs: []regMask{fp64}, outputs: []regMask{gp}}, typ: "Uint64"}, // truncates the float arg0 to an unsigned integer (saturating)
235+
{name: "I32TruncSatF32S", asm: "I32TruncSatF32S", argLength: 1, reg: regInfo{inputs: []regMask{fp32}, outputs: []regMask{gp}}, typ: "Int64"}, // truncates the float arg0 to a signed integer (saturating)
236+
{name: "I32TruncSatF32U", asm: "I32TruncSatF32U", argLength: 1, reg: regInfo{inputs: []regMask{fp32}, outputs: []regMask{gp}}, typ: "Uint64"}, // truncates the float arg0 to an unsigned integer (saturating)
237+
229238
{name: "F32ConvertI64S", asm: "F32ConvertI64S", argLength: 1, reg: regInfo{inputs: []regMask{gp}, outputs: []regMask{fp32}}, typ: "Float32"}, // converts the signed integer arg0 to a float
230239
{name: "F32ConvertI64U", asm: "F32ConvertI64U", argLength: 1, reg: regInfo{inputs: []regMask{gp}, outputs: []regMask{fp32}}, typ: "Float32"}, // converts the unsigned integer arg0 to a float
231240
{name: "F64ConvertI64S", asm: "F64ConvertI64S", argLength: 1, reg: regInfo{inputs: []regMask{gp}, outputs: []regMask{fp64}}, typ: "Float64"}, // converts the signed integer arg0 to a float

src/cmd/compile/internal/ssa/opGen.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/ssa/rewriteWasm.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/wasm/ssa.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,17 @@ func ssaGenValueOnStack(s *ssagen.State, v *ssa.Value, extend bool) {
430430
getValue64(s, v.Args[0])
431431
s.Prog(v.Op.Asm())
432432

433+
// 32-bit integer conversion results
434+
case ssa.OpWasmI32TruncSatF32S, ssa.OpWasmI32TruncSatF64S:
435+
getValue64(s, v.Args[0])
436+
s.Prog(v.Op.Asm())
437+
s.Prog(wasm.AI64ExtendI32S)
438+
439+
case ssa.OpWasmI32TruncSatF32U, ssa.OpWasmI32TruncSatF64U:
440+
getValue64(s, v.Args[0])
441+
s.Prog(v.Op.Asm())
442+
s.Prog(wasm.AI64ExtendI32U)
443+
433444
case ssa.OpWasmF32DemoteF64:
434445
getValue64(s, v.Args[0])
435446
s.Prog(v.Op.Asm())

test/convert5.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
// Use of this source code is governed by a BSD-style
55
// license that can be found in the LICENSE file.
66

7-
//go:build !wasm
8-
9-
// TODO fix this to work for wasm
10-
// Doing more than this, however, expands the change.
11-
127
package main
138

149
import (

0 commit comments

Comments
 (0)