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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-llvm

Author: Guray Ozen (grypp)

<details>
<summary>Changes</summary>

Implement `nvvm.lg2` with ftz flag

---
Full diff: https://github.com/llvm/llvm-project/pull/193789.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..36683d623e2fc 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_Lg2Op : NVVM_Op<"lg2", [Pure, SameOperandsAndResultType]> {
+  let summary = "Base-2 logarithm (fast approximation)";
+  let description = [{
+    Computes a fast approximation of the base-2 logarithm of the input value.
+    Lowers to PTX `lg2.approx{.ftz}.f32`. The `ftz` attribute, when set,
+    flushes subnormal inputs and results to sign-preserving zero.
+
+    For more information, see PTX ISA:
+    [lg2](https://docs.nvidia.com/cuda/parallel-thread-execution/#floating-point-instructions-lg2)
+  }];
+  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_lg2_approx_ftz_f
+                        : llvm::Intrinsic::nvvm_lg2_approx_f;
+    $res = createIntrinsicCall(builder, IID, {$src});
+  }];
+}
+
 //===----------------------------------------------------------------------===//
 // 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..3ef1b3f32f587
--- /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_lg2_f32
+func.func @nvvm_lg2_f32(%arg0: f32) -> f32 {
+  // CHECK: nvvm.lg2 {{.*}} : f32
+  %0 = nvvm.lg2 %arg0 : f32
+  return %0 : f32
+}
+
+// CHECK-LABEL: @nvvm_lg2_ftz_f32
+func.func @nvvm_lg2_ftz_f32(%arg0: f32) -> f32 {
+  // CHECK: nvvm.lg2 {{.*}} {ftz = true} : f32
+  %0 = nvvm.lg2 %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..da7f318d1eede
--- /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_lg2
+llvm.func @nvvm_lg2(%arg0: f32) -> f32 {
+  // CHECK: call float @llvm.nvvm.lg2.approx.f(float %{{.*}})
+  %0 = nvvm.lg2 %arg0 : f32
+  llvm.return %0 : f32
+}
+
+// CHECK-LABEL: @nvvm_lg2_ftz
+llvm.func @nvvm_lg2_ftz(%arg0: f32) -> f32 {
+  // CHECK: call float @llvm.nvvm.lg2.approx.ftz.f(float %{{.*}})
+  %0 = nvvm.lg2 %arg0 {ftz = true} : f32
+  llvm.return %0 : f32
+}

``````````

</details>


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


More information about the Mlir-commits mailing list