Skip to content

Linking: skip passes over the program linked so far that have nothing to do - #9602

Draft
tautschnig wants to merge 3 commits into
diffblue:developfrom
tautschnig:kiro/linking-skip-unneeded-passes
Draft

tautschnig wants to merge 3 commits into
diffblue:developfrom
tautschnig:kiro/linking-skip-unneeded-passes

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Each linking step makes three passes over the whole program linked so far: linkingt::link applies rename_main_symbol to the type and value of every symbol, linkingt::copy_symbols applies object_type_updates to every initializer, and link_functions applies the same renaming as linkingt::link to every instruction of every function. Linking N object files one at a time, as goto-cc does, thus takes time quadratic in N. rename_symbolt::rename(exprt &) visits every subexpression even if nothing is to be renamed.

rename_main_symbol is empty unless linking renames a symbol of the program linked so far, which happens only for a file-local symbol there and a symbol of the same name that is not file-local in the file being linked; object_type_updates is empty unless the file being linked changes the type of a symbol linked before or defines a function declared before. Skip each pass if its renaming or update is empty, as it then cannot change anything. rename_symbolt gets an empty() like replace_symbolt's.

Linking the goto programs of ghostscript 10.08's libgs.so (650 objects) and of binutils' libbfd (113 objects) on Debian/arm64, with the fixes they need to link at all (not on develop): with sampling, 84 % of the libgs.so link was in these three passes (36 % renaming the symbol table, 27 % the functions, 20 % the type updates of initializers). With this change, the goto programs are byte-identical, and the links take 50 s instead of 153 s (libgs.so) and 9.9 s instead of 15.3 s (libbfd). On develop, linking N generated object files that each define 20 small functions and a table of pointers to them (and refer to the previous file's functions) takes 2.0, 6.6 and 24.4 s for N = 200, 400 and 800, and 0.7, 1.6 and 3.5 s with this change, with identical goto binaries.

Two new regression tests make the passes change the program, and fail if a pass is skipped although its renaming or update is not empty. In linking-goto-binaries/renamed_existing_symbols, main.c, linked first, has file-local definitions that supp.c's external definitions of the same names make linking rename, and initializers that refer to them; the assertions about their values fail without the pass in linkingt::link or the one in link_functions. In
goto-instrument/linking-initializer-type-update, decl.c declares a function without a prototype and refers to it in an initializer, and main.c, linked second, defines it with one; without the pass in copy_symbols, the initializer keeps the type of the declaration.

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

… to do

Each linking step makes three passes over the whole program linked so
far: linkingt::link applies rename_main_symbol to the type and value of
every symbol, linkingt::copy_symbols applies object_type_updates to
every initializer, and link_functions applies the same renaming as
linkingt::link to every instruction of every function. Linking N object
files one at a time, as goto-cc does, thus takes time quadratic in N.
rename_symbolt::rename(exprt &) visits every subexpression even if
nothing is to be renamed.

rename_main_symbol is empty unless linking renames a symbol of the
program linked so far, which happens only for a file-local symbol there
and a symbol of the same name that is not file-local in the file being
linked; object_type_updates is empty unless the file being linked
changes the type of a symbol linked before or defines a function
declared before. Skip each pass if its renaming or update is empty, as
it then cannot change anything. rename_symbolt gets an empty() like
replace_symbolt's.

Linking the goto programs of ghostscript 10.08's libgs.so (650 objects)
and of binutils' libbfd (113 objects) on Debian/arm64, with the fixes
they need to link at all (not on develop): with sampling, 84 % of the
libgs.so link was in these three passes (36 % renaming the symbol
table, 27 % the functions, 20 % the type updates of initializers). With
this change, the goto programs are byte-identical, and the links take
50 s instead of 153 s (libgs.so) and 9.9 s instead of 15.3 s (libbfd).
On develop, linking N generated object files that each define 20 small
functions and a table of pointers to them (and refer to the previous
file's functions) takes 2.0, 6.6 and 24.4 s for N = 200, 400 and 800,
and 0.7, 1.6 and 3.5 s with this change, with identical goto binaries.

Two new regression tests make the passes change the program, and fail
if a pass is skipped although its renaming or update is not empty. In
linking-goto-binaries/renamed_existing_symbols, main.c, linked first,
has file-local definitions that supp.c's external definitions of the
same names make linking rename, and initializers that refer to them; the
assertions about their values fail without the pass in linkingt::link
or the one in link_functions. In
goto-instrument/linking-initializer-type-update, decl.c declares a
function without a prototype and refers to it in an initializer, and
main.c, linked second, defines it with one; without the pass in
copy_symbols, the initializer keeps the type of the declaration.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.21569% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.35%. Comparing base (cf12e35) to head (b2663db).
⚠️ Report is 51 commits behind head on develop.

Files with missing lines Patch % Lines
src/linking/initializer_references.cpp 81.39% 8 Missing ⚠️
src/linking/linking.cpp 92.30% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9602      +/-   ##
===========================================
+ Coverage    81.28%   81.35%   +0.06%     
===========================================
  Files         1725     1729       +4     
  Lines       191618   191796     +178     
  Branches        73       73              
===========================================
+ Hits        155763   156028     +265     
+ Misses       35855    35768      -87     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

tautschnig and others added 2 commits December 1, 2026 14:37
Each linking step that makes type changes (object_type_updates: a
definition that replaces a declaration of another type, or an object
whose type changes) applies them to the values of all symbols of the
program linked so far (linkingt::copy_symbols); the previous commit
skips this pass only when there are no such changes. Linking N goto
binaries one at a time, as read_objects_and_link does for goto-cc and
cbmc, therefore still takes time quadratic in N whenever most steps
have updates, which is common: any definition of a function that a file
linked before declares is one.

Keep an index, initializer_referencest, of the symbols whose values refer
to each symbol. It is built at the first linking step that has a type
update, and each later step records the symbols it adds, merges with
existing ones or renames. The step then applies the updates only to the
values of the symbols that the index lists for the updated symbols. The
index may list more symbols than refer to a symbol, but never fewer, so
the resulting values are unchanged. read_objects_and_link passes one index
through link_goto_model for all its linking steps; other callers of
linkingt and link_goto_model keep the pass over all symbols, which is
still skipped when there are no updates.

Measurements (CMake Release; the linked goto binaries are byte-identical
with and without this change):

- Linking N generated files that each define 20 functions and declare
  the previous file's (as in the previous commit), in reverse order, so
  that each file declares functions a file linked later defines and
  every step has updates: 2.64 s and 6.36 s for N = 400 and 800 with the
  previous commit, 1.75 s and 3.38 s with this change (median of 3). In
  forward order, where only one step has updates, the times do not
  change (3.51 s and 3.57 s for N = 800).
- The goto-ld link of the Linux kernel's vmlinux.o (v7.4-rc4, arm64
  defconfig: 4320 goto binaries, 2.37 million symbols), on a branch with
  the previous commit and further goto-cc fixes, and with the index also
  passed by that branch's linking of archive members: the pass took
  51 % of the link (perf), and the link takes 449 s instead of 1008 s.
  In the link of fs/built-in.a (298 goto binaries), 155 of the 298 steps
  had updates; they visited 4.68 million initializers and changed 567 of
  them.

regression/linking-goto-binaries/chain.sh now links any number of files,
in the order given, and passes options to cbmc. The new test
initializer_type_updates links four files, the last three of which each
define a function that the files linked before declare without prototype
and use in an initializer; it fails if the index misses an initializer
added by an earlier linking step.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Before each linking step, link_goto_model collected the weak symbols of
the goto model linked so far by visiting all of its symbols, which makes
linking N goto models one at a time, as read_objects_and_link does,
take time quadratic in N. link_functions uses the set only to decide
whether a function being linked replaces a function of the same name
that is weakly defined: names of functions being linked are the only
names it looks up, as functions that linking renames get names the goto
model linked so far does not have. Collect the weak symbols among the
names of the functions being linked instead, still before linking, which
may change whether a symbol is weak.

Measurements (CMake Release; the linked goto binaries are byte-identical
with and without this change):

- The goto-ld link of the Linux kernel's vmlinux.o (v7.4-rc4, arm64
  defconfig: 4320 goto binaries, 2.37 million symbols), on a branch with
  the two previous commits and further goto-cc fixes: the loop took 7 %
  of the link before those fixes (perf). With this change, the link
  takes 319 s instead of 401 s (both runs at the same time on a shared
  host).
- Linking the N = 800 generated files of the two previous commits takes
  3.19 s instead of 3.45 s in reverse order and 3.20 s instead of 3.44 s
  in forward order (median of 5, runs alternating between the two
  builds).

The new test linking-goto-binaries/weak_function_definition links a goto
binary with a weak definition and then one with a strong definition of
the same function, which must replace it. It needs GCC's weak attribute,
so the test is tagged gcc-only, which the linking-goto-binaries suite now
excludes under Visual Studio as other suites do.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant