blob: 9df4e84a4459f5c2511d63cae3b06de9c7899892 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
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 Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080020#include "arch/instruction_set.h"
21#include "arch/instruction_set_features.h"
Mathieu Chartier085fc872015-10-15 18:19:01 -070022#include "base/stringpiece.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070024#include "base/timing_logger.h"
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000025#include "base/unix_file/fd_file.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080026#include "compiler_callbacks.h"
27#include "dex/pass_manager.h"
28#include "dex/quick_compiler_callbacks.h"
29#include "driver/compiler_driver.h"
30#include "driver/compiler_options.h"
31#include "jit/jit.h"
32#include "jit/jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010034#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035#include "object_lock.h"
36#include "thread_list.h"
37#include "verifier/method_verifier-inl.h"
38
39namespace art {
40namespace jit {
41
42JitCompiler* JitCompiler::Create() {
43 return new JitCompiler();
44}
45
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000046extern "C" void* jit_load(CompilerCallbacks** callbacks, bool* generate_debug_info) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080047 VLOG(jit) << "loading jit compiler";
48 auto* const jit_compiler = JitCompiler::Create();
49 CHECK(jit_compiler != nullptr);
50 *callbacks = jit_compiler->GetCompilerCallbacks();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000051 *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052 VLOG(jit) << "Done loading jit compiler";
53 return jit_compiler;
54}
55
56extern "C" void jit_unload(void* handle) {
57 DCHECK(handle != nullptr);
58 delete reinterpret_cast<JitCompiler*>(handle);
59}
60
Mathieu Chartiere401d142015-04-22 13:56:20 -070061extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -070062 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080063 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
64 DCHECK(jit_compiler != nullptr);
65 return jit_compiler->CompileMethod(self, method);
66}
67
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000068// Callers of this method assume it has NO_RETURN.
69NO_RETURN static void Usage(const char* fmt, ...) {
70 va_list ap;
71 va_start(ap, fmt);
72 std::string error;
73 StringAppendV(&error, fmt, ap);
74 LOG(FATAL) << error;
75 va_end(ap);
76 exit(EXIT_FAILURE);
77}
78
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080079JitCompiler::JitCompiler() : total_time_(0) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080 compiler_options_.reset(new CompilerOptions(
81 CompilerOptions::kDefaultCompilerFilter,
82 CompilerOptions::kDefaultHugeMethodThreshold,
83 CompilerOptions::kDefaultLargeMethodThreshold,
84 CompilerOptions::kDefaultSmallMethodThreshold,
85 CompilerOptions::kDefaultTinyMethodThreshold,
86 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010087 CompilerOptions::kDefaultInlineDepthLimit,
88 CompilerOptions::kDefaultInlineMaxCodeUnits,
Jeff Haodcdc85b2015-12-04 14:06:18 -080089 /* no_inline_from */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010090 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010092 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +010093 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010094 /* implicit_null_checks */ true,
95 /* implicit_so_checks */ true,
96 /* implicit_suspend_checks */ false,
97 /* pic */ true, // TODO: Support non-PIC in optimizing.
98 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010099 /* init_failure_output */ nullptr,
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000100 /* abort_on_hard_verifier_failure */ false,
101 /* dump_cfg_file_name */ "",
102 /* dump_cfg_append */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000103 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
104 compiler_options_->ParseCompilerOption(argument, Usage);
105 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800106 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700107 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
108 VLOG(compiler) << "JIT compiler option " << option;
109 std::string error_msg;
110 if (option.starts_with("--instruction-set-variant=")) {
111 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
112 VLOG(compiler) << "JIT instruction set variant " << str;
113 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
114 instruction_set, str.as_string(), &error_msg));
115 if (instruction_set_features_ == nullptr) {
116 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
117 }
118 } else if (option.starts_with("--instruction-set-features=")) {
119 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
120 VLOG(compiler) << "JIT instruction set features " << str;
121 if (instruction_set_features_.get() == nullptr) {
122 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
123 instruction_set, "default", &error_msg));
124 if (instruction_set_features_ == nullptr) {
125 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
126 }
127 }
128 instruction_set_features_.reset(
129 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
130 if (instruction_set_features_ == nullptr) {
131 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
132 }
133 }
134 }
135 if (instruction_set_features_ == nullptr) {
136 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
137 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800138 cumulative_logger_.reset(new CumulativeLogger("jit times"));
139 verification_results_.reset(new VerificationResults(compiler_options_.get()));
140 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
141 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700142 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700143 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800144 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100145 compiler_options_.get(),
146 verification_results_.get(),
147 method_inliner_map_.get(),
148 Compiler::kOptimizing,
149 instruction_set,
150 instruction_set_features_.get(),
151 /* image */ false,
152 /* image_classes */ nullptr,
153 /* compiled_classes */ nullptr,
154 /* compiled_methods */ nullptr,
155 /* thread_count */ 1,
156 /* dump_stats */ false,
157 /* dump_passes */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100158 cumulative_logger_.get(),
159 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200160 /* dex to oat map */ nullptr,
161 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800162 // Disable dedupe so we can remove compiled methods.
163 compiler_driver_->SetDedupeEnabled(false);
164 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000165
166 if (compiler_options_->GetGenerateDebugInfo()) {
167#ifdef __ANDROID__
168 const char* prefix = GetAndroidData();
169#else
170 const char* prefix = "/tmp";
171#endif
172 DCHECK_EQ(compiler_driver_->GetThreadCount(), 1u)
173 << "Generating debug info only works with one compiler thread";
174 std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map";
175 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
176 if (perf_file_ == nullptr) {
177 LOG(FATAL) << "Could not create perf file at " << perf_filename;
178 }
179 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180}
181
182JitCompiler::~JitCompiler() {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000183 if (perf_file_ != nullptr) {
184 UNUSED(perf_file_->Flush());
185 UNUSED(perf_file_->Close());
186 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800187}
188
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700190 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700191 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192 StackHandleScope<2> hs(self);
193 self->AssertNoPendingException();
194 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100195
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100196 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100198 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
199 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
200 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800201 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100202
203 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000204 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700205 {
206 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000207 // If we get a request to compile a proxy method, we pass the actual Java method
208 // of that proxy method, as the compiler does not expect a proxy method.
209 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100210 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000211 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000212 if (success && compiler_options_->GetGenerateDebugInfo()) {
213 const void* ptr = method_to_compile->GetEntryPointFromQuickCompiledCode();
214 std::ostringstream stream;
215 stream << std::hex
216 << reinterpret_cast<uintptr_t>(ptr)
217 << " "
218 << code_cache->GetMemorySizeOfCodePointer(ptr)
219 << " "
220 << PrettyMethod(method_to_compile)
221 << std::endl;
222 std::string str = stream.str();
223 bool res = perf_file_->WriteFully(str.c_str(), str.size());
224 CHECK(res);
225 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700226 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100227
228 // Trim maps to reduce memory usage.
229 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700230 {
231 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700232 runtime->GetArenaPool()->TrimMaps();
233 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100234
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800235 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700236 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000237 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800238}
239
240CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
241 return callbacks_.get();
242}
243
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800244} // namespace jit
245} // namespace art