Skip to content

Optimization for powi(x, y) * x #69862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
k-arrows opened this issue Oct 22, 2023 · 3 comments · Fixed by #69998
Closed

Optimization for powi(x, y) * x #69862

k-arrows opened this issue Oct 22, 2023 · 3 comments · Fixed by #69998

Comments

@k-arrows
Copy link

Consider the following:

#include <cmath>

double f1(double x)
{
    return pow(x, 3) * pow(x, 1);
}

double f2(double x)
{
    return pow(x, 4);
}

(X86) https://godbolt.org/z/nd3ME13Po
(ARM) https://godbolt.org/z/z5vjnPhrK

On X86, if -Ofast is specified, GCC just calculates x * x * x * x for f1 with two mulsd, but Clang performs mulsd three times. There is no such difference on ARM.

@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2023

@llvm/issue-subscribers-backend-x86

Author: None (k-arrows)

Consider the following: ```cpp #include <cmath>

double f1(double x)
{
return pow(x, 3) * pow(x, 1);
}

double f2(double x)
{
return pow(x, 4);
}


(X86) https://godbolt.org/z/nd3ME13Po
(ARM) https://godbolt.org/z/z5vjnPhrK

On X86, if `-Ofast` is specified, GCC just calculates `x * x * x * x` for `f1` with two `mulsd`, but Clang performs `mulsd` three times. There is no such difference on ARM.
</details>

@dtcxzyw
Copy link
Member

dtcxzyw commented Oct 22, 2023

// pow(X, Y) * X --> pow(X, Y+1)
// X * pow(X, Y) --> pow(X, Y+1)
if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::pow>(m_Value(X),
m_Value(Y))),
m_Deferred(X)))) {
Value *Y1 =
Builder.CreateFAddFMF(Y, ConstantFP::get(I.getType(), 1.0), &I);
Value *Pow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, X, Y1, &I);
return replaceInstUsesWith(I, Pow);
}

powi(X, Y) * X --> powi(X, Y + 1) also works.
@vfdff I guess you are interested in this optimization :)

@dtcxzyw dtcxzyw changed the title Optimization for pow(x, i1) * pow(x, i2) (X86 vs ARM) Optimization for powi(x, y) * x Oct 22, 2023
@vfdff vfdff self-assigned this Oct 23, 2023
@vfdff
Copy link
Contributor

vfdff commented Oct 23, 2023

define double @f1(double noundef %x) {
  %p1 = tail call fast double @llvm.powi.f64.i32(double %x, i32 3)
  %mul = fmul fast double %p1, %x
  ret double %mul
}

declare double @llvm.powi.f64.i32(double, i32)
  • arm64: clang --target=aarch64 -march=armv8.3-a+sve -O3 -Ofast
f1: // @f1
  fmul d1, d0, d0
  fmul d1, d0, d1
  fmul d0, d1, d0
  ret

vfdff added a commit to vfdff/llvm-project that referenced this issue Oct 24, 2023
Try to transform the powi(X, Y) * X into powi(X, Y+1) with Ofast

For this case, when the Y is 3, then powi(X, 4) is replaced by
X2 = X * X; X2 * X2 in the further step.
Similar to D109954, who requires reassoc.

Fixes llvm#69862.
vfdff added a commit to vfdff/llvm-project that referenced this issue Mar 2, 2024
Try to transform the powi(X, Y) * X into powi(X, Y+1) with Ofast

For this case, when the Y is 3, then powi(X, 4) is replaced by
X2 = X * X; X2 * X2 in the further step.
Similar to D109954, who requires reassoc.

Fixes llvm#69862.
vfdff added a commit to vfdff/llvm-project that referenced this issue Mar 7, 2024
Try to transform the powi(X, Y) * X into powi(X, Y+1) with Ofast

For this case, when the Y is 3, then powi(X, 4) is replaced by
X2 = X * X; X2 * X2 in the further step.
Similar to D109954, who requires reassoc.

Fixes llvm#69862.
@vfdff vfdff closed this as completed in 0985202 Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants