Line data Source code
1 : // Copyright 2018 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_WASM_WASM_IMPORT_WRAPPER_CACHE_INL_H_
6 : #define V8_WASM_WASM_IMPORT_WRAPPER_CACHE_INL_H_
7 :
8 : #include "src/compiler/wasm-compiler.h"
9 : #include "src/counters.h"
10 : #include "src/wasm/value-type.h"
11 : #include "src/wasm/wasm-code-manager.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 : namespace wasm {
16 :
17 : // Implements a cache for import wrappers.
18 1241902 : class WasmImportWrapperCache {
19 : public:
20 12493 : WasmCode* GetOrCompile(WasmEngine* wasm_engine, Counters* counters,
21 : compiler::WasmImportCallKind kind, FunctionSig* sig) {
22 12493 : base::MutexGuard lock(&mutex_);
23 : CacheKey key(static_cast<uint8_t>(kind), *sig);
24 : WasmCode*& cached = entry_map_[key];
25 12493 : if (cached == nullptr) {
26 : // TODO(wasm): no need to hold the lock while compiling an import wrapper.
27 6714 : bool source_positions = native_module_->module()->origin == kAsmJsOrigin;
28 6714 : cached = compiler::CompileWasmImportCallWrapper(
29 6714 : wasm_engine, native_module_, kind, sig, source_positions);
30 : counters->wasm_generated_code_size()->Increment(
31 6714 : cached->instructions().length());
32 13428 : counters->wasm_reloc_size()->Increment(cached->reloc_info().length());
33 : }
34 24986 : return cached;
35 : }
36 :
37 : private:
38 : friend class NativeModule;
39 : mutable base::Mutex mutex_;
40 : NativeModule* native_module_;
41 : using CacheKey = std::pair<uint8_t, FunctionSig>;
42 : std::unordered_map<CacheKey, WasmCode*, base::hash<CacheKey>> entry_map_;
43 :
44 : explicit WasmImportWrapperCache(NativeModule* native_module)
45 1241902 : : native_module_(native_module) {}
46 : };
47 :
48 : } // namespace wasm
49 : } // namespace internal
50 : } // namespace v8
51 :
52 : #endif // V8_WASM_WASM_IMPORT_WRAPPER_CACHE_INL_H_
|