From: David Chase Date: Tue, 28 Oct 2025 17:32:56 +0000 (-0400) Subject: [dev.simd] cmd/compile: enhance inlining for closure-of-SIMD X-Git-Tag: go1.26rc1~147^2~31 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e452f4ac7d;p=gostls13.git [dev.simd] cmd/compile: enhance inlining for closure-of-SIMD We noticed some hand-translated code that used nested functions as the translation of asm macros, and they were too big to inline, and the resulting performance was underwhelming. Any such closures really need to be inlined. Because Gerrit removed votes from a previous patch set, and because in offline discussion we realized that this was actually a hard-to-abuse inlining hack, I decided to turn it up some more, and also add a "this one goes to 11" joke. The number is utterly unprincipled, only "simd is supposed to go fast, and this is a natural use of closures, and we don't want there to be issues where it doesn't go fast." The test verifies that the inlining occurs for a function that exceeds the current inlining threshold. Inspection of the generated code shows that it has the desired effect. Change-Id: I7a8b57c07d6482e6d98cedaf9622c960f956834d Reviewed-on: https://go-review.googlesource.com/c/go/+/715740 LUCI-TryBot-Result: Go LUCI Reviewed-by: Junyang Shao --- diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index 813c019a35..b1ae55cdb6 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -183,9 +183,18 @@ func simdCreditMultiplier(fn *ir.Func) int32 { for _, field := range fn.Type().RecvParamsResults() { if field.Type.IsSIMD() { return 3 - break } } + // Sometimes code uses closures, that do not take simd + // parameters, to perform repetitive SIMD operations. + // fn. These really need to be inlined, or the anticipated + // awesome SIMD performance will be missed. + for _, v := range fn.ClosureVars { + if v.Type().IsSIMD() { + return 11 // 11 ought to be enough. + } + } + return 1 } diff --git a/test/simd_inline.go b/test/simd_inline.go new file mode 100644 index 0000000000..b8c4e0de9e --- /dev/null +++ b/test/simd_inline.go @@ -0,0 +1,42 @@ +// errorcheck -0 -m + +//go:build goexperiment.simd && amd64 + +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo + +import "simd" + +func hasClosure(a, b, c, d simd.Int64x4) (w, x, y, z simd.Int64x4) { + shuf := func() { // ERROR "can inline hasClosure.func1" + w = z.RotateAllLeft(1).Xor(a) + x = w.RotateAllLeft(3).Xor(b) + y = x.RotateAllLeft(5).Xor(c) + z = y.RotateAllLeft(7).Xor(d) + a, b, c, d = b.RotateAllLeft(1).Xor(a.RotateAllLeft(23)), c.RotateAllLeft(1).Xor(b.RotateAllLeft(23)), d.RotateAllLeft(1).Xor(c.RotateAllLeft(23)), a.RotateAllLeft(1).Xor(d.RotateAllLeft(23)) + w = z.RotateAllLeft(1).Xor(a) + x = w.RotateAllLeft(3).Xor(b) + y = x.RotateAllLeft(5).Xor(c) + z = y.RotateAllLeft(7).Xor(d) + a, b, c, d = b.RotateAllLeft(1).Xor(a.RotateAllLeft(23)), c.RotateAllLeft(1).Xor(b.RotateAllLeft(23)), d.RotateAllLeft(1).Xor(c.RotateAllLeft(23)), a.RotateAllLeft(1).Xor(d.RotateAllLeft(23)) + w = z.RotateAllLeft(1).Xor(a) + x = w.RotateAllLeft(3).Xor(b) + y = x.RotateAllLeft(5).Xor(c) + z = y.RotateAllLeft(7).Xor(d) + a, b, c, d = b.RotateAllLeft(1).Xor(a.RotateAllLeft(23)), c.RotateAllLeft(1).Xor(b.RotateAllLeft(23)), d.RotateAllLeft(1).Xor(c.RotateAllLeft(23)), a.RotateAllLeft(1).Xor(d.RotateAllLeft(23)) + } + + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + shuf() // ERROR "inlining call to hasClosure.func1" + return +}