Fix BitShift UB when shift amount >= bit width#28272
Open
Conversation
Shifting by >= the bit width of an unsigned integer type is undefined behavior in C++. On x86, 64-bit shifts mask the shift amount to 6 bits, so shifting uint64 by 64 effectively shifts by 0, returning the original value instead of 0. Add SafeShiftLeft/SafeShiftRight helpers that return 0 when the shift amount >= the bit width, and use them in all three BitShift broadcast code paths (scalar-X, scalar-Y, and element-wise). Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/784db8bc-a08a-4d19-abcc-84a8421b4d05 Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix ONNX Runtime CPU BitShift for uint64 shift amount of 64
Fix BitShift UB when shift amount >= bit width
Apr 29, 2026
Contributor
|
@copilot, DirectML CI pipeline failed: |
DirectML has the same hardware-level shift masking behavior as the original CPU issue, so these tests are not applicable to DML. Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/92da631d-7a7f-4868-af90-510f7575710c Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Contributor
Author
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.
Description
Shifting by >= the bit width of an unsigned type is undefined behavior in C++. On x86-64, the hardware masks 64-bit shift amounts to 6 bits, so
x >> 64silently becomesx >> 0, returning the original value instead of 0.Added
SafeShiftLeft/SafeShiftRighthelpers that return 0 whenshift >= sizeof(T) * 8, applied across all three broadcast code paths (scalar-X, scalar-Y, element-wise).Added tests covering:
uint32_tanduint64_tMotivation and Context
BitShiftwithdirection="RIGHT"onuint64inputs with shift amount 64 returns the original values instead of zeros. Reproduces withCPUExecutionProviderandORT_DISABLE_ALL(constant folding masks the bug underORT_ENABLE_ALL).