Skip to content

Commit

Permalink
cmd/compile: function "property" defs for inl heuristics
Browse files Browse the repository at this point in the history
Add definitions for a set of Go function "properties" intended to be
useful for driving inlining decisions. This CL just defines a set of
flags and a container to hold them; a subsequent CL will add code to
compute the properties for a function given its IR/AST representation.

Updates #61502.

Change-Id: Ifa26c1ad055c02ca0ce9cf37078cee7b3385e18a
Reviewed-on: https://go-review.googlesource.com/c/go/+/511556
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
Run-TryBot: Than McIntosh <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
  • Loading branch information
thanm committed Aug 10, 2023
1 parent 7c2b690 commit 03d457a
Show file tree
Hide file tree
Showing 7 changed files with 483 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/cmd/compile/internal/inline/inlheur/funcprop_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 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 inlheur

import (
"fmt"
"strings"
)

func (fp *FuncProps) String() string {
return fp.ToString("")
}

func (fp *FuncProps) ToString(prefix string) string {
var sb strings.Builder
if fp.Flags != 0 {
fmt.Fprintf(&sb, "%sFlags %s\n", prefix, fp.Flags)
}
flagSliceToSB[ParamPropBits](&sb, fp.ParamFlags,
prefix, "ParamFlags")
flagSliceToSB[ResultPropBits](&sb, fp.ResultFlags,
prefix, "ResultFlags")
return sb.String()
}

func flagSliceToSB[T interface {
~uint32
String() string
}](sb *strings.Builder, sl []T, prefix string, tag string) {
var sb2 strings.Builder
foundnz := false
fmt.Fprintf(&sb2, "%s%s\n", prefix, tag)
for i, e := range sl {
if e != 0 {
foundnz = true
}
fmt.Fprintf(&sb2, "%s %d %s\n", prefix, i, e.String())
}
if foundnz {
sb.WriteString(sb2.String())
}
}
58 changes: 58 additions & 0 deletions src/cmd/compile/internal/inline/inlheur/funcpropbits_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions src/cmd/compile/internal/inline/inlheur/function_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2023 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 inlheur

// This file defines a set of Go function "properties" intended to
// guide inlining heuristics; these properties may apply to the
// function as a whole, or to one or more function return values or
// parameters.
//
// IMPORTANT: function properties are produced on a "best effort"
// basis, meaning that the code that computes them doesn't verify that
// the properties are guaranteed to be true in 100% of cases. For this
// reason, properties should only be used to drive always-safe
// optimization decisions (e.g. "should I inline this call", or
// "should I unroll this loop") as opposed to potentially unsafe IR
// alterations that could change program semantics (e.g. "can I delete
// this variable" or "can I move this statement to a new location").
//
//----------------------------------------------------------------

// FuncProps describes a set of function or method properties that may
// be useful for inlining heuristics. Here 'Flags' are properties that
// we think apply to the entire function; 'RecvrParamFlags' are
// properties of specific function params (or the receiver), and
// 'ResultFlags' are things properties we think will apply to values
// of specific results. Note that 'ParamFlags' includes and entry for
// the receiver if applicable, and does include etries for blank
// params; for a function such as "func foo(_ int, b byte, _ float32)"
// the length of ParamFlags will be 3.
type FuncProps struct {
Flags FuncPropBits
ParamFlags []ParamPropBits // slot 0 receiver if applicable
ResultFlags []ResultPropBits
}

type FuncPropBits uint32

const (
// Function always panics or invokes os.Exit() or a func that does
// likewise.
FuncPropNeverReturns FuncPropBits = 1 << iota
)

type ParamPropBits uint32

const (
// No info about this param
ParamNoInfo ParamPropBits = 0

// Parameter value feeds unmodified into a top-level interface
// call (this assumes the parameter is of interface type).
ParamFeedsInterfaceMethodCall ParamPropBits = 1 << iota

// Parameter value feeds unmodified into an interface call that
// may be conditional/nested and not always executed (this assumes
// the parameter is of interface type).
ParamMayFeedInterfaceMethodCall ParamPropBits = 1 << iota

// Parameter value feeds unmodified into a top level indirect
// function call (assumes parameter is of function type).
ParamFeedsIndirectCall

// Parameter value feeds unmodified into an indirect function call
// that is conditional/nested (not guaranteed to execute). Assumes
// parameter is of function type.
ParamMayFeedIndirectCall

// Parameter value feeds unmodified into a top level "switch"
// statement or "if" statement simple expressions (see more on
// "simple" expression classification below).
ParamFeedsIfOrSwitch

// Parameter value feeds unmodified into a "switch" or "if"
// statement simple expressions (see more on "simple" expression
// classification below), where the if/switch is
// conditional/nested.
ParamMayFeedIfOrSwitch
)

type ResultPropBits uint32

const (
// No info about this result
ResultNoInfo ResultPropBits = 0
// This result always contains allocated memory.
ResultIsAllocatedMem ResultPropBits = 1 << iota
// This result is always a single concrete type that is
// implicitly converted to interface.
ResultIsConcreteTypeConvertedToInterface
// Result is always the same non-composite compile time constant.
ResultAlwaysSameConstant
// Result is always the same function or closure.
ResultAlwaysSameFunc
// Result is always the same (potentially) inlinable function or closure.
ResultAlwaysSameInlinableFunc
)
70 changes: 70 additions & 0 deletions src/cmd/compile/internal/inline/inlheur/parampropbits_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/cmd/compile/internal/inline/inlheur/resultpropbits_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 03d457a

Please sign in to comment.