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