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