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" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 24 | #include "base/logging.h" // For VLOG |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 25 | #include "base/stringpiece.h" |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 26 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | #include "base/time_utils.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 28 | #include "base/timing_logger.h" |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 29 | #include "base/unix_file/fd_file.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 30 | #include "debug/elf_debug_writer.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 31 | #include "driver/compiler_driver.h" |
| 32 | #include "driver/compiler_options.h" |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 33 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "jit/jit.h" |
| 35 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 36 | #include "jit/jit_logger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 37 | #include "oat_file-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 38 | #include "oat_quick_method_header.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 39 | #include "object_lock.h" |
Matthew Gharrity | 2cd05b7 | 2016-08-03 16:57:37 -0700 | [diff] [blame] | 40 | #include "optimizing/register_allocator.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 41 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 42 | |
| 43 | namespace art { |
| 44 | namespace jit { |
| 45 | |
| 46 | JitCompiler* JitCompiler::Create() { |
| 47 | return new JitCompiler(); |
| 48 | } |
| 49 | |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 50 | extern "C" void* jit_load(bool* generate_debug_info) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 51 | VLOG(jit) << "loading jit compiler"; |
| 52 | auto* const jit_compiler = JitCompiler::Create(); |
| 53 | CHECK(jit_compiler != nullptr); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 54 | *generate_debug_info = jit_compiler->GetCompilerOptions().GetGenerateDebugInfo(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 55 | VLOG(jit) << "Done loading jit compiler"; |
| 56 | return jit_compiler; |
| 57 | } |
| 58 | |
| 59 | extern "C" void jit_unload(void* handle) { |
| 60 | DCHECK(handle != nullptr); |
| 61 | delete reinterpret_cast<JitCompiler*>(handle); |
| 62 | } |
| 63 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 64 | extern "C" bool jit_compile_method( |
| 65 | void* handle, ArtMethod* method, Thread* self, bool osr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 66 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 67 | auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle); |
| 68 | DCHECK(jit_compiler != nullptr); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 69 | return jit_compiler->CompileMethod(self, method, osr); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 72 | 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] | 73 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 74 | auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle); |
| 75 | DCHECK(jit_compiler != nullptr); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 76 | const CompilerOptions& compiler_options = jit_compiler->GetCompilerOptions(); |
| 77 | if (compiler_options.GetGenerateDebugInfo()) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 78 | const ArrayRef<mirror::Class*> types_array(types, count); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 79 | std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses( |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 80 | kRuntimeISA, compiler_options.GetInstructionSetFeatures(), types_array); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 81 | MutexLock mu(Thread::Current(), *Locks::native_debug_interface_lock_); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 82 | // We never free debug info for types, so we don't need to provide a handle |
| 83 | // (which would have been otherwise used as identifier to remove it later). |
| 84 | AddNativeDebugInfoForJit(nullptr /* handle */, elf_file); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 88 | JitCompiler::JitCompiler() { |
Nicolas Geoffray | 57c4704 | 2017-06-29 11:31:39 +0100 | [diff] [blame] | 89 | compiler_options_.reset(new CompilerOptions()); |
Nicolas Geoffray | 62a2f27 | 2017-11-10 16:46:43 +0000 | [diff] [blame] | 90 | // Special case max code units for inlining, whose default is "unset" (implictly |
Roland Levillain | ef07132 | 2018-04-17 14:16:49 +0100 | [diff] [blame] | 91 | // meaning no limit). Do this before parsing the actual passed options. |
Nicolas Geoffray | 62a2f27 | 2017-11-10 16:46:43 +0000 | [diff] [blame] | 92 | compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 93 | { |
| 94 | std::string error_msg; |
| 95 | if (!compiler_options_->ParseCompilerOptions(Runtime::Current()->GetCompilerOptions(), |
| 96 | true /* ignore_unrecognized */, |
| 97 | &error_msg)) { |
| 98 | LOG(FATAL) << error_msg; |
| 99 | UNREACHABLE(); |
| 100 | } |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 101 | } |
Vladimir Marko | 2fad527 | 2017-05-17 12:57:18 +0100 | [diff] [blame] | 102 | // JIT is never PIC, no matter what the runtime compiler options specify. |
| 103 | compiler_options_->SetNonPic(); |
| 104 | |
Nicolas Geoffray | 57c4704 | 2017-06-29 11:31:39 +0100 | [diff] [blame] | 105 | // Set debuggability based on the runtime value. |
| 106 | compiler_options_->SetDebuggable(Runtime::Current()->IsJavaDebuggable()); |
| 107 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 108 | const InstructionSet instruction_set = compiler_options_->GetInstructionSet(); |
| 109 | if (kRuntimeISA == InstructionSet::kArm) { |
| 110 | DCHECK_EQ(instruction_set, InstructionSet::kThumb2); |
| 111 | } else { |
| 112 | DCHECK_EQ(instruction_set, kRuntimeISA); |
| 113 | } |
| 114 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features; |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 115 | for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) { |
| 116 | VLOG(compiler) << "JIT compiler option " << option; |
| 117 | std::string error_msg; |
| 118 | if (option.starts_with("--instruction-set-variant=")) { |
| 119 | StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); |
| 120 | VLOG(compiler) << "JIT instruction set variant " << str; |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 121 | instruction_set_features = InstructionSetFeatures::FromVariant( |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 122 | instruction_set, str.as_string(), &error_msg); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 123 | if (instruction_set_features == nullptr) { |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 124 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 125 | } |
| 126 | } else if (option.starts_with("--instruction-set-features=")) { |
| 127 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 128 | VLOG(compiler) << "JIT instruction set features " << str; |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 129 | if (instruction_set_features == nullptr) { |
| 130 | instruction_set_features = InstructionSetFeatures::FromVariant( |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 131 | instruction_set, "default", &error_msg); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 132 | if (instruction_set_features == nullptr) { |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 133 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 134 | } |
| 135 | } |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 136 | instruction_set_features = |
| 137 | instruction_set_features->AddFeaturesFromString(str.as_string(), &error_msg); |
| 138 | if (instruction_set_features == nullptr) { |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 139 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 140 | } |
| 141 | } |
| 142 | } |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 143 | if (instruction_set_features == nullptr) { |
| 144 | instruction_set_features = InstructionSetFeatures::FromCppDefines(); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 145 | } |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 146 | compiler_options_->instruction_set_features_ = std::move(instruction_set_features); |
| 147 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 148 | compiler_driver_.reset(new CompilerDriver( |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 149 | compiler_options_.get(), |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 150 | /* verification_results */ nullptr, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 151 | Compiler::kOptimizing, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 152 | /* image_classes */ nullptr, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 153 | /* thread_count */ 1, |
Vladimir Marko | 1a2a5cd | 2018-11-07 15:39:48 +0000 | [diff] [blame^] | 154 | /* swap_fd */ -1)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 155 | // Disable dedupe so we can remove compiled methods. |
| 156 | compiler_driver_->SetDedupeEnabled(false); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 157 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 158 | size_t thread_count = compiler_driver_->GetThreadCount(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 159 | if (compiler_options_->GetGenerateDebugInfo()) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 160 | DCHECK_EQ(thread_count, 1u) |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 161 | << "Generating debug info only works with one compiler thread"; |
xueliang.zhong | 383b57d | 2016-10-04 11:19:17 +0100 | [diff] [blame] | 162 | jit_logger_.reset(new JitLogger()); |
| 163 | jit_logger_->OpenLog(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 164 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | JitCompiler::~JitCompiler() { |
xueliang.zhong | 383b57d | 2016-10-04 11:19:17 +0100 | [diff] [blame] | 168 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 169 | jit_logger_->CloseLog(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 170 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 173 | bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) { |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 174 | SCOPED_TRACE << "JIT compiling " << method->PrettyMethod(); |
| 175 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 176 | DCHECK(!method->IsProxyMethod()); |
Nicolas Geoffray | 23ddfe8 | 2017-06-07 14:09:43 +0100 | [diff] [blame] | 177 | DCHECK(method->GetDeclaringClass()->IsResolved()); |
| 178 | |
Nicolas Geoffray | 3d69922 | 2017-09-20 15:15:20 +0100 | [diff] [blame] | 179 | TimingLogger logger( |
| 180 | "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 181 | self->AssertNoPendingException(); |
| 182 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 183 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 184 | // Do the compilation. |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 185 | bool success = false; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 186 | { |
| 187 | TimingLogger::ScopedTiming t2("Compiling", &logger); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 188 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
Nicolas Geoffray | 01db5f7 | 2017-07-19 15:05:49 +0100 | [diff] [blame] | 189 | success = compiler_driver_->GetCompiler()->JitCompile( |
Nicolas Geoffray | acc56ac | 2018-10-09 08:45:24 +0100 | [diff] [blame] | 190 | self, code_cache, method, /* baseline= */ false, osr, jit_logger_.get()); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 191 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 192 | |
| 193 | // Trim maps to reduce memory usage. |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 194 | // TODO: move this to an idle phase. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 195 | { |
| 196 | TimingLogger::ScopedTiming t2("TrimMaps", &logger); |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 197 | runtime->GetJitArenaPool()->TrimMaps(); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 198 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 199 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 200 | runtime->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 201 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 204 | } // namespace jit |
| 205 | } // namespace art |