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 : #include "src/compiler/wasm-compiler.h"
6 : #include "src/wasm/function-compiler.h"
7 : #include "src/wasm/wasm-code-manager.h"
8 : #include "src/wasm/wasm-engine.h"
9 : #include "src/wasm/wasm-import-wrapper-cache-inl.h"
10 : #include "src/wasm/wasm-module.h"
11 :
12 : #include "test/cctest/cctest.h"
13 : #include "test/common/wasm/test-signatures.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 : namespace wasm {
18 : namespace test_wasm_import_wrapper_cache {
19 :
20 16 : std::shared_ptr<NativeModule> NewModule(Isolate* isolate) {
21 32 : std::shared_ptr<WasmModule> module(new WasmModule);
22 : bool can_request_more = false;
23 : size_t size = 16384;
24 : auto native_module = isolate->wasm_engine()->NewNativeModule(
25 32 : isolate, kAllWasmFeatures, size, can_request_more, std::move(module));
26 16 : native_module->SetRuntimeStubs(isolate);
27 16 : return native_module;
28 : }
29 :
30 26067 : TEST(CacheHit) {
31 : Isolate* isolate = CcTest::InitIsolateOnce();
32 4 : auto module = NewModule(isolate);
33 4 : TestSignatures sigs;
34 :
35 : auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
36 :
37 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
38 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
39 :
40 4 : CHECK_NOT_NULL(c1);
41 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
42 :
43 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
44 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
45 :
46 4 : CHECK_NOT_NULL(c2);
47 4 : CHECK_EQ(c1, c2);
48 4 : }
49 :
50 26067 : TEST(CacheMissSig) {
51 : Isolate* isolate = CcTest::InitIsolateOnce();
52 4 : auto module = NewModule(isolate);
53 4 : TestSignatures sigs;
54 :
55 : auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
56 :
57 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
58 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
59 :
60 4 : CHECK_NOT_NULL(c1);
61 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
62 :
63 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
64 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_ii());
65 :
66 4 : CHECK_NOT_NULL(c2);
67 4 : CHECK_NE(c1, c2);
68 4 : }
69 :
70 26067 : TEST(CacheMissKind) {
71 : Isolate* isolate = CcTest::InitIsolateOnce();
72 4 : auto module = NewModule(isolate);
73 4 : TestSignatures sigs;
74 :
75 : auto kind1 = compiler::WasmImportCallKind::kJSFunctionArityMatch;
76 : auto kind2 = compiler::WasmImportCallKind::kJSFunctionArityMismatch;
77 :
78 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
79 4 : isolate->wasm_engine(), isolate->counters(), kind1, sigs.i_i());
80 :
81 4 : CHECK_NOT_NULL(c1);
82 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
83 :
84 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
85 4 : isolate->wasm_engine(), isolate->counters(), kind2, sigs.i_i());
86 :
87 4 : CHECK_NOT_NULL(c2);
88 4 : CHECK_NE(c1, c2);
89 4 : }
90 :
91 26067 : TEST(CacheHitMissSig) {
92 : Isolate* isolate = CcTest::InitIsolateOnce();
93 4 : auto module = NewModule(isolate);
94 4 : TestSignatures sigs;
95 :
96 : auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
97 :
98 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
99 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
100 :
101 4 : CHECK_NOT_NULL(c1);
102 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
103 :
104 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
105 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_ii());
106 :
107 4 : CHECK_NOT_NULL(c2);
108 4 : CHECK_NE(c1, c2);
109 :
110 : WasmCode* c3 = module->import_wrapper_cache()->GetOrCompile(
111 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
112 :
113 4 : CHECK_NOT_NULL(c3);
114 4 : CHECK_EQ(c1, c3);
115 :
116 : WasmCode* c4 = module->import_wrapper_cache()->GetOrCompile(
117 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_ii());
118 :
119 4 : CHECK_NOT_NULL(c4);
120 4 : CHECK_EQ(c2, c4);
121 4 : }
122 :
123 : } // namespace test_wasm_import_wrapper_cache
124 : } // namespace wasm
125 : } // namespace internal
126 78189 : } // namespace v8
|