1; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj -S | FileCheck %s -DPTR=i32 2; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj -S --mattr=+atomics,+bulk-memory | FileCheck %s -DPTR=i32 3; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj --mtriple=wasm64-unknown-unknown -data-layout="e-m:e-p:64:64-i64:64-n32:64-S128" -S | FileCheck %s -DPTR=i64 4 5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" 6target triple = "wasm32-unknown-unknown" 7 8%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] } 9 10@global_var = global i32 0, align 4 11@global_longjmp_ptr = global ptr @longjmp, align 4 12; CHECK-DAG: @__THREW__ = external thread_local global [[PTR]] 13; CHECK-DAG: @__threwValue = external thread_local global i32 14; CHECK-DAG: @global_longjmp_ptr = global ptr @emscripten_longjmp 15 16; Test a simple setjmp - longjmp sequence 17define void @setjmp_longjmp() { 18; CHECK-LABEL: @setjmp_longjmp 19entry: 20 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 21 %call = call i32 @setjmp(ptr %buf) #0 22 call void @longjmp(ptr %buf, i32 1) #1 23 unreachable 24; CHECK: entry: 25; CHECK-NEXT: %functionInvocationId = alloca i32, align 4 26; CHECK-NEXT: br label %entry.split 27 28; CHECK: entry.split 29; CHECK-NEXT: %[[BUF:.*]] = alloca [1 x %struct.__jmp_buf_tag] 30; CHECK-NEXT: call void @__wasm_setjmp(ptr %[[BUF]], i32 1, ptr %functionInvocationId) 31; CHECK-NEXT: br label %entry.split.split 32 33; CHECK: entry.split.split: 34; CHECK-NEXT: phi i32 [ 0, %entry.split ], [ %[[LONGJMP_RESULT:.*]], %if.end ] 35; CHECK-NEXT: %[[JMPBUF:.*]] = ptrtoint ptr %[[BUF]] to [[PTR]] 36; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__ 37; CHECK-NEXT: call cc{{.*}} void @__invoke_void_[[PTR]]_i32(ptr @emscripten_longjmp, [[PTR]] %[[JMPBUF]], i32 1) 38; CHECK-NEXT: %[[__THREW__VAL:.*]] = load [[PTR]], ptr @__THREW__ 39; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__ 40; CHECK-NEXT: %[[CMP0:.*]] = icmp ne [[PTR]] %__THREW__.val, 0 41; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, ptr @__threwValue 42; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0 43; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]] 44; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1 45 46; CHECK: entry.split.split.split: 47; CHECK-NEXT: unreachable 48 49; CHECK: if.then1: 50; CHECK-NEXT: %[[__THREW__VAL_P:.*]] = inttoptr [[PTR]] %[[__THREW__VAL]] to ptr 51; CHECK-NEXT: %[[LABEL:.*]] = call i32 @__wasm_setjmp_test(ptr %[[__THREW__VAL_P]], ptr %functionInvocationId) 52; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[LABEL]], 0 53; CHECK-NEXT: br i1 %[[CMP]], label %call.em.longjmp, label %if.end2 54 55; CHECK: if.else1: 56; CHECK-NEXT: br label %if.end 57 58; CHECK: if.end: 59; CHECK-NEXT: %[[LABEL_PHI:.*]] = phi i32 [ %[[LABEL:.*]], %if.end2 ], [ -1, %if.else1 ] 60; CHECK-NEXT: %[[LONGJMP_RESULT]] = call i32 @getTempRet0() 61; CHECK-NEXT: switch i32 %[[LABEL_PHI]], label %entry.split.split.split [ 62; CHECK-NEXT: i32 1, label %entry.split.split 63; CHECK-NEXT: ] 64 65; CHECK: call.em.longjmp: 66; CHECK-NEXT: %threw.phi = phi [[PTR]] [ %[[__THREW__VAL]], %if.then1 ] 67; CHECK-NEXT: %threwvalue.phi = phi i32 [ %[[THREWVALUE_VAL]], %if.then1 ] 68; CHECK-NEXT: call void @emscripten_longjmp([[PTR]] %threw.phi, i32 %threwvalue.phi) 69; CHECK-NEXT: unreachable 70 71; CHECK: if.end2: 72; CHECK-NEXT: call void @setTempRet0(i32 %[[THREWVALUE_VAL]]) 73; CHECK-NEXT: br label %if.end 74} 75 76; Test a case of a function call (which is not longjmp) after a setjmp 77define void @setjmp_longjmpable_call() { 78; CHECK-LABEL: @setjmp_longjmpable_call 79entry: 80 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 81 %call = call i32 @setjmp(ptr %buf) #0 82 call void @foo() 83 ret void 84; CHECK: entry: 85; CHECK: call void @__wasm_setjmp( 86 87; CHECK: entry.split.split: 88; CHECK: @__invoke_void(ptr @foo) 89 90; CHECK: entry.split.split.split: 91; CHECK-NEXT: ret void 92} 93 94; When there are multiple longjmpable calls after setjmp. In this test we 95; specifically check if 'call.em.longjmp' BB, which rethrows longjmps by calling 96; emscripten_longjmp for ones that are not for this function's setjmp, is 97; correctly created for multiple predecessors. 98define void @setjmp_multiple_longjmpable_calls() { 99; CHECK-LABEL: @setjmp_multiple_longjmpable_calls 100entry: 101 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 102 %call = call i32 @setjmp(ptr %buf) #0 103 call void @foo() 104 call void @foo() 105 ret void 106; CHECK: call.em.longjmp: 107; CHECK-NEXT: %threw.phi = phi [[PTR]] [ %__THREW__.val, %if.then1 ], [ %__THREW__.val2, %if.then13 ] 108; CHECK-NEXT: %threwvalue.phi = phi i32 [ %__threwValue.val, %if.then1 ], [ %__threwValue.val6, %if.then13 ] 109; CHECK-NEXT: call void @emscripten_longjmp([[PTR]] %threw.phi, i32 %threwvalue.phi) 110; CHECK-NEXT: unreachable 111} 112 113; Test a case where a function has a setjmp call but no other calls that can 114; longjmp. We don't need to do any transformation in this case. 115define i32 @setjmp_only(ptr %ptr) { 116; CHECK-LABEL: @setjmp_only 117entry: 118 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 119 %call = call i32 @setjmp(ptr %buf) #0 120 ; free cannot longjmp 121 call void @free(ptr %ptr) 122 ret i32 %call 123; CHECK: entry: 124; CHECK-NOT: @malloc 125; CHECK-NOT: %setjmpTable 126; CHECK-NOT: @saveSetjmp 127; CHECK-NOT: @testSetjmp 128; The remaining setjmp call is converted to constant 0, because setjmp returns 0 129; when called directly. 130; CHECK: ret i32 0 131} 132 133; Test SSA validity 134define void @ssa(i32 %n) { 135; CHECK-LABEL: @ssa 136entry: 137 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 138 %cmp = icmp sgt i32 %n, 5 139 br i1 %cmp, label %if.then, label %if.end 140; CHECK: entry: 141 142if.then: ; preds = %entry 143 %0 = load i32, ptr @global_var, align 4 144 %call = call i32 @setjmp(ptr %buf) #0 145 store i32 %0, ptr @global_var, align 4 146 br label %if.end 147; CHECK: if.then: 148; CHECK: %[[VAR0:.*]] = load i32, ptr @global_var, align 4 149; CHECK: call void @__wasm_setjmp( 150 151; CHECK: if.then.split: 152; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR2:.*]], %if.end1 ], [ %[[VAR0]], %if.then ] 153; CHECK: store i32 %[[VAR1]], ptr @global_var, align 4 154 155if.end: ; preds = %if.then, %entry 156 call void @longjmp(ptr %buf, i32 5) #1 157 unreachable 158; CHECK: if.end: 159; CHECK: %[[VAR2]] = phi i32 [ %[[VAR1]], %if.then.split ], [ undef, %entry.split ] 160} 161 162; Test a case when a function only calls other functions that are neither setjmp nor longjmp 163define void @other_func_only() { 164; CHECK-LABEL: @other_func_only 165entry: 166 call void @foo() 167 ret void 168; CHECK: call void @foo() 169} 170 171; Test inline asm handling 172define void @inline_asm() { 173; CHECK-LABEL: @inline_asm 174entry: 175 %env = alloca [1 x %struct.__jmp_buf_tag], align 16 176 %call = call i32 @setjmp(ptr %env) #4 177; Inline assembly should not generate __invoke wrappers. 178; Doing so would fail as inline assembly cannot be passed as a function pointer. 179; CHECK: call void asm sideeffect "", ""() 180; CHECK-NOT: __invoke_void 181 call void asm sideeffect "", ""() 182 ret void 183} 184 185; Test that the allocsize attribute is being transformed properly 186declare ptr @allocator(i32, ptr) #3 187define ptr @allocsize() { 188; CHECK-LABEL: @allocsize 189entry: 190 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 191 %call = call i32 @setjmp(ptr %buf) #0 192; CHECK: call cc{{.*}} ptr @__invoke_ptr_i32_ptr([[ARGS:.*]]) #[[ALLOCSIZE_ATTR:[0-9]+]] 193 %alloc = call ptr @allocator(i32 20, ptr %buf) #3 194 ret ptr %alloc 195} 196 197; Test a case when a function only calls longjmp and not setjmp 198@buffer = global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16 199define void @longjmp_only() { 200; CHECK-LABEL: @longjmp_only 201entry: 202 ; CHECK: call void @emscripten_longjmp 203 call void @longjmp(ptr @buffer, i32 1) #1 204 unreachable 205} 206 207; Tests if SSA rewrite works when a use and its def are within the same BB. 208define void @ssa_rewite_in_same_bb() { 209; CHECK-LABEL: @ssa_rewite_in_same_bb 210entry: 211 call void @foo() 212 br label %for.cond 213 214for.cond: ; preds = %for.inc, %entry 215 ; CHECK: %{{.*}} = phi i32 [ %var[[VARNO:.*]], %for.inc.split ] 216 %0 = phi i32 [ %var, %for.inc ], [ undef, %entry ] 217 %var = add i32 0, 0 218 br label %for.inc 219 220for.inc: ; preds = %for.cond 221 %call5 = call i32 @setjmp(ptr undef) #0 222 br label %for.cond 223 224; CHECK: for.inc.split: 225 ; CHECK: %var[[VARNO]] = phi i32 [ undef, %if.end ], [ %var, %for.inc ] 226} 227 228; Tests cases where longjmp function pointer is used in other ways than direct 229; calls. longjmps should be replaced with 230; (void(*)(jmp_buf*, int))emscripten_longjmp. 231declare void @take_longjmp(ptr %arg_ptr) 232define void @indirect_longjmp() { 233; CHECK-LABEL: @indirect_longjmp 234entry: 235 %local_longjmp_ptr = alloca ptr, align 4 236 %buf0 = alloca [1 x %struct.__jmp_buf_tag], align 16 237 %buf1 = alloca [1 x %struct.__jmp_buf_tag], align 16 238 239 ; Store longjmp in a local variable, load it, and call it 240 store ptr @longjmp, ptr %local_longjmp_ptr, align 4 241 ; CHECK: store ptr @emscripten_longjmp, ptr %local_longjmp_ptr, align 4 242 %longjmp_from_local_ptr = load ptr, ptr %local_longjmp_ptr, align 4 243 call void %longjmp_from_local_ptr(ptr %buf0, i32 0) 244 245 ; Load longjmp from a global variable and call it 246 %longjmp_from_global_ptr = load ptr, ptr @global_longjmp_ptr, align 4 247 call void %longjmp_from_global_ptr(ptr %buf1, i32 0) 248 249 ; Pass longjmp as a function argument. This is a call but longjmp is not a 250 ; callee but an argument. 251 call void @take_longjmp(ptr @longjmp) 252 ; CHECK: call void @take_longjmp(ptr @emscripten_longjmp) 253 ret void 254} 255 256; Test if _setjmp and _longjmp calls are treated in the same way as setjmp and 257; longjmp 258define void @_setjmp__longjmp() { 259; CHECK-LABEL: @_setjmp__longjmp 260; These calls should have been transformed away 261; CHECK-NOT: call i32 @_setjmp 262; CHECK-NOT: call void @_longjmp 263entry: 264 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 265 %call = call i32 @_setjmp(ptr %buf) #0 266 call void @_longjmp(ptr %buf, i32 1) #1 267 unreachable 268} 269 270; Function Attrs: nounwind 271declare void @foo() #2 272; Function Attrs: returns_twice 273declare i32 @setjmp(ptr) #0 274declare i32 @_setjmp(ptr) #0 275; Function Attrs: noreturn 276declare void @longjmp(ptr, i32) #1 277declare void @_longjmp(ptr, i32) #1 278declare i32 @__gxx_personality_v0(...) 279declare ptr @__cxa_begin_catch(ptr) 280declare void @__cxa_end_catch() 281declare void @free(ptr) 282 283; JS glue functions and invoke wrappers declaration 284; CHECK-DAG: declare i32 @getTempRet0() 285; CHECK-DAG: declare void @setTempRet0(i32) 286; CHECK-DAG: declare void @__wasm_setjmp(ptr, i32, ptr) 287; CHECK-DAG: declare i32 @__wasm_setjmp_test(ptr, ptr) 288; CHECK-DAG: declare void @emscripten_longjmp([[PTR]], i32) 289; CHECK-DAG: declare void @__invoke_void(ptr) 290 291attributes #0 = { returns_twice } 292attributes #1 = { noreturn } 293attributes #2 = { nounwind } 294attributes #3 = { allocsize(0) } 295; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="getTempRet0" } 296; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="setTempRet0" } 297; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void" } 298; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__wasm_setjmp" } 299; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__wasm_setjmp_test" } 300; CHECK-DAG: attributes #{{[0-9]+}} = { noreturn "wasm-import-module"="env" "wasm-import-name"="emscripten_longjmp" } 301; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_ptr_i32_ptr" } 302; CHECK-DAG: attributes #[[ALLOCSIZE_ATTR]] = { allocsize(1) } 303 304!llvm.dbg.cu = !{!2} 305!llvm.module.flags = !{!0} 306 307!0 = !{i32 2, !"Debug Info Version", i32 3} 308!1 = !DIFile(filename: "lower-em-sjlj.c", directory: "test") 309!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1) 310!3 = distinct !DISubprogram(name: "setjmp_debug_info", unit:!2, file: !1, line: 1) 311!4 = !DILocation(line:2, scope: !3) 312!5 = !DILocation(line:3, scope: !3) 313!6 = !DILocation(line:4, scope: !3) 314!7 = !DILocation(line:5, scope: !3) 315!8 = !DILocation(line:6, scope: !3) 316