From 9bc023bd1bd50acc583c27ce8a0417fecf2b9b54 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 3 Dec 2021 15:07:29 -0500 Subject: [PATCH 1/8] chore: rename instr --- runtime/compiletime/{instr.c => numeric_instructions.c} | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename runtime/compiletime/{instr.c => numeric_instructions.c} (99%) diff --git a/runtime/compiletime/instr.c b/runtime/compiletime/numeric_instructions.c similarity index 99% rename from runtime/compiletime/instr.c rename to runtime/compiletime/numeric_instructions.c index 2664d53..c596f18 100644 --- a/runtime/compiletime/instr.c +++ b/runtime/compiletime/numeric_instructions.c @@ -1,6 +1,7 @@ #include #include -#include + +#include "types.h" #define CHAR_BIT 8 From 30c3d17a2cc876c9ed7ea20cea92149cca0e59ff Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 3 Dec 2021 15:08:04 -0500 Subject: [PATCH 2/8] refactor: Rename memory_instructions --- runtime/compiletime/{memory/64bit_nix.c => memory_instructions.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename runtime/compiletime/{memory/64bit_nix.c => memory_instructions.c} (100%) diff --git a/runtime/compiletime/memory/64bit_nix.c b/runtime/compiletime/memory_instructions.c similarity index 100% rename from runtime/compiletime/memory/64bit_nix.c rename to runtime/compiletime/memory_instructions.c From bb41caeae0caf3ed1e9623efc5ed729136843b03 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 3 Dec 2021 15:12:45 -0500 Subject: [PATCH 3/8] refactor: Remove zombie table instruction --- runtime/compiletime/memory_instructions.c | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/runtime/compiletime/memory_instructions.c b/runtime/compiletime/memory_instructions.c index c52f820..347f15f 100644 --- a/runtime/compiletime/memory_instructions.c +++ b/runtime/compiletime/memory_instructions.c @@ -164,19 +164,3 @@ set_global_i64(uint32_t offset, int64_t v) { set_i64(offset, v); } - -// Table handling functionality -// INLINE char * -// get_function_from_table(uint32_t idx, uint32_t type_id) -// { -// assert(idx < INDIRECT_TABLE_SIZE); - -// struct indirect_table_entry f = local_sandbox_context_cache.module_indirect_table[idx]; - -// // FIXME: Commented out function type check because of gocr -// // assert(f.type_id == type_id); - -// assert(f.func_pointer); - -// return f.func_pointer; -// } From 1a34f91cddb203e0653e773a6ead82aee214cdb2 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 3 Dec 2021 15:14:02 -0500 Subject: [PATCH 4/8] refactor: table instructions --- runtime/compiletime/table_instructions.c | 53 ++++++++++++++++++++++++ runtime/src/memory/64bit_nix.c | 27 ------------ runtime/src/memory/common.c | 16 ------- 3 files changed, 53 insertions(+), 43 deletions(-) create mode 100644 runtime/compiletime/table_instructions.c diff --git a/runtime/compiletime/table_instructions.c b/runtime/compiletime/table_instructions.c new file mode 100644 index 0000000..575f1c5 --- /dev/null +++ b/runtime/compiletime/table_instructions.c @@ -0,0 +1,53 @@ + +#include "types.h" + +/* This file contains the stub functions that the aWsm compiler expects + * This corresponds to awsm/src/codegen/runtime_stubs.rs + * This should be linked with the *.bc file generated by aWsm in order to compile a module as a *.so + */ + +extern thread_local struct wasm_module_instance current_wasm_module_instance; + +INLINE void +add_function_to_table(uint32_t idx, uint32_t type_id, char *pointer) +{ + assert(idx < INDIRECT_TABLE_SIZE); + assert(local_sandbox_context_cache.module_indirect_table != NULL); + + /* TODO: atomic for multiple concurrent invocations? Issue #97 */ + if (local_sandbox_context_cache.module_indirect_table[idx].type_id == type_id + && local_sandbox_context_cache.module_indirect_table[idx].func_pointer == pointer) + return; + + local_sandbox_context_cache.module_indirect_table[idx] = (struct indirect_table_entry){ + .type_id = type_id, .func_pointer = pointer + }; +} + +/* + * Table handling functionality + * This was moved from compiletime in order to place the + * function in the callstack in GDB. It can be moved back + * to runtime/compiletime/memory/64bit_nix.c to remove the + * additional function call + */ +INLINE char * +get_function_from_table(uint32_t idx, uint32_t type_id) +{ +#ifdef LOG_FUNCTION_TABLE + fprintf(stderr, "get_function_from_table(idx: %u, type_id: %u)\n", idx, type_id); + fprintf(stderr, "indirect_table_size: %u\n", INDIRECT_TABLE_SIZE); +#endif + assert(idx < INDIRECT_TABLE_SIZE); + + struct indirect_table_entry f = local_sandbox_context_cache.module_indirect_table[idx]; +#ifdef LOG_FUNCTION_TABLE + fprintf(stderr, "assumed type: %u, type in table: %u\n", type_id, f.type_id); +#endif + // FIXME: Commented out function type check because of gocr + // assert(f.type_id == type_id); + + assert(f.func_pointer != NULL); + + return f.func_pointer; +} diff --git a/runtime/src/memory/64bit_nix.c b/runtime/src/memory/64bit_nix.c index 498b7ae..4728236 100644 --- a/runtime/src/memory/64bit_nix.c +++ b/runtime/src/memory/64bit_nix.c @@ -89,30 +89,3 @@ instruction_memory_grow(uint32_t count) return rc; } -/* - * Table handling functionality - * This was moved from compiletime in order to place the - * function in the callstack in GDB. It can be moved back - * to runtime/compiletime/memory/64bit_nix.c to remove the - * additional function call - */ -char * -get_function_from_table(uint32_t idx, uint32_t type_id) -{ -#ifdef LOG_FUNCTION_TABLE - fprintf(stderr, "get_function_from_table(idx: %u, type_id: %u)\n", idx, type_id); - fprintf(stderr, "indirect_table_size: %u\n", INDIRECT_TABLE_SIZE); -#endif - assert(idx < INDIRECT_TABLE_SIZE); - - struct indirect_table_entry f = local_sandbox_context_cache.module_indirect_table[idx]; -#ifdef LOG_FUNCTION_TABLE - fprintf(stderr, "assumed type: %u, type in table: %u\n", type_id, f.type_id); -#endif - // FIXME: Commented out function type check because of gocr - // assert(f.type_id == type_id); - - assert(f.func_pointer != NULL); - - return f.func_pointer; -} diff --git a/runtime/src/memory/common.c b/runtime/src/memory/common.c index a3b4939..1c166aa 100644 --- a/runtime/src/memory/common.c +++ b/runtime/src/memory/common.c @@ -14,22 +14,6 @@ initialize_region(uint32_t offset, uint32_t data_count, char *data) memcpy(get_memory_ptr_for_runtime(offset, data_count), data, data_count); } -void -add_function_to_table(uint32_t idx, uint32_t type_id, char *pointer) -{ - assert(idx < INDIRECT_TABLE_SIZE); - assert(local_sandbox_context_cache.module_indirect_table != NULL); - - /* TODO: atomic for multiple concurrent invocations? Issue #97 */ - if (local_sandbox_context_cache.module_indirect_table[idx].type_id == type_id - && local_sandbox_context_cache.module_indirect_table[idx].func_pointer == pointer) - return; - - local_sandbox_context_cache.module_indirect_table[idx] = (struct indirect_table_entry){ - .type_id = type_id, .func_pointer = pointer - }; -} - /* If we are using runtime globals, we need to populate them */ WEAK void populate_globals() From 93a7ed00175f2e4112c1075e9b80df803d700669 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sat, 4 Dec 2021 14:15:49 -0500 Subject: [PATCH 5/8] chore: format nit --- runtime/src/memory/64bit_nix.c | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/src/memory/64bit_nix.c b/runtime/src/memory/64bit_nix.c index 4728236..915c137 100644 --- a/runtime/src/memory/64bit_nix.c +++ b/runtime/src/memory/64bit_nix.c @@ -88,4 +88,3 @@ instruction_memory_grow(uint32_t count) return rc; } - From ded4717ed828f0c55697de45e2243e171fbcbc1e Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sat, 4 Dec 2021 14:54:17 -0500 Subject: [PATCH 6/8] docs: Create compiletime README --- runtime/compiletime/README.md | 5 +++++ runtime/compiletime/table_instructions.c | 12 ------------ 2 files changed, 5 insertions(+), 12 deletions(-) create mode 100644 runtime/compiletime/README.md diff --git a/runtime/compiletime/README.md b/runtime/compiletime/README.md new file mode 100644 index 0000000..314fbd8 --- /dev/null +++ b/runtime/compiletime/README.md @@ -0,0 +1,5 @@ +A WebAssembly module instance is statically linked with the backing functions implementing the wasm32 ABI, yielding a *.so file that SLEdge can execute. This ensures that the instance is able to aggressively inline and optimize this code. + +They are broken into instruction types as on https://webassembly.github.io/spec/core/exec/instructions.html. They depend on common headers for the WebAssembly types located in the WebAssembly instance struct. These are located in runtime/include/common. + +The stubs correspond to awsm/src/codegen/runtime_stubs.rs diff --git a/runtime/compiletime/table_instructions.c b/runtime/compiletime/table_instructions.c index 575f1c5..2803ddf 100644 --- a/runtime/compiletime/table_instructions.c +++ b/runtime/compiletime/table_instructions.c @@ -1,11 +1,6 @@ #include "types.h" -/* This file contains the stub functions that the aWsm compiler expects - * This corresponds to awsm/src/codegen/runtime_stubs.rs - * This should be linked with the *.bc file generated by aWsm in order to compile a module as a *.so - */ - extern thread_local struct wasm_module_instance current_wasm_module_instance; INLINE void @@ -24,13 +19,6 @@ add_function_to_table(uint32_t idx, uint32_t type_id, char *pointer) }; } -/* - * Table handling functionality - * This was moved from compiletime in order to place the - * function in the callstack in GDB. It can be moved back - * to runtime/compiletime/memory/64bit_nix.c to remove the - * additional function call - */ INLINE char * get_function_from_table(uint32_t idx, uint32_t type_id) { From 13e84078848cbef5ea72e4e1c71e7afc38fe260b Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sat, 4 Dec 2021 15:02:10 -0500 Subject: [PATCH 7/8] chore: Adjust memory path --- .github/workflows/main.yaml | 4 ++-- runtime/tests/Makefile | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 7b525ae..842bdf6 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -101,7 +101,7 @@ jobs: uses: actions/cache@v2 with: path: ./runtime/bin/gocr_wasm.so - key: ${{ runner.os }}-gocr2-${{ hashFiles('./runtime/tests/Makefile', './runtime/tests/gocr/**', './runtime/compiletime/**', './runtime/compiletime/memory/**') }} + key: ${{ runner.os }}-gocr2-${{ hashFiles('./runtime/tests/Makefile', './runtime/tests/gocr/**', './runtime/compiletime/**') }} if: success() || failure() - name: Hyde run: | @@ -138,7 +138,7 @@ jobs: uses: actions/cache@v2 with: path: ./runtime/bin/ekf_wasm.so - key: ${{ runner.os }}-gocr2-${{ hashFiles('./runtime/tests/Makefile', './runtime/tests/TinyEKF/**', './runtime/compiletime/**', './runtime/compiletime/memory/**') }} + key: ${{ runner.os }}-gocr2-${{ hashFiles('./runtime/tests/Makefile', './runtime/tests/TinyEKF/**', './runtime/compiletime/**') }} if: success() || failure() - name: EKF one iteration run: | diff --git a/runtime/tests/Makefile b/runtime/tests/Makefile index 2723001..f4c71dc 100644 --- a/runtime/tests/Makefile +++ b/runtime/tests/Makefile @@ -7,7 +7,7 @@ OPTFLAGS=-O3 -flto SLEDGE_BASE_DIR=../../ SLEDGE_RT_DIR=${SLEDGE_BASE_DIR}/runtime/ SLEDGE_COMPILETIME_INC=${SLEDGE_RT_DIR}/include -SLEDGE_COMPILETIME_SRC=${SLEDGE_RT_DIR}/compiletime/instr.c ${SLEDGE_RT_DIR}/compiletime/memory/64bit_nix.c +SLEDGE_COMPILETIME_SRC=${SLEDGE_RT_DIR}/compiletime/*.c ALL=fibonacci empty ekf cifar10 gocr lpd resize ALL_COPY_DEST=$(ALL:%=../../runtime/bin/%_wasm.so) From 5efee76cae5966ebe06c51bd420c57c64adcd49d Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sat, 4 Dec 2021 15:02:35 -0500 Subject: [PATCH 8/8] refactor: header include nit --- runtime/compiletime/memory_instructions.c | 1 + runtime/compiletime/table_instructions.c | 1 + 2 files changed, 2 insertions(+) diff --git a/runtime/compiletime/memory_instructions.c b/runtime/compiletime/memory_instructions.c index 347f15f..c9e1ed0 100644 --- a/runtime/compiletime/memory_instructions.c +++ b/runtime/compiletime/memory_instructions.c @@ -1,4 +1,5 @@ #include + #include "types.h" uint32_t diff --git a/runtime/compiletime/table_instructions.c b/runtime/compiletime/table_instructions.c index 2803ddf..6ceb323 100644 --- a/runtime/compiletime/table_instructions.c +++ b/runtime/compiletime/table_instructions.c @@ -1,3 +1,4 @@ +#include #include "types.h"