V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
wasm-import-wrapper-cache-inl.h
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/handles-inl.h"
11 #include "src/objects/code-inl.h"
12 #include "src/wasm/value-type.h"
13 #include "src/wasm/wasm-code-manager.h"
14 
15 namespace v8 {
16 namespace internal {
17 namespace wasm {
18 
19 // Implements a cache for import wrappers.
21  public:
22  WasmCode* GetOrCompile(Isolate* isolate, compiler::WasmImportCallKind kind,
23  FunctionSig* sig) {
24  // TODO(titzer): remove the isolate parameter.
25  base::MutexGuard lock(&mutex_);
26  CacheKey key(static_cast<uint8_t>(kind), *sig);
27  WasmCode*& cached = entry_map_[key];
28  if (cached == nullptr) {
29  // TODO(wasm): no need to hold the lock while compiling an import wrapper.
30  HandleScope scope(isolate);
31  bool source_positions = native_module_->module()->origin == kAsmJsOrigin;
32  cached = compiler::CompileWasmImportCallWrapper(
33  isolate, native_module_, kind, sig, source_positions);
34  auto counters = isolate->counters();
35  counters->wasm_generated_code_size()->Increment(
36  cached->instructions().length());
37  counters->wasm_reloc_size()->Increment(cached->reloc_info().length());
38  }
39  return cached;
40  }
41 
42  private:
43  friend class NativeModule;
44  mutable base::Mutex mutex_;
45  NativeModule* native_module_;
46  using CacheKey = std::pair<uint8_t, FunctionSig>;
47  std::unordered_map<CacheKey, WasmCode*, base::hash<CacheKey>> entry_map_;
48 
49  explicit WasmImportWrapperCache(NativeModule* native_module)
50  : native_module_(native_module) {}
51 };
52 
53 } // namespace wasm
54 } // namespace internal
55 } // namespace v8
56 
57 #endif // V8_WASM_WASM_IMPORT_WRAPPER_CACHE_INL_H_
Definition: libplatform.h:13