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

runtime: mask bucket shift on s390x? #31165

Closed
josharian opened this issue Mar 30, 2019 · 3 comments
Closed

runtime: mask bucket shift on s390x? #31165

josharian opened this issue Mar 30, 2019 · 3 comments

Comments

@josharian
Copy link
Contributor

// bucketShift returns 1<<b, optimized for code generation.
func bucketShift(b uint8) uintptr {
	if sys.GoarchAmd64|sys.GoarchAmd64p32|sys.Goarch386 != 0 {
		b &= sys.PtrSize*8 - 1 // help x86 archs remove shift overflow checks
	}
	return uintptr(1) << b
}

IIRC, s390x also makes use of masks to remove shift overflow checks. @mundaym care to send a CL adding s390x if that's correct? (And close this issue if not?)

@josharian josharian added this to the Go1.13 milestone Mar 30, 2019
@mundaym
Copy link
Member

mundaym commented Mar 31, 2019

Yes, s390x does (and so does ppc64{,le} as well actually). My vote would be just to get rid of the if statement since in the worst case it just adds one extra AND instruction, and most architectures can generate better code with the mask even if they don't currently.

If we are particularly worried about this code path for some reason we could add an extra bunch of generic rules to detect this pattern and turn it into a rotate instruction, since rotate and shift are equivalent operations when the input is 1 and the shift amount is masked to the bit-width of the input.

@mundaym
Copy link
Member

mundaym commented Mar 31, 2019

Something like this would do the trick I think:

(Lsh32x(8|16|32|64) x:(Const32 [1]) (And(8|16|32|64) (Const(8|16|32|64) [31]) y)) -> (RotateLeft32 x y)
(Lsh64x(8|16|32|64) x:(Const64 [1]) (And(8|16|32|64) (Const(8|16|32|64) [63]) y)) -> (RotateLeft64 x y)

@gopherbot
Copy link

Change https://golang.org/cl/170122 mentions this issue: runtime: always mask shift amount regardless of architecture

@golang golang locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants