Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seperate JIT build and add RISC-V build #300

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,36 @@ jobs:
python3 ./tools/run-tests.py --engine="./out/pure/walrus" basic-tests wasm-test-core jit
python3 ./tools/run-tests.py --jit --engine="./out/pure/walrus" basic-tests wasm-test-core jit

build-test-on-riscv64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Build in riscv64 container
uses: uraimo/[email protected]
with:
arch: riscv64
distro: ubuntu22.04

# Install deps into the container. With the token, the container will be cached
# The image is cached publically like a package
githubToken: ${{ github.token }}

install: |
apt-get update
apt-get install -y cmake build-essential ninja-build pkg-config python3 clang git
run: |
# TODO enable JIT for RISC-V
CC=clang CXX=clang++ cmake -H. -Bout/release -DWALRUS_JIT=OFF -DWALRUS_MODE=release -DWALRUS_OUTPUT=shell -GNinja
CC=clang CXX=clang++ cmake -H. -Bout/pure -DWALRUS_JIT=OFF -DWALRUS_MODE=release -DWALRUS_OUTPUT=shell -DWALRUS_WASI=OFF -GNinja
ninja -Cout/release
ninja -Cout/pure
python3 ./tools/run-tests.py --engine="./out/release/walrus"
#python3 ./tools/run-tests.py --jit --engine="./out/release/walrus"
python3 ./tools/run-tests.py --engine="./out/pure/walrus" basic-tests wasm-test-core jit
#python3 ./tools/run-tests.py --jit --engine="./out/pure/walrus" basic-tests wasm-test-core jit

test-on-windows-x86-x64:
runs-on: windows-2022
strategy:
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ IF (NOT DEFINED WALRUS_ARCH)
SET(WALRUS_ARCH "arm")
ELSEIF (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
SET(WALRUS_ARCH "aarch64")
ELSEIF (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "riscv64")
SET(WALRUS_ARCH "riscv64")
ENDIF()
ENDIF()

Expand Down
8 changes: 8 additions & 0 deletions build/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ IF (${WALRUS_OUTPUT} STREQUAL "shared_lib" AND ${WALRUS_HOST} STREQUAL "android"
SET (WALRUS_LDFLAGS ${WALRUS_LDFLAGS} -shared)
ENDIF()

IF (NOT DEFINED WALRUS_JIT)
SET (WALRUS_JIT ON)
ENDIF()

#######################################################
# FLAGS FOR ADDITIONAL FUNCTION
#######################################################
SET (WALRUS_LIBRARIES)
SET (WALRUS_INCDIRS)

IF (WALRUS_JIT)
SET (WALRUS_DEFINITIONS ${WALRUS_DEFINITIONS} -DWALRUS_ENABLE_JIT)
ENDIF()

#######################################################
# FLAGS FOR TEST
#######################################################
Expand Down
6 changes: 6 additions & 0 deletions src/Walrus.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ if (f.type == Type::B) { puts("failed in msvc."); }
#elif defined(__aarch64__)
#define CPU_ARM64

#elif defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 32
#define CPU_RISCV32

#elif defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
#define CPU_RISCV64

#else
#error "Could't find cpu arch."
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/interpreter/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ class Interpreter {
size_t programCounter = reinterpret_cast<size_t>(moduleFunction->byteCode());
ByteCodeStackOffset* resultOffsets;

#if defined(WALRUS_ENABLE_JIT)
if (moduleFunction->jitFunction() != nullptr) {
resultOffsets = moduleFunction->jitFunction()->call(newState, function->instance(), functionStackBase);
} else if (considerException) {
#else
if (considerException) {
#endif
while (true) {
try {
resultOffsets = interpret(newState, programCounter, functionStackBase, function->instance());
Expand Down
6 changes: 4 additions & 2 deletions src/jit/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

#include "Walrus.h"
#if defined(WALRUS_ENABLE_JIT)

#include "Walrus.h"
#include "jit/Compiler.h"

#include <set>

namespace Walrus {
Expand Down Expand Up @@ -780,3 +780,5 @@ void JITCompiler::buildVariables(uint32_t requiredStackSize)
}

} // namespace Walrus

#endif // WALRUS_ENABLE_JIT
4 changes: 4 additions & 0 deletions src/jit/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#if defined(WALRUS_ENABLE_JIT)

#include "Walrus.h"

#include "runtime/Global.h"
Expand Down Expand Up @@ -1682,3 +1684,5 @@ void JITCompiler::emitEpilog()
}

} // namespace Walrus

#endif // WALRUS_ENABLE_JIT
4 changes: 4 additions & 0 deletions src/jit/ByteCodeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#if defined(WALRUS_ENABLE_JIT)

#include "Walrus.h"

#include "jit/Compiler.h"
Expand Down Expand Up @@ -2259,3 +2261,5 @@ void Module::jitCompile(ModuleFunction** functions, size_t functionsLength, uint
}

} // namespace Walrus

#endif // WALRUS_ENABLE_JIT
3 changes: 3 additions & 0 deletions src/jit/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef __WalrusJITCompiler__
#define __WalrusJITCompiler__

#if defined(WALRUS_ENABLE_JIT)

#include "interpreter/ByteCode.h"
#include "jit/SljitLir.h"
#include "runtime/Module.h"
Expand Down Expand Up @@ -841,4 +843,5 @@ class JITCompiler {

} // namespace Walrus

#endif // WALRUS_ENABLE_JIT
#endif // __WalrusJITCompiler__
4 changes: 4 additions & 0 deletions src/jit/InstList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#if defined(WALRUS_ENABLE_JIT)

#include "Walrus.h"

#include "jit/Compiler.h"
Expand Down Expand Up @@ -517,3 +519,5 @@ void JITCompiler::append(InstructionListItem* item)
}

} // namespace Walrus

#endif // WALRUS_ENABLE_JIT
6 changes: 4 additions & 2 deletions src/jit/RegisterAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

#include "Walrus.h"
#if defined(WALRUS_ENABLE_JIT)

#include "Walrus.h"
#include "jit/Compiler.h"

#include <set>

namespace Walrus {
Expand Down Expand Up @@ -1056,3 +1056,5 @@ void JITCompiler::freeVariables()
}

} // namespace Walrus

