blob: 8fdbf4a3f73e0dfd8ce2e6b2c0129143f9f377fb [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"
Tamas Berghammer160e6df2016-01-05 14:29:02 +000031#include "elf_writer_debug.h"
32#include "jit/debugger_interface.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "jit/jit.h"
34#include "jit/jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010036#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037#include "object_lock.h"
38#include "thread_list.h"
39#include "verifier/method_verifier-inl.h"
40
41namespace art {
42namespace jit {
43
44JitCompiler* JitCompiler::Create() {
45 return new JitCompiler();
46}
47
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000048extern "C" void* jit_load(CompilerCallbacks** callbacks, bool* generate_debug_info) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080049 VLOG(jit) << "loading jit compiler";
50 auto* const jit_compiler = JitCompiler::Create();
51 CHECK(jit_compiler != nullptr);
52 *callbacks = jit_compiler->GetCompilerCallbacks();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000053 *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080054 VLOG(jit) << "Done loading jit compiler";
55 return jit_compiler;
56}
57
58extern "C" void jit_unload(void* handle) {
59 DCHECK(handle != nullptr);
60 delete reinterpret_cast<JitCompiler*>(handle);
61}
62
Mathieu Chartiere401d142015-04-22 13:56:20 -070063extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -070064 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080065 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
66 DCHECK(jit_compiler != nullptr);
67 return jit_compiler->CompileMethod(self, method);
68}
69
Tamas Berghammerfffbee42016-01-15 13:09:34 +000070extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
Tamas Berghammer160e6df2016-01-05 14:29:02 +000071 SHARED_REQUIRES(Locks::mutator_lock_) {
72 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
73 DCHECK(jit_compiler != nullptr);
74 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +000075 const ArrayRef<mirror::Class*> types_array(types, count);
76 ArrayRef<const uint8_t> elf_file = dwarf::WriteDebugElfFileForClasses(kRuntimeISA, types_array);
Tamas Berghammer160e6df2016-01-05 14:29:02 +000077 CreateJITCodeEntry(std::unique_ptr<const uint8_t[]>(elf_file.data()), elf_file.size());
78 }
79}
80
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000081// Callers of this method assume it has NO_RETURN.
82NO_RETURN static void Usage(const char* fmt, ...) {
83 va_list ap;
84 va_start(ap, fmt);
85 std::string error;
86 StringAppendV(&error, fmt, ap);
87 LOG(FATAL) << error;
88 va_end(ap);
89 exit(EXIT_FAILURE);
90}
91
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080092JitCompiler::JitCompiler() : total_time_(0) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080093 compiler_options_.reset(new CompilerOptions(
94 CompilerOptions::kDefaultCompilerFilter,
95 CompilerOptions::kDefaultHugeMethodThreshold,
96 CompilerOptions::kDefaultLargeMethodThreshold,
97 CompilerOptions::kDefaultSmallMethodThreshold,
98 CompilerOptions::kDefaultTinyMethodThreshold,
99 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +0100100 CompilerOptions::kDefaultInlineDepthLimit,
101 CompilerOptions::kDefaultInlineMaxCodeUnits,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800102 /* no_inline_from */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100103 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800104 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100105 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +0100106 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100107 /* implicit_null_checks */ true,
108 /* implicit_so_checks */ true,
109 /* implicit_suspend_checks */ false,
110 /* pic */ true, // TODO: Support non-PIC in optimizing.
111 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100112 /* init_failure_output */ nullptr,
113 /* abort_on_hard_verifier_failure */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000114 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
115 compiler_options_->ParseCompilerOption(argument, Usage);
116 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700118 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
119 VLOG(compiler) << "JIT compiler option " << option;
120 std::string error_msg;
121 if (option.starts_with("--instruction-set-variant=")) {
122 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
123 VLOG(compiler) << "JIT instruction set variant " << str;
124 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
125 instruction_set, str.as_string(), &error_msg));
126 if (instruction_set_features_ == nullptr) {
127 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
128 }
129 } else if (option.starts_with("--instruction-set-features=")) {
130 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
131 VLOG(compiler) << "JIT instruction set features " << str;
132 if (instruction_set_features_.get() == nullptr) {
133 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
134 instruction_set, "default", &error_msg));
135 if (instruction_set_features_ == nullptr) {
136 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
137 }
138 }
139 instruction_set_features_.reset(
140 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
141 if (instruction_set_features_ == nullptr) {
142 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
143 }
144 }
145 }
146 if (instruction_set_features_ == nullptr) {
147 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
148 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800149 cumulative_logger_.reset(new CumulativeLogger("jit times"));
150 verification_results_.reset(new VerificationResults(compiler_options_.get()));
151 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
152 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700153 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700154 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100156 compiler_options_.get(),
157 verification_results_.get(),
158 method_inliner_map_.get(),
159 Compiler::kOptimizing,
160 instruction_set,
161 instruction_set_features_.get(),
162 /* image */ false,
163 /* image_classes */ nullptr,
164 /* compiled_classes */ nullptr,
165 /* compiled_methods */ nullptr,
166 /* thread_count */ 1,
167 /* dump_stats */ false,
168 /* dump_passes */ false,
169 /* dump_cfg_file_name */ "",
Calin Juravle87000a92015-08-24 15:34:44 +0100170 /* dump_cfg_append */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100171 cumulative_logger_.get(),
172 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200173 /* dex to oat map */ nullptr,
174 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175 // Disable dedupe so we can remove compiled methods.
176 compiler_driver_->SetDedupeEnabled(false);
177 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000178
179 if (compiler_options_->GetGenerateDebugInfo()) {
180#ifdef __ANDROID__
181 const char* prefix = GetAndroidData();
182#else
183 const char* prefix = "/tmp";
184#endif
185 DCHECK_EQ(compiler_driver_->GetThreadCount(), 1u)
186 << "Generating debug info only works with one compiler thread";
187 std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map";
188 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
189 if (perf_file_ == nullptr) {
190 LOG(FATAL) << "Could not create perf file at " << perf_filename;
191 }
192 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800193}
194
195JitCompiler::~JitCompiler() {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000196 if (perf_file_ != nullptr) {
197 UNUSED(perf_file_->Flush());
198 UNUSED(perf_file_->Close());
199 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800200}
201
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700203 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700204 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205 StackHandleScope<2> hs(self);
206 self->AssertNoPendingException();
207 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100208
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100209 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100211 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
212 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
213 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800214 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100215
216 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000217 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700218 {
219 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000220 // If we get a request to compile a proxy method, we pass the actual Java method
221 // of that proxy method, as the compiler does not expect a proxy method.
222 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100223 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000224 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000225 if (success && compiler_options_->GetGenerateDebugInfo()) {
226 const void* ptr = method_to_compile->GetEntryPointFromQuickCompiledCode();
227 std::ostringstream stream;
228 stream << std::hex
229 << reinterpret_cast<uintptr_t>(ptr)
230 << " "
231 << code_cache->GetMemorySizeOfCodePointer(ptr)
232 << " "
233 << PrettyMethod(method_to_compile)
234 << std::endl;
235 std::string str = stream.str();
236 bool res = perf_file_->WriteFully(str.c_str(), str.size());
237 CHECK(res);
238 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700239 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100240
241 // Trim maps to reduce memory usage.
242 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700243 {
244 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700245 runtime->GetArenaPool()->TrimMaps();
246 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100247
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800248 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700249 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000250 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800251}
252
253CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
254 return callbacks_.get();
255}
256
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800257} // namespace jit
258} // namespace art