__viaddmax_s32 and __viaddmin_s32 return incorrect results on sm_80 when mixing constant INT_MIN with dynamic runtime arguments (CUDA 13.2)

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:

  1. sm_90+ Implementation: Uses explicit PTX assembly instructions (add.s32), which strictly perform two’s complement wrapping arithmetic on hardware registers.

  2. sm_80 Fallback 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 + b is rewritten (e.g., into b | 0x80000000 via or.b32 or 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

  1. Is this a bug? Is the divergence on sm_80 under mixed constant/runtime argument optimization considered a compiler/header bug?

  2. 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?

Thanks for the report. This is a bug and it will be fixed in the future release. Expected behavior is safe wraparound.

Thank you for your reply.All these test cases exhibit incorrect results only when constants are passed as arguments. Is it generally true that when arguments are supplied as variables and evaluated on the GPU at runtime, those runtime results serve as the reference correct values?

Runtime implementation may have bugs, just as you have pointed out in another report for Bessel functions: CUDA 13.2 Double-Precision Math Bugs on sm_80: Device Implementation Defects in jn/yn and Constant Folding Errors in llrint/lrint/copysign . So runtime in general cannot serve as a true reference in all cases. It is our best intention to fully specify the behavior in the documentation, and documentation shall be considered the source of truth.

Another example when runtime cannot be treated as a reference even in the absence of bugs: there are undefined behavior corner cases, those are typically called out in the C/C++ standards and other specifications. Runtime may provide you some result, but it is not guaranteed per language specification, so you cannot rely on it.