#endif // WALRUS_ENABLE_JIT
3 changes: 3 additions & 0 deletions src/jit/SljitLir.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef __WalrusSljitLir__
#define __WalrusSljitLir__

#if defined(WALRUS_ENABLE_JIT)

// Setup the configuration
#define SLJIT_CONFIG_AUTO 1
#define SLJIT_CONFIG_STATIC 1
Expand All @@ -32,4 +34,5 @@ extern "C" {
#include "../../third_party/sljit/sljit_src/sljitLir.h"
}

#endif // WALRUS_ENABLE_JIT
#endif // __WalrusSljitLir__
2 changes: 2 additions & 0 deletions src/parser/WASMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2737,9 +2737,11 @@ std::pair<Optional<Module*>, std::string> WASMParser::parseBinary(Store* store,
}

Module* module = new Module(store, delegate.parsingResult());
#if defined(WALRUS_ENABLE_JIT)
if (JITFlags & JITFlagValue::useJIT) {
module->jitCompile(nullptr, 0, JITFlags);
}
#endif

return std::make_pair(module, std::string());
}
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ ModuleFunction::ModuleFunction(FunctionType* functionType)
: m_hasTryCatch(false)
, m_requiredStackSize(std::max(functionType->paramStackSize(), functionType->resultStackSize()))
, m_functionType(functionType)
#if defined(WALRUS_ENABLE_JIT)
, m_jitFunction(nullptr)
#endif
{
}

Expand All @@ -55,16 +57,20 @@ Module::Module(Store* store, WASMParsingResult& result)
, m_tableTypes(std::move(result.m_tableTypes))
, m_memoryTypes(std::move(result.m_memoryTypes))
, m_tagTypes(std::move(result.m_tagTypes))
#if defined(WALRUS_ENABLE_JIT)
, m_jitModule(nullptr)
#endif
{
store->appendModule(this);
}

ModuleFunction::~ModuleFunction()
{
#if defined(WALRUS_ENABLE_JIT)
if (m_jitFunction != nullptr) {
delete m_jitFunction;
}
#endif
}

Module::~Module()
Expand Down Expand Up @@ -109,9 +115,11 @@ Module::~Module()
delete m_tagTypes[i];
}

#if defined(WALRUS_ENABLE_JIT)
if (m_jitModule != nullptr) {
delete m_jitModule;
}
#endif
}

Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports)
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class ModuleFunction {
return m_catchInfo;
}

#if defined(WALRUS_ENABLE_JIT)
void setJITFunction(JITFunction* jitFunction)
{
ASSERT(m_jitFunction == nullptr);
Expand All @@ -243,6 +244,7 @@ class ModuleFunction {
{
return m_jitFunction;
}
#endif

private:
bool m_hasTryCatch;
Expand All @@ -255,7 +257,9 @@ class ModuleFunction {
Vector<std::pair<Value, size_t>, std::allocator<std::pair<Value, size_t>>> m_constantDebugData;
#endif
Vector<CatchInfo, std::allocator<CatchInfo>> m_catchInfo;
#if defined(WALRUS_ENABLE_JIT)
JITFunction* m_jitFunction;
#endif
};

class Data {
Expand Down Expand Up @@ -449,8 +453,10 @@ class Module : public Object {

Instance* instantiate(ExecutionState& state, const ExternVector& imports);

#if defined(WALRUS_ENABLE_JIT)
/* Passing 0 as functionsLength compiles all functions. */
void jitCompile(ModuleFunction** functions, size_t functionsLength, uint32_t JITFlags);
#endif

private:
Store* m_store;
Expand All @@ -471,7 +477,9 @@ class Module : public Object {
TableTypeVector m_tableTypes;
MemoryTypeVector m_memoryTypes;
TagTypeVector m_tagTypes;
#if defined(WALRUS_ENABLE_JIT)
JITModule* m_jitModule;
#endif
};

} // namespace Walrus
Expand Down
6 changes: 3 additions & 3 deletions src/shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ static void parseArguments(int argc, char* argv[], ParseOptions& options)
++i;
options.exportToRun = argv[i];
continue;
#if defined(WALRUS_ENABLE_JIT)
} else if (strcmp(argv[i], "--jit") == 0) {
s_JITFlags |= JITFlagValue::useJIT;
continue;
Expand All @@ -1064,9 +1065,8 @@ static void parseArguments(int argc, char* argv[], ParseOptions& options)
} else if (strcmp(argv[i], "--jit-no-reg-alloc") == 0) {
s_JITFlags |= JITFlagValue::disableRegAlloc;
continue;
}

else if (strcmp(argv[i], "--env") == 0) {
#endif
} else if (strcmp(argv[i], "--env") == 0) {
if (i + 1 == argc || argv[i + 1][0] == '-') {
fprintf(stderr, "error: --env requires an argument\n");
exit(1);
Expand Down
Loading