diff --git a/docs/protection-research.md b/docs/protection-research.md new file mode 100644 index 00000000..c494e217 --- /dev/null +++ b/docs/protection-research.md @@ -0,0 +1,60 @@ +# Can NX2navi be protected from copying, including by AI tools? + +Research note, 2026-11-07. Not legal advice. + +**Short answer.** Nothing stops a person with read access from copying code or pasting it into an AI tool. You can limit who gets access, set legal terms, and opt out of cooperative crawlers. Ideas are free to reuse; only patents cover them, and they are impractical here. + +## Facts in this repo that change the answer + +- `LICENSE` names **Valiantsin Dzerakh**, not chrg6. The local clone is shallow; its root commit is by that author, and the remote is `valentderah/totk-pc-recomp`, which the GitHub page showed as **public, MIT, about 5.5k stars and 459 forks**. Copies already made under MIT stay MIT. Only the author of a piece of code can relicense it, so chrg6 can relicense their own commits (23 of 24 here), not the base code. +- `AGENTS.md` says "This repository is public"; update it once the decision is made. +- `THIRD_PARTY_NOTICES.md` lists about 40 components under MIT, BSD, zlib, ISC and Apache-2.0. 44 of 112 `runtime/*.c` files carry copyright lines. + +## 1. Technical limits + +| Measure | What it does | Limit | +|---|---|---| +| Private repo | Strongest control. [GitHub's terms](https://docs.github.com/en/site-policy/github-terms/github-terms-of-service) treat private content as confidential, with narrow staff access | Anyone invited can clone and paste it anywhere | +| GitHub AI settings | Since 24 Apr 2026, Copilot Free/Pro/Pro+ interaction data trains models unless you opt out at `github.com/settings/copilot/features`. Private content at rest is reportedly not used; code processed while Copilot is active in it is ([GitHub blog](https://github.blog/news-insights/company-news/updates-to-github-copilot-interaction-data-usage-policy/); details from secondary reports) | Copilot only. For public repos the terms (D.4) license your content to GitHub for AI training, and the opt-out excludes public repo content (J.3) | +| robots.txt | You cannot edit github.com's. [GitHub's](https://github.com/robots.txt) lets GPTBot, ClaudeBot and Claude-User crawl with path limits such as `/copilot/`; only Bytespider is blocked. No per-repo opt-out | Only for sites you host. Voluntary | +| Crawler tokens | [Anthropic](https://support.claude.com/en/articles/8896518): ClaudeBot (training), Claude-SearchBot, Claude-User; [OpenAI](https://developers.openai.com/api/docs/bots): GPTBot (training), OAI-SearchBot, ChatGPT-User (robots.txt "may not apply"); [Google-Extended](https://developers.google.com/search/docs/crawling-indexing/google-common-crawlers): Gemini training and grounding, not Search | Forward-looking; each bot named | + +**Cannot be prevented:** a clone pasted into an AI tool, a local assistant reading files, a collaborator's own settings, a user-triggered fetch of a public URL, or any scraper that ignores robots.txt. Notices in `AGENTS.md` or file headers are signals, not controls. + +## 2. Legal options + +[17 U.S.C. 102(b)](https://www.law.cornell.edu/uscode/text/17/102): copyright never extends to "any idea, procedure, process, system, method of operation, concept, principle, or discovery". A clean rewrite of our approach is legal. + +| Licence | Effect | Enforceability | +|---|---|---| +| MIT (now) | Anyone may copy, sell, relicense; must keep the notice | Nothing to enforce beyond the notice | +| GPL / AGPL | Copiers must publish derived source under the same terms; AGPL covers network use. Does not stop copying | Enforceable by the holder; fits only if you want sharing | +| PolyForm Noncommercial | Source-available; bars commercial use | Works as a copyright licence; not open source | +| BSL 1.1 | Limits production use, then [converts to GPL-compatible](https://mariadb.com/bsl11/) within 4 years; "not an Open Source license" | Built for companies; poor fit | +| All rights reserved | Default under copyright; fine while private | Strong in law, but no one else may use or contribute | +| "No AI training" clause | Custom wording, no standard text; breaks the [Open Source Definition](https://opensource.org/osd) rule against field-of-use limits | Untested. US district courts found AI training fair use on the facts in 2025 (Bartz v. Anthropic, Kadrey v. Meta; [review](https://copyrightalliance.org/ai-copyright-case-developments-2025/)), and in [Doe v. GitHub](https://www.gibsondunn.com/ninth-circuit-clarifies-limits-of-dmca-liability-for-ai-generated-code/) (9th Cir., Sept 2026) only the contract claims survive. I found no appeals ruling on fair use | + +Enforcement means finding a copy, proving copying and paying a lawyer. A DMCA takedown is the cheap tool for copies on GitHub. + +**Patents** are the only protection for ideas. Reported costs: USPTO provisional $65 to $325, software drafting and filing about $8,000 to $15,000, the full path $15,000 to $30,000 or more. They need filing before public disclosure, and published upstream work is prior art. From general knowledge, not checked here: software eligibility after *Alice* is shaky. Not worth it. + +## 3. What can and cannot be relicensed + +**Cannot:** every row of `THIRD_PARTY_NOTICES.md`. This covers `tools/vendor/` (unmodified), Ryujinx's shader translator, `tools/xlate/` (its `LICENSE` is MIT, jhaizhou) and files adapted from MacNativeGames, LASRecomp and others. They stay MIT, zlib or BSD with their notices. zlib code (`runtime/nvn_tex.c`) must be marked if altered. Stricter terms can cover only our own additions; anyone can still take the MIT parts from their upstream projects. + +**Translator:** pinned MIT Ryujinx code plus patches. Patch lines we wrote (nine of ten commits touching `tools/xlate/patches/` are chrg6's; per-patch authorship is not recorded) can carry our terms. The patched translator still ships under MIT with Ryujinx's notice, and the upstream stays free to everyone. + +**Can, if the author agrees or it is chrg6's alone:** new files such as `runtime/widescreen.c` and `recomp/patches-v111.txt`, and the Ocarina port docs. + +**Gap:** the TOTK-Ultrawide row is GPL with no URL or version; keep it data-only and record the source. + +## 4. Recommendations, ranked + +1. **Keep the repo private**, invite only trusted people, turn off the Copilot training opt-in in your account and ask collaborators to do the same. Free; the only real control. +2. **Settle ownership with Dzerakh in writing**: who owns what, and whether a restrictive licence on the additions is acceptable. Without it you can relicense only your own work. +3. **Choose a licence for our additions** (all rights reserved while private; PolyForm Noncommercial if shared), via per-file SPDX headers and a `LICENSE` that states the split. One short lawyer review. +4. **Add a notice** in `README.md` reserving rights and stating that AI training is not permitted. A legal signal, not protection. +5. **Add robots.txt** blocks for ClaudeBot, GPTBot and Google-Extended on any site you host. +6. **Skip patents.** The practical moat is staying ahead. + +Expect deterrence and a clean legal footing, not prevention. diff --git a/docs/shader-delivery-research.md b/docs/shader-delivery-research.md new file mode 100644 index 00000000..13c9da9e --- /dev/null +++ b/docs/shader-delivery-research.md @@ -0,0 +1,57 @@ +# Shader delivery: Advanced Shader Delivery and Vulkan equivalents + +Research only, 2026-11-07. No code was changed or built. Question: can Microsoft's Advanced Shader Delivery (ASD) cut first-run shader compile cost here? + +**Verdict: no.** ASD is a Direct3D 12 feature, fed by a store, and its inputs are game-derived. A Vulkan equivalent already exists in this codebase. The remaining gap is cheap to close locally. + +## 1. What ASD is + +- A "Windows 11 Direct3D 12 technology" with three stages: create a State Object Database (SODB), test it by compiling a Precompiled Shader Database (PSDB) and registering it locally, deploy the SODB to a storefront ([overview](https://learn.microsoft.com/en-us/windows/win32/direct3d12/advanced-shader-delivery)). +- The SODB is a SQLite file holding serialized PSO and state-object descriptions plus their DXIL bytecode, recorded from `CreateGraphicsPipelineState`, `CreateStateObject` and similar calls ([spec](https://microsoft.github.io/DirectX-Specs/d3d/StateObjectDatabase.html)). Recording uses `d3dconfig.exe` capture or a programmatic API; capture "adds significant overhead" ([Agility SDK 1.618](https://devblogs.microsoft.com/directx/agility-sdk-1-618/)). +- The store compiles the SODB with each GPU vendor's compiler plugin (`D3D12StateObjectCompiler`) into a per-GPU PSDB and ships it with the download; Windows refreshes it after driver updates ([intro](https://devblogs.microsoft.com/directx/introducing-advanced-shader-delivery/)). The D3D12 runtime finds it inside the game process by executable path through the [registration API](https://microsoft.github.io/DirectX-Specs/d3d/ShaderCacheRegistrationAPI.html). A PSDB must match the adapter family and compiler ABI. +- Requirements: Windows 11 24H2 (build 26100.1000+) and Agility SDK 1.619.5+ ([adoption guide](https://github.com/microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12AdvancedShaderDelivery/readme.md)). Submission is through Xbox Partner Center with an application identity ([GDC 2026](https://devblogs.microsoft.com/directx/advanced-shader-delivery-whats-new-at-gdc-2026/)). +- Hardware per the [21 Oct 2026 post](https://devblogs.microsoft.com/directx/advanced-shader-delivery-is-available-now-across-hardware): AMD RDNA 1-4, Intel Arc B-series and Core Ultra 2/3, NVIDIA RTX, Qualcomm Snapdragon X2, each with a minimum driver. Delivery is through the Microsoft Store and Xbox PC app; Steam and Epic are "coming soon", undated. +- No Microsoft page I read mentions Vulkan. + +## 2. Can it apply here? + +**No. ASD needs D3D12.** The SODB records D3D12 create calls and DXIL; the PSDB is looked up by the D3D12 runtime. A Vulkan process never touches either. + +Current flow (from the top comments of `runtime/gfx_shader.c` and `runtime/vk_pipeline.c`): +- An NVN program (control section, code, c1, translator answers) goes to `nx2navi_xlate.exe --serve` children and comes back as SPIR-V plus reflection, cached in memory and under `shader-cache/` (packed into `shader-cache.pack`). The key includes the translator identity, so any translator change invalidates every entry. `in/.in` files allow retranslation without guest memory. +- Play records `shader-answers.bin` and `shader-programs.bin`. `pipeline-keys.bin` records every built pipeline, shaders named by input id, independent of the translator. +- Pipelines use `VK_EXT_graphics_pipeline_library`: stage libraries, a fast link on a miss, an optimised pipeline built on a background thread. `pipeline-cache.bin` is dropped when vendor, device or cache UUID differ. +- Boot (`vk_pipeline_prewarm`) retranslates and rebuilds what the key file names. The installer runs `nx2navi.exe --prepare-shaders`, which warms the driver's disk cache. +- A comment in `runtime/boot.c` records a cold start (new translator or driver): 2,132 shaders translated and 1,579 pipelines built in 9.8 s. The guest waits at most `NX2NAVI_PREWARM_WAIT_MS` (default 3000), then the work continues in the background. +- Compute shaders are translated and built synchronously on the replay thread, because "a skipped dispatch can change what the game computes". Graphics misses are asynchronous and the draw is skipped. So wrong lighting early in a cold start more plausibly comes from skipped graphics draws than from compute. I did not reproduce or measure it. + +**A D3D12 back end** would replace about 25,800 lines (28 `vk_*.c` files plus headers) using push descriptors, mesh shaders, pipeline libraries and dynamic state 3, and the translator would have to emit DXIL, not SPIR-V. My estimate is several person-months. It would still not reach ASD, because delivery needs a store submission (section 3). The local "compile a SODB, register the PSDB" test path runs the same vendor compiler on the player's machine, so it gains nothing over the existing prepare pass. + +## 3. Content boundary + +`AGENTS.md` forbids anything derived from game data in the repository. Shaders, SPIR-V, DXIL, SODBs, PSDBs and `pipeline-cache.bin` are all derived from game code. `check_content.py` also blocks `.pack` files. + +- **Ruled out:** shipping or uploading any shader database, SODB, PSDB, SPIR-V or driver cache. ASD's deploy stage is exactly an upload to a store. +- **Still possible:** anything built on the player's machine from the player's own files. The installer's prepare step does this today, and everything stays in ignored folders. +- **Existing precedent:** the installer bundles a variant list (shader hash plus translator answers, no code), outside the repository. Keep any new shared data to that level. Whether a hash counts as "derived" is the owner's call. + +## 4. Vulkan-side options + +| Option | State here | Verdict | +|---|---|---| +| `VK_EXT_graphics_pipeline_library` | In use (`vk_device.c`, `vk_pipeline.c`, `NX2NAVI_GPL`) | Done | +| Driver disk cache | Used; a library built again on AMD takes 0.1 ms instead of 55 ms (`vk_pipeline.c`) | Done | +| Fossilize-style capture | `pipeline-keys.bin`, `in/*.in` and the answer files play that role, with the translator in front | Done. [Fossilize](https://github.com/ValveSoftware/Fossilize) replays on any device. [Steam pre-caching](https://www.phoronix.com/news/Steam-Vulkan-Shader-Pre-Cache) needs Steam and uploaded data: unusable | +| [`VK_KHR_pipeline_binary`](https://www.khronos.org/blog/bringing-explicit-pipeline-caching-control-to-vulkan) | Not used | Per-pipeline blobs with your own eviction, still per-driver. 52.8% of Windows devices expose it ([database](https://vulkan.gpuinfo.org/listextensions.php?platform=windows&extension=VK_KHR_pipeline_binary)); Mesa has it ([RADV](https://www.phoronix.com/news/RADV-Pipeline-Binary-Merged), [ANV](https://phoronix.com/news/Intel-ANV-Pipeline-Binary)). Per-vendor Windows support unconfirmed. Optional | +| [`VK_EXT_shader_object`](https://docs.vulkan.org/features/latest/features/proposals/VK_EXT_shader_object.html) | Not used | Skip: replaces pipelines, conflicts with the library design, binaries are device-bound | +| First-run "prepare shaders" pass | Installer runs it; boot prewarm capped at 3 s | The gap to close | + +After a translator change the dominant cost is likely translation (CPU, translator children), which neither pipeline binaries nor ASD-style delivery can speed up. The driver compile only repeats for shaders whose SPIR-V changed. Measure the split first; the prewarm logs report both counts. + +## 5. Recommendation + +ASD is not feasible: D3D12-only, store-gated, built from game-derived data. The practical steps: + +1. **Blocking "Preparing shaders" step on a cold cache (1-3 days).** When the translator identity, GPU or driver changes, run the existing prewarm to completion before the guest starts (the `NX2NAVI_PREWARM_WAIT_MS=0` behaviour), with progress shown. The `boot.c` figure is about 10 s, once. This removes the wrong-looking start. +2. **Re-run the library prepare pass after a driver change (2-4 days).** `lb_warm_prepared` and `--warm-shaders` exist, and the skip list already keys on vendor:device:driverVersion. Triggering the pass when `cache_load` drops a stale cache is the local counterpart of ASD's driver-update handling. I found no such trigger in `runtime/`; confirm before building. +3. **Measure, then decide on `VK_KHR_pipeline_binary` (1-2 weeks, low priority).** Prototype it only if the measurement points at the pipeline or driver cache. It could let libraries persist without the roughly 240 MB cache growth `vk_pipeline.c` avoids today. diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 42aaf15f..f856bcf7 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -70,6 +70,19 @@ # the CPU threads). # lib_prebuild = 1 # lib_prebuild_threads = 4 +# 1: very large compute shaders that no cache holds are built on one background thread after the start. 0: each is +# built when the game first uses it, which can hold that frame for a second or two. +# lib_prebuild_big_bg = 1 +# The commit headroom in MB those background builds wait for (0: no check). +# big_build_free_mb = 8192 +# How far those background builds may raise the process commit in MB before the rest wait for their first use instead +# (0: no limit). Each large shader costs the driver 1-4 GB; on a 31 GB machine four of them took the process from 15 to +# 29 GB and the game stuttered, so raise this only on a machine with room to spare. +# big_build_max_mb = 4096 +# 1: the log says how many pipelines came from pipeline-cache.bin; 2 also logs every compute pipeline built. 0: off. +# pipe_feedback = 1 +# pipeline-cache.bin larger than this (MB) starts empty and is rewritten (0: no limit). +# pipeline_cache_max_mb = 512 # Build known pipelines ahead of time. # pipe_prebuild = 1 # 1: the start waits until every known pipeline is built. Default 0: with the graphics pipeline library the start @@ -411,3 +424,9 @@ # names each dirty target that stays on the GPU ("fence write-back skipped-drawn-every-frame") and each one over 65536 # bytes that went back ("over-default"). Raising it costs a copy per such target and wait. # wb_fence_limit = 65536 +# Log kernel services that block longer than this many ms, with the service number (0: off). A stalled frame shows up as +# time in the replay; this says whether the guest was waiting in a service, and which one. +# svc_slow_ms = 0 +# 1: the frame-time summary also splits the replay by op kind (draw, bind, transfer), which says where a stalled frame +# went. The clock reads cost a fraction of a millisecond a frame, so it is off by default. +# replay_time = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index 840b9723..b366fec7 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -45,17 +45,18 @@ static const char *const known[] = { "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", - "LIB_PREBUILD_BIG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", + "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", - "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC", + "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC", "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "REFLEX", "RENDER_SCALE", "REPLAY_ASYNC", "RTSS", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", "SHADER_MEMO", "SHADOW_COMPARE", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", + "SVC_SLOW_MS", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "SPV_OVERRIDE", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SET_AT", "SWKBD_TEST", "TAA", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TEX_RELEASED_MB", "TEX_COMMIT_FREE_MB", "TEX_EVICT", "MIRROR_EVICT", "VRAM_BUDGET_MB", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NOOPT", "VK_NO_FLOAT64", @@ -63,7 +64,7 @@ static const char *const known[] = { "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "ASTC_GPU_DESWIZZLE", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", - "RECORDER_CORE", "RECORDER_PRIO", + "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", }; // The nx2navi.ini section a key cfg_set may write goes to when the file lacks it (nx2navi.ini.example's sections). @@ -142,6 +143,7 @@ static const CfgMeta metas[] = { {"DEVICE_PROFILE", "auto", "auto|desktop|deck", RESTART}, {"SHADOW_RES", "game", "game|256|512|768|1024|1536|2048", RESTART}, {"SHADOW_SCALE", "1", "1..4", RESTART}, {"SHADOW_COMPARE", "1", "0|1", RESTART}, // vk_texture.c + {"SVC_SLOW_MS", "0", "0..60000", RESTART}, // kernel.c kern_svc: log services slower than this {"SHADOW_RANGE", "100", "50..400", RESTART}, {"CONTACT_SHADOWS", "0", "0..100", RESTART}, {"PCSS", "0", "0..200", RESTART}, // Game @@ -177,13 +179,16 @@ static const CfgMeta metas[] = { {"DYN_RES", "0", "0|1", LIVE}, {"DYN_RES_TARGET", "16.7", "1..100", LIVE}, {"DYN_RES_STEP", "45", "8..600", LIVE}, // the recording thread: a core of its own (auto: with 12 or more logical processors) and the highest priority // (vk_exec.c vk_exec_record) - {"RECORDER_CORE", "auto", "auto|0|1", RESTART}, {"RECORDER_PRIO", "1", "0|1", RESTART}, + {"RECORDER_CORE", "auto", "auto|0|1", RESTART}, {"RECORDER_PRIO", "1", "0|1", RESTART}, {"REPLAY_TIME", "0", "0|1", RESTART}, // held submissions handed to the queue together (vk_sync.c, submit batching) {"SUBMIT_BATCH", "4", "1..16", RESTART}, // shaders and pipelines {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART}, {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART}, + {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, + {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, + {"BIG_BUILD_MAX_MB", "4096", "0..262144", RESTART}, // textures and memory {"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART}, {"TEX_ALIAS", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "90", "0..100", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART}, diff --git a/runtime/host.h b/runtime/host.h index 5cb1381c..0e8fe3a6 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -47,6 +47,11 @@ enum { PERF_CNT_PIPE_ASYNC, PERF_CNT_PIPE_SKIP, PERF_CNT_TEX_UP, PERF_CNT_TEX_RE PERF_CNT_PIPE_WAIT, PERF_CNT_ONESHOT_WAIT, PERF_CNT_PIPE_BUILT, PERF_CNT_TEX_NEW, PERF_CNT_TEX_ALIAS, PERF_CNT_WB, PERF_CNT_WB_SKIP, PERF_CNT_N }; void perf_count(int kind); +// NX2NAVI_REPLAY_TIME=1: how long the replay spends in each kind of op (perf.c perf_replay_op reports it with the +// frame-time summary). Draws and dispatches include their bindings and the vkCmd calls they record; binds are the state +// and resource binds between them; xfer is copies and clears. +enum { PERF_RP_DRAW, PERF_RP_BIND, PERF_RP_XFER, PERF_RP_OTHER, PERF_RP_N }; +void perf_replay_op(int cat, uint64_t ns); void perf_present(uint64_t frame); void perf_summary_flush(void); // prints the frame-time summary of the running period (exit); the 60 s lines come from perf_present void gfx_exit_log(void); // back end's lines for the exit summary (weak no-op in prof_win.c) diff --git a/runtime/kernel.c b/runtime/kernel.c index 1adb54dc..dd33700f 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -13,6 +13,7 @@ #include "cfg.h" #include "host.h" #include "kernel.h" +#include "prof.h" #include "platform/platform.h" #define R_OK 0u @@ -1675,7 +1676,7 @@ static void cv_hook(void) { if (h) h(); } -int kern_svc(Cpu *c, uint32_t imm) { +static int kern_svc_inner(Cpu *c, uint32_t imm) { uint64_t *x = c->x; if (g_diag > 0 && tcur) tcur->dg_svc = imm; switch (imm) { @@ -2397,3 +2398,21 @@ HOST_OVERRIDE(KERN_SYM_INITIALIZE_FIBER, kern_fiber_initialize); HOST_OVERRIDE(KERN_SYM_FINALIZE_FIBER, kern_fiber_finalize); HOST_OVERRIDE(KERN_SYM_SWITCH_TO_FIBER, kern_fiber_switch); HOST_OVERRIDE(KERN_SYM_GET_CURRENT_FIBER, kern_fiber_current); + +// NX2NAVI_SVC_SLOW_MS (default 0, off): logs each kernel service that blocks longer than that, with the service number. +// A stalled frame shows up as time in the replay; this says whether the guest was waiting in a service, and which one, +// instead of leaving it to a sampler to find. +int kern_svc(Cpu *c, uint32_t imm) { + static double lim = -1; + if (lim < 0) lim = strtod(cfg_value("NX2NAVI_SVC_SLOW_MS"), NULL); + if (lim <= 0) return kern_svc_inner(c, imm); + uint64_t t0 = plat_now_ns(); + int r = kern_svc_inner(c, imm); + double ms = (plat_now_ns() - t0) / 1e6; + if (ms >= lim) { + uint32_t tid = plat_thread_id(); + rt_log("kernel: service %#x blocked %.1f ms on thread %u%s", imm, ms, tid, + tid == prof_replay_tid ? " (the replay thread)" : ""); + } + return r; +} diff --git a/runtime/perf.c b/runtime/perf.c index f047273c..9a2d1495 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -116,14 +116,28 @@ void perf_count(int kind) { #define FT_WORST 5 #define FT_PERIOD_NS 60000000000ull typedef struct { uint64_t frame, ns, wait[PERF_N], replay; } FtWorst; +// What a frame over 25 ms spent its time on, so a count of slow frames says where to look instead of only that they +// happened. A frame is charged to the largest part when that part is at least a quarter of it, and to "unattributed" +// otherwise - the part of a slow frame none of the recorded waits explains (the GPU, or a stall outside them). +enum { FT_C_PIPE, FT_C_XLATE, FT_C_TEX, FT_C_FENCE, FT_C_PACE, FT_C_REPLAY, FT_C_OTHER, FT_C_N }; +static const char *const ft_cause_name[FT_C_N] = { "pipeline", "translation", "texture", "fence", "pacing", "replay", + "unattributed" }; static struct { uint64_t t0, frames, sum, over25, over50, over100; uint32_t hist[FT_BINS]; + uint64_t slow_n[FT_C_N], slow_ns[FT_C_N]; // frames over 25 ms by cause FtWorst worst[FT_WORST]; // slowest first int nworst; } ft; static uint64_t summary_t0; // start of the current period (0: not started) +// NX2NAVI_REPLAY_TIME=1: how the replay splits between the kinds of op it walks, reported with the frame-time summary. +// The replay is recorded on one thread, so these need no atomics. The kinds are PERF_RP_* in host.h. +static uint64_t rp_ns[PERF_RP_N], rp_n[PERF_RP_N]; +void perf_replay_op(int cat, uint64_t ns) { + if ((unsigned)cat < PERF_RP_N) rp_ns[cat] += ns, rp_n[cat]++; +} + static void ft_report(uint64_t now, const char *tag) { if (ft.frames && ft.sum) { uint64_t cut = (ft.frames + 99) / 100, acc = 0, low = 0; // the slowest 1% of frames @@ -134,6 +148,24 @@ static void ft_report(uint64_t now, const char *tag) { rt_log("frametime: %s: %llu frames in %.1f s, mean %.1f fps, 1%% low %.1f fps, over 25 ms %llu, 50 ms %llu, 100 ms %llu", tag, (unsigned long long)ft.frames, (now - ft.t0) / 1e9, ft.frames * 1e9 / ft.sum, low ? 1e9 / low : 0.0, (unsigned long long)ft.over25, (unsigned long long)ft.over50, (unsigned long long)ft.over100); + if (ft.over25) { + char line[512]; + int n = snprintf(line, sizeof line, "frametime: over 25 ms by cause:"); + for (int c = 0; c < FT_C_N && n > 0 && n < (int)sizeof line; c++) + if (ft.slow_n[c]) + n += snprintf(line + n, sizeof line - n, " %s %llu (%.1f ms avg)", ft_cause_name[c], + (unsigned long long)ft.slow_n[c], (double)ft.slow_ns[c] / ft.slow_n[c] / 1e6); + if (n > 0) rt_log("%s", line); + } + if (rp_n[PERF_RP_DRAW] || rp_n[PERF_RP_BIND] || rp_n[PERF_RP_XFER] || rp_n[PERF_RP_OTHER]) { + uint64_t all = 0; + for (int c = 0; c < PERF_RP_N; c++) all += rp_ns[c]; + rt_log("frametime: replay ops: %.0f ms in all (%.0f%% of the period); draw %.0f ms (%llu), bind %.0f ms (%llu), " + "xfer %.0f ms (%llu), other %.0f ms (%llu)", all / 1e6, 100.0 * all / (now - ft.t0), + rp_ns[PERF_RP_DRAW] / 1e6, (unsigned long long)rp_n[PERF_RP_DRAW], rp_ns[PERF_RP_BIND] / 1e6, + (unsigned long long)rp_n[PERF_RP_BIND], rp_ns[PERF_RP_XFER] / 1e6, (unsigned long long)rp_n[PERF_RP_XFER], + rp_ns[PERF_RP_OTHER] / 1e6, (unsigned long long)rp_n[PERF_RP_OTHER]); + } for (int i = 0; i < ft.nworst; i++) { const FtWorst *w = &ft.worst[i]; rt_log("frametime: worst frame %llu: %.1f ms; shader translation %.1f, pipeline build %.1f, texture upload %.1f (astc %.1f), replay %.1f, fence/present wait %.1f, pacing %.1f", @@ -143,6 +175,8 @@ static void ft_report(uint64_t now, const char *tag) { } } memset(&ft, 0, sizeof ft); + memset(rp_ns, 0, sizeof rp_ns); + memset(rp_n, 0, sizeof rp_n); ft.t0 = now; } @@ -171,6 +205,24 @@ static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) { if (d > 25000000ull) ft.over25++; if (d > 50000000ull) ft.over50++; if (d > 100000000ull) ft.over100++; + if (d > 25000000ull) { // which part it went to + uint64_t part[FT_C_N]; + part[FT_C_PIPE] = frame_ns[PERF_PIPE]; + part[FT_C_XLATE] = frame_ns[PERF_XLATE]; + part[FT_C_TEX] = frame_ns[PERF_TEX]; + part[FT_C_FENCE] = frame_ns[PERF_FENCE] + frame_ns[PERF_DRAIN]; + part[FT_C_PACE] = frame_ns[PERF_PACE]; + part[FT_C_REPLAY] = (uint64_t)rep > 0 ? (uint64_t)rep : 0; + uint64_t known = 0; + for (int c = 0; c < FT_C_OTHER; c++) known += part[c]; + part[FT_C_OTHER] = d > known ? d - known : 0; + int best = FT_C_OTHER; + for (int c = 0; c < FT_C_N; c++) + if (part[c] > part[best]) best = c; + if (part[best] * 4 < d) best = FT_C_OTHER; // no recorded part explains a quarter of the frame + ft.slow_n[best]++; + ft.slow_ns[best] += d; + } if (d > 25000000ull && (ft.nworst < FT_WORST || d > ft.worst[FT_WORST - 1].ns)) { int i = ft.nworst < FT_WORST ? ft.nworst++ : FT_WORST - 1; while (i > 0 && ft.worst[i - 1].ns < d) ft.worst[i] = ft.worst[i - 1], i--; diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 7e577542..47e3dd5b 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -4606,6 +4606,10 @@ static void op_dispatch(VkExec *ex, const p_dispatch *d) { vk_contact_flush(ex); cb_res(ex); dummies_init(); + // A tensor-core program runs the stand-in while NX2NAVI_NATIVE_TENSOR is off, so ask that before translating it. + // Translating it anyway recorded four AI-upscaler compute shaders of 0.87-4.4M SPIR-V words that this game never + // dispatches natively, and building them cost gigabytes of memory at boot (NX2NAVI_LIB_PREBUILD_BIG, bg_main). + if (!native_tensor(ex)) { untranslated_dispatch_fallback(ex); return; } int present; const GfxShader *cs = shader_for(ex, NVN_STAGE_CS, &present, NULL, 0); // synchronous: a skipped dispatch changes results if (knob_log_cs_binds()) { // diagnosis: the compute bindings of the first dispatch of each program @@ -4657,6 +4661,7 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) { return warn_once(W_INDIRECT, "indirect dispatch arguments at %#llx are not backed or not aligned: skipped", (unsigned long long)d->ind); cb_res(ex); dummies_init(); + if (!native_tensor(ex)) { untranslated_dispatch_fallback(ex); return; } // as in op_dispatch: never translate it int present; const GfxShader *cs = shader_for(ex, NVN_STAGE_CS, &present, NULL, 0); if (!cs || !native_tensor(ex)) { untranslated_dispatch_fallback(ex); return; } @@ -4730,7 +4735,7 @@ static void bind_handles(uint64_t *dst, int cap, const p_bind_handles *b) { memcpy(&dst[b->first], b->h, sizeof(uint64_t) * (size_t)b->n); } -void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) { +static void op_draw_impl(VkExec *ex, uint32_t op, const void *payload) { switch (op) { case OP_BIND_PROGRAM: { const p_bind_program *b = payload; @@ -4840,3 +4845,27 @@ void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) { // The last executed list's draws as " /:" (bit n: cn), for NX2NAVI_MV_CHECK's lines. const char *vk_mvobj_frame_masks(void) { return fm_last; } + +// NX2NAVI_REPLAY_TIME (default 0, off): times each op so the frame-time summary can say how the replay splits between +// draws, binds and transfers. The clock reads cost a fraction of a millisecond a frame, which is why it is off by +// default and why the wrapper is the only thing between the walk and the switch. +static int op_is_draw(uint32_t op) { + switch (op) { + case OP_DRAW: case OP_DRAW_IDX: case OP_DRAW_IDX_IND: + case OP_DISPATCH: case OP_DISPATCH_IND: + case OP_DRAW_MESH: case OP_DRAW_MESH_IND: + case OP_UPDATE_UB: + return 1; + default: + return 0; + } +} + +void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) { + static int on = -1; + if (on < 0) on = cfg_bool("NX2NAVI_REPLAY_TIME"); + if (!on) { op_draw_impl(ex, op, payload); return; } + uint64_t t0 = plat_now_ns(); + op_draw_impl(ex, op, payload); + perf_replay_op(op_is_draw(op) ? PERF_RP_DRAW : PERF_RP_BIND, plat_now_ns() - t0); +} diff --git a/runtime/vk_exec_xfer.c b/runtime/vk_exec_xfer.c index 77ab8fa6..57109bb3 100644 --- a/runtime/vk_exec_xfer.c +++ b/runtime/vk_exec_xfer.c @@ -34,6 +34,8 @@ #include #include +#include "cfg.h" +#include "platform/platform.h" #include "host.h" #include "nvn_api.h" #include "nvn_tex.h" @@ -786,7 +788,7 @@ static void clear_tex(VkExec *ex, const p_clear_tex *p) { vk_texture_gpu_written(t); } -void vk_exec_op_xfer(VkExec *ex, uint32_t op, const void *payload) { +static void op_xfer_impl(VkExec *ex, uint32_t op, const void *payload) { if (op != OP_RENDER_ENABLE && op != OP_SIGNAL_EVENT) vk_contact_flush(ex); switch (op) { case OP_COPY_B2B: copy_b2b(ex, payload); break; @@ -807,3 +809,13 @@ void vk_exec_op_xfer(VkExec *ex, uint32_t op, const void *payload) { default: rt_fatal("vk: op %s is not a transfer", nvn_op_name(op)); } } + +// NX2NAVI_REPLAY_TIME: see the note over vk_exec_op_draw in vk_exec_draw.c; transfers are the third kind. +void vk_exec_op_xfer(VkExec *ex, uint32_t op, const void *payload) { + static int on = -1; + if (on < 0) on = cfg_bool("NX2NAVI_REPLAY_TIME"); + if (!on) { op_xfer_impl(ex, op, payload); return; } + uint64_t t0 = plat_now_ns(); + op_xfer_impl(ex, op, payload); + perf_replay_op(PERF_RP_XFER, plat_now_ns() - t0); +} diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 00aaec37..c818fb37 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -77,6 +77,8 @@ static struct { int dyn_vi, dyn_clamp, dyn_poly, dyn_blend_en, dyn_blend_eq, dyn_wmask, dyn_a2c; int f_logic_op, f_depth_clamp, f_fill_non_solid, f_dual_src, f_full_subgroups; int cache_control; // pipelineCreationCacheControl: a create can ask for the pipeline caches only + int feedback; // NX2NAVI_PIPE_FEEDBACK: 1 count pipeline cache hits, 2 also log each compute build + uint64_t cache_loaded; // bytes of pipeline-cache.bin the driver took at boot int pv_last; // provokingVertexLast enabled: NVN's default is the last vertex VkShaderStageFlags sg32_stages; // stages that accept requiredSubgroupSize = 32 (0: not available) int reconverge; // modules with subgroup ops get MaximallyReconvergesKHR (spirv_reconverge) @@ -138,6 +140,7 @@ static uint64_t fnv(uint64_t h, const void *p, size_t n) { return h; } #define FNV0 0xcbf29ce484222325ull +static __thread uint64_t mm_hash; // FNV-1a of the module make_module last created on this thread (PIPE_FEEDBACK 2) // Hash of a pipeline table key, 8 bytes per step: a draw hashes its whole GKey, and byte-wise FNV over it cost more // than the lookup. Only the in-memory table uses it; the key file keeps fnv. @@ -182,6 +185,18 @@ static void cache_load(VkCtx *c) { rt_log("vk_pipeline: %s was written by another device or driver; starting empty", P.cache_path); size = 0; } + // NX2NAVI_PIPELINE_CACHE_MAX_MB (default 512; 0 no limit): a larger file starts the run empty, and the run's + // saves replace it. The file only grows: entries of earlier translators and builds stay, and on NVIDIA + // (621.01) an entry another executable wrote is neither used nor written again, so a renamed or updated exe + // compiles while the file keeps its size (2026-11-09: 650 MB, 15-18 % of the pipelines read from it). The + // whole file sits in process memory twice at the boot (this read and the driver's copy) and is copied out + // at every save, which a 16 GB machine near its commit limit cannot spare. + uint64_t cap = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_MAX_MB"), NULL, 10) << 20; + if (size && cap && size > cap) { + rt_log("vk_pipeline: %s holds %.0f MB, over NX2NAVI_PIPELINE_CACHE_MAX_MB %llu: starting empty; this run " + "rewrites it", P.cache_path, size / 1048576.0, (unsigned long long)(cap >> 20)); + size = 0; + } } VkPipelineCacheCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, .initialDataSize = size, .pInitialData = size ? data : NULL}; @@ -190,6 +205,14 @@ static void cache_load(VkCtx *c) { if (vkCreatePipelineCache(c->device, &ci, NULL, &P.cache) != VK_SUCCESS) P.cache = VK_NULL_HANDLE; } free(data); + // What the driver kept of the file: a driver may drop initial data it does not accept without failing the create, + // which leaves every build of the run compiling while the cache file looks valid. + size_t kept = 0; + if (P.cache && vkGetPipelineCacheData(c->device, P.cache, &kept, NULL) != VK_SUCCESS) kept = 0; + P.cache_loaded = kept; + if (size) + rt_log("vk_pipeline: %s: %.1f MB read, the driver holds %.1f MB of it", P.cache_path, size / 1048576.0, + kept / 1048576.0); } static void keys_open(void); @@ -245,6 +268,8 @@ static int ensure_init(void) { P.reconverge ? "on" : c->has_maximal_reconvergence ? "off (NX2NAVI_VK_RECONVERGE=0)" : "not supported by the driver"); P.f_full_subgroups = v13.computeFullSubgroups; P.cache_control = v13.pipelineCreationCacheControl; + // Pipeline creation feedback is core in Vulkan 1.3; the driver reports per pipeline whether P.cache held it. + P.feedback = c->props.apiVersion >= VK_API_VERSION_1_3 ? atoi(cfg_value("NX2NAVI_PIPE_FEEDBACK")) : 0; P.f_logic_op = f2.features.logicOp; P.f_depth_clamp = f2.features.depthClamp; P.f_fill_non_solid = f2.features.fillModeNonSolid; @@ -1000,6 +1025,7 @@ static VkShaderModule make_module(const uint32_t *w, uint64_t words) { free(fixed); w = fixed = rc, words = rc_n; } + if (P.feedback >= 2) mm_hash = fnv(FNV0, w, (size_t)words * 4); VkShaderModuleCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = (size_t)words * 4, .pCode = w}; VkShaderModule m = VK_NULL_HANDLE; VkResult r = vkCreateShaderModule(P.device, &ci, NULL, &m); @@ -1392,6 +1418,57 @@ static VkPipelineCreateFlags noopt_flag(void) { return on ? VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT : 0; } +// ---- pipeline creation feedback (NX2NAVI_PIPE_FEEDBACK, default 1; 0 off) +// Each pipeline, library and compute build asks the driver whether pipeline-cache.bin (P.cache) held it +// (VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT) and how long the create took. The counts appear +// with the prewarm, the stage library prebuild and every cache save, so a log tells whether a slow boot compiled or +// read its pipelines: on NVIDIA the driver's own disk cache is kept per executable, so a renamed or new exe gets nothing +// from it, and only pipeline-cache.bin can make its first boot quick. =2 also logs every compute build with a hash of +// its final module, so two runs can be compared for a module that changes between them. +enum { FB_GRAPHICS, FB_LIBRARY, FB_COMPUTE, FB_KINDS }; +static struct { + PlatMutex lock; + uint64_t n[FB_KINDS], hit[FB_KINDS]; + double hit_ms[FB_KINDS], miss_ms[FB_KINDS]; +} FB = {.lock = PLAT_MUTEX_INIT}; + +// Chains fb into a create info's pNext (next: its current pNext). Returns what the create info's pNext becomes. +static const void *fb_chain(VkPipelineCreationFeedbackCreateInfo *ci, VkPipelineCreationFeedback *fb, const void *next) { + if (!P.feedback) return next; + memset(fb, 0, sizeof *fb); + *ci = (VkPipelineCreationFeedbackCreateInfo){.sType = VK_STRUCTURE_TYPE_PIPELINE_CREATION_FEEDBACK_CREATE_INFO, + .pNext = next, .pPipelineCreationFeedback = fb}; + return ci; +} + +static void fb_note(int kind, const VkPipelineCreationFeedback *fb) { + if (!P.feedback || !(fb->flags & VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT)) return; + int hit = (fb->flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT) != 0; + double ms = (double)fb->duration / 1e6; + plat_mutex_lock(&FB.lock); + FB.n[kind]++, FB.hit[kind] += (uint64_t)hit; + if (hit) FB.hit_ms[kind] += ms; + else FB.miss_ms[kind] += ms; + plat_mutex_unlock(&FB.lock); +} + +// One line: per kind, builds that came from pipeline-cache.bin of all builds so far, with the average time of each. +static void fb_log(const char *when) { + if (!P.feedback) return; + static const char *const kn[FB_KINDS] = {"pipelines", "libraries", "compute"}; + char line[512]; + int o = snprintf(line, sizeof line, "vk_pipeline: pipeline-cache.bin hits %s:", when); + plat_mutex_lock(&FB.lock); + for (int k = 0; k < FB_KINDS && o < (int)sizeof line - 96; k++) { + uint64_t miss = FB.n[k] - FB.hit[k]; + o += snprintf(line + o, sizeof line - o, " %s %llu of %llu (%.0f%%; avg %.2f ms from the file, %.1f ms otherwise)%s", kn[k], + (unsigned long long)FB.hit[k], (unsigned long long)FB.n[k], FB.n[k] ? 100.0 * FB.hit[k] / FB.n[k] : 0.0, + FB.hit[k] ? FB.hit_ms[k] / FB.hit[k] : 0.0, miss ? FB.miss_ms[k] / miss : 0.0, k + 1 < FB_KINDS ? ";" : ""); + } + plat_mutex_unlock(&FB.lock); + rt_log("%s", line); +} + static int uses_src1(const VkPipeState *s) { for (uint32_t m = s->color.blend_en & 0xffu; m; m &= m - 1) { int i = __builtin_ctz(m); @@ -1636,6 +1713,9 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay .pDynamicState = &dy, .layout = l->layout, .flags = noopt_flag()}; if (k->fbl & 1) pi.flags |= VK_PIPELINE_CREATE_COLOR_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT; if (k->fbl & 2) pi.flags |= VK_PIPELINE_CREATE_DEPTH_STENCIL_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT; + VkPipelineCreationFeedbackCreateInfo fci; + VkPipelineCreationFeedback fb; + pi.pNext = fb_chain(&fci, &fb, pi.pNext); uint64_t p0 = perf_begin(); crash_note("a graphics pipeline", s->vs, s->fs, s->ts); VkResult r = vkCreateGraphicsPipelines(P.device, P.cache, 1, &pi, NULL, &pipe); @@ -1646,6 +1726,7 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay pipe = VK_NULL_HANDLE; } else { perf_count(PERF_CNT_PIPE_BUILT); + fb_note(FB_GRAPHICS, &fb); } out: if (vm) vkDestroyShaderModule(P.device, vm, NULL); @@ -1792,9 +1873,13 @@ static VkPipeline lib_create(int kind, const VkPipeState *s, const GKey *k, VkPi ri.depthAttachmentFormat = (VkFormat)k->depth, ri.stencilAttachmentFormat = (VkFormat)k->stencil; pi.pColorBlendState = &cb, pi.pMultisampleState = &ms; } + VkPipelineCreationFeedbackCreateInfo fci; + VkPipelineCreationFeedback fb; + pi.pNext = fb_chain(&fci, &fb, pi.pNext); crash_note("a pipeline library", s->vs, s->fs, NULL); if (vkCreateGraphicsPipelines(P.device, P.cache, 1, &pi, NULL, &pipe) != VK_SUCCESS) pipe = VK_NULL_HANDLE; plat_crash_context(NULL); + if (pipe && (kind == LIB_PR || kind == LIB_FS)) fb_note(FB_LIBRARY, &fb); // the shader libraries: the others compile nothing out: if (m) vkDestroyShaderModule(P.device, m, NULL); free(patched); @@ -2883,6 +2968,7 @@ static void prewarm_run(void) { free(K.file); K.file = NULL; plat_mutex_unlock(&P.lock); + fb_log("after the boot prewarm"); vk_pipeline_prewarm_marker_clear(); // the boot's share is done; what the pool builds later runs during play lb_start(pr_hist, fs_hist); } @@ -3008,9 +3094,18 @@ static VkPipeline compute_create(const GfxShader *cs, const Layout *l, int cache if (P.f_full_subgroups && vk_caps.sg_size && lx && lx % vk_caps.sg_size == 0 && uses_subgroup_ops(code, words)) ci.stage.flags |= VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT; VkPipeline pipe = VK_NULL_HANDLE; + VkPipelineCreationFeedbackCreateInfo fci; + VkPipelineCreationFeedback fb; + ci.pNext = fb_chain(&fci, &fb, ci.pNext); crash_note("a compute pipeline", cs, NULL, NULL); if (vkCreateComputePipelines(P.device, P.cache, 1, &ci, NULL, &pipe) != VK_SUCCESS) pipe = VK_NULL_HANDLE; plat_crash_context(NULL); + if (pipe) fb_note(FB_COMPUTE, &fb); + if (P.feedback >= 2 && (pipe || !cached_only)) + rt_log("vk_pipeline: compute %016llx: %llu words, module %016llx, %s, %.1f ms", (unsigned long long)fnv(FNV0, code, words * 4), + (unsigned long long)words, (unsigned long long)mm_hash, + !pipe ? "failed" : fb.flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT ? "from pipeline-cache.bin" : "compiled", + (double)fb.duration / 1e6); vkDestroyShaderModule(P.device, m, NULL); free(over); return pipe; @@ -3226,6 +3321,19 @@ static void cs_flight_remove(const GfxShader *cs) { if (cs_flight[i] == cs) cs_flight[i] = NULL; } +// The background builder of large compute shaders (comment at bg_main). +static struct { + const GfxShader **cs; + uint8_t (*iid)[32]; + uint64_t *words; + size_t n, cap; + PlatThread *th; + const GfxShader *volatile building; // the shader on the driver now, NULL between builds + volatile int32_t raised; // a dispatch waited for it and raised the thread + uint64_t built, failed, there; + double ms; +} BG; + VkPipeline vk_pipeline_compute(const GfxShader *cs, const VkPipeInfo **info) { if (info) *info = NULL; if (!cs || !ensure_init()) return VK_NULL_HANDLE; @@ -3233,12 +3341,22 @@ VkPipeline vk_pipeline_compute(const GfxShader *cs, const VkPipeInfo **info) { uint64_t h = key_hash(&k, sizeof k); plat_mutex_lock(&P.lock); Pipe *p = pipe_find(&k, sizeof k, h); + double w0 = !p && cs_flight_has(cs) ? now_ms() : 0; + int bg = 0; while (!p && cs_flight_has(cs)) { // another thread is compiling it: its result, not a second compile + if (BG.building == cs && BG.th && !plat_xchg32(&BG.raised, 1)) // the background builder: normal priority now + plat_thread_set_priority(BG.th, PLAT_PRIO_NORMAL), bg = 1; plat_mutex_unlock(&P.lock); plat_sleep_ms(1); plat_mutex_lock(&P.lock); p = pipe_find(&k, sizeof k, h); } + if (w0 && now_ms() - w0 >= 2.0) { + static int logs; + if (logs++ < 16) + rt_log("vk_exec: a dispatch waited %.0f ms for its compute pipeline (%s)", now_ms() - w0, + bg ? "built on the background thread" : "built by another thread"); + } if (p) { P.stats.hits++; } else { @@ -3322,6 +3440,177 @@ static void lb_defer(const uint8_t iid[32], uint64_t words) { (unsigned long long)LB.big_words); } +// ---- large compute shaders on a background thread (NX2NAVI_LIB_PREBUILD_BIG_BG, default 1; 0 leaves them to their +// first dispatch as before) +// The boot prebuild leaves a compute shader over LB.big_words that no pipeline cache holds (lb_compute). Its first +// dispatch then compiled it on the replay thread: frames of 0.65-1.9 s in Kokiri Forest on an RTX 5090 after a +// translator change. One thread builds them instead, once the boot prebuild is done, one at a time and the smallest +// first: four compiled side by side grew the process by 16-19 GB on an RTX 4090 (comment at LB_BIG_WORDS). The shader +// being built is in cs_flight, so a dispatch that needs it waits for this build (vk_pipeline_compute raises the +// thread to normal priority then) instead of compiling a second copy. What is built is entered for the process, as +// lb_compute's pipelines are, and lands in pipeline-cache.bin for the next boot. + +static int bg_on(void) { + static int on = -1; + if (on < 0) on = cfg_bool("NX2NAVI_LIB_PREBUILD_BIG_BG"); + return on; +} + +// A large compute shader no cache holds goes to the background builder (lb_worker threads; under LB.stat). +static void bg_queue(const GfxShader *cs, const uint8_t iid[32], uint64_t words) { + plat_mutex_lock(&LB.stat); + if (BG.n == BG.cap) { + size_t cap = BG.cap ? BG.cap * 2 : 16; + const GfxShader **c = realloc(BG.cs, cap * sizeof *c); + if (c) BG.cs = c; + uint8_t (*i)[32] = realloc(BG.iid, cap * sizeof *i); + if (i) BG.iid = i; + uint64_t *w = realloc(BG.words, cap * sizeof *w); + if (w) BG.words = w; + if (c && i && w) BG.cap = cap; + } + int ok = BG.n < BG.cap; + if (ok) BG.cs[BG.n] = cs, memcpy(BG.iid[BG.n], iid, 32), BG.words[BG.n] = words, BG.n++; + plat_mutex_unlock(&LB.stat); + if (!ok) { + lb_defer(iid, words); + return; + } + rt_log("vk_pipeline: stage library prebuild: compute shader %02x%02x%02x%02x%02x%02x%02x%02x (%llu SPIR-V words) not in " + "the pipeline caches: built on the background thread after the prebuild (NX2NAVI_LIB_PREBUILD_BIG_BG)", + iid[0], iid[1], iid[2], iid[3], iid[4], iid[5], iid[6], iid[7], (unsigned long long)words); +} + +// NX2NAVI_BIG_BUILD_FREE_MB (default 8192; 0 no check): the system commit headroom a large compute build waits for. +// A build of one of these shaders took the process up by 1-3 GB on an RTX 5090 and four at once by 16-19 GB on an +// RTX 4090; started on a machine near its commit limit it can end the game. While the headroom is short the shader +// waits, and if the game dispatches it first it is built there, as without the background builder. +static int bg_room(double *used, double *limit) { + static double need = -1; + if (need < 0) need = strtod(cfg_value("NX2NAVI_BIG_BUILD_FREE_MB"), NULL); + double proc; + plat_commit_info(&proc, used, limit); + return need <= 0 || *limit < 0 || *limit - *used >= need; +} + +// NX2NAVI_BIG_BUILD_MAX_MB (default 4096; 0 no cap): how far the builder may raise the process commit before it stops +// and leaves the rest to their first dispatch. One of these shaders costs the driver 1-4 GB: on a 31 GB machine four of +// them took the process from 15 to 29 GB and system commit to 60 of 65 GB, which is where a player feels the game +// stutter. The builder exists to remove hitches, not to trade them for paging, so a machine that cannot hold them all +// builds only what it can and pays one hitch per shader later. +static double bg_growth_cap(void) { + static double cap = -1; + if (cap < 0) cap = strtod(cfg_value("NX2NAVI_BIG_BUILD_MAX_MB"), NULL); + return cap; +} + +static void bg_main(void *arg) { + (void)arg; + plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); + // The queue can grow while this thread builds (bg_queue runs on the replay thread and reallocs these arrays), so + // the sort and every read below happen under LB.stat; an entry is copied out before its compile so no pointer into + // the arrays is held across it. + plat_mutex_lock(&LB.stat); + for (size_t i = 1; i < BG.n; i++) // smallest first: insertion sort of a handful + for (size_t j = i; j > 0 && BG.words[j] < BG.words[j - 1]; j--) { + const GfxShader *c = BG.cs[j]; + BG.cs[j] = BG.cs[j - 1], BG.cs[j - 1] = c; + uint64_t w = BG.words[j]; + BG.words[j] = BG.words[j - 1], BG.words[j - 1] = w; + uint8_t t[32]; + memcpy(t, BG.iid[j], 32), memcpy(BG.iid[j], BG.iid[j - 1], 32), memcpy(BG.iid[j - 1], t, 32); + } + plat_mutex_unlock(&LB.stat); + double t_all = now_ms(); + double c_start, c_used0, c_limit0; + plat_commit_info(&c_start, &c_used0, &c_limit0); + for (size_t i = 0; !LB.stop; i++) { + uint8_t idbuf[32]; + uint64_t words = 0; + const GfxShader *cs; + plat_mutex_lock(&LB.stat); + int have = i < BG.n; + if (have) { + cs = BG.cs[i]; + memcpy(idbuf, BG.iid[i], 32); + words = BG.words[i]; + } + plat_mutex_unlock(&LB.stat); + if (!have) break; + const uint8_t *iid = idbuf; + struct { const GfxShader *cs; uint32_t compute; } k = {cs, 1}; + uint64_t h = key_hash(&k, sizeof k); + Bindings B = {0}; + const char *why = NULL; + double used, limit, c_now; + plat_commit_info(&c_now, &used, &limit); + double cap = bg_growth_cap(); + if (cap > 0 && c_now - c_start >= cap) { + rt_log("vk_pipeline: background compute build: stopping, the process grew %.0f MB in all (NX2NAVI_BIG_BUILD_MAX_MB " + "%.0f); the shaders left build at their first dispatch", c_now - c_start, cap); + break; + } + int waited = 0, no_room = 0; + double w0 = now_ms(); + while (!LB.stop && !bg_room(&used, &limit)) { // not under P.lock: a dispatch builds it meanwhile + if (!waited++) + rt_log("vk_pipeline: background compute build: waiting for commit headroom (system %.0f of %.0f MB, " + "NX2NAVI_BIG_BUILD_FREE_MB %s)", used, limit, cfg_value("NX2NAVI_BIG_BUILD_FREE_MB")); + if (now_ms() - w0 > 30000.0) { // waiting is only useful when the headroom will come back + rt_log("vk_pipeline: background compute build: no commit headroom after %.0f s; the shaders left build at " + "their first dispatch", (now_ms() - w0) / 1000.0); + no_room = 1; + break; + } + plat_sleep_ms(2000); + } + if (LB.stop || no_room) break; + plat_mutex_lock(&P.lock); + int there = cs_flight_has(cs) || pipe_find(&k, sizeof k, h); // a dispatch got to it first + Layout *l = there ? NULL : compute_layout(cs, &B, &why); + int flight = l ? cs_flight_add(cs) : 0; + if (l) BG.building = cs; + plat_mutex_unlock(&P.lock); + if (!l) { + if (there) BG.there++; + else BG.failed++, rt_log("vk_pipeline: background compute build: %s", why ? why : "no layout"); + continue; + } + double t0 = now_ms(); + VkPipeline pipe = compute_create(cs, l, 0); + double ms = now_ms() - t0; + plat_mutex_lock(&P.lock); + if (flight) cs_flight_remove(cs); + BG.building = NULL; + if (pipe && !pipe_find(&k, sizeof k, h)) { + pipe_add(&k, sizeof k, h, pipe, l, &B, VK_PIPELINE_BIND_POINT_COMPUTE, 0); + P.stats.compute++; + } else if (pipe) { + vkDestroyPipeline(P.device, pipe, NULL); + } + plat_mutex_unlock(&P.lock); + if (plat_xchg32(&BG.raised, 0)) plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); + if (pipe) BG.built++, BG.ms += ms; + else BG.failed++; + double c_proc, c_used, c_limit; + plat_commit_info(&c_proc, &c_used, &c_limit); + rt_log("vk_pipeline: background compute build: %02x%02x%02x%02x%02x%02x%02x%02x (%llu SPIR-V words) %s in %.0f ms; " + "commit: process %.0f MB, system %.0f of %.0f MB", iid[0], iid[1], iid[2], iid[3], iid[4], iid[5], iid[6], + iid[7], (unsigned long long)words, pipe ? "built" : "failed", ms, c_proc, c_used, c_limit); + } + rt_log("vk_pipeline: background compute builds %s: %llu built (%.0f ms in all), %llu failed, %llu dispatched before " + "their turn; %.0f ms after the prebuild", LB.stop ? "stopped" : "done", (unsigned long long)BG.built, BG.ms, + (unsigned long long)BG.failed, (unsigned long long)BG.there, now_ms() - t_all); +} + +// The last boot prebuild worker starts the builder when it queued anything. +static void bg_start(void) { + if (!BG.n || LB.stop || BG.th) return; + if (!(BG.th = plat_thread_new(bg_main, NULL, "big compute"))) + rt_log("vk_pipeline: cannot start the background compute builder; %zu large compute shaders build at their first " + "dispatch", BG.n); +} + static void lb_compute(const GfxShader *cs, const uint8_t iid[32]) { struct { const GfxShader *cs; uint32_t compute; } k = {cs, 1}; uint64_t h = key_hash(&k, sizeof k); @@ -3331,7 +3620,8 @@ static void lb_compute(const GfxShader *cs, const uint8_t iid[32]) { gfx_shader_spirv(cs, &words); int big = LB.big_words && words > LB.big_words; // only from the caches (LB_BIG_WORDS) if (big && !P.cache_control) { - lb_defer(iid, words); + if (bg_on()) bg_queue(cs, iid, words); + else lb_defer(iid, words); return; } lb_lock(); @@ -3359,7 +3649,8 @@ static void lb_compute(const GfxShader *cs, const uint8_t iid[32]) { have = 1; } lb_unlock(); - if (!pipe && big) lb_defer(iid, words); + if (!pipe && big && bg_on()) bg_queue(cs, iid, words); + else if (!pipe && big) lb_defer(iid, words); else lb_count(2, pipe ? have : 2, ms); } @@ -3438,8 +3729,8 @@ static void lb_report(const char *how) { "%llu there; fragment %llu built (avg %.1f ms), %llu there; compute %llu built (avg %.1f ms), %llu there; driver " "cache only: vertex %llu (avg %.1f ms), fragment %llu (avg %.1f ms), compute %llu (avg %.1f ms); %llu failed, %llu not in the shader cache, " "%llu vertex/fragment skipped (graphics pipeline library off), %llu skipped (the driver crashed on them before, " - "shader-prepare-skip.txt), %llu large compute shaders left to their first dispatch (NX2NAVI_LIB_PREBUILD_BIG); " - "process working set %.0f MB", how, (unsigned long long)LB.done, LB.n, + "shader-prepare-skip.txt), %llu large compute shaders left to their first dispatch (NX2NAVI_LIB_PREBUILD_BIG), %zu " + "queued for the background builder (NX2NAVI_LIB_PREBUILD_BIG_BG); process working set %.0f MB", how, (unsigned long long)LB.done, LB.n, now_ms() - LB.t0, (unsigned long long)LB.built[0], LB.built[0] ? LB.ms[0] / LB.built[0] : 0.0, (unsigned long long)LB.have[0], (unsigned long long)LB.built[1], LB.built[1] ? LB.ms[1] / LB.built[1] : 0.0, (unsigned long long)LB.have[1], (unsigned long long)LB.built[2], LB.built[2] ? LB.ms[2] / LB.built[2] : 0.0, @@ -3448,7 +3739,7 @@ static void lb_report(const char *how) { (unsigned long long)LB.warmed[2], LB.warmed[2] ? LB.warm_ms[2] / LB.warmed[2] : 0.0, (unsigned long long)(LB.fail[0] + LB.fail[1] + LB.fail[2]), (unsigned long long)LB.noshader, (unsigned long long)LB.nolib, (unsigned long long)LB.skipped, - (unsigned long long)LB.deferred, (double)mem_proc); + (unsigned long long)LB.deferred, BG.n, (double)mem_proc); plat_mutex_unlock_shared(&LB.stat); } @@ -3483,7 +3774,11 @@ static void lb_worker(void *arg) { LB.done++; plat_mutex_unlock(&LB.stat); } - if (!plat_dec32(&LB.running)) lb_report(LB.stop ? "stopped" : "done"); + if (!plat_dec32(&LB.running)) { + lb_report(LB.stop ? "stopped" : "done"); + fb_log("after the stage library prebuild"); + bg_start(); + } return; } @@ -3551,6 +3846,7 @@ static void lb_stop(void) { for (int i = 0; i < LB.threads; i++) if (LB.th[i]) plat_thread_join(LB.th[i]), LB.th[i] = NULL; LB.threads = 0; + if (BG.th) plat_thread_join(BG.th), BG.th = NULL; // after its current build, if one runs } // Preparation: one prepared variant at a time into the driver's disk cache (private shader copy, nothing kept). arg is @@ -3857,6 +4153,12 @@ void vk_pipeline_save(void) { if (vkGetPipelineCacheData(P.device, P.cache, &size, NULL) == VK_SUCCESS && size && (data = malloc(size)) && vkGetPipelineCacheData(P.device, P.cache, &size, data) == VK_SUCCESS) { if (!plat_file_write_atomic(P.cache_path, data, size)) rt_log("vk_pipeline: cannot write %s", P.cache_path); + else if (P.feedback) { + char when[96]; + snprintf(when, sizeof when, "at a save of %.1f MB (%+.1f MB since the boot)", size / 1048576.0, + ((double)size - (double)P.cache_loaded) / 1048576.0); + fb_log(when); + } } free(data); plat_mutex_unlock(&save_lock); diff --git a/tools/build_xlate.py b/tools/build_xlate.py index 20536448..aaac0b69 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -101,14 +101,21 @@ def prepare_sources() -> Path: patches = sorted((XLATE / "patches").glob("*.patch")) if len(patches) != 35: sys.exit(f"expected 35 patches, found {len(patches)}") + # build/xlate is inside this checkout, and git inside a repository reads a "diff --git" patch's paths from the + # repository's top: those hunks fall outside the directory handed to --directory, git skips every file of the patch + # and still exits 0. 0021-prmt-byte-permute.patch is the only one written in that form, so it used to be skipped in + # silence and every byte permute stayed a no-op. GIT_CEILING_DIRECTORIES stops git from walking up to the checkout, + # so it reads the patch as it would outside a repository, and a "Skipped patch" line fails the build. + env = dict(os.environ, GIT_CEILING_DIRECTORIES=str(BUILD.parent), LC_ALL="C") for patch in patches: - # git apply works outside a repository when given a directory; --check first so a failure names the patch. for extra in (["--check"], []): r = subprocess.run( - ["git", "apply", *extra, "--whitespace=nowarn", "--directory", str(src.relative_to(BUILD)), str(patch)], - cwd=BUILD, capture_output=True, text=True, + ["git", "apply", *extra, "--verbose", "--whitespace=nowarn", + "--directory", str(src.relative_to(BUILD)), str(patch)], + cwd=BUILD, capture_output=True, text=True, env=env, ) - if r.returncode != 0: + skipped = "Skipped patch" in r.stdout or "Skipped patch" in r.stderr + if r.returncode != 0 or skipped: sys.exit(f"patch does not apply: {patch.name}\n{r.stdout}{r.stderr}") print(f"applied {patch.name}") return src diff --git a/tools/prof_report.py b/tools/prof_report.py index f982045e..a79bc370 100644 --- a/tools/prof_report.py +++ b/tools/prof_report.py @@ -18,13 +18,15 @@ import sys from pathlib import Path SYM = re.compile(r'^([0-9a-f]{8}) +[0-9a-f]{8} +\d+ {2,}(\S+)$') # In column empty: object lines have a path:(section) there +SYM_LD = re.compile(r'^\s*0x([0-9a-f]{8,16})\s+(\S+)\s*$') # a GNU ld map: the address, then the name, no size def load_map(path): syms = {} with open(path, encoding='utf-8', errors='replace') as f: for line in f: - m = SYM.match(line.rstrip('\n')) + line = line.rstrip('\n') + m = SYM.match(line) or SYM_LD.match(line) if m and '(' not in m.group(2): syms.setdefault(int(m.group(1), 16), m.group(2)) addrs = sorted(syms) @@ -96,16 +98,23 @@ def main(): prof = load_profile(a.profile) addrs, names = load_map(a.map) base = prof['base'] + # The map's addresses include the image base (0x140000000 for an x64 exe); a runtime Rip is an offset from wherever + # the loader put the image. The lowest map address aligned down to 64 KiB recovers that base, so a Rip becomes a map + # address by adding it back. + img_base = addrs[0] & ~0xFFFFF if addrs else 0 exe = next((m for m in prof['modules'] if m[0] == base), None) exe_end = base + exe[1] if exe else base def in_exe(rip): return base <= rip < exe_end + def sym_at(rip): + i = bisect.bisect_right(addrs, img_base + (rip - base)) - 1 + return names[i] if i >= 0 else '?' + def name_of(rip): if in_exe(rip): - i = bisect.bisect_right(addrs, rip - base) - 1 - return names[i] if i >= 0 else '?' + return sym_at(rip) m = next((m for m in prof['modules'] if m[0] <= rip < m[0] + m[1]), None) return f'{Path(m[2]).name}!+{rip - m[0]:#x}' if m else 'unknown' total = sum(n for _, n in prof['rips']) or 1 diff --git a/tools/xlate/AmpereLowering.cs b/tools/xlate/AmpereLowering.cs index 491ba5bc..83d2edb1 100644 --- a/tools/xlate/AmpereLowering.cs +++ b/tools/xlate/AmpereLowering.cs @@ -1594,8 +1594,9 @@ static class AmpereLowering // HFMA2.RELU (79) clamps each lane at zero where the predicate in bits 87-90 holds; only PT is lowered. Measured // on an NVIDIA GPU (tests/tensor_lowering_check.py): a negative result or -0 becomes +0, +inf stays, -inf // becomes 0, a NaN result stays NaN (0x7fff). The sum goes to TexOut and integer operations build the mask of - // the half lanes to clear. Maxwell PRMT would be the obvious tool, but the translator emits nothing for it - // (Instructions/InstEmit.cs: PrmtR only logs "not implemented"). + // the half lanes to clear. Maxwell PRMT would do that in one instruction and the translator implements it + // (xlate/patches/0021-prmt-byte-permute.patch, Instructions/InstEmitPermute.cs PrmtR); this keeps the integer + // form, which is what was measured. // F32 results (78), the H0_NH1 source (86) and absolute values where Maxwell has none are not lowered. HADD2's // second operand is B, or C for forms 2/3. void Half(ulong maxwell)