Skip to content

Commit b0b8f1b

Browse files
zahiraamvladimirlaz
authored andcommitted
Ignore llvm.trap instrinsic
Clang, due to some ABI constraints needs to generate llvm.trap intrinsics. According to the LangRef documentation a trap instruction is lowered to a target's trap instruction or to an abort() function if the target doesn't have a trap instruction. SPIRV has neither a trap nor an abort instruction and no current opcode has the semantics of an abort/trap. Currently the IR to SPIRV translator is crashing when it finds an llvm.trap intrinsic. The solution will require some thoughts on the SPIRV side to decide how to implement an abort instruction. This patch changes the translator so it doesn't crash. This will be revised when a decision is taken. NOTE: clang could eventually not generate an llvm.trap instruction in the current case (non-base destructor of an abstract class needs to be emmitted) but keep in mind that clang might generate an llvm.trap intrinsic some other ways and we might stumble into this issue again. An alternative to this, could be to add an LLVM pass that will get rid of the llvm.trap intrisincs in the code before the SPIRV translator. But having the translator solve this issue is a preferred solution.
1 parent e648d0b commit b0b8f1b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,9 @@ SPIRVValue *LLVMToSPIRV::transIntrinsicInst(IntrinsicInst *II,
19231923
case Intrinsic::invariant_start:
19241924
case Intrinsic::invariant_end:
19251925
case Intrinsic::dbg_label:
1926+
case Intrinsic::trap:
1927+
// llvm.trap intrinsic is not implemented. But for now don't crash. This
1928+
// change is pending the trap/abort intrisinc implementation.
19261929
return nullptr;
19271930
default:
19281931
if (SPIRVAllowUnknownIntrinsics)

llvm-spirv/test/trap.ll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -spirv-text -o %t
3+
; RUN: FileCheck < %t %s
4+
; RUN: llvm-spirv %t.bc -o %t.spv
5+
; RUN: spirv-val %t.spv
6+
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
7+
target triple = "spir-unknown-unknown"
8+
9+
; Function Attrs: nounwind
10+
; CHECK: Capability Addresses
11+
; CHECK: "foo"
12+
13+
; Function Attrs: cold noreturn nounwind
14+
declare void @llvm.trap() #8
15+
16+
define spir_kernel void @foo(i32 addrspace(1)* %a) #0 !kernel_arg_addr_space !1 !kernel_arg_access_qual !2 !kernel_arg_type !3 !kernel_arg_base_type !4 !kernel_arg_type_qual !5 {
17+
entry:
18+
%a.addr = alloca i32 addrspace(1)*, align 4
19+
store i32 addrspace(1)* %a, i32 addrspace(1)** %a.addr, align 4
20+
call void @llvm.trap() #12
21+
ret void
22+
}
23+
24+
attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-realign-stack" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
25+
attributes #12 = { noreturn nounwind }
26+
27+
!opencl.enable.FP_CONTRACT = !{}
28+
!opencl.spir.version = !{!6}
29+
!opencl.ocl.version = !{!6}
30+
!opencl.used.extensions = !{!7}
31+
!opencl.used.optional.core.features = !{!7}
32+
!opencl.compiler.options = !{!7}
33+
34+
!1 = !{i32 1}
35+
!2 = !{!"none"}
36+
!3 = !{!"int*"}
37+
!4 = !{!"int*"}
38+
!5 = !{!""}
39+
!6 = !{i32 1, i32 2}
40+
!7 = !{}

0 commit comments

Comments
 (0)