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 | 3d21bdf | 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 | 41b175a | 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 | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 57 | extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 58 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 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, |
| 74 | false, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 75 | CompilerOptions::kDefaultTopKProfileThreshold, |
Andreas Gampe | 7b2f09e | 2015-03-02 14:07:33 -0800 | [diff] [blame] | 76 | false, // TODO: Think about debuggability of JIT-compiled code. |
David Srbecky | a26cb57 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 77 | CompilerOptions::kDefaultGenerateDebugInfo, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 78 | false, |
| 79 | false, |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 80 | false, |
Mathieu Chartier | dce71f3 | 2015-02-27 14:24:37 -0800 | [diff] [blame] | 81 | false, // pic |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 82 | nullptr, |
| 83 | pass_manager_options, |
Andreas Gampe | 6cf49e5 | 2015-03-05 13:08:45 -0800 | [diff] [blame] | 84 | nullptr, |
| 85 | false)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 86 | 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 Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 92 | method_inliner_map_.get(), |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 93 | CompilerCallbacks::CallbackMode::kCompileApp)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 94 | 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, |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 97 | nullptr, nullptr, nullptr, 1, false, true, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 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 | |
| 104 | JitCompiler::~JitCompiler() { |
| 105 | } |
| 106 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 107 | bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 108 | TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit)); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 109 | const uint64_t start_time = NanoTime(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 110 | StackHandleScope<2> hs(self); |
| 111 | self->AssertNoPendingException(); |
| 112 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 113 | if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) { |
| 114 | VLOG(jit) << "Already compiled " << PrettyMethod(method); |
| 115 | return true; // Already compiled |
| 116 | } |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 117 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 118 | { |
| 119 | TimingLogger::ScopedTiming t2("Initializing", &logger); |
| 120 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 121 | VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 124 | } |
| 125 | const DexFile* dex_file = h_class->GetDexCache()->GetDexFile(); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 126 | MethodReference method_ref(dex_file, method->GetDexMethodIndex()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 127 | // Only verify if we don't already have verification results. |
| 128 | if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 129 | TimingLogger::ScopedTiming t2("Verifying", &logger); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 130 | std::string error; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 131 | if (verifier::MethodVerifier::VerifyMethod(method, true, &error) == |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 132 | verifier::MethodVerifier::kHardFailure) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 133 | VLOG(jit) << "Not compile method " << PrettyMethod(method) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 134 | << " due to verification failure " << error; |
| 135 | return false; |
| 136 | } |
| 137 | } |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 138 | CompiledMethod* compiled_method = nullptr; |
| 139 | { |
| 140 | TimingLogger::ScopedTiming t2("Compiling", &logger); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 141 | compiled_method = compiler_driver_->CompileMethod(self, method); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 148 | if (compiled_method == nullptr) { |
| 149 | return false; |
| 150 | } |
| 151 | total_time_ += NanoTime() - start_time; |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 152 | // Don't add the method if we are supposed to be deoptimized. |
| 153 | bool result = false; |
| 154 | if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 155 | const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 156 | if (code != nullptr) { |
| 157 | // Already have some compiled code, just use this instead of linking. |
| 158 | // TODO: Fix recompilation. |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 159 | method->SetEntryPointFromQuickCompiledCode(code); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 160 | result = true; |
| 161 | } else { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 162 | TimingLogger::ScopedTiming t2("MakeExecutable", &logger); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 163 | result = MakeExecutable(compiled_method, method); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 166 | // Remove the compiled method to save memory. |
| 167 | compiler_driver_->RemoveCompiledMethod(method_ref); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 168 | runtime->GetJit()->AddTimingLogger(logger); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 169 | return result; |
| 170 | } |
| 171 | |
| 172 | CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const { |
| 173 | return callbacks_.get(); |
| 174 | } |
| 175 | |
| 176 | uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method, |
| 177 | uint8_t* reserve_begin, uint8_t* reserve_end, |
| 178 | const uint8_t* mapping_table, |
| 179 | const uint8_t* vmap_table, |
| 180 | const uint8_t* gc_map) { |
| 181 | reserve_begin += sizeof(OatQuickMethodHeader); |
| 182 | reserve_begin = reinterpret_cast<uint8_t*>( |
| 183 | compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin))); |
| 184 | const auto* quick_code = compiled_method->GetQuickCode(); |
| 185 | CHECK_LE(reserve_begin, reserve_end); |
| 186 | CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin)); |
| 187 | auto* code_ptr = reserve_begin; |
| 188 | OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1; |
| 189 | // Construct the header last. |
| 190 | const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 191 | const auto core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 192 | const auto fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 193 | const auto code_size = quick_code->size(); |
| 194 | CHECK_NE(code_size, 0U); |
| 195 | std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr); |
| 196 | // After we are done writing we need to update the method header. |
| 197 | // Write out the method header last. |
| 198 | method_header = new(method_header)OatQuickMethodHeader( |
| 199 | code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes, |
| 200 | core_spill_mask, fp_spill_mask, code_size); |
| 201 | // Return the code ptr. |
| 202 | return code_ptr; |
| 203 | } |
| 204 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 205 | bool JitCompiler::AddToCodeCache(ArtMethod* method, const CompiledMethod* compiled_method, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 206 | OatFile::OatMethod* out_method) { |
| 207 | Runtime* runtime = Runtime::Current(); |
| 208 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
| 209 | const auto* quick_code = compiled_method->GetQuickCode(); |
| 210 | if (quick_code == nullptr) { |
| 211 | return false; |
| 212 | } |
| 213 | const auto code_size = quick_code->size(); |
| 214 | Thread* const self = Thread::Current(); |
| 215 | const uint8_t* base = code_cache->CodeCachePtr(); |
| 216 | auto* const mapping_table = compiled_method->GetMappingTable(); |
| 217 | auto* const vmap_table = compiled_method->GetVmapTable(); |
| 218 | auto* const gc_map = compiled_method->GetGcMap(); |
Mathieu Chartier | b4e1808 | 2015-03-22 13:52:48 -0700 | [diff] [blame] | 219 | CHECK(gc_map != nullptr) << PrettyMethod(method); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 220 | // Write out pre-header stuff. |
| 221 | uint8_t* const mapping_table_ptr = code_cache->AddDataArray( |
| 222 | self, mapping_table->data(), mapping_table->data() + mapping_table->size()); |
Mathieu Chartier | b4e1808 | 2015-03-22 13:52:48 -0700 | [diff] [blame] | 223 | if (mapping_table_ptr == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 224 | return false; // Out of data cache. |
| 225 | } |
| 226 | uint8_t* const vmap_table_ptr = code_cache->AddDataArray( |
| 227 | self, vmap_table->data(), vmap_table->data() + vmap_table->size()); |
Mathieu Chartier | b4e1808 | 2015-03-22 13:52:48 -0700 | [diff] [blame] | 228 | if (vmap_table_ptr == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 229 | return false; // Out of data cache. |
| 230 | } |
| 231 | uint8_t* const gc_map_ptr = code_cache->AddDataArray( |
| 232 | self, gc_map->data(), gc_map->data() + gc_map->size()); |
Mathieu Chartier | b4e1808 | 2015-03-22 13:52:48 -0700 | [diff] [blame] | 233 | if (gc_map_ptr == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 234 | return false; // Out of data cache. |
| 235 | } |
| 236 | // Don't touch this until you protect / unprotect the code. |
| 237 | const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32; |
| 238 | uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size); |
| 239 | if (code_reserve == nullptr) { |
| 240 | return false; |
| 241 | } |
| 242 | auto* code_ptr = WriteMethodHeaderAndCode( |
| 243 | compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr, |
| 244 | vmap_table_ptr, gc_map_ptr); |
| 245 | |
Mathieu Chartier | 2791d5e | 2015-06-01 19:12:36 -0700 | [diff] [blame] | 246 | __builtin___clear_cache(reinterpret_cast<char*>(code_ptr), |
| 247 | reinterpret_cast<char*>(code_ptr + quick_code->size())); |
| 248 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 249 | const size_t thumb_offset = compiled_method->CodeDelta(); |
| 250 | const uint32_t code_offset = code_ptr - base + thumb_offset; |
| 251 | *out_method = OatFile::OatMethod(base, code_offset); |
| 252 | DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr); |
| 253 | DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr); |
| 254 | DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr); |
| 255 | DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes()); |
| 256 | DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask()); |
| 257 | DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask()); |
| 258 | VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size=" |
| 259 | << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr) |
| 260 | << "," << reinterpret_cast<void*>(code_ptr + code_size); |
| 261 | return true; |
| 262 | } |
| 263 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 264 | bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, ArtMethod* method) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 265 | CHECK(method != nullptr); |
| 266 | CHECK(compiled_method != nullptr); |
| 267 | OatFile::OatMethod oat_method(nullptr, 0); |
| 268 | if (!AddToCodeCache(method, compiled_method, &oat_method)) { |
| 269 | return false; |
| 270 | } |
| 271 | // TODO: Flush instruction cache. |
| 272 | oat_method.LinkMethod(method); |
| 273 | CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method)) |
| 274 | << PrettyMethod(method); |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | } // namespace jit |
| 279 | } // namespace art |