Skip to content

Commit 25c39d6

Browse files
Victor Perezaleclearmind
authored andcommitted
[mlir][llvm] Add trap intrinsics to the dialect
Define llvm.trap, llvm.debugtrap, and llvm.ubsantrap intrinsics in the llvm dialect. Signed-off-by: Victor Perez <[email protected]> Differential Revision: https://reviews.llvm.org/D149574 (cherry picked from commit b8064c1a039ef819a2f2ce0ca79990666b23366f)
1 parent 7a3320a commit 25c39d6

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,26 @@ def LLVM_masked_compressstore
664664
let arguments = (ins LLVM_AnyVector, LLVM_AnyPointer, LLVM_VectorOf<I1>);
665665
}
666666

667+
//
668+
// Trap intrinsics.
669+
//
670+
671+
def LLVM_Trap : LLVM_ZeroResultIntrOp<"trap">;
672+
673+
def LLVM_DebugTrap : LLVM_ZeroResultIntrOp<"debugtrap">;
674+
675+
def LLVM_UBSanTrap : LLVM_ZeroResultIntrOp<"ubsantrap"> {
676+
let arguments = (ins I8Attr:$failureKind);
677+
string llvmBuilder = [{
678+
builder.CreateIntrinsic(
679+
llvm::Intrinsic::ubsantrap, {}, {builder.getInt8($failureKind)});
680+
}];
681+
string mlirBuilder = [{
682+
$_op =
683+
$_builder.create<LLVM::UBSanTrap>($_location, $_int_attr($failureKind));
684+
}];
685+
}
686+
667687
/// Create a call to vscale intrinsic.
668688
def LLVM_vscale : LLVM_IntrOp<"vscale", [0], [], [], 1>;
669689

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,17 @@ define void @masked_expand_compress_intrinsics(ptr %0, <7 x i1> %1, <7 x float>
355355
ret void
356356
}
357357

358+
; CHECK-LABEL: llvm.func @trap_intrinsics
359+
define void @trap_intrinsics() {
360+
; CHECK: "llvm.intr.trap"() : () -> ()
361+
call void @llvm.trap()
362+
; CHECK: "llvm.intr.debugtrap"() : () -> ()
363+
call void @llvm.debugtrap()
364+
; CHECK: "llvm.intr.ubsantrap"() {failureKind = 1 : i8} : () -> ()
365+
call void @llvm.ubsantrap(i8 1)
366+
ret void
367+
}
368+
358369
; CHECK-LABEL: llvm.func @memcpy_test
359370
define void @memcpy_test(i32 %0, ptr %1, ptr %2) {
360371
; CHECK: %[[FALSE:.+]] = llvm.mlir.constant(false) : i1
@@ -741,6 +752,9 @@ declare <7 x float> @llvm.masked.gather.v7f32.v7p0(<7 x ptr>, i32 immarg, <7 x i
741752
declare void @llvm.masked.scatter.v7f32.v7p0(<7 x float>, <7 x ptr>, i32 immarg, <7 x i1>)
742753
declare <7 x float> @llvm.masked.expandload.v7f32(ptr, <7 x i1>, <7 x float>)
743754
declare void @llvm.masked.compressstore.v7f32(<7 x float>, ptr, <7 x i1>)
755+
declare void @llvm.trap()
756+
declare void @llvm.debugtrap()
757+
declare void @llvm.ubsantrap(i8 immarg)
744758
declare void @llvm.memcpy.p0.p0.i32(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i32, i1 immarg)
745759
declare void @llvm.memcpy.inline.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64 immarg, i1 immarg)
746760
declare void @llvm.memmove.p0.p0.i32(ptr nocapture writeonly, ptr nocapture readonly, i32, i1 immarg)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,17 @@ llvm.func @masked_expand_compress_intrinsics(%ptr: !llvm.ptr<f32>, %mask: vector
374374
llvm.return
375375
}
376376

377+
// CHECK-LABEL: @trap_intrinsics
378+
llvm.func @trap_intrinsics() {
379+
// CHECK: call void @llvm.trap()
380+
"llvm.intr.trap"() : () -> ()
381+
// CHECK: call void @llvm.debugtrap()
382+
"llvm.intr.debugtrap"() : () -> ()
383+
// CHECK: call void @llvm.ubsantrap(i8 1)
384+
"llvm.intr.ubsantrap"() {failureKind = 1 : i8} : () -> ()
385+
llvm.return
386+
}
387+
377388
// CHECK-LABEL: @memcpy_test
378389
llvm.func @memcpy_test(%arg0: i32, %arg2: !llvm.ptr<i8>, %arg3: !llvm.ptr<i8>) {
379390
%i1 = llvm.mlir.constant(false) : i1
@@ -785,6 +796,9 @@ llvm.func @lifetime(%p: !llvm.ptr) {
785796
// CHECK-DAG: declare void @llvm.masked.scatter.v7f32.v7p0(<7 x float>, <7 x ptr>, i32 immarg, <7 x i1>)
786797
// CHECK-DAG: declare <7 x float> @llvm.masked.expandload.v7f32(ptr nocapture, <7 x i1>, <7 x float>)
787798
// CHECK-DAG: declare void @llvm.masked.compressstore.v7f32(<7 x float>, ptr nocapture, <7 x i1>)
799+
// CHECK-DAG: declare void @llvm.trap()
800+
// CHECK-DAG: declare void @llvm.debugtrap()
801+
// CHECK-DAG: declare void @llvm.ubsantrap(i8 immarg)
788802
// CHECK-DAG: declare void @llvm.memcpy.p0.p0.i32(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i32, i1 immarg)
789803
// CHECK-DAG: declare void @llvm.memcpy.inline.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64 immarg, i1 immarg)
790804
// CHECK-DAG: declare { i32, i1 } @llvm.sadd.with.overflow.i32(i32, i32)

0 commit comments

Comments
 (0)