[go] cmd/compile: (wasm) optimize float32(round64(float64(x)))

4 views
Skip to first unread message

David Chase (Gerrit)

unread,
Dec 15, 2025, 12:31:42 PM (2 days ago) Dec 15
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

David Chase has uploaded the change for review

Commit message

cmd/compile: (wasm) optimize float32(round64(float64(x)))

Not a fix because there are other architectures
still to be done.

Updates #75463.
Change-Id: Ia5233c2b6c5f4439e269950efdd851e72e8e7ff6

Change diff

diff --git a/src/cmd/compile/internal/ssa/_gen/Wasm.rules b/src/cmd/compile/internal/ssa/_gen/Wasm.rules
index 6028152..4b533df 100644
--- a/src/cmd/compile/internal/ssa/_gen/Wasm.rules
+++ b/src/cmd/compile/internal/ssa/_gen/Wasm.rules
@@ -349,6 +349,9 @@
(Abs ...) => (F64Abs ...)
(Copysign ...) => (F64Copysign ...)

+(F32DemoteF64 (F64(Sqrt|Trunc|Ceil|Floor|Nearest|Abs) (F64PromoteF32 x))) => (F32(Sqrt|Trunc|Ceil|Floor|Nearest|Abs) x)
+(F32DemoteF64 (F64Copysign (F64PromoteF32 x) (F64PromoteF32 y))) => (F32Copysign x y)
+
(Sqrt32 ...) => (F32Sqrt ...)

(Ctz64 ...) => (I64Ctz ...)
diff --git a/src/cmd/compile/internal/ssa/rewriteWasm.go b/src/cmd/compile/internal/ssa/rewriteWasm.go
index faba41b..f7fe8fa 100644
--- a/src/cmd/compile/internal/ssa/rewriteWasm.go
+++ b/src/cmd/compile/internal/ssa/rewriteWasm.go
@@ -599,6 +599,8 @@
case OpWB:
v.Op = OpWasmLoweredWB
return true
+ case OpWasmF32DemoteF64:
+ return rewriteValueWasm_OpWasmF32DemoteF64(v)
case OpWasmF64Add:
return rewriteValueWasm_OpWasmF64Add(v)
case OpWasmF64Mul:
@@ -3617,6 +3619,121 @@
}
return false
}
+func rewriteValueWasm_OpWasmF32DemoteF64(v *Value) bool {
+ v_0 := v.Args[0]
+ // match: (F32DemoteF64 (F64Sqrt (F64PromoteF32 x)))
+ // result: (F32Sqrt x)
+ for {
+ if v_0.Op != OpWasmF64Sqrt {
+ break
+ }
+ v_0_0 := v_0.Args[0]
+ if v_0_0.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ x := v_0_0.Args[0]
+ v.reset(OpWasmF32Sqrt)
+ v.AddArg(x)
+ return true
+ }
+ // match: (F32DemoteF64 (F64Trunc (F64PromoteF32 x)))
+ // result: (F32Trunc x)
+ for {
+ if v_0.Op != OpWasmF64Trunc {
+ break
+ }
+ v_0_0 := v_0.Args[0]
+ if v_0_0.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ x := v_0_0.Args[0]
+ v.reset(OpWasmF32Trunc)
+ v.AddArg(x)
+ return true
+ }
+ // match: (F32DemoteF64 (F64Ceil (F64PromoteF32 x)))
+ // result: (F32Ceil x)
+ for {
+ if v_0.Op != OpWasmF64Ceil {
+ break
+ }
+ v_0_0 := v_0.Args[0]
+ if v_0_0.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ x := v_0_0.Args[0]
+ v.reset(OpWasmF32Ceil)
+ v.AddArg(x)
+ return true
+ }
+ // match: (F32DemoteF64 (F64Floor (F64PromoteF32 x)))
+ // result: (F32Floor x)
+ for {
+ if v_0.Op != OpWasmF64Floor {
+ break
+ }
+ v_0_0 := v_0.Args[0]
+ if v_0_0.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ x := v_0_0.Args[0]
+ v.reset(OpWasmF32Floor)
+ v.AddArg(x)
+ return true
+ }
+ // match: (F32DemoteF64 (F64Nearest (F64PromoteF32 x)))
+ // result: (F32Nearest x)
+ for {
+ if v_0.Op != OpWasmF64Nearest {
+ break
+ }
+ v_0_0 := v_0.Args[0]
+ if v_0_0.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ x := v_0_0.Args[0]
+ v.reset(OpWasmF32Nearest)
+ v.AddArg(x)
+ return true
+ }
+ // match: (F32DemoteF64 (F64Abs (F64PromoteF32 x)))
+ // result: (F32Abs x)
+ for {
+ if v_0.Op != OpWasmF64Abs {
+ break
+ }
+ v_0_0 := v_0.Args[0]
+ if v_0_0.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ x := v_0_0.Args[0]
+ v.reset(OpWasmF32Abs)
+ v.AddArg(x)
+ return true
+ }
+ // match: (F32DemoteF64 (F64Copysign (F64PromoteF32 x) (F64PromoteF32 y)))
+ // result: (F32Copysign x y)
+ for {
+ if v_0.Op != OpWasmF64Copysign {
+ break
+ }
+ _ = v_0.Args[1]
+ v_0_0 := v_0.Args[0]
+ if v_0_0.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ x := v_0_0.Args[0]
+ v_0_1 := v_0.Args[1]
+ if v_0_1.Op != OpWasmF64PromoteF32 {
+ break
+ }
+ y := v_0_1.Args[0]
+ v.reset(OpWasmF32Copysign)
+ v.AddArg2(x, y)
+ return true
+ }
+ return false
+}
func rewriteValueWasm_OpWasmF64Add(v *Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]
diff --git a/test/codegen/floats.go b/test/codegen/floats.go
index bf9e70d..3f27e54 100644
--- a/test/codegen/floats.go
+++ b/test/codegen/floats.go
@@ -283,15 +283,22 @@
func WideCeilNarrow(x float32) float32 {
// amd64/v3:"ROUNDSS"
// arm64:"FRINTPS"
+ // wasm:"F32Ceil"
return float32(math.Ceil(float64(x)))
}

