summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/wasm/test-wasm-import-wrapper-cache.cc
blob: 15267215e1ab7d7d2a507b4f7404a15c5469ee5d (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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"
#include "src/wasm/module-compiler.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-import-wrapper-cache.h"
#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 {

std::shared_ptr<NativeModule> NewModule(Isolate* isolate) {
  std::shared_ptr<WasmModule> module(new WasmModule);
  bool can_request_more = false;
  size_t size = 16384;
  return isolate->wasm_engine()->NewNativeModule(
      isolate, kAllWasmFeatures, size, can_request_more, std::move(module));
}

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

  auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;

  WasmCode* c1 =
      CompileImportWrapper(isolate->wasm_engine(), module.get(),
                           isolate->counters(), kind, sigs.i_i(), &cache_scope);

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

  WasmCode* c2 = cache_scope[{kind, sigs.i_i()}];

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

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

  auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;

  WasmCode* c1 =
      CompileImportWrapper(isolate->wasm_engine(), module.get(),
                           isolate->counters(), kind, sigs.i_i(), &cache_scope);

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

  WasmCode* c2 = cache_scope[{kind, sigs.i_ii()}];

  CHECK_NULL(c2);
}

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

  auto kind1 = compiler::WasmImportCallKind::kJSFunctionArityMatch;
  auto kind2 = compiler::WasmImportCallKind::kJSFunctionArityMismatch;

  WasmCode* c1 = CompileImportWrapper(isolate->wasm_engine(), module.get(),
                                      isolate->counters(), kind1, sigs.i_i(),
                                      &cache_scope);

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

  WasmCode* c2 = cache_scope[{kind2, sigs.i_i()}];

  CHECK_NULL(c2);
}

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

  auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;

  WasmCode* c1 =
      CompileImportWrapper(isolate->wasm_engine(), module.get(),
                           isolate->counters(), kind, sigs.i_i(), &cache_scope);

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

  WasmCode* c2 = cache_scope[{kind, sigs.i_ii()}];

  CHECK_NULL(c2);

  c2 = CompileImportWrapper(isolate->wasm_engine(), module.get(),
                            isolate->counters(), kind, sigs.i_ii(),
                            &cache_scope);

  CHECK_NE(c1, c2);

  WasmCode* c3 = cache_scope[{kind, sigs.i_i()}];

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

  WasmCode* c4 = cache_scope[{kind, sigs.i_ii()}];

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

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