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" |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 22 | #include "base/stringpiece.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "base/time_utils.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 24 | #include "base/timing_logger.h" |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 25 | #include "base/unix_file/fd_file.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 26 | #include "compiler_callbacks.h" |
| 27 | #include "dex/pass_manager.h" |
| 28 | #include "dex/quick_compiler_callbacks.h" |
| 29 | #include "driver/compiler_driver.h" |
| 30 | #include "driver/compiler_options.h" |
| 31 | #include "jit/jit.h" |
| 32 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 33 | #include "oat_file-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 34 | #include "oat_quick_method_header.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | #include "object_lock.h" |
| 36 | #include "thread_list.h" |
| 37 | #include "verifier/method_verifier-inl.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace jit { |
| 41 | |
| 42 | JitCompiler* JitCompiler::Create() { |
| 43 | return new JitCompiler(); |
| 44 | } |
| 45 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 46 | extern "C" void* jit_load(CompilerCallbacks** callbacks, bool* generate_debug_info) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 47 | VLOG(jit) << "loading jit compiler"; |
| 48 | auto* const jit_compiler = JitCompiler::Create(); |
| 49 | CHECK(jit_compiler != nullptr); |
| 50 | *callbacks = jit_compiler->GetCompilerCallbacks(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 51 | *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 52 | VLOG(jit) << "Done loading jit compiler"; |
| 53 | return jit_compiler; |
| 54 | } |
| 55 | |
| 56 | extern "C" void jit_unload(void* handle) { |
| 57 | DCHECK(handle != nullptr); |
| 58 | delete reinterpret_cast<JitCompiler*>(handle); |
| 59 | } |
| 60 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 61 | extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 62 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 63 | auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle); |
| 64 | DCHECK(jit_compiler != nullptr); |
| 65 | return jit_compiler->CompileMethod(self, method); |
| 66 | } |
| 67 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 68 | // Callers of this method assume it has NO_RETURN. |
| 69 | NO_RETURN static void Usage(const char* fmt, ...) { |
| 70 | va_list ap; |
| 71 | va_start(ap, fmt); |
| 72 | std::string error; |
| 73 | StringAppendV(&error, fmt, ap); |
| 74 | LOG(FATAL) << error; |
| 75 | va_end(ap); |
| 76 | exit(EXIT_FAILURE); |
| 77 | } |
| 78 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 79 | JitCompiler::JitCompiler() : total_time_(0) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 80 | compiler_options_.reset(new CompilerOptions( |
| 81 | CompilerOptions::kDefaultCompilerFilter, |
| 82 | CompilerOptions::kDefaultHugeMethodThreshold, |
| 83 | CompilerOptions::kDefaultLargeMethodThreshold, |
| 84 | CompilerOptions::kDefaultSmallMethodThreshold, |
| 85 | CompilerOptions::kDefaultTinyMethodThreshold, |
| 86 | CompilerOptions::kDefaultNumDexMethodsThreshold, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 87 | CompilerOptions::kDefaultInlineDepthLimit, |
| 88 | CompilerOptions::kDefaultInlineMaxCodeUnits, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 89 | /* no_inline_from */ nullptr, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 90 | /* include_patch_information */ false, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 91 | CompilerOptions::kDefaultTopKProfileThreshold, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 92 | Runtime::Current()->IsDebuggable(), |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 93 | CompilerOptions::kDefaultGenerateDebugInfo, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 94 | /* implicit_null_checks */ true, |
| 95 | /* implicit_so_checks */ true, |
| 96 | /* implicit_suspend_checks */ false, |
| 97 | /* pic */ true, // TODO: Support non-PIC in optimizing. |
| 98 | /* verbose_methods */ nullptr, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 99 | /* init_failure_output */ nullptr, |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame^] | 100 | /* abort_on_hard_verifier_failure */ false, |
| 101 | /* dump_cfg_file_name */ "", |
| 102 | /* dump_cfg_append */ false)); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 103 | for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) { |
| 104 | compiler_options_->ParseCompilerOption(argument, Usage); |
| 105 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 106 | const InstructionSet instruction_set = kRuntimeISA; |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 107 | for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) { |
| 108 | VLOG(compiler) << "JIT compiler option " << option; |
| 109 | std::string error_msg; |
| 110 | if (option.starts_with("--instruction-set-variant=")) { |
| 111 | StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); |
| 112 | VLOG(compiler) << "JIT instruction set variant " << str; |
| 113 | instruction_set_features_.reset(InstructionSetFeatures::FromVariant( |
| 114 | instruction_set, str.as_string(), &error_msg)); |
| 115 | if (instruction_set_features_ == nullptr) { |
| 116 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 117 | } |
| 118 | } else if (option.starts_with("--instruction-set-features=")) { |
| 119 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 120 | VLOG(compiler) << "JIT instruction set features " << str; |
| 121 | if (instruction_set_features_.get() == nullptr) { |
| 122 | instruction_set_features_.reset(InstructionSetFeatures::FromVariant( |
| 123 | instruction_set, "default", &error_msg)); |
| 124 | if (instruction_set_features_ == nullptr) { |
| 125 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 126 | } |
| 127 | } |
| 128 | instruction_set_features_.reset( |
| 129 | instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg)); |
| 130 | if (instruction_set_features_ == nullptr) { |
| 131 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | if (instruction_set_features_ == nullptr) { |
| 136 | instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines()); |
| 137 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 138 | cumulative_logger_.reset(new CumulativeLogger("jit times")); |
| 139 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 140 | method_inliner_map_.reset(new DexFileToMethodInlinerMap); |
| 141 | callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 142 | method_inliner_map_.get(), |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 143 | CompilerCallbacks::CallbackMode::kCompileApp)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 144 | compiler_driver_.reset(new CompilerDriver( |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 145 | compiler_options_.get(), |
| 146 | verification_results_.get(), |
| 147 | method_inliner_map_.get(), |
| 148 | Compiler::kOptimizing, |
| 149 | instruction_set, |
| 150 | instruction_set_features_.get(), |
| 151 | /* image */ false, |
| 152 | /* image_classes */ nullptr, |
| 153 | /* compiled_classes */ nullptr, |
| 154 | /* compiled_methods */ nullptr, |
| 155 | /* thread_count */ 1, |
| 156 | /* dump_stats */ false, |
| 157 | /* dump_passes */ false, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 158 | cumulative_logger_.get(), |
| 159 | /* swap_fd */ -1, |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 160 | /* dex to oat map */ nullptr, |
| 161 | /* profile_compilation_info */ nullptr)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 162 | // Disable dedupe so we can remove compiled methods. |
| 163 | compiler_driver_->SetDedupeEnabled(false); |
| 164 | compiler_driver_->SetSupportBootImageFixup(false); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 165 | |
| 166 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 167 | #ifdef __ANDROID__ |
| 168 | const char* prefix = GetAndroidData(); |
| 169 | #else |
| 170 | const char* prefix = "/tmp"; |
| 171 | #endif |
| 172 | DCHECK_EQ(compiler_driver_->GetThreadCount(), 1u) |
| 173 | << "Generating debug info only works with one compiler thread"; |
| 174 | std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map"; |
| 175 | perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str())); |
| 176 | if (perf_file_ == nullptr) { |
| 177 | LOG(FATAL) << "Could not create perf file at " << perf_filename; |
| 178 | } |
| 179 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | JitCompiler::~JitCompiler() { |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 183 | if (perf_file_ != nullptr) { |
| 184 | UNUSED(perf_file_->Flush()); |
| 185 | UNUSED(perf_file_->Close()); |
| 186 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 189 | bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 190 | TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit)); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 191 | const uint64_t start_time = NanoTime(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 192 | StackHandleScope<2> hs(self); |
| 193 | self->AssertNoPendingException(); |
| 194 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 195 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 196 | // Ensure the class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 197 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 198 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
| 199 | VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method); |
| 200 | return false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 201 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 202 | |
| 203 | // Do the compilation. |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 204 | bool success = false; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 205 | { |
| 206 | TimingLogger::ScopedTiming t2("Compiling", &logger); |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 207 | // If we get a request to compile a proxy method, we pass the actual Java method |
| 208 | // of that proxy method, as the compiler does not expect a proxy method. |
| 209 | ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*)); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 210 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 211 | success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 212 | if (success && compiler_options_->GetGenerateDebugInfo()) { |
| 213 | const void* ptr = method_to_compile->GetEntryPointFromQuickCompiledCode(); |
| 214 | std::ostringstream stream; |
| 215 | stream << std::hex |
| 216 | << reinterpret_cast<uintptr_t>(ptr) |
| 217 | << " " |
| 218 | << code_cache->GetMemorySizeOfCodePointer(ptr) |
| 219 | << " " |
| 220 | << PrettyMethod(method_to_compile) |
| 221 | << std::endl; |
| 222 | std::string str = stream.str(); |
| 223 | bool res = perf_file_->WriteFully(str.c_str(), str.size()); |
| 224 | CHECK(res); |
| 225 | } |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 226 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 227 | |
| 228 | // Trim maps to reduce memory usage. |
| 229 | // TODO: measure how much this increases compile time. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 230 | { |
| 231 | TimingLogger::ScopedTiming t2("TrimMaps", &logger); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 232 | runtime->GetArenaPool()->TrimMaps(); |
| 233 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 234 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 235 | total_time_ += NanoTime() - start_time; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 236 | runtime->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 237 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const { |
| 241 | return callbacks_.get(); |
| 242 | } |
| 243 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 244 | } // namespace jit |
| 245 | } // namespace art |