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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 21 | #include "arch/instruction_set.h" |
| 22 | #include "arch/instruction_set_features.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 24 | #include "base/stringpiece.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/time_utils.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 26 | #include "base/timing_logger.h" |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 27 | #include "base/unix_file/fd_file.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 28 | #include "debug/elf_debug_writer.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 29 | #include "driver/compiler_driver.h" |
| 30 | #include "driver/compiler_options.h" |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 31 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 32 | #include "jit/jit.h" |
| 33 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "oat_file-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 35 | #include "oat_quick_method_header.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 36 | #include "object_lock.h" |
Matthew Gharrity | 2cd05b7 | 2016-08-03 16:57:37 -0700 | [diff] [blame] | 37 | #include "optimizing/register_allocator.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 38 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | namespace jit { |
| 42 | |
| 43 | JitCompiler* JitCompiler::Create() { |
| 44 | return new JitCompiler(); |
| 45 | } |
| 46 | |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 47 | extern "C" void* jit_load(bool* generate_debug_info) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 48 | VLOG(jit) << "loading jit compiler"; |
| 49 | auto* const jit_compiler = JitCompiler::Create(); |
| 50 | CHECK(jit_compiler != nullptr); |
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 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 61 | extern "C" bool jit_compile_method( |
| 62 | void* handle, ArtMethod* method, Thread* self, bool osr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 64 | auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle); |
| 65 | DCHECK(jit_compiler != nullptr); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 66 | return jit_compiler->CompileMethod(self, method, osr); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 69 | extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 71 | auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle); |
| 72 | DCHECK(jit_compiler != nullptr); |
| 73 | if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 74 | const ArrayRef<mirror::Class*> types_array(types, count); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 75 | std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses( |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 76 | kRuntimeISA, jit_compiler->GetCompilerDriver()->GetInstructionSetFeatures(), types_array); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 77 | CreateJITCodeEntry(std::move(elf_file)); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 81 | // Callers of this method assume it has NO_RETURN. |
| 82 | NO_RETURN static void Usage(const char* fmt, ...) { |
| 83 | va_list ap; |
| 84 | va_start(ap, fmt); |
| 85 | std::string error; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 86 | android::base::StringAppendV(&error, fmt, ap); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 87 | LOG(FATAL) << error; |
| 88 | va_end(ap); |
| 89 | exit(EXIT_FAILURE); |
| 90 | } |
| 91 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 92 | JitCompiler::JitCompiler() { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 93 | compiler_options_.reset(new CompilerOptions( |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 94 | CompilerFilter::kDefaultCompilerFilter, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 95 | CompilerOptions::kDefaultHugeMethodThreshold, |
| 96 | CompilerOptions::kDefaultLargeMethodThreshold, |
| 97 | CompilerOptions::kDefaultSmallMethodThreshold, |
| 98 | CompilerOptions::kDefaultTinyMethodThreshold, |
| 99 | CompilerOptions::kDefaultNumDexMethodsThreshold, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 100 | CompilerOptions::kDefaultInlineMaxCodeUnits, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 101 | /* no_inline_from */ nullptr, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 102 | CompilerOptions::kDefaultTopKProfileThreshold, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 103 | Runtime::Current()->IsJavaDebuggable(), |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 104 | CompilerOptions::kDefaultGenerateDebugInfo, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 105 | /* implicit_null_checks */ true, |
| 106 | /* implicit_so_checks */ true, |
| 107 | /* implicit_suspend_checks */ false, |
| 108 | /* pic */ true, // TODO: Support non-PIC in optimizing. |
| 109 | /* verbose_methods */ nullptr, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 110 | /* init_failure_output */ nullptr, |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 111 | /* abort_on_hard_verifier_failure */ false, |
| 112 | /* dump_cfg_file_name */ "", |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 113 | /* dump_cfg_append */ false, |
Matthew Gharrity | 2cd05b7 | 2016-08-03 16:57:37 -0700 | [diff] [blame] | 114 | /* force_determinism */ false, |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 115 | RegisterAllocator::kRegisterAllocatorDefault, |
| 116 | /* passes_to_run */ nullptr)); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 117 | for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) { |
| 118 | compiler_options_->ParseCompilerOption(argument, Usage); |
| 119 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 120 | const InstructionSet instruction_set = kRuntimeISA; |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 121 | for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) { |
| 122 | VLOG(compiler) << "JIT compiler option " << option; |
| 123 | std::string error_msg; |
| 124 | if (option.starts_with("--instruction-set-variant=")) { |
| 125 | StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); |
| 126 | VLOG(compiler) << "JIT instruction set variant " << str; |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 127 | instruction_set_features_ = InstructionSetFeatures::FromVariant( |
| 128 | instruction_set, str.as_string(), &error_msg); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 129 | if (instruction_set_features_ == nullptr) { |
| 130 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 131 | } |
| 132 | } else if (option.starts_with("--instruction-set-features=")) { |
| 133 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 134 | VLOG(compiler) << "JIT instruction set features " << str; |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 135 | if (instruction_set_features_ == nullptr) { |
| 136 | instruction_set_features_ = InstructionSetFeatures::FromVariant( |
| 137 | instruction_set, "default", &error_msg); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 138 | if (instruction_set_features_ == nullptr) { |
| 139 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 140 | } |
| 141 | } |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 142 | instruction_set_features_ = |
| 143 | instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 144 | if (instruction_set_features_ == nullptr) { |
| 145 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | if (instruction_set_features_ == nullptr) { |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 150 | instruction_set_features_ = InstructionSetFeatures::FromCppDefines(); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 151 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 152 | cumulative_logger_.reset(new CumulativeLogger("jit times")); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 153 | compiler_driver_.reset(new CompilerDriver( |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 154 | compiler_options_.get(), |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 155 | /* verification_results */ nullptr, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 156 | Compiler::kOptimizing, |
| 157 | instruction_set, |
| 158 | instruction_set_features_.get(), |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 159 | /* image_classes */ nullptr, |
| 160 | /* compiled_classes */ nullptr, |
| 161 | /* compiled_methods */ nullptr, |
| 162 | /* thread_count */ 1, |
| 163 | /* dump_stats */ false, |
| 164 | /* dump_passes */ false, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 165 | cumulative_logger_.get(), |
| 166 | /* swap_fd */ -1, |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 167 | /* profile_compilation_info */ nullptr)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 168 | // Disable dedupe so we can remove compiled methods. |
| 169 | compiler_driver_->SetDedupeEnabled(false); |
| 170 | compiler_driver_->SetSupportBootImageFixup(false); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 171 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 172 | size_t thread_count = compiler_driver_->GetThreadCount(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 173 | if (compiler_options_->GetGenerateDebugInfo()) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 174 | DCHECK_EQ(thread_count, 1u) |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 175 | << "Generating debug info only works with one compiler thread"; |
xueliang.zhong | 383b57d | 2016-10-04 11:19:17 +0100 | [diff] [blame] | 176 | jit_logger_.reset(new JitLogger()); |
| 177 | jit_logger_->OpenLog(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 178 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | JitCompiler::~JitCompiler() { |
xueliang.zhong | 383b57d | 2016-10-04 11:19:17 +0100 | [diff] [blame] | 182 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 183 | jit_logger_->CloseLog(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 184 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 187 | bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 188 | DCHECK(!method->IsProxyMethod()); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 189 | TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 190 | StackHandleScope<2> hs(self); |
| 191 | self->AssertNoPendingException(); |
| 192 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 193 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 194 | // Ensure the class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 195 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 196 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 197 | VLOG(jit) << "JIT failed to initialize " << method->PrettyMethod(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 198 | return false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 199 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 200 | |
| 201 | // Do the compilation. |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 202 | bool success = false; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 203 | { |
| 204 | TimingLogger::ScopedTiming t2("Compiling", &logger); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 205 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 206 | success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method, osr); |
xueliang.zhong | 383b57d | 2016-10-04 11:19:17 +0100 | [diff] [blame] | 207 | if (success && (jit_logger_ != nullptr)) { |
xueliang.zhong | 5900149 | 2017-01-11 10:36:41 +0000 | [diff] [blame] | 208 | jit_logger_->WriteLog(code_cache, method, osr); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 209 | } |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 210 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 211 | |
| 212 | // Trim maps to reduce memory usage. |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 213 | // TODO: move this to an idle phase. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 214 | { |
| 215 | TimingLogger::ScopedTiming t2("TrimMaps", &logger); |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 216 | runtime->GetJitArenaPool()->TrimMaps(); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 217 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 218 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 219 | runtime->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 220 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 223 | } // namespace jit |
| 224 | } // namespace art |