The curious case of the missing multiply-by-2

As I recall, with the second iteration of the Maxwell architecture (sm_52) an optional scale factor 2k was incorporated into the FMUL instruction, for k of small magnitude. In examining some code the other day where the SASS consisted of one more instruction than I had expected, I noticed that this curiously does not seem to apply to a scale factor of 2. Here is a worked example for sm_89 compiled with CUDA 13.3:

__device__ float fmul_m2 (float a, float b) { return a * b * 2.0f; }
__device__ float fmul_m4 (float a, float b) { return a * b * 4.0f; }
__device__ float fmul_m8 (float a, float b) { return a * b * 8.0f; }
__device__ float fmul_d2 (float a, float b) { return a * b * 0.5f; }
__device__ float fmul_d4 (float a, float b) { return a * b * 0.25f; }
__device__ float fmul_d8 (float a, float b) { return a * b * 0.125f; }

The resulting code is as expected, except for fmul_m2():

fmul_m2(float, float):
 FMUL R3, R4, R5 
 FFMA R4, R4, R5, R3 
 RET.ABS.NODEC R20 0x0 

fmul_m4(float, float):
 FMUL.M4 R4, R4, R5 
 RET.ABS.NODEC R20 0x0 

fmul_m8(float, float):
 FMUL.M8 R4, R4, R5 
 RET.ABS.NODEC R20 0x0 

fmul_d2(float, float):
 FMUL.D2 R4, R4, R5 
 RET.ABS.NODEC R20 0x0 

fmul_d4(float, float):
 FMUL.D4 R4, R4, R5 
 RET.ABS.NODEC R20 0x0 

fmul_d8(float, float):
 FMUL.D8 R4, R4, R5 
 RET.ABS.NODEC R20 0x0

Some participants in this forum seem to have studied the GPU machine code instructions in some detail, so I figured I would bring this up here. I have a hard time imagining that NVIDIA structured the instruction encodings such that the common case of multiplication by 2 did not fit into the bit field presumably used. I assume that this is a 3-bit field, and I recognize the possibility that some of the 8 encodings might be used for other functionality, but if the GPU architects ran out of encodings, why remove the multiplication by 2, which I would think is more frequent than multiplication by 0.125?

Assuming the hardware actually supports it and that this is the result of a compiler issue, it would seem similar curious that the compiler recognized other cases but missed the common multiply-by-2.

I did an internet search to see if anybody else had noticed this anomaly before but could not find any mention of it. I am genuinely puzzled as to what may be going here.

There seems to be a compiler pass in nvcc which replaces multiplication by two with addition (in PTX). ptxas later tries to fold 2k as a scale factor into FMUL, but does not recognise the addition pattern.

Since the difference is in the generated PTX, this works to produce FMUL.M2:

__device__ float mul3(float a, float b, float c) { 
    asm("mul.f32 %0, %0, %1; mul.f32 %0, %0, %2;":"+f"(a):"f"(b), "f"(c)); 
    return a; 
}
__device__ float fmul_m2 (float a, float b) { return mul3(a, b, 2.0f); }

Thanks for the analysis. This seems to be a classical “impedance mismatch” between the front end and the back end of the compiler then. I had been under the impression that this class of issues had been eliminated years ago (after being a major annoyance in the early years of CUDA), but apparently not.

[Later:]

The following workaround based on device intrinsics also seems to work:

__device__ float fmul_m2 (float a, float b)
{
    return __fmul_rn (a * b, 2.0f);
}

I am pondering whether the current translation produced by the CUDA compiler, i.e. fmul_m2 (a, b) := fmaf (a, b, a * b) can produce unexpected (surprising) results, because a * b incurs one rounding, while the following multiplication by 2.0f is exact, for a total of one rounding error, whereas the FMUL / FFMA sequence produced by CUDA incurs two roundings.

[even laters]

At least experimentally, the transformation FMUL (FMUL (a, b), 2) ==> FFMA (a, b, FMUL (a, b)) appears to deliver bit-identical answers. That is not a proof of course.

With the exact magnitude Mprod = abs(a * b) = Mtruncate + Mrest, the rounded absolute result of FMUL is either Mtruncate or (Mtruncate + 1 ULP). Let’s write that Mprod_r = Mtruncate + x.

Then a multiply-add (a * b) + Mprod_r without rounding would produce Mmad = 2 * Mtruncate + x + Mrest
I believe this should round the same way as the first MUL when using the same rounding mode.

Given that we have convinced ourselves that the transformation is valid, it seems the only thing needed is for ptxas to apply the transformation FFMA (a, b, FMUL (a, b)) ==> FMUL.M2 (a, b). And likewise, FADD (FMUL (a, b), FMUL (a, b)) ==> FMUL.M2 (a, b), FMUL (FMUL (a, b), 2) ==> FMUL.M2 (a, b).