2016-09-02 05:05:15 +08:00
|
|
|
; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
|
|
|
|
|
|
|
|
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
|
2018-05-11 01:49:11 +08:00
|
|
|
target triple = "wasm32-unknown-unknown"
|
2016-09-02 05:05:15 +08:00
|
|
|
|
|
|
|
%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
|
|
|
|
|
2020-02-07 11:29:01 +08:00
|
|
|
@global_var = global i32 0, align 4
|
2018-10-03 06:12:15 +08:00
|
|
|
; CHECK-DAG: __THREW__ = external global i32
|
|
|
|
; CHECK-DAG: __threwValue = external global i32
|
2016-09-02 05:05:15 +08:00
|
|
|
|
|
|
|
; Test a simple setjmp - longjmp sequence
|
2020-02-07 11:29:01 +08:00
|
|
|
define void @setjmp_longjmp() {
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-LABEL: @setjmp_longjmp
|
|
|
|
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
|
|
|
|
%arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
|
|
|
|
call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 1) #1
|
|
|
|
unreachable
|
|
|
|
; CHECK: entry:
|
|
|
|
; CHECK-NEXT: %[[MALLOCCALL:.*]] = tail call i8* @malloc(i32 40)
|
|
|
|
; CHECK-NEXT: %[[SETJMP_TABLE:.*]] = bitcast i8* %[[MALLOCCALL]] to i32*
|
|
|
|
; CHECK-NEXT: store i32 0, i32* %[[SETJMP_TABLE]]
|
|
|
|
; CHECK-NEXT: %[[SETJMP_TABLE_SIZE:.*]] = add i32 4, 0
|
|
|
|
; CHECK-NEXT: %[[BUF:.*]] = alloca [1 x %struct.__jmp_buf_tag]
|
|
|
|
; CHECK-NEXT: %[[ARRAYDECAY:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
|
|
|
|
; CHECK-NEXT: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(%struct.__jmp_buf_tag* %[[ARRAYDECAY]], i32 1, i32* %[[SETJMP_TABLE]], i32 %[[SETJMP_TABLE_SIZE]])
|
2018-11-21 03:25:07 +08:00
|
|
|
; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-NEXT: br label %entry.split
|
|
|
|
|
|
|
|
; CHECK: entry.split:
|
|
|
|
; CHECK-NEXT: phi i32 [ 0, %entry ], [ %[[LONGJMP_RESULT:.*]], %if.end ]
|
|
|
|
; CHECK-NEXT: %[[ARRAYDECAY1:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
|
2018-07-18 00:40:03 +08:00
|
|
|
; CHECK-NEXT: store i32 0, i32* @__THREW__
|
[WebAssembly] Fix conflict between ret legalization and sjlj
Summary:
When the WebAssembly backend encounters a return type that doesn't
fit within i32, SelectionDAG performs sret demotion, adding an
additional argument to the start of the function that contains
a pointer to an sret buffer to use instead. However, this conflicts
with the emscripten sjlj lowering pass. There we translate calls like:
```
call {i32, i32} @foo()
```
into (in pseudo-llvm)
```
%addr = @foo
call {i32, i32} @__invoke_{i32,i32}(%addr)
```
i.e. we perform an indirect call through an extra function.
However, the sret transform now transforms this into
the equivalent of
```
%addr = @foo
%sret = alloca {i32, i32}
call {i32, i32} @__invoke_{i32,i32}(%sret, %addr)
```
(while simultaneously translation the implementation of @foo as well).
Unfortunately, this doesn't work out. The __invoke_ ABI expected
the function address to be the first argument, causing crashes.
There is several possible ways to fix this:
1. Implementing the sret rewrite at the IR level as well and performing
it as part of lowering to __invoke
2. Fixing the wasm backend to recognize that __invoke has a special ABI
3. A change to the binaryen/emscripten ABI to recognize this situation
This revision implements the middle option, teaching the backend to
treat __invoke_ functions specially in sret lowering. This is achieved
by
1) Introducing a new CallingConv ID for invoke functions
2) When this CallingConv ID is seen in the backend and the first argument
is marked as sret (a function pointer would never be marked as sret),
swapping the first two arguments.
Reviewed By: tlively, aheejin
Differential Revision: https://reviews.llvm.org/D65463
llvm-svn: 367935
2019-08-06 05:36:09 +08:00
|
|
|
; CHECK-NEXT: call cc{{.*}} void @"__invoke_void_%struct.__jmp_buf_tag*_i32"(void (%struct.__jmp_buf_tag*, i32)* @emscripten_longjmp_jmpbuf, %struct.__jmp_buf_tag* %[[ARRAYDECAY1]], i32 1)
|
2018-07-18 00:40:03 +08:00
|
|
|
; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
|
|
|
|
; CHECK-NEXT: store i32 0, i32* @__THREW__
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-NEXT: %[[CMP0:.*]] = icmp ne i32 %__THREW__.val, 0
|
2018-07-18 00:40:03 +08:00
|
|
|
; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, i32* @__threwValue
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
|
|
|
|
; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
|
|
|
|
; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
|
|
|
|
|
|
|
|
; CHECK: entry.split.split:
|
|
|
|
; CHECK-NEXT: unreachable
|
|
|
|
|
|
|
|
; CHECK: if.then1:
|
|
|
|
; CHECK-NEXT: %[[__THREW__VAL_I32P:.*]] = inttoptr i32 %[[__THREW__VAL]] to i32*
|
|
|
|
; CHECK-NEXT: %[[__THREW__VAL_I32P_LOADED:.*]] = load i32, i32* %[[__THREW__VAL_I32P]]
|
|
|
|
; CHECK-NEXT: %[[LABEL:.*]] = call i32 @testSetjmp(i32 %[[__THREW__VAL_I32P_LOADED]], i32* %[[SETJMP_TABLE1]], i32 %[[SETJMP_TABLE_SIZE1]])
|
|
|
|
; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[LABEL]], 0
|
|
|
|
; CHECK-NEXT: br i1 %[[CMP]], label %if.then2, label %if.end2
|
|
|
|
|
|
|
|
; CHECK: if.else1:
|
|
|
|
; CHECK-NEXT: br label %if.end
|
|
|
|
|
|
|
|
; CHECK: if.end:
|
|
|
|
; CHECK-NEXT: %[[LABEL_PHI:.*]] = phi i32 [ %[[LABEL:.*]], %if.end2 ], [ -1, %if.else1 ]
|
2018-11-21 03:25:07 +08:00
|
|
|
; CHECK-NEXT: %[[LONGJMP_RESULT]] = call i32 @getTempRet0()
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-NEXT: switch i32 %[[LABEL_PHI]], label %entry.split.split [
|
|
|
|
; CHECK-NEXT: i32 1, label %entry.split
|
|
|
|
; CHECK-NEXT: ]
|
|
|
|
|
|
|
|
; CHECK: if.then2:
|
|
|
|
; CHECK-NEXT: call void @emscripten_longjmp(i32 %[[__THREW__VAL]], i32 %[[THREWVALUE_VAL]])
|
|
|
|
; CHECK-NEXT: unreachable
|
|
|
|
|
|
|
|
; CHECK: if.end2:
|
2018-11-21 03:25:07 +08:00
|
|
|
; CHECK-NEXT: call void @setTempRet0(i32 %[[THREWVALUE_VAL]])
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-NEXT: br label %if.end
|
|
|
|
}
|
|
|
|
|
|
|
|
; Test a case of a function call (which is not longjmp) after a setjmp
|
2020-02-07 11:29:01 +08:00
|
|
|
define void @setjmp_other() {
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-LABEL: @setjmp_other
|
|
|
|
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: entry:
|
|
|
|
; CHECK: %[[SETJMP_TABLE:.*]] = call i32* @saveSetjmp(
|
|
|
|
|
|
|
|
; CHECK: entry.split:
|
[WebAssembly] Fix conflict between ret legalization and sjlj
Summary:
When the WebAssembly backend encounters a return type that doesn't
fit within i32, SelectionDAG performs sret demotion, adding an
additional argument to the start of the function that contains
a pointer to an sret buffer to use instead. However, this conflicts
with the emscripten sjlj lowering pass. There we translate calls like:
```
call {i32, i32} @foo()
```
into (in pseudo-llvm)
```
%addr = @foo
call {i32, i32} @__invoke_{i32,i32}(%addr)
```
i.e. we perform an indirect call through an extra function.
However, the sret transform now transforms this into
the equivalent of
```
%addr = @foo
%sret = alloca {i32, i32}
call {i32, i32} @__invoke_{i32,i32}(%sret, %addr)
```
(while simultaneously translation the implementation of @foo as well).
Unfortunately, this doesn't work out. The __invoke_ ABI expected
the function address to be the first argument, causing crashes.
There is several possible ways to fix this:
1. Implementing the sret rewrite at the IR level as well and performing
it as part of lowering to __invoke
2. Fixing the wasm backend to recognize that __invoke has a special ABI
3. A change to the binaryen/emscripten ABI to recognize this situation
This revision implements the middle option, teaching the backend to
treat __invoke_ functions specially in sret lowering. This is achieved
by
1) Introducing a new CallingConv ID for invoke functions
2) When this CallingConv ID is seen in the backend and the first argument
is marked as sret (a function pointer would never be marked as sret),
swapping the first two arguments.
Reviewed By: tlively, aheejin
Differential Revision: https://reviews.llvm.org/D65463
llvm-svn: 367935
2019-08-06 05:36:09 +08:00
|
|
|
; CHECK: @__invoke_void(void ()* @foo)
|
2016-09-02 05:05:15 +08:00
|
|
|
|
|
|
|
; CHECK: entry.split.split:
|
|
|
|
; CHECK-NEXT: %[[BUF:.*]] = bitcast i32* %[[SETJMP_TABLE]] to i8*
|
|
|
|
; CHECK-NEXT: tail call void @free(i8* %[[BUF]])
|
|
|
|
; CHECK-NEXT: ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; Test a case when a function call is within try-catch, after a setjmp
|
2020-02-07 11:29:01 +08:00
|
|
|
define void @exception_and_longjmp() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-LABEL: @exception_and_longjmp
|
|
|
|
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
|
|
|
|
invoke void @foo()
|
|
|
|
to label %try.cont unwind label %lpad
|
|
|
|
|
|
|
|
; CHECK: entry.split:
|
2018-07-18 00:40:03 +08:00
|
|
|
; CHECK: store i32 0, i32* @__THREW__
|
[WebAssembly] Fix conflict between ret legalization and sjlj
Summary:
When the WebAssembly backend encounters a return type that doesn't
fit within i32, SelectionDAG performs sret demotion, adding an
additional argument to the start of the function that contains
a pointer to an sret buffer to use instead. However, this conflicts
with the emscripten sjlj lowering pass. There we translate calls like:
```
call {i32, i32} @foo()
```
into (in pseudo-llvm)
```
%addr = @foo
call {i32, i32} @__invoke_{i32,i32}(%addr)
```
i.e. we perform an indirect call through an extra function.
However, the sret transform now transforms this into
the equivalent of
```
%addr = @foo
%sret = alloca {i32, i32}
call {i32, i32} @__invoke_{i32,i32}(%sret, %addr)
```
(while simultaneously translation the implementation of @foo as well).
Unfortunately, this doesn't work out. The __invoke_ ABI expected
the function address to be the first argument, causing crashes.
There is several possible ways to fix this:
1. Implementing the sret rewrite at the IR level as well and performing
it as part of lowering to __invoke
2. Fixing the wasm backend to recognize that __invoke has a special ABI
3. A change to the binaryen/emscripten ABI to recognize this situation
This revision implements the middle option, teaching the backend to
treat __invoke_ functions specially in sret lowering. This is achieved
by
1) Introducing a new CallingConv ID for invoke functions
2) When this CallingConv ID is seen in the backend and the first argument
is marked as sret (a function pointer would never be marked as sret),
swapping the first two arguments.
Reviewed By: tlively, aheejin
Differential Revision: https://reviews.llvm.org/D65463
llvm-svn: 367935
2019-08-06 05:36:09 +08:00
|
|
|
; CHECK-NEXT: call cc{{.*}} void @__invoke_void(void ()* @foo)
|
2018-07-18 00:40:03 +08:00
|
|
|
; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
|
|
|
|
; CHECK-NEXT: store i32 0, i32* @__THREW__
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-NEXT: %[[CMP0:.*]] = icmp ne i32 %[[__THREW__VAL]], 0
|
2018-07-18 00:40:03 +08:00
|
|
|
; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, i32* @__threwValue
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
|
|
|
|
; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
|
|
|
|
; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
|
|
|
|
|
|
|
|
; CHECK: entry.split.split:
|
|
|
|
; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[__THREW__VAL]], 1
|
|
|
|
; CHECK-NEXT: br i1 %[[CMP]], label %lpad, label %try.cont
|
|
|
|
|
|
|
|
lpad: ; preds = %entry
|
|
|
|
%0 = landingpad { i8*, i32 }
|
|
|
|
catch i8* null
|
|
|
|
%1 = extractvalue { i8*, i32 } %0, 0
|
|
|
|
%2 = extractvalue { i8*, i32 } %0, 1
|
|
|
|
%3 = call i8* @__cxa_begin_catch(i8* %1) #2
|
|
|
|
call void @__cxa_end_catch()
|
|
|
|
br label %try.cont
|
|
|
|
|
|
|
|
try.cont: ; preds = %entry, %lpad
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; Test SSA validity
|
2020-02-07 11:29:01 +08:00
|
|
|
define void @ssa(i32 %n) {
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-LABEL: @ssa
|
|
|
|
entry:
|
|
|
|
%buf = alloca [1 x %struct.__jmp_buf_tag], align 16
|
|
|
|
%cmp = icmp sgt i32 %n, 5
|
|
|
|
br i1 %cmp, label %if.then, label %if.end
|
|
|
|
; CHECK: entry:
|
|
|
|
; CHECK: %[[SETJMP_TABLE0:.*]] = bitcast i8*
|
|
|
|
; CHECK: %[[SETJMP_TABLE_SIZE0:.*]] = add i32 4, 0
|
|
|
|
|
|
|
|
if.then: ; preds = %entry
|
|
|
|
%0 = load i32, i32* @global_var, align 4
|
|
|
|
%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
|
|
|
|
store i32 %0, i32* @global_var, align 4
|
|
|
|
br label %if.end
|
|
|
|
; CHECK: if.then:
|
|
|
|
; CHECK: %[[VAR0:.*]] = load i32, i32* @global_var, align 4
|
|
|
|
; CHECK: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(
|
2018-11-21 03:25:07 +08:00
|
|
|
; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
|
2016-09-02 05:05:15 +08:00
|
|
|
|
|
|
|
; CHECK: if.then.split:
|
|
|
|
; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR0]], %if.then ], [ %[[VAR2:.*]], %if.end3 ]
|
|
|
|
; CHECK: %[[SETJMP_TABLE_SIZE2:.*]] = phi i32 [ %[[SETJMP_TABLE_SIZE1]], %if.then ], [ %[[SETJMP_TABLE_SIZE3:.*]], %if.end3 ]
|
|
|
|
; CHECK: %[[SETJMP_TABLE2:.*]] = phi i32* [ %[[SETJMP_TABLE1]], %if.then ], [ %[[SETJMP_TABLE3:.*]], %if.end3 ]
|
|
|
|
; CHECK: store i32 %[[VAR1]], i32* @global_var, align 4
|
|
|
|
|
|
|
|
if.end: ; preds = %if.then, %entry
|
|
|
|
%arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
|
|
|
|
call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 5) #1
|
|
|
|
unreachable
|
|
|
|
; CHECK: if.end:
|
|
|
|
; CHECK: %[[VAR2]] = phi i32 [ %[[VAR1]], %if.then.split ], [ undef, %entry ]
|
|
|
|
; CHECK: %[[SETJMP_TABLE_SIZE3]] = phi i32 [ %[[SETJMP_TABLE_SIZE2]], %if.then.split ], [ %[[SETJMP_TABLE_SIZE0]], %entry ]
|
|
|
|
; CHECK: %[[SETJMP_TABLE3]] = phi i32* [ %[[SETJMP_TABLE2]], %if.then.split ], [ %[[SETJMP_TABLE0]], %entry ]
|
|
|
|
}
|
|
|
|
|
|
|
|
; Test a case when a function only calls other functions that are neither setjmp nor longjmp
|
2020-02-07 11:29:01 +08:00
|
|
|
define void @only_other_func() {
|
2016-09-02 05:05:15 +08:00
|
|
|
entry:
|
|
|
|
call void @foo()
|
|
|
|
ret void
|
|
|
|
; CHECK: call void @foo()
|
|
|
|
}
|
|
|
|
|
|
|
|
; Test a case when a function only calls longjmp and not setjmp
|
2020-02-07 11:29:01 +08:00
|
|
|
define void @only_longjmp() {
|
2016-09-02 05:05:15 +08:00
|
|
|
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 void @longjmp(%struct.__jmp_buf_tag* %arraydecay, i32 5) #1
|
|
|
|
unreachable
|
|
|
|
; CHECK: %[[ARRAYDECAY:.*]] = getelementptr inbounds
|
2019-08-04 05:38:19 +08:00
|
|
|
; CHECK-NEXT: call void @emscripten_longjmp_jmpbuf(%struct.__jmp_buf_tag* %[[ARRAYDECAY]], i32 5)
|
2016-09-02 05:05:15 +08:00
|
|
|
}
|
[WebAssembly] Prevent inline assembly from being mangled by SjLj
Summary:
Before, inline assembly gets mangled by the SjLj transformation.
For example, in a function with setjmp/longjmp, this LLVM IR code
call void asm sideeffect "", ""()
would be transformed into
call void @__invoke_void(void ()* asm sideeffect "", "")
This is invalid, and results in the error:
Cannot take the address of an inline asm!
In this diff, we skip the transformation for inline assembly.
Reviewers: aheejin, tlively
Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64115
llvm-svn: 364985
2019-07-03 08:37:49 +08:00
|
|
|
|
|
|
|
; Test inline asm handling
|
2020-02-07 11:29:01 +08:00
|
|
|
define void @inline_asm() {
|
[WebAssembly] Prevent inline assembly from being mangled by SjLj
Summary:
Before, inline assembly gets mangled by the SjLj transformation.
For example, in a function with setjmp/longjmp, this LLVM IR code
call void asm sideeffect "", ""()
would be transformed into
call void @__invoke_void(void ()* asm sideeffect "", "")
This is invalid, and results in the error:
Cannot take the address of an inline asm!
In this diff, we skip the transformation for inline assembly.
Reviewers: aheejin, tlively
Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64115
llvm-svn: 364985
2019-07-03 08:37:49 +08:00
|
|
|
; CHECK-LABEL: @inline_asm
|
|
|
|
entry:
|
|
|
|
%env = alloca [1 x %struct.__jmp_buf_tag], align 16
|
|
|
|
%arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %env, i32 0, i32 0
|
|
|
|
%call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #4
|
2019-07-04 06:11:14 +08:00
|
|
|
; Inline assembly should not generate __invoke wrappers.
|
|
|
|
; Doing so would fail as inline assembly cannot be passed as a function pointer.
|
[WebAssembly] Prevent inline assembly from being mangled by SjLj
Summary:
Before, inline assembly gets mangled by the SjLj transformation.
For example, in a function with setjmp/longjmp, this LLVM IR code
call void asm sideeffect "", ""()
would be transformed into
call void @__invoke_void(void ()* asm sideeffect "", "")
This is invalid, and results in the error:
Cannot take the address of an inline asm!
In this diff, we skip the transformation for inline assembly.
Reviewers: aheejin, tlively
Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64115
llvm-svn: 364985
2019-07-03 08:37:49 +08:00
|
|
|
; CHECK: call void asm sideeffect "", ""()
|
2019-07-04 06:11:14 +08:00
|
|
|
; CHECK-NOT: __invoke_void
|
[WebAssembly] Prevent inline assembly from being mangled by SjLj
Summary:
Before, inline assembly gets mangled by the SjLj transformation.
For example, in a function with setjmp/longjmp, this LLVM IR code
call void asm sideeffect "", ""()
would be transformed into
call void @__invoke_void(void ()* asm sideeffect "", "")
This is invalid, and results in the error:
Cannot take the address of an inline asm!
In this diff, we skip the transformation for inline assembly.
Reviewers: aheejin, tlively
Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64115
llvm-svn: 364985
2019-07-03 08:37:49 +08:00
|
|
|
call void asm sideeffect "", ""()
|
|
|
|
ret void
|
|
|
|
}
|
2016-09-02 05:05:15 +08:00
|
|
|
|
2019-08-04 05:38:19 +08:00
|
|
|
; Test that the allocsize attribute is being transformed properly
|
|
|
|
declare i8 *@allocator(i32, %struct.__jmp_buf_tag*) #3
|
2020-02-07 11:29:01 +08:00
|
|
|
define i8 *@allocsize() {
|
2019-08-04 05:38:19 +08:00
|
|
|
; CHECK-LABEL: @allocsize
|
|
|
|
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
|
[WebAssembly] Fix conflict between ret legalization and sjlj
Summary:
When the WebAssembly backend encounters a return type that doesn't
fit within i32, SelectionDAG performs sret demotion, adding an
additional argument to the start of the function that contains
a pointer to an sret buffer to use instead. However, this conflicts
with the emscripten sjlj lowering pass. There we translate calls like:
```
call {i32, i32} @foo()
```
into (in pseudo-llvm)
```
%addr = @foo
call {i32, i32} @__invoke_{i32,i32}(%addr)
```
i.e. we perform an indirect call through an extra function.
However, the sret transform now transforms this into
the equivalent of
```
%addr = @foo
%sret = alloca {i32, i32}
call {i32, i32} @__invoke_{i32,i32}(%sret, %addr)
```
(while simultaneously translation the implementation of @foo as well).
Unfortunately, this doesn't work out. The __invoke_ ABI expected
the function address to be the first argument, causing crashes.
There is several possible ways to fix this:
1. Implementing the sret rewrite at the IR level as well and performing
it as part of lowering to __invoke
2. Fixing the wasm backend to recognize that __invoke has a special ABI
3. A change to the binaryen/emscripten ABI to recognize this situation
This revision implements the middle option, teaching the backend to
treat __invoke_ functions specially in sret lowering. This is achieved
by
1) Introducing a new CallingConv ID for invoke functions
2) When this CallingConv ID is seen in the backend and the first argument
is marked as sret (a function pointer would never be marked as sret),
swapping the first two arguments.
Reviewed By: tlively, aheejin
Differential Revision: https://reviews.llvm.org/D65463
llvm-svn: 367935
2019-08-06 05:36:09 +08:00
|
|
|
; CHECK: call cc{{.*}} i8* @"__invoke_i8*_i32_%struct.__jmp_buf_tag*"([[ARGS:.*]]) #[[ALLOCSIZE_ATTR:[0-9]+]]
|
2019-08-04 05:38:19 +08:00
|
|
|
%alloc = call i8* @allocator(i32 20, %struct.__jmp_buf_tag* %arraydecay) #3
|
|
|
|
ret i8 *%alloc
|
|
|
|
}
|
|
|
|
|
2019-08-27 05:41:17 +08:00
|
|
|
; Tests if program does not crash when there's no setjmp function calls in the
|
|
|
|
; module.
|
|
|
|
@buffer = global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16
|
|
|
|
define void @longjmp_only() {
|
|
|
|
entry:
|
|
|
|
; CHECK: call void @emscripten_longjmp_jmpbuf
|
|
|
|
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
|
|
|
|
unreachable
|
|
|
|
}
|
|
|
|
|
[WebAssembly] Fix SSA rebuilding in SjLj transformation
Summary:
Previously we skipped uses within the same BB as a def when rebuilding
SSA after SjLj transformation. For example, before transformation,
```
for.cond:
%0 = phi i32 [ %var, %for.inc ] ...
%var = ...
br label %for.inc
for.inc: ; preds = %for.cond
call i32 @setjmp(...)
br %for.cond
```
In this BB, %var should be defined in all paths from %for.inc to make %0
valid. In the input it was true; %for.inc's only predecessor was
%for.cond. But after SjLj transformation, it is possible that %for.inc
has other predecessors that are reachable without reaching %for.cond.
```
entry.split:
...
br i1 %a, label %bb.1, label %for.inc
for.cond:
%0 = phi i32 [ %var, %for.inc ] ... ; Not valid!
%var = ...
br label %for.inc
for.inc: ; preds = %for.cond, %entry.split
call i32 @setjmp(...)
...
br %for.cond
```
In this case, we can't use %var in the `phi` instruction in %for.cond,
because %var is not defined in all paths through %for.inc (If the
control flow is %entry -> %entry.split -> %for.inc -> %for.cond, %var
has not been defined until we reach the `phi`). But the previous code
excluded users within the same BB, skipping instructions within the same
BB so they are not rewritten properly. User instructions within the same
BB also should be candidates for rewriting if they are _before_ the
original definition.
Fixes PR43097.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66729
llvm-svn: 369978
2019-08-27 05:51:35 +08:00
|
|
|
; Tests if SSA rewrite works when a use and its def are within the same BB.
|
|
|
|
define void @ssa_rewite_in_same_bb() {
|
|
|
|
entry:
|
|
|
|
call void @foo()
|
|
|
|
br label %for.cond
|
|
|
|
|
|
|
|
for.cond: ; preds = %for.inc, %entry
|
|
|
|
; CHECK: %{{.*}} = phi i32 [ %var[[VARNO:.*]], %for.inc.split ]
|
|
|
|
%0 = phi i32 [ %var, %for.inc ], [ undef, %entry ]
|
|
|
|
%var = add i32 0, 0
|
|
|
|
br label %for.inc
|
|
|
|
|
|
|
|
for.inc: ; preds = %for.cond
|
|
|
|
%call5 = call i32 @setjmp(%struct.__jmp_buf_tag* undef) #0
|
|
|
|
br label %for.cond
|
|
|
|
|
|
|
|
; CHECK: for.inc.split:
|
|
|
|
; CHECK: %var[[VARNO]] = phi i32 [ %var, %for.inc ]
|
|
|
|
}
|
|
|
|
|
2016-09-02 05:05:15 +08:00
|
|
|
declare void @foo()
|
|
|
|
; Function Attrs: returns_twice
|
|
|
|
declare i32 @setjmp(%struct.__jmp_buf_tag*) #0
|
|
|
|
; Function Attrs: noreturn
|
|
|
|
declare void @longjmp(%struct.__jmp_buf_tag*, i32) #1
|
|
|
|
declare i32 @__gxx_personality_v0(...)
|
|
|
|
declare i8* @__cxa_begin_catch(i8*)
|
|
|
|
declare void @__cxa_end_catch()
|
|
|
|
declare i8* @malloc(i32)
|
|
|
|
declare void @free(i8*)
|
|
|
|
|
|
|
|
; JS glue functions and invoke wrappers declaration
|
2018-11-21 03:25:07 +08:00
|
|
|
; CHECK-DAG: declare i32 @getTempRet0()
|
|
|
|
; CHECK-DAG: declare void @setTempRet0(i32)
|
2016-09-02 05:05:15 +08:00
|
|
|
; CHECK-DAG: declare i32* @saveSetjmp(%struct.__jmp_buf_tag*, i32, i32*, i32)
|
|
|
|
; CHECK-DAG: declare i32 @testSetjmp(i32, i32*, i32)
|
|
|
|
; CHECK-DAG: declare void @emscripten_longjmp_jmpbuf(%struct.__jmp_buf_tag*, i32)
|
|
|
|
; CHECK-DAG: declare void @emscripten_longjmp(i32, i32)
|
|
|
|
; CHECK-DAG: declare void @__invoke_void(void ()*)
|
|
|
|
; CHECK-DAG: declare void @"__invoke_void_%struct.__jmp_buf_tag*_i32"(void (%struct.__jmp_buf_tag*, i32)*, %struct.__jmp_buf_tag*, i32)
|
|
|
|
|
|
|
|
attributes #0 = { returns_twice }
|
|
|
|
attributes #1 = { noreturn }
|
|
|
|
attributes #2 = { nounwind }
|
2019-08-04 05:38:19 +08:00
|
|
|
attributes #3 = { allocsize(0) }
|
2020-04-03 10:44:00 +08:00
|
|
|
; CHECK: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="getTempRet0" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="setTempRet0" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__resumeException" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="llvm_eh_typeid_for" }
|
2020-04-01 12:45:57 +08:00
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__cxa_find_matching_catch_3" }
|
2020-04-03 10:44:00 +08:00
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="emscripten_longjmp_jmpbuf" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="saveSetjmp" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="testSetjmp" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="emscripten_longjmp" }
|
2020-04-01 12:45:57 +08:00
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_i8*_i32_%struct.__jmp_buf_tag*" }
|
|
|
|
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void_%struct.__jmp_buf_tag*_i32" }
|
2019-08-04 05:38:19 +08:00
|
|
|
; CHECK: attributes #[[ALLOCSIZE_ATTR]] = { allocsize(1) }
|
2020-02-07 11:29:01 +08:00
|
|
|
|
|
|
|
!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_info", 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)
|