]> granicus.if.org Git - llvm/commit
[WebAssembly] Fix conflict between ret legalization and sjlj
authorKeno Fischer <keno@alumni.harvard.edu>
Mon, 5 Aug 2019 21:36:09 +0000 (21:36 +0000)
committerKeno Fischer <keno@alumni.harvard.edu>
Mon, 5 Aug 2019 21:36:09 +0000 (21:36 +0000)
commit22147c3f5c4c3760bb03e0668c7daf23acc3dfd5
treed08035ddffb1e6ed656520f73d824b7384331aff
parentb30047122635d84cf90fe478d53c29ad5824c6b3
[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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367935 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/IR/CallingConv.h
lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
test/CodeGen/WebAssembly/lower-em-exceptions-whitelist.ll
test/CodeGen/WebAssembly/lower-em-exceptions.ll
test/CodeGen/WebAssembly/lower-em-sjlj-sret.ll [new file with mode: 0644]
test/CodeGen/WebAssembly/lower-em-sjlj.ll