1 // Copyright 2021 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_BASELINE_BASELINE_BATCH_COMPILER_H_
6 #define V8_BASELINE_BASELINE_BATCH_COMPILER_H_
7 
8 #include <atomic>
9 
10 #include "src/handles/global-handles.h"
11 #include "src/handles/handles.h"
12 
13 namespace v8 {
14 namespace internal {
15 namespace baseline {
16 
17 class BaselineCompiler;
18 class ConcurrentBaselineCompiler;
19 
20 class BaselineBatchCompiler {
21  public:
22   static const int kInitialQueueSize = 32;
23 
24   explicit BaselineBatchCompiler(Isolate* isolate);
25   ~BaselineBatchCompiler();
26   // Enqueues SharedFunctionInfo of |function| for compilation.
27   void EnqueueFunction(Handle<JSFunction> function);
28 
set_enabled(bool enabled)29   void set_enabled(bool enabled) { enabled_ = enabled; }
is_enabled()30   bool is_enabled() { return enabled_; }
31 
32   void InstallBatch();
33 
34  private:
35   // Ensure there is enough space in the compilation queue to enqueue another
36   // function, growing the queue if necessary.
37   void EnsureQueueCapacity();
38 
39   // Enqueues SharedFunctionInfo.
40   void Enqueue(Handle<SharedFunctionInfo> shared);
41 
42   // Returns true if the current batch exceeds the threshold and should be
43   // compiled.
44   bool ShouldCompileBatch() const;
45 
46   // Compiles the current batch.
47   void CompileBatch(Handle<JSFunction> function);
48 
49   // Resets the current batch.
50   void ClearBatch();
51 
52   // Tries to compile |maybe_sfi|. Returns false if compilation was not possible
53   // (e.g. bytecode was fushed, weak handle no longer valid, ...).
54   bool MaybeCompileFunction(MaybeObject maybe_sfi);
55 
56   Isolate* isolate_;
57 
58   // Global handle to shared function infos enqueued for compilation in the
59   // current batch.
60   Handle<WeakFixedArray> compilation_queue_;
61 
62   // Last index set in compilation_queue_;
63   int last_index_;
64 
65   // Estimated insturction size of current batch.
66   int estimated_instruction_size_;
67 
68   // Flag indicating whether batch compilation is enabled.
69   // Batch compilation can be dynamically disabled e.g. when creating snapshots.
70   bool enabled_;
71 
72   // Handle to the background compilation jobs.
73   std::unique_ptr<ConcurrentBaselineCompiler> concurrent_compiler_;
74 };
75 
76 }  // namespace baseline
77 }  // namespace internal
78 }  // namespace v8
79 
80 #endif  // V8_BASELINE_BASELINE_BATCH_COMPILER_H_
81