v8/test/cctest/wasm/test-compilation-cache.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.0 KiB
C++
Raw Normal View History

// Copyright 2020 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/api/api-inl.h"
#include "src/init/v8.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-module-builder.h"
#include "test/cctest/cctest.h"
#include "test/common/wasm/test-signatures.h"
#include "test/common/wasm/wasm-macro-gen.h"
namespace v8 {
namespace internal {
namespace wasm {
namespace {
class TestResolver : public CompilationResultResolver {
public:
explicit TestResolver(std::atomic<int>* pending)
: native_module_(nullptr), pending_(pending) {}
void OnCompilationSucceeded(i::Handle<i::WasmModuleObject> module) override {
if (!module.is_null()) {
native_module_ = module->shared_native_module();
pending_->fetch_sub(1);
}
}
void OnCompilationFailed(i::Handle<i::Object> error_reason) override {
CHECK(false);
}
std::shared_ptr<NativeModule> native_module() { return native_module_; }
private:
std::shared_ptr<NativeModule> native_module_;
std::atomic<int>* pending_;
};
// Create a valid module such that the bytes depend on {n}.
ZoneBuffer GetValidModuleBytes(Zone* zone, int n) {
ZoneBuffer buffer(zone);
TestSignatures sigs;
WasmModuleBuilder builder(zone);
{
WasmFunctionBuilder* f = builder.AddFunction(sigs.v_v());
uint8_t code[] = {kExprI32Const, n, kExprDrop, kExprEnd};
f->EmitCode(code, arraysize(code));
}
builder.WriteTo(&buffer);
return buffer;
}
} // namespace
TEST(TestAsyncCache) {
CcTest::InitializeVM();
Revert "Reland "[wasm] Cache streaming compilation result"" This reverts commit 9781aa076f333ca27094a4d92beb8194af0051a1. Reason for revert: tsan bot failure: https://ci.chromium.org/p/v8/builders/ci/V8%20Linux64%20TSAN/30110 Original change's description: > Reland "[wasm] Cache streaming compilation result" > > This is a reland of 015f379aa1f90168fe07a1c078ff5aae43a7915a > > Original change's description: > > [wasm] Cache streaming compilation result > > > > Before compiling the code section, check whether the > > bytes received so far match a cached module. If they do, delay > > compilation until we receive the full bytes, since we are likely to find > > a cache entry for them. > > > > R=clemensb@chromium.org > > > > Bug: v8:6847 > > Change-Id: Ie5170d1274da3da6d52ff1b408abc7cb441bbe3c > > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2002823 > > Commit-Queue: Thibaud Michaud <thibaudm@chromium.org> > > Reviewed-by: Clemens Backes <clemensb@chromium.org> > > Cr-Commit-Position: refs/heads/master@{#66000} > > Bug: v8:6847 > Change-Id: I0b5acffa01aeb7dade3dc966392814383d900015 > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2022951 > Commit-Queue: Thibaud Michaud <thibaudm@chromium.org> > Reviewed-by: Clemens Backes <clemensb@chromium.org> > Cr-Commit-Position: refs/heads/master@{#66047} TBR=clemensb@chromium.org,thibaudm@chromium.org Change-Id: I76e3561835815ac3d5bca74e76079e82f9f3d581 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: v8:6847 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2030727 Reviewed-by: Thibaud Michaud <thibaudm@chromium.org> Commit-Queue: Thibaud Michaud <thibaudm@chromium.org> Cr-Commit-Position: refs/heads/master@{#66050}
2020-01-30 14:27:15 +00:00
i::HandleScope internal_scope_(CcTest::i_isolate());
AccountingAllocator allocator;
Zone zone(&allocator, "CompilationCacheTester");
auto bufferA = GetValidModuleBytes(&zone, 0);
auto bufferB = GetValidModuleBytes(&zone, 1);
std::atomic<int> pending(3);
auto resolverA1 = std::make_shared<TestResolver>(&pending);
auto resolverA2 = std::make_shared<TestResolver>(&pending);
auto resolverB = std::make_shared<TestResolver>(&pending);
CcTest::i_isolate()->wasm_engine()->AsyncCompile(
CcTest::i_isolate(), WasmFeatures::All(), resolverA1,
ModuleWireBytes(bufferA.begin(), bufferA.end()), true,
"WebAssembly.compile");
CcTest::i_isolate()->wasm_engine()->AsyncCompile(
CcTest::i_isolate(), WasmFeatures::All(), resolverA2,
ModuleWireBytes(bufferA.begin(), bufferA.end()), true,
"WebAssembly.compile");
CcTest::i_isolate()->wasm_engine()->AsyncCompile(
CcTest::i_isolate(), WasmFeatures::All(), resolverB,
ModuleWireBytes(bufferB.begin(), bufferB.end()), true,
"WebAssembly.compile");
while (pending > 0) {
v8::platform::PumpMessageLoop(i::V8::GetCurrentPlatform(),
CcTest::isolate());
}
CHECK_EQ(resolverA1->native_module(), resolverA2->native_module());
CHECK_NE(resolverA1->native_module(), resolverB->native_module());
}
} // namespace wasm
} // namespace internal
} // namespace v8