Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1 | /* |
| 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 20 | #include "arch/instruction_set.h" |
| 21 | #include "arch/instruction_set_features.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | #include "base/time_utils.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 23 | #include "base/timing_logger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 24 | #include "compiler_callbacks.h" |
| 25 | #include "dex/pass_manager.h" |
| 26 | #include "dex/quick_compiler_callbacks.h" |
| 27 | #include "driver/compiler_driver.h" |
| 28 | #include "driver/compiler_options.h" |
| 29 | #include "jit/jit.h" |
| 30 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 31 | #include "oat_file-inl.h" |
| 32 | #include "object_lock.h" |
| 33 | #include "thread_list.h" |
| 34 | #include "verifier/method_verifier-inl.h" |
| 35 | |
| 36 | namespace art { |
| 37 | namespace jit { |
| 38 | |
| 39 | JitCompiler* JitCompiler::Create() { |
| 40 | return new JitCompiler(); |
| 41 | } |
| 42 | |
| 43 | extern "C" void* jit_load(CompilerCallbacks** callbacks) { |
| 44 | VLOG(jit) << "loading jit compiler"; |
| 45 | auto* const jit_compiler = JitCompiler::Create(); |
| 46 | CHECK(jit_compiler != nullptr); |
| 47 | *callbacks = jit_compiler->GetCompilerCallbacks(); |
| 48 | VLOG(jit) << "Done loading jit compiler"; |
| 49 | return jit_compiler; |
| 50 | } |
| 51 | |
| 52 | extern "C" void jit_unload(void* handle) { |
| 53 | DCHECK(handle != nullptr); |
| 54 | delete reinterpret_cast<JitCompiler*>(handle); |
| 55 | } |
| 56 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 57 | extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 58 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 59 | auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle); |
| 60 | DCHECK(jit_compiler != nullptr); |
| 61 | return jit_compiler->CompileMethod(self, method); |
| 62 | } |
| 63 | |
| 64 | JitCompiler::JitCompiler() : total_time_(0) { |
| 65 | auto* pass_manager_options = new PassManagerOptions; |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 66 | pass_manager_options->SetDisablePassList("GVN,DCE,GVNCleanup"); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 67 | compiler_options_.reset(new CompilerOptions( |
| 68 | CompilerOptions::kDefaultCompilerFilter, |
| 69 | CompilerOptions::kDefaultHugeMethodThreshold, |
| 70 | CompilerOptions::kDefaultLargeMethodThreshold, |
| 71 | CompilerOptions::kDefaultSmallMethodThreshold, |
| 72 | CompilerOptions::kDefaultTinyMethodThreshold, |
| 73 | CompilerOptions::kDefaultNumDexMethodsThreshold, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 74 | CompilerOptions::kDefaultInlineDepthLimit, |
| 75 | CompilerOptions::kDefaultInlineMaxCodeUnits, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 76 | /* include_patch_information */ false, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 77 | CompilerOptions::kDefaultTopKProfileThreshold, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 78 | Runtime::Current()->IsDebuggable(), |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 79 | CompilerOptions::kDefaultGenerateDebugInfo, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 80 | /* implicit_null_checks */ true, |
| 81 | /* implicit_so_checks */ true, |
| 82 | /* implicit_suspend_checks */ false, |
| 83 | /* pic */ true, // TODO: Support non-PIC in optimizing. |
| 84 | /* verbose_methods */ nullptr, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 85 | pass_manager_options, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 86 | /* init_failure_output */ nullptr, |
| 87 | /* abort_on_hard_verifier_failure */ false)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 88 | const InstructionSet instruction_set = kRuntimeISA; |
| 89 | instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines()); |
| 90 | cumulative_logger_.reset(new CumulativeLogger("jit times")); |
| 91 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 92 | method_inliner_map_.reset(new DexFileToMethodInlinerMap); |
| 93 | callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 94 | method_inliner_map_.get(), |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 95 | CompilerCallbacks::CallbackMode::kCompileApp)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 96 | compiler_driver_.reset(new CompilerDriver( |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 97 | compiler_options_.get(), |
| 98 | verification_results_.get(), |
| 99 | method_inliner_map_.get(), |
| 100 | Compiler::kOptimizing, |
| 101 | instruction_set, |
| 102 | instruction_set_features_.get(), |
| 103 | /* image */ false, |
| 104 | /* image_classes */ nullptr, |
| 105 | /* compiled_classes */ nullptr, |
| 106 | /* compiled_methods */ nullptr, |
| 107 | /* thread_count */ 1, |
| 108 | /* dump_stats */ false, |
| 109 | /* dump_passes */ false, |
| 110 | /* dump_cfg_file_name */ "", |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 111 | /* dump_cfg_append */ false, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 112 | cumulative_logger_.get(), |
| 113 | /* swap_fd */ -1, |
| 114 | /* profile_file */ "")); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 115 | // Disable dedupe so we can remove compiled methods. |
| 116 | compiler_driver_->SetDedupeEnabled(false); |
| 117 | compiler_driver_->SetSupportBootImageFixup(false); |
| 118 | } |
| 119 | |
| 120 | JitCompiler::~JitCompiler() { |
| 121 | } |
| 122 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 123 | bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 124 | TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit)); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 125 | const uint64_t start_time = NanoTime(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 126 | StackHandleScope<2> hs(self); |
| 127 | self->AssertNoPendingException(); |
| 128 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 129 | |
| 130 | // Check if the method is already compiled. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 131 | if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) { |
| 132 | VLOG(jit) << "Already compiled " << PrettyMethod(method); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 133 | return true; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 134 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 135 | |
| 136 | // Don't compile the method if we are supposed to be deoptimized. |
| 137 | if (runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | // Ensure the class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 142 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 143 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
| 144 | VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method); |
| 145 | return false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 146 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 147 | |
| 148 | // Do the compilation. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 149 | CompiledMethod* compiled_method = nullptr; |
| 150 | { |
| 151 | TimingLogger::ScopedTiming t2("Compiling", &logger); |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 152 | compiled_method = compiler_driver_->CompileArtMethod(self, method); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 153 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 154 | |
| 155 | // Trim maps to reduce memory usage. |
| 156 | // TODO: measure how much this increases compile time. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 157 | { |
| 158 | TimingLogger::ScopedTiming t2("TrimMaps", &logger); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 159 | runtime->GetArenaPool()->TrimMaps(); |
| 160 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 161 | |
| 162 | // Check if we failed compiling. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 163 | if (compiled_method == nullptr) { |
| 164 | return false; |
| 165 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 166 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 167 | total_time_ += NanoTime() - start_time; |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 168 | bool result = false; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 169 | const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method); |
| 170 | |
| 171 | if (code != nullptr) { |
| 172 | // Already have some compiled code, just use this instead of linking. |
| 173 | // TODO: Fix recompilation. |
| 174 | method->SetEntryPointFromQuickCompiledCode(code); |
| 175 | result = true; |
| 176 | } else { |
| 177 | TimingLogger::ScopedTiming t2("LinkCode", &logger); |
| 178 | OatFile::OatMethod oat_method(nullptr, 0); |
| 179 | if (AddToCodeCache(method, compiled_method, &oat_method)) { |
| 180 | oat_method.LinkMethod(method); |
| 181 | CHECK(runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) << PrettyMethod(method); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 182 | result = true; |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 185 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 186 | // Remove the compiled method to save memory. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 187 | compiler_driver_->RemoveCompiledMethod( |
| 188 | MethodReference(h_class->GetDexCache()->GetDexFile(), method->GetDexMethodIndex())); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 189 | runtime->GetJit()->AddTimingLogger(logger); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 190 | return result; |
| 191 | } |
| 192 | |
| 193 | CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const { |
| 194 | return callbacks_.get(); |
| 195 | } |
| 196 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 197 | bool JitCompiler::AddToCodeCache(ArtMethod* method, |
| 198 | const CompiledMethod* compiled_method, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 199 | OatFile::OatMethod* out_method) { |
| 200 | Runtime* runtime = Runtime::Current(); |
| 201 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
| 202 | const auto* quick_code = compiled_method->GetQuickCode(); |
| 203 | if (quick_code == nullptr) { |
| 204 | return false; |
| 205 | } |
| 206 | const auto code_size = quick_code->size(); |
| 207 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 208 | auto* const mapping_table = compiled_method->GetMappingTable(); |
| 209 | auto* const vmap_table = compiled_method->GetVmapTable(); |
| 210 | auto* const gc_map = compiled_method->GetGcMap(); |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 211 | uint8_t* mapping_table_ptr = nullptr; |
| 212 | uint8_t* vmap_table_ptr = nullptr; |
| 213 | uint8_t* gc_map_ptr = nullptr; |
| 214 | |
| 215 | if (mapping_table != nullptr) { |
| 216 | // Write out pre-header stuff. |
| 217 | mapping_table_ptr = code_cache->AddDataArray( |
| 218 | self, mapping_table->data(), mapping_table->data() + mapping_table->size()); |
| 219 | if (mapping_table_ptr == nullptr) { |
| 220 | return false; // Out of data cache. |
| 221 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 222 | } |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 223 | |
| 224 | if (vmap_table != nullptr) { |
| 225 | vmap_table_ptr = code_cache->AddDataArray( |
| 226 | self, vmap_table->data(), vmap_table->data() + vmap_table->size()); |
| 227 | if (vmap_table_ptr == nullptr) { |
| 228 | return false; // Out of data cache. |
| 229 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 230 | } |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 231 | |
| 232 | if (gc_map != nullptr) { |
| 233 | gc_map_ptr = code_cache->AddDataArray( |
| 234 | self, gc_map->data(), gc_map->data() + gc_map->size()); |
| 235 | if (gc_map_ptr == nullptr) { |
| 236 | return false; // Out of data cache. |
| 237 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 238 | } |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 239 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 240 | uint8_t* const code = code_cache->CommitCode(self, |
| 241 | mapping_table_ptr, |
| 242 | vmap_table_ptr, |
| 243 | gc_map_ptr, |
| 244 | compiled_method->GetFrameSizeInBytes(), |
| 245 | compiled_method->GetCoreSpillMask(), |
| 246 | compiled_method->GetFpSpillMask(), |
| 247 | compiled_method->GetQuickCode()->data(), |
| 248 | compiled_method->GetQuickCode()->size()); |
| 249 | |
| 250 | if (code == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 251 | return false; |
| 252 | } |
Mathieu Chartier | 5783a74 | 2015-06-01 19:12:36 -0700 | [diff] [blame] | 253 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 254 | const size_t thumb_offset = compiled_method->CodeDelta(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 255 | const uint32_t code_offset = sizeof(OatQuickMethodHeader) + thumb_offset; |
| 256 | *out_method = OatFile::OatMethod(code, code_offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 257 | DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr); |
| 258 | DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr); |
| 259 | DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr); |
| 260 | DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes()); |
| 261 | DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask()); |
| 262 | DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask()); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame^] | 263 | VLOG(jit) |
| 264 | << "JIT added " |
| 265 | << PrettyMethod(method) << "@" << method |
| 266 | << " ccache_size=" << PrettySize(code_cache->CodeCacheSize()) << ": " |
| 267 | << reinterpret_cast<void*>(code + code_offset) |
| 268 | << "," << reinterpret_cast<void*>(code + code_offset + code_size); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | |
| 272 | } // namespace jit |
| 273 | } // namespace art |