Skip to content

Commit

Permalink
Add a seperate build option for JIT
Browse files Browse the repository at this point in the history
Signed-off-by: HyukWoo Park <[email protected]>
  • Loading branch information
clover2123 authored and ksh8281 committed Nov 8, 2024
1 parent 3c714e5 commit 8fae950
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 7 deletions.
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
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

0 comments on commit 8fae950

Please sign in to comment.