cmov: use black_box in portable impl
#1255
Merged
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.
The core predication function of the portable implementation is called
is_non_zeroand it returns a 1-bit mask value which is subsequently used to "select" between two values using bitwise masking.In some previous real-world cases in the Rust compiler, it has been able to deduce, when using this sort of pattern in a loop, that the entire loop can be skipped when e.g. the condition is zero. RUSTSEC-2024-0344 is one such example.
Adding a
black_boxin this position should hopefully help hint to the compiler that we do not want such an "optimization" performed.Note that the only thing we're actually relying on from
black_boxfor program correctness is that it behaves as an identity function, so this will not have deleterious effects. Whether it actually helps avoid problems like RUSTSEC-2024-0344 is something that can only be seen in practice, unfortunately, as we don't have any way to get actual guarantees out of the Rust compiler in cases where we can't useasm!.