1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#include "WAVM/IR/Module.h"
#include <memory>
#include <utility>
#include "RuntimePrivate.h"
#include "WAVM/IR/IR.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Timing.h"
#include "WAVM/LLVMJIT/LLVMJIT.h"
#include "WAVM/Platform/Intrinsic.h"
#include "WAVM/Platform/RWMutex.h"
#include "WAVM/Runtime/Runtime.h"
#include "WAVM/WASM/WASM.h"
using namespace WAVM;
using namespace WAVM::IR;
using namespace WAVM::Runtime;
Platform::RWMutex globalObjectCacheMutex;
std::shared_ptr<ObjectCacheInterface> globalObjectCache;
void Runtime::setGlobalObjectCache(std::shared_ptr<ObjectCacheInterface>&& objectCache)
{
Platform::RWMutex::ExclusiveLock globalObjectCacheLock(globalObjectCacheMutex);
globalObjectCache = std::move(objectCache);
}
static std::shared_ptr<ObjectCacheInterface> getGlobalObjectCache()
{
Platform::RWMutex::ShareableLock globalObjectCacheLock(globalObjectCacheMutex);
return globalObjectCache;
}
ModuleRef Runtime::compileModule(const IR::Module& irModule)
{
// Get a pointer to the global object cache, if there is one.
std::shared_ptr<ObjectCacheInterface> objectCache = getGlobalObjectCache();
std::vector<U8> objectCode;
if(!objectCache)
{
// If there's no global object cache, just compile the module.
objectCode = LLVMJIT::compileModule(irModule, LLVMJIT::getHostTargetSpec());
}
else
{
// Serialize the IR module to WASM.
Timing::Timer keyTimer;
std::vector<U8> wasmBytes = WASM::saveBinaryModule(irModule);
Timing::logTimer("Created object cache key from IR module", keyTimer);
// Check for cached object code for the module before compiling it.
objectCode
= objectCache->getCachedObject(wasmBytes.data(), wasmBytes.size(), [&irModule]() {
return LLVMJIT::compileModule(irModule, LLVMJIT::getHostTargetSpec());
});
}
return std::make_shared<Runtime::Module>(IR::Module(irModule), std::move(objectCode));
}
bool Runtime::loadBinaryModule(const U8* wasmBytes,
Uptr numWASMBytes,
ModuleRef& outModule,
const IR::FeatureSpec& featureSpec,
WASM::LoadError* outError)
{
// Load the module IR.
IR::Module irModule(std::move(featureSpec));
if(!WASM::loadBinaryModule(wasmBytes, numWASMBytes, irModule, outError)) { return false; }
// Get a pointer to the global object cache, if there is one.
std::shared_ptr<ObjectCacheInterface> objectCache = getGlobalObjectCache();
std::vector<U8> objectCode;
if(!objectCache)
{
// If there's no global object cache, just compile the module.
objectCode = LLVMJIT::compileModule(irModule, LLVMJIT::getHostTargetSpec());
}
else
{
// Check for cached object code for the module before compiling it.
objectCode = objectCache->getCachedObject(wasmBytes, numWASMBytes, [&irModule]() {
return LLVMJIT::compileModule(irModule, LLVMJIT::getHostTargetSpec());
});
}
outModule = std::make_shared<Runtime::Module>(std::move(irModule), std::move(objectCode));
return true;
}
ModuleRef Runtime::loadPrecompiledModule(const IR::Module& irModule,
const std::vector<U8>& objectCode)
{
return std::make_shared<Module>(IR::Module(irModule), std::vector<U8>(objectCode));
}
const IR::Module& Runtime::getModuleIR(ModuleConstRefParam module) { return module->ir; }
std::vector<U8> Runtime::getObjectCode(ModuleConstRefParam module) { return module->objectCode; }
|