carbon-lang icon indicating copy to clipboard operation
carbon-lang copied to clipboard

Consider tracking the list of symbols discarded when linking over time

Open zygoloid opened this issue 9 months ago • 2 comments

It would be useful to identify dead code in the toolchain and clean it up. In discussion with @chandlerc and others, I found the following recipe seems to do a reasonable job of identifying symbols discarded at link time:

$ bazel build --copt=-ffunction-sections --copt=-fdata-sections --linkopt=-O2 --linkopt=-Wl,--gc-sections -c dbg //toolchain/install:carbon-busybox
$ grep '^bazel.*\.o$' bazel-bin/toolchain/install/carbon-busybox-0.params | xargs nm -Ugj | sort -u > /tmp/defined.syms
$ echo bazel-bin/toolchain/install/carbon-busybox | xargs nm -Ugj | sort -u > /tmp/retained.syms
$ diff -u /tmp/defined.syms /tmp/retained.syms | grep '^-_ZN6Carbon'

This currently lists a few hundred symbols. It'd be interesting to investigate running something like a cleaned up version of this over time to check for regressions. (Unfortunately it's fairly slow, and needs an unoptimized build with different compilation and linker flags than anything we usually do, so may not work well as a pre-merge action. Maybe we could find a way to do a nightly run or similar?)

zygoloid avatar Feb 12 '25 01:02 zygoloid