Repository navigation
Fix ten PTX functional-simulation bugs (f16 rounding, 64-bit immediates, bfi/mad/mul.hi, conversions, signed loads, zero signs) - #154
Open
shatalov-alex wants to merge 10 commits into
Conversation
half.h and half.hpp were configured with HALF_ROUND_STYLE -1 (truncation) and HALF_ROUND_TIES_TO_EVEN 0 (ties away from zero). cvt.rn.f16.f32 therefore rounded ties away from zero, and f16 arithmetic truncated, while PTX .rn and NVIDIA hardware round to nearest even. Example: cvt.rn.f16.f32 of 1 + 2^-11 gave 0x3c01 instead of 0x3c00.
bfi_impl used 32-bit masks and values for both widths, so bfi.b64 lost every bit at position 32 and above. nvcc emits bfi.b64 to assemble 64-bit values; CUDA's sinf/cosf use it in their large-argument (|x| > 105615) reduction, which therefore returned 0.
mad_def multiplied two 32-bit operands in 32 bits before widening
(t.u64 = a.u32 * b.u32), so mad.wide.{u,s}32 and mad.hi.{u,s}32 lost the
upper half of the product; mul_impl already casts first. The addend of
mad.wide is now read at the doubled width as well.
Example: mad.wide.u32 d, 0xdeadbeef, 0xcafebabe, 0x100000000 gave
0x0000000088cf5b62 instead of 0xb092ab7c88cf5b62.
The PTX lexer stored integer literals in an int (atoi, sscanf %x), so immediates beyond 32 bits were truncated: mov.u64 %rd, 4294967296 loaded 0. nvcc emits such immediates for 64-bit constants, e.g. in CUDA's sinf/cosf argument reduction. Literals are now parsed and stored as 64-bit values; values that fit in 32 bits are unchanged.
In s2f and u2f the 64-bit branch converted y.s64 / y.u64, but y was never initialised there; the 32-bit branch copies the source first. cvt.rn.f32.s64, cvt.rn.f64.s64 and their unsigned forms returned garbage (often 0).
The shift amount of shl/shr is always a .u32 operand, but the 64-bit cases shifted by b.u64, which includes whatever the register's upper bits hold. shr.u64 d, 0xf000000000000000, 40 gave 0 instead of 0xf00000.
f2x converted through an int / unsigned int for every destination width, so cvt.rzi.s64.f32 and cvt.rzi.u64.f32 were clamped to the 32-bit range: 2^40 became 0x7fffffff (s64) or 0xffffffff (u64). 64-bit destinations now round by the instruction's mode and saturate to the 64-bit range, with NaN converting to 0, as PTX specifies.
ld_exec sign-extended only .s16 and .s32 scalar loads, so ld.global.s8 into a 16- or 32-bit register was zero-extended (a byte 0x8e read as 142, not -114). Vector loads read each element into an uninitialised ptx_reg_t, leaving stale upper bits, and never sign-extended. Elements are now zeroed before the read and signed elements are sign-extended.
mul.hi.u64 hit assert(!pI->is_hi()), and mul.hi.s64 (whose assertion is commented out) returned the low half of the product. nvcc emits both for 64-bit integer division and remainder by constants. They now return the upper 64 bits of the 128-bit product. The signed low product is computed in unsigned arithmetic to avoid signed-overflow undefined behaviour.
abs.f32/f64 used x < 0 ? -x : x, which returns -0.0 for -0.0, and neg.f16/f32/f64 computed 0 - x, which returns +0.0 for +0.0. On NVIDIA GPUs abs clears and neg flips the sign bit: abs(-0.0) is +0.0, neg(+0.0) is -0.0.
Author
|
Update: checked against real hardware. The reproducer in the description (unchanged, compiled to SASS for sm_75) passes every check on a Tesla T4 (driver 580.178.04, CUDA 12.6.3). So the expected values in the table are what the hardware produces, not only what the PTX spec says. On |
This was referenced Nov 13, 2026
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.
Ten independent fixes to the functional (PTX) simulation, one commit each. In every case GPGPU-Sim computed a different value from the one PTX specifies and NVIDIA GPUs produce. These are not timing-model changes.
I found them by running the same CUDA programs on GPGPU-Sim (
dev, CUDA 12.6,PTX_SIM_MODE_FUNC=1) and as C++ on the CPU, comparing outputs element by element, then reducing each mismatch to single instructions.cvt.rn.f16.f32, f16 arithmetichalf.h/half.hppconfigured for truncation and ties away from zerocvt.rn.f16.f32of 1 + 2^-11 gave0x3c01, not0x3c00bfi.b64sinf(200000.0f)returned 0 (large-argument reduction)mad.wide/.hi.{u,s}32mad.wide.u32 d, 0xdeadbeef, 0xcafebabe, 2^32gave0x0000000088cf5b62intmov.u64 %rd, 4294967296loaded 0cvt.{f32,f64}.{s,u}64cvt.rn.f64.s64gave 0shl/shr.b64,shr.s64shr.u64 d, 0xf000000000000000, 40gave 0cvt.r*i.{s,u}64.f320xffffffff(u64)ld.s8,ld.v*.s*0x8eloaded as 142, not -114mul.hi.{u,s}64abs.f32/f64,neg.f16/f32/f64x < 0 ? -x : xand0 - xabs(-0.0)gave -0.0;neg(+0.0)gave +0.0Several of these combine in nvcc's code for
sinf/cosfwith|x| > 105615:mov.u64of a 64-bit constant,mad.wide.u32,bfi.b64,cvt.rn.f64.s64. Before this PR that path returned 0. It now matches the CPU'ssinfwithin 1 ulp in the cases I checked.Testing
clang-format16.0.6 reports no changes.devthe program aborts inmul.hi.s64; with this PR every check passes.short-testsregressions (self-hosted runner).Building on aarch64 also needs #153, which drops x86-only flags from the AccelWattch Makefile, like #150 did for CMake.
Reproducer (
nvcc -O2 -fmad=false -cudart shared -gencode arch=compute_75,code=compute_75, run withPTX_SIM_MODE_FUNC=1)