[Mlir-commits] [mlir] [MLIR][NVVM] Add `nvvm.ex2` OP (PR #193790)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Apr 23 09:17:34 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Guray Ozen (grypp)

<details>
<summary>Changes</summary>

Implement `nvvm.ex2` with ftz flag

---
Full diff: https://github.com/llvm/llvm-project/pull/193790.diff


3 Files Affected:

- (modified) mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td (+29) 
- (added) mlir/test/Dialect/LLVMIR/nvvm-transcendentals.mlir (+15) 
- (added) mlir/test/Target/LLVMIR/nvvm/transcendentals.mlir (+15) 


``````````diff
diff --git a/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
index c892ee18166f2..518903c3df51d 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
@@ -518,6 +518,35 @@ def NVVM_RcpApproxFtzF32Op : NVVM_IntrOp<"rcp.approx.ftz.f", [Pure], 1> {
   let assemblyFormat = "$arg attr-dict `:` type($res)";
 }
 
+//===----------------------------------------------------------------------===//
+// NVVM transcendental op definitions
+//===----------------------------------------------------------------------===//
+// PTX provides only fast approximations for sin/cos/lg2/ex2/tanh. Exposing
+// these as plain `nvvm.{sin,cos,lg2,ex2,tanh}` (without a `.approx` suffix)
+// keeps the NVVM dialect self-contained -- users don't need to reach for
+// `math.*` ops for something that already has a PTX instruction.
+
+def NVVM_Ex2Op : NVVM_Op<"ex2", [Pure, SameOperandsAndResultType]> {
+  let summary = "Base-2 exponential (fast approximation)";
+  let description = [{
+    Computes a fast approximation of 2 raised to the power of the input value.
+    Lowers to PTX `ex2.approx{.ftz}.f32`. The `ftz` attribute, when set,
+    flushes subnormal inputs and results to sign-preserving zero.
+
+    For more information, see PTX ISA:
+    [ex2](https://docs.nvidia.com/cuda/parallel-thread-execution/#floating-point-instructions-ex2)
+  }];
+  let arguments = (ins F32:$src,
+                       DefaultValuedAttr<BoolAttr, "false">:$ftz);
+  let results = (outs F32:$res);
+  let assemblyFormat = "$src attr-dict `:` type($src)";
+  string llvmBuilder = [{
+    unsigned IID = $ftz ? llvm::Intrinsic::nvvm_ex2_approx_ftz
+                        : llvm::Intrinsic::nvvm_ex2_approx;
+    $res = createIntrinsicCall(builder, IID, {$src}, {$src->getType()});
+  }];
+}
+
 //===----------------------------------------------------------------------===//
 // NVVM redux op definitions
 //===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/LLVMIR/nvvm-transcendentals.mlir b/mlir/test/Dialect/LLVMIR/nvvm-transcendentals.mlir
new file mode 100644
index 0000000000000..f9ec5fb092821
--- /dev/null
+++ b/mlir/test/Dialect/LLVMIR/nvvm-transcendentals.mlir
@@ -0,0 +1,15 @@
+// RUN: mlir-opt %s -split-input-file -verify-diagnostics | FileCheck %s
+
+// CHECK-LABEL: @nvvm_ex2_f32
+func.func @nvvm_ex2_f32(%arg0: f32) -> f32 {
+  // CHECK: nvvm.ex2 {{.*}} : f32
+  %0 = nvvm.ex2 %arg0 : f32
+  return %0 : f32
+}
+
+// CHECK-LABEL: @nvvm_ex2_ftz_f32
+func.func @nvvm_ex2_ftz_f32(%arg0: f32) -> f32 {
+  // CHECK: nvvm.ex2 {{.*}} {ftz = true} : f32
+  %0 = nvvm.ex2 %arg0 {ftz = true} : f32
+  return %0 : f32
+}
diff --git a/mlir/test/Target/LLVMIR/nvvm/transcendentals.mlir b/mlir/test/Target/LLVMIR/nvvm/transcendentals.mlir
new file mode 100644
index 0000000000000..7bbac3b9843fe
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/nvvm/transcendentals.mlir
@@ -0,0 +1,15 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// CHECK-LABEL: @nvvm_ex2
+llvm.func @nvvm_ex2(%arg0: f32) -> f32 {
+  // CHECK: call float @llvm.nvvm.ex2.approx.f32(float %{{.*}})
+  %0 = nvvm.ex2 %arg0 : f32
+  llvm.return %0 : f32
+}
+
+// CHECK-LABEL: @nvvm_ex2_ftz
+llvm.func @nvvm_ex2_ftz(%arg0: f32) -> f32 {
+  // CHECK: call float @llvm.nvvm.ex2.approx.ftz.f32(float %{{.*}})
+  %0 = nvvm.ex2 %arg0 {ftz = true} : f32
+  llvm.return %0 : f32
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/193790


More information about the Mlir-commits mailing list