fix(HwSort): update sorting logic to prioritize valid elements#148
Merged
Conversation
…lete stability guarantees
linjuanZ
approved these changes
May 18, 2026
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.
1
HwSort only swapped two entries when both were valid. An invalid entry could block valid entries behind it, so valid entries were not guaranteed to be packed together or sorted.
Example:
in0: valid, ptr = 10
in1: invalid, ptr = X
in2: valid, ptr = 5
The old logic could keep this order:
valid ptr=10, invalid, valid ptr=5
Solution: change the base 2-entry compare-swap to valid-first ordering:
val swap = xVec(1).valid && (!xVec(0).valid || cmp(xVec(1).ptr, xVec(0).ptr))New behavior: valid entries are moved to the front and sorted by cmp. Equal keys and invalid entries are not guaranteed to be stable.
2
The old 4-way final-stage wiring was incorrect: it compared (0,1) but wrote the result to (1,2), and compared (2,3) but wrote the result to (0,3).
This could corrupt even already sorted inputs:
input = [0, 1, 2, 3]
output = [2, 0, 1, 3]
The fix writes each compare-swap result back to its matching lanes:
tmp3_1 -> row3(0), row3(1)
tmp3_2 -> row3(2), row3(3)