Skip to content

Reimplement 16 TileMapState functions that need volatile reads or a __forceinline resolver get() - #519

Merged
gynt merged 5 commits into
reimpl/TileMapState-100-2from
reimpl/TileMapState-100-3
Dec 1, 2026
Merged

gynt merged 5 commits into
reimpl/TileMapState-100-2from
reimpl/TileMapState-100-3

Conversation

@gynt

@gynt gynt commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Split out of #396 at review: the Map::TileMapState functions that need something this project has not needed before, so they can be looked at separately. Builds on #396, which has to be merged first; until then this PR is diffed against reimpl/TileMapState-100-2.

What is in it

16 functions in two groups.

Five compiled without global optimisation, which only match when FunctionResolver's get() is __forceinline: setBitFlagBasedOnWallTowerGatehouseOrKeep, setTerrain, updateGFXLayers, updateMacroLayerRelated and updateGfxLayer.

  • They share the hand-written neighbour-flag asm in NeighbourFlagsGlobalAsm.hpp, which is part of this PR.
  • No #pragma optimize. The original macros jump with jz $+9 / jz $+12, relative to the location counter, since a macro used several times in one function cannot repeat a label. MSVC encodes such a jump as the near 0F 84 rel32 (a jump to a label is always the 2-byte short form, even with near ptr) and counts it as control flow leaving the __asm block. That raises C4740, a level-4 warning that is silent at /W3, and turns global optimisation off for the whole enclosing function.
  • That is the unoptimised frame of every original function containing these macros. Functions with only the jump-free MACRO_MARK_CHANGED_NEIGHBOURS_GLOBAL (createPlateau, useLevelBrush, useTerrainHeightBrush) are optimised, and so are PathFindingState's macros, which jump to labels.
  • MACRO_ASM_JZ_NEAR writes that jump (it used to _emit the bytes), so the four pragmas are gone. All five functions compile to the same bytes as before.

Six callers of getTileForBrush built in one unit with it: createPlateau, isValidCastleSiteLocation, setLand, useEraserBrush, useLevelBrush and useTerrainHeightBrush, listed in cmake/function-visibility.txt.

  • The original reads the tile and y out-parameters once after the call and keeps them in registers across later stores and calls. A plain int const brushTile = tile; compiles that way only when getTileForBrush's body is in the same translation unit, as it was in the original (0x004FBD80 sits among its callers).
  • With the callee out of sight, c2 propagates the copy back to the parameter. Seven plain spellings tried on setLand all give the same wrong function, which is what the earlier *(int volatile*)&tile reads worked around. Those reads are gone.
  • This needs OPENSHC_FUNCTION_VISIBILITY_UNITS on, like Reimplement UpdatePitchman and UpdateEngineer from gynt's units branch, 100% with OPENSHC_FUNCTION_VISIBILITY_UNITS on #514. With it off the six read lower than with the old volatiles (setLand falls well below 100%).

Five that still need a volatile: raiseLand, processEntityDamageToBuildingCollateral, placeRock, processDamageToBuilding and renderPreviewMapperWithBrush. Every volatile that a plain form matches was removed in #396's review round; the replies there give, per site, the best non-volatile score measured.

  • None of these is a brush-tile copy that callee visibility explains: both also call getTileForBrush, but in a unit with it a plain brush copy still loses (quick_diff ratio raiseLand 1.00 -> 0.65, collateral 0.90 -> 0.82), and dropping their other volatiles as well (raiseLand's in-memory LogicLayer &= ~L_ROCKY, the collateral function's HeightLayer re-read) only gets back to 0.81 and 0.84.
  • updateGFXLayers and updateGfxLayer also read GMFirstPictureIndex through a volatile cast. At /Od every plain spelling tried (12) narrows that read to mov ax, word ptr, while the original loads the full dword first; the table itself is not volatile in the original, which narrows it elsewhere (0x4f8de6).

determineBuildingPlacementRotation also keeps a volatile but stays in #396, because main's updateBuildingPlacementRotationPreview calls it under /GL and drops from 100% to 46.2% without it.

Match

Measured with reccmp on this branch (80f58eed5), struct and function resolvers on, OPENSHC_FUNCTION_VISIBILITY_UNITS on, with the 186 TileMapState sources plus calculateHoveredTile.cpp and xyAreValid.cpp in the build list (188 functions, average 99.37%):

