Files
clang-p2996/llvm/test/CodeGen/WebAssembly/lower-em-sjlj-debuginfo.ll
Heejin Ahn b8fc71b7ae [WebAssembly] Share rethrowing BBs in LowerEmscriptenEHSjLj
There are three kinds of "rethrowing" BBs in this pass:
1. In Emscripten SjLj, after a possibly longjmping function call, we
   check if the thrown longjmp corresponds to one of setjmps within the
   current function. If not, we rethrow the longjmp by calling
   `emscripten_longjmp`.
2. In Emscripten EH, after a possibly throwing function call, we check
   if the thrown exception corresponds to the current `catch` clauses.
   If not, we rethrow the exception by calling `__resumeException`.
3. When both Emscripten EH and SjLj are used, when we check for an
   exception after a possibly throwing function call, it is possible
   that we get not an exception but a longjmp. In this case, we
   shouldn't swallow it; we should rethrow the longjmp by calling
   `emscripten_longjmp`.
4. When both Emscripten EH and SjLj are used, when we check for a
   longjmp after a possibly longjmping function call, it is possible
   that we get not a longjmp but an exception. In this case, we
   shouldn't swallot it; we should rethrow the exception by calling
   `__resumeException`.

Case 1 is in Emscripten SjLj, 2 is in Emscripten EH, and 3 and 4 are
relevant when both Emscripten EH and SjLj are used. 3 and 4 were first
implemented in D106525.

We create BBs for 1, 3, and 4 in this pass. We create those BBs for
every throwing/longjmping function call, along with other BBs that
contain condition checks. What this CL does is to create a single BB
within a function for each of 1, 3, and 4 cases. These BBs are exiting
BBs in the function and thus don't have successors, so easy to be shared
between calls.

The names of BBs created are:
Case 1: `call.em.longjmp`
Case 3: `rethrow.exn`
Case 4: `rethrow.longjmp`

For the case 2 we don't currently create BBs; we only replace the
existing `resume` instruction with `call @__resumeException`. And Clang
already creates only a single `resume` BB per function and reuses it,
so we don't need to optimize this case.

Not sure what are good benchmarks for EH/SjLj, but this decreases the
size of the object file for `grfmt_jpeg.bc` (presumably from opencv) we
got from one of our users by 8.9%. Even after running `wasm-opt -O4` on
them, there is still 4.8% improvement.

Reviewed By: dschuff

Differential Revision: https://reviews.llvm.org/D108945
2021-08-30 21:44:34 -07:00

90 lines
3.4 KiB
LLVM

; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj -S | FileCheck %s
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"
%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
; Basic debug info test. All existing instructions have debug info and inserted
; 'malloc' and 'free' calls take debug info from the next instruction.
define void @setjmp_debug_info0() !dbg !3 {
; CHECK-LABEL: @setjmp_debug_info0
entry:
%buf = alloca [1 x %struct.__jmp_buf_tag], align 16, !dbg !4
%arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0, !dbg !5
%call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0, !dbg !6
call void @foo(), !dbg !7
ret void, !dbg !8
; CHECK: entry:
; CHECK-NEXT: call i8* @malloc(i32 40), !dbg ![[DL0:.*]]
; CHECK-NEXT: bitcast {{.*}}, !dbg ![[DL0]]
; CHECK: entry.split:
; CHECK: alloca {{.*}}, !dbg ![[DL0]]
; CHECK: call i32* @saveSetjmp{{.*}}, !dbg ![[DL1:.*]]
; CHECK-NEXT: call i32 @getTempRet0{{.*}}, !dbg ![[DL1]]
; CHECK-NEXT: br {{.*}}, !dbg ![[DL2:.*]]
; CHECK: entry.split.split:
; CHECK: call {{.*}} void @__invoke_void{{.*}}, !dbg ![[DL2]]
; CHECK: entry.split.split.split:
; CHECK-NEXT: bitcast {{.*}}, !dbg ![[DL3:.*]]
; CHECK-NEXT: call void @free{{.*}}, !dbg ![[DL3]]
; CHECK: if.then1:
; CHECK: call i32 @testSetjmp{{.*}}, !dbg ![[DL2]]
; CHECK: if.end:
; CHECK: call i32 @getTempRet0{{.*}}, !dbg ![[DL2]]
; CHECK: call.em.longjmp:
; CHECK: call void @emscripten_longjmp{{.*}}, !dbg ![[DL2]]
; CHECK: if.end2:
; CHECK: call void @setTempRet0{{.*}}, !dbg ![[DL2]]
}
; No instruction has debug info but the current function (setjmp_debug_info2)
; and the called function (malloc / free) have DISubprograms, so the newly
; generated calls should have debug info attached. We don't have an instruction
; to take debug info from, so we create dummy debug info.
define void @setjmp_debug_info1() !dbg !9 {
; CHECK-LABEL: @setjmp_debug_info1
entry:
%buf = alloca [1 x %struct.__jmp_buf_tag], align 16
%arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
%call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
call void @foo()
ret void
; CHECK: call i8* @malloc(i32 40), !dbg ![[DL_DUMMY:.*]]
; CHECK: call void @free{{.*}}, !dbg ![[DL_DUMMY]]
}
; Note that these functions have DISubprograms.
declare !dbg !10 i8* @malloc(i32)
declare !dbg !11 void @free(i8*)
declare void @foo()
; Function Attrs: returns_twice
declare i32 @setjmp(%struct.__jmp_buf_tag*) #0
!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !DIFile(filename: "lower-em-sjlj.c", directory: "test")
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
!3 = distinct !DISubprogram(name: "setjmp_debug_info0", unit:!2, file: !1, line: 1)
!4 = !DILocation(line:2, scope: !3)
!5 = !DILocation(line:3, scope: !3)
!6 = !DILocation(line:4, scope: !3)
!7 = !DILocation(line:5, scope: !3)
!8 = !DILocation(line:6, scope: !3)
!9 = distinct !DISubprogram(name: "setjmp_debug_info1", unit:!2, file: !1, line: 50)
!10 = !DISubprogram(name: "malloc", file: !1, line: 10, isDefinition: false)
!11 = !DISubprogram(name: "free", file: !1, line: 20, isDefinition: false)
; Dummy debug info generated
; CHECK: ![[DL_DUMMY]] = !DILocation(line: 50, column: 1, scope: !9)