Skip to content

Commit b0f730c

Browse files
committed
WAMR support clone
- Let WAMR support `CompiledBytecode` level of clone - enable WAMR JIT - upgrade to a WMAR newer commit Signed-off-by: [email protected] <[email protected]>
1 parent 59102f3 commit b0f730c

File tree

9 files changed

+211
-17
lines changed

9 files changed

+211
-17
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ build:clang-tsan --linkopt -fsanitize=thread
5656
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect
5757
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=@proxy_wasm_cpp_host//:clang_tidy_config
5858
build:clang-tidy --output_groups=report
59+
build:clang-tidy --config=clang
5960

6061
# Use GCC compiler.
6162
build:gcc --action_env=BAZEL_COMPILER=gcc

.github/workflows/format.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jobs:
122122
- uses: actions/checkout@v2
123123

124124
- name: Install dependencies (Linux)
125-
run: sudo apt update -y && sudo apt install -y clang-tidy-12
125+
run: sudo apt update -y && sudo apt install -y clang-tidy-12 lld-12 && sudo ln -sf /usr/bin/lld-12 /usr/bin/lld
126126

127127
- name: Bazel cache
128128
uses: PiotrSikora/[email protected]

.github/workflows/test.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@ jobs:
174174
arch: x86_64
175175
action: test
176176
flags: --config=clang
177+
cache: true
177178
- name: 'WAMR on macOS/x86_64'
178179
engine: 'wamr'
179180
repo: 'com_github_bytecodealliance_wasm_micro_runtime'
180181
os: macos-11
181182
arch: x86_64
182183
action: test
184+
cache: true
183185
- name: 'WasmEdge on Linux/x86_64'
184186
engine: 'wasmedge'
185187
repo: 'com_github_wasmedge_wasmedge'
@@ -245,7 +247,7 @@ jobs:
245247

246248
- name: Install dependencies (Linux)
247249
if: ${{ matrix.deps != '' && startsWith(matrix.os, 'ubuntu') }}
248-
run: sudo apt update -y && sudo apt install -y ${{ matrix.deps }}
250+
run: sudo apt update -y && sudo apt install -y clang-tidy-12 lld-12 && sudo ln -sf /usr/bin/lld-12 /usr/bin/lld
249251

250252
- name: Activate Docker/QEMU
251253
if: startsWith(matrix.run_under, 'docker')
@@ -282,6 +284,16 @@ jobs:
282284
echo "filegroup(name = \"$i\", srcs = [\"$i.wasm\"])" >> test/test_data/BUILD; \
283285
done
284286
287+
- name: Prepare LLVM libraries (WAMR)
288+
shell: bash
289+
if: ${{ matrix.engine == 'wamr' }}
290+
run: >
291+
${{ matrix.run_under }}
292+
bazel build
293+
--define engine=${{ matrix.engine }}
294+
${{ matrix.flags }}
295+
-- //:wamr_lib
296+
285297
- name: Bazel build/test
286298
shell: bash
287299
run: >

