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 @@ -768,17 +768,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 @@ -1911,6 +1911,10 @@ %19 = "llvm.intr.powi"(%arg0, %exp) {fastmathFlags = #llvm.fastmath} : (f32, i32) -> f32 // CHECK: call afn float @llvm.powi.f32.i32(float {{.*}}, i32 {{.*}}) %20 = "llvm.intr.powi"(%arg0, %exp) {fastmathFlags = #llvm.fastmath} : (f32, i32) -> f32 + + %21 = llvm.mlir.constant(true) : i1 +// CHECK: select contract i1 + %22 = llvm.select %21, %arg0, %20 {fastmathFlags = #llvm.fastmath} : i1, f32 llvm.return }