Skip to content

Commit 8be07ad

Browse files
committed
[mlir][LLVM] Introduce reduction intrinsics for minimum/maximum
This patch adds supports for the reduction intrinsic for floating point minimum and maximum that have been added to LLVM by https://reviews.llvm.org/D152370. Related to: #63969 Reviewed By: dcaballe Differential Revision: https://reviews.llvm.org/D155869
1 parent 3bc74be commit 8be07ad

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ def LLVM_vector_reduce_xor : LLVM_VecReductionI<"xor">;
718718

719719
def LLVM_vector_reduce_fmax : LLVM_VecReductionF<"fmax">;
720720
def LLVM_vector_reduce_fmin : LLVM_VecReductionF<"fmin">;
721+
def LLVM_vector_reduce_fmaximum : LLVM_VecReductionF<"fmaximum">;
722+
def LLVM_vector_reduce_fminimum : LLVM_VecReductionF<"fminimum">;
721723

722724
def LLVM_vector_reduce_fadd : LLVM_VecReductionAccF<"fadd">;
723725
def LLVM_vector_reduce_fmul : LLVM_VecReductionAccF<"fmul">;

mlir/test/Dialect/LLVMIR/roundtrip.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ func.func @fastmathFlags(%arg0: f32, %arg1: f32, %arg2: i32, %arg3: vector<2 x f
516516
%13 = llvm.intr.vector.reduce.fmin(%arg3) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
517517
// CHECK: {{.*}} = llvm.intr.vector.reduce.fmax(%arg3) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
518518
%14 = llvm.intr.vector.reduce.fmax(%arg3) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
519+
// CHECK: {{.*}} = llvm.intr.vector.reduce.fminimum(%arg3) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
520+
%15 = llvm.intr.vector.reduce.fminimum(%arg3) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
521+
// CHECK: {{.*}} = llvm.intr.vector.reduce.fmaximum(%arg3) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
522+
%16 = llvm.intr.vector.reduce.fmaximum(%arg3) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
519523
return
520524
}
521525

