Skip to content
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

Inconsistency in sinh(const), cosh(const), and tanh(const) optimizations with -ffast-math #132503

Open
k-arrows opened this issue Mar 22, 2025 · 6 comments · May be fixed by #132671
Open

Inconsistency in sinh(const), cosh(const), and tanh(const) optimizations with -ffast-math #132503

k-arrows opened this issue Mar 22, 2025 · 6 comments · May be fixed by #132671
Assignees
Labels
constant-folding Problems related to constant folding in the optimizer floating-point Floating-point math good first issue https://github.com/llvm/llvm-project/contribute missed-optimization

Comments

@k-arrows
Copy link

Consider the following test case.
https://godbolt.org/z/Ecd865P97

#include <cmath>

double f1() {
  return std::sinh(0.);
}

double f2() {
  return std::cosh(0.);
}

double f3() {
  return std::tanh(0.);
}

The optimization results for std::sinh(0.), std::cosh(0.), and std::tanh(0.) differ between -O1 and -O1 -ffast-math. Specifically, when -ffast-math is enabled, sinh(0.) and cosh(0.) are not folded into constants but instead call the corresponding library functions. This suggests that -ffast-math is restricting optimization rather than enhancing it.

@frederick-vs-ja frederick-vs-ja added clang:codegen IR generation bugs: mangling, exceptions, etc. missed-optimization and removed new issue labels Mar 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2025

@llvm/issue-subscribers-clang-codegen

Author: None (k-arrows)

Consider the following test case. https://godbolt.org/z/Ecd865P97 ```cpp #include <cmath>

double f1() {
return std::sinh(0.);
}

double f2() {
return std::cosh(0.);
}

double f3() {
return std::tanh(0.);
}


The optimization results for `std::sinh(0.)`, `std::cosh(0.)`, and `std::tanh(0.)` differ between `-O1` and `-O1 -ffast-math`. Specifically, when `-ffast-math` is enabled, `sinh(0.)` and `cosh(0.)` are not folded into constants but instead call the corresponding library functions. This suggests that `-ffast-math` is restricting optimization rather than enhancing it.

</details>

@frederick-vs-ja frederick-vs-ja added the floating-point Floating-point math label Mar 22, 2025
@dtcxzyw dtcxzyw added constant-folding Problems related to constant folding in the optimizer and removed clang:codegen IR generation bugs: mangling, exceptions, etc. labels Mar 22, 2025
@dtcxzyw
Copy link
Member

dtcxzyw commented Mar 22, 2025

Missing constant folding support for llvm.sinh/cosh.

@dtcxzyw dtcxzyw added the good first issue https://github.com/llvm/llvm-project/contribute label Mar 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2025

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
  2. In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2025

@llvm/issue-subscribers-good-first-issue

Author: None (k-arrows)

Consider the following test case. https://godbolt.org/z/Ecd865P97 ```cpp #include <cmath>

double f1() {
return std::sinh(0.);
}

double f2() {
return std::cosh(0.);
}

double f3() {
return std::tanh(0.);
}


The optimization results for `std::sinh(0.)`, `std::cosh(0.)`, and `std::tanh(0.)` differ between `-O1` and `-O1 -ffast-math`. Specifically, when `-ffast-math` is enabled, `sinh(0.)` and `cosh(0.)` are not folded into constants but instead call the corresponding library functions. This suggests that `-ffast-math` is restricting optimization rather than enhancing it.

</details>

@el-ev
Copy link
Contributor

el-ev commented Mar 23, 2025

I would like to work on this. Any hints on where to start?

@YLChenZ
Copy link
Contributor

YLChenZ commented Mar 23, 2025

@dtcxzyw hey, here are some of my findings:

lldb -- opt -O1 --ffast-math math.ll -o
* frame #0: 0x000055555959a790 opt`llvm::ConstantFoldCall(llvm::CallBase const*, llvm::Function*, llvm::ArrayRef<llvm::Constant*>, llvm::TargetLibraryInfo const*, bool)
    frame #1: 0x000055555965b5b7 opt`tryConstantFoldCall(llvm::CallBase*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::SimplifyQuery const&) + 199
    frame #2: 0x00005555596753e4 opt`llvm::simplifyCall(llvm::CallBase*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::SimplifyQuery const&) + 100
    frame #3: 0x000055555966b726 opt`simplifyInstructionWithOperands(llvm::Instruction*, llvm::ArrayRef<llvm::Value*>, llvm::SimplifyQuery const&, unsigned int) + 2374
    frame #4: 0x000055555967d7de opt`llvm::simplifyInstruction(llvm::Instruction*, llvm::SimplifyQuery const&) + 174

I found llvm::ConstantFoldCall llvm/lib/Analysis/ConstantFolding.cpp. I guess I need to modify the logic related to ConstantFoldScalarCall1 called by ConstantFoldScalarCall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
constant-folding Problems related to constant folding in the optimizer floating-point Floating-point math good first issue https://github.com/llvm/llvm-project/contribute missed-optimization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants