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

Missed optimisation to take advantage of commutative SVE fmul instruction

Open ktkachov opened this issue 2 years ago • 6 comments

Example AArch64 SVE intrinsics code:

#include <arm_sve.h>

svfloat64_t svmul_x_2(svfloat64_t x, svfloat64_t y, svbool_t pg)
{
    return svmul_x(pg, svdup_f64(2), x);
}

This can generate just a single FMUL instruction but LLVM moves the immediate 2 into a register first. https://godbolt.org/z/11aEEGoce

ktkachov avatar Nov 11 '24 08:11 ktkachov

@llvm/issue-subscribers-backend-aarch64

Author: Kyrill Tkachov (ktkachov)

Example AArch64 SVE intrinsics code: ``` #include <arm_sve.h>

svfloat64_t svmul_x_2(svfloat64_t x, svfloat64_t y, svbool_t pg) { return svmul_x(pg, svdup_f64(2), x); }

This can generate just a singly FMUL instruction but LLVM moves the immediate 2 into a register first.
https://godbolt.org/z/11aEEGoce
</details>

llvmbot avatar Nov 11 '24 12:11 llvmbot

For input values #0.5 and #2.0, GCC generates the merged form of FMUL instruction as reported in this issue.

RamaKishanMalladi avatar Dec 03 '24 01:12 RamaKishanMalladi

@ktkachov , is there a perf penalty of not doing this optimization? Any measurements you have on the same? Thanks

RamaKishanMalladi avatar Dec 04 '24 11:12 RamaKishanMalladi

For 2.0 I'm not sure why we would not want to just use an add instead.

@ktkachov , is there a perf penalty of not doing this optimization? Any measurements you have on the same? Thanks

It helps avoid the need for the fmov 0.5 and might save a register, but otherwise should be similar.

davemgreen avatar Dec 11 '24 05:12 davemgreen

It helps avoid the need for the fmov 0.5 and might save a register, but otherwise should be similar.

Yes, I see that benefit. Thank you.

RamaKishanMalladi avatar Dec 11 '24 10:12 RamaKishanMalladi

Fixed by https://github.com/llvm/llvm-project/pull/134116. Whilst the PR is specific to multiply instructions, the intent is to expand the coverage to all the operations that can use the existing IR simplification logic.

paulwalker-arm avatar Apr 16 '25 16:04 paulwalker-arm