llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[VectorCombine] failed to fold fptoui(fdiv(uitofp(x),uitofp(y))) into udiv(x,y) when target has native vector udiv

Open Andarwinux opened this issue 5 months ago • 6 comments

https://godbolt.org/z/eafrhETE3 ref: https://github.com/llvm/llvm-project/issues/205305#issuecomment-4787165610

typedef unsigned int u __attribute__((__vector_size__(16)));
typedef double d __attribute__((__vector_size__(32)));

u a(u x, u y){
    d a = __builtin_convertvector(x, d);
    d b = __builtin_convertvector(y, d);
    d r = a / b;
    return __builtin_convertvector(r, u);
}

u b(u x, u y){
    return x / y;
}
a:
        ushll   v2.2d, v0.2s, #0
        ushll   v3.2d, v1.2s, #0
        ushll2  v0.2d, v0.4s, #0
        ushll2  v1.2d, v1.4s, #0
        ucvtf   v2.2d, v2.2d
        ucvtf   v3.2d, v3.2d
        ucvtf   v0.2d, v0.2d
        ucvtf   v1.2d, v1.2d
        fdiv    v2.2d, v2.2d, v3.2d
        fdiv    v0.2d, v0.2d, v1.2d
        fcvtzu  v1.2d, v2.2d
        fcvtzu  v0.2d, v0.2d
        uzp1    v0.4s, v1.4s, v0.4s
        ret

b:
        ptrue   p0.s, vl4
        udiv    z0.s, p0/m, z0.s, z1.s
        ret

This is particularly important for vector cases, as u32->f64->u32 will halve the throughput.

Andarwinux avatar Jun 24 '26 08:06 Andarwinux

Hello @Andarwinux, I'm working on it.

Xylecrack avatar Aug 01 '26 17:08 Xylecrack

For testing, Below is the IR equivalent of the example given in the issue

define <4 x i32> @test<4 x i32> %x, <4 x i32> %y) {
entry:
  %a = uitofp <4 x i32> %x to <4 x double>
  %b = uitofp <4 x i32> %y to <4 x double>
  %r = fdiv <4 x double> %a, %b
  %res = fptoui <4 x double> %r to <4 x i32>
  ret <4 x i32> %res
}

Since %y (divisor) is not isKnownNonZero the fold should not occur. So is the fold is valid only for constant/splat vectors? @RKSimon

Xylecrack avatar Aug 06 '26 06:08 Xylecrack

Looks like it can't be implemented in VectorCombine, but it can be implemented in DAG/Backend? For aarch64, since udiv returns 0 for division by zero, it meets the langref definition of fptoui.

https://llvm.org/docs/LangRef.html#fptoui-to-instruction

The ‘fptoui’ instruction converts its floating-point operand into the nearest (rounding towards zero) unsigned integer value. If the value cannot fit in ty2, the result is a poison value.

The ‘fptosi’ instruction converts its floating-point operand into the nearest (rounding towards zero) signed integer value. If the value cannot fit in ty2, the result is a poison value.

Andarwinux avatar Aug 06 '26 06:08 Andarwinux

So is the fold is valid only for constant/splat vectors?

Its valid for any case that can prove the divisor isn't zero - constants are the easiest of course, but value tracking can often determine non-constant cases as well.

I expect this fold will rely on a lot of value tracking tbh.....

RKSimon avatar Aug 06 '26 06:08 RKSimon

Looks like it can't be implemented in VectorCombine, but it can be implemented in DAG/Backend? For aarch64, since udiv returns 0 for division by zero, it meets the langref definition of fptoui.

At VectorCombine level, the fold might still be useful if divisor is isKnownNonZero?

Xylecrack avatar Aug 06 '26 18:08 Xylecrack

At VectorCombine level, the fold might still be useful if divisor is isKnownNonZero?

Yes, it is still useful as a canonicalization.

For example, although vector integer division on x86 backend uses FP division, it can attempt to shrink the FP division to a narrower version. However, users who do this in C code cannot benefit from this optimization. If VectorCombine could perform pre-canonicalization, this could be resolved.

Andarwinux avatar Aug 06 '26 19:08 Andarwinux