Hi CUDA Team and Community,
I encountered a correctness issue in CUDA 13.2 when using the SIMD add-extrema intrinsic family (__viaddmax_s32, __viaddmax_s32_relu, __viaddmin_s32, __viaddmin_s32_relu) targeting sm_80 (e.g., NVIDIA A30) with nvcc -O3.
When a is a compile-time constant set to INT_MIN (-2147483648) and b is a dynamic runtime variable evaluated to -1, the addition a + b produces unexpected values instead of following expected 32-bit two’s complement wrapping arithmetic (INT_MIN + (-1) == INT_MAX).
Environment & Hardware
-
CUDA Toolkit: CUDA 13.2
-
Target Architecture:
sm_80(NVIDIA A30 ) -
Compiler Options:
nvcc -O3 -arch=sm_80
Root Cause Analysis
Inspecting crt/device_functions.hpp in CUDA 13.2 reveals a divergence between architecture implementations:
-
sm_90+Implementation: Uses explicit PTX assembly instructions (add.s32), which strictly perform two’s complement wrapping arithmetic on hardware registers. -
sm_80Fallback Implementation: Uses plain C++ fallback expressions:C++
// Abstracted fallback logic in crt/device_functions.hpp for sm_80 return max(a + b, c);
Because C++ signed integer overflow is Undefined Behavior (UB) according to the language standard, when a is a compile-time constant INT_MIN and b is dynamic, NVCC’s optimization pass assumes signed overflow cannot happen. Consequently:
-
The expression
INT_MIN + bis rewritten (e.g., intob | 0x80000000viaor.b32or constant-folded incorrectly). -
This eliminates the two’s complement wraparound (
INT_MIN + (-1) -> 2147483647), causing the intrinsics to output incorrect values.
Note that when all operands are dynamic runtime arguments, full constants, or when targeting sm_90+, the execution path avoids this optimization bug and yields correct wrapping results.
Godbolt Link:Compiler Explorer
Questions for NVIDIA
-
Is this a bug? Is the divergence on
sm_80under mixed constant/runtime argument optimization considered a compiler/header bug? -
Normative Behavior: What is the normative, canonical result specified for these CUDA intrinsic functions when signed integer overflow occurs? Should developers strictly expect 32-bit two’s complement wrapping semantics (
INT_MIN + (-1) == INT_MAX) across all GPU target architectures and compilation modes?