311 lines · plain
1; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj -S | FileCheck %s -DPTR=i322; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj -S --mattr=+atomics,+bulk-memory | FileCheck %s -DPTR=i323; 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=i644 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 411@global_longjmp_ptr = global ptr @longjmp, align 412; CHECK-DAG: @__THREW__ = external thread_local global [[PTR]]13; CHECK-DAG: @__threwValue = external thread_local global i3214; CHECK-DAG: @global_longjmp_ptr = global ptr @emscripten_longjmp15 16; Test a simple setjmp - longjmp sequence17define void @setjmp_longjmp() {18; CHECK-LABEL: @setjmp_longjmp19entry:20 %buf = alloca [1 x %struct.__jmp_buf_tag], align 1621 %call = call i32 @setjmp(ptr %buf) #022 call void @longjmp(ptr %buf, i32 1) #123 unreachable24; CHECK: entry:25; CHECK-NEXT: %buf = alloca [1 x %struct.__jmp_buf_tag], align 1626; CHECK-NEXT: %functionInvocationId = alloca i32, align 427; CHECK-NEXT: br label %entry.split28 29; CHECK: entry.split30; CHECK-NEXT: call void @__wasm_setjmp(ptr %buf, i32 1, ptr %functionInvocationId)31; CHECK-NEXT: br label %entry.split.split32 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, 041; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, ptr @__threwValue42; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 043; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]44; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else145 46; CHECK: entry.split.split.split:47; CHECK-NEXT: unreachable48 49; CHECK: if.then1:50; CHECK-NEXT: %[[__THREW__VAL_P:.*]] = inttoptr [[PTR]] %[[__THREW__VAL]] to ptr51; CHECK-NEXT: %[[LABEL:.*]] = call i32 @__wasm_setjmp_test(ptr %[[__THREW__VAL_P]], ptr %functionInvocationId)52; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[LABEL]], 053; CHECK-NEXT: br i1 %[[CMP]], label %call.em.longjmp, label %if.end254 55; CHECK: if.else1:56; CHECK-NEXT: br label %if.end57 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.split63; 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: unreachable70 71; CHECK: if.end2:72; CHECK-NEXT: call void @setTempRet0(i32 %[[THREWVALUE_VAL]])73; CHECK-NEXT: br label %if.end74}75 76; Test a case of a function call (which is not longjmp) after a setjmp77define void @setjmp_longjmpable_call() {78; CHECK-LABEL: @setjmp_longjmpable_call79entry:80 %buf = alloca [1 x %struct.__jmp_buf_tag], align 1681 %call = call i32 @setjmp(ptr %buf) #082 call void @foo()83 ret void84; 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 void92}93 94; When there are multiple longjmpable calls after setjmp. In this test we95; specifically check if 'call.em.longjmp' BB, which rethrows longjmps by calling96; emscripten_longjmp for ones that are not for this function's setjmp, is97; correctly created for multiple predecessors.98define void @setjmp_multiple_longjmpable_calls() {99; CHECK-LABEL: @setjmp_multiple_longjmpable_calls100entry:101 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16102 %call = call i32 @setjmp(ptr %buf) #0103 call void @foo()104 call void @foo()105 ret void106; 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: unreachable111}112 113; Test a case where a function has a setjmp call but no other calls that can114; longjmp. We don't need to do any transformation in this case.115define i32 @setjmp_only(ptr %ptr) {116; CHECK-LABEL: @setjmp_only117entry:118 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16119 %call = call i32 @setjmp(ptr %buf) #0120 ; free cannot longjmp121 call void @free(ptr %ptr)122 ret i32 %call123; CHECK: entry:124; CHECK-NOT: @malloc125; CHECK-NOT: %setjmpTable126; CHECK-NOT: @saveSetjmp127; CHECK-NOT: @testSetjmp128; The remaining setjmp call is converted to constant 0, because setjmp returns 0129; when called directly.130; CHECK: ret i32 0131}132 133; Test SSA validity134define void @ssa(i32 %n) {135; CHECK-LABEL: @ssa136entry:137 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16138 %cmp = icmp sgt i32 %n, 5139 br i1 %cmp, label %if.then, label %if.end140; CHECK: entry:141 142if.then: ; preds = %entry143 %0 = load i32, ptr @global_var, align 4144 %call = call i32 @setjmp(ptr %buf) #0145 store i32 %0, ptr @global_var, align 4146 br label %if.end147; CHECK: if.then:148; CHECK: %[[VAR0:.*]] = load i32, ptr @global_var, align 4149; 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 4154 155if.end: ; preds = %if.then, %entry156 call void @longjmp(ptr %buf, i32 5) #1157 unreachable158; 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 longjmp163define void @other_func_only() {164; CHECK-LABEL: @other_func_only165entry:166 call void @foo()167 ret void168; CHECK: call void @foo()169}170 171; Test inline asm handling172define void @inline_asm() {173; CHECK-LABEL: @inline_asm174entry:175 %env = alloca [1 x %struct.__jmp_buf_tag], align 16176 %call = call i32 @setjmp(ptr %env) #4177; 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_void181 call void asm sideeffect "", ""()182 ret void183}184 185; Test that the allocsize attribute is being transformed properly186declare ptr @allocator(i32, ptr) #3187define ptr @allocsize() {188; CHECK-LABEL: @allocsize189entry:190 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16191 %call = call i32 @setjmp(ptr %buf) #0192; CHECK: call cc{{.*}} ptr @__invoke_ptr_i32_ptr([[ARGS:.*]]) #[[ALLOCSIZE_ATTR:[0-9]+]]193 %alloc = call ptr @allocator(i32 20, ptr %buf) #3194 ret ptr %alloc195}196 197; Test a case when a function only calls longjmp and not setjmp198@buffer = global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16199define void @longjmp_only() {200; CHECK-LABEL: @longjmp_only201entry:202 ; CHECK: call void @emscripten_longjmp203 call void @longjmp(ptr @buffer, i32 1) #1204 unreachable205}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_bb210entry:211 call void @foo()212 br label %for.cond213 214for.cond: ; preds = %for.inc, %entry215 ; CHECK: %{{.*}} = phi i32 [ %var[[VARNO:.*]], %for.inc.split ]216 %0 = phi i32 [ %var, %for.inc ], [ undef, %entry ]217 %var = add i32 0, 0218 br label %for.inc219 220for.inc: ; preds = %for.cond221 %call5 = call i32 @setjmp(ptr undef) #0222 br label %for.cond223 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 direct229; calls. longjmps should be replaced with230; (void(*)(jmp_buf*, int))emscripten_longjmp.231declare void @take_longjmp(ptr %arg_ptr)232define void @indirect_longjmp() {233; CHECK-LABEL: @indirect_longjmp234entry:235 %local_longjmp_ptr = alloca ptr, align 4236 %buf0 = alloca [1 x %struct.__jmp_buf_tag], align 16237 %buf1 = alloca [1 x %struct.__jmp_buf_tag], align 16238 239 ; Store longjmp in a local variable, load it, and call it240 store ptr @longjmp, ptr %local_longjmp_ptr, align 4241 ; CHECK: store ptr @emscripten_longjmp, ptr %local_longjmp_ptr, align 4242 %longjmp_from_local_ptr = load ptr, ptr %local_longjmp_ptr, align 4243 call void %longjmp_from_local_ptr(ptr %buf0, i32 0)244 245 ; Load longjmp from a global variable and call it246 %longjmp_from_global_ptr = load ptr, ptr @global_longjmp_ptr, align 4247 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 a250 ; callee but an argument.251 call void @take_longjmp(ptr @longjmp)252 ; CHECK: call void @take_longjmp(ptr @emscripten_longjmp)253 ret void254}255 256; Test if _setjmp and _longjmp calls are treated in the same way as setjmp and257; longjmp258define void @_setjmp__longjmp() {259; CHECK-LABEL: @_setjmp__longjmp260; These calls should have been transformed away261; CHECK-NOT: call i32 @_setjmp262; CHECK-NOT: call void @_longjmp263entry:264 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16265 %call = call i32 @_setjmp(ptr %buf) #0266 call void @_longjmp(ptr %buf, i32 1) #1267 unreachable268}269 270; Function Attrs: nounwind271declare void @foo() #2272; Function Attrs: returns_twice273declare i32 @setjmp(ptr) #0274declare i32 @_setjmp(ptr) #0275; Function Attrs: noreturn276declare void @longjmp(ptr, i32) #1277declare void @_longjmp(ptr, i32) #1278declare 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 declaration284; 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]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void" }296; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_ptr_i32_ptr" }297; CHECK-DAG: attributes #[[ALLOCSIZE_ATTR]] = { allocsize(1) }298 299!llvm.dbg.cu = !{!2}300!llvm.module.flags = !{!0}301 302!0 = !{i32 2, !"Debug Info Version", i32 3}303!1 = !DIFile(filename: "lower-em-sjlj.c", directory: "test")304!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)305!3 = distinct !DISubprogram(name: "setjmp_debug_info", unit:!2, file: !1, line: 1)306!4 = !DILocation(line:2, scope: !3)307!5 = !DILocation(line:3, scope: !3)308!6 = !DILocation(line:4, scope: !3)309!7 = !DILocation(line:5, scope: !3)310!8 = !DILocation(line:6, scope: !3)311