blob: df5d5cca3b23a6c3967f814a29884a7f1400595c [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(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070092 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -070093 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080094 compiler_driver_.reset(new CompilerDriver(
95 compiler_options_.get(), verification_results_.get(), method_inliner_map_.get(),
96 Compiler::kQuick, instruction_set, instruction_set_features_.get(), false,
97 nullptr, new std::set<std::string>, 1, false, true,
98 std::string(), cumulative_logger_.get(), -1, std::string()));
99 // Disable dedupe so we can remove compiled methods.
100 compiler_driver_->SetDedupeEnabled(false);
101 compiler_driver_->SetSupportBootImageFixup(false);
102}
103
104JitCompiler::~JitCompiler() {
105}
106
107bool JitCompiler::CompileMethod(Thread* self, mirror::ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700108 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700109 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800110 StackHandleScope<2> hs(self);
111 self->AssertNoPendingException();
112 Runtime* runtime = Runtime::Current();
113 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
114 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
115 VLOG(jit) << "Already compiled " << PrettyMethod(method);
116 return true; // Already compiled
117 }
118 Handle<mirror::Class> h_class(hs.NewHandle(h_method->GetDeclaringClass()));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700119 {
120 TimingLogger::ScopedTiming t2("Initializing", &logger);
121 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
122 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(h_method.Get());
123 return false;
124 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 }
126 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
127 MethodReference method_ref(dex_file, h_method->GetDexMethodIndex());
128 // Only verify if we don't already have verification results.
129 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700130 TimingLogger::ScopedTiming t2("Verifying", &logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800131 std::string error;
132 if (verifier::MethodVerifier::VerifyMethod(h_method.Get(), true, &error) ==
133 verifier::MethodVerifier::kHardFailure) {
134 VLOG(jit) << "Not compile method " << PrettyMethod(h_method.Get())
135 << " due to verification failure " << error;
136 return false;
137 }
138 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700139 CompiledMethod* compiled_method = nullptr;
140 {
141 TimingLogger::ScopedTiming t2("Compiling", &logger);
142 compiled_method = compiler_driver_->CompileMethod(self, h_method.Get());
143 }
144 {
145 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
146 // Trim maps to reduce memory usage, TODO: measure how much this increases compile time.
147 runtime->GetArenaPool()->TrimMaps();
148 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800149 if (compiled_method == nullptr) {
150 return false;
151 }
152 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800153 // Don't add the method if we are supposed to be deoptimized.
154 bool result = false;
155 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700156 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800157 h_method.Get());
158 if (code != nullptr) {
159 // Already have some compiled code, just use this instead of linking.
160 // TODO: Fix recompilation.
161 h_method->SetEntryPointFromQuickCompiledCode(code);
162 result = true;
163 } else {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700164 TimingLogger::ScopedTiming t2("MakeExecutable", &logger);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800165 result = MakeExecutable(compiled_method, h_method.Get());
166 }
167 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800168 // Remove the compiled method to save memory.
169 compiler_driver_->RemoveCompiledMethod(method_ref);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700170 runtime->GetJit()->AddTimingLogger(logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171 return result;
172}
173
174CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
175 return callbacks_.get();
176}
177
178uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
179 uint8_t* reserve_begin, uint8_t* reserve_end,
180 const uint8_t* mapping_table,
181 const uint8_t* vmap_table,
182 const uint8_t* gc_map) {
183 reserve_begin += sizeof(OatQuickMethodHeader);
184 reserve_begin = reinterpret_cast<uint8_t*>(
185 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
186 const auto* quick_code = compiled_method->GetQuickCode();
187 CHECK_LE(reserve_begin, reserve_end);
188 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
189 auto* code_ptr = reserve_begin;
190 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
191 // Construct the header last.
192 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
193 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
194 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
195 const auto code_size = quick_code->size();
196 CHECK_NE(code_size, 0U);
197 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
198 // After we are done writing we need to update the method header.
199 // Write out the method header last.
200 method_header = new(method_header)OatQuickMethodHeader(
201 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
202 core_spill_mask, fp_spill_mask, code_size);
203 // Return the code ptr.
204 return code_ptr;
205}
206
207bool JitCompiler::AddToCodeCache(mirror::ArtMethod* method, const CompiledMethod* compiled_method,
208 OatFile::OatMethod* out_method) {
209 Runtime* runtime = Runtime::Current();
210 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
211 const auto* quick_code = compiled_method->GetQuickCode();
212 if (quick_code == nullptr) {
213 return false;
214 }
215 const auto code_size = quick_code->size();
216 Thread* const self = Thread::Current();
217 const uint8_t* base = code_cache->CodeCachePtr();
218 auto* const mapping_table = compiled_method->GetMappingTable();
219 auto* const vmap_table = compiled_method->GetVmapTable();
220 auto* const gc_map = compiled_method->GetGcMap();
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700221 CHECK(gc_map != nullptr) << PrettyMethod(method);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800222 // Write out pre-header stuff.
223 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
224 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700225 if (mapping_table_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800226 return false; // Out of data cache.
227 }
228 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
229 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700230 if (vmap_table_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800231 return false; // Out of data cache.
232 }
233 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
234 self, gc_map->data(), gc_map->data() + gc_map->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700235 if (gc_map_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800236 return false; // Out of data cache.
237 }
238 // Don't touch this until you protect / unprotect the code.
239 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
240 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
241 if (code_reserve == nullptr) {
242 return false;
243 }
244 auto* code_ptr = WriteMethodHeaderAndCode(
245 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
246 vmap_table_ptr, gc_map_ptr);
247
248 const size_t thumb_offset = compiled_method->CodeDelta();
249 const uint32_t code_offset = code_ptr - base + thumb_offset;
250 *out_method = OatFile::OatMethod(base, code_offset);
251 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
252 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
253 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
254 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
255 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
256 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
257 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
258 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
259 << "," << reinterpret_cast<void*>(code_ptr + code_size);
260 return true;
261}
262
263bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, mirror::ArtMethod* method) {
264 CHECK(method != nullptr);
265 CHECK(compiled_method != nullptr);
266 OatFile::OatMethod oat_method(nullptr, 0);
267 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
268 return false;
269 }
270 // TODO: Flush instruction cache.
271 oat_method.LinkMethod(method);
272 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
273 << PrettyMethod(method);
274 return true;
275}
276
277} // namespace jit
278} // namespace art