Add a pattern for expanding tanh op into exp form.

A `tanh` is expanded into:

- 1-exp^{-2x} / 1+exp^{-2x}, if x => 0
- exp^{2x}-1 / exp^{2x}+1 , if x < 0.

Differential D81618
[mlir] Add support for lowering tanh to LLVMIR. hanchung on Jun 10 2020, 3:57 PM.
Details Add a pattern for expanding tanh op into exp form. - 1-exp^{-2x} / 1+exp^{-2x}, if x => 0
- exp^{2x}-1 / exp^{2x}+1 , if x < 0.
Diff Detail - Repository
- rG LLVM Github Monorepo
## Event TimelineComment Actions Didn't go through the code yet. high level comments: - Is it possible to get this lowered to a tanh function call instead of the exp rewrite ?
- If we have to rewrite tanh(x) then lets express it in a numerically stable way because tanh(x) = exp^{2x}-1 / exp^{2x}+1 will blow up for relatively not very big positive values of x, so we can do:
tanh(x) = 1-exp^{-2x} / 1+exp^{-2x}, x => 0 tanh(x) = exp^{2x}-1 / exp^{2x}+1, x < 0 Comment Actions It looks like this is better done as an intra-dialect transform, everything that you need is available in the standard dialect so the code can just expand std.tanh to a set of std. operations and then use the regular conversion. We already do so for atomics https://github.com/llvm/llvm-project/blob/master/mlir/lib/Dialect/StandardOps/Transforms/ExpandAtomic.cpp, we can put ExpandTanh.cpp next to it. Comment Actions
Unless we assume libc is linked, there's no tanh function. Comment Actions It depends on the LLVM backend. LLVM models _all_ intrinsics as function calls, but it does not necessarily mean they actually become function calls in the generated assembly (e.g., llvm.intr.fma won't). Comment Actions Thanks!
Comment Actions Address comments.
Comment Actions Reverted in a9a21bb4b682474248dc85f9e7db4b260d249ab9 ; the standalone test was broken Comment Actions It passed with bazel on my local machine, I'll take a look at what happened, sorry about that. |