[WIP] ssa: check abiInit flag for abiInitList/abiPrune#1635
[WIP] ssa: check abiInit flag for abiInitList/abiPrune#1635visualfc wants to merge 5 commits intoxgo-dev:mainfrom
Conversation
Summary of ChangesHello @visualfc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the ABI (Application Binary Interface) generation and reflection metadata handling within the SSA compiler. The primary goal is to optimize the generated code by introducing a sophisticated pruning mechanism that only includes necessary reflection information. This is achieved through granular control flags for ABI initialization and by analyzing actual usage of reflection functions and method invocations, leading to potentially smaller binaries and improved runtime efficiency. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant optimization by adding ABI type pruning, which should help reduce final binary sizes. The mechanism tracks reflection usage and method invocations to determine which ABI information can be safely removed. The refactoring to centralize resolveLinkname and to use a genConfig struct for genMainModule are positive changes for code clarity. I have a couple of suggestions to improve the performance and readability of the new pruning logic.
| for v := range invoked { | ||
| if v.Name() == name { | ||
| if !types.Identical(prog.Patch(v.Type().(*types.Signature).Recv().Type()), method.Type().(*types.Signature).Recv().Type()) { | ||
| continue | ||
| } | ||
| if !types.Identical(prog.Patch(v.Type()), method.Type()) { | ||
| continue | ||
| } | ||
| return true | ||
| } | ||
| } |
There was a problem hiding this comment.
This loop iterates over all invoked functions for every method being checked for pruning. This can be inefficient if the number of invoked functions and methods is large, leading to O(M*N) complexity where M is the number of methods and N is the number of invoked functions.
To optimize this, you can pre-process the invoked map into a map[string][]*ssa.Function before calling PruneAbiTypes. This allows for a much faster lookup by method name inside this callback.
Example of pre-processing (to be added after line 102):
invokedByName := make(map[string][]*ssa.Function)
for f := range invoked {
invokedByName[f.Name()] = append(invokedByName[f.Name()], f)
}Then, you can use this map for an efficient lookup as suggested below.
if funcs, ok := invokedByName[name]; ok {
for _, v := range funcs {
if !types.Identical(prog.Patch(v.Type().(*types.Signature).Recv().Type()), method.Type().(*types.Signature).Recv().Type()) {
continue
}
if !types.Identical(prog.Patch(v.Type()), method.Type()) {
continue
}
return true
}
}| @@ -322,6 +341,17 @@ func (p Program) Linkname(name string) (link string, ok bool) { | |||
| return | |||
There was a problem hiding this comment.
[Documentation/Security] ResolveLinkname was moved from context to Program and is now public. The panic behavior on invalid links (non-C. prefix) is now reachable from more call sites. Please add a doc comment noting:
- This method panics if the link prefix is not
C - What callers should expect for valid linknames
| const ( | ||
| ReflectArrayOf = 1 << iota | ||
| ReflectChanOf | ||
| ReflectFuncOf | ||
| ReflectMapOf | ||
| ReflectPointerTo | ||
| ReflectSliceOf | ||
| ReflectStructOf | ||
| ReflectMethodByIndex | ||
| ReflectMethodByName | ||
| ReflectMethodDynamic | ||
| ) |
There was a problem hiding this comment.
[Documentation] The Reflect* constants are the core of the new NeedAbiInit bitmask system. Please add a doc comment block explaining:
- These are bitmask flags meant to be combined with bitwise OR
- Each corresponds to a specific
reflectpackage function - The distinction between
ReflectMethodByIndex,ReflectMethodByName, andReflectMethodDynamic
| return initFn | ||
| } | ||
|
|
||
| func (p Package) PruneAbiTypes(needAbiInit int, methodIsInvoke func(index int, method *types.Selection) bool) { |
There was a problem hiding this comment.
[Documentation] PruneAbiTypes is the central new public method and has no doc comment. Please document:
- How
needAbiInitrelates to theReflect*bitmask constants - The semantics of
methodIsInvokecallback (whatindexrefers to, what returningtruemeans) - That passing
nilformethodIsInvokeprovides a default implementation
| for v := range invoked { | ||
| if v.Name() == name { | ||
| if !types.Identical(prog.Patch(v.Type().(*types.Signature).Recv().Type()), method.Type().(*types.Signature).Recv().Type()) { | ||
| continue | ||
| } | ||
| if !types.Identical(prog.Patch(v.Type()), method.Type()) { | ||
| continue | ||
| } | ||
| return true | ||
| } | ||
| } |
There was a problem hiding this comment.
[Performance] The inner loop for v := range invoked iterates over all invoked functions for every method being checked, resulting in O(S × M × I) complexity where S = uncommon ABI symbols, M = methods per type, I = invoked functions.
Consider building a name-indexed lookup (e.g., map[string][]*ssa.Function) during buildInvokeIndex to make name-based lookup O(1) amortized:
// In buildInvokeIndex, also build:
invokedByName := make(map[string][]*ssa.Function)
for fn := range invoked {
invokedByName[fn.Name()] = append(invokedByName[fn.Name()], fn)
}| } | ||
| for v := range invoked { | ||
| if v.Name() == name { | ||
| if !types.Identical(prog.Patch(v.Type().(*types.Signature).Recv().Type()), method.Type().(*types.Signature).Recv().Type()) { |
There was a problem hiding this comment.
[Security] The chained type assertions and dereferences v.Type().(*types.Signature).Recv().Type() could panic if:
- The type is not a
*types.Signature Recv()returns nil (non-method function)
While CHA should only populate invoked with interface dispatch targets, consider adding defensive guards:
sig, ok := v.Type().(*types.Signature)
if !ok || sig.Recv() == nil {
continue
}| var ( | ||
| reflectFunc = map[string]struct{}{ | ||
| "reflect.ArrayOf": {}, | ||
| "reflect.ChanOf": {}, | ||
| "reflect.FuncOf": {}, | ||
| "reflect.MapOf": {}, | ||
| "reflect.SliceOf": {}, | ||
| "reflect.StructOf": {}, | ||
| "reflect.PointerTo": {}, | ||
| "reflect.PtrTo": {}, | ||
| "reflect.SliceOf": {}, | ||
| "reflect.StructOf": {}, | ||
| "reflect.Value.Method": {}, | ||
| "reflect.Value.MethodByName": {}, | ||
| } |
There was a problem hiding this comment.
[Code Quality] The reflectFunc map is now dead code since checkReflect() uses a switch statement instead. Consider removing it to avoid confusion and linter warnings.
| m = make(map[types.Type]none) | ||
| p.invokeMethods[name] = m | ||
| } | ||
| m[p.Patch(fn.Type())] = none{} | ||
| } | ||
|
|
||
| // A Program presents a program. | ||
| type Program = *aProgram |
There was a problem hiding this comment.
[Documentation] Program.AddInvoke is a new public method critical for the pruning algorithm. Please add a doc comment explaining:
- What "invoke" means (interface dispatch vs direct call)
- When callers should use this method
- The relationship between
AddInvokeandPruneAbiTypes
| prog.abiTypePruning = true | ||
| defer func() { | ||
| prog.abiTypePruning = false | ||
| }() | ||
| prog.methodIsInvoke = methodIsInvoke |
There was a problem hiding this comment.
[Code Quality] Storing transient state (abiTypePruning, methodIsInvoke) on the long-lived Program object is fragile. If PruneAbiTypes were called reentrantly or concurrently, these flags could be incorrectly reset. Consider passing the pruning predicate through the call chain instead of storing on the program object.
Code Review SummaryThis PR introduces ABI type pruning via call graph analysis - a valuable optimization to reduce binary size by eliminating unused method function pointers. The implementation approach using CHA and tracking reflect usage at compile-time is architecturally sound. Key findings:
See inline comments for specific suggestions. |
fe18da7 to
a62c57a
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1635 +/- ##
==========================================
- Coverage 91.35% 91.09% -0.26%
==========================================
Files 47 47
Lines 12681 12806 +125
==========================================
+ Hits 11585 11666 +81
- Misses 906 948 +42
- Partials 190 192 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
7ba67aa to
a90fe5d
Compare
a90fe5d to
6eb6051
Compare
based of #1590