blob: d520208d32f823a80bc80c68893c5639c117173c [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
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000066// Callers of this method assume it has NO_RETURN.
67NO_RETURN static void Usage(const char* fmt, ...) {
68 va_list ap;
69 va_start(ap, fmt);
70 std::string error;
71 StringAppendV(&error, fmt, ap);
72 LOG(FATAL) << error;
73 va_end(ap);
74 exit(EXIT_FAILURE);
75}
76
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077JitCompiler::JitCompiler() : total_time_(0) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080078 compiler_options_.reset(new CompilerOptions(
79 CompilerOptions::kDefaultCompilerFilter,
80 CompilerOptions::kDefaultHugeMethodThreshold,
81 CompilerOptions::kDefaultLargeMethodThreshold,
82 CompilerOptions::kDefaultSmallMethodThreshold,
83 CompilerOptions::kDefaultTinyMethodThreshold,
84 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010085 CompilerOptions::kDefaultInlineDepthLimit,
86 CompilerOptions::kDefaultInlineMaxCodeUnits,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010087 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080088 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010089 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +010090 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010091 /* implicit_null_checks */ true,
92 /* implicit_so_checks */ true,
93 /* implicit_suspend_checks */ false,
94 /* pic */ true, // TODO: Support non-PIC in optimizing.
95 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010096 /* init_failure_output */ nullptr,
97 /* abort_on_hard_verifier_failure */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000098 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
99 compiler_options_->ParseCompilerOption(argument, Usage);
100 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800101 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700102 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
103 VLOG(compiler) << "JIT compiler option " << option;
104 std::string error_msg;
105 if (option.starts_with("--instruction-set-variant=")) {
106 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
107 VLOG(compiler) << "JIT instruction set variant " << str;
108 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
109 instruction_set, str.as_string(), &error_msg));
110 if (instruction_set_features_ == nullptr) {
111 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
112 }
113 } else if (option.starts_with("--instruction-set-features=")) {
114 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
115 VLOG(compiler) << "JIT instruction set features " << str;
116 if (instruction_set_features_.get() == nullptr) {
117 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
118 instruction_set, "default", &error_msg));
119 if (instruction_set_features_ == nullptr) {
120 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
121 }
122 }
123 instruction_set_features_.reset(
124 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
125 if (instruction_set_features_ == nullptr) {
126 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
127 }
128 }
129 }
130 if (instruction_set_features_ == nullptr) {
131 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
132 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800133 cumulative_logger_.reset(new CumulativeLogger("jit times"));
134 verification_results_.reset(new VerificationResults(compiler_options_.get()));
135 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
136 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700137 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700138 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800139 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100140 compiler_options_.get(),
141 verification_results_.get(),
142 method_inliner_map_.get(),
143 Compiler::kOptimizing,
144 instruction_set,
145 instruction_set_features_.get(),
146 /* image */ false,
147 /* image_classes */ nullptr,
148 /* compiled_classes */ nullptr,
149 /* compiled_methods */ nullptr,
150 /* thread_count */ 1,
151 /* dump_stats */ false,
152 /* dump_passes */ false,
153 /* dump_cfg_file_name */ "",
Calin Juravle87000a92015-08-24 15:34:44 +0100154 /* dump_cfg_append */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100155 cumulative_logger_.get(),
156 /* swap_fd */ -1,
157 /* profile_file */ ""));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 // Disable dedupe so we can remove compiled methods.
159 compiler_driver_->SetDedupeEnabled(false);
160 compiler_driver_->SetSupportBootImageFixup(false);
161}
162
163JitCompiler::~JitCompiler() {
164}
165
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700167 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700168 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800169 StackHandleScope<2> hs(self);
170 self->AssertNoPendingException();
171 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100172
173 // Check if the method is already compiled.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100174 if (runtime->GetJit()->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175 VLOG(jit) << "Already compiled " << PrettyMethod(method);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100176 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100178
179 // Don't compile the method if we are supposed to be deoptimized.
180 if (runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
181 return false;
182 }
183
184 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100186 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
187 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
188 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800189 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100190
191 // Do the compilation.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700192 CompiledMethod* compiled_method = nullptr;
193 {
194 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000195 // If we get a request to compile a proxy method, we pass the actual Java method
196 // of that proxy method, as the compiler does not expect a proxy method.
197 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
198 compiled_method = compiler_driver_->CompileArtMethod(self, method_to_compile);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700199 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100200
201 // Trim maps to reduce memory usage.
202 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700203 {
204 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700205 runtime->GetArenaPool()->TrimMaps();
206 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100207
208 // Check if we failed compiling.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800209 if (compiled_method == nullptr) {
210 return false;
211 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100212
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800213 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800214 bool result = false;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100215 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method);
216
217 if (code != nullptr) {
218 // Already have some compiled code, just use this instead of linking.
219 // TODO: Fix recompilation.
220 method->SetEntryPointFromQuickCompiledCode(code);
221 result = true;
222 } else {
223 TimingLogger::ScopedTiming t2("LinkCode", &logger);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100224 if (AddToCodeCache(method, compiled_method)) {
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800225 result = true;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800226 }
227 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100228
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800229 // Remove the compiled method to save memory.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100230 compiler_driver_->RemoveCompiledMethod(
231 MethodReference(h_class->GetDexCache()->GetDexFile(), method->GetDexMethodIndex()));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700232 runtime->GetJit()->AddTimingLogger(logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800233 return result;
234}
235
236CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
237 return callbacks_.get();
238}
239
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100240bool JitCompiler::AddToCodeCache(ArtMethod* method,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100241 const CompiledMethod* compiled_method) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800242 Runtime* runtime = Runtime::Current();
243 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Vladimir Marko35831e82015-09-11 11:59:18 +0100244 auto const quick_code = compiled_method->GetQuickCode();
245 if (quick_code.empty()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800246 return false;
247 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100248 const auto code_size = quick_code.size();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800249 Thread* const self = Thread::Current();
Vladimir Marko35831e82015-09-11 11:59:18 +0100250 auto const mapping_table = compiled_method->GetMappingTable();
251 auto const vmap_table = compiled_method->GetVmapTable();
252 auto const gc_map = compiled_method->GetGcMap();
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100253 uint8_t* mapping_table_ptr = nullptr;
254 uint8_t* vmap_table_ptr = nullptr;
255 uint8_t* gc_map_ptr = nullptr;
256
Vladimir Marko35831e82015-09-11 11:59:18 +0100257 if (!mapping_table.empty()) {
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100258 // Write out pre-header stuff.
259 mapping_table_ptr = code_cache->AddDataArray(
Vladimir Marko35831e82015-09-11 11:59:18 +0100260 self, mapping_table.data(), mapping_table.data() + mapping_table.size());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100261 if (mapping_table_ptr == nullptr) {
262 return false; // Out of data cache.
263 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800264 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100265
Vladimir Marko35831e82015-09-11 11:59:18 +0100266 if (!vmap_table.empty()) {
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100267 vmap_table_ptr = code_cache->AddDataArray(
Vladimir Marko35831e82015-09-11 11:59:18 +0100268 self, vmap_table.data(), vmap_table.data() + vmap_table.size());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100269 if (vmap_table_ptr == nullptr) {
270 return false; // Out of data cache.
271 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800272 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100273
Vladimir Marko35831e82015-09-11 11:59:18 +0100274 if (!gc_map.empty()) {
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100275 gc_map_ptr = code_cache->AddDataArray(
Vladimir Marko35831e82015-09-11 11:59:18 +0100276 self, gc_map.data(), gc_map.data() + gc_map.size());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100277 if (gc_map_ptr == nullptr) {
278 return false; // Out of data cache.
279 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800280 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100281
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100282 uint8_t* const code = code_cache->CommitCode(self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100283 method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100284 mapping_table_ptr,
285 vmap_table_ptr,
286 gc_map_ptr,
287 compiled_method->GetFrameSizeInBytes(),
288 compiled_method->GetCoreSpillMask(),
289 compiled_method->GetFpSpillMask(),
Vladimir Marko35831e82015-09-11 11:59:18 +0100290 compiled_method->GetQuickCode().data(),
291 compiled_method->GetQuickCode().size());
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100292
293 if (code == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800294 return false;
295 }
Mathieu Chartier5783a742015-06-01 19:12:36 -0700296
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800297 const size_t thumb_offset = compiled_method->CodeDelta();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100298 const uint32_t code_offset = sizeof(OatQuickMethodHeader) + thumb_offset;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100299 VLOG(jit)
300 << "JIT added "
301 << PrettyMethod(method) << "@" << method
302 << " ccache_size=" << PrettySize(code_cache->CodeCacheSize()) << ": "
303 << reinterpret_cast<void*>(code + code_offset)
304 << "," << reinterpret_cast<void*>(code + code_offset + code_size);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800305 return true;
306}
307
308} // namespace jit
309} // namespace art