v8/test/cctest/compiler/test-concurrent-shared-function-info.cc
Dan Elphick ec06bb6ce5 Reland "[include] Split out v8.h"
This is a reland of d1b27019d3

Fixes include:
Adding missing file to bazel build
Forward-declaring classing before friend-classing them to fix win/gcc
Add missing v8-isolate.h include for vtune builds

Original change's description:
> [include] Split out v8.h
>
> This moves every single class/function out of include/v8.h into a
> separate header in include/, which v8.h then includes so that
> externally nothing appears to have changed.
>
> Every include of v8.h from inside v8 has been changed to a more
> fine-grained include.
>
> Previously inline functions defined at the bottom of v8.h would call
> private non-inline functions in the V8 class. Since that class is now
> in v8-initialization.h and is rarely included (as that would create
> dependency cycles), this is not possible and so those methods have been
> moved out of the V8 class into the namespace v8::api_internal.
>
> None of the previous files in include/ now #include v8.h, which means
> if embedders were relying on this transitive dependency then it will
> give compile failures.
>
> v8-inspector.h does depend on v8-scripts.h for the time being to ensure
> that Chrome continue to compile but that change will be reverted once
> those transitive #includes in chrome are changed to include it directly.
>
> Full design:
> https://docs.google.com/document/d/1rTD--I8hCAr-Rho1WTumZzFKaDpEp0IJ8ejZtk4nJdA/edit?usp=sharing
>
> Bug: v8:11965
> Change-Id: I53b84b29581632710edc80eb11f819c2097a2877
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3097448
> Reviewed-by: Yang Guo <yangguo@chromium.org>
> Reviewed-by: Camillo Bruni <cbruni@chromium.org>
> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
> Reviewed-by: Leszek Swirski <leszeks@chromium.org>
> Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
> Commit-Queue: Dan Elphick <delphick@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#76424}

Cq-Include-Trybots: luci.v8.try:v8_linux_vtunejit
Bug: v8:11965
Change-Id: I99f5d3a73bf8fe25b650adfaf9567dc4e44a09e6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3113629
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Simon Zünd <szuend@chromium.org>
Commit-Queue: Dan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76460}
2021-08-24 13:08:55 +00:00

206 lines
7.0 KiB
C++

