[Runtime] Fix shift-count-overflow in next_power_of_two on 32-bit targets (#25142) On 32-bit targets, iree_host_size_t and iree_device_size_t are only 32 bits wide. The `value |= value >> 32;` statement in iree_host_size_next_power_of_two() and iree_device_size_next_power_of_two() then fails to compile with -Werror=shift-count-overflow. This happens even though the code is inside an `if (sizeof(...) == 8)` branch that never runs on those targets: the compiler still checks it. This change splits the 32-bit shift into two 16-bit shifts, `(value >> 16) >> 16`. The result is the same on 64-bit targets, and no shift is ever as wide as the type on 32-bit targets, so the warning goes away. Fixes #25134 Assisted-by: Claude Opus 5.5 Signed-off-by: Moritz Riesterer <moritz.riesterer@nxp.com>
diff --git a/runtime/src/iree/base/alignment.h b/runtime/src/iree/base/alignment.h index 49a8798..44575b5 100644 --- a/runtime/src/iree/base/alignment.h +++ b/runtime/src/iree/base/alignment.h
@@ -133,7 +133,10 @@ value |= value >> 8; value |= value >> 16; if (sizeof(iree_host_size_t) == 8) { - value |= value >> 32; + // Shift by 32 as two shifts of 16: on 32-bit targets iree_host_size_t + // is 32 bits wide and "value >> 32" triggers -Werror=shift-count-overflow + // even though this branch is never taken. + value |= (value >> 16) >> 16; } return value + 1; } @@ -181,7 +184,10 @@ value |= value >> 8; value |= value >> 16; if (sizeof(iree_device_size_t) == 8) { - value |= value >> 32; + // Shift by 32 as two shifts of 16: on 32-bit targets iree_device_size_t + // is 32 bits wide and "value >> 32" triggers -Werror=shift-count-overflow + // even though this branch is never taken. + value |= (value >> 16) >> 16; } return value + 1; }