bazel/external/wamr.BUILD

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ filegroup(
1212
cmake(
1313
name = "wamr_lib",
1414
cache_entries = {
15-
"WAMR_BUILD_INTERP": "1",
16-
"WAMR_BUILD_FAST_INTERP": "1",
17-
"WAMR_BUILD_JIT": "0",
18-
"WAMR_BUILD_LAZY_JIT": "0",
19-
"WAMR_BUILD_AOT": "0",
20-
"WAMR_BUILD_SIMD": "0",
21-
"WAMR_BUILD_MULTI_MODULE": "1",
15+
"LLVM_DIR": "$EXT_BUILD_DEPS/copy_llvm_13_0_1/llvm/lib/cmake/llvm",
16+
"WAMR_BUILD_INTERP": "0",
17+
"WAMR_BUILD_JIT": "1",
18+
"WAMR_BUILD_AOT": "1",
19+
"WAMR_BUILD_SIMD": "1",
20+
"WAMR_BUILD_MULTI_MODULE": "0",
2221
"WAMR_BUILD_LIBC_WASI": "0",
2322
"WAMR_BUILD_TAIL_CALL": "1",
23+
"WAMR_DISABLE_HW_BOUND_CHECK": "0",
24+
"WAMR_DISABLE_STACK_HW_BOUND_CHECK": "1",
2425
},
2526
generate_args = ["-GNinja"],
2627
lib_source = ":srcs",
28+
linkopts = ["-ldl"],
2729
out_static_libs = ["libvmlib.a"],
30+
deps = ["@llvm-13_0_1//:llvm_13_0_1_lib"],
2831
)

bazel/external/wamr_llvm.BUILD

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
2+
3+
licenses(["notice"]) # Apache 2
4+
5+
package(default_visibility = ["//visibility:public"])
6+
7+
filegroup(
8+
name = "srcs",
9+
srcs = glob(["**"]),
10+
)
11+
12+
cmake(
13+
name = "llvm_13_0_1_lib",
14+
cache_entries = {
15+
# Disable both: BUILD and INCLUDE, since some of the INCLUDE
16+
# targets build code instead of only generating build files.
17+
"LLVM_BUILD_BENCHMARKS": "off",
18+
"LLVM_INCLUDE_BENCHMARKS": "off",
19+
"LLVM_BUILD_DOCS": "off",
20+
"LLVM_INCLUDE_DOCS": "off",
21+
"LLVM_BUILD_EXAMPLES": "off",
22+
"LLVM_INCLUDE_EXAMPLES": "off",
23+
"LLVM_BUILD_RUNTIME": "off",
24+
"LLVM_BUILD_RUNTIMES": "off",
25+
"LLVM_INCLUDE_RUNTIMES": "off",
26+
"LLVM_BUILD_TESTS": "off",
27+
"LLVM_INCLUDE_TESTS": "off",
28+
"LLVM_BUILD_TOOLS": "off",
29+
"LLVM_INCLUDE_TOOLS": "off",
30+
"LLVM_BUILD_UTILS": "off",
31+
"LLVM_INCLUDE_UTILS": "off",
32+
"LLVM_ENABLE_IDE": "off",
33+
"LLVM_ENABLE_LIBEDIT": "off",
34+
"LLVM_ENABLE_LIBXML2": "off",
35+
"LLVM_ENABLE_TERMINFO": "off",
36+
"LLVM_ENABLE_ZLIB": "off",
37+
"LLVM_TARGETS_TO_BUILD": "X86",
38+
"CMAKE_CXX_FLAGS": "-Wno-unused-command-line-argument",
39+
},
40+
# `lld` doesn't work on MacOS
41+
generate_args = select({
42+
"@platforms//os:linux": [
43+
"-GNinja",
44+
"-DLLVM_USE_LINKER=lld",
45+
],
46+
"//conditions:default": [
47+
"-GNinja",
48+
],
49+
}),
50+
lib_source = ":srcs",
51+
out_data_dirs = [
52+
"bin",
53+
"include",
54+
"lib",
55+
"libexec",
56+
"share",
57+
],
58+
out_static_libs = [
59+
# How to get the library list:
60+
# build LLVM with "-DLLVM_INCLUDE_TOOLS=ON"
61+
# cd bin and run "./llvm-config --libnames"
62+
"libLLVMWindowsManifest.a",
63+
"libLLVMXRay.a",
64+
"libLLVMLibDriver.a",
65+
"libLLVMDlltoolDriver.a",
66+
"libLLVMCoverage.a",
67+
"libLLVMLineEditor.a",
68+
"libLLVMX86Disassembler.a",
69+
"libLLVMX86AsmParser.a",
70+
"libLLVMX86CodeGen.a",
71+
"libLLVMX86Desc.a",
72+
"libLLVMX86Info.a",
73+
"libLLVMOrcJIT.a",
74+
"libLLVMMCJIT.a",
75+
"libLLVMJITLink.a",
76+
"libLLVMInterpreter.a",
77+
"libLLVMExecutionEngine.a",
78+
"libLLVMRuntimeDyld.a",
79+
"libLLVMOrcTargetProcess.a",
80+
"libLLVMOrcShared.a",
81+
"libLLVMDWP.a",
82+
"libLLVMSymbolize.a",
83+
"libLLVMDebugInfoPDB.a",
84+
"libLLVMDebugInfoGSYM.a",
85+
"libLLVMOption.a",
86+
"libLLVMObjectYAML.a",
87+
"libLLVMMCA.a",
88+
"libLLVMMCDisassembler.a",
89+
"libLLVMLTO.a",
90+
"libLLVMPasses.a",
91+
"libLLVMCFGuard.a",
92+
"libLLVMCoroutines.a",
93+
"libLLVMObjCARCOpts.a",
94+
"libLLVMipo.a",
95+
"libLLVMVectorize.a",
96+
"libLLVMLinker.a",
97+
"libLLVMInstrumentation.a",
98+
"libLLVMFrontendOpenMP.a",
99+
"libLLVMFrontendOpenACC.a",
100+
"libLLVMExtensions.a",
101+
"libLLVMDWARFLinker.a",
102+
"libLLVMGlobalISel.a",
103+
"libLLVMMIRParser.a",
104+
"libLLVMAsmPrinter.a",
105+
"libLLVMDebugInfoMSF.a",
106+
"libLLVMDebugInfoDWARF.a",
107+
"libLLVMSelectionDAG.a",
108+
"libLLVMCodeGen.a",
109+
"libLLVMIRReader.a",
110+
"libLLVMAsmParser.a",
111+
"libLLVMInterfaceStub.a",
112+
"libLLVMFileCheck.a",
113+
"libLLVMFuzzMutate.a",
114+
"libLLVMTarget.a",
115+
"libLLVMScalarOpts.a",
116+
"libLLVMInstCombine.a",
117+
"libLLVMAggressiveInstCombine.a",
118+
"libLLVMTransformUtils.a",
119+
"libLLVMBitWriter.a",
120+
"libLLVMAnalysis.a",
121+
"libLLVMProfileData.a",
122+
"libLLVMObject.a",
123+
"libLLVMTextAPI.a",
124+
"libLLVMMCParser.a",
125+
"libLLVMMC.a",
126+
"libLLVMDebugInfoCodeView.a",
127+
"libLLVMBitReader.a",
128+
"libLLVMCore.a",
129+
"libLLVMRemarks.a",
130+
"libLLVMBitstreamReader.a",
131+
"libLLVMBinaryFormat.a",
132+
"libLLVMTableGen.a",
133+
"libLLVMSupport.a",
134+
"libLLVMDemangle.a",
135+
],
136+
)

bazel/repositories.bzl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,25 @@ def proxy_wasm_cpp_host_repositories():
159159
http_archive,
160160
name = "com_github_bytecodealliance_wasm_micro_runtime",
161161
build_file = "@proxy_wasm_cpp_host//bazel/external:wamr.BUILD",
162-
# WAMR-05-18-2022
163-
sha256 = "350736fffdc49533f5f372221d01e3b570ecd7b85f4429b22f5d89594eb99d9c",
164-
strip_prefix = "wasm-micro-runtime-d7a2888b18c478d87ce8094e1419b4e061db289f",
165-
url = "https://github.com/bytecodealliance/wasm-micro-runtime/archive/d7a2888b18c478d87ce8094e1419b4e061db289f.tar.gz",
162+
sha256 = "401061c7684d0b62e197b919236dd2ec37ef205976d641c2c9fbe8a1f3fd6f33",
163+
strip_prefix = "wasm-micro-runtime-30ee992762972e3dbf8c8440b9f352b33b79f38f",
164+
url = "https://github.com/bytecodealliance/wasm-micro-runtime/archive/30ee992762972e3dbf8c8440b9f352b33b79f38f.tar.gz",
166165
)
167166

