Skip to content

Shaders: a per-lane code load indexes only the program's data words - #50

Merged
ThatRecompDev merged 2 commits into
ThatRecompDev:mainfrom
Sr-0w:shaders/code-loads-only-from-pc
Nov 25, 2026
Merged

ThatRecompDev merged 2 commits into
ThatRecompDev:mainfrom
Sr-0w:shaders/code-loads-only-from-pc

Conversation

@Sr-0w

@Sr-0w Sr-0w commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

A program reads constant tables placed after its last s_endpgm through s_getpc_b64 pointers. The translation emits the whole code as a static array (g_code) read through CodeLoad:

  • a scalar load has a constant address, which dxc folds;
  • a per-lane (vector) load indexes the array per lane, and the driver then materialises the whole array.

Measured on an RX 7800 XT, cold (fivestar shader pso, never-compiled DXIL), for a tiled-lighting material pixel program from a retail game. It has 7,005 code words and 9 per-lane loads into a 55-word table at the end of its code.

pipeline creation driver peak private memory
before 26 s 1.9 GB
after 14 s 0.5 GB

In a game run, hundreds of these material programs were queued behind each other. They were built at about 0.3 pipelines per second, and 2 million draws were skipped while they waited.

Change

  • A per-lane code load now goes through CodeLoadLane, which indexes g_data: the code words after the last translated instruction (CodeData.Start), where the compiler puts the tables.
  • The gaps between translated instructions are not data. They are dead code that an s_branch skips over, as in this program at 0x3074.
  • An address before the data reads the data's first word, in the same way CodeLoad already clamps an address past the end.
  • A program without data words keeps g_code.

Ruled out along the way

  • A fallback that reads g_code with a wave-uniform index (a readfirstlane loop) brought back 28 s and 1.9 GB: any dynamic index into the whole array costs this much.
  • A per-register analysis that limits the CODE_TAG test to pointers derived from s_getpc produced byte-identical DXIL, because dxc already folds those tests. It is not included.

Test: a compute case reads an 8-entry table placed after its s_endpgm, per lane, through an s_getpc_b64 pointer. It runs through dxc and through the DXIL backend, and an off-by-one in CodeLoadLane fails it. The Shaders tests (404) and the architecture rules pass. Docs are updated in shader-translation.md.

🤖 Generated with Claude Code

@ThatRecompDev

ThatRecompDev commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

thanks, 26 s to 14 s on that tiled-lighting program is a nice win, and the test case is neat.

my worry is the "data only after the last translated instruction" assumption. if a table ever sits before some later code (between functions, or ahead of a long jump target), the lane load quietly reads g_data[0] where CodeLoad used to return the right word. so it'd be wrong without any error.

could the start come from the s_getpc-relative offsets the program actually adds (like the .Ltable-.Lpc add here) instead of the end of the code?

also heads up: this changes the hlsl of every s_getpc program with a global load, even ones with no data words, so everything gets re-translated and recompiled. fine if we go in knowing it, but if you can keep programs without data words byte-identical that'd help a lot.

tiny thing, the comment says 54 words and the doc says 55

Sr-0w added a commit to Sr-0w/five-star-recomp that referenced this pull request Nov 25, 2026
Review of ThatRecompDev#50:
- The data start is the earliest table the program's s_getpc pointers are moved to (s_add_u32 / s_sub_u32 of the
  low half right after each s_getpc), not the end of the translated code: a table between functions or before a
  long jump's target is found, and code after it is kept in g_data.
- When an s_getpc is used otherwise, or there is no table, the loads keep CodeLoad: the program's HLSL is byte for
  byte what it was before.
- Comment and doc agree on the word count (53 from 0x6ca0 in the program measured).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Sr-0w
Sr-0w force-pushed the shaders/code-loads-only-from-pc branch from 6455818 to 11c61d3 Compare November 25, 2026 21:25
@Sr-0w

