[llvm] AMDGPU: Libcall expand fast pow/powr/pown/rootn for float case (PR #180553)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 9 07:59:35 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
This is to eliminate the special case global unsafe math options
in these functions from the library. The core operation only
uses about 4 instructions, and then there's an additional prolog
and/or epilog to fixup special cases.
I have an alternative patch which implements this by using separate
entrypoints in the library, and having the pass replace the calls
instead of this full handling. However, given the unfortunate state
of library development, it requires a full year to make cross project
changes. This is the most expedient path to deleting the control library;
in the future we can do libcall emission when compiler has the real
ability to properly emit new calls.
This is mostly a direct port of these functions:
https://github.com/ROCm/llvm-project/blob/amd-staging/amd/device-libs/ocml/src/powF_base.h
I used copilot to do the heavy lifting on the drudgery of writing out
all the IRBuilder calls. It mostly worked, though it made mistakes in
porting != (used the ONE instead of UNE), plus some API usage errors.
The exact output isn't exactly the same. The ordering of some instructions
is different and some conditions and selects are inverted. This expansion
also preserves the flags, so the finite only case optimizes better than
with the library code (this is largely because fast math flags aren't propagated
yet, except for the few places SimplifyDemandedFPClass is called).
Alive2 verifies the library function with the special fast math path
functions have the same behavior as the result of this expansion. afn
only case: https://alive2.llvm.org/ce/z/uoPBC2
I did not bother trying to specially optimize the finite only cases here.
They get cleaned up by instcombine anyway, so doing so would only be a
hypothetical compile time improvement for a lot of additional complexity.
The exception would be existing, overlapped code in the pass.
There's some overlap here with existing code in the pass. The afn+ninf+nnan
case for powr already does the rewrite to the 4 instruction core sequence,
which takes precedence over this. In the future this should be merged,
but it's more annoying now since the old path also handles it for the f64
case via libcall emission since the intrinsic won't work.
---
Patch is 265.35 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/180553.diff
7 Files Affected:
- (modified) llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp (+285-3)
- (modified) llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow-codegen.ll (+45-4)
- (modified) llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow.ll (+1038-56)
- (modified) llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pown.ll (+77-8)
- (modified) llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-powr.ll (+737-92)
- (modified) llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-rootn.ll (+326-36)
- (modified) llvm/test/CodeGen/AMDGPU/simplify-libcalls.ll (+15-3)
``````````diff
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp b/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp
index 4de9349fe5166..d0c13d154995d 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp
@@ -19,6 +19,7 @@
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/IntrinsicsAMDGPU.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/PatternMatch.h"
#include <cmath>
@@ -43,6 +44,8 @@ static cl::list<std::string> UseNative("amdgpu-use-native",
#define MATH_SQRT2 numbers::sqrt2
#define MATH_SQRT1_2 numbers::inv_sqrt2
+enum class PowKind { Pow, PowR, PowN, RootN };
+
namespace llvm {
class AMDGPULibCalls {
@@ -69,6 +72,9 @@ class AMDGPULibCalls {
// pow/powr/pown
bool fold_pow(FPMathOperator *FPOp, IRBuilder<> &B, const FuncInfo &FInfo);
+ /// Peform a fast math expansion of pow, powr, pown or rootn.
+ bool expandFastPow(FPMathOperator *FPOp, IRBuilder<> &B, PowKind Kind);
+
// rootn
bool fold_rootn(FPMathOperator *FPOp, IRBuilder<> &B, const FuncInfo &FInfo);
@@ -710,13 +716,35 @@ bool AMDGPULibCalls::fold(CallInst *CI) {
}
}
- return fold_pow(FPOp, B, FInfo);
+ if (fold_pow(FPOp, B, FInfo))
+ return true;
+
+ if (!FMF.approxFunc())
+ return false;
+ return expandFastPow(FPOp, B, PowKind::Pow);
}
case AMDGPULibFunc::EI_POWR:
+ if (fold_pow(FPOp, B, FInfo))
+ return true;
+ if (!FMF.approxFunc())
+ return false;
+ if (!shouldReplaceLibcallWithIntrinsic(CI))
+ return false;
+ return expandFastPow(FPOp, B, PowKind::PowR);
case AMDGPULibFunc::EI_POWN:
- return fold_pow(FPOp, B, FInfo);
+ if (fold_pow(FPOp, B, FInfo))
+ return true;
+ if (!FMF.approxFunc())
+ return false;
+ if (!shouldReplaceLibcallWithIntrinsic(CI))
+ return false;
+ return expandFastPow(FPOp, B, PowKind::PowN);
case AMDGPULibFunc::EI_ROOTN:
- return fold_rootn(FPOp, B, FInfo);
+ if (fold_rootn(FPOp, B, FInfo))
+ return true;
+ if (!FMF.approxFunc())
+ return false;
+ return expandFastPow(FPOp, B, PowKind::RootN);
case AMDGPULibFunc::EI_SQRT:
// TODO: Allow with strictfp + constrained intrinsic
return tryReplaceLibcallWithSimpleIntrinsic(
@@ -1170,6 +1198,260 @@ bool AMDGPULibCalls::fold_rootn(FPMathOperator *FPOp, IRBuilder<> &B,
return false;
}
+// is_integer(y) => trunc(y) == y
+static Value *emitIsInteger(IRBuilder<> &B, Value *Y) {
+ Value *TruncY = B.CreateUnaryIntrinsic(Intrinsic::trunc, Y);
+ return B.CreateFCmpOEQ(TruncY, Y);
+}
+
+static Value *emitIsEvenInteger(IRBuilder<> &B, Value *Y) {
+ // Even integers are still integers after division by 2.
+ auto *HalfY = B.CreateFMul(Y, ConstantFP::get(Y->getType(), 0.5));
+ return emitIsInteger(B, HalfY);
+}
+
+// is_odd_integer(y) => is_integer(y) && !is_even_integer(y)
+static Value *emitIsOddInteger(IRBuilder<> &B, Value *Y) {
+ Value *IsIntY = emitIsInteger(B, Y);
+ Value *IsEvenY = emitIsEvenInteger(B, Y);
+ Value *NotEvenY = B.CreateNot(IsEvenY);
+ return B.CreateAnd(IsIntY, NotEvenY);
+}
+
+// isinf(val) => fabs(val) == +inf
+static Value *emitIsInf(IRBuilder<> &B, Value *val) {
+ auto *fabsVal = B.CreateUnaryIntrinsic(Intrinsic::fabs, val);
+ return B.CreateFCmpOEQ(fabsVal, ConstantFP::getInfinity(val->getType()));
+}
+
+// y * log2(fabs(x))
+static Value *emitFastExpYLnx(IRBuilder<> &B, Value *X, Value *Y) {
+ Value *AbsX = B.CreateUnaryIntrinsic(Intrinsic::fabs, X);
+ Value *LogAbsX = B.CreateUnaryIntrinsic(Intrinsic::log2, AbsX);
+ Value *YTimesLogX = B.CreateFMul(Y, LogAbsX);
+ return B.CreateUnaryIntrinsic(Intrinsic::exp2, YTimesLogX);
+}
+
+/// Emit special case management epilog code for fast pow, powr, pown, and rootn
+/// expansions. \p x and \p y should be the arguments to the library call
+/// (possibly with some values clamped). \p expylnx should be the result to use
+/// in normal circumstances.
+static Value *emitPowFixup(IRBuilder<> &B, Value *X, Value *Y, Value *ExpYLnX,
+ PowKind Kind) {
+ Constant *Zero = ConstantFP::getZero(X->getType());
+ Constant *One = ConstantFP::get(X->getType(), 1.0);
+ Constant *QNaN = ConstantFP::getQNaN(X->getType());
+ Constant *PInf = ConstantFP::getInfinity(X->getType());
+
+ switch (Kind) {
+ case PowKind::Pow: {
+ // is_odd_integer(y)
+ Value *IsOddY = emitIsOddInteger(B, Y);
+
+ // ret = copysign(expylnx, is_odd_y ? x : 1.0f)
+ Value *SelSign = B.CreateSelect(IsOddY, X, One);
+ Value *Ret = B.CreateCopySign(ExpYLnX, SelSign);
+
+ // if (x < 0 && !is_integer(y)) ret = QNAN
+ Value *IsIntY = emitIsInteger(B, Y);
+ Value *condNegX = B.CreateFCmpOLT(X, Zero);
+ Value *condNotIntY = B.CreateNot(IsIntY);
+ Value *condNaN = B.CreateAnd(condNegX, condNotIntY);
+ Ret = B.CreateSelect(condNaN, QNaN, Ret);
+
+ // if (isinf(ay)) { ... }
+
+ // FIXME: Missing backend optimization to save on materialization cost of
+ // mixed sign constant infinities.
+ Value *YIsInf = emitIsInf(B, Y);
+
+ Value *AY = B.CreateUnaryIntrinsic(Intrinsic::fabs, Y);
+ Value *YIsNegInf = B.CreateFCmpUNE(Y, AY);
+
+ Value *AX = B.CreateUnaryIntrinsic(Intrinsic::fabs, X);
+ Value *AxEqOne = B.CreateFCmpOEQ(AX, One);
+ Value *AxLtOne = B.CreateFCmpOLT(AX, One);
+ Value *XorCond = B.CreateXor(AxLtOne, YIsNegInf);
+ Value *SelInf =
+ B.CreateSelect(AxEqOne, AX, B.CreateSelect(XorCond, Zero, AY));
+ Ret = B.CreateSelect(YIsInf, SelInf, Ret);
+
+ // if (isinf(ax) || x == 0.0f) { ... }
+ Value *XIsInf = emitIsInf(B, X);
+ Value *XEqZero = B.CreateFCmpOEQ(X, Zero);
+ Value *AxInfOrZero = B.CreateOr(XIsInf, XEqZero);
+ Value *YLtZero = B.CreateFCmpOLT(Y, Zero);
+ Value *XorZeroInf = B.CreateXor(XEqZero, YLtZero);
+ Value *SelVal = B.CreateSelect(XorZeroInf, Zero, PInf);
+ Value *SelSign2 = B.CreateSelect(IsOddY, X, Zero);
+ Value *Copysign = B.CreateCopySign(SelVal, SelSign2);
+ Ret = B.CreateSelect(AxInfOrZero, Copysign, Ret);
+
+ // if (isunordered(x, y)) ret = QNAN
+ Value *isUnordered = B.CreateFCmpUNO(X, Y);
+ return B.CreateSelect(isUnordered, QNaN, Ret);
+ }
+ case PowKind::PowR: {
+ Value *YIsNeg = B.CreateFCmpOLT(Y, Zero);
+ Value *IZ = B.CreateSelect(YIsNeg, PInf, Zero);
+ Value *ZI = B.CreateSelect(YIsNeg, Zero, PInf);
+
+ Value *YEqZero = B.CreateFCmpOEQ(Y, Zero);
+ Value *SelZeroCase = B.CreateSelect(YEqZero, QNaN, IZ);
+ Value *XEqZero = B.CreateFCmpOEQ(X, Zero);
+ Value *Ret = B.CreateSelect(XEqZero, SelZeroCase, ExpYLnX);
+
+ Value *XEqInf = B.CreateFCmpOEQ(X, PInf);
+ Value *YNeZero = B.CreateFCmpUNE(Y, Zero);
+ Value *CondInfCase = B.CreateAnd(XEqInf, YNeZero);
+ Ret = B.CreateSelect(CondInfCase, ZI, Ret);
+
+ Value *IsInfY = emitIsInf(B, Y);
+ Value *XNeOne = B.CreateFCmpUNE(X, One);
+ Value *CondInfY = B.CreateAnd(IsInfY, XNeOne);
+ Value *XLtOne = B.CreateFCmpOLT(X, One);
+ Value *SelInfYCase = B.CreateSelect(XLtOne, IZ, ZI);
+ Ret = B.CreateSelect(CondInfY, SelInfYCase, Ret);
+
+ Value *IsUnordered = B.CreateFCmpUNO(X, Y);
+ return B.CreateSelect(IsUnordered, QNaN, Ret);
+ }
+ case PowKind::PowN: {
+ Constant *ZeroI = ConstantInt::get(Y->getType(), 0);
+
+ // is_odd_y = (ny & 1) != 0
+ Value *OneI = ConstantInt::get(Y->getType(), 1);
+ Value *YAnd1 = B.CreateAnd(Y, OneI);
+ Value *IsOddY = B.CreateICmpNE(YAnd1, ZeroI);
+
+ // ret = copysign(expylnx, is_odd_y ? x : 1.0f)
+ Value *SelSign = B.CreateSelect(IsOddY, X, One);
+ Value *Ret = B.CreateCopySign(ExpYLnX, SelSign);
+
+ // if (isinf(x) || x == 0.0f)
+ Value *FabsX = B.CreateUnaryIntrinsic(Intrinsic::fabs, X);
+ Value *XIsInf = B.CreateFCmpOEQ(FabsX, PInf);
+ Value *XEqZero = B.CreateFCmpOEQ(X, Zero);
+ Value *InfOrZero = B.CreateOr(XIsInf, XEqZero);
+
+ // (x == 0.0f) ^ (ny < 0) ? 0.0f : +inf
+ Value *YLtZero = B.CreateICmpSLT(Y, ZeroI);
+ Value *XorZeroInf = B.CreateXor(XEqZero, YLtZero);
+ Value *SelVal = B.CreateSelect(XorZeroInf, Zero, PInf);
+
+ // copysign(selVal, is_odd_y ? x : 0.0f)
+ Value *SelSign2 = B.CreateSelect(IsOddY, X, Zero);
+ Value *Copysign = B.CreateCopySign(SelVal, SelSign2);
+
+ return B.CreateSelect(InfOrZero, Copysign, Ret);
+ }
+ case PowKind::RootN: {
+ Constant *ZeroI = ConstantInt::get(Y->getType(), 0);
+
+ // is_odd_y = (ny & 1) != 0
+ Value *YAnd1 = B.CreateAnd(Y, ConstantInt::get(Y->getType(), 1));
+ Value *IsOddY = B.CreateICmpNE(YAnd1, ZeroI);
+
+ // ret = copysign(expylnx, is_odd_y ? x : 1.0f)
+ Value *SelSign = B.CreateSelect(IsOddY, X, One);
+ Value *Ret = B.CreateCopySign(ExpYLnX, SelSign);
+
+ // if (isinf(x) || x == 0.0f)
+ Value *FabsX = B.CreateUnaryIntrinsic(Intrinsic::fabs, X);
+ Value *IsInfX = B.CreateFCmpOEQ(FabsX, PInf);
+ Value *XEqZero = B.CreateFCmpOEQ(X, Zero);
+ Value *CondInfOrZero = B.CreateOr(IsInfX, XEqZero);
+
+ // (x == 0.0f) ^ (ny < 0) ? 0.0f : +inf
+ Value *YLtZero = B.CreateICmpSLT(Y, ZeroI);
+ Value *XorZeroInf = B.CreateXor(XEqZero, YLtZero);
+ Value *SelVal = B.CreateSelect(XorZeroInf, Zero, PInf);
+
+ // copysign(selVal, is_odd_y ? x : 0.0f)
+ Value *SelSign2 = B.CreateSelect(IsOddY, X, Zero);
+ Value *Copysign = B.CreateCopySign(SelVal, SelSign2);
+
+ Ret = B.CreateSelect(CondInfOrZero, Copysign, Ret);
+
+ // if ((x < 0.0f && !is_odd_y) || ny == 0) ret = QNAN
+ Value *XIsNeg = B.CreateFCmpOLT(X, Zero);
+ Value *NotOddY = B.CreateNot(IsOddY);
+ Value *CondNegAndNotOdd = B.CreateAnd(XIsNeg, NotOddY);
+ Value *YEqZero = B.CreateICmpEQ(Y, ZeroI);
+ Value *CondBad = B.CreateOr(CondNegAndNotOdd, YEqZero);
+ return B.CreateSelect(CondBad, QNaN, Ret);
+ }
+ }
+
+ llvm_unreachable("covered switch");
+}
+
+// TODO: Move the fold_pow folding to sqrt/fdiv here
+bool AMDGPULibCalls::expandFastPow(FPMathOperator *FPOp, IRBuilder<> &B,
+ PowKind Kind) {
+ Type *Ty = FPOp->getType();
+
+ // There's currently no reason to do this for half. The correct path is
+ // promote to float and use the fast float expansion.
+ //
+ // TODO: We could move this expansion to lowering to get half pow to work.
+ if (!Ty->getScalarType()->isFloatTy())
+ return false;
+
+ // TODO: Verify optimization for double and bfloat.
+ Value *X = FPOp->getOperand(0);
+ Value *Y = FPOp->getOperand(1);
+
+ switch (Kind) {
+ case PowKind::Pow: {
+ Constant *One = ConstantFP::get(X->getType(), 1.0);
+
+ // if (x == 1.0f) y = 1.0f;
+ Value *XEqOne = B.CreateFCmpOEQ(X, One);
+ Y = B.CreateSelect(XEqOne, One, Y);
+
+ // if (y == 0.0f) x = 1.0f;
+ Value *YEqZero = B.CreateFCmpOEQ(Y, ConstantFP::getZero(X->getType()));
+ X = B.CreateSelect(YEqZero, One, X);
+
+ Value *ExpYLnX = emitFastExpYLnx(B, X, Y);
+ Value *Fixed = emitPowFixup(B, X, Y, ExpYLnX, Kind);
+ replaceCall(FPOp, Fixed);
+ return true;
+ }
+ case PowKind::PowR: {
+ Value *NegX = B.CreateFCmpOLT(X, ConstantFP::getZero(X->getType()));
+ X = B.CreateSelect(NegX, ConstantFP::getQNaN(X->getType()), X);
+
+ Value *ExpYLnX = emitFastExpYLnx(B, X, Y);
+ Value *Fixed = emitPowFixup(B, X, Y, ExpYLnX, Kind);
+ replaceCall(FPOp, Fixed);
+ return true;
+ }
+ case PowKind::PowN: {
+ // ny == 0
+ Value *YEqZero = B.CreateICmpEQ(Y, ConstantInt::get(Y->getType(), 0));
+
+ // x = (ny == 0 ? 1.0f : x)
+ X = B.CreateSelect(YEqZero, ConstantFP::get(X->getType(), 1.0), X);
+
+ Value *CastY = B.CreateSIToFP(Y, X->getType());
+ Value *ExpYLnX = emitFastExpYLnx(B, X, CastY);
+ Value *Fixed = emitPowFixup(B, X, Y, ExpYLnX, Kind);
+ replaceCall(FPOp, Fixed);
+ return true;
+ }
+ case PowKind::RootN: {
+ Value *CastY = B.CreateSIToFP(Y, X->getType());
+ Value *RcpY = B.CreateUnaryIntrinsic(Intrinsic::amdgcn_rcp, CastY);
+ Value *ExpYLnX = emitFastExpYLnx(B, X, RcpY);
+ Value *Fixed = emitPowFixup(B, X, Y, ExpYLnX, Kind);
+ replaceCall(FPOp, Fixed);
+ return true;
+ }
+ }
+}
+
// Get a scalar native builtin single argument FP function
FunctionCallee AMDGPULibCalls::getNativeFunction(Module *M,
const FuncInfo &FInfo) {
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow-codegen.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow-codegen.ll
index 65053d00f9c90..5d08cef95810d 100644
--- a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow-codegen.ll
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow-codegen.ll
@@ -33,10 +33,51 @@ define float @test_pow_fast_f32(float %x, float %y) {
; CHECK-LABEL: test_pow_fast_f32:
; CHECK: ; %bb.0:
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT: s_getpc_b64 s[16:17]
-; CHECK-NEXT: s_add_u32 s16, s16, _Z3powff at rel32@lo+4
-; CHECK-NEXT: s_addc_u32 s17, s17, _Z3powff at rel32@hi+12
-; CHECK-NEXT: s_setpc_b64 s[16:17]
+; CHECK-NEXT: v_cmp_lg_f32_e32 vcc, 1.0, v0
+; CHECK-NEXT: v_cndmask_b32_e32 v1, 1.0, v1, vcc
+; CHECK-NEXT: v_cmp_lg_f32_e32 vcc, 0, v1
+; CHECK-NEXT: v_cndmask_b32_e32 v0, 1.0, v0, vcc
+; CHECK-NEXT: s_mov_b32 s4, 0x800000
+; CHECK-NEXT: v_cmp_lt_f32_e64 vcc, |v0|, s4
+; CHECK-NEXT: v_cndmask_b32_e64 v2, 0, 32, vcc
+; CHECK-NEXT: v_ldexp_f32 v2, |v0|, v2
+; CHECK-NEXT: v_log_f32_e32 v2, v2
+; CHECK-NEXT: v_mov_b32_e32 v3, 0x42000000
+; CHECK-NEXT: v_cndmask_b32_e32 v3, 0, v3, vcc
+; CHECK-NEXT: s_mov_b32 s4, 0xc2fc0000
+; CHECK-NEXT: v_sub_f32_e32 v2, v2, v3
+; CHECK-NEXT: v_mul_f32_e32 v3, v1, v2
+; CHECK-NEXT: v_mov_b32_e32 v4, 0x42800000
+; CHECK-NEXT: v_cmp_gt_f32_e32 vcc, s4, v3
+; CHECK-NEXT: v_cndmask_b32_e32 v3, 0, v4, vcc
+; CHECK-NEXT: v_fma_f32 v2, v1, v2, v3
+; CHECK-NEXT: v_exp_f32_e32 v2, v2
+; CHECK-NEXT: v_not_b32_e32 v3, 63
+; CHECK-NEXT: v_cndmask_b32_e32 v3, 0, v3, vcc
+; CHECK-NEXT: v_mul_f32_e32 v4, 0.5, v1
+; CHECK-NEXT: v_ldexp_f32 v2, v2, v3
+; CHECK-NEXT: v_trunc_f32_e32 v3, v1
+; CHECK-NEXT: v_trunc_f32_e32 v5, v4
+; CHECK-NEXT: v_cmp_eq_f32_e32 vcc, v3, v1
+; CHECK-NEXT: v_cmp_lg_f32_e64 s[4:5], v5, v4
+; CHECK-NEXT: s_and_b64 vcc, vcc, s[4:5]
+; CHECK-NEXT: v_cndmask_b32_e32 v4, 1.0, v0, vcc
+; CHECK-NEXT: s_brev_b32 s8, -2
+; CHECK-NEXT: v_cmp_lg_f32_e64 s[4:5], v3, v1
+; CHECK-NEXT: v_cmp_gt_f32_e64 s[6:7], 0, v0
+; CHECK-NEXT: v_bfi_b32 v2, s8, v2, v4
+; CHECK-NEXT: v_mov_b32_e32 v3, 0x7fc00000
+; CHECK-NEXT: s_and_b64 s[4:5], s[6:7], s[4:5]
+; CHECK-NEXT: v_cndmask_b32_e64 v2, v2, v3, s[4:5]
+; CHECK-NEXT: v_cmp_eq_f32_e64 s[4:5], 0, v0
+; CHECK-NEXT: v_cmp_gt_f32_e64 s[6:7], 0, v1
+; CHECK-NEXT: v_mov_b32_e32 v1, 0x7f800000
+; CHECK-NEXT: s_xor_b64 s[6:7], s[4:5], s[6:7]
+; CHECK-NEXT: v_cndmask_b32_e64 v1, v1, 0, s[6:7]
+; CHECK-NEXT: v_cndmask_b32_e32 v0, 0, v0, vcc
+; CHECK-NEXT: v_bfi_b32 v0, s8, v1, v0
+; CHECK-NEXT: v_cndmask_b32_e64 v0, v2, v0, s[4:5]
+; CHECK-NEXT: s_setpc_b64 s[30:31]
%pow = tail call fast float @_Z3powff(float %x, float %y)
ret float %pow
}
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow.ll
index 193e3a740b92e..3bb84f0586875 100644
--- a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow.ll
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-pow.ll
@@ -31,8 +31,35 @@ declare float @llvm.roundeven.f32(float)
define float @test_pow_fast_f32(float %x, float %y) {
; CHECK-LABEL: define float @test_pow_fast_f32
; CHECK-SAME: (float [[X:%.*]], float [[Y:%.*]]) {
-; CHECK-NEXT: [[POW:%.*]] = tail call fast float @_Z3powff(float [[X]], float [[Y]])
-; CHECK-NEXT: ret float [[POW]]
+; CHECK-NEXT: [[TMP1:%.*]] = fcmp fast oeq float [[X]], 1.000000e+00
+; CHECK-NEXT: [[TMP2:%.*]] = select fast i1 [[TMP1]], float 1.000000e+00, float [[Y]]
+; CHECK-NEXT: [[TMP3:%.*]] = fcmp fast oeq float [[TMP2]], 0.000000e+00
+; CHECK-NEXT: [[TMP4:%.*]] = select fast i1 [[TMP3]], float 1.000000e+00, float [[X]]
+; CHECK-NEXT: [[TMP5:%.*]] = call fast float @llvm.fabs.f32(float [[TMP4]])
+; CHECK-NEXT: [[TMP6:%.*]] = call fast float @llvm.log2.f32(float [[TMP5]])
+; CHECK-NEXT: [[TMP7:%.*]] = fmul fast float [[TMP2]], [[TMP6]]
+; CHECK-NEXT: [[TMP8:%.*]] = call fast float @llvm.exp2.f32(float [[TMP7]])
+; CHECK-NEXT: [[TMP9:%.*]] = call fast float @llvm.trunc.f32(float [[TMP2]])
+; CHECK-NEXT: [[TMP10:%.*]] = fcmp fast oeq float [[TMP9]], [[TMP2]]
+; CHECK-NEXT: [[TMP11:%.*]] = fmul fast float [[TMP2]], 5.000000e-01
+; CHECK-NEXT: [[TMP12:%.*]] = call fast float @llvm.trunc.f32(float [[TMP11]])
+; CHECK-NEXT: [[TMP13:%.*]] = fcmp fast une float [[TMP12]], [[TMP11]]
+; CHECK-NEXT: [[TMP14:%.*]] = and i1 [[TMP10]], [[TMP13]]
+; CHECK-NEXT: [[TMP15:%.*]] = select fast i1 [[TMP14]], float [[TMP4]], float 1.000000e+00
+; CHECK-NEXT: [[TMP16:%.*]] = call fast float @llvm.copysign.f32(float [[TMP8]], float [[TMP15]])
+; CHECK-NEXT: [[TMP17:%.*]] = call fast float @llvm.trunc.f32(float [[TMP2]])
+; CHECK-NEXT: [[TMP18:%.*]] = fcmp fast une float [[TMP17]], [[TMP2]]
+; CHECK-NEXT: [[TMP19:%.*]] = fcmp fast olt float [[TMP4]], 0.000000e+00
+; CHECK-NEXT: [[TMP20:%.*]] = and i1 [[TMP19]], [[TMP18]]
+; CHECK-NEXT: [[TMP21:%.*]] = select fast i1 [[TMP20]], float 0x7FF8000000000000, float [[TMP16]]
+; CHECK-NEXT: [[TMP22:%.*]] = fcmp fast oeq float [[TMP4]], 0.000000e+00
+; CHECK-NEXT: [[TMP23:%.*]] = fcmp fast olt float [[TMP2]], 0.000000e+00
+; CHECK-NEXT: [[TMP24:%.*]] = xor i1 [[TMP22]], [[TMP23]]
+; CHECK-NEXT: [[TMP25:%.*]] = select fast i1 [[TMP24]], float 0.000000e+00, float 0x7FF0000000000000
+; CHECK-NEXT: [[TMP26:%.*]] = select fast i1 [[TMP14]], float [[TMP4]], float 0.000000e+00
+; CHECK-NEXT: [[TMP27:%.*]] = call fast float @llvm.copysign.f32(float [[TMP25]], float [[TMP26]])
+; CHECK-NEXT: [[TMP28:%.*]] = select fast i1 [[TMP22]], float [[TMP27]], float [[TMP21]]
+; CHECK-NEXT: ret float [[TMP28]]
;
%pow = tail call fast float @_Z3powff(float %x, float %y)
ret float %pow
@@ -41,8 +68,35 @@ define float @test_pow_fast_f32(float %x, float %y) {
define <2 x float> @test_pow_fast_v2f32(<2 x float> %x, <2 x float> %y) {
; CHECK-LABEL: define <2 x float> @test_pow_fast_v2f32
; CHECK-SAME: (<2 x float> [[X:%.*]], <2 x float> [[Y:%.*]]) {
-; CHECK-NEXT: [[POW:%.*]] = tail call fast <2 x float> @_Z3powDv2_fS_(<2 x float> [[X]], <2 x float> [[Y]])
-; CHECK-NEXT: ret <2 x float> [[POW]]
+; CHECK-NEXT: [[TMP1:%.*]] = fcmp fast oeq <2 x float> [[X]], splat (float 1.000000e+00)
+; CHECK-NEXT: [[TMP2:%.*]] = select fast <2 x i1> [[TMP1]], <2 x float> splat (float 1.000000e+00), <2 x float> [[Y]]
+; CHECK-NEXT: [[TMP3:%.*]] = fcmp fast oeq <2 x float> [[TMP2]], zeroinitializer
+; CHECK-NEXT: [[TMP4:%.*]] = select fast <2 x i1> [[TMP3]], <2 x float> splat (float 1.000000e+00), <2 x float> [[X]]
+; CHECK-NEXT: [[TMP5:%.*]] = call fast <2 x float> @llvm.fabs.v2f32(<2 x float> [[TMP4]])
+; CHECK-NEXT: [[TMP6:%.*]] = call fast <2 x float> @llvm.log2.v2f32(<2 x float> [[TMP5]])
+; CHECK-NEXT: [[TMP7:%.*]] = fmul fast <2 x float> [[TMP2]], [[TMP6]]
+; CHECK-NEXT: [[TMP8:%.*]] = call fast <2 x float> @llvm.exp2.v2f32(<2 x float> [[TMP7]])
+; CHECK-NEXT: [[TMP9:%.*]] = call fast <2 x float> @llvm.trunc.v2f32(<2 x float> [[TMP2]])
+; CHECK-NEXT: [[TMP10:%.*]] = fcmp fast oeq <2 x float> [[TMP9]], [[TMP2]]
+; CHECK-NEXT: [[TMP11:%.*]] = fmul fast <2 x float> [[TMP2]], splat (float 5.000000e-01)
+; CHECK-NEXT: [[TMP12:%.*]] = call fast <2 x float> @llvm.trunc.v2f32(<2 x float> [[TMP11]])
+; CHECK-NEXT: [[TMP13:%.*]] = fcmp fast une <2 x float> [[TMP12]], [[TMP11]]
+; CHECK-NEXT: [[TMP14:%.*]] = and <2 x i1> [[TMP10]], [[TMP13]]
+; CHECK-NEXT: [[TMP15:%.*]] = select fast <2 x i1> [[TMP14]], <2 x float> [[TMP4]], <2 x float> splat (float 1.000000e+00)
+; CHECK-NEXT: [[TMP16:%.*]] = call fast <2 x float> @llvm.copysign.v2f32(<2 x float> [[TMP8]], <2 x float> [[TMP15]])
+; CHECK-NEXT: [[TMP17:%.*]] = call fast <2 x float> @llvm.trunc.v2f32(<2 x float> [[TMP2]])
+; CHECK-NEXT: [[TMP18:%.*]] = fcmp fast une <2 x float> [[TM...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/180553
More information about the llvm-commits
mailing list