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" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 25 | #include "compiler_callbacks.h" |
| 26 | #include "dex/pass_manager.h" |
| 27 | #include "dex/quick_compiler_callbacks.h" |
| 28 | #include "driver/compiler_driver.h" |
| 29 | #include "driver/compiler_options.h" |
| 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" |
| 36 | #include "verifier/method_verifier-inl.h" |
| 37 | |
| 38 | namespace art { |
| 39 | namespace jit { |
| 40 | |
| 41 | JitCompiler* JitCompiler::Create() { |
| 42 | return new JitCompiler(); |
| 43 | } |
| 44 | |
| 45 | extern "C" void* jit_load(CompilerCallbacks** callbacks) { |
| 46 | VLOG(jit) << "loading jit compiler"; |
| 47 | auto* const jit_compiler = JitCompiler::Create(); |
| 48 | CHECK(jit_compiler != nullptr); |
| 49 | *callbacks = jit_compiler->GetCompilerCallbacks(); |
| 50 | VLOG(jit) << "Done loading jit compiler"; |
| 51 | return jit_compiler; |
| 52 | } |
| 53 | |
| 54 | extern "C" void jit_unload(void* handle) { |
| 55 | DCHECK(handle != nullptr); |
| 56 | delete reinterpret_cast<JitCompiler*>(handle); |
| 57 | } |
| 58 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 59 | extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self) |
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); |
| 63 | return jit_compiler->CompileMethod(self, method); |
| 64 | } |
| 65 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 66 | // Callers of this method assume it has NO_RETURN. |
| 67 | NO_RETURN static void Usage(const char* fmt, ...) { |
| 68 | va_list ap; |
| 69 | va_start(ap, fmt); |
| 70 | std::string error; |
| 71 | StringAppendV(&error, fmt, ap); |
| 72 | LOG(FATAL) << error; |
| 73 | va_end(ap); |
| 74 | exit(EXIT_FAILURE); |
| 75 | } |
| 76 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 77 | JitCompiler::JitCompiler() : total_time_(0) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 78 | compiler_options_.reset(new CompilerOptions( |
| 79 | CompilerOptions::kDefaultCompilerFilter, |
| 80 | CompilerOptions::kDefaultHugeMethodThreshold, |
| 81 | CompilerOptions::kDefaultLargeMethodThreshold, |
| 82 | CompilerOptions::kDefaultSmallMethodThreshold, |
| 83 | CompilerOptions::kDefaultTinyMethodThreshold, |
| 84 | CompilerOptions::kDefaultNumDexMethodsThreshold, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 85 | CompilerOptions::kDefaultInlineDepthLimit, |
| 86 | CompilerOptions::kDefaultInlineMaxCodeUnits, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 87 | /* include_patch_information */ false, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 88 | CompilerOptions::kDefaultTopKProfileThreshold, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 89 | Runtime::Current()->IsDebuggable(), |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 90 | CompilerOptions::kDefaultGenerateDebugInfo, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 91 | /* implicit_null_checks */ true, |
| 92 | /* implicit_so_checks */ true, |
| 93 | /* implicit_suspend_checks */ false, |
| 94 | /* pic */ true, // TODO: Support non-PIC in optimizing. |
| 95 | /* verbose_methods */ nullptr, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 96 | /* init_failure_output */ nullptr, |
| 97 | /* abort_on_hard_verifier_failure */ false)); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 98 | for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) { |
| 99 | compiler_options_->ParseCompilerOption(argument, Usage); |
| 100 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 101 | const InstructionSet instruction_set = kRuntimeISA; |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 102 | for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) { |
| 103 | VLOG(compiler) << "JIT compiler option " << option; |
| 104 | std::string error_msg; |
| 105 | if (option.starts_with("--instruction-set-variant=")) { |
| 106 | StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); |
| 107 | VLOG(compiler) << "JIT instruction set variant " << str; |
| 108 | instruction_set_features_.reset(InstructionSetFeatures::FromVariant( |
| 109 | instruction_set, str.as_string(), &error_msg)); |
| 110 | if (instruction_set_features_ == nullptr) { |
| 111 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 112 | } |
| 113 | } else if (option.starts_with("--instruction-set-features=")) { |
| 114 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 115 | VLOG(compiler) << "JIT instruction set features " << str; |
| 116 | if (instruction_set_features_.get() == nullptr) { |
| 117 | instruction_set_features_.reset(InstructionSetFeatures::FromVariant( |
| 118 | instruction_set, "default", &error_msg)); |
| 119 | if (instruction_set_features_ == nullptr) { |
| 120 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 121 | } |
| 122 | } |
| 123 | instruction_set_features_.reset( |
| 124 | instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg)); |
| 125 | if (instruction_set_features_ == nullptr) { |
| 126 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | if (instruction_set_features_ == nullptr) { |
| 131 | instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines()); |
| 132 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 133 | cumulative_logger_.reset(new CumulativeLogger("jit times")); |
| 134 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 135 | method_inliner_map_.reset(new DexFileToMethodInlinerMap); |
| 136 | callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 137 | method_inliner_map_.get(), |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 138 | CompilerCallbacks::CallbackMode::kCompileApp)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 139 | compiler_driver_.reset(new CompilerDriver( |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 140 | compiler_options_.get(), |
| 141 | verification_results_.get(), |
| 142 | method_inliner_map_.get(), |
| 143 | Compiler::kOptimizing, |
| 144 | instruction_set, |
| 145 | instruction_set_features_.get(), |
| 146 | /* image */ false, |
| 147 | /* image_classes */ nullptr, |
| 148 | /* compiled_classes */ nullptr, |
| 149 | /* compiled_methods */ nullptr, |
| 150 | /* thread_count */ 1, |
| 151 | /* dump_stats */ false, |
| 152 | /* dump_passes */ false, |
| 153 | /* dump_cfg_file_name */ "", |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 154 | /* dump_cfg_append */ false, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 155 | cumulative_logger_.get(), |
| 156 | /* swap_fd */ -1, |
| 157 | /* profile_file */ "")); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 158 | // Disable dedupe so we can remove compiled methods. |
| 159 | compiler_driver_->SetDedupeEnabled(false); |
| 160 | compiler_driver_->SetSupportBootImageFixup(false); |
| 161 | } |
| 162 | |
| 163 | JitCompiler::~JitCompiler() { |
| 164 | } |
| 165 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 166 | bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 167 | TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit)); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 168 | const uint64_t start_time = NanoTime(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 169 | StackHandleScope<2> hs(self); |
| 170 | self->AssertNoPendingException(); |
| 171 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 172 | |
| 173 | // Check if the method is already compiled. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 174 | if (runtime->GetJit()->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 175 | VLOG(jit) << "Already compiled " << PrettyMethod(method); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 176 | return true; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 177 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 178 | |
| 179 | // Don't compile the method if we are supposed to be deoptimized. |
| 180 | if (runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Ensure the class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 185 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 186 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
| 187 | VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method); |
| 188 | return false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 189 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 190 | |
| 191 | // Do the compilation. |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame^] | 192 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
| 193 | bool success = false; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 194 | { |
| 195 | TimingLogger::ScopedTiming t2("Compiling", &logger); |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 196 | // If we get a request to compile a proxy method, we pass the actual Java method |
| 197 | // of that proxy method, as the compiler does not expect a proxy method. |
| 198 | ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame^] | 199 | success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 200 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 201 | |
| 202 | // Trim maps to reduce memory usage. |
| 203 | // TODO: measure how much this increases compile time. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 204 | { |
| 205 | TimingLogger::ScopedTiming t2("TrimMaps", &logger); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 206 | runtime->GetArenaPool()->TrimMaps(); |
| 207 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 208 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 209 | total_time_ += NanoTime() - start_time; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 210 | runtime->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame^] | 211 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const { |
| 215 | return callbacks_.get(); |
| 216 | } |
| 217 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 218 | } // namespace jit |
| 219 | } // namespace art |