1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_COMPILER_JIT_COMPILER_H_
6#define RUNTIME_VM_COMPILER_JIT_COMPILER_H_
7
8#include "vm/allocation.h"
9#include "vm/compiler/api/deopt_id.h"
10#include "vm/growable_array.h"
11#include "vm/runtime_entry.h"
12#include "vm/thread_pool.h"
13
14namespace dart {
15
16// Forward declarations.
17class BackgroundCompilationQueue;
18class Class;
19class Code;
20class CompilationWorkQueue;
21class FlowGraph;
22class Function;
23class IndirectGotoInstr;
24class Library;
25class ParsedFunction;
26class QueueElement;
27class Script;
28class SequenceNode;
29
30class CompilationPipeline : public ZoneAllocated {
31 public:
32 static CompilationPipeline* New(Zone* zone, const Function& function);
33
34 virtual void ParseFunction(ParsedFunction* parsed_function) = 0;
35 virtual FlowGraph* BuildFlowGraph(
36 Zone* zone,
37 ParsedFunction* parsed_function,
38 ZoneGrowableArray<const ICData*>* ic_data_array,
39 intptr_t osr_id,
40 bool optimized) = 0;
41 virtual ~CompilationPipeline() {}
42};
43
44class DartCompilationPipeline : public CompilationPipeline {
45 public:
46 void ParseFunction(ParsedFunction* parsed_function) override;
47
48 FlowGraph* BuildFlowGraph(Zone* zone,
49 ParsedFunction* parsed_function,
50 ZoneGrowableArray<const ICData*>* ic_data_array,
51 intptr_t osr_id,
52 bool optimized) override;
53};
54
55class IrregexpCompilationPipeline : public CompilationPipeline {
56 public:
57 IrregexpCompilationPipeline() : backtrack_goto_(nullptr) {}
58
59 void ParseFunction(ParsedFunction* parsed_function) override;
60
61 FlowGraph* BuildFlowGraph(Zone* zone,
62 ParsedFunction* parsed_function,
63 ZoneGrowableArray<const ICData*>* ic_data_array,
64 intptr_t osr_id,
65 bool optimized) override;
66
67 private:
68 IndirectGotoInstr* backtrack_goto_;
69};
70
71class Compiler : public AllStatic {
72 public:
73 static constexpr intptr_t kNoOSRDeoptId = DeoptId::kNone;
74
75 static bool IsBackgroundCompilation();
76 // The result for a function may change if debugging gets turned on/off.
77 static bool CanOptimizeFunction(Thread* thread, const Function& function);
78
79 // Generates code for given function without optimization and sets its code
80 // field.
81 //
82 // Returns the raw code object if compilation succeeds. Otherwise returns an
83 // ErrorPtr. Also installs the generated code on the function.
84 static ObjectPtr CompileFunction(Thread* thread, const Function& function);
85
86 // Generates unoptimized code if not present, current code is unchanged.
87 static ErrorPtr EnsureUnoptimizedCode(Thread* thread,
88 const Function& function);
89
90 // Generates optimized code for function.
91 //
92 // Returns the code object if compilation succeeds. Returns an Error if
93 // there is a compilation error. If optimization fails, but there is no
94 // error, returns null. Any generated code is installed unless we are in
95 // OSR mode.
96 static ObjectPtr CompileOptimizedFunction(Thread* thread,
97 const Function& function,
98 intptr_t osr_id = kNoOSRDeoptId);
99
100 // Generates local var descriptors and sets it in 'code'. Do not call if the
101 // local var descriptor already exists.
102 static void ComputeLocalVarDescriptors(const Code& code);
103
104 // Eagerly compiles all functions in a class.
105 //
106 // Returns Error::null() if there is no compilation error.
107 static ErrorPtr CompileAllFunctions(const Class& cls);
108
109 // Notify the compiler that background (optimized) compilation has failed
110 // because the mutator thread changed the state (e.g., deoptimization,
111 // deferred loading). The background compilation may retry to compile
112 // the same function later.
113 static void AbortBackgroundCompilation(intptr_t deopt_id, const char* msg);
114};
115
116// Class to run optimizing compilation in a background thread.
117// Current implementation: one task per isolate, it dies with the owning
118// isolate.
119// No OSR compilation in the background compiler.
120class BackgroundCompiler {
121 public:
122 explicit BackgroundCompiler(IsolateGroup* isolate_group);
123 virtual ~BackgroundCompiler();
124
125 static void Stop(IsolateGroup* isolate_group) {
126 isolate_group->background_compiler()->Stop();
127 }
128
129 // Enqueues a function to be compiled in the background.
130 //
131 // Return `true` if successful.
132 bool EnqueueCompilation(const Function& function);
133
134 void VisitPointers(ObjectPointerVisitor* visitor);
135
136 BackgroundCompilationQueue* function_queue() const { return function_queue_; }
137 bool is_running() const { return running_; }
138
139 void Run();
140
141 private:
142 friend class NoBackgroundCompilerScope;
143
144 void Stop();
145 void StopLocked(Thread* thread, SafepointMonitorLocker* done_locker);
146 void Enable();
147 void Disable();
148 bool IsRunning() { return !done_; }
149
150 IsolateGroup* isolate_group_;
151
152 Monitor monitor_; // Controls access to the queue and running state.
153 BackgroundCompilationQueue* function_queue_;
154 bool running_; // While true, will try to read queue and compile.
155 bool done_; // True if the thread is done.
156 int16_t disabled_depth_;
157
158 DISALLOW_IMPLICIT_CONSTRUCTORS(BackgroundCompiler);
159};
160
161class NoBackgroundCompilerScope : public StackResource {
162 public:
163 explicit NoBackgroundCompilerScope(Thread* thread)
164 : StackResource(thread), isolate_group_(thread->isolate_group()) {
165#if defined(DART_PRECOMPILED_RUNTIME)
166 UNREACHABLE();
167#else
168 isolate_group_->background_compiler()->Disable();
169#endif
170 }
171 ~NoBackgroundCompilerScope() {
172#if defined(DART_PRECOMPILED_RUNTIME)
173 UNREACHABLE();
174#else
175 isolate_group_->background_compiler()->Enable();
176#endif
177 }
178
179 private:
180 IsolateGroup* isolate_group_;
181};
182
183} // namespace dart
184
185#endif // RUNTIME_VM_COMPILER_JIT_COMPILER_H_
186

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com

source code of flutter_engine/third_party/dart/runtime/vm/compiler/jit/compiler.h