Skip to content

Commit adb467f

Browse files
egonelbrejosharian
authored andcommitted
cmd/compile: reduce inline cost of OCONVOP
OCONVOP doesn't have effect in the compiled code so, it can be safely excluded from inline cost calculation. Also make sequence ODEREF OCONVNOP* OADDR cost 1. This is rather common conversion, such as *(*uint32)(unsafe.Pointer(&x)). Fixes #42788 Change-Id: I5001f7e89d985c198b6405694cdd5b819cf3f47a Reviewed-on: https://go-review.googlesource.com/c/go/+/281232 Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Elias Naur <[email protected]>
1 parent 27684ea commit adb467f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/cmd/compile/internal/inline/inl.go

+16
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,22 @@ func (v *hairyVisitor) doNode(n ir.Node) bool {
384384
case ir.OAPPEND:
385385
v.budget -= inlineExtraAppendCost
386386

387+
case ir.ODEREF:
388+
// *(*X)(unsafe.Pointer(&x)) is low-cost
389+
n := n.(*ir.StarExpr)
390+
391+
ptr := n.X
392+
for ptr.Op() == ir.OCONVNOP {
393+
ptr = ptr.(*ir.ConvExpr).X
394+
}
395+
if ptr.Op() == ir.OADDR {
396+
v.budget += 1 // undo half of default cost of ir.ODEREF+ir.OADDR
397+
}
398+
399+
case ir.OCONVNOP:
400+
// This doesn't produce code, but the children might.
401+
v.budget++ // undo default cost
402+
387403
case ir.ODCLCONST, ir.OFALL:
388404
// These nodes don't produce code; omit from inlining budget.
389405
return false

test/inline.go

+23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package foo
1111

1212
import (
13+
"math"
1314
"runtime"
1415
"unsafe"
1516
)
@@ -262,3 +263,25 @@ func gd2() int { // ERROR "can inline gd2"
262263
func gd3() func() { // ERROR "can inline gd3"
263264
return ii
264265
}
266+
267+
// Issue #42788 - ensure ODEREF OCONVNOP* OADDR is low cost.
268+
func EncodeQuad(d []uint32, x [6]float32) { // ERROR "can inline EncodeQuad" "d does not escape"
269+
_ = d[:6]
270+
d[0] = math.Float32bits(x[0]) // ERROR "inlining call to math.Float32bits"
271+
d[1] = math.Float32bits(x[1]) // ERROR "inlining call to math.Float32bits"
272+
d[2] = math.Float32bits(x[2]) // ERROR "inlining call to math.Float32bits"
273+
d[3] = math.Float32bits(x[3]) // ERROR "inlining call to math.Float32bits"
274+
d[4] = math.Float32bits(x[4]) // ERROR "inlining call to math.Float32bits"
275+
d[5] = math.Float32bits(x[5]) // ERROR "inlining call to math.Float32bits"
276+
}
277+
278+
// Ensure OCONVNOP is zero cost.
279+
func Conv(v uint64) uint64 { // ERROR "can inline Conv"
280+
return conv2(conv2(conv2(v))) // ERROR "inlining call to (conv1|conv2)"
281+
}
282+
func conv2(v uint64) uint64 { // ERROR "can inline conv2"
283+
return conv1(conv1(conv1(conv1(v)))) // ERROR "inlining call to conv1"
284+
}
285+
func conv1(v uint64) uint64 { // ERROR "can inline conv1"
286+
return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v)))))))))))
287+
}

0 commit comments

Comments
 (0)