Skip to content

Commit 9b8742f

Browse files
committed
cmd/compile: don't depend on arch-dependent conversions in the compiler
Leave those constant foldings for runtime, similar to how we do it for NaN generation. These are the only instances I could find in cmd/compile/..., using objdump -d ../pkg/tool/darwin_arm64/compile| egrep "(fcvtz|>:)" | grep -B1 fcvt (There are instances in other places, like runtime and reflect, but I don't think those places would affect compiler output.) Change-Id: I4113fe4570115e4765825cf442cb1fde97cf2f27 Reviewed-on: https://go-review.googlesource.com/c/go/+/711281 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Keith Randall <khr@google.com>
1 parent 0e64ee1 commit 9b8742f

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
(Cvt32to64F (Const32 [c])) => (Const64F [float64(c)])
5151
(Cvt64to32F (Const64 [c])) => (Const32F [float32(c)])
5252
(Cvt64to64F (Const64 [c])) => (Const64F [float64(c)])
53-
(Cvt32Fto32 (Const32F [c])) => (Const32 [int32(c)])
54-
(Cvt32Fto64 (Const32F [c])) => (Const64 [int64(c)])
55-
(Cvt64Fto32 (Const64F [c])) => (Const32 [int32(c)])
56-
(Cvt64Fto64 (Const64F [c])) => (Const64 [int64(c)])
53+
(Cvt32Fto32 (Const32F [c])) && c >= -1<<31 && c < 1<<31 => (Const32 [int32(c)])
54+
(Cvt32Fto64 (Const32F [c])) && c >= -1<<63 && c < 1<<63 => (Const64 [int64(c)])
55+
(Cvt64Fto32 (Const64F [c])) && c >= -1<<31 && c < 1<<31 => (Const32 [int32(c)])
56+
(Cvt64Fto64 (Const64F [c])) && c >= -1<<63 && c < 1<<63 => (Const64 [int64(c)])
5757
(Round32F x:(Const32F)) => x
5858
(Round64F x:(Const64F)) => x
5959
(CvtBoolToUint8 (ConstBool [false])) => (Const8 [0])

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

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

test/codegen/math.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,48 @@ func nanGenerate32() float32 {
330330
// amd64/v3:"VFMADD231SS"
331331
return z0 + z1
332332
}
333+
334+
func outOfBoundsConv(i32 *[2]int32, u32 *[2]uint32, i64 *[2]int64, u64 *[2]uint64) {
335+
// arm64: "FCVTZSDW"
336+
// amd64: "CVTTSD2SL", "CVTSD2SS"
337+
i32[0] = int32(two40())
338+
// arm64: "FCVTZSDW"
339+
// amd64: "CVTTSD2SL", "CVTSD2SS"
340+
i32[1] = int32(-two40())
341+
// arm64: "FCVTZSDW"
342+
// amd64: "CVTTSD2SL", "CVTSD2SS"
343+
u32[0] = uint32(two41())
344+
// on arm64, this uses an explicit <0 comparison, so it constant folds.
345+
// on amd64, this uses an explicit <0 comparison, so it constant folds.
346+
// amd64: "MOVL\t[$]0,"
347+
u32[1] = uint32(minus1())
348+
// arm64: "FCVTZSD"
349+
// amd64: "CVTTSD2SQ"
350+
i64[0] = int64(two80())
351+
// arm64: "FCVTZSD"
352+
// amd64: "CVTTSD2SQ"
353+
i64[1] = int64(-two80())
354+
// arm64: "FCVTZUD"
355+
// amd64: "CVTTSD2SQ"
356+
u64[0] = uint64(two81())
357+
// arm64: "FCVTZUD"
358+
// on amd64, this uses an explicit <0 comparison, so it constant folds.
359+
// amd64: "MOVQ\t[$]0,"
360+
u64[1] = uint64(minus1())
361+
}
362+
363+
func two40() float64 {
364+
return 1 << 40
365+
}
366+
func two41() float64 {
367+
return 1 << 41
368+
}
369+
func two80() float64 {
370+
return 1 << 80
371+
}
372+
func two81() float64 {
373+
return 1 << 81
374+
}
375+
func minus1() float64 {
376+
return -1
377+
}

0 commit comments

Comments
 (0)