Refine LTO reflect MethodByName handling#2029
Open
zhouguangyuan0718 wants to merge 9 commits into
Open
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
0d2d81b to
f2ad958
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
reflect.Value.MethodByName/reflect.Type.MethodByNamereachability after LLVM optimization.MethodByNamecall sites in LLGo IR with a reflect-kind attribute and mark the actual method-name argument separately, then preserve and remap those attributes through C ABI lowering.llvm.type.checked.loadchecks when optimized IR proves a finite string-name set, so GlobalDCE can remove methods that cannot be reached by those names.Implementation Details
The Go side emits two custom attributes only when the optional LTO plugin path is enabled:
llgo.reflect.methodbynameon the call site, withvalueortypeto distinguishreflect.Value.MethodByNamefromreflect.Type.MethodByName.llgo.reflect.methodbyname.nameon the method-name parameter, so later lowering does not need to infer ABI-dependent argument positions.internal/cabinow carries the call-site marker forward and remaps the name-parameter marker onto the lowered string data argument. This keeps the plugin independent of the pre-lowering Go ABI shape while still letting it find the string value precisely after C ABI lowering.The plugin is built from
ltoplugin/with CMake against LLVM 19 and registers atFullLinkTimeOptimizationEarlyEP. It scans marked reflect calls, derives finite string sets from the optimized IR, traces the call'ssretresult users to find the corresponding generic reflect checked-loads, adds name-specific checked-loads for each proven method name, and erases the generic dynamic check when the replacement is complete.The reflect modeling still distinguishes Value and Type method metadata, so a
Value.MethodByNameuse does not keep the broaderType.MethodByNameset alive, and vice versa.Tests and CI
MethodByNameflows.Validation
cmake --build /tmp/llgo-ltoplugin-buildLLGO_LTO_PLUGIN=/tmp/llgo-ltoplugin-build/LLGOLTOPlugin.so go test -tags dev ./cl -run '^(TestRunAndTestFromTestltoLTOPlugin|TestBuildAndCheckSymbolsFromTestltoLTOPlugin)' -count=1go test -tags dev ./cl -run '^TestBuildAndCheckSymbolsFromTestlto/globaldce_' -count=1LLGO_LTO_PLUGIN=/tmp/llgo-ltoplugin-build/LLGOLTOPlugin.so go test -tags=dev -timeout 30m -covermode=atomic -coverpkg=github.com/goplus/llgo/ssa,github.com/goplus/llgo/internal/build,github.com/goplus/llgo/internal/crosscompile -coverprofile=/tmp/coverage-dev-lto-plugin-cl.txt -run '^(TestRunAndTestFromTestltoLTOPlugin|TestBuildAndCheckSymbolsFromTestltoLTOPlugin)' ./clgo test -tags dev ./ssa ./internal/cabi -count=1go test -tags dev -cover ./ssa ./internal/cabi -count=1github.com/goplus/llgo/ssa: 87.5%github.com/goplus/llgo/internal/cabi: 82.2%