blob: a47c601de8bcf177b2f224a425d19bb80c88f5c3 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080020#include "arch/instruction_set.h"
21#include "arch/instruction_set_features.h"
Mathieu Chartier085fc872015-10-15 18:19:01 -070022#include "base/stringpiece.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070024#include "base/timing_logger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080025#include "compiler_callbacks.h"
26#include "dex/pass_manager.h"
27#include "dex/quick_compiler_callbacks.h"
28#include "driver/compiler_driver.h"
29#include "driver/compiler_options.h"
30#include "jit/jit.h"
31#include "jit/jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "object_lock.h"
35#include "thread_list.h"
36#include "verifier/method_verifier-inl.h"
37
38namespace art {
39namespace jit {
40
41JitCompiler* JitCompiler::Create() {
42 return new JitCompiler();
43}
44
45extern "C" void* jit_load(CompilerCallbacks** callbacks) {
46 VLOG(jit) << "loading jit compiler";
47 auto* const jit_compiler = JitCompiler::Create();
48 CHECK(jit_compiler != nullptr);
49 *callbacks = jit_compiler->GetCompilerCallbacks();
50 VLOG(jit) << "Done loading jit compiler";
51 return jit_compiler;
52}
53
54extern "C" void jit_unload(void* handle) {
55 DCHECK(handle != nullptr);
56 delete reinterpret_cast<JitCompiler*>(handle);
57}
58
Mathieu Chartiere401d142015-04-22 13:56:20 -070059extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -070060 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
62 DCHECK(jit_compiler != nullptr);
63 return jit_compiler->CompileMethod(self, method);
64}
65
66JitCompiler::JitCompiler() : total_time_(0) {
67 auto* pass_manager_options = new PassManagerOptions;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070068 pass_manager_options->SetDisablePassList("GVN,DCE,GVNCleanup");
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069 compiler_options_.reset(new CompilerOptions(
70 CompilerOptions::kDefaultCompilerFilter,
71 CompilerOptions::kDefaultHugeMethodThreshold,
72 CompilerOptions::kDefaultLargeMethodThreshold,
73 CompilerOptions::kDefaultSmallMethodThreshold,
74 CompilerOptions::kDefaultTinyMethodThreshold,
75 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010076 CompilerOptions::kDefaultInlineDepthLimit,
77 CompilerOptions::kDefaultInlineMaxCodeUnits,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010078 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080079 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010080 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +010081 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010082 /* implicit_null_checks */ true,
83 /* implicit_so_checks */ true,
84 /* implicit_suspend_checks */ false,
85 /* pic */ true, // TODO: Support non-PIC in optimizing.
86 /* verbose_methods */ nullptr,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080087 pass_manager_options,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010088 /* init_failure_output */ nullptr,
89 /* abort_on_hard_verifier_failure */ false));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080090 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -070091 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
92 VLOG(compiler) << "JIT compiler option " << option;
93 std::string error_msg;
94 if (option.starts_with("--instruction-set-variant=")) {
95 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
96 VLOG(compiler) << "JIT instruction set variant " << str;
97 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
98 instruction_set, str.as_string(), &error_msg));
99 if (instruction_set_features_ == nullptr) {
100 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
101 }
102 } else if (option.starts_with("--instruction-set-features=")) {
103 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
104 VLOG(compiler) << "JIT instruction set features " << str;
105 if (instruction_set_features_.get() == nullptr) {
106 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
107 instruction_set, "default", &error_msg));
108 if (instruction_set_features_ == nullptr) {
109 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
110 }
111 }
112 instruction_set_features_.reset(
113 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
114 if (instruction_set_features_ == nullptr) {
115 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
116 }
117 }
118 }
119 if (instruction_set_features_ == nullptr) {
120 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
121 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800122 cumulative_logger_.reset(new CumulativeLogger("jit times"));
123 verification_results_.reset(new VerificationResults(compiler_options_.get()));
124 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
125 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700126 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700127 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100129 compiler_options_.get(),
130 verification_results_.get(),
131 method_inliner_map_.get(),
132 Compiler::kOptimizing,
133 instruction_set,
134 instruction_set_features_.get(),
135 /* image */ false,
136 /* image_classes */ nullptr,
137 /* compiled_classes */ nullptr,
138 /* compiled_methods */ nullptr,
139 /* thread_count */ 1,
140 /* dump_stats */ false,
141 /* dump_passes */ false,
142 /* dump_cfg_file_name */ "",
Calin Juravle87000a92015-08-24 15:34:44 +0100143 /* dump_cfg_append */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100144 cumulative_logger_.get(),
145 /* swap_fd */ -1,
146 /* profile_file */ ""));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800147 // Disable dedupe so we can remove compiled methods.
148 compiler_driver_->SetDedupeEnabled(false);
149 compiler_driver_->SetSupportBootImageFixup(false);
150}
151
152JitCompiler::~JitCompiler() {
153}
154
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700156 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700157 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 StackHandleScope<2> hs(self);
159 self->AssertNoPendingException();
160 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100161
162 // Check if the method is already compiled.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800163 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
164 VLOG(jit) << "Already compiled " << PrettyMethod(method);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100165 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100167
168 // Don't compile the method if we are supposed to be deoptimized.
169 if (runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
170 return false;
171 }
172
173 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100175 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
176 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
177 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100179
180 // Do the compilation.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700181 CompiledMethod* compiled_method = nullptr;
182 {
183 TimingLogger::ScopedTiming t2("Compiling", &logger);
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700184 compiled_method = compiler_driver_->CompileArtMethod(self, method);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700185 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100186
187 // Trim maps to reduce memory usage.
188 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700189 {
190 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700191 runtime->GetArenaPool()->TrimMaps();
192 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100193
194 // Check if we failed compiling.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195 if (compiled_method == nullptr) {
196 return false;
197 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100198
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800199 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800200 bool result = false;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100201 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method);
202
203 if (code != nullptr) {
204 // Already have some compiled code, just use this instead of linking.
205 // TODO: Fix recompilation.
206 method->SetEntryPointFromQuickCompiledCode(code);
207 result = true;
208 } else {
209 TimingLogger::ScopedTiming t2("LinkCode", &logger);
210 OatFile::OatMethod oat_method(nullptr, 0);
211 if (AddToCodeCache(method, compiled_method, &oat_method)) {
212 oat_method.LinkMethod(method);
213 CHECK(runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) << PrettyMethod(method);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800214 result = true;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800215 }
216 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100217
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800218 // Remove the compiled method to save memory.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100219 compiler_driver_->RemoveCompiledMethod(
220 MethodReference(h_class->GetDexCache()->GetDexFile(), method->GetDexMethodIndex()));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700221 runtime->GetJit()->AddTimingLogger(logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800222 return result;
223}
224
225CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
226 return callbacks_.get();
227}
228
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100229bool JitCompiler::AddToCodeCache(ArtMethod* method,
230 const CompiledMethod* compiled_method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800231 OatFile::OatMethod* out_method) {
232 Runtime* runtime = Runtime::Current();
233 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Vladimir Marko35831e82015-09-11 11:59:18 +0100234 auto const quick_code = compiled_method->GetQuickCode();
235 if (quick_code.empty()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800236 return false;
237 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100238 const auto code_size = quick_code.size();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800239 Thread* const self = Thread::Current();
Vladimir Marko35831e82015-09-11 11:59:18 +0100240 auto const mapping_table = compiled_method->GetMappingTable();
241 auto const vmap_table = compiled_method->GetVmapTable();
242 auto const gc_map = compiled_method->GetGcMap();
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100243 uint8_t* mapping_table_ptr = nullptr;
244 uint8_t* vmap_table_ptr = nullptr;
245 uint8_t* gc_map_ptr = nullptr;
246
Vladimir Marko35831e82015-09-11 11:59:18 +0100247 if (!mapping_table.empty()) {
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100248 // Write out pre-header stuff.
249 mapping_table_ptr = code_cache->AddDataArray(
Vladimir Marko35831e82015-09-11 11:59:18 +0100250 self, mapping_table.data(), mapping_table.data() + mapping_table.size());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100251 if (mapping_table_ptr == nullptr) {
252 return false; // Out of data cache.
253 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800254 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100255
Vladimir Marko35831e82015-09-11 11:59:18 +0100256 if (!vmap_table.empty()) {
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100257 vmap_table_ptr = code_cache->AddDataArray(
Vladimir Marko35831e82015-09-11 11:59:18 +0100258 self, vmap_table.data(), vmap_table.data() + vmap_table.size());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100259 if (vmap_table_ptr == nullptr) {
260 return false; // Out of data cache.
261 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800262 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100263
Vladimir Marko35831e82015-09-11 11:59:18 +0100264 if (!gc_map.empty()) {
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100265 gc_map_ptr = code_cache->AddDataArray(
Vladimir Marko35831e82015-09-11 11:59:18 +0100266 self, gc_map.data(), gc_map.data() + gc_map.size());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100267 if (gc_map_ptr == nullptr) {
268 return false; // Out of data cache.
269 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800270 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100271
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100272 uint8_t* const code = code_cache->CommitCode(self,
273 mapping_table_ptr,
274 vmap_table_ptr,
275 gc_map_ptr,
276 compiled_method->GetFrameSizeInBytes(),
277 compiled_method->GetCoreSpillMask(),
278 compiled_method->GetFpSpillMask(),
Vladimir Marko35831e82015-09-11 11:59:18 +0100279 compiled_method->GetQuickCode().data(),
280 compiled_method->GetQuickCode().size());
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100281
282 if (code == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800283 return false;
284 }
Mathieu Chartier5783a742015-06-01 19:12:36 -0700285
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800286 const size_t thumb_offset = compiled_method->CodeDelta();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100287 const uint32_t code_offset = sizeof(OatQuickMethodHeader) + thumb_offset;
288 *out_method = OatFile::OatMethod(code, code_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800289 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
290 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
291 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
292 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
293 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
294 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100295 VLOG(jit)
296 << "JIT added "
297 << PrettyMethod(method) << "@" << method
298 << " ccache_size=" << PrettySize(code_cache->CodeCacheSize()) << ": "
299 << reinterpret_cast<void*>(code + code_offset)
300 << "," << reinterpret_cast<void*>(code + code_offset + code_size);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800301 return true;
302}
303
304} // namespace jit
305} // namespace art