Commit Graph

208 Commits

Author SHA1 Message Date
Wenyong Huang
b0d5b8df1d
Fix issues of build/run with llvm-17 (#2853)
- Fix compilation error of using PGOOptions
- Fix LLVM JIT run error due to `llvm_orc_registerEHFrameSectionWrapper`
  symbol not found
2023-12-04 16:40:54 +08:00
liang.he
8aa813f44a
Generate jitdump to support linux perf for LLVM JIT (#2788) 2023-11-27 15:42:00 +08:00
Wenyong Huang
103cb89593
aot compiler: Fix handle next reachable if block (#2793)
The popped reachable block may be if block whose else branch hasn't been
translated, and should push the params for the else block if there are.

And use LLVMDisposeMessage to free memory allocated in is_win_platform.
2023-11-20 17:14:10 +08:00
YAMAMOTO Takashi
562a5dd1b6
Fix data/elem drop (#2747)
Currently, `data.drop` instruction is implemented by directly modifying the
underlying module. It breaks use cases where you have multiple instances
sharing a single loaded module. `elem.drop` has the same problem too.

This PR  fixes the issue by keeping track of which data/elem segments have
been dropped by using bitmaps for each module instances separately, and
add a sample to demonstrate the issue and make the CI run it.

Also add a missing check of dropped elements to the fast-jit `table.init`.

Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2735
Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2772
2023-11-18 08:50:16 +08:00
TianlongLiang
08c0ec74c4
More precise help info of enabled targets for wamrc (#2783)
Instead of printing all support targets of wamrc, print only the targets
that are included in the LLVM library with which wamrc was compiled.
2023-11-17 19:05:00 +08:00
Wenyong Huang
dd0556a729
aot compiler: Disable musttail for thumb (#2771)
Error is reported when executing `wamrc --target=thumb -o <aot_file> <wasm_file>`:
```
LLVM ERROR: failed to perform tail call elimination on a call site marked musttail
Aborted (core dumped)
```
2023-11-16 11:05:23 +08:00
Wenyong Huang
9a32e0672c
Fix aot compiler issue reported by Coverity (#2770)
Set `abi` to "gnu" for the bare-metal target when `abi` is NULL,
or the below `bh_assert` and `bh_memcpy` may deference a NULL
pointer. Error is reported when running wamrc compiled with
`cmake .. -DCMAKE_BUILD_TYPE=Debug`:
```
core/iwasm/compilation/aot_llvm.c:2584:13: runtime error:
  null pointer passed as argument 1, which is declared to never be null
```
2023-11-16 10:54:39 +08:00
Huang Qi
e4353b4e1f
Add eabihf ABI support and set vendor-sys of bare-metal targets (#2745)
Set the vendor-sys of bare-metal targets to "-unknown-none-",
and currently only add "thumbxxx" to the bare-metal target list.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2023-11-13 18:01:15 +08:00
Wenyong Huang
caf805ece7
Fix AOT compiler simd shift opcodes (#2715) 2023-11-03 10:38:53 +08:00
Wenyong Huang
68a627ea2c
Fix several AOT compiler issues (#2697)
- Fix potential invalid push param phis and add incoming phis to a un-existed basic block
- Fix potential invalid shift count int rotl/rotr opcodes
- Resize memory_data_size to UINT32_MAX if it is 4G when hw bound check is enabled
- Fix negative linear memory offset is used for 64-bit target it is const and larger than INT32_MAX
2023-11-02 20:36:21 +08:00
Marcin Kolny
72b34eaf30
Fix AOT shift operations for indirect constants (#2627)
When doing more investigations related to this PR:
  https://github.com/bytecodealliance/wasm-micro-runtime/pull/2619
We found that in some scenarios the constant might not be directly
available to the LLVM IR builder, e.g.:
```
  (func $const_ret (result i32)
    i32.const -5
  )
  (func $foo
    (i32.shr_u (i32.const -1) (call $const_ret))
    (i32.const 31)
  )
```
In that case, the right parameter to `i32.shr_u` is not constant, therefore
the `SHIFT_COUNT_MASK` isn't applied. However, when the optimization
is enabled (`--opt-level` is 2 or 3), the optimization passes resolve the
call into constant, and that constant is poisoned, causing the compiler to
resolve the whole function to an exception.
2023-10-11 11:22:34 +08:00
liang.he
e73993709e
Use another default pipeline when opt-level is 0 (#2624)
According to the description of `buildPerModuleDefaultPipeline()` and
`buildLTOPreLinkDefaultPipeline()`, it is not allowed to call them with `O0` level.
Use `buildO0DefaultPipeline` instead when the opt-level is 0.
2023-10-10 10:52:37 +08:00
TianlongLiang
059fbfc252
Fix potential issue in aot compiler when translating block opcodes (#2622)
The LLVM zext IR may be inserted after the terminator of a basic block
when popping the arguments of a wasm block. Change to insert the
zext IR before the terminator of the basic block to resolve the issue.

Reported in #2620.
2023-10-08 09:17:54 +08:00
Marcin Kolny
b115b7baac
Fix compilation of shift opcodes on x86_64 and i386 architectures (#2619)
This change fixes the case where the right parameter of shift
operator is negative, specifically, when both parameters of
shift opcode are constants.
2023-10-07 19:55:14 +08:00
YAMAMOTO Takashi
e1ea15d94e
aot_resolve_stack_sizes: Disable the size check for now (#2608)
cf.
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2555#issuecomment-1738530877
https://github.com/llvm/llvm-project/issues/67765
2023-09-29 15:30:52 +08:00
liang.he
3c17a36ccb
Patch implementations of vfbinop(min,max,pmin,pax) (#2584)
According to the specification,
- fNxM_pmin/max returns v1 or v2 based on flt(v1,v2) result
- fNxM_min/max returns +/-NaN, +/-Inf, v1 or v2 based on more than
  flt(v1,v2) result

Fixes issue #2561.
2023-09-28 09:32:01 +08:00
liang.he
cd0cec5beb
Check ValueKind before extracting a constant int value (#2595)
Only when the value kind is LLVMConstantIntValueKind and the value
is not undef and not poison can we extract the value of a constant int.

Fixes #2557 and #2559.
2023-09-28 09:15:56 +08:00
Alfred E. Neumayer
99b47fd334
Support AOT compiler with LLVM 17 (#2567)
Adapt API usage to new interfaces where applicable, including LLVM function
usage, obsoleted llvm::Optional type and removal of unavailable headers.

Know issues:
- AOT static PGO isn't enabled
- LLVM JIT may run failed due to llvm_orc_registerEHFrameSectionWrapper
  isn't linked into iwasm
2023-09-25 19:00:46 +08:00
TianlongLiang
71e07a7fa4
Fix potential unaligned store issue when extra return value is v128 (#2583)
Unaligned store v128 value to the AOT function argument of the pointer for
the extra return value may cause segmentation fault.

Fix the issue reported in #2556.
2023-09-23 09:06:35 +08:00
Huang Qi
051dfbbb9e
Fix a typo in is_win_platform (#2490) 2023-08-22 11:50:11 +08:00
Wenyong Huang
a2f76cf93c
Fix typo in aot_emit_aot_file.c (#2478) 2023-08-18 13:57:23 +08:00
Wenyong Huang
2cb701f7f3
Fix windows AOT hw bound check (#2475)
When AOT out of bound linear memory access or stack overflow occurs, the call stack of
AOT functions cannot be unwound currently, so from the exception handler, runtime
cannot jump back into the place that calls the AOT function.
We temporarily skip the current instruction and let AOT code continue to run and return
to caller as soon as possible. And use the zydis library the decode the current instruction
to get its size.

And remove using RtlAddFunctionTable to register the AOT functions since it doesn't work
currently.
2023-08-18 10:16:03 +08:00
Wenyong Huang
6716c23dd1
wamrc: Fix windows relocation to aot_func_internal#n (#2474)
AOT relocation to aot_func_internal#n is generated by wamrc --bounds-checks=1.
Resolve the issue by applying the relocation in the compilation stage by wamrc and
don't generate these relocations in the AOT file.

Fixes #2471.
2023-08-18 09:56:00 +08:00
Wenyong Huang
e2f8721ec9
Fix issues reported by Coverity and clear windows warnings (#2467) 2023-08-17 10:54:02 +08:00
TianlongLiang
c820643b2b
wamrc: More friendly to print help info (#2451)
Allow wamrc to print help info like below:
```bash
wamrc --target=help
wamrc --target-abi=help
wamrc --target=<target> --cpu=help
wamrc --target=<target> --cpu=help --cpu-features=+help
```
2023-08-16 11:28:45 +08:00
YAMAMOTO Takashi
0f18051e66
aot: Disable musttail for mips (#2457)
Fixes https://github.com/bytecodealliance/wasm-micro-runtime/issues/2412
2023-08-15 11:15:45 +08:00
Wenyong Huang
923e8d6471
Fix windows link error and clear windows warnings (#2463)
- Fix windows wamrc link error: aot_generate_tempfile_name undefined.
- Clear windows compile warnings.
- And rename folder `samples/bh_atomic` and `samples/mem_allocator` to
  `samples/bh-atomic` and `samples/mem-allocator`.
2023-08-14 19:04:49 +08:00
Huang Qi
10b18d85cd
Fix ExpandMemoryOpPass doesn't work properly (#2399)
The old method may not work for some cases. This PR iterates over all instructions
in the function, looking for memcpy, memmove and memset instructions, putting
them into a set, and finally expands them into a loop one by one.

And move this LLVM Pass after building the pipe line of pass builder to ensure that
the memcpy/memmove/memset instrinsics are generated before applying the pass.
2023-07-29 10:28:09 +08:00
Wenyong Huang
24c6c6977b
Fix llvm jit failed to lookup aot_stack_sizes symbol issue (#2384)
LVM JIT failed to lookup symbol "aot_stack_sizes" as it is an internal symbol,
change to lookup "aot_stack_sizes_alias" instead.

Reported in #2372.
2023-07-24 15:15:48 +08:00
Cengizhan Pasaoglu
57abdfdb5c
Fix typo (dwarf) in the codebase (#2367)
In the codebase, the struct and functions were written without "f" for dwarf.
2023-07-19 17:58:52 +08:00
Huang Qi
aafea39b8c
Add "--enable-builtin-intrinsics=<flags>" option to wamrc (#2341)
Refer to doc/xip.md for details.
2023-07-06 18:20:35 +08:00
YAMAMOTO Takashi
3bbf59ad45
wamrc: Warn on text relocations for XIP (#2340) 2023-07-05 10:49:45 +08:00
Huang Qi
ae4069df41
Migrate ExpandMemoryOpPass to llvm new pass manager (#2334)
Fix #2328
2023-07-04 17:17:15 +08:00
YAMAMOTO Takashi
1f89e446d9
Avoid switch lowering to lookup tables for XIP (#2339)
Because it involves relocations for the table. (.Lswitch.table.XXX)

Discussions: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2316
2023-07-04 16:48:32 +08:00
Huang Qi
44f4b4f062
Add "--enable-llvm-passes=<passes>" option to wamrc (#2335)
Add "--enable-llvm-passes=<passes>" option to wamrc for customizing LLVM passes
2023-07-04 12:20:52 +08:00
YAMAMOTO Takashi
03418ef5ac
aot: Avoid possible relocations around "stack_sizes" for XIP mode (#2322)
Fixes https://github.com/bytecodealliance/wasm-micro-runtime/issues/2316

Lightly tested on riscv64 qemu.
2023-06-29 18:45:33 +08:00
YAMAMOTO Takashi
5831531449
aot: Move stack_sizes table to a dedicated section (#2317)
To solve the "AOT module load failed: resolve symbol stack_sizes failed" issue.

This PR partly fixes #2312 and was lightly tested on qemu armhf.
2023-06-27 16:18:14 +08:00
Wenyong Huang
ea78b89965
Fix wamrc build issues with LLVM 13 and LLVM 16 (#2313)
Fix some build errors when building wamrc with LLVM-13, reported in #2311
Fix some build warnings when building wamrc with LLVM-16:
```
  core/iwasm/compilation/aot_llvm_extra2.cpp:26:26: warning:
  ‘llvm::None’ is deprecated: Use std::nullopt instead. [-Wdeprecated-declarations]
     26 |             return llvm::None;
```
Fix a maybe-uninitialized compile warning:
```
  core/iwasm/compilation/aot_llvm.c:413:9: warning:
  ‘update_top_block’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    413 |         LLVMPositionBuilderAtEnd(b, update_top_block);
```
2023-06-27 08:59:49 +08:00
YAMAMOTO Takashi
cd7941cc39
AOT/JIT native stack bound check improvement (#2244)
Move the native stack overflow check from the caller to the callee because the
former doesn't work for call_indirect and imported functions.

Make the stack usage estimation more accurate. Instead of making a guess from
the number of wasm locals in the function, use the LLVM's idea of the stack size
of each MachineFunction. The former is inaccurate because a) it doesn't reflect
optimization passes, and b) wasm locals are not the only reason to use stack.

To use the post-compilation stack usage information without requiring 2-pass
compilation or machine-code imm rewriting, introduce a global array to store
stack consumption of each functions:
For JIT, use a custom IRCompiler with an extra pass to fill the array.
For AOT, use `clang -fstack-usage` equivalent because we support external llc.

Re-implement function call stack usage estimation to reflect the real calling
conventions better. (aot_estimate_stack_usage_for_function_call)

Re-implement stack estimation logic (--enable-memory-profiling) based on the new
machinery.

Discussions: #2105.
2023-06-22 07:27:07 +08:00
YAMAMOTO Takashi
92e073b8ce
AOTFuncContext: Remove a stale comment (#2283) 2023-06-09 22:31:08 +08:00
YAMAMOTO Takashi
cabcb177c8
dwarf_extractor: Constify a bit (#2278) 2023-06-09 09:52:03 +08:00
YAMAMOTO Takashi
6e3c3fe9ec
Fix build error with LLVM 16 (#2259) 2023-06-06 13:45:18 +08:00
YAMAMOTO Takashi
5d69f364db
aot/jit: Set module layout (#2260)
LLVM 15 and later sometimes perform wrong optimizations without this.
2023-06-06 10:18:16 +08:00
Wenyong Huang
8ef09be604
Fix compile error of wamrc with llvm-13/llvm-14 (#2261) 2023-06-06 08:33:15 +08:00
Wenyong Huang
8d88471c46
Implement AOT static PGO (#2243)
LLVM PGO (Profile-Guided Optimization) allows the compiler to better optimize code
for how it actually runs. This PR implements the AOT static PGO, and is tested on
Linux x86-64 and x86-32. The basic steps are:

1. Use `wamrc --enable-llvm-pgo -o <aot_file_of_pgo> <wasm_file>`
   to generate an instrumented aot file.
2. Compile iwasm with `cmake -DWAMR_BUILD_STATIC_PGO=1` and run
      `iwasm --gen-prof-file=<raw_profile_file> <aot_file_of_pgo>`
    to generate the raw profile file.
3. Run `llvm-profdata merge -output=<profile_file> <raw_profile_file>`
    to merge the raw profile file into the profile file.
4. Run `wamrc --use-prof-file=<profile_file> -o <aot_file> <wasm_file>`
    to generate the optimized aot file.
5. Run the optimized aot_file: `iwasm <aot_file>`.

The test scripts are also added for each benchmark, run `test_pgo.sh` under
each benchmark's folder to test the AOT static pgo.
2023-06-05 09:17:39 +08:00
Wenyong Huang
76be848ec3
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT

This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
    i32.load, i64.load, f32.load, f64.load, v128.load,
    i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.

Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 10:13:33 +08:00
YAMAMOTO Takashi
94204b90ad
aot_compile_op_call: Remove a wrong optimization (#2233)
Unlike a tail-call, the caller of an ordinary recursive call doesn't
necessarily return immediately.
2023-05-25 07:44:54 +08:00
YAMAMOTO Takashi
670567f8b3
core/iwasm/compilation: constify a bit (#2223)
Just to make the code a bit easier to read.
2023-05-20 11:55:02 +08:00
YAMAMOTO Takashi
f759a1f960
A few changes related to WAMRC_LLC_COMPILER (#2218)
Print `target triple` for wamrc and set target triple for the LLVM module.
And update document.
2023-05-17 09:56:35 +08:00
YAMAMOTO Takashi
2b896c80ef
wamrc: Add --stack-usage option (#2158) 2023-04-28 13:56:44 +08:00