-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/compile: function "property" defs for inl heuristics
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
Showing
7 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/cmd/compile/internal/inline/inlheur/funcprop_string.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
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.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
src/cmd/compile/internal/inline/inlheur/function_properties.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
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.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
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.
Oops, something went wrong.
Oops, something went wrong.