-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[SimplifyLibCalls] Move constant folding logic in ConstantFoldLibCall2
#172139
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
Moved constant folding logic from SimplifyLibCalls.cpp to ConstantFoldLibCall2
|
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-analysis @llvm/pr-subscribers-llvm-transforms Author: Ron Xavier (Ronxvier) ChangesMoved constant folding logic from SimplifyLibCalls.cpp to ConstantFoldLibCall2 Full diff: https://github.com/llvm/llvm-project/pull/172139.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index a9b51065a1d99..8a73c02bc363e 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3241,6 +3241,18 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
if (TLI->has(Func))
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
break;
+ case LibFunc_fdim:
+ case LibFunc_fdimf:
+ case LibFunc_fdiml:
+ if (TLI->has(Func)){
+ APFloat Difference = Op1V;
+ Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
+
+ APFloat MaxVal =
+ maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
+ return ConstantFP::get(Ty->getContext(), MaxVal);
+ }
+ break;
}
return nullptr;
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index c3537f544c432..55cd08b3de855 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3201,18 +3201,8 @@ Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
if (isa<PoisonValue>(CI->getArgOperand(1)))
return CI->getArgOperand(1);
- const APFloat *X, *Y;
- // Check if both values are constants
- if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
- !match(CI->getArgOperand(1), m_APFloat(Y)))
- return nullptr;
-
- APFloat Difference = *X;
- Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
-
- APFloat MaxVal =
- maximum(Difference, APFloat::getZero(CI->getType()->getFltSemantics()));
- return ConstantFP::get(CI->getType(), MaxVal);
+ // Constant folding will be handled by ConstantFoldLibCall2
+ return nullptr;
}
//===----------------------------------------------------------------------===//
|
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/Analysis/ConstantFolding.cpp llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index dd4a3abf8..10e22b55c 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3248,7 +3248,7 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
APFloat MaxVal =
- maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
+ maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
return ConstantFP::get(Ty->getContext(), MaxVal);
break;
}
|
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) LLVMLLVM.Transforms/InstCombine/fdim.llLLVM.Transforms/InstCombine/win-fdim.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 |
🪟 Windows x64 Test Results
Failed Tests(click on a test name to see its output) LLVMLLVM.Transforms/InstCombine/fdim.llLLVM.Transforms/InstCombine/win-fdim.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 |
Might be an overguarding issue
antoniofrighetto
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.
We should introduce fdim names here too, to allow constant folding:
llvm-project/llvm/lib/Analysis/ConstantFolding.cpp
Lines 2002 to 2005 in 6a25e45
| case 'f': | |
| return Name == "fabs" || Name == "fabsf" || | |
| Name == "floor" || Name == "floorf" || | |
| Name == "fmod" || Name == "fmodf"; |
(I wonder if they should be intrinsics though)
ConstantFoldLibCall2
| if (isa<PoisonValue>(CI->getArgOperand(0))) | ||
| return CI->getArgOperand(0); | ||
| if (isa<PoisonValue>(CI->getArgOperand(1))) | ||
| return CI->getArgOperand(1); |
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.
The poison cases shouldn't be in this file either.
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.
@nikic Sorry, I'm new to llvm contribution, do you mean poison cases should just be removed entirely? Because as I understand it, ConstantFoldLibCall2 only takes in operands of known constants
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.
Poison values are also constants. Though I don't think it matters since we always set noundef for operands passed to fdim: https://godbolt.org/z/9MhdToo6W
Currently we cannot benefit from the vectorization of fdim (and other transformations except for constant folding). So it may be too early to turn it into intrinsics. |
antoniofrighetto
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.
Point above still unresolved?
remove break that isn't reached Co-authored-by: Antonio Frighetto <me@antoniofrighetto.com>
| APFloat Difference = Op1V; | ||
| Difference.subtract(Op2V, RoundingMode::NearestTiesToEven); | ||
|
|
||
| APFloat MaxVal = | ||
| maximum(Difference, APFloat::getZero(Ty->getFltSemantics())); |
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.
The actual implementation logic should probably be in APFloat
Moved constant folding logic from SimplifyLibCalls.cpp to ConstantFoldLibCall2
Meant to resolve #171877