1 ; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-cxx-exceptions -S | FileCheck %s -DPTR=i32
2 ; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-cxx-exceptions -S --mattr=+atomics,+bulk-memory | FileCheck %s -DPTR=i32
3 ; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-cxx-exceptions --mtriple=wasm64-unknown-unknown -data-layout="e-m:e-p:64:64-i64:64-n32:64-S128" -S | FileCheck %s -DPTR=i64
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown"
8 @_ZTIi = external constant ptr
9 @_ZTIc = external constant ptr
10 ; CHECK: @__THREW__ = external thread_local global [[PTR]]
11 ; __threwValue is only used in Emscripten SjLj, so it shouldn't be generated.
12 ; CHECK-NOT: @__threwValue =
14 ; Test invoke instruction with clauses (try-catch block)
15 define void @clause() personality ptr @__gxx_personality_v0 {
16 ; CHECK-LABEL: @clause(
18 invoke void @foo(i32 3)
19 to label %invoke.cont unwind label %lpad
21 ; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__
22 ; CHECK-NEXT: call cc{{.*}} void @__invoke_void_i32(ptr @foo, i32 3)
23 ; CHECK-NEXT: %[[__THREW__VAL:.*]] = load [[PTR]], ptr @__THREW__
24 ; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__
25 ; CHECK-NEXT: %cmp = icmp eq [[PTR]] %[[__THREW__VAL]], 1
26 ; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
28 invoke.cont: ; preds = %entry
31 lpad: ; preds = %entry
32 %0 = landingpad { ptr, i32 }
35 %1 = extractvalue { ptr, i32 } %0, 0
36 %2 = extractvalue { ptr, i32 } %0, 1
37 br label %catch.dispatch
39 ; CHECK-NEXT: %[[FMC:.*]] = call ptr @__cxa_find_matching_catch_4(ptr @_ZTIi, ptr null)
40 ; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { ptr, i32 } poison, ptr %[[FMC]], 0
41 ; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = call i32 @getTempRet0()
42 ; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { ptr, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
43 ; CHECK-NEXT: extractvalue { ptr, i32 } %[[IVI2]], 0
44 ; CHECK-NEXT: %[[CDR:.*]] = extractvalue { ptr, i32 } %[[IVI2]], 1
46 catch.dispatch: ; preds = %lpad
47 %3 = call i32 @llvm.eh.typeid.for.p0(ptr @_ZTIi)
48 %matches = icmp eq i32 %2, %3
49 br i1 %matches, label %catch1, label %catch
50 ; CHECK: catch.dispatch:
51 ; CHECK-NEXT: %[[TYPEID:.*]] = call i32 @llvm_eh_typeid_for(ptr @_ZTIi)
52 ; CHECK-NEXT: %matches = icmp eq i32 %[[CDR]], %[[TYPEID]]
54 catch1: ; preds = %catch.dispatch
55 %4 = call ptr @__cxa_begin_catch(ptr %1)
56 %5 = load i32, ptr %4, align 4
57 call void @__cxa_end_catch()
60 try.cont: ; preds = %catch, %catch1, %invoke.cont
63 catch: ; preds = %catch.dispatch
64 %6 = call ptr @__cxa_begin_catch(ptr %1)
65 call void @__cxa_end_catch()
69 ; Test invoke instruction with filters (functions with throw(...) declaration)
70 ; Currently we don't support exception specifications correctly in JS glue code,
71 ; so we ignore all filters here.
72 ; See https://github.com/llvm/llvm-project/issues/49740.
73 define void @filter() personality ptr @__gxx_personality_v0 {
74 ; CHECK-LABEL: @filter(
76 invoke void @foo(i32 3)
77 to label %invoke.cont unwind label %lpad
79 ; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__
80 ; CHECK-NEXT: call cc{{.*}} void @__invoke_void_i32(ptr @foo, i32 3)
81 ; CHECK-NEXT: %[[__THREW__VAL:.*]] = load [[PTR]], ptr @__THREW__
82 ; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__
83 ; CHECK-NEXT: %cmp = icmp eq [[PTR]] %[[__THREW__VAL]], 1
84 ; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
86 invoke.cont: ; preds = %entry
89 lpad: ; preds = %entry
90 %0 = landingpad { ptr, i32 }
91 filter [2 x ptr] [ptr @_ZTIi, ptr @_ZTIc]
92 %1 = extractvalue { ptr, i32 } %0, 0
93 %2 = extractvalue { ptr, i32 } %0, 1
94 br label %filter.dispatch
96 ; We now temporarily ignore filters because of the bug, so we pass nothing to
97 ; __cxa_find_matching_catch
98 ; CHECK-NEXT: %[[FMC:.*]] = call ptr @__cxa_find_matching_catch_2()
100 filter.dispatch: ; preds = %lpad
101 %ehspec.fails = icmp slt i32 %2, 0
102 br i1 %ehspec.fails, label %ehspec.unexpected, label %eh.resume
104 ehspec.unexpected: ; preds = %filter.dispatch
105 call void @__cxa_call_unexpected(ptr %1) #4
108 eh.resume: ; preds = %filter.dispatch
109 %lpad.val = insertvalue { ptr, i32 } poison, ptr %1, 0
110 %lpad.val3 = insertvalue { ptr, i32 } %lpad.val, i32 %2, 1
111 resume { ptr, i32 } %lpad.val3
113 ; CHECK-NEXT: insertvalue
114 ; CHECK-NEXT: %[[LPAD_VAL:.*]] = insertvalue
115 ; CHECK-NEXT: %[[LOW:.*]] = extractvalue { ptr, i32 } %[[LPAD_VAL]], 0
116 ; CHECK-NEXT: call void @__resumeException(ptr %[[LOW]])
117 ; CHECK-NEXT: unreachable
120 ; Test if argument attributes indices in newly created call instructions are correct
121 define void @arg_attributes() personality ptr @__gxx_personality_v0 {
122 ; CHECK-LABEL: @arg_attributes(
124 %0 = invoke noalias ptr @bar(i8 signext 1, i8 zeroext 2)
125 to label %invoke.cont unwind label %lpad
127 ; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__
128 ; CHECK-NEXT: %0 = call cc{{.*}} noalias ptr @__invoke_ptr_i8_i8(ptr @bar, i8 signext 1, i8 zeroext 2)
130 invoke.cont: ; preds = %entry
133 lpad: ; preds = %entry
134 %1 = landingpad { ptr, i32 }
137 %2 = extractvalue { ptr, i32 } %1, 0
138 %3 = extractvalue { ptr, i32 } %1, 1
139 br label %catch.dispatch
141 catch.dispatch: ; preds = %lpad
142 %4 = call i32 @llvm.eh.typeid.for.p0(ptr @_ZTIi)
143 %matches = icmp eq i32 %3, %4
144 br i1 %matches, label %catch1, label %catch
146 catch1: ; preds = %catch.dispatch
147 %5 = call ptr @__cxa_begin_catch(ptr %2)
148 %6 = load i32, ptr %5, align 4
149 call void @__cxa_end_catch()
152 try.cont: ; preds = %catch, %catch1, %invoke.cont
155 catch: ; preds = %catch.dispatch
156 %7 = call ptr @__cxa_begin_catch(ptr %2)
157 call void @__cxa_end_catch()
161 declare void @foo(i32)
162 declare ptr @bar(i8, i8)
164 declare i32 @__gxx_personality_v0(...)
165 declare i32 @llvm.eh.typeid.for.p0(ptr)
166 declare ptr @__cxa_begin_catch(ptr)
167 declare void @__cxa_end_catch()
168 declare void @__cxa_call_unexpected(ptr)
170 ; JS glue functions and invoke wrappers declaration
171 ; CHECK-DAG: declare i32 @getTempRet0()
172 ; CHECK-DAG: declare void @__resumeException(ptr)
173 ; CHECK-DAG: declare void @__invoke_void_i32(ptr, i32)
174 ; CHECK-DAG: declare ptr @__cxa_find_matching_catch_4(ptr, ptr)