Repository navigation
agx, hk: address robust buffer accesses at the offset their bounds check tested - #559
Merged
RyanTheTide merged 2 commits intoNov 20, 2026
Merged
Conversation
…eck tested With robustBufferAccess or robustBufferAccess2, nir_lower_explicit_io checks each storage and uniform buffer access on its 32-bit byte offset, amul(index, stride) plus member offsets, and accesses base + u2u64(offset). The check tests that offset modulo 2^32. Address lowering instead takes amul as a product that does not wrap: the relaxed lea rules, the zero-extended constant fold, the index fold and the stride rescale in fold_byte_offset all rebuild the address from the unwrapped index. For an index whose product wraps, the address accessed then differs from the offset checked by a multiple of 4 GiB. amul's no-wrap promise holds only for a valid access, and an access the check admits need not be valid. Add agx_shader_key:: checked_offsets, which Honeykrisp sets when storage or uniform buffers are robust. agx_nir_lower_address then makes every 32-bit amul an imul before it fuses anything, so only the exact rules apply: an index is taken unwrapped at its scale only where range analysis proves the product cannot wrap (scale_is_exact), and otherwise as the product shifted back. The same holds for SSBO descriptor array indices, which are checked against the binding's size the same way. scale_is_exact no longer takes a no-wrap flag as that proof for checked offsets: the flags come from the shader's SPIR-V decorations, and a shader that breaks them still gets the checked offset. Shaders without robust buffers are unchanged. test-lower-address-checked-offsets.cpp lowers p + u2u64(offset) for offsets amul(x, d), amul(x, d) + c, amul(x + k, d) and amul(x + k, d) + c with strides 1-64, member offsets up to 0x7ff0 and k = -1, 1, 3, for 8- to 32-bit loads and stores and 32-bit atomics under every option set, and checks that each access names base plus the 32-bit offset for indices across 2^28-2^32. It also checks the forms above, that no-wrap flags do not move a checked access, and that a range-bounded index is still taken unwrapped at the stride's scale. Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
fold_byte_offset turns an index amul(i + c, stride) into i * stride plus the byte offset c * stride, with i * stride a sign-extended 32-bit product. For c < 0 that product exceeds the valid offset by up to the byte offset's 32 KiB range, so in the last 32 KiB of a 2 GiB buffer it can pass 2^31 and turn negative: a byte array of 3-byte records read at a[i - 1] = offset 0x7ffffffe went to 2 GiB below the buffer instead. The fold excluded c < 0 only for a byte stride, but the overflow depends on the scale, not the stride: where the access scales the index by 2 or more, the product stays below 2^30 + 2^14. Require c >= 0 at a byte scale instead. LowerAddressCheckedOffsets.ValidOffsetsKeepTheirAddressUnchecked lowers the same offset forms without checked offsets and checks every valid offset (below 2^31), including the last elements of a 2 GiB buffer. Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
funkzy
marked this pull request as ready for review
November 20, 2026 02:22
|
Lab row from the M4 Pro (J616s). A = aurora-wip e7e9ef3, B = A + this PR (d5ade7b) + #561. Native release builds, user-space overlay, same kernel:
An AGX2 row (M1 Pro or M2) is next. The x86 suite on the combined tree is running too. |
|
M1 Pro (G13S) row. A = aurora-wip 173d51d, B = A + #561 + #559 + #562. Native release builds, user-space overlay, same kernel (7.3.0-aurora-fleet74):
|
Author
|
@erikwb, the two commits here came from your report and follow the fix you suggested in it, and they were merged without saying so. That was an omission on our side. For the record: Reported-by and Suggested-by Erik Bourget (erikwb). Our other open pull requests from your reports now carry both trailers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With robust buffer access,
nir_lower_explicit_iochecks each storage and uniform buffer access on its 32-bit byte offset,amul(index, stride)plus member offsets. The check therefore tests the offset modulo 2^32. AGX address lowering treatsamulas a product that does not wrap, and rebuilds the address from the unwrapped index in four places:fold_byte_offset.When the product wraps, the address accessed then differs from the offset checked by a multiple of 4 GiB. This series makes robust accesses use exactly the checked offset, and fixes a related fold for byte accesses near 2 GiB.
Changes
agx_shader_key::checked_offsetsis set by Honeykrisp when storage or uniform buffers are robust.agx_nir_lower_addressturns every 32-bitamulintoimulbefore fusing, so only the exact rules (agx: take a shifted index at its scale only where the shift cannot wrap #379) apply: an index is taken unwrapped only where range analysis proves the product cannot wrap.scale_is_exactno longer treats the SPIR-V no-wrap decorations as that proof for these shaders.fold_byte_offsetexcluded negative addends only for a byte stride, but the overflow depends on the access's scale. A byte access in the last 32 KiB of a 2 GiB buffer could be addressed 4 GiB too low. This applies with or without robustness.Cost
Robust shaders lose some folded address modes. Instructions on the M5 profile, before → after:
uint[]indexed bygl_GlobalInvocationIDvec4[]loopgl_LocalInvocationIndexindices, whose range is proven.max_workgroup_countinagx_nir_options. Without it, range analysis cannot boundgl_GlobalInvocationID.Testing
test-lower-address-checked-offsets. For each offset form at strides 1-64, member offsets up to 0x7ff0, and 8- to 32-bit accesses and atomics, under every option set, it checks that the access is made at base plus the 32-bit offset.dEQP-VK on the Mac Studio
Run with and without the series, in the same boot. As with the test above, the series was applied on top of #362, which the M5 Ultra needs to run Vulkan. Each case gave the same result on both builds. There were no GPU faults.
robustness.buffer_access,robustness.pipeline_robustnessrobustness.robustness2, storage and uniform buffer casesssbo.layout,ssbo.unsized_array_lengthubospirv_assembly.instruction.compute: 8-bit storage, 16-bit storage, variable pointers