Skip to content

borealis,agx: close G17P bounded-UBO and dynamically-indexed sampler codegen gaps - #1246

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
perf/borealis-dxvk-ubo-border-opt
Nov 27, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
perf/borealis-dxvk-ubo-border-opt

Conversation

@DjDeveloperr

Copy link
Copy Markdown

What

Close two general Borealis / AGX3 shader codegen gaps on G17P (!soft_fault) that caused large instruction bloat and loop-load penalties on robust Vulkan / DXVK shaders:

  1. Recognize dynamically-indexed samplers in custom-border emulation elision (hk_nir_border_index.h):

    • hk_nir_border_address() previously matched only static base64 + imm64 descriptor offsets. Dynamically-indexed sampler descriptor arrays (used by DXVK's separate sampler heap) compute set_ptr + u2u64(dyn + imm32) (or zero-offset u2u64(dyn) / power-of-two shift-disjoint ior), causing hk_is_border_emulation_if() to reject every SampleLevel/txl sample and retain full custom-border emulation + border-swizzle preambles even when no custom-border sampler exists on the device.
    • Extend hk_nir_border_address() to match dynamically-indexed descriptor offsets with identical base, identical dynamic index, and exact field delta, with unit tests in test-border-index.cpp.
  2. Speculate, hoist before unrolling, and uniform-DMA bounded G17P buffer loads (hk_nir_lower_descriptors.c, hk_shader.c, agx_nir_opt_licm.c, agx_compile.c):

    • Preserve constant descriptor array indices through clamp_desc_array_bounds (MIN2(const_idx, array_size - 1) instead of an unfolded nir_umin that forced nir_def_is_const(index) to false on every robust shader) and attach ACCESS_BUFFER_DESCRIPTOR_AGX to statically-indexed, non-partially-bound dynamic buffer descriptors (UNIFORM_BUFFER_DYNAMIC / STORAGE_BUFFER_DYNAMIC), matching static UBO/SSBO descriptors.
    • Mark lower_load_global_bounded() and hk_lower_aligned_ubo() loads with ACCESS_CAN_SPECULATE when they are read-only, reorderable, non-coherent/non-volatile, guarded onto AGX_ZERO_PAGE_ADDRESS, and backed by an ACCESS_BUFFER_DESCRIPTOR_AGX descriptor. Also tighten bound_offset()'s zero-page constant check to offset + load_size <= AGX_ZERO_PAGE_SIZE.
    • Add agx_nir_opt_licm_direct_const_loads() (paralleling agx_nir_opt_licm_direct_sfu() under the same 56-half register-pressure cap) before agx_nir_unroll_runtime_loops() so loop-invariant speculatable constant loads and their bounds checks move out of loops before unroll-budget counting.
    • Extend agx3_uniform_dma_buffer() and agx3_vertex_preamble_only_forwards() to recognize G17P's bounded bcsel(valid, desc.xy, AGX_ZERO_PAGE_ADDRESS) form when valid is an exact bounds predicate against desc.z, promoting constant-offset robust UBO reads to AGX3_UNIFORM_DMA_BUFFER.
    • Add unit tests in test-agx3-feature-matching-opts.cpp.

Tested

  • Policy check: DEFAULT_BRANCH=aurora-wip ./.github/aurora/agent-policy.sh pr HEAD -> PR is clean.
  • Asahi unit suite (./rig/fedora-mesaci.sh 984f7df4658b920af15ca6597c54950d745c3d1d): 218/218 PASS (0 failures, 0 timeouts, 0 new warnings).
  • MacBook Neo (J700, Apple A18 Pro G17P B1, kernel 7.3.0-41) exact-commit same-boot ABBA+BAAB (base=4a868406592 vs head=984f7df4658, n=4 per arm, MESA_SHADER_CACHE_DISABLE=true, dxvk.enableStateCache = False):
    • gpubench-d3d11 compute.heavy kind=alu (d=4): 1463.79 us -> 1168.22 us (-20.19% / 1.253x, matching native Vulkan vk_alu 1196.17 us). Shader unrolls 4x (i201:g21 vs i142:g27), zero loop loads, all 3 UBO fields promoted to AGX3_UNIFORM_DMA_BUFFER.
    • gpubench-d3d11 render.postfx msaa=1,1440p (d=2): 1392.77 us -> 1351.39 us (-2.97%); all 3 fragment shaders match HK_PERF=noborder hashes bit-for-bit (postfx_down i113:g47 -> i57:g31, postfx_up i86:g47 -> i40:g31, postfx_tonemap i83:g39 -> i61:g31, 0 remaining CLAMP_TO_0 samples).
    • gpubench-d3d11 render.deferred VS (vs-fb492fb3): vertex software preamble eliminated via AGX3_UNIFORM_DMA_BUFFER.
    • gpubench-d3d11 compile.pso kind=graphics (count=32, clean cache): 2.67 ms -> 2.58 ms (-3.42%).
    • Native gpubench-vk controls: vk_alu 1193.32 -> 1196.17 us (+0.24%), vk_postfx1 996.02 -> 992.06 us (-0.40%), vk_fill 672.05 -> 673.86 us (+0.27%).
    • Bit-exact CRC parity (base == head) on all verified D3D11 and Vulkan cases.
  • Vulkan CTS on MacBook Neo (deqp-vk, head=984f7df4658):
    • dEQP-VK.robustness.robustness2.bind.notemplate.*.uniform_buffer*: 3456/3456 PASS (0 fail, 768 not supported).
    • dEQP-VK.robustness.robustness2.bind.notemplate.*.storage_buffer*: 13824/16896 PASS (0 fail, 3072 not supported).
    • dEQP-VK.ubo.random.scalar_types.*: 25/25 PASS (0 fail).
    • dEQP-VK.descriptor_indexing.uniform_texel_buffer*: 10/10 PASS (0 fail).
    • dEQP-VK.pipeline.monolithic.sampler.border_swizzle.r8g8b8a8_unorm.*: 948/1280 PASS, 112 pre-existing opaque_white.gather.*compute failures matching base=4a868406592 identically (948 pass / 112 fail / 220 not supported), 0 failures on all custom border and all graphics cases.
  • Device health: stock.sh PRE==POST (0 diffs), kfaults_delta=0, avc_delta=0 across all runs.

Risk

  • Low. ACCESS_CAN_SPECULATE is only attached to bounded loads guarded onto AGX_ZERO_PAGE_ADDRESS through statically-indexed, non-partially-bound, non-variable-count descriptors (ACCESS_BUFFER_DESCRIPTOR_AGX), preserving fault safety for partially-bound and dynamically-indexed descriptors.
  • agx3_uniform_dma_buffer() only accepts bounded loads whose predicate exactly matches [offset_B, offset_B + size_B) against desc.z for dword-aligned 32-bit+ loads, matching AGX3_UNIFORM_DMA_BUFFER's zero-fill contract.
  • hk_nir_border_address() requires matching descriptor set base, matching dynamic index expression, and exact field offset delta (clamp_0_sampler_index - unbiased_clamp_0_sampler_index).

Not yet

  • Fragment cbuffers bound through UPDATE_AFTER_BIND descriptor sets remain ineligible for AGX3_UNIFORM_DMA_BUFFER (by design in hk_static_set_mask()).
  • DXVK's compute.conv2d remaining gap vs native Vulkan stems from ZeroInitializeWorkgroupMemory adding a workgroup clear + barrier and DXVK's per-submit event-query vk-meta-fill-buffer dispatch/flush; dead shared-memory zero-init elision is left for follow-up work.

…elision

hk_nir_border_address() only matched static 64-bit descriptor offsets of
the form base + imm64. Dynamically-indexed sampler arrays (such as DXVK's
separate sampler descriptors) compute set_ptr + u2u64(dyn + imm32), so
hk_is_border_emulation_if() failed to match clamp_0_sampler_index against
unbiased_clamp_0_sampler_index and kept full custom-border emulation on
every SampleLevel/txl instruction even when no custom-border sampler
exists on the device.

Extend hk_nir_border_address() to match base + u2u64(dyn + imm32) (as well
as zero-offset u2u64(dyn) and power-of-two shift-disjoint ior folding) and
require identical base and dynamic index with the exact field delta. Add
positive and negative unit tests in test-border-index.cpp.

Signed-off-by: DjDeveloperr <hey@djdev.me>
…oads

On G17P (!soft_fault), robust buffer loads (used by every DXVK shader)
are lowered by lower_load_global_bounded() and hk_lower_aligned_ubo() to
a bounds check selecting AGX_ZERO_PAGE_ADDRESS when out of bounds. Three
gaps prevented these guarded loads from reaching native codegen parity:

1. hk_nir_lower_descriptors.c did not mark statically indexed,
   non-partially-bound dynamic buffer descriptors with
   ACCESS_BUFFER_DESCRIPTOR_AGX (unlike static UBO/SSBO descriptors).
2. lower_load_global_bounded() and hk_lower_aligned_ubo() omitted
   ACCESS_CAN_SPECULATE even when the underlying descriptor is guaranteed
   valid (or null with size == 0) and out-of-bounds accesses are clamped
   to AGX_ZERO_PAGE_ADDRESS. Constant loads inside loops stayed trapped
   in the loop body, preventing preamble hoisting and bloating the loop
   body past the 4x runtime-unroll threshold.
3. agx3_uniform_dma_buffer() and agx3_vertex_preamble_only_forwards()
   only matched unguarded pack_64_2x32_split(desc.x, desc.y) pointers,
   missing G17P's bounded bcsel(..., AGX_ZERO_PAGE_ADDRESS) form even
   though AGX3_UNIFORM_DMA_BUFFER already zero-fills past buffer.size.

Fix all three gaps, add pre-unroll direct-body LICM for speculatable
constant-load chains (agx_nir_opt_licm_direct_const_loads) under the same
56-half register-pressure budget as agx_nir_opt_licm_direct_sfu, and add
unit tests covering direct constant-load LICM and bounded uniform DMA.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 27, 2026 17:21

@RyanTheTide RyanTheTide 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.

Verdict: MERGE at 984f7df4658b920af15ca6597c54950d745c3d1d (code is ready; a row from a second GPU generation at this head is still owed before it can land, see below)

Blockers: none

Checked:

  • Read the whole diff of both commits and the body.
  • Border elision (hk_nir_border_index.h, caller hk_is_border_emulation_if). The dynamic match requires the same base scalar, the same dynamic scalar (has_dyn must agree on both sides) and the exact field delta. The ior form is only accepted when the other operand is ishl(x, s) with constant s < 32 and c < 2^s, so the bits really are disjoint and ior == iadd for every x. A 32-bit wrap of dyn + imm cannot cause a wrong elision. The match only identifies which branch is the emulation branch. Dropping the branch is still gated device-wide on pdev->border_emulation (any custom-border sampler created flips it, and the serialized twin is kept), so no dynamic index can reach a custom-border sampler in an elided shader. The only CLAMP_TO_0 samples come from agx_nir_lower_custom_border.
  • Bounded loads (hk_shader.c). The bounds check itself is unchanged: OOB still selects AGX_ZERO_PAGE_ADDRESS, and the offset is either clamped or a constant now checked as offset + load_size <= AGX_ZERO_PAGE_SIZE. Before, it was a fixed +16, which was wrong for 32-byte loads, so the new check is tighter and more correct. Speculation is only added for NON_WRITEABLE|CAN_REORDER, non-coherent/volatile loads whose base and size come from the same ACCESS_BUFFER_DESCRIPTOR_AGX load. That flag means a statically indexed, non-partially-bound, non-variable-count descriptor, which is valid whenever it is statically used. Null descriptors give size 0 and so go to the zero page. Unbound sparse pages map to the zeroed page. The new dynamic-buffer always_valid mirrors the existing static UBO/SSBO rule exactly. Descriptor buffers cannot hold dynamic buffers. The gating is agx_has_soft_fault() (DRM_ASAHI_FEATURE_SOFT_FAULTS) and the chip-info descriptor generation, so there is no raw chip-id compare.
  • Constant clamp fold in load_descriptor / load_image_handle: MIN2(const, array_size - 1) is the same value nir_umin would fold to.
  • LICM (agx_licm_filter_const_load). Same shape as the existing agx_licm_filter_sfu: dominate-exit invariants as before, plus direct-body chains that end in a CAN_SPECULATE load_global_constant. It refuses loops with terminate. Descriptor-only loads do not start a chain. Only loads that cannot fault are hoisted, so a zero-trip loop is safe. load_global (writable SSBO) is never a candidate, so no load moves past a store or barrier.
  • Uniform DMA (agx3_uniform_dma_is_bounded_check). All four accepted predicate forms are equivalent to off + size <= desc.size, with usub_sat covering size < load_size - 1. Condition, size word and address words must all come from the same descriptor load, and loads are dword-aligned and at least 4 bytes. A partially OOB vector load is zeroed per dword by DMA but whole-vector by the software path, which is fine under the robustness 16-byte rule.
  • Local drm-shim gate, head merged into aurora-wip ef132f5 (clean merge, tree eb79f6e5): 337/338. The only failure is hk_sync_stamps, the known #1235/#1213 clash that also fails on the base, so there are no new failures. The new BorderIndex.*, LicmDirectConstLoads* and BoundedDynamicUboPromotesToUniformDma tests pass.
  • Tests without the fix: I put agx_compile.c and hk_nir_border_index.h back to the base versions and kept the new tests. BorderIndex.MatchesOnlyRelatedImmutableDescriptorFields then fails 4 assertions (lines 60/63/66/77) and BoundedDynamicUboPromotesToUniformDma fails (lines 1158/1159). The LICM tests exercise the new pass itself.

Non-blocking:

  1. No unit test covers the speculation flags added in lower_load_global_bounded / hk_lower_aligned_ubo, or the dynamic-descriptor ACCESS_BUFFER_DESCRIPTOR_AGX. Only the CTS rows cover them.
  2. agx3_vertex_preamble_only_forwards counts the bounds-check ALU as free even when the condition also feeds a load that is not DMA'd. This only affects the cost heuristic, not correctness.

Rows at this head: J700 (G17P) is in the body (robustness2 uniform/storage buffer, ubo.random, descriptor_indexing texel buffer, border_swizzle). Missing: a second generation. The constant clamp fold is in shared descriptor lowering for every generation, and the gen-9 dynamic-descriptor flag is not G17P-only. A J713 row (G16G, gen 9) covering robustness.robustness2.buffer, ubo., binding_model descriptor indexing and pipeline.samplerborder would cover both.

Evidence
  • Merged gate: meson test --suite asahi --suite poly on aurora-wip ef132f5 + 984f7df -> Ok 337, Fail 1 (hk_sync_stamps, baseline).
  • Fix-reverted run (agx_tests --gtest_filter='BorderIndex.*:FeatureMatchingOpts.LicmDirect*:FeatureMatchingOpts.BoundedDynamic*'): 6 passed, 2 failed (BorderIndex.MatchesOnlyRelatedImmutableDescriptorFields, FeatureMatchingOpts.BoundedDynamicUboPromotesToUniformDma).

@RyanTheTide

Copy link
Copy Markdown

984f7df4658b J316s (M1 Pro, Apple M1 Pro / G13S C0, 7.3.0-aurora-fleet89) + J413 (M2 Air, Apple M2 / G14G B0, 7.3.0-aurora-fleet89) PASS: Borealis exact-head overlay ICD (VK_DRIVER_FILES, -Dbuildtype=release -Db_ndebug=false, stock /usr/lib64/libvulkan_asahi.so verified byte-identical before/after on both Macs); deqp-vk 20,768 PASS / 0 FAIL on J316s and 20,768 PASS / 0 FAIL on J413 (api.smoke.* 8/8, robustness.robustness2.*buffer* 1,5 2472/9312, ubo.* 1,5 1624/2478, pipeline.*sampler*border* 1,5 16664/51181); 0 GPU faults/recoveries, Enforcing, 0 failed units, 0 new AVC.

@RyanTheTide
RyanTheTide merged commit 78d817d into aurora-wip Nov 27, 2026
@RyanTheTide
RyanTheTide deleted the perf/borealis-dxvk-ubo-border-opt branch November 27, 2026 17:46
erikwb added a commit that referenced this pull request Nov 28, 2026
Resolve against #1291 (212c440) and #1246 (984f7df) in hk_lower_aligned_ubo(): keep the dynamically indexed vec4 element-bounds fast path behind #1291's upfront constant-bound inline-block check, propagate ubo_root_always_valid() / ACCESS_CAN_SPECULATE on the fast-path global constant load, reuse hk_lower_aligned_ubo_nir(), and update test-brl-aligned-ubo.c to call hk_lower_aligned_ubo_nir() and expect explicit I/O for constant-bound inline uniform blocks.

Signed-off-by: Erik Bourget <erik@ebourget.net>
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