11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_BASELINE_BASELINE_BATCH_COMPILER_H_
61cb0ef41Sopenharmony_ci#define V8_BASELINE_BASELINE_BATCH_COMPILER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <atomic>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "src/handles/global-handles.h"
111cb0ef41Sopenharmony_ci#include "src/handles/handles.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_cinamespace baseline {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciclass BaselineCompiler;
181cb0ef41Sopenharmony_ciclass ConcurrentBaselineCompiler;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciclass BaselineBatchCompiler {
211cb0ef41Sopenharmony_ci public:
221cb0ef41Sopenharmony_ci  static const int kInitialQueueSize = 32;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  explicit BaselineBatchCompiler(Isolate* isolate);
251cb0ef41Sopenharmony_ci  ~BaselineBatchCompiler();
261cb0ef41Sopenharmony_ci  // Enqueues SharedFunctionInfo of |function| for compilation.
271cb0ef41Sopenharmony_ci  void EnqueueFunction(Handle<JSFunction> function);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  void set_enabled(bool enabled) { enabled_ = enabled; }
301cb0ef41Sopenharmony_ci  bool is_enabled() { return enabled_; }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  void InstallBatch();
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci private:
351cb0ef41Sopenharmony_ci  // Ensure there is enough space in the compilation queue to enqueue another
361cb0ef41Sopenharmony_ci  // function, growing the queue if necessary.
371cb0ef41Sopenharmony_ci  void EnsureQueueCapacity();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // Enqueues SharedFunctionInfo.
401cb0ef41Sopenharmony_ci  void Enqueue(Handle<SharedFunctionInfo> shared);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  // Returns true if the current batch exceeds the threshold and should be
431cb0ef41Sopenharmony_ci  // compiled.
441cb0ef41Sopenharmony_ci  bool ShouldCompileBatch() const;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  // Compiles the current batch.
471cb0ef41Sopenharmony_ci  void CompileBatch(Handle<JSFunction> function);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // Resets the current batch.
501cb0ef41Sopenharmony_ci  void ClearBatch();
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  // Tries to compile |maybe_sfi|. Returns false if compilation was not possible
531cb0ef41Sopenharmony_ci  // (e.g. bytecode was fushed, weak handle no longer valid, ...).
541cb0ef41Sopenharmony_ci  bool MaybeCompileFunction(MaybeObject maybe_sfi);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  Isolate* isolate_;
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  // Global handle to shared function infos enqueued for compilation in the
591cb0ef41Sopenharmony_ci  // current batch.
601cb0ef41Sopenharmony_ci  Handle<WeakFixedArray> compilation_queue_;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  // Last index set in compilation_queue_;
631cb0ef41Sopenharmony_ci  int last_index_;
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  // Estimated insturction size of current batch.
661cb0ef41Sopenharmony_ci  int estimated_instruction_size_;
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  // Flag indicating whether batch compilation is enabled.
691cb0ef41Sopenharmony_ci  // Batch compilation can be dynamically disabled e.g. when creating snapshots.
701cb0ef41Sopenharmony_ci  bool enabled_;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  // Handle to the background compilation jobs.
731cb0ef41Sopenharmony_ci  std::unique_ptr<ConcurrentBaselineCompiler> concurrent_compiler_;
741cb0ef41Sopenharmony_ci};
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci}  // namespace baseline
771cb0ef41Sopenharmony_ci}  // namespace internal
781cb0ef41Sopenharmony_ci}  // namespace v8
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci#endif  // V8_BASELINE_BASELINE_BATCH_COMPILER_H_
81