test-wasm-import-wrapper-cache.cc 4.6 KB
Newer Older
1 2 3 4 5 6
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/wasm-compiler.h"
#include "src/wasm/function-compiler.h"
7
#include "src/wasm/module-compiler.h"
8 9
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-engine.h"
10
#include "src/wasm/wasm-import-wrapper-cache.h"
11 12 13 14 15 16 17 18 19 20
#include "src/wasm/wasm-module.h"

#include "test/cctest/cctest.h"
#include "test/common/wasm/test-signatures.h"

namespace v8 {
namespace internal {
namespace wasm {
namespace test_wasm_import_wrapper_cache {

21
std::shared_ptr<NativeModule> NewModule(Isolate* isolate) {
22
  std::shared_ptr<WasmModule> module(new WasmModule);
23
  constexpr size_t kCodeSizeEstimate = 16384;
24
  auto native_module = GetWasmEngine()->NewNativeModule(
25
      isolate, WasmFeatures::All(), std::move(module), kCodeSizeEstimate);
26 27
  native_module->SetWireBytes({});
  return native_module;
28 29 30 31 32 33
}

TEST(CacheHit) {
  Isolate* isolate = CcTest::InitIsolateOnce();
  auto module = NewModule(isolate);
  TestSignatures sigs;
34
  WasmCodeRefScope wasm_code_ref_scope;
35 36
  WasmImportWrapperCache::ModificationScope cache_scope(
      module->import_wrapper_cache());
37 38

  auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
39 40
  auto sig = sigs.i_i();
  int expected_arity = static_cast<int>(sig->parameter_count());
41

42 43
  WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind,
                                      sig, expected_arity, &cache_scope);
44 45 46 47

  CHECK_NOT_NULL(c1);
  CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());

48
  WasmCode* c2 = cache_scope[{kind, sig, expected_arity}];
49 50 51 52 53 54 55 56 57

  CHECK_NOT_NULL(c2);
  CHECK_EQ(c1, c2);
}

TEST(CacheMissSig) {
  Isolate* isolate = CcTest::InitIsolateOnce();
  auto module = NewModule(isolate);
  TestSignatures sigs;
58
  WasmCodeRefScope wasm_code_ref_scope;
59 60
  WasmImportWrapperCache::ModificationScope cache_scope(
      module->import_wrapper_cache());
61 62

  auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
63 64 65 66
  auto sig1 = sigs.i_i();
  int expected_arity1 = static_cast<int>(sig1->parameter_count());
  auto sig2 = sigs.i_ii();
  int expected_arity2 = static_cast<int>(sig2->parameter_count());
67

68 69
  WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind,
                                      sig1, expected_arity1, &cache_scope);
70 71 72 73

  CHECK_NOT_NULL(c1);
  CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());

74
  WasmCode* c2 = cache_scope[{kind, sig2, expected_arity2}];
75

76
  CHECK_NULL(c2);
77 78 79 80 81 82
}

TEST(CacheMissKind) {
  Isolate* isolate = CcTest::InitIsolateOnce();
  auto module = NewModule(isolate);
  TestSignatures sigs;
83
  WasmCodeRefScope wasm_code_ref_scope;
84 85
  WasmImportWrapperCache::ModificationScope cache_scope(
      module->import_wrapper_cache());
86 87 88

  auto kind1 = compiler::WasmImportCallKind::kJSFunctionArityMatch;
  auto kind2 = compiler::WasmImportCallKind::kJSFunctionArityMismatch;
89 90
  auto sig = sigs.i_i();
  int expected_arity = static_cast<int>(sig->parameter_count());
91

92 93
  WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind1,
                                      sig, expected_arity, &cache_scope);
94 95 96 97

  CHECK_NOT_NULL(c1);
  CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());

98
  WasmCode* c2 = cache_scope[{kind2, sig, expected_arity}];
99

100
  CHECK_NULL(c2);
101 102 103 104 105 106
}

TEST(CacheHitMissSig) {
  Isolate* isolate = CcTest::InitIsolateOnce();
  auto module = NewModule(isolate);
  TestSignatures sigs;
107
  WasmCodeRefScope wasm_code_ref_scope;
108 109
  WasmImportWrapperCache::ModificationScope cache_scope(
      module->import_wrapper_cache());
110 111

  auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
112 113 114 115
  auto sig1 = sigs.i_i();
  int expected_arity1 = static_cast<int>(sig1->parameter_count());
  auto sig2 = sigs.i_ii();
  int expected_arity2 = static_cast<int>(sig2->parameter_count());
116

117 118
  WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind,
                                      sig1, expected_arity1, &cache_scope);
119 120 121 122

  CHECK_NOT_NULL(c1);
  CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());

123
  WasmCode* c2 = cache_scope[{kind, sig2, expected_arity2}];
124 125 126

  CHECK_NULL(c2);

127 128
  c2 = CompileImportWrapper(module.get(), isolate->counters(), kind, sig2,
                            expected_arity2, &cache_scope);
129 130 131

  CHECK_NE(c1, c2);

132
  WasmCode* c3 = cache_scope[{kind, sig1, expected_arity1}];
133 134 135 136

  CHECK_NOT_NULL(c3);
  CHECK_EQ(c1, c3);

137
  WasmCode* c4 = cache_scope[{kind, sig2, expected_arity2}];
138 139 140 141 142 143 144 145 146

  CHECK_NOT_NULL(c4);
  CHECK_EQ(c2, c4);
}

}  // namespace test_wasm_import_wrapper_cache
}  // namespace wasm
}  // namespace internal
}  // namespace v8