// 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 <limits>
#include "include/v8-function.h"
#include "src/api/api-inl.h"
#include "src/codegen/compiler.h"
#include "src/codegen/optimized-compilation-info.h"
#include "src/compiler/pipeline.h"
#include "src/debug/debug.h"
#include "src/handles/handles.h"
#include "src/logging/counters.h"
#include "src/objects/js-function.h"
#include "src/objects/shared-function-info.h"
#include "src/utils/utils-inl.h"
#include "src/zone/zone.h"
#include "test/cctest/cctest.h"
#include "test/common/flag-utils.h"
namespace v8 {
namespace internal {
namespace compiler {
enum class SfiState {
Compiled,
DebugInfo,
PreparedForDebugExecution,
};
void ExpectSharedFunctionInfoState(SharedFunctionInfo sfi,
SfiState expectedState) {
Object function_data = sfi.function_data(kAcquireLoad);
HeapObject script_or_debug_info = sfi.script_or_debug_info(kAcquireLoad);
switch (expectedState) {
case SfiState::Compiled:
CHECK(function_data.IsBytecodeArray() || function_data.IsBaselineData());
CHECK(script_or_debug_info.IsScript());
break;
case SfiState::DebugInfo:
CHECK(function_data.IsBytecodeArray() || function_data.IsBaselineData());
CHECK(script_or_debug_info.IsDebugInfo());
{
DebugInfo debug_info = DebugInfo::cast(script_or_debug_info);
CHECK(!debug_info.HasInstrumentedBytecodeArray());
}
break;
case SfiState::PreparedForDebugExecution:
CHECK(function_data.IsBytecodeArray());
CHECK(script_or_debug_info.IsDebugInfo());
{
DebugInfo debug_info = DebugInfo::cast(script_or_debug_info);
CHECK(debug_info.HasInstrumentedBytecodeArray());
}
break;
}
}
class BackgroundCompilationThread final : public v8::base::Thread {
public:
BackgroundCompilationThread(Isolate* isolate,
base::Semaphore* sema_execute_start,
base::Semaphore* sema_execute_complete,
OptimizedCompilationJob* job)
: base::Thread(base::Thread::Options("BackgroundCompilationThread")),
isolate_(isolate),
sema_execute_start_(sema_execute_start),
sema_execute_complete_(sema_execute_complete),
job_(job) {}
void Run() override {
RuntimeCallStats stats(RuntimeCallStats::kWorkerThread);
LocalIsolate local_isolate(isolate_, ThreadKind::kBackground);
sema_execute_start_->Wait();
const CompilationJob::Status status =
job_->ExecuteJob(&stats, &local_isolate);
CHECK_EQ(status, CompilationJob::SUCCEEDED);
sema_execute_complete_->Signal();
}
private:
Isolate* isolate_;
base::Semaphore* sema_execute_start_;
base::Semaphore* sema_execute_complete_;
OptimizedCompilationJob* job_;
};
TEST(TestConcurrentSharedFunctionInfo) {
FlagScope<bool> allow_natives_syntax(&i::FLAG_allow_natives_syntax, true);
FlagScope<bool> concurrent_inlining(&i::FLAG_concurrent_inlining, true);
HandleAndZoneScope scope;
Isolate* isolate = scope.main_isolate();
Zone zone(isolate->allocator(), ZONE_NAME);
HandleScope handle_scope(isolate);
const char* source_code =
"function f(x, y) { return x + y; }\n"
"function test(x) { return f(f(1, x), f(x, 1)); }\n"
"%PrepareFunctionForOptimization(f);\n"
"%PrepareFunctionForOptimization(test);\n"
"test(3);\n"
"test(-9);\n";
CompileRun(source_code);
// Get function "test"
Local<Function> function_test = Local<Function>::Cast(
CcTest::global()
->Get(CcTest::isolate()->GetCurrentContext(), v8_str("test"))
.ToLocalChecked());
Handle<JSFunction> test =
Handle<JSFunction>::cast(v8::Utils::OpenHandle(*function_test));
Handle<SharedFunctionInfo> test_sfi(test->shared(), isolate);
DCHECK(test_sfi->HasBytecodeArray());
IsCompiledScope compiled_scope_test(*test_sfi, isolate);
JSFunction::EnsureFeedbackVector(test, &compiled_scope_test);
// Get function "f"
Local<Function> function_f = Local<Function>::Cast(
CcTest::global()
->Get(CcTest::isolate()->GetCurrentContext(), v8_str("f"))
.ToLocalChecked());
Handle<JSFunction> f =
Handle<JSFunction>::cast(v8::Utils::OpenHandle(*function_f));
Handle<SharedFunctionInfo> f_sfi(f->shared(), isolate);
DCHECK(f_sfi->HasBytecodeArray());
OptimizedCompilationInfo f_info(&zone, isolate, f_sfi, f, CodeKind::TURBOFAN);
Handle<Code> f_code =
Pipeline::GenerateCodeForTesting(&f_info, isolate).ToHandleChecked();
f->set_code(*f_code, kReleaseStore);
IsCompiledScope compiled_scope_f(*f_sfi, isolate);
JSFunction::EnsureFeedbackVector(f, &compiled_scope_f);
ExpectSharedFunctionInfoState(*test_sfi, SfiState::Compiled);
auto job =
Pipeline::NewCompilationJob(isolate, test, CodeKind::TURBOFAN, true);
// Prepare job.
{
CompilationHandleScope compilation(isolate, job->compilation_info());
CanonicalHandleScope canonical(isolate, job->compilation_info());
job->compilation_info()->ReopenHandlesInNewHandleScope(isolate);
const CompilationJob::Status status = job->PrepareJob(isolate);
CHECK_EQ(status, CompilationJob::SUCCEEDED);
}
// Start a background thread to execute the compilation job.
base::Semaphore sema_execute_start(0);
base::Semaphore sema_execute_complete(0);
BackgroundCompilationThread thread(isolate, &sema_execute_start,
&sema_execute_complete, job.get());
CHECK(thread.Start());
sema_execute_start.Signal();
// Background thread is running, now mess with test's SFI.
ExpectSharedFunctionInfoState(*test_sfi, SfiState::Compiled);
// Compiled ==> DebugInfo
{
isolate->debug()->GetOrCreateDebugInfo(test_sfi);
ExpectSharedFunctionInfoState(*test_sfi, SfiState::DebugInfo);
}
for (int i = 0; i < 100; ++i) {
// DebugInfo ==> PreparedForDebugExecution
{
int breakpoint_id;
CHECK(isolate->debug()->SetBreakpointForFunction(
test_sfi, isolate->factory()->empty_string(), &breakpoint_id));
ExpectSharedFunctionInfoState(*test_sfi,
SfiState::PreparedForDebugExecution);
}
// PreparedForDebugExecution ==> DebugInfo
{
DebugInfo debug_info = test_sfi->GetDebugInfo();
debug_info.ClearBreakInfo(isolate);
ExpectSharedFunctionInfoState(*test_sfi, SfiState::DebugInfo);
}
}
sema_execute_complete.Wait();
thread.Join();
// Finalize job.
{
// Cannot assert successful completion here since concurrent modifications
// may have invalidated compilation dependencies (e.g. since the serialized
// JSFunctionRef no longer matches the actual JSFunction state).
const CompilationJob::Status status = job->FinalizeJob(isolate);
if (status == CompilationJob::SUCCEEDED) {
CHECK(job->compilation_info()->has_bytecode_array());
} else {
CHECK_EQ(status, CompilationJob::FAILED);
}
}
}
} // namespace compiler
} // namespace internal
} // namespace v8