Function Raw Normalized
createPlateau, getTileForBrush, isValidCastleSiteLocation, raiseLand, renderPreviewMapperWithBrush, setBitFlagBasedOnWallTowerGatehouseOrKeep, setLand, setTerrain, updateGFXLayers, updateMacroLayerRelated, useEraserBrush, useLevelBrush 100 100
updateGfxLayer 100 95.2
processEntityDamageToBuildingCollateral 99.9 95.8
placeRock 93.5 88.1
processDamageToBuilding 89.5 82.5
useTerrainHeightBrush 76.4 57.8

isValidCastleSiteLocation was 83.8% with its volatile reads; the unit takes it to 100%. Every other function in that list scores the same as on #396.

src/precomp/FunctionResolver.h: get() is __forceinline

All five get() definitions of the function resolver change from inline to __forceinline: the implemented, the game-function and the wrapper FunctionPtrUnifier, GameFunction, and the member GameFunction macro.

  • Why. A function compiled without global optimisation calls get() out of line. Every MACRO_CALL there becomes call Resolver::get + call eax where the original has one direct call, which is two bytes longer per call and shifts every later jump displacement.
  • Root cause in c2.dll (InlCallTupleIsCandidate, 0x10b69bd0): with _Globopt_flag cleared, only callees carrying the __forceinline bit are inline candidates. Plain inline is not enough, and neither /Ob nor #pragma inline_depth helps. All five definitions are needed.
  • Effect: the five functions above go from 92.8–98.1% to 100%. Optimised functions already inlined get(). A build of all 2515 sources under src/OpenSHC agreed that nothing else changes; the only two reported "worse", UI::Menu::Constructor_Menu and handleMenuItems, have unchanged bytes and differ only in how reccmp names a cmp .., 0x11000000 immediate.

Gotos

processDamageToBuilding and processEntityDamageToBuildingCollateral no longer use a goto: a destroyNormally flag, cleared in the special cases with the shared block after the switch, gives the same code. updateGfxLayer keeps its gotos, because every goto-free form measured lost match. Its former __asm jmp wallAlpha __asm jmp wallAlpha is now a plain goto wallAlpha; with identical bytes, since the macros already turn global optimisation off.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmZXUzxbDcYJj2mVg9V

…_forceinline resolver get()

Split out of #396 at review. Five of them are compiled without global
optimisation (#pragma optimize or an asm jump to a C label), which only
matches when FunctionResolver's get() is __forceinline; the other eleven
need a volatile read the original's code shape implies.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmZXUzxbDcYJj2mVg9V
gynt and others added 2 commits November 30, 2026 17:49
…ped with jz $+N

The original neighbour-flags macros jumped relative to the location counter
(jz $+9 / jz $+12), as a macro used several times in one function cannot
repeat a label. MSVC encodes such a jump in the near form 0F 84 rel32 and
counts it as control flow leaving the __asm block, so it raises C4740 and
turns global optimisation off for the enclosing function. That is the
unoptimised frame of every original function that contains these macros.

MACRO_ASM_JZ_NEAR now writes that jump, so setBitFlagBasedOnWallTowerGatehouseOrKeep,
setTerrain, updateGFXLayers and updateMacroLayerRelated need no pragma, and
updateGfxLayer's "__asm jmp wallAlpha __asm jmp wallAlpha" is a plain goto.
All five compile to the same bytes as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… in one unit with it

The original reads the tile and y out-parameters of getTileForBrush once after
the call and keeps them in registers across later stores and calls. A plain
copy of them compiles that way only when the callee's body is in the same
translation unit, as it was in the original; with the callee out of sight c2
propagates the copy, which is what the volatile reads worked around.

function-visibility.txt now lists createPlateau, isValidCastleSiteLocation,
setLand, useEraserBrush, useLevelBrush and useTerrainHeightBrush as callers of
getTileForBrush, and their volatile reads are plain copies. Measured with
OPENSHC_FUNCTION_VISIBILITY_UNITS on and both resolver sets enabled:
isValidCastleSiteLocation 83.8% -> 100%, the other five unchanged (useTerrainHeightBrush
stays at its 76.4% block-order wall), TileMapState average 99.28 -> 99.37.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gynt and others added 2 commits December 1, 2026 23:06
…orceinline resolver get()

Each line now states its percentage together with the caveat it depends on:
a volatile read or store in the source, or (for the five functions the
original's jz $+N asm macros leave unoptimised) __forceinline on
FunctionResolver's get().

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…racters

Co-Authored-By: Claude Opus 5.5 (1M context) <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.

1 participant