Skip to content

Commit e9c5740

Browse files
[MLIR][NVVM] Add lg2.approx.f32 intrinsic with optional FTZ support
1 parent 841d71a commit e9c5740

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -449,16 +449,35 @@ def NVVM_RcpApproxFtzF32Op : NVVM_IntrOp<"rcp.approx.ftz.f", [Pure], 1> {
449449
let assemblyFormat = "$arg attr-dict `:` type($res)";
450450
}
451451

452-
def NVVM_lg2ApproxF32Op : NVVM_IntrOp<"lg2.approx.f", [Pure], 1> {
453-
let arguments = (ins F32:$arg);
454-
let results = (outs F32:$res);
455-
let assemblyFormat = "$arg attr-dict `:` type($res)";
456-
}
452+
//===----------------------------------------------------------------------===//
453+
// NVVM lg2 approximate op
454+
//===----------------------------------------------------------------------===//
457455

458-
def NVVM_lg2ApproxFtzF32Op : NVVM_IntrOp<"lg2.approx.ftz.f", [Pure], 1> {
459-
let arguments = (ins F32:$arg);
456+
def NVVM_Lg2ApproxF32Op : NVVM_Op<"lg2.approx.f", [Pure]> {
457+
458+
let summary = "Compute approximate base-2 log (lg2.approx.f32)";
459+
460+
let description = [{
461+
Compute the approximate base-2 logarithm of the input value.
462+
If 'ftz' is true, subnormal numbers are flushed to zero.
463+
}];
464+
465+
let arguments = (ins
466+
F32:$arg,
467+
OptionalAttr<BoolAttr>:$ftz);
468+
460469
let results = (outs F32:$res);
470+
461471
let assemblyFormat = "$arg attr-dict `:` type($res)";
472+
473+
string llvmBuilder = [{
474+
bool ftz = $ftz && *$ftz;
475+
llvm::Intrinsic::ID intrinsicID =
476+
ftz ? llvm::Intrinsic::nvvm_lg2_approx_ftz_f
477+
: llvm::Intrinsic::nvvm_lg2_approx_f;
478+
479+
$res = createIntrinsicCall(builder, intrinsicID, {$arg});
480+
}];
462481
}
463482

464483
//===----------------------------------------------------------------------===//

mlir/test/Dialect/LLVMIR/nvvm.mlir

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ func.func @nvvm_rcp(%arg0: f32) -> f32 {
3636
llvm.return %0 : f32
3737
}
3838

39-
// CHECK-LABEL: @nvvm_lg2_f32
40-
func.func @nvvm_lg2_f32(%arg0: f32) -> f32 {
39+
// CHECK-LABEL: @nvvm_lg2_approx_f
40+
func.func @nvvm_lg2_approx_f(%arg0: f32) -> f32 {
4141
// CHECK: nvvm.lg2.approx.f %arg0 : f32
4242
%0 = nvvm.lg2.approx.f %arg0 : f32
4343
llvm.return %0 : f32
4444
}
4545

46-
// CHECK-LABEL: @nvvm_lg2_ftz_f32
47-
func.func @nvvm_lg2_ftz_f32(%arg0: f32) -> f32 {
48-
// CHECK: nvvm.lg2.approx.ftz.f %arg0 : f32
49-
%0 = nvvm.lg2.approx.ftz.f %arg0 : f32
46+
// CHECK-LABEL: @nvvm_lg2_approx_ftz_f
47+
func.func @nvvm_lg2_approx_ftz_f(%arg0: f32) -> f32 {
48+
// CHECK: nvvm.lg2.approx.f %arg0 {ftz = true} : f32
49+
%0 = nvvm.lg2.approx.f %arg0 {ftz = true} : f32
5050
llvm.return %0 : f32
5151
}
5252

0 commit comments

Comments
 (0)