Skip to content

cmake: optional wrapper units built from function-visibility.txt - #399

Merged
TheRedDaemon merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/function-visibility-wrappers
Nov 21, 2026
Merged

TheRedDaemon merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/function-visibility-wrappers

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Adds an optional way to build the units that cmake/function-visibility.txt describes, instead of relying on /GL. It builds on #398, the manifest, so its diff holds #398's commits too: please review #398 first. Off by default: without the option, nothing changes.

cmakew --preset RelWithDebInfo -DOPENSHC_FUNCTION_VISIBILITY_UNITS=ON

What it does

At configure time, cmake/function-visibility-units.py reads the manifest and groups every caller with the callees whose bodies it must see. A function can be defined only once, so everything joined by entries ends up in one group. For each group it writes one wrapper .cpp into the build folder:

// Generated from cmake/function-visibility.txt by cmake/function-visibility-units.py. Do not edit.
#include "C:/.../src/OpenSHC/Map/Units/UnitsState/checkIfCitizenUnitIsAliveBasedOnState.cpp"
#include "C:/.../src/OpenSHC/Map/Units/UpdateCamel.cpp"
...

CMakeLists.txt then builds the wrappers in place of their members. The members come off the /GL list, and the wrappers themselves are built without /GL. The source tree is not touched.

  • No .cpp is included twice: groups are formed over files, not functions, so a file that defines several listed functions lands in exactly one wrapper.
  • Name clashes: two members defining the same internal-linkage name (a file-scope static, or anything in an anonymous namespace) would not compile in one unit. Such a group is not wrapped: its files build as before, and the clash is printed at configure time. The check reads the source line by line rather than parsing it, so a brace inside a string or a comment could hide a static from it. A clash it misses fails loudly (C2084). Today src/OpenSHC has one file-scope static and no anonymous namespaces.
  • Missing names: manifest names that no .cpp in the build defines are skipped, and configure prints how many and which. On main today that is the 22 names cmake: function-visibility.txt, which callers must see which callees' bodies #398 lists.
  • Python: the option needs Python at configure time (find_package(Python3)). Under a preset that replaces PATH, CMake may not find it; pass -DPython3_EXECUTABLE=<path to python.exe> then.

Does it reproduce /GL?

