HomePhabricator

[CUDA] Work around a bug in rint/nearbyint caused by a broken implementation…

Authored by tra on Aug 4 2020, 11:52 AM.

Description

[CUDA] Work around a bug in rint/nearbyint caused by a broken implementation provided by CUDA.

Normally math functions are forwarded to nv_* counterparts provided by CUDA's
libdevice bitcode. However,
nv_rint*()/__nv_nearbyint*() functions there have
a bug -- they use round() which rounds *up* instead of rounding towards the
nearest integer, so we end up with rint(2.5f) producing 3.0 instead of expected
2.0. The broken bitcode is not actually used by NVCC itself, which has both a
work-around in CUDA headers and, in recent versions, uses correct
implementations in NVCC's built-ins.

This patch implements equivalent workaround and directs rint*/nearbyint* to
__builtin_* variants that produce correct results.

Differential Revision: https://reviews.llvm.org/D85236