Flutter Engine
The Flutter Engine
compiler.h
Go to the documentation of this file.
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"
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
31 public:
32 static CompilationPipeline* New(Zone* zone, const Function& function);
33
34 virtual void ParseFunction(ParsedFunction* parsed_function) = 0;
36 Zone* zone,
37 ParsedFunction* parsed_function,
39 intptr_t osr_id,
40 bool optimized) = 0;
42};
43
45 public:
46 void ParseFunction(ParsedFunction* parsed_function) override;
47
49 ParsedFunction* parsed_function,
51 intptr_t osr_id,
52 bool optimized) override;
53};
54
56 public:
57 IrregexpCompilationPipeline() : backtrack_goto_(nullptr) {}
58
59 void ParseFunction(ParsedFunction* parsed_function) override;
60
62 ParsedFunction* parsed_function,
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.
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.
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.
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:
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
159};
160
162 public:
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 }
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_
#define UNREACHABLE()
Definition: assert.h:248
void VisitPointers(ObjectPointerVisitor *visitor)
Definition: compiler.cc:1210
static void Stop(IsolateGroup *isolate_group)
Definition: compiler.h:125
BackgroundCompiler(IsolateGroup *isolate_group)
Definition: compiler.cc:1112
bool EnqueueCompilation(const Function &function)
Definition: compiler.cc:1179
BackgroundCompilationQueue * function_queue() const
Definition: compiler.h:136
bool is_running() const
Definition: compiler.h:137
virtual ~BackgroundCompiler()
Definition: compiler.cc:1121
virtual FlowGraph * BuildFlowGraph(Zone *zone, ParsedFunction *parsed_function, ZoneGrowableArray< const ICData * > *ic_data_array, intptr_t osr_id, bool optimized)=0
virtual void ParseFunction(ParsedFunction *parsed_function)=0
virtual ~CompilationPipeline()
Definition: compiler.h:41
static CompilationPipeline * New(Zone *zone, const Function &function)
Definition: compiler.cc:201
static bool IsBackgroundCompilation()
Definition: compiler.cc:298
static bool CanOptimizeFunction(Thread *thread, const Function &function)
Definition: compiler.cc:229
static constexpr intptr_t kNoOSRDeoptId
Definition: compiler.h:73
static ErrorPtr EnsureUnoptimizedCode(Thread *thread, const Function &function)
Definition: compiler.cc:854
static ObjectPtr CompileFunction(Thread *thread, const Function &function)
Definition: compiler.cc:824
static ObjectPtr CompileOptimizedFunction(Thread *thread, const Function &function, intptr_t osr_id=kNoOSRDeoptId)
Definition: compiler.cc:886
static ErrorPtr CompileAllFunctions(const Class &cls)
Definition: compiler.cc:948
static void ComputeLocalVarDescriptors(const Code &code)
Definition: compiler.cc:909
static void AbortBackgroundCompilation(intptr_t deopt_id, const char *msg)
Definition: compiler.cc:971
void ParseFunction(ParsedFunction *parsed_function) override
Definition: compiler.cc:124
FlowGraph * BuildFlowGraph(Zone *zone, ParsedFunction *parsed_function, ZoneGrowableArray< const ICData * > *ic_data_array, intptr_t osr_id, bool optimized) override
Definition: compiler.cc:128
static constexpr intptr_t kNone
Definition: deopt_id.h:27
void ParseFunction(ParsedFunction *parsed_function) override
Definition: compiler.cc:143
FlowGraph * BuildFlowGraph(Zone *zone, ParsedFunction *parsed_function, ZoneGrowableArray< const ICData * > *ic_data_array, intptr_t osr_id, bool optimized) override
Definition: compiler.cc:169
BackgroundCompiler * background_compiler() const
Definition: isolate.h:298
NoBackgroundCompilerScope(Thread *thread)
Definition: compiler.h:163
ThreadState * thread() const
Definition: allocation.h:33
Dart_NativeFunction function
Definition: fuchsia.cc:51
Definition: dart_vm.cc:33
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
Definition: globals.h:593