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.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 26660 : TEST(CacheHit) {
31 : Isolate* isolate = CcTest::InitIsolateOnce();
32 4 : auto module = NewModule(isolate);
33 4 : TestSignatures sigs;
34 8 : WasmCodeRefScope wasm_code_ref_scope;
35 :
36 : auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
37 :
38 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
39 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
40 :
41 4 : CHECK_NOT_NULL(c1);
42 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
43 :
44 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
45 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
46 :
47 4 : CHECK_NOT_NULL(c2);
48 4 : CHECK_EQ(c1, c2);
49 4 : }
50 :
51 26660 : TEST(CacheMissSig) {
52 : Isolate* isolate = CcTest::InitIsolateOnce();
53 4 : auto module = NewModule(isolate);
54 4 : TestSignatures sigs;
55 8 : WasmCodeRefScope wasm_code_ref_scope;
56 :
57 : auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
58 :
59 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
60 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
61 :
62 4 : CHECK_NOT_NULL(c1);
63 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
64 :
65 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
66 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_ii());
67 :
68 4 : CHECK_NOT_NULL(c2);
69 4 : CHECK_NE(c1, c2);
70 4 : }
71 :
72 26660 : TEST(CacheMissKind) {
73 : Isolate* isolate = CcTest::InitIsolateOnce();
74 4 : auto module = NewModule(isolate);
75 4 : TestSignatures sigs;
76 8 : WasmCodeRefScope wasm_code_ref_scope;
77 :
78 : auto kind1 = compiler::WasmImportCallKind::kJSFunctionArityMatch;
79 : auto kind2 = compiler::WasmImportCallKind::kJSFunctionArityMismatch;
80 :
81 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
82 4 : isolate->wasm_engine(), isolate->counters(), kind1, sigs.i_i());
83 :
84 4 : CHECK_NOT_NULL(c1);
85 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
86 :
87 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
88 4 : isolate->wasm_engine(), isolate->counters(), kind2, sigs.i_i());
89 :
90 4 : CHECK_NOT_NULL(c2);
91 4 : CHECK_NE(c1, c2);
92 4 : }
93 :
94 26660 : TEST(CacheHitMissSig) {
95 : Isolate* isolate = CcTest::InitIsolateOnce();
96 4 : auto module = NewModule(isolate);
97 4 : TestSignatures sigs;
98 8 : WasmCodeRefScope wasm_code_ref_scope;
99 :
100 : auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
101 :
102 : WasmCode* c1 = module->import_wrapper_cache()->GetOrCompile(
103 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
104 :
105 4 : CHECK_NOT_NULL(c1);
106 4 : CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
107 :
108 : WasmCode* c2 = module->import_wrapper_cache()->GetOrCompile(
109 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_ii());
110 :
111 4 : CHECK_NOT_NULL(c2);
112 4 : CHECK_NE(c1, c2);
113 :
114 : WasmCode* c3 = module->import_wrapper_cache()->GetOrCompile(
115 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_i());
116 :
117 4 : CHECK_NOT_NULL(c3);
118 4 : CHECK_EQ(c1, c3);
119 :
120 : WasmCode* c4 = module->import_wrapper_cache()->GetOrCompile(
121 4 : isolate->wasm_engine(), isolate->counters(), kind, sigs.i_ii());
122 :
123 4 : CHECK_NOT_NULL(c4);
124 4 : CHECK_EQ(c2, c4);
125 4 : }
126 :
127 : } // namespace test_wasm_import_wrapper_cache
128 : } // namespace wasm
129 : } // namespace internal
130 79968 : } // namespace v8
|