diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td @@ -265,6 +265,27 @@ def LLVM_AssumeOp : LLVM_ZeroResultIntrOp<"assume", []>, Arguments<(ins I1:$cond)>; +// +// Expect intrinsics. +// + +def LLVM_ExpectOp + : LLVM_OneResultIntrOp<"expect", [], [0], + [Pure, SameOperandsAndResultType]> { + let arguments = (ins AnySignlessInteger:$val, + AnySignlessInteger:$expected); + let assemblyFormat = "$val `,` $expected attr-dict `:` type($val)"; +} + +def LLVM_ExpectWithProbabilityOp + : LLVM_OneResultIntrOp<"expect.with.probability", [], [0], + [Pure, AllTypesMatch<["val", "expected", "res"]>]> { + let arguments = (ins AnySignlessInteger:$val, + AnySignlessInteger:$expected, + F64:$prob); + let assemblyFormat = "$val `,` $expected `,` $prob attr-dict `:` type($val)"; +} + // // Coroutine intrinsics. // diff --git a/mlir/test/Target/LLVMIR/Import/intrinsic.ll b/mlir/test/Target/LLVMIR/Import/intrinsic.ll --- a/mlir/test/Target/LLVMIR/Import/intrinsic.ll +++ b/mlir/test/Target/LLVMIR/Import/intrinsic.ll @@ -467,6 +467,25 @@ ret void } +; CHECK-LABEL: @expect +; CHECK-SAME: %[[VAL:[a-zA-Z0-9]+]] +define void @expect(i32 %0) { + ; CHECK: %[[EXP:.+]] = llvm.mlir.constant(42 : i32) : i32 + ; CHECK: llvm.intr.expect %[[VAL]], %[[EXP]] : i32 + %2 = call i32 @llvm.expect.i32(i32 %0, i32 42) + ret void +} + +; CHECK-LABEL: @expect_with_probability +; CHECK-SAME: %[[VAL:[a-zA-Z0-9]+]] +define void @expect_with_probability(i16 %0) { + ; CHECK: %[[EXP:.+]] = llvm.mlir.constant(42 : i16) : i16 + ; CHECK: %[[PROB:.+]] = llvm.mlir.constant(5.000000e-01 : f64) : f64 + ; CHECK: llvm.intr.expect.with.probability %[[VAL]], %[[EXP]], %[[PROB]] : i16 + %2 = call i16 @llvm.expect.with.probability.i16(i16 %0, i16 42, double 0.5) + ret void +} + ; CHECK-LABEL: llvm.func @coro_id define void @coro_id(i32 %0, ptr %1) { ; CHECK: llvm.intr.coro.id %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}} : (i32, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> !llvm.token @@ -774,6 +793,8 @@ declare { <8 x i32>, <8 x i1> } @llvm.smul.with.overflow.v8i32(<8 x i32>, <8 x i32>) declare { i32, i1 } @llvm.umul.with.overflow.i32(i32, i32) declare { <8 x i32>, <8 x i1> } @llvm.umul.with.overflow.v8i32(<8 x i32>, <8 x i32>) +declare i32 @llvm.expect.i32(i32, i32) +declare i16 @llvm.expect.with.probability.i16(i16, i16, double immarg) declare token @llvm.coro.id(i32, ptr readnone, ptr nocapture readonly, ptr) declare ptr @llvm.coro.begin(token, ptr writeonly) declare i64 @llvm.coro.size.i64() diff --git a/mlir/test/Target/LLVMIR/llvmir-intrinsics.mlir b/mlir/test/Target/LLVMIR/llvmir-intrinsics.mlir --- a/mlir/test/Target/LLVMIR/llvmir-intrinsics.mlir +++ b/mlir/test/Target/LLVMIR/llvmir-intrinsics.mlir @@ -466,6 +466,23 @@ llvm.return } +// CHECK-LABEL: @expect +llvm.func @expect(%arg0: i32) { + %0 = llvm.mlir.constant(42 : i32) : i32 + // CHECK: call i32 @llvm.expect.i32(i32 %{{.*}}, i32 42) + %1 = llvm.intr.expect %arg0, %0 : i32 + llvm.return +} + +// CHECK-LABEL: @expect_with_probability +llvm.func @expect_with_probability(%arg0: i16) { + %0 = llvm.mlir.constant(42 : i16) : i16 + %1 = llvm.mlir.constant(0.5 : f64) : f64 + // CHECK: call i16 @llvm.expect.with.probability.i16(i16 %{{.*}}, i16 42, double 5.000000e-01) + %2 = llvm.intr.expect.with.probability %arg0, %0, %1 : i16 + llvm.return +} + // CHECK-LABEL: @coro_id llvm.func @coro_id(%arg0: i32, %arg1: !llvm.ptr) { // CHECK: call token @llvm.coro.id @@ -814,6 +831,8 @@ // CHECK-DAG: declare { <8 x i32>, <8 x i1> } @llvm.usub.with.overflow.v8i32(<8 x i32>, <8 x i32>) // CHECK-DAG: declare { i32, i1 } @llvm.umul.with.overflow.i32(i32, i32) // CHECK-DAG: declare { <8 x i32>, <8 x i1> } @llvm.umul.with.overflow.v8i32(<8 x i32>, <8 x i32>) +// CHECK-DAG: declare i32 @llvm.expect.i32(i32, i32) +// CHECK-DAG: declare i16 @llvm.expect.with.probability.i16(i16, i16, double immarg) // CHECK-DAG: declare token @llvm.coro.id(i32, ptr readnone, ptr nocapture readonly, ptr) // CHECK-DAG: declare ptr @llvm.coro.begin(token, ptr writeonly) // CHECK-DAG: declare i64 @llvm.coro.size.i64()