mlir/test/Integration/Dialect/LLVMIR/CPU/test-vector-reductions-fp.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ module {
3535
llvm.call @printNewline() : () -> ()
3636
// CHECK: 1
3737

38+
%maximum = llvm.intr.vector.reduce.fmaximum(%v)
39+
: (vector<4xf32>) -> f32
40+
llvm.call @printF32(%maximum) : (f32) -> ()
41+
llvm.call @printNewline() : () -> ()
42+
// CHECK: 4
43+
44+
%minimum = llvm.intr.vector.reduce.fminimum(%v)
45+
: (vector<4xf32>) -> f32
46+
llvm.call @printF32(%minimum) : (f32) -> ()
47+
llvm.call @printNewline() : () -> ()
48+
// CHECK: 1
49+
3850
%add1 = "llvm.intr.vector.reduce.fadd"(%0, %v)
3951
: (f32, vector<4xf32>) -> f32
4052
llvm.call @printF32(%add1) : (f32) -> ()

mlir/test/Target/LLVMIR/Import/fastmath.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ declare float @llvm.pow.f32(float, float)
4545
declare float @llvm.fmuladd.f32(float, float, float)
4646
declare float @llvm.vector.reduce.fmin.v2f32(<2 x float>)
4747
declare float @llvm.vector.reduce.fmax.v2f32(<2 x float>)
48+
declare float @llvm.vector.reduce.fminimum.v2f32(<2 x float>)
49+
declare float @llvm.vector.reduce.fmaximum.v2f32(<2 x float>)
4850

4951
; CHECK-LABEL: @fastmath_intr
5052
define void @fastmath_intr(float %arg1, i32 %arg2, <2 x float> %arg3) {
@@ -60,6 +62,10 @@ define void @fastmath_intr(float %arg1, i32 %arg2, <2 x float> %arg3) {
6062
%5 = call nnan float @llvm.vector.reduce.fmin.v2f32(<2 x float> %arg3)
6163
; CHECK: %{{.*}} = llvm.intr.vector.reduce.fmax({{.*}}) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
6264
%6 = call nnan float @llvm.vector.reduce.fmax.v2f32(<2 x float> %arg3)
65+
; CHECK: %{{.*}} = llvm.intr.vector.reduce.fminimum({{.*}}) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
66+
%7 = call nnan float @llvm.vector.reduce.fminimum.v2f32(<2 x float> %arg3)
67+
; CHECK: %{{.*}} = llvm.intr.vector.reduce.fmaximum({{.*}}) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
68+
%8 = call nnan float @llvm.vector.reduce.fmaximum.v2f32(<2 x float> %arg3)
6369

6470
ret void
6571
}

mlir/test/Target/LLVMIR/Import/intrinsic.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ define void @vector_reductions(float %0, <8 x float> %1, <8 x i32> %2) {
364364
%17 = call reassoc float @llvm.vector.reduce.fmul.v8f32(float %0, <8 x float> %1)
365365
; CHECK: "llvm.intr.vector.reduce.xor"(%{{.*}}) : (vector<8xi32>) -> i32
366366
%18 = call i32 @llvm.vector.reduce.xor.v8i32(<8 x i32> %2)
367+
; CHECK: llvm.intr.vector.reduce.fmaximum(%{{.*}}) : (vector<8xf32>) -> f32
368+
%19 = call float @llvm.vector.reduce.fmaximum.v8f32(<8 x float> %1)
369+
; CHECK: llvm.intr.vector.reduce.fminimum(%{{.*}}) : (vector<8xf32>) -> f32
370+
%20 = call float @llvm.vector.reduce.fminimum.v8f32(<8 x float> %1)
367371
ret void
368372
}
369373

@@ -944,6 +948,8 @@ declare i32 @llvm.vector.reduce.add.v8i32(<8 x i32>)
944948
declare i32 @llvm.vector.reduce.and.v8i32(<8 x i32>)
945949
declare float @llvm.vector.reduce.fmax.v8f32(<8 x float>)
946950
declare float @llvm.vector.reduce.fmin.v8f32(<8 x float>)
951+
declare float @llvm.vector.reduce.fmaximum.v8f32(<8 x float>)
952+
declare float @llvm.vector.reduce.fminimum.v8f32(<8 x float>)
947953
declare i32 @llvm.vector.reduce.mul.v8i32(<8 x i32>)
948954
declare i32 @llvm.vector.reduce.or.v8i32(<8 x i32>)
949955
declare i32 @llvm.vector.reduce.smax.v8i32(<8 x i32>)

mlir/test/Target/LLVMIR/llvmir-intrinsics.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ llvm.func @vector_reductions(%arg0: f32, %arg1: vector<8xf32>, %arg2: vector<8xi
354354
llvm.intr.vector.reduce.fmax(%arg1) : (vector<8xf32>) -> f32
355355
// CHECK: call float @llvm.vector.reduce.fmin.v8f32
356356
llvm.intr.vector.reduce.fmin(%arg1) : (vector<8xf32>) -> f32
357+
// CHECK: call float @llvm.vector.reduce.fmaximum.v8f32
358+
llvm.intr.vector.reduce.fmaximum(%arg1) : (vector<8xf32>) -> f32
359+
// CHECK: call float @llvm.vector.reduce.fminimum.v8f32
360+
llvm.intr.vector.reduce.fminimum(%arg1) : (vector<8xf32>) -> f32
357361
// CHECK: call i32 @llvm.vector.reduce.mul.v8i32
358362
"llvm.intr.vector.reduce.mul"(%arg2) : (vector<8xi32>) -> i32
359363
// CHECK: call i32 @llvm.vector.reduce.or.v8i32

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,10 +2011,14 @@ llvm.func @fastmathFlags(%arg0: f32, %arg1 : vector<2xf32>) {
20112011
%21 = llvm.intr.vector.reduce.fmax(%arg1) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
20122012
%22 = llvm.intr.vector.reduce.fmin(%arg1) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
20132013

2014+
// CHECK: call nnan float @llvm.vector.reduce.fmaximum.v2f32(<2 x float> {{.*}})
2015+
// CHECK: call nnan float @llvm.vector.reduce.fminimum.v2f32(<2 x float> {{.*}})
2016+
%23 = llvm.intr.vector.reduce.fmaximum(%arg1) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
2017+
%24 = llvm.intr.vector.reduce.fminimum(%arg1) {fastmathFlags = #llvm.fastmath<nnan>} : (vector<2xf32>) -> f32
20142018

2015-
%23 = llvm.mlir.constant(true) : i1
2019+
%25 = llvm.mlir.constant(true) : i1
20162020
// CHECK: select contract i1
2017-
%24 = llvm.select %23, %arg0, %20 {fastmathFlags = #llvm.fastmath<contract>} : i1, f32
2021+
%26 = llvm.select %25, %arg0, %20 {fastmathFlags = #llvm.fastmath<contract>} : i1, f32
20182022
llvm.return
20192023
}
20202024

0 commit comments

Comments
 (0)