2018-10-10 12:22:19 +00:00
|
|
|
// 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"
|
2019-05-23 15:12:58 +00:00
|
|
|
#include "src/wasm/module-compiler.h"
|
2018-10-10 12:22:19 +00:00
|
|
|
#include "src/wasm/wasm-code-manager.h"
|
|
|
|
#include "src/wasm/wasm-engine.h"
|
2019-03-22 12:41:21 +00:00
|
|
|
#include "src/wasm/wasm-import-wrapper-cache.h"
|
2018-10-10 12:22:19 +00:00
|
|
|
#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 {
|
|
|
|
|
2019-03-12 16:10:58 +00:00
|
|
|
std::shared_ptr<NativeModule> NewModule(Isolate* isolate) {
|
2018-10-10 12:22:19 +00:00
|
|
|
std::shared_ptr<WasmModule> module(new WasmModule);
|
2019-11-07 17:16:45 +00:00
|
|
|
constexpr size_t kCodeSizeEstimate = 16384;
|
2021-06-18 14:29:39 +00:00
|
|
|
auto native_module = GetWasmEngine()->NewNativeModule(
|
2019-11-26 16:25:14 +00:00
|
|
|
isolate, WasmFeatures::All(), std::move(module), kCodeSizeEstimate);
|
2019-12-18 13:41:12 +00:00
|
|
|
native_module->SetWireBytes({});
|
|
|
|
return native_module;
|
2018-10-10 12:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheHit) {
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
auto module = NewModule(isolate);
|
|
|
|
TestSignatures sigs;
|
2019-04-02 10:41:00 +00:00
|
|
|
WasmCodeRefScope wasm_code_ref_scope;
|
2019-05-23 15:12:58 +00:00
|
|
|
WasmImportWrapperCache::ModificationScope cache_scope(
|
|
|
|
module->import_wrapper_cache());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
|
2020-07-27 19:19:55 +00:00
|
|
|
auto sig = sigs.i_i();
|
|
|
|
int expected_arity = static_cast<int>(sig->parameter_count());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
2021-06-21 11:20:13 +00:00
|
|
|
WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind,
|
|
|
|
sig, expected_arity, &cache_scope);
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NOT_NULL(c1);
|
|
|
|
CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
|
|
|
|
|
2020-07-27 19:19:55 +00:00
|
|
|
WasmCode* c2 = cache_scope[{kind, sig, expected_arity}];
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NOT_NULL(c2);
|
|
|
|
CHECK_EQ(c1, c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheMissSig) {
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
auto module = NewModule(isolate);
|
|
|
|
TestSignatures sigs;
|
2019-04-02 10:41:00 +00:00
|
|
|
WasmCodeRefScope wasm_code_ref_scope;
|
2019-05-23 15:12:58 +00:00
|
|
|
WasmImportWrapperCache::ModificationScope cache_scope(
|
|
|
|
module->import_wrapper_cache());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
|
2020-07-27 19:19:55 +00:00
|
|
|
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());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
2021-06-21 11:20:13 +00:00
|
|
|
WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind,
|
|
|
|
sig1, expected_arity1, &cache_scope);
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NOT_NULL(c1);
|
|
|
|
CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
|
|
|
|
|
2020-07-27 19:19:55 +00:00
|
|
|
WasmCode* c2 = cache_scope[{kind, sig2, expected_arity2}];
|
2018-10-10 12:22:19 +00:00
|
|
|
|
2019-05-23 15:12:58 +00:00
|
|
|
CHECK_NULL(c2);
|
2018-10-10 12:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheMissKind) {
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
auto module = NewModule(isolate);
|
|
|
|
TestSignatures sigs;
|
2019-04-02 10:41:00 +00:00
|
|
|
WasmCodeRefScope wasm_code_ref_scope;
|
2019-05-23 15:12:58 +00:00
|
|
|
WasmImportWrapperCache::ModificationScope cache_scope(
|
|
|
|
module->import_wrapper_cache());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
auto kind1 = compiler::WasmImportCallKind::kJSFunctionArityMatch;
|
|
|
|
auto kind2 = compiler::WasmImportCallKind::kJSFunctionArityMismatch;
|
2020-07-27 19:19:55 +00:00
|
|
|
auto sig = sigs.i_i();
|
|
|
|
int expected_arity = static_cast<int>(sig->parameter_count());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
2021-06-21 11:20:13 +00:00
|
|
|
WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind1,
|
|
|
|
sig, expected_arity, &cache_scope);
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NOT_NULL(c1);
|
|
|
|
CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
|
|
|
|
|
2020-07-27 19:19:55 +00:00
|
|
|
WasmCode* c2 = cache_scope[{kind2, sig, expected_arity}];
|
2018-10-10 12:22:19 +00:00
|
|
|
|
2019-05-23 15:12:58 +00:00
|
|
|
CHECK_NULL(c2);
|
2018-10-10 12:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheHitMissSig) {
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
auto module = NewModule(isolate);
|
|
|
|
TestSignatures sigs;
|
2019-04-02 10:41:00 +00:00
|
|
|
WasmCodeRefScope wasm_code_ref_scope;
|
2019-05-23 15:12:58 +00:00
|
|
|
WasmImportWrapperCache::ModificationScope cache_scope(
|
|
|
|
module->import_wrapper_cache());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
auto kind = compiler::WasmImportCallKind::kJSFunctionArityMatch;
|
2020-07-27 19:19:55 +00:00
|
|
|
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());
|
2018-10-10 12:22:19 +00:00
|
|
|
|
2021-06-21 11:20:13 +00:00
|
|
|
WasmCode* c1 = CompileImportWrapper(module.get(), isolate->counters(), kind,
|
|
|
|
sig1, expected_arity1, &cache_scope);
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NOT_NULL(c1);
|
|
|
|
CHECK_EQ(WasmCode::Kind::kWasmToJsWrapper, c1->kind());
|
|
|
|
|
2020-07-27 19:19:55 +00:00
|
|
|
WasmCode* c2 = cache_scope[{kind, sig2, expected_arity2}];
|
2019-05-23 15:12:58 +00:00
|
|
|
|
|
|
|
CHECK_NULL(c2);
|
|
|
|
|
2021-06-21 11:20:13 +00:00
|
|
|
c2 = CompileImportWrapper(module.get(), isolate->counters(), kind, sig2,
|
|
|
|
expected_arity2, &cache_scope);
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NE(c1, c2);
|
|
|
|
|
2020-07-27 19:19:55 +00:00
|
|
|
WasmCode* c3 = cache_scope[{kind, sig1, expected_arity1}];
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NOT_NULL(c3);
|
|
|
|
CHECK_EQ(c1, c3);
|
|
|
|
|
2020-07-27 19:19:55 +00:00
|
|
|
WasmCode* c4 = cache_scope[{kind, sig2, expected_arity2}];
|
2018-10-10 12:22:19 +00:00
|
|
|
|
|
|
|
CHECK_NOT_NULL(c4);
|
|
|
|
CHECK_EQ(c2, c4);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace test_wasm_import_wrapper_cache
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|