-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Added fix to enable constant folding that is missing for math library functions (scalbln , scalbn, ldexp) #169893
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
base: main
Are you sure you want to change the base?
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-llvm-transforms Author: Raja Sudha Sri Harika (Sriharika1506) ChangesIssue : #112631 This patch adds support for constant folding for certain math library functions wherever it was previously missing. Full diff: https://github.com/llvm/llvm-project/pull/169893.diff 2 Files Affected:
diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 4e7c97194cc59..72b8f4b427b3b 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -214,6 +214,9 @@ class LibCallSimplifier {
Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
+
+ Value *foldLdexp(CallInst *CI, IRBuilderBase &B);
+
// Wrapper for all floating point library call optimizations
Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
IRBuilderBase &B);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 4a1565977b91c..7508048d3032e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2464,6 +2464,24 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
return nullptr;
}
+Value *LibCallSimplifier::foldLdexp(CallInst *CI,IRBuilderBase &B) {
+
+ Type*RequiredType = CI->getType();
+ if(!RequiredType->isFPOrFPVectorTy())
+ return nullptr;
+
+ Value*x = CI->getArgOperand(0);
+ Value*exp = CI->getArgOperand(1);
+ Module*M = CI->getModule();
+
+ Function*ldexpDecl = llvm::Intrinsic::getOrInsertDeclaration(M, Intrinsic::ldexp,{RequiredType, exp->getType()});
+
+ CallInst *NewCall = B.CreateCall(ldexpDecl, {x, exp}, CI->getName());
+ NewCall->setAttributes(CI->getAttributes());
+ return copyFlags(*CI, NewCall);
+}
+
+
Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
Module *M = CI->getModule();
Function *Callee = CI->getCalledFunction();
@@ -4031,6 +4049,17 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
case LibFunc_exp2:
case LibFunc_exp2f:
return optimizeExp2(CI, Builder);
+ // New ldexp / scalbn / scalbln family:
+ case LibFunc_ldexp:
+ case LibFunc_ldexpf:
+ case LibFunc_ldexpl:
+ case LibFunc_scalbn:
+ case LibFunc_scalbnf:
+ case LibFunc_scalbnl:
+ case LibFunc_scalbln:
+ case LibFunc_scalblnf:
+ case LibFunc_scalblnl:
+ return foldLdexp(CI, Builder);
case LibFunc_fabsf:
case LibFunc_fabs:
case LibFunc_fabsl:
|
|
@ashwinagrl @YogeshFebyani also worked on this |
|
Consider referencing PR #114417 and adding corresponding tests |
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions h,cpp -- llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 72b8f4b42..67d6bb133 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -216,7 +216,7 @@ private:
Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
Value *foldLdexp(CallInst *CI, IRBuilderBase &B);
-
+
// Wrapper for all floating point library call optimizations
Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
IRBuilderBase &B);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 702705ec0..809af8d3f 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2469,24 +2469,24 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
return nullptr;
}
-Value *LibCallSimplifier::foldLdexp(CallInst *CI,IRBuilderBase &B) {
+Value *LibCallSimplifier::foldLdexp(CallInst *CI, IRBuilderBase &B) {
- Type*RequiredType = CI->getType();
- if(!RequiredType->isFPOrFPVectorTy())
+ Type *RequiredType = CI->getType();
+ if (!RequiredType->isFPOrFPVectorTy())
return nullptr;
- Value*x = CI->getArgOperand(0);
- Value*exp = CI->getArgOperand(1);
- Module*M = CI->getModule();
+ Value *x = CI->getArgOperand(0);
+ Value *exp = CI->getArgOperand(1);
+ Module *M = CI->getModule();
+
+ Function *ldexpDecl = llvm::Intrinsic::getOrInsertDeclaration(
+ M, Intrinsic::ldexp, {RequiredType, exp->getType()});
- Function*ldexpDecl = llvm::Intrinsic::getOrInsertDeclaration(M, Intrinsic::ldexp,{RequiredType, exp->getType()});
-
CallInst *NewCall = B.CreateCall(ldexpDecl, {x, exp}, CI->getName());
NewCall->setAttributes(CI->getAttributes());
return copyFlags(*CI, NewCall);
}
-
Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
Module *M = CI->getModule();
Function *Callee = CI->getCalledFunction();
|
dtcxzyw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest modifying the code based on https://github.com/llvm/llvm-project/pull/114417/files and fixing previous comments.
I am a bit confused that you are fixing an issue that was assigned to @ashwinagrl. Have you two communicated with each other?
|
@dtcxzyw We are working in a team along with @ashwinagrl. |
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) LLVMLLVM.Transforms/InstCombine/RISCV/libcall-arg-exts.llLLVM.Transforms/InstCombine/SystemZ/libcall-arg-exts.llLLVM.Transforms/InstCombine/exp2-1.llLLVM.Transforms/InstCombine/pow-to-ldexp.llLLVM.Transforms/InstCombine/simplify-libcalls-i16.llLLVM.Transforms/InstCombine/simplify-libcalls.llIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
Issue : #112631
This patch adds support for constant folding for certain math library functions wherever it was previously missing.