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 |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 25 | #include "base/string_view_cpp20.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" |
Vladimir Marko | 038924b | 2019-02-19 15:09:35 +0000 | [diff] [blame] | 29 | #include "compiler.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_options.h" |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 32 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 33 | #include "jit/jit.h" |
| 34 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 35 | #include "jit/jit_logger.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 | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 44 | void JitCompiler::ParseCompilerOptions() { |
Nicolas Geoffray | 62a2f27 | 2017-11-10 16:46:43 +0000 | [diff] [blame] | 45 | // Special case max code units for inlining, whose default is "unset" (implictly |
Roland Levillain | ef07132 | 2018-04-17 14:16:49 +0100 | [diff] [blame] | 46 | // meaning no limit). Do this before parsing the actual passed options. |
Nicolas Geoffray | 62a2f27 | 2017-11-10 16:46:43 +0000 | [diff] [blame] | 47 | compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits); |
Vladimir Marko | 6be1dbd | 2018-11-13 13:09:51 +0000 | [diff] [blame] | 48 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 49 | { |
| 50 | std::string error_msg; |
Vladimir Marko | 6be1dbd | 2018-11-13 13:09:51 +0000 | [diff] [blame] | 51 | if (!compiler_options_->ParseCompilerOptions(runtime->GetCompilerOptions(), |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 52 | /*ignore_unrecognized=*/ true, |
| 53 | &error_msg)) { |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 54 | LOG(FATAL) << error_msg; |
| 55 | UNREACHABLE(); |
| 56 | } |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 57 | } |
Vladimir Marko | 695348f | 2020-05-19 14:42:02 +0100 | [diff] [blame] | 58 | // Set to appropriate JIT compiler type. |
| 59 | compiler_options_->compiler_type_ = runtime->IsZygote() |
| 60 | ? CompilerOptions::CompilerType::kSharedCodeJitCompiler |
| 61 | : CompilerOptions::CompilerType::kJitCompiler; |
Vladimir Marko | 2fad527 | 2017-05-17 12:57:18 +0100 | [diff] [blame] | 62 | // JIT is never PIC, no matter what the runtime compiler options specify. |
| 63 | compiler_options_->SetNonPic(); |
| 64 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 65 | // If the options don't provide whether we generate debuggable code, set |
| 66 | // debuggability based on the runtime value. |
| 67 | if (!compiler_options_->GetDebuggable()) { |
| 68 | compiler_options_->SetDebuggable(runtime->IsJavaDebuggable()); |
| 69 | } |
Nicolas Geoffray | 57c4704 | 2017-06-29 11:31:39 +0100 | [diff] [blame] | 70 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 71 | const InstructionSet instruction_set = compiler_options_->GetInstructionSet(); |
| 72 | if (kRuntimeISA == InstructionSet::kArm) { |
| 73 | DCHECK_EQ(instruction_set, InstructionSet::kThumb2); |
| 74 | } else { |
| 75 | DCHECK_EQ(instruction_set, kRuntimeISA); |
| 76 | } |
| 77 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features; |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 78 | for (const std::string& option : runtime->GetCompilerOptions()) { |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 79 | VLOG(compiler) << "JIT compiler option " << option; |
| 80 | std::string error_msg; |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 81 | if (StartsWith(option, "--instruction-set-variant=")) { |
| 82 | const char* str = option.c_str() + strlen("--instruction-set-variant="); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 83 | VLOG(compiler) << "JIT instruction set variant " << str; |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 84 | instruction_set_features = InstructionSetFeatures::FromVariant( |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 85 | instruction_set, str, &error_msg); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 86 | if (instruction_set_features == nullptr) { |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 87 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 88 | } |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 89 | } else if (StartsWith(option, "--instruction-set-features=")) { |
| 90 | const char* str = option.c_str() + strlen("--instruction-set-features="); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 91 | VLOG(compiler) << "JIT instruction set features " << str; |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 92 | if (instruction_set_features == nullptr) { |
| 93 | instruction_set_features = InstructionSetFeatures::FromVariant( |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 94 | instruction_set, "default", &error_msg); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 95 | if (instruction_set_features == nullptr) { |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 96 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 97 | } |
| 98 | } |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 99 | instruction_set_features = |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 100 | instruction_set_features->AddFeaturesFromString(str, &error_msg); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 101 | if (instruction_set_features == nullptr) { |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 102 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 103 | } |
| 104 | } |
| 105 | } |
xueliang.zhong | 7f88c1a | 2018-11-06 11:42:41 +0000 | [diff] [blame] | 106 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 107 | if (instruction_set_features == nullptr) { |
xueliang.zhong | 7f88c1a | 2018-11-06 11:42:41 +0000 | [diff] [blame] | 108 | // '--instruction-set-features/--instruction-set-variant' were not used. |
| 109 | // Use build-time defined features. |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 110 | instruction_set_features = InstructionSetFeatures::FromCppDefines(); |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 111 | } |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 112 | compiler_options_->instruction_set_features_ = std::move(instruction_set_features); |
| 113 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 114 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 115 | jit_logger_.reset(new JitLogger()); |
| 116 | jit_logger_->OpenLog(); |
| 117 | } |
| 118 | } |
| 119 | |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 120 | extern "C" JitCompilerInterface* jit_load() { |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 121 | VLOG(jit) << "Create jit compiler"; |
| 122 | auto* const jit_compiler = JitCompiler::Create(); |
| 123 | CHECK(jit_compiler != nullptr); |
| 124 | VLOG(jit) << "Done creating jit compiler"; |
| 125 | return jit_compiler; |
| 126 | } |
| 127 | |
David Srbecky | 9ac8e43 | 2019-08-13 13:16:13 +0100 | [diff] [blame] | 128 | void JitCompiler::TypesLoaded(mirror::Class** types, size_t count) { |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 129 | const CompilerOptions& compiler_options = GetCompilerOptions(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 130 | if (compiler_options.GetGenerateDebugInfo()) { |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 131 | InstructionSet isa = compiler_options.GetInstructionSet(); |
| 132 | const InstructionSetFeatures* features = compiler_options.GetInstructionSetFeatures(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 133 | const ArrayRef<mirror::Class*> types_array(types, count); |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 134 | std::vector<uint8_t> elf_file = |
| 135 | debug::WriteDebugElfFileForClasses(isa, features, types_array); |
| 136 | |
| 137 | // NB: Don't allow packing since it would remove non-backtrace data. |
David Srbecky | 9ac8e43 | 2019-08-13 13:16:13 +0100 | [diff] [blame] | 138 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 139 | AddNativeDebugInfoForJit(/*code_ptr=*/ nullptr, elf_file, /*allow_packing=*/ false); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 143 | bool JitCompiler::GenerateDebugInfo() { |
| 144 | return GetCompilerOptions().GetGenerateDebugInfo(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 145 | } |
| 146 | |
David Srbecky | e09b87e | 2019-08-19 21:31:31 +0100 | [diff] [blame] | 147 | std::vector<uint8_t> JitCompiler::PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files, |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 148 | ArrayRef<const void*> removed_symbols, |
| 149 | bool compress, |
| 150 | /*out*/ size_t* num_symbols) { |
| 151 | return debug::PackElfFileForJIT(elf_files, removed_symbols, compress, num_symbols); |
| 152 | } |
| 153 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 154 | JitCompiler::JitCompiler() { |
| 155 | compiler_options_.reset(new CompilerOptions()); |
| 156 | ParseCompilerOptions(); |
Vladimir Marko | 038924b | 2019-02-19 15:09:35 +0000 | [diff] [blame] | 157 | compiler_.reset( |
| 158 | Compiler::Create(*compiler_options_, /*storage=*/ nullptr, Compiler::kOptimizing)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | JitCompiler::~JitCompiler() { |
xueliang.zhong | 383b57d | 2016-10-04 11:19:17 +0100 | [diff] [blame] | 162 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 163 | jit_logger_->CloseLog(); |
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 | |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame] | 167 | bool JitCompiler::CompileMethod( |
Nicolas Geoffray | 0d60a2b | 2020-06-17 14:31:56 +0100 | [diff] [blame] | 168 | Thread* self, JitMemoryRegion* region, ArtMethod* method, CompilationKind compilation_kind) { |
Nicolas Geoffray | 76e52e8 | 2020-03-09 14:16:42 +0000 | [diff] [blame] | 169 | SCOPED_TRACE << "JIT compiling " |
| 170 | << method->PrettyMethod() |
Nicolas Geoffray | 0d60a2b | 2020-06-17 14:31:56 +0100 | [diff] [blame] | 171 | << " (kind=" << compilation_kind << ")"; |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 172 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 173 | DCHECK(!method->IsProxyMethod()); |
Nicolas Geoffray | 23ddfe8 | 2017-06-07 14:09:43 +0100 | [diff] [blame] | 174 | DCHECK(method->GetDeclaringClass()->IsResolved()); |
| 175 | |
Nicolas Geoffray | 3d69922 | 2017-09-20 15:15:20 +0100 | [diff] [blame] | 176 | TimingLogger logger( |
| 177 | "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 178 | self->AssertNoPendingException(); |
| 179 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 180 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 181 | // Do the compilation. |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 182 | bool success = false; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 183 | { |
Nicolas Geoffray | c473dc7 | 2020-07-03 15:04:21 +0100 | [diff] [blame] | 184 | TimingLogger::ScopedTiming t2(compilation_kind == CompilationKind::kOsr |
| 185 | ? "Compiling OSR" |
| 186 | : compilation_kind == CompilationKind::kOptimized |
| 187 | ? "Compiling optimized" |
| 188 | : "Compiling baseline", |
| 189 | &logger); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 190 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
Nicolas Geoffray | f59bc11 | 2019-04-03 10:29:29 +0100 | [diff] [blame] | 191 | uint64_t start_ns = NanoTime(); |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame] | 192 | success = compiler_->JitCompile( |
Nicolas Geoffray | 0d60a2b | 2020-06-17 14:31:56 +0100 | [diff] [blame] | 193 | self, code_cache, region, method, compilation_kind, jit_logger_.get()); |
Nicolas Geoffray | f59bc11 | 2019-04-03 10:29:29 +0100 | [diff] [blame] | 194 | uint64_t duration_ns = NanoTime() - start_ns; |
| 195 | VLOG(jit) << "Compilation of " |
| 196 | << method->PrettyMethod() |
| 197 | << " took " |
| 198 | << PrettyDuration(duration_ns); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 199 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 200 | |
| 201 | // Trim maps to reduce memory usage. |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 202 | // TODO: move this to an idle phase. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 203 | { |
| 204 | TimingLogger::ScopedTiming t2("TrimMaps", &logger); |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 205 | runtime->GetJitArenaPool()->TrimMaps(); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 206 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 207 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 208 | runtime->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 209 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 212 | } // namespace jit |
| 213 | } // namespace art |