168167
native.bind(
169168
name = "wamr",
170169
actual = "@com_github_bytecodealliance_wasm_micro_runtime//:wamr_lib",
171170
)
172171

172+
maybe(
173+
http_archive,
174+
name = "llvm-13_0_1",
175+
build_file = "@proxy_wasm_cpp_host//bazel/external:wamr_llvm.BUILD",
176+
sha256 = "ec6b80d82c384acad2dc192903a6cf2cdbaffb889b84bfb98da9d71e630fc834",
177+
strip_prefix = "llvm-13.0.1.src",
178+
url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.1/llvm-13.0.1.src.tar.xz",
179+
)
180+
173181
# WasmEdge with dependencies.
174182

175183
maybe(

src/wamr/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ using WasmEnginePtr = common::CSmartPtr<wasm_engine_t, wasm_engine_delete>;
2121
using WasmFuncPtr = common::CSmartPtr<wasm_func_t, wasm_func_delete>;
2222
using WasmStorePtr = common::CSmartPtr<wasm_store_t, wasm_store_delete>;
2323
using WasmModulePtr = common::CSmartPtr<wasm_module_t, wasm_module_delete>;
24+
using WasmSharedModulePtr = common::CSmartPtr<wasm_shared_module_t, wasm_shared_module_delete>;
2425
using WasmMemoryPtr = common::CSmartPtr<wasm_memory_t, wasm_memory_delete>;
2526
using WasmTablePtr = common::CSmartPtr<wasm_table_t, wasm_table_delete>;
2627
using WasmInstancePtr = common::CSmartPtr<wasm_instance_t, wasm_instance_delete>;

src/wamr/wamr.cc

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class Wamr : public WasmVm {
5858
std::string_view getEngineName() override { return "wamr"; }
5959
std::string_view getPrecompiledSectionName() override { return ""; }
6060

61-
Cloneable cloneable() override { return Cloneable::NotCloneable; }
62-
std::unique_ptr<WasmVm> clone() override { return nullptr; }
61+
Cloneable cloneable() override { return Cloneable::CompiledBytecode; }
62+
std::unique_ptr<WasmVm> clone() override;
6363

6464
bool load(std::string_view bytecode, std::string_view precompiled,
6565
const std::unordered_map<uint32_t, std::string> &function_names) override;
@@ -108,6 +108,7 @@ class Wamr : public WasmVm {
108108

109109
WasmStorePtr store_;
110110
WasmModulePtr module_;
111+
WasmSharedModulePtr shared_module_;
111112
WasmInstancePtr instance_;
112113

113114
WasmMemoryPtr memory_;
@@ -132,9 +133,41 @@ bool Wamr::load(std::string_view bytecode, std::string_view /*precompiled*/,
132133
return false;
133134
}
134135

136+
shared_module_ = wasm_module_share(module_.get());
137+
if (shared_module_ == nullptr) {
138+
return false;
139+
}
140+
135141
return true;
136142
}
137143

144+
std::unique_ptr<WasmVm> Wamr::clone() {
145+
assert(module_ != nullptr);
146+
147+
auto vm = std::make_unique<Wamr>();
148+
if (vm == nullptr) {
149+
return nullptr;
150+
}
151+
152+
vm->store_ = wasm_store_new(engine());
153+
if (vm->store_ == nullptr) {
154+
return nullptr;
155+
}
156+
157+
vm->module_ = wasm_module_obtain(vm->store_.get(), shared_module_.get());
158+
if (vm->module_ == nullptr) {
159+
return nullptr;
160+
}
161+
162+
auto *integration_clone = integration()->clone();
163+
if (integration_clone == nullptr) {
164+
return nullptr;
165+
}
166+
vm->integration().reset(integration_clone);
167+
168+
return vm;
169+
}
170+
138171
static bool equalValTypes(const wasm_valtype_vec_t *left, const wasm_valtype_vec_t *right) {
139172
if (left->size != right->size) {
140173
return false;

test/wasm_vm_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getWasmEngines()
3131
});
3232

3333
TEST_P(TestVm, Basic) {
34-
if (engine_ == "wamr" || engine_ == "wasmedge") {
34+
if (engine_ == "wasmedge") {
3535
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::NotCloneable);
36-
} else if (engine_ == "wasmtime" || engine_ == "v8") {
36+
} else if (engine_ == "wasmtime" || engine_ == "v8" || engine_ == "wamr") {
3737
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::CompiledBytecode);
3838
} else if (engine_ == "wavm") {
3939
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::InstantiatedModule);

0 commit comments

Comments
 (0)