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