Sr-0w commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Good catch, reworked (rebased on main):

  • data start: CodeData.Start now takes, for each s_getpc_b64 s[n:n+1], the s_add_u32 sn, sn, C (or s_sub_u32) right after it, and uses the earliest getpc.Next ± C. In the measured program all three getpcs land on 0x6ca0. A table sitting between functions or before later code is found, and any code after it is just carried along in g_data.
    • The remaining assumption: lanes read at or after the table their pointer was moved to.
    • If any getpc is used otherwise (copied before it moves, added from another register) or there is no getpc, the tables are unknown and nothing changes.
  • byte-identical: when no table is found, the program keeps CodeLoad (no CodeLoadLane, no g_data), so its HLSL is byte for byte what main makes. Only programs with a found table are re-translated.
  • count: 53 words from 0x6ca0 in both the comment and the doc.

Driver time is unchanged: 14 s, 0.4 GB, against 26 s / 1.9 GB before. New CodeDataTests cover s_add, s_sub, a table before later code, and the cases left unknown. Shader tests 455/455.

Sr-0w and others added 2 commits November 25, 2026 22:29
A program reads constant tables placed after its last s_endpgm through
s_getpc_b64 pointers. The translation emits the whole code as a static
array (g_code) and reads it through CodeLoad. A scalar load has a
constant address that dxc folds. A per-lane (vector) load indexes the
array per lane, and the driver then materializes the whole array.

For a tiled-lighting pixel program (7,005 code words, 9 per-lane loads
into a 55-word table at its end), the AMD driver took 26 s and 1.9 GB
to create the pipeline. A game run builds hundreds of these material
programs, at about 0.3 pipelines a second, and skipped 2 million draws
waiting for them.

A per-lane code load now goes through CodeLoadLane, which indexes
g_data: the code words after the last translated instruction, where
the compiler puts the tables. The same program takes 14 s and 0.5 GB.
An address before the data reads its first word, as CodeLoad clamps
one past the end; a program without data words keeps g_code.

Test: a compute case reads an 8-entry table after its s_endpgm per
lane through an s_getpc_b64 pointer (dxc and the DXIL backend); an
off-by-one in CodeLoadLane fails it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of ThatRecompDev#50:
- The data start is the earliest table the program's s_getpc pointers are moved to (s_add_u32 / s_sub_u32 of the
  low half right after each s_getpc), not the end of the translated code: a table between functions or before a
  long jump's target is found, and code after it is kept in g_data.
- When an s_getpc is used otherwise, or there is no table, the loads keep CodeLoad: the program's HLSL is byte for
  byte what it was before.
- Comment and doc agree on the word count (53 from 0x6ca0 in the program measured).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Sr-0w
Sr-0w force-pushed the shaders/code-loads-only-from-pc branch from 11c61d3 to e2276c2 Compare November 25, 2026 21:30
@Sr-0w

Sr-0w commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

In game (recorded runs, RX 7800 XT, 600 s each, same input script)

without this PR with it
pipelines the driver finished during the run 431 547 (+27%)
one tiled-lighting pixel pipeline, cold (shader pso) 26 s, 1.9 GB 14 s, 0.4 GB
fps in the opening cinematic 6–13 9–14 by the end of the run

The tiled-lighting material programs are most of the backlog. While a pipeline builds, its draws are skipped, so faster compiles mean more of the scene is actually drawn and the queue drains sooner. With this PR, the cinematic and its credits render over the lit scene in the second half of the run.

@ThatRecompDev
ThatRecompDev merged commit b7b877d into ThatRecompDev:main Nov 25, 2026
0 of 2 checks passed
ThatRecompDev added a commit that referenced this pull request Nov 25, 2026
@ThatRecompDev

Copy link
Copy Markdown
Owner

thanks, taking the data start from the s_getpc offsets is a much safer rule, and keeping programs without a table byte-identical means no recompile churn.

merged (566a88d). i fixed two rule hits in CodeData on the way in (a named hex prefix and an explicit if/else instead of the conditional).

one thing worth adding sometime: a test that the HLSL actually contains g_data for cs_code_table_lanes, since the case would also pass if it silently fell back to CodeLoad

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