Tanh on LLVM NVPTX

Hyperbolic Tangent

— unknown status

Operation

OperationTanh (bpd_tanh)
Formulaf(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
CategoryTranscendental (exp)

Kernel Specification (Prolog/Logtalk)

This is the backend-neutral op_expr fact that specifies the operation. The same term is lowered to LLVM IR, CUDA C, MLIR, and every other backend — one specification, many targets.

op_expr(bpd_tanh,
    call(tanh, var)).

Backend

BackendLLVM NVPTX (llvm_nvptx)
Targetnvptx
DescriptionLLVM IR targeting NVIDIA PTX (GPU). Compiled with llc for nvptx64, loaded via CUDA driver API.

Generated Code

This is the actual LLVM IR (NVPTX) emitted by our Prolog/Logtalk toolchain for this operation. Every instruction was generated from a declarative op_expr specification — no hand-written assembly.

; BPD tanh: calls scalar tanhf from libm — matches PyTorch exactly
define void @bpd_tanh(i32 %n, float* %out, float* %in) {
entry:
  %cmp = icmp sgt i32 %n, 0
  br i1 %cmp, label %loop, label %exit

loop:
  %i = phi i32 [ 0, %entry ], [ %next, %loop ]
  %idx = sext i32 %i to i64
  %inptr = getelementptr float, float* %in, i64 %idx
  %x = load float, float* %inptr, align 4
  %result = call float @tanhf(float %x)
  %outptr = getelementptr float, float* %out, i64 %idx
  store float %result, float* %outptr, align 4
  %next = add i32 %i, 1
  %done = icmp sge i32 %next, %n
  br i1 %done, label %exit, label %loop

exit:
  ret void
}

declare float @tanhf(float)

Verification

TierSILVER — 0 ULP, robustness testing pending
ULP Divergence0
Source Commitb03bc23f7
Deploymentdeployed
Measured2026-09-25
HardwareNVIDIA Tesla P4 (enclave GPU)
GateOutput compared to PyTorch reference on identical input tensor. 0 ULP = every floating-point bit matches.