Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: bad compilation with inlined math functions #23522

Closed
ianlancetaylor opened this issue Jan 23, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@ianlancetaylor
Copy link
Contributor

commented Jan 23, 2018

This test case is miscompiled with devel +9745eed4fd Fri Jan 12 05:25:55 2018. The programs panics when it should not. This passes with Go 1.9 and gccgo.

CC @randall77

package main

import (
	"math"
)

type S struct {
	u int64
	n int32
}

func F1(f float64) *S {
	s := f
	pf := math.Copysign(f, 1)
	u := math.Floor(pf)
	return &S{
		u: int64(math.Copysign(u, s)),
		n: int32(math.Copysign((pf-u)*1e9, s)),
	}
}

func F2(f float64) *S {
	s := f
	f = math.Copysign(f, 1)
	u := math.Floor(f)
	return &S{
		u: int64(math.Copysign(u, s)),
		n: int32(math.Copysign((f-u)*1e9, s)),
	}
}

func main() {
	s1 := F1(-1)
	s2 := F2(-1)
	if *s1 != *s2 {
		println("F1:", s1.u, s1.n)
		println("F2:", s2.u, s2.n)
		panic("different")
	}
}
@randall77

This comment has been minimized.

Copy link
Contributor

commented Jan 23, 2018

This boils down to a missing interference edge between two views of the same argument.
There's f, the float64 argument, and f, the uint64 argument generated by inlining math.Copysign and from there, math.Float64bits.
This causes the compiler to spill to one of those fs, clobbering the other one that was still live.

@randall77 randall77 self-assigned this Jan 23, 2018

@gopherbot

This comment has been minimized.

Copy link

commented Jan 23, 2018

Change https://golang.org/cl/89335 mentions this issue: cmd/compile: don't let spills clobber arguments

@gopherbot gopherbot closed this in 7eaa8ef Jan 23, 2018

@golang golang locked and limited conversation to collaborators Jan 23, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.