Skip to content

asahi/compiler: don't let the post-opt NIR capture keep stack options - #1264

Merged
RyanTheTide merged 1 commit into
aurora-wipfrom
fix/1245-postopt-nir-options
Nov 27, 2026
Merged

RyanTheTide merged 1 commit into
aurora-wipfrom
fix/1245-postopt-nir-options

Conversation

@RyanTheTide

Copy link
Copy Markdown

Follow-up to #1245, which merged as a0164c9 with a use-after-scope.

agx_compile_shader_nir_internal() points nir->options at its stack-local agx3_options (agx_compile.c:19768/19911) and restores the caller's options only at line 20031. The new post-agx_optimize_nir capture (*post_opt_nir_out = nir_shader_clone(NULL, nir), line 20004) clones inside that window. So hk_shader::g17p_mono_postopt_nir->options points into a dead stack frame, and hk_g17p_inline_epilog_part() / agx_compile_shader_nir_postopt() later read whatever is left there.

Seen on the tip: hk-g16s-layered-depth aborts in nir_validate ("NIR validation failed after nir_opt_copy_prop in hk_cmd_draw.c:9734", validate_float_mul_add) on the vk_meta copy-buffer-to-image fragment whenever the shader cache is cold. That makes the drm-shim suite flaky: it passes once the cache is warm or the pipeline was cached by an earlier test. Release builds skip validation and compile with garbage lowering options instead.

The fix gives the clone the caller's options, which is what the deserialize path and agx_compile_shader_nir_postopt() already expect. test_postopt_fs_inline_equivalence now also checks that the capture keeps the input options.

Tested:

  • x86 drm-shim gate (-Dbuildtype=release -Db_ndebug=false, --suite asahi --suite poly) at this head: 339/339.
  • g17p-vulkan-layered-depth --shim --expect-compressed (ASAHI_SHIM_CHIP=g16s), each run with a fresh empty shader cache: tip a0164c9 aborts 20/20; this branch 0/30; pre-asahi,borealis: speed up cold AGX3/Borealis pipeline compilation #1245 c9ec6b9 0/20.
  • agx3_pso_compile_fastpath with only the test change: FAIL ("postopt capture kept transient compiler options"); with the fix: PASS.

Risk: low. It only changes the options pointer of the in-memory capture. The capture is used only for the 1x non-ALU monolithic epilog path from #1245, and that path now runs with the same options as the deserialized path.

agx_compile_shader_nir_internal() points nir->options at its stack-local
agx3_options for the duration of the compile and restores the caller's
options only after the post-optimization passes. The post-agx_optimize_nir
capture added for monolithic fragment reuse cloned the shader inside that
window, so hk_shader::g17p_mono_postopt_nir kept a pointer into a dead
stack frame. Pre-linking then validated and compiled the clone with
whatever was left on the stack: debug builds abort in nir_validate
(float_mul_add16/32 checks) on a cold shader cache, e.g. the vk_meta
copy-buffer-to-image fragment in hk-g16s-layered-depth, and release
builds can compile with garbage lowering options.

Give the clone the caller's options, which is what the deserialized
path and agx_compile_shader_nir_postopt() expect.

Fixes: 0d342cb ("asahi/compiler,nir: skip no-op NIR passes and optimize ILP scheduler bitsets")
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that nir_shader_clone preserves the input options pointer and that the capture runs before the compiler restores original_options. Resetting the clone pointer to original_options matches the Vulkan deserialize path, which uses the persistent agx_nir_options table. The pointer-identity test directly catches the lifetime bug, and the reported cold-cache control and 339/339 host gate cover the observed regression. Code approved at this commit. Record the applicable landing checks and revert sequence in the merge decision.

@RyanTheTide

Copy link
Copy Markdown
Author

Exact-head 88d306bc00e5 2-gen Borealis cold-cache peer rows (J316s + J700): PASS

  • Build: native aarch64 Clang 23.1.4 (-Dbuildtype=release -Db_ndebug=false -Dtools=drm-shim), overlay libvulkan_asahi.so SHA-256 4b3e86311ac2362dd4f2d0c436856123db042d306eb8cf9f23834bcb845a6c83 via isolated VK_DRIVER_FILES (stock /usr/lib64/libvulkan_asahi.so untouched, verified pre/post on both Macs). All runs executed with MESA_SHADER_CACHE_DISABLE=1.
  • J316s (M1 Pro, G13S C0, 7.3.0-aurora-fleet89):
    • MESA_SHADER_CACHE_DISABLE=1 meson test --suite asahi: 261/261 PASS (asserts enabled, incl. agx3_pso_compile_fastpath 0.01s, hk-g16s-layered-depth 0.08s, agx_tests 7.94s).
    • Vulkan CTS (MESA_SHADER_CACHE_DISABLE=1): 2,759 PASS / 0 FAIL (api.smoke 8/8, pipeline.monolithic.depth.* 2,751/8,521 [5,770 NS]); 0 GPU faults/recoveries, Enforcing, 0 failed units, 0 new AVC.
  • J700 (Neo, G17P B1, 7.3.0-aurora-fleet89):
    • Vulkan CTS (MemoryMax=3G, MESA_SHADER_CACHE_DISABLE=1): 2,762 PASS / 0 FAIL (api.smoke 8/8, pipeline.monolithic.depth.* 2,754/8,521 [5,767 NS]); 0 GPU faults/recoveries, Enforcing, 0 failed units, 0 new AVC.

@RyanTheTide

Copy link
Copy Markdown
Author

This fixes a regression on aurora-wip from #1245: the G17P monolithic epilog's saved shader keeps a dangling options pointer, so hk-g16s-layered-depth aborts on a cold cache (tip a0164c9 onwards: 338/339) and release builds can compile that path with garbage options. Cold-cache rows at this head passed on J316s and J700 (above), and the independent App review approved it, but the ruleset needs an approval from a human reviewer. @DjDeveloperr (your #1245) or @satchlj: could one of you approve 88d306b so it can merge?

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and verified exact head 88d306bc00e5 on Apple M5 Max (J775c, G17C B1).

Code review:

  • Confirmed agx_compile_shader_nir_internal() sets nir->options = &agx3_options (stack-local, line 19911) and only restores nir->options = original_options at line 20031, so cloning into *post_opt_nir_out at line 20004 left a dangling stack pointer. Resetting (*post_opt_nir_out)->options = original_options directly after nir_shader_clone() cleanly fixes the use-after-return, and test_postopt_fs_inline_equivalence directly locks down the pointer-identity invariant.

Exact-head 88d306bc00e5 verification on Apple M5 Max (J775c, G17C B1):

  • Build (-Dbuildtype=release -Db_ndebug=false -Dbuild-tests=true -Dtools=asahi,drm-shim): MESA_SHADER_CACHE_DISABLE=1 MALLOC_PERTURB_=42 meson test --suite asahi --suite poly 342/342 PASS (0 fail — clears all 5 cold-cache nir_validate validate_float_mul_add aborts seen on aurora-wip a0164c9101c: hk-g17p-bind-tail-state-change, hk-g16g-indirect-multi, hk-g17p-indirect-multi-stream, hk-g15s-indirect-multi, hk-g17p-indirect-multi, plus agx3_pso_compile_fastpath and hk-g16s-layered-depth).
  • Live Borealis overlay ICD (MESA_SHADER_CACHE_DISABLE=1): dEQP-VK.api.smoke.* 8/8 PASS, dEQP-VK.pipeline.monolithic.depth.format.d16_unorm.* 224/471 PASS (247 NotSupported, 0 fail) — 232 PASS / 0 FAIL / 0 GPU faults.

LGTM, approved!

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