v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
maglev-concurrent-dispatcher.h
Go to the documentation of this file.
1// Copyright 2022 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#ifndef V8_MAGLEV_MAGLEV_CONCURRENT_DISPATCHER_H_
6#define V8_MAGLEV_MAGLEV_CONCURRENT_DISPATCHER_H_
7
8#ifdef V8_ENABLE_MAGLEV
9
10#include <memory>
11
12#include "src/codegen/compiler.h" // For OptimizedCompilationJob.
15
16namespace v8 {
17namespace internal {
18
19class Isolate;
20
21namespace maglev {
22
23class MaglevCompilationInfo;
24
25// TODO(v8:7700): While basic infrastructure now exists, there are many TODOs
26// that should still be addressed soon:
27// - Full tracing support through --trace-opt.
28// - Concurrent codegen.
29// - Concurrent InstructionStream object creation (optional?).
30// - Test support for concurrency (see %FinalizeOptimization).
31
32// Exports needed functionality without exposing implementation details.
33class ExportedMaglevCompilationInfo final {
34 public:
35 explicit ExportedMaglevCompilationInfo(MaglevCompilationInfo* info)
36 : info_(info) {}
37
38 Zone* zone() const;
39 void set_canonical_handles(
40 std::unique_ptr<CanonicalHandlesMap>&& canonical_handles);
41
42 private:
43 MaglevCompilationInfo* const info_;
44};
45
46// The job is a single actual compilation task.
47class MaglevCompilationJob final : public OptimizedCompilationJob {
48 public:
49 static std::unique_ptr<MaglevCompilationJob> New(Isolate* isolate,
50 Handle<JSFunction> function,
51 BytecodeOffset osr_offset);
52 ~MaglevCompilationJob() override;
53
54 Status PrepareJobImpl(Isolate* isolate) override;
55 Status ExecuteJobImpl(RuntimeCallStats* stats,
56 LocalIsolate* local_isolate) override;
57 Status FinalizeJobImpl(Isolate* isolate) override;
58
59 IndirectHandle<JSFunction> function() const;
60 MaybeIndirectHandle<Code> code() const;
61 BytecodeOffset osr_offset() const;
62 bool is_osr() const;
63
64 bool specialize_to_function_context() const;
65
66 base::TimeDelta time_taken_to_prepare() { return time_taken_to_prepare_; }
67 base::TimeDelta time_taken_to_execute() { return time_taken_to_execute_; }
68 base::TimeDelta time_taken_to_finalize() { return time_taken_to_finalize_; }
69
70 void RecordCompilationStats(Isolate* isolate) const;
71
72 void DisposeOnMainThread(Isolate* isolate);
73
74 // Intended for use as a globally unique id in trace events.
75 uint64_t trace_id() const;
76
77 BailoutReason bailout_reason_ = BailoutReason::kNoReason;
78
79 private:
80 explicit MaglevCompilationJob(Isolate* isolate,
81 std::unique_ptr<MaglevCompilationInfo>&& info);
82 void BeginPhaseKind(const char* name);
83 void EndPhaseKind();
84 GlobalHandleVector<Map> CollectRetainedMaps(Isolate* isolate,
85 DirectHandle<Code> code);
86
87 MaglevCompilationInfo* info() const { return info_.get(); }
88
89 const std::unique_ptr<MaglevCompilationInfo> info_;
90 // TODO(pthier): Gather more fine grained stats for maglev compilation.
91 // Currently only totals are collected.
92 compiler::ZoneStats zone_stats_;
93 std::unique_ptr<MaglevPipelineStatistics> pipeline_statistics_;
94};
95
96// The public API for Maglev concurrent compilation.
97// Keep this as minimal as possible.
98class V8_EXPORT_PRIVATE MaglevConcurrentDispatcher final {
99 class JobTask;
100
101 // TODO(jgruber): There's no reason to use locking queues here, we only use
102 // them for simplicity - consider replacing with lock-free data structures.
103 using QueueT = LockedQueue<std::unique_ptr<MaglevCompilationJob>>;
104
105 public:
106 explicit MaglevConcurrentDispatcher(Isolate* isolate);
107 ~MaglevConcurrentDispatcher();
108
109 // Called from the main thread.
110 void EnqueueJob(std::unique_ptr<MaglevCompilationJob>&& job);
111
112 // Called from the main thread.
113 void FinalizeFinishedJobs();
114
115 void AwaitCompileJobs();
116
117 void Flush(BlockingBehavior blocking_behavior);
118
119 bool is_enabled() const { return static_cast<bool>(job_handle_); }
120
121 private:
122 Isolate* const isolate_;
123 std::unique_ptr<JobHandle> job_handle_;
124 QueueT incoming_queue_;
125 QueueT outgoing_queue_;
126 QueueT destruction_queue_;
127};
128
129} // namespace maglev
130} // namespace internal
131} // namespace v8
132
133#endif // V8_ENABLE_MAGLEV
134
135#endif // V8_MAGLEV_MAGLEV_CONCURRENT_DISPATCHER_H_
Isolate * isolate_
friend Zone
Definition asm-types.cc:195
Handle< Code > code
Handle< SharedFunctionInfo > info
LiftoffBailoutReason bailout_reason_
v8::JobTask JobTask
Definition platform.h:21
OptimizedCompilationInfo * info_
Definition pipeline.cc:305
#define V8_EXPORT_PRIVATE
Definition macros.h:460