diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td @@ -771,17 +771,22 @@ // Misc operations. def LLVM_SelectOp : LLVM_Op<"select", - [Pure, AllTypesMatch<["trueValue", "falseValue", "res"]>]>, + [Pure, AllTypesMatch<["trueValue", "falseValue", "res"]>, + DeclareOpInterfaceMethods]>, LLVM_Builder< "$res = builder.CreateSelect($condition, $trueValue, $falseValue);"> { let arguments = (ins LLVM_ScalarOrVectorOf:$condition, - LLVM_Type:$trueValue, LLVM_Type:$falseValue); + LLVM_Type:$trueValue, LLVM_Type:$falseValue, + DefaultValuedAttr:$fastmathFlags); let results = (outs LLVM_Type:$res); let assemblyFormat = "operands attr-dict `:` type($condition) `,` type($res)"; string llvmInstName = "Select"; string mlirBuilder = [{ - $res = $_builder.create( + auto op = $_builder.create( $_location, $_resultType, $condition, $trueValue, $falseValue); + moduleImport.setFastmathFlagsAttr(inst, op); + $res = op; }]; } def LLVM_FreezeOp : LLVM_Op<"freeze", [SameOperandsAndResultType]> { diff --git a/mlir/test/Target/LLVMIR/Import/fastmath.ll b/mlir/test/Target/LLVMIR/Import/fastmath.ll --- a/mlir/test/Target/LLVMIR/Import/fastmath.ll +++ b/mlir/test/Target/LLVMIR/Import/fastmath.ll @@ -1,7 +1,7 @@ ; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s ; CHECK-LABEL: @fastmath_inst -define void @fastmath_inst(float %arg1, float %arg2) { +define void @fastmath_inst(float %arg1, float %arg2, i1 %arg3) { ; CHECK: llvm.fadd %{{.*}}, %{{.*}} {fastmathFlags = #llvm.fastmath} : f32 %1 = fadd nnan ninf float %arg1, %arg2 ; CHECK: llvm.fsub %{{.*}}, %{{.*}} {fastmathFlags = #llvm.fastmath} : f32 @@ -12,6 +12,8 @@ %4 = fdiv afn reassoc float %arg1, %arg2 ; CHECK: llvm.fneg %{{.*}} {fastmathFlags = #llvm.fastmath} : f32 %5 = fneg fast float %arg1 + ; CHECK: llvm.select %{{.*}}, %{{.*}}, %{{.*}} {fastmathFlags = #llvm.fastmath} : i1, f32 + %6 = select contract i1 %arg3, float %arg1, float %arg2 ret void } diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir --- a/mlir/test/Target/LLVMIR/llvmir.mlir +++ b/mlir/test/Target/LLVMIR/llvmir.mlir @@ -1949,6 +1949,10 @@ %21 = llvm.intr.vector.reduce.fmax(%arg1) {fastmathFlags = #llvm.fastmath} : (vector<2xf32>) -> f32 %22 = llvm.intr.vector.reduce.fmin(%arg1) {fastmathFlags = #llvm.fastmath} : (vector<2xf32>) -> f32 + + %23 = llvm.mlir.constant(true) : i1 +// CHECK: select contract i1 + %24 = llvm.select %23, %arg0, %20 {fastmathFlags = #llvm.fastmath} : i1, f32 llvm.return }