func WideTruncNarrow(x float32) float32 {
// amd64/v3:"ROUNDSS"
// arm64:"FRINTZS"
+ // wasm:"F32Trunc"
return float32(math.Trunc(float64(x)))
}

+func WideCopysignNarrow(x, y float32) float32 {
+ // wasm:"F32Copysign"
+ return float32(math.Copysign(float64(x), float64(y)))
+}
+
// ------------------------ //
// Subnormal tests //
// ------------------------ //

Change information

Files:
  • M src/cmd/compile/internal/ssa/_gen/Wasm.rules
  • M src/cmd/compile/internal/ssa/rewriteWasm.go
  • M test/codegen/floats.go
Change size: M
Delta: 3 files changed, 127 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia5233c2b6c5f4439e269950efdd851e72e8e7ff6
Gerrit-Change-Number: 730160
Gerrit-PatchSet: 1
Gerrit-Owner: David Chase <drc...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

David Chase (Gerrit)

unread,
Dec 15, 2025, 12:35:39 PM (2 days ago) Dec 15
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

David Chase voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedNo-Wait-Release
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia5233c2b6c5f4439e269950efdd851e72e8e7ff6
    Gerrit-Change-Number: 730160
    Gerrit-PatchSet: 1
    Gerrit-Owner: David Chase <drc...@google.com>
    Gerrit-Reviewer: David Chase <drc...@google.com>
    Gerrit-Comment-Date: Mon, 15 Dec 2025 17:35:34 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Keith Randall (Gerrit)

    unread,
    Dec 16, 2025, 12:55:19 PM (13 hours ago) Dec 16
    to David Chase, goph...@pubsubhelper.golang.org, Keith Randall, Martin Möhrmann, Gopher Robot, Go LUCI, golang-co...@googlegroups.com
    Attention needed from David Chase and Martin Möhrmann

    Keith Randall voted Code-Review+2

    Code-Review+2
    Open in Gerrit

    Related details

    Attention is currently required from:
    • David Chase
    • Martin Möhrmann
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedNo-Wait-Release
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia5233c2b6c5f4439e269950efdd851e72e8e7ff6
    Gerrit-Change-Number: 730160
    Gerrit-PatchSet: 1
    Gerrit-Owner: David Chase <drc...@google.com>
    Gerrit-Reviewer: David Chase <drc...@google.com>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: David Chase <drc...@google.com>
    Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
    Gerrit-Comment-Date: Tue, 16 Dec 2025 17:55:15 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Keith Randall (Gerrit)

    unread,
    Dec 16, 2025, 12:55:42 PM (13 hours ago) Dec 16
    to David Chase, goph...@pubsubhelper.golang.org, Keith Randall, Martin Möhrmann, Gopher Robot, Go LUCI, golang-co...@googlegroups.com
    Attention needed from David Chase and Martin Möhrmann

    Keith Randall voted Code-Review+1

    Code-Review+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • David Chase
    • Martin Möhrmann
    Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedNo-Wait-Release
      • requirement satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia5233c2b6c5f4439e269950efdd851e72e8e7ff6
      Gerrit-Change-Number: 730160
      Gerrit-PatchSet: 1
      Gerrit-Owner: David Chase <drc...@google.com>
      Gerrit-Reviewer: David Chase <drc...@google.com>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: David Chase <drc...@google.com>
      Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
      Gerrit-Comment-Date: Tue, 16 Dec 2025 17:55:39 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      David Chase (Gerrit)

      unread,
      Dec 16, 2025, 6:21:35 PM (8 hours ago) Dec 16
      to goph...@pubsubhelper.golang.org, Keith Randall, Keith Randall, Martin Möhrmann, Gopher Robot, Go LUCI, golang-co...@googlegroups.com
      Attention needed from Martin Möhrmann

      David Chase voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Martin Möhrmann
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedNo-Wait-Release
      • requirement satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia5233c2b6c5f4439e269950efdd851e72e8e7ff6
      Gerrit-Change-Number: 730160
      Gerrit-PatchSet: 2
      Gerrit-Owner: David Chase <drc...@google.com>
      Gerrit-Reviewer: David Chase <drc...@google.com>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
      Gerrit-Comment-Date: Tue, 16 Dec 2025 23:21:32 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages