blob: a63e14a98043b727be766eda3cfab4572a954bb2 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "jit_compiler.h"
18
19#include "arch/instruction_set.h"
20#include "arch/instruction_set_features.h"
21#include "compiler_callbacks.h"
22#include "dex/pass_manager.h"
23#include "dex/quick_compiler_callbacks.h"
24#include "driver/compiler_driver.h"
25#include "driver/compiler_options.h"
26#include "jit/jit.h"
27#include "jit/jit_code_cache.h"
28#include "mirror/art_method-inl.h"
29#include "oat_file-inl.h"
30#include "object_lock.h"
31#include "thread_list.h"
32#include "verifier/method_verifier-inl.h"
33
34namespace art {
35namespace jit {
36
37JitCompiler* JitCompiler::Create() {
38 return new JitCompiler();
39}
40
41extern "C" void* jit_load(CompilerCallbacks** callbacks) {
42 VLOG(jit) << "loading jit compiler";
43 auto* const jit_compiler = JitCompiler::Create();
44 CHECK(jit_compiler != nullptr);
45 *callbacks = jit_compiler->GetCompilerCallbacks();
46 VLOG(jit) << "Done loading jit compiler";
47 return jit_compiler;
48}
49
50extern "C" void jit_unload(void* handle) {
51 DCHECK(handle != nullptr);
52 delete reinterpret_cast<JitCompiler*>(handle);
53}
54
55extern "C" bool jit_compile_method(void* handle, mirror::ArtMethod* method, Thread* self)
56 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
57 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
58 DCHECK(jit_compiler != nullptr);
59 return jit_compiler->CompileMethod(self, method);
60}
61
62JitCompiler::JitCompiler() : total_time_(0) {
63 auto* pass_manager_options = new PassManagerOptions;
64 pass_manager_options->SetDisablePassList("GVN,DCE");
65 compiler_options_.reset(new CompilerOptions(
66 CompilerOptions::kDefaultCompilerFilter,
67 CompilerOptions::kDefaultHugeMethodThreshold,
68 CompilerOptions::kDefaultLargeMethodThreshold,
69 CompilerOptions::kDefaultSmallMethodThreshold,
70 CompilerOptions::kDefaultTinyMethodThreshold,
71 CompilerOptions::kDefaultNumDexMethodsThreshold,
72 false,
73 false,
74 CompilerOptions::kDefaultTopKProfileThreshold,
Andreas Gampe7b2f09e2015-03-02 14:07:33 -080075 false, // TODO: Think about debuggability of JIT-compiled code.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 false,
77 false,
78 false,
79 false,
Mathieu Chartierdce71f32015-02-27 14:24:37 -080080 false, // pic
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080081 nullptr,
82 pass_manager_options,
Andreas Gampe6cf49e52015-03-05 13:08:45 -080083 nullptr,
84 false));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085 const InstructionSet instruction_set = kRuntimeISA;
86 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
87 cumulative_logger_.reset(new CumulativeLogger("jit times"));
88 verification_results_.reset(new VerificationResults(compiler_options_.get()));
89 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
90 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
91 method_inliner_map_.get()));
92 compiler_driver_.reset(new CompilerDriver(
93 compiler_options_.get(), verification_results_.get(), method_inliner_map_.get(),
94 Compiler::kQuick, instruction_set, instruction_set_features_.get(), false,
95 nullptr, new std::set<std::string>, 1, false, true,
96 std::string(), cumulative_logger_.get(), -1, std::string()));
97 // Disable dedupe so we can remove compiled methods.
98 compiler_driver_->SetDedupeEnabled(false);
99 compiler_driver_->SetSupportBootImageFixup(false);
100}
101
102JitCompiler::~JitCompiler() {
103}
104
105bool JitCompiler::CompileMethod(Thread* self, mirror::ArtMethod* method) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700106 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107 StackHandleScope<2> hs(self);
108 self->AssertNoPendingException();
109 Runtime* runtime = Runtime::Current();
110 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
111 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
112 VLOG(jit) << "Already compiled " << PrettyMethod(method);
113 return true; // Already compiled
114 }
115 Handle<mirror::Class> h_class(hs.NewHandle(h_method->GetDeclaringClass()));
116 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
117 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(h_method.Get());
118 return false;
119 }
120 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
121 MethodReference method_ref(dex_file, h_method->GetDexMethodIndex());
122 // Only verify if we don't already have verification results.
123 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
124 std::string error;
125 if (verifier::MethodVerifier::VerifyMethod(h_method.Get(), true, &error) ==
126 verifier::MethodVerifier::kHardFailure) {
127 VLOG(jit) << "Not compile method " << PrettyMethod(h_method.Get())
128 << " due to verification failure " << error;
129 return false;
130 }
131 }
132 CompiledMethod* compiled_method(compiler_driver_->CompileMethod(self, h_method.Get()));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700133 // Trim maps to reduce memory usage, TODO: measure how much this increases compile time.
134 runtime->GetArenaPool()->TrimMaps();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800135 if (compiled_method == nullptr) {
136 return false;
137 }
138 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800139 // Don't add the method if we are supposed to be deoptimized.
140 bool result = false;
141 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700142 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800143 h_method.Get());
144 if (code != nullptr) {
145 // Already have some compiled code, just use this instead of linking.
146 // TODO: Fix recompilation.
147 h_method->SetEntryPointFromQuickCompiledCode(code);
148 result = true;
149 } else {
150 result = MakeExecutable(compiled_method, h_method.Get());
151 }
152 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153 // Remove the compiled method to save memory.
154 compiler_driver_->RemoveCompiledMethod(method_ref);
155 return result;
156}
157
158CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
159 return callbacks_.get();
160}
161
162uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
163 uint8_t* reserve_begin, uint8_t* reserve_end,
164 const uint8_t* mapping_table,
165 const uint8_t* vmap_table,
166 const uint8_t* gc_map) {
167 reserve_begin += sizeof(OatQuickMethodHeader);
168 reserve_begin = reinterpret_cast<uint8_t*>(
169 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
170 const auto* quick_code = compiled_method->GetQuickCode();
171 CHECK_LE(reserve_begin, reserve_end);
172 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
173 auto* code_ptr = reserve_begin;
174 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
175 // Construct the header last.
176 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
177 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
178 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
179 const auto code_size = quick_code->size();
180 CHECK_NE(code_size, 0U);
181 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
182 // After we are done writing we need to update the method header.
183 // Write out the method header last.
184 method_header = new(method_header)OatQuickMethodHeader(
185 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
186 core_spill_mask, fp_spill_mask, code_size);
187 // Return the code ptr.
188 return code_ptr;
189}
190
191bool JitCompiler::AddToCodeCache(mirror::ArtMethod* method, const CompiledMethod* compiled_method,
192 OatFile::OatMethod* out_method) {
193 Runtime* runtime = Runtime::Current();
194 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
195 const auto* quick_code = compiled_method->GetQuickCode();
196 if (quick_code == nullptr) {
197 return false;
198 }
199 const auto code_size = quick_code->size();
200 Thread* const self = Thread::Current();
201 const uint8_t* base = code_cache->CodeCachePtr();
202 auto* const mapping_table = compiled_method->GetMappingTable();
203 auto* const vmap_table = compiled_method->GetVmapTable();
204 auto* const gc_map = compiled_method->GetGcMap();
205 // Write out pre-header stuff.
206 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
207 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
208 if (mapping_table == nullptr) {
209 return false; // Out of data cache.
210 }
211 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
212 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
213 if (vmap_table == nullptr) {
214 return false; // Out of data cache.
215 }
216 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
217 self, gc_map->data(), gc_map->data() + gc_map->size());
218 if (gc_map == nullptr) {
219 return false; // Out of data cache.
220 }
221 // Don't touch this until you protect / unprotect the code.
222 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
223 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
224 if (code_reserve == nullptr) {
225 return false;
226 }
227 auto* code_ptr = WriteMethodHeaderAndCode(
228 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
229 vmap_table_ptr, gc_map_ptr);
230
231 const size_t thumb_offset = compiled_method->CodeDelta();
232 const uint32_t code_offset = code_ptr - base + thumb_offset;
233 *out_method = OatFile::OatMethod(base, code_offset);
234 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
235 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
236 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
237 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
238 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
239 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
240 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
241 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
242 << "," << reinterpret_cast<void*>(code_ptr + code_size);
243 return true;
244}
245
246bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, mirror::ArtMethod* method) {
247 CHECK(method != nullptr);
248 CHECK(compiled_method != nullptr);
249 OatFile::OatMethod oat_method(nullptr, 0);
250 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
251 return false;
252 }
253 // TODO: Flush instruction cache.
254 oat_method.LinkMethod(method);
255 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
256 << PrettyMethod(method);
257 return true;
258}
259
260} // namespace jit
261} // namespace art