Skip to content

Conversation

@lizhijin1024
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 15, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (lizhijin1024)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/112373.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+3)
  • (modified) llvm/test/Transforms/InstCombine/sqrt.ll (+22)
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index db2acb9eed0938..aa7864ad4f6c0e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2720,6 +2720,9 @@ Value *LibCallSimplifier::mergeSqrtToExp(CallInst *CI, IRBuilderBase &B) {
       ExpLb = LibFunc_exp;
       Exp2Lb = LibFunc_exp2;
       Exp10Lb = LibFunc_exp10;
+    } else if (CI->getType()->getScalarType()->isFP128Ty()) {
+      ExpLb = LibFunc_expl;
+      Exp2Lb = LibFunc_exp2l;
     } else
       return nullptr;
   } else
diff --git a/llvm/test/Transforms/InstCombine/sqrt.ll b/llvm/test/Transforms/InstCombine/sqrt.ll
index f72fe5a6a5817b..6168a61cf86cb0 100644
--- a/llvm/test/Transforms/InstCombine/sqrt.ll
+++ b/llvm/test/Transforms/InstCombine/sqrt.ll
@@ -201,6 +201,25 @@ define <2 x float> @sqrt_exp_vec(<2 x float> %x) {
   ret <2 x float> %res
 }
 
+define dso_local fp128 @sqrt_exp_long_double(fp128 %x, fp128 %y) {
+; CHECK-LABEL: @sqrt_exp_long_double(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul fast fp128 [[X:%.*]], 0xL00000000000000003FFE000000000000
+; CHECK-NEXT:    [[TMP0:%.*]] = call fast fp128 @llvm.exp.f128(fp128 [[MERGED_SQRT]])
+; CHECK-NEXT:    [[MERGED_SQRT1:%.*]] = fmul fast fp128 [[Y:%.*]], 0xL00000000000000003FFE000000000000
+; CHECK-NEXT:    [[TMP1:%.*]] = call fast fp128 @llvm.exp2.f128(fp128 [[MERGED_SQRT1]])
+; CHECK-NEXT:    [[ADD:%.*]] = fadd fast fp128 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret fp128 [[ADD]]
+;
+entry:
+  %0 = call fast fp128 @llvm.exp.f128(fp128 %x)
+  %1 = call fast fp128 @llvm.sqrt.f128(fp128 %0)
+  %2 = call fast fp128 @llvm.exp2.f128(fp128 %y)
+  %3 = call fast fp128 @llvm.sqrt.f128(fp128 %2)
+  %add = fadd fast fp128 %1, %3
+  ret fp128 %add
+}
+
 declare i32 @foo(double)
 declare double @sqrt(double) readnone
 declare float @sqrtf(float)
@@ -212,3 +231,6 @@ declare double @exp2(double)
 declare double @exp10(double)
 declare <2 x float> @llvm.exp.v2f32(<2 x float>)
 declare <2 x float> @llvm.sqrt.v2f32(<2 x float>)
+declare fp128 @llvm.exp.f128(fp128)
+declare fp128 @llvm.sqrt.f128(fp128)
+declare fp128 @llvm.exp2.f128(fp128)

@arsenm arsenm added the floating-point Floating-point math label Oct 15, 2024
ret <2 x float> %res
}

define dso_local fp128 @sqrt_exp_long_double(fp128 %x, fp128 %y) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need dso_local

Comment on lines +215 to +219
%0 = call fast fp128 @llvm.exp.f128(fp128 %x)
%1 = call fast fp128 @llvm.sqrt.f128(fp128 %0)
%2 = call fast fp128 @llvm.exp2.f128(fp128 %y)
%3 = call fast fp128 @llvm.sqrt.f128(fp128 %2)
%add = fadd fast fp128 %1, %3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the different cases in different functions.

Use minimal fast math flags, this looks like it only requires reassoc. Add an additional test which shows the preservation behavior of other fast math flags.

Also test the same situation with equivalent library calls instead of intrinsics (and the mixed intrinsic + libcall case)

Exp2Lb = LibFunc_exp2;
Exp10Lb = LibFunc_exp10;
} else if (CI->getType()->getScalarType()->isFP128Ty()) {
ExpLb = LibFunc_expl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This intrinsic->intrinsic transform really shouldn't depend on the libcall availability...

@snehasish snehasish removed their request for review February 6, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants