aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
diff options
context:
space:
mode:
authorsrcarroll <50210727+srcarroll@users.noreply.github.com>2024-03-14 19:18:56 -0500
committerGitHub <noreply@github.com>2024-03-14 19:18:56 -0500
commit58ef9bec071383744fb703ff08df9806f25e4095 (patch)
tree06d3f56fe66b0dbaa9b322382e99d66ddc0f4d81 /llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
parentad0de4e6e6146d72d376b8f4c84dfa72817fa80d (diff)
downloadllvm-58ef9bec071383744fb703ff08df9806f25e4095.zip
llvm-58ef9bec071383744fb703ff08df9806f25e4095.tar.gz
llvm-58ef9bec071383744fb703ff08df9806f25e4095.tar.bz2
[mlir][math] Implement alternative decomposition for tanh (#85025)
The previous implementation decomposes `tanh(x)` into `(exp(2x) - 1)/(exp(2x)+1), x < 0` `(1 - exp(-2x))/(1 + exp(-2x)), x >= 0` This is fine as it avoids overflow with the exponential, but the whole decomposition is computed for both cases unconditionally, then the result is chosen based off the sign of the input. This results in doing two expensive `exp` computations. The proposed change avoids doing the whole computation twice by exploiting the reflection symmetry `tanh(-x) = -tanh(x)`. We can "normalize" the input to be positive by setting `y = sign(x) * x`, where the sign of `x` is computed as `sign(x) = (float)(x > 0) * (-2) + 1`. Then compute `z = tanh(y)` with the decomposition above for `x >=0` and "denormalize" the result `z * sign(x)` to retain the sign. The reason it is done this way is that it is very amenable to vectorization. This method trades the duplicate decomposition computations (which takes 5 instructions including an extra expensive `exp` and `div`) for 4 cheap instructions to compute the signs value 1. `arith.cmpf` (which is a pre-existing instruction in the previous impl) 2. `arith.sitofp` 3. `arith.mulf` 4. `arith.addf` and 1 more instruction to get the right sign in the result 5. `arith.mulf`. Moreover, numerically, this implementation will yield the exact same results as the previous implementation.
Diffstat (limited to 'llvm/lib/Target/DirectX/DirectXTargetMachine.cpp')
0 files changed, 0 insertions, 0 deletions