[llvm] [ConstantFolding] Consider `tanh*` to always be a noop (PR #70794)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 31 05:33:53 PDT 2023


https://github.com/Nilstrieb created https://github.com/llvm/llvm-project/pull/70794

fixes https://github.com/rust-lang/rust/issues/108965

Most trigonometry functions are already considered, but tanh* was missing. According to POSIX, these functions may fail on subnormals, so we choose not to fail, allowing the calls to be eliminated. https://pubs.opengroup.org/onlinepubs/9699919799/functions/tanh.html

The existing test llvm/test/Transforms/InstSimplify/ConstProp/calls.ll already eliminated the call since it marked tanh as `nounwind`, which causes it to be eliminated as well.

I am not sure whether I should add a new test without the `nounwind` or whether it makes more sense to remove the `nounwind` from the existing tests.

>From 4b2ccc18afc935f36c0a0a4951d869260a262ff9 Mon Sep 17 00:00:00 2001
From: Nilstrieb <48135649+Nilstrieb at users.noreply.github.com>
Date: Tue, 31 Oct 2023 12:22:44 +0000
Subject: [PATCH] [ConstantFolding] Consider `tanh*` to always be a noop

Most trigonometry functions are already considered, but tanh* was
missing. According to POSIX, these functions may fail on subnormals,
so we choose not to fail, allowing the calls to be eliminated.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/tanh.html

The existing test llvm/test/Transforms/InstSimplify/ConstProp/calls.ll
already eliminated the call since it marked tanh as `nounwind`, which
causes it to be eliminated as well.
---
 llvm/lib/Analysis/ConstantFolding.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 2f164e80f24060a..a8190d259bbe7b1 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3440,6 +3440,9 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
       case LibFunc_atan:
       case LibFunc_atanf:
       case LibFunc_atanl:
+      case LibFunc_tanh:
+      case LibFunc_tanhf:
+      case LibFunc_tanhl:
         // Per POSIX, this MAY fail if Op is denormal. We choose not failing.
         return true;
 



More information about the llvm-commits mailing list