blob: 8b311542f6e917aff5092a013f72b58a2d06c4b5 [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"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070021#include "base/timing_logger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080022#include "compiler_callbacks.h"
23#include "dex/pass_manager.h"
24#include "dex/quick_compiler_callbacks.h"
25#include "driver/compiler_driver.h"
26#include "driver/compiler_options.h"
27#include "jit/jit.h"
28#include "jit/jit_code_cache.h"
29#include "mirror/art_method-inl.h"
30#include "oat_file-inl.h"
31#include "object_lock.h"
32#include "thread_list.h"
33#include "verifier/method_verifier-inl.h"
34
35namespace art {
36namespace jit {
37
38JitCompiler* JitCompiler::Create() {
39 return new JitCompiler();
40}
41
42extern "C" void* jit_load(CompilerCallbacks** callbacks) {
43 VLOG(jit) << "loading jit compiler";
44 auto* const jit_compiler = JitCompiler::Create();
45 CHECK(jit_compiler != nullptr);
46 *callbacks = jit_compiler->GetCompilerCallbacks();
47 VLOG(jit) << "Done loading jit compiler";
48 return jit_compiler;
49}
50
51extern "C" void jit_unload(void* handle) {
52 DCHECK(handle != nullptr);
53 delete reinterpret_cast<JitCompiler*>(handle);
54}
55
56extern "C" bool jit_compile_method(void* handle, mirror::ArtMethod* method, Thread* self)
57 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
58 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
59 DCHECK(jit_compiler != nullptr);
60 return jit_compiler->CompileMethod(self, method);
61}
62
63JitCompiler::JitCompiler() : total_time_(0) {
64 auto* pass_manager_options = new PassManagerOptions;
65 pass_manager_options->SetDisablePassList("GVN,DCE");
66 compiler_options_.reset(new CompilerOptions(
67 CompilerOptions::kDefaultCompilerFilter,
68 CompilerOptions::kDefaultHugeMethodThreshold,
69 CompilerOptions::kDefaultLargeMethodThreshold,
70 CompilerOptions::kDefaultSmallMethodThreshold,
71 CompilerOptions::kDefaultTinyMethodThreshold,
72 CompilerOptions::kDefaultNumDexMethodsThreshold,
73 false,
74 false,
75 CompilerOptions::kDefaultTopKProfileThreshold,
Andreas Gampe7b2f09e2015-03-02 14:07:33 -080076 false, // TODO: Think about debuggability of JIT-compiled code.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077 false,
78 false,
79 false,
80 false,
Mathieu Chartierdce71f32015-02-27 14:24:37 -080081 false, // pic
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080082 nullptr,
83 pass_manager_options,
Andreas Gampe6cf49e52015-03-05 13:08:45 -080084 nullptr,
85 false));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086 const InstructionSet instruction_set = kRuntimeISA;
87 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
88 cumulative_logger_.reset(new CumulativeLogger("jit times"));
89 verification_results_.reset(new VerificationResults(compiler_options_.get()));
90 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
91 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
92 method_inliner_map_.get()));
93 compiler_driver_.reset(new CompilerDriver(
94 compiler_options_.get(), verification_results_.get(), method_inliner_map_.get(),
95 Compiler::kQuick, instruction_set, instruction_set_features_.get(), false,
96 nullptr, new std::set<std::string>, 1, false, true,
97 std::string(), cumulative_logger_.get(), -1, std::string()));
98 // Disable dedupe so we can remove compiled methods.
99 compiler_driver_->SetDedupeEnabled(false);
100 compiler_driver_->SetSupportBootImageFixup(false);
101}
102
103JitCompiler::~JitCompiler() {
104}
105
106bool JitCompiler::CompileMethod(Thread* self, mirror::ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700107 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700108 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800109 StackHandleScope<2> hs(self);
110 self->AssertNoPendingException();
111 Runtime* runtime = Runtime::Current();
112 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
113 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
114 VLOG(jit) << "Already compiled " << PrettyMethod(method);
115 return true; // Already compiled
116 }
117 Handle<mirror::Class> h_class(hs.NewHandle(h_method->GetDeclaringClass()));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700118 {
119 TimingLogger::ScopedTiming t2("Initializing", &logger);
120 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
121 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(h_method.Get());
122 return false;
123 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 }
125 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
126 MethodReference method_ref(dex_file, h_method->GetDexMethodIndex());
127 // Only verify if we don't already have verification results.
128 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700129 TimingLogger::ScopedTiming t2("Verifying", &logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800130 std::string error;
131 if (verifier::MethodVerifier::VerifyMethod(h_method.Get(), true, &error) ==
132 verifier::MethodVerifier::kHardFailure) {
133 VLOG(jit) << "Not compile method " << PrettyMethod(h_method.Get())
134 << " due to verification failure " << error;
135 return false;
136 }
137 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700138 CompiledMethod* compiled_method = nullptr;
139 {
140 TimingLogger::ScopedTiming t2("Compiling", &logger);
141 compiled_method = compiler_driver_->CompileMethod(self, h_method.Get());
142 }
143 {
144 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
145 // Trim maps to reduce memory usage, TODO: measure how much this increases compile time.
146 runtime->GetArenaPool()->TrimMaps();
147 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800148 if (compiled_method == nullptr) {
149 return false;
150 }
151 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800152 // Don't add the method if we are supposed to be deoptimized.
153 bool result = false;
154 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700155 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800156 h_method.Get());
157 if (code != nullptr) {
158 // Already have some compiled code, just use this instead of linking.
159 // TODO: Fix recompilation.
160 h_method->SetEntryPointFromQuickCompiledCode(code);
161 result = true;
162 } else {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700163 TimingLogger::ScopedTiming t2("MakeExecutable", &logger);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800164 result = MakeExecutable(compiled_method, h_method.Get());
165 }
166 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800167 // Remove the compiled method to save memory.
168 compiler_driver_->RemoveCompiledMethod(method_ref);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700169 runtime->GetJit()->AddTimingLogger(logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 return result;
171}
172
173CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
174 return callbacks_.get();
175}
176
177uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
178 uint8_t* reserve_begin, uint8_t* reserve_end,
179 const uint8_t* mapping_table,
180 const uint8_t* vmap_table,
181 const uint8_t* gc_map) {
182 reserve_begin += sizeof(OatQuickMethodHeader);
183 reserve_begin = reinterpret_cast<uint8_t*>(
184 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
185 const auto* quick_code = compiled_method->GetQuickCode();
186 CHECK_LE(reserve_begin, reserve_end);
187 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
188 auto* code_ptr = reserve_begin;
189 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
190 // Construct the header last.
191 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
192 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
193 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
194 const auto code_size = quick_code->size();
195 CHECK_NE(code_size, 0U);
196 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
197 // After we are done writing we need to update the method header.
198 // Write out the method header last.
199 method_header = new(method_header)OatQuickMethodHeader(
200 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
201 core_spill_mask, fp_spill_mask, code_size);
202 // Return the code ptr.
203 return code_ptr;
204}
205
206bool JitCompiler::AddToCodeCache(mirror::ArtMethod* method, const CompiledMethod* compiled_method,
207 OatFile::OatMethod* out_method) {
208 Runtime* runtime = Runtime::Current();
209 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
210 const auto* quick_code = compiled_method->GetQuickCode();
211 if (quick_code == nullptr) {
212 return false;
213 }
214 const auto code_size = quick_code->size();
215 Thread* const self = Thread::Current();
216 const uint8_t* base = code_cache->CodeCachePtr();
217 auto* const mapping_table = compiled_method->GetMappingTable();
218 auto* const vmap_table = compiled_method->GetVmapTable();
219 auto* const gc_map = compiled_method->GetGcMap();
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700220 CHECK(gc_map != nullptr) << PrettyMethod(method);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800221 // Write out pre-header stuff.
222 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
223 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700224 if (mapping_table_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800225 return false; // Out of data cache.
226 }
227 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
228 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700229 if (vmap_table_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800230 return false; // Out of data cache.
231 }
232 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
233 self, gc_map->data(), gc_map->data() + gc_map->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700234 if (gc_map_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800235 return false; // Out of data cache.
236 }
237 // Don't touch this until you protect / unprotect the code.
238 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
239 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
240 if (code_reserve == nullptr) {
241 return false;
242 }
243 auto* code_ptr = WriteMethodHeaderAndCode(
244 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
245 vmap_table_ptr, gc_map_ptr);
246
247 const size_t thumb_offset = compiled_method->CodeDelta();
248 const uint32_t code_offset = code_ptr - base + thumb_offset;
249 *out_method = OatFile::OatMethod(base, code_offset);
250 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
251 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
252 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
253 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
254 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
255 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
256 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
257 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
258 << "," << reinterpret_cast<void*>(code_ptr + code_size);
259 return true;
260}
261
262bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, mirror::ArtMethod* method) {
263 CHECK(method != nullptr);
264 CHECK(compiled_method != nullptr);
265 OatFile::OatMethod oat_method(nullptr, 0);
266 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
267 return false;
268 }
269 // TODO: Flush instruction cache.
270 oat_method.LinkMethod(method);
271 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
272 << PrettyMethod(method);
273 return true;
274}
275
276} // namespace jit
277} // namespace art