Measured on our Map::Units branch (reimpl/units-B-development, which contains main 677469cd2, so main's whole /GL list is in it). One full build used /GL as today, one used this option with the manifest from #398. Both had every resolver switched on, and all files compiled. reccmp:

functions identical better worse 100% with /GL still 100%
in a wrapper 128 123 5 0 108 108
everything else 2,126 2,124 2 0 1,999 1,999
all 2,254 2,247 7 0 2,107 2,107

The seven that got better:

function /GL wrappers
Map::Units::UpdateDairyFarmer 93.9 100.0
Map::Units::UpdateSlave 81.8 88.8
Map::Units::UpdateMerchantHorse 51.5 72.5
Map::Units::UpdateMerchant 70.1 76.7
Map::Units::UpdateAppleFarmer 85.1 87.7
UI::Menu::Constructor_Menu 98.4 100.0
UI::Menu::handleMenuItems 97.9 100.0

UpdateDairyFarmer is the case /GL can't express: it must not see getResourceCountThatCanBeDeposited, but addResourceToGranary must, and under /GL every listed file sees every other. Every function that is 100% under /GL stays 100%, including UpdateCamel, UpdateLady, UpdateFireEater, UpdateInnkeeper and all the main /GL callers. UpdateHorseArcher_DoShooting and UpdateSiegeTent need no unit-mates, so they are not in a wrapper, and they stay 100% as well.

On main 3458fea34 it was built and measured too (update of 2026-11-21 below): 24 wrappers holding 92 files, and none of the 150 functions that can differ reads a different raw number than under /GL.

Things worth a look

  • One big wrapper. With every manifest name present, one wrapper holds 40 files: 37 from Map::Units, 2 from Map::Buildings and ViewportRenderState::xyAreValid. Almost every Map::Units Update function needs checkIfCitizenUnitIsAliveBasedOnState or hasUnitReachedDestination, which chains them all together. The two far edges still listed below pull in the rest. On main alone the largest wrapper is 18 files (the building-menu render functions).
  • Far edges. Three measured edges spanned most of the image. By the unit boundaries in tools/compiler-internals/gl-conundrum.md (section 4), each of these pairs has a boundary between them, so they can't share a file.
    • UpdateInnkeeper (0x540EC0) → BuildingsState::getBuildingResourceAmountByUid (0x409330): kept. Without it, Innkeeper drops from 100 to 97.3. It may be a body in a header (see "A third kind of visibility").
    • UpdateMerchant (0x5416B0) → GameStateStructures::activateTraderState (0x4586D0): removed. It was measured on a draft, and the 100% version on main must not see that body (update of 2026-11-21).
    • UpdatePikeman and UpdateMaceman (0x55F110, 0x560700) → ViewportRenderState::xyAreValid (0x401000, the image's first function): still listed, and skipped by the generator until the two callers land. To be measured again then.
  • /GL files outside every group. With the option on, 45 files on the /GL list are in no group and keep /GL, while the files they were in one /GL module with leave it. Four of the 45 don't exist on main (stale lines in compiler-flags-gl.txt, listed in cmake: function-visibility.txt, which callers must see which callees' bodies #398). The rest had no measured edge: nothing they call changed their score. None of them got worse in the build above. The rule: once a callee is in a wrapper, every caller that must see it needs an entry. On main with this manifest no such caller is left out: nothing outside the wrappers reads lower (update of 2026-11-21). It did happen once, with a hand-made manifest on our branch (update of 2026-11-20).
  • Generated paths are absolute. The wrappers live in the build folder and #include the members by absolute path, so moving the source tree needs a reconfigure, which happens anyway.

Update (2026-11-20): measured again, with #378's functions and a fuller manifest

A second measurement, on a different tree and with more files in the wrappers. With the option on, 327 of the 363 functions in the build read 100%, against 323 under /GL, and no function that reads 100% under /GL reads lower. Three unit updates reach 100% that /GL can only get there by breaking six functions on main.

What was measured

Result

12 of the 363 functions read differently between the two builds:

Function /GL Wrappers
BuildingsState::canUnitReachBuildingPerimeter 60.1 100.0
Units::UpdateBaker 93.2 100.0
Units::UpdateHunter 96.3 100.0
Units::UpdateDairyFarmer 93.9 100.0
Units::UpdateWoodcutter 95.5 99.2
Units::UpdateBlacksmith 92.6 98.9
Units::UpdateBrewer 89.8 95.2
Units::UpdateSlave 81.8 88.8
Units::UpdateAppleFarmer 85.1 87.7
WindowAndDirectDraw::takeScreenshot (unfinished) 44.5 44.8
Units::UpdateCatapult 86.8 86.5
UI::Rendering::InitBlendFilterArraysUnk 97.9 97.9 (normalised 97.5 → 95.0)
  • 327 functions read 100.0 with the wrappers, 323 under /GL.
  • No function that reads 100.0 under /GL reads lower with the wrappers.
  • UpdateCatapult is the only function that reads lower, by 0.3. It is not a missing entry: all 14 /GL files in its call closure are in its wrapper, and a /GL build of only Catapult and those 14 files reads the same 86.5. The 86.8 shows up only when the whole list is one /GL module, so it comes from something outside its own calls.
  • InitBlendFilterArraysUnk keeps its raw number; only the normalised one differs. It is a callee in two entries the manifest already had.

The manifest that was used

It was made by rule, not from measured edges: every Map::Units source on the /GL list gets an entry with each /GL callee it calls, except callees under Map/Buildings. The manifest of this PR is kept as it is. That gives 127 entries and 516 lines, where this PR has 209 lines. It is an upper bound: measured edges would be fewer.

The generator needed no change. It wrote 28 wrappers holding 186 of the sources. The largest has 94 files: 35 unit updates, 43 UnitsState functions, 2 files from Map::Buildings and 9 others. It compiled and linked first try.

These lines are not added to this PR. They name functions that only #378 has, and unit updates that aren't on main yet. They can follow once those are in.

A first, hand-measured manifest was tried before the rule: 26 lines, with entries for UpdateBaker and UpdateHunter and the callees they must see. It already put the three units at 100.0, but three other functions fell from 100.0: UnitsState::deleteUnit (98.7), UpdateOx (86.8) and UpdateGhost (49.3). Each calls a function that had moved into a wrapper while the caller stayed outside. So the caveat under "/GL files outside every group" is real: once a callee is wrapped, every caller that must see it needs an entry. With entries for them, all three read 100.0 again.

What it means

  • Per-caller visibility does something /GL can't. The unit updates must not see four BuildingsState bodies that other callers need: updateBuildingSignpostCounter, getResourceCountThatCanBeDeposited, getStorageBuildingForResourceTypeAndAmount and addResourceToStockpile. Under /GL every listed file sees every other, so UpdateBaker, UpdateHunter and UpdateDairyFarmer read 93.2, 96.3 and 93.9. Taking the four off the /GL list puts those three at 100.0, but six functions on main fall with it: addResourceToGranary 100 → 76.3, getStorageBuildingForResourceTypeAndAmount 100 → 58.8, getResourceSpace 100 → 88.9, addResourceToStockpile 100 → 97.9, UpdateBadBuildingDungeon 100 → 96.4, UpdateBadBuildingGallows 100 → 94.1. With the wrappers the three units and the six main functions all read 100.0: the four bodies sit in three small wrappers with the callers that need them, and the units' wrapper doesn't hold them.
  • The same in the other direction. canUnitReachBuildingPerimeter (on main) reads 60.1 in the /GL build of this merge, because Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 puts UnitsState/selectionContainsOnlyArabAssassins.cpp on the /GL list and the caller then sees a body the original didn't see. A second /GL build without that one line reads 100.0, and it is the only one of the 363 functions that differs between those two builds. In its own wrapper the function doesn't see that body either, and reads 100.0.
  • The build was faster. The two wrapper builds took 39 and 16 minutes, the /GL builds of the same sources between 43 and 86. Other builds ran on the same PC at times, so take it as an indication, not a benchmark.

Still open

  • A third kind of visibility. UpdateWoodcutter and UpdateBlacksmith stop one instruction short, at 99.2 and 98.9, under /GL and with the wrappers alike. Behind BuildingsState::getBuildingResourceAmountByUid the original loads ecx again but doesn't read the unit slot again: it knows what the callee writes, but has no register record for it. Neither /GL nor a wrapper can state that. In a local test with that one definition marked inline and included from its class header, both units read 100.0, and main's two callers (UpdateIronMine, UpdateInnkeeper) stay at 100.0. That touches a generated header, so it is a separate question; the write-up is ready.
  • Catapult's 0.3. What the whole-list /GL module gives UpdateCatapult beyond its own callees is not known. The function is at 86%, so nothing at 100% depends on it.
  • This run is not the whole tree. It built 350 sources. The whole-tree comparison is the one above, under "Does it reproduce /GL?".

Update (2026-11-21): built and measured on main

Builds of main, alone and with #378, with the option off and on. The build list is the /GL list plus every file the manifest names: every other file has the same compile command in both modes. reccmp, raw.

tree functions at 100.0 with /GL at 100.0 with the wrappers (manifest with #398's fix)
main 3458fea34 + this PR 150 146 146
main + #378 e81059741 + this PR 298 289 292

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 12, 2026 19:37
… bodies

One entry per caller, with the callees whose bodies it must see when it is
compiled, fully qualified as the definitions are written:

    OpenSHC::funcA
        OpenSHC::funcB

87 entries, 127 edges, sorted by the caller's address. Every edge is a
tu_merge.py measurement: pasting the callee into the caller's unit raises the
caller's quick_diff ratio, and for callers with several callees a leave-one-out
over the full set decided which are needed together. In all 127 the callee is
at a lower address than its caller.

Twelve measured gains are left out because they cannot be same-file edges:
in seven the callee is at a higher address than its caller, and the other
five would put a callee into a unit with callers that measure worse when they
see it.

23 names are not on main yet. 22 are Map::Units Update callers: they were
measured on our local branches reimpl/units-B-development and
reimpl/units-A-development (UpdateCamel is also on the local
reimpl/units-B-round2-100), and gynt's reimpl/map-units-updatefunctions has
drafts of all 22. The 23rd is UnitsState::changeDestinationByAmount from sourcehold#378.
A generator skips names it cannot find. The measurements are in the PR
description.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -DOPENSHC_FUNCTION_VISIBILITY_UNITS=ON, configure runs
cmake/function-visibility-units.py. It groups every caller of
cmake/function-visibility.txt with the callees whose bodies it must see (one
group per connected set, since a function is defined once) and writes one
wrapper .cpp per group that #includes the members. The wrappers replace their
members in the build, and the members leave the /GL list; the wrappers are
built without /GL. Off by default; nothing changes without the option.

Groups are formed over files, so a .cpp that defines several listed functions
is included once. A group whose members define the same internal-linkage name
(a file-scope static or anything in an anonymous namespace) is not wrapped,
and its files build as before. Manifest names with no .cpp in the build are
skipped and listed at configure time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kotijekriv and others added 3 commits November 14, 2026 00:14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tCounter's body

sourcehold#290 rewrote UpdateBadBuildingGibbet and UpdateBadBuildingStretchingRack so that they reach 100% without /GL. On
main (ea63a49), tu_merge.py gives both 1.0000 in all three layouts: the caller alone, the callee's body pasted
before it, and pasted after it (45 of 45 and 41 of 41 instructions). Seeing updateBuildingSignpostCounter no longer
helps either of them, so their two entries go. The callee keeps its other two callers, UpdateBadBuildingGallows and
UpdateBadBuildingDungeon.

The manifest now has 85 entries and 125 edges. On main's sources the wrapper generator from sourcehold#399 makes 24 units
holding 91 source files (93 before), with no internal-name clashes and the same 23 names not on main yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lity-wrappers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kotijekriv and others added 4 commits November 21, 2026 00:44
The entry for UpdateMerchant listed GameStateStructures::activateTraderState. That edge was measured on a 93.8%
draft of UpdateMerchant. The function has since landed on main at 100% (sourcehold#409), and it reaches that under /GL
without the body: activateTraderState.cpp is not on the /GL list. With the body in its unit the caller keeps the
unit slot in esi across the call, where the original reads it again, so the original's caller did not know it.

Measured on main 3458fea with sourcehold#399's wrapper build (-DOPENSHC_FUNCTION_VISIBILITY_UNITS=ON), reccmp, raw:
UpdateMerchant reads 100.0 under /GL, 90.0 with the wrappers before this change and 100.0 after it. It was the only
one of the 150 functions in that build (main's /GL list plus every file the manifest names) whose raw number
differed between /GL and the wrappers, and none differs now.

The manifest now has 85 entries and 124 edges. On main's sources the generator makes 24 units holding 92 source
files (93 before).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…lity-wrappers

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…lity-wrappers

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kotijekriv kotijekriv changed the title cmake: optional wrapper units built from function-visibility.txt (prototype) cmake: optional wrapper units built from function-visibility.txt Nov 21, 2026
@kotijekriv
kotijekriv marked this pull request as ready for review November 21, 2026 00:31
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 21, 2026
UpdateTrebuchet (0x00569410) calls GameSynchronyState::isAIPlayer twice, at 0x0056A05B and 0x0056A124. Behind each
call the original pushes ebx, the unit slot it read from 0xEE0FC8, without reading the slot again (0x0056A068,
0x0056A12D); behind the next call, to 0x00465F20, it reads it again (0x0056A06F, 0x0056A134). So the original's
caller knew that isAIPlayer writes no memory, which it can know only from the body.

Measured with tu_merge.py on main 3458fea (in a clone with two UI commits on top, nothing under Map or Synchrony)
plus worker C's UpdateTrebuchet.cpp (6757ed1, at 100% under /GL), every struct and function resolver on,
quick_diff ratio of the caller:

    caller alone                                          0.5440
    isAIPlayer, hasUnitReachedDestination, deselectUnit   0.9818
    hasUnitReachedDestination, deselectUnit               0.9132
    isAIPlayer alone                                      0.5587

(callees first and caller first read the same in each row). Leaving isAIPlayer out lowers the score, so it is needed
together with the other two. isAIPlayer (0x004010B0) is at a lower address than its caller, and it goes first in the
entry, whose callees are sorted by address.

The manifest now has 85 entries and 125 edges. On main's sources the generator from sourcehold#399 makes the same 24 units
holding 92 source files, file for file, since UpdateTrebuchet is not on main. With that UpdateTrebuchet.cpp in the
build, isAIPlayer.cpp joins its unit (24 units holding 95 source files, 94 without the line); no other file in that
unit calls isAIPlayer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheRedDaemon
TheRedDaemon added this pull request to the merge queue Nov 21, 2026
Merged via the queue into sourcehold:main with commit 02fab42 Nov 21, 2026
8 checks passed
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 21, 2026
The manifest itself is on main since sourcehold#399 (02fab42); this branch keeps
one more edge, UpdateTrebuchet -> GameSynchronyState::isAIPlayer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants