2020-09-26 02:45:16 +08:00
|
|
|
; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s --check-prefixes=CHECK,NO-TLS
|
|
|
|
; RUN: opt < %s -wasm-lower-em-ehsjlj -S --mattr=+atomics,+bulk-memory | FileCheck %s --check-prefixes=CHECK,TLS
|
2016-08-02 05:34:04 +08:00
|
|
|
|
2016-09-02 05:05:15 +08:00
|
|
|
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
|
|
|
|
2016-08-02 05:34:04 +08:00
|
|
|
@_ZTIi = external constant i8*
|
|
|
|
@_ZTIc = external constant i8*
|
2020-09-26 02:45:16 +08:00
|
|
|
; NO-TLS-DAG: __THREW__ = external global i32
|
|
|
|
; NO-TLS-DAG: __threwValue = external global i32
|
|
|
|
; TLS-DAG: __THREW__ = external thread_local(localexec) global i32
|
|
|
|
; TLS-DAG: __threwValue = external thread_local(localexec) global i32
|
2016-08-02 05:34:04 +08:00
|
|
|
|
|
|
|
; Test invoke instruction with clauses (try-catch block)
|
|
|
|
define void @clause() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
|
|
|
|
; CHECK-LABEL: @clause(
|
|
|
|
entry:
|
|
|
|
invoke void @foo(i32 3)
|
|
|
|
to label %invoke.cont unwind label %lpad
|
|
|
|
; CHECK: entry:
|
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_i32(void (i32)* @foo, i32 3)
|
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: %cmp = icmp eq i32 %[[__THREW__VAL]], 1
|
|
|
|
; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
|
2016-08-02 05:34:04 +08:00
|
|
|
|
|
|
|
invoke.cont: ; preds = %entry
|
|
|
|
br label %try.cont
|
|
|
|
|
|
|
|
lpad: ; preds = %entry
|
|
|
|
%0 = landingpad { i8*, i32 }
|
|
|
|
catch i8* bitcast (i8** @_ZTIi to i8*)
|
|
|
|
catch i8* null
|
|
|
|
%1 = extractvalue { i8*, i32 } %0, 0
|
|
|
|
%2 = extractvalue { i8*, i32 } %0, 1
|
|
|
|
br label %catch.dispatch
|
|
|
|
; CHECK: lpad:
|
2016-08-09 08:29:55 +08:00
|
|
|
; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* null)
|
2016-08-02 05:34:04 +08:00
|
|
|
; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
|
2018-11-21 03:25:07 +08:00
|
|
|
; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = call i32 @getTempRet0()
|
2016-08-02 05:34:04 +08:00
|
|
|
; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
|
|
|
|
; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
|
2016-08-09 08:29:55 +08:00
|
|
|
; CHECK-NEXT: %[[CDR:.*]] = extractvalue { i8*, i32 } %[[IVI2]], 1
|
2016-08-02 05:34:04 +08:00
|
|
|
|
|
|
|
catch.dispatch: ; preds = %lpad
|
|
|
|
%3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
|
|
|
|
%matches = icmp eq i32 %2, %3
|
|
|
|
br i1 %matches, label %catch1, label %catch
|
2016-08-09 08:29:55 +08:00
|
|
|
; CHECK: catch.dispatch:
|
|
|
|
; CHECK-NEXT: %[[TYPEID:.*]] = call i32 @llvm_eh_typeid_for(i8* bitcast (i8** @_ZTIi to i8*))
|
|
|
|
; CHECK-NEXT: %matches = icmp eq i32 %[[CDR]], %[[TYPEID]]
|
2016-08-02 05:34:04 +08:00
|
|
|
|
|
|
|
catch1: ; preds = %catch.dispatch
|
|
|
|
%4 = call i8* @__cxa_begin_catch(i8* %1)
|
|
|
|
%5 = bitcast i8* %4 to i32*
|
|
|
|
%6 = load i32, i32* %5, align 4
|
|
|
|
call void @__cxa_end_catch()
|
|
|
|
br label %try.cont
|
|
|
|
|
|
|
|
try.cont: ; preds = %catch, %catch1, %invoke.cont
|
|
|
|
ret void
|
|
|
|
|
|
|
|
catch: ; preds = %catch.dispatch
|
|
|
|
%7 = call i8* @__cxa_begin_catch(i8* %1)
|
|
|
|
call void @__cxa_end_catch()
|
|
|
|
br label %try.cont
|
|
|
|
}
|
|
|
|
|
|
|
|
; Test invoke instruction with filters (functions with throw(...) declaration)
|
|
|
|
define void @filter() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
|
|
|
|
; CHECK-LABEL: @filter(
|
|
|
|
entry:
|
|
|
|
invoke void @foo(i32 3)
|
|
|
|
to label %invoke.cont unwind label %lpad
|
|
|
|
; CHECK: entry:
|
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_i32(void (i32)* @foo, i32 3)
|
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: %cmp = icmp eq i32 %[[__THREW__VAL]], 1
|
|
|
|
; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
|
2016-08-02 05:34:04 +08:00
|
|
|
|
|
|
|
invoke.cont: ; preds = %entry
|
|
|
|
ret void
|
|
|
|
|
|
|
|
lpad: ; preds = %entry
|
|
|
|
%0 = landingpad { i8*, i32 }
|
|
|
|
filter [2 x i8*] [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*)]
|
|
|
|
%1 = extractvalue { i8*, i32 } %0, 0
|
|
|
|
%2 = extractvalue { i8*, i32 } %0, 1
|
|
|
|
br label %filter.dispatch
|
|
|
|
; CHECK: lpad:
|
2016-08-09 08:29:55 +08:00
|
|
|
; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*))
|
2016-08-02 05:34:04 +08:00
|
|
|
; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
|
2018-11-21 03:25:07 +08:00
|
|
|
; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = call i32 @getTempRet0()
|
2016-08-02 05:34:04 +08:00
|
|
|
; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
|
|
|
|
; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
|
|
|
|
; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 1
|
|
|
|
|
|
|
|
filter.dispatch: ; preds = %lpad
|
|
|
|
%ehspec.fails = icmp slt i32 %2, 0
|
|
|
|
br i1 %ehspec.fails, label %ehspec.unexpected, label %eh.resume
|
|
|
|
|
|
|
|
ehspec.unexpected: ; preds = %filter.dispatch
|
|
|
|
call void @__cxa_call_unexpected(i8* %1) #4
|
|
|
|
unreachable
|
|
|
|
|
|
|
|
eh.resume: ; preds = %filter.dispatch
|
|
|
|
%lpad.val = insertvalue { i8*, i32 } undef, i8* %1, 0
|
|
|
|
%lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %2, 1
|
|
|
|
resume { i8*, i32 } %lpad.val3
|
|
|
|
; CHECK: eh.resume:
|
|
|
|
; CHECK-NEXT: insertvalue
|
|
|
|
; CHECK-NEXT: %[[LPAD_VAL:.*]] = insertvalue
|
|
|
|
; CHECK-NEXT: %[[LOW:.*]] = extractvalue { i8*, i32 } %[[LPAD_VAL]], 0
|
2016-08-09 08:29:55 +08:00
|
|
|
; CHECK-NEXT: call void @__resumeException(i8* %[[LOW]])
|
2016-08-02 05:34:04 +08:00
|
|
|
; CHECK-NEXT: unreachable
|
|
|
|
}
|
|
|
|
|
2016-08-09 08:29:55 +08:00
|
|
|
; Test if argument attributes indices in newly created call instructions are correct
|
|
|
|
define void @arg_attributes() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
|
|
|
|
; CHECK-LABEL: @arg_attributes(
|
|
|
|
entry:
|
|
|
|
%0 = invoke noalias i8* @bar(i8 signext 1, i8 zeroext 2)
|
|
|
|
to label %invoke.cont unwind label %lpad
|
|
|
|
; CHECK: entry:
|
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: %0 = call cc{{.*}} noalias i8* @"__invoke_i8*_i8_i8"(i8* (i8, i8)* @bar, i8 signext 1, i8 zeroext 2)
|
2016-08-09 08:29:55 +08:00
|
|
|
|
|
|
|
invoke.cont: ; preds = %entry
|
|
|
|
br label %try.cont
|
|
|
|
|
|
|
|
lpad: ; preds = %entry
|
|
|
|
%1 = landingpad { i8*, i32 }
|
|
|
|
catch i8* bitcast (i8** @_ZTIi to i8*)
|
|
|
|
catch i8* null
|
|
|
|
%2 = extractvalue { i8*, i32 } %1, 0
|
|
|
|
%3 = extractvalue { i8*, i32 } %1, 1
|
|
|
|
br label %catch.dispatch
|
|
|
|
|
|
|
|
catch.dispatch: ; preds = %lpad
|
|
|
|
%4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
|
|
|
|
%matches = icmp eq i32 %3, %4
|
|
|
|
br i1 %matches, label %catch1, label %catch
|
|
|
|
|
|
|
|
catch1: ; preds = %catch.dispatch
|
|
|
|
%5 = call i8* @__cxa_begin_catch(i8* %2)
|
|
|
|
%6 = bitcast i8* %5 to i32*
|
|
|
|
%7 = load i32, i32* %6, align 4
|
|
|
|
call void @__cxa_end_catch()
|
|
|
|
br label %try.cont
|
|
|
|
|
|
|
|
try.cont: ; preds = %catch, %catch1, %invoke.cont
|
|
|
|
ret void
|
|
|
|
|
|
|
|
catch: ; preds = %catch.dispatch
|
|
|
|
%8 = call i8* @__cxa_begin_catch(i8* %2)
|
|
|
|
call void @__cxa_end_catch()
|
|
|
|
br label %try.cont
|
|
|
|
}
|
|
|
|
|
2016-08-02 05:34:04 +08:00
|
|
|
declare void @foo(i32)
|
2016-08-09 08:29:55 +08:00
|
|
|
declare i8* @bar(i8, i8)
|
2016-08-02 05:34:04 +08:00
|
|
|
|
|
|
|
declare i32 @__gxx_personality_v0(...)
|
|
|
|
declare i32 @llvm.eh.typeid.for(i8*)
|
|
|
|
declare i8* @__cxa_begin_catch(i8*)
|
|
|
|
declare void @__cxa_end_catch()
|
|
|
|
declare void @__cxa_call_unexpected(i8*)
|
|
|
|
|
2016-09-02 05:05:15 +08:00
|
|
|
; 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 void @__resumeException(i8*)
|
|
|
|
; CHECK-DAG: declare void @__invoke_void_i32(void (i32)*, i32)
|
|
|
|
; CHECK-DAG: declare i8* @__cxa_find_matching_catch_4(i8*, i8*)
|