blob: 9305877083e182a63aec0633457bab8407b1a1c2 [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
19#include "arch/instruction_set.h"
20#include "arch/instruction_set_features.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000021#include "art_method-inl.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"
David Srbecky4fda4eb2016-02-05 13:34:46 +000027#include "debug/elf_writer_debug.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080028#include "dex/pass_manager.h"
29#include "dex/quick_compiler_callbacks.h"
30#include "driver/compiler_driver.h"
31#include "driver/compiler_options.h"
Tamas Berghammer160e6df2016-01-05 14:29:02 +000032#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,
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000113 /* abort_on_hard_verifier_failure */ false,
114 /* dump_cfg_file_name */ "",
Andreas Gampeace0dc12016-01-20 13:33:13 -0800115 /* dump_cfg_append */ false,
116 /* force_determinism */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000117 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
118 compiler_options_->ParseCompilerOption(argument, Usage);
119 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700121 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
122 VLOG(compiler) << "JIT compiler option " << option;
123 std::string error_msg;
124 if (option.starts_with("--instruction-set-variant=")) {
125 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
126 VLOG(compiler) << "JIT instruction set variant " << str;
127 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
128 instruction_set, str.as_string(), &error_msg));
129 if (instruction_set_features_ == nullptr) {
130 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
131 }
132 } else if (option.starts_with("--instruction-set-features=")) {
133 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
134 VLOG(compiler) << "JIT instruction set features " << str;
135 if (instruction_set_features_.get() == nullptr) {
136 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
137 instruction_set, "default", &error_msg));
138 if (instruction_set_features_ == nullptr) {
139 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
140 }
141 }
142 instruction_set_features_.reset(
143 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
144 if (instruction_set_features_ == nullptr) {
145 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
146 }
147 }
148 }
149 if (instruction_set_features_ == nullptr) {
150 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
151 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800152 cumulative_logger_.reset(new CumulativeLogger("jit times"));
153 verification_results_.reset(new VerificationResults(compiler_options_.get()));
154 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
155 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700156 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700157 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100159 compiler_options_.get(),
160 verification_results_.get(),
161 method_inliner_map_.get(),
162 Compiler::kOptimizing,
163 instruction_set,
164 instruction_set_features_.get(),
165 /* image */ false,
166 /* image_classes */ nullptr,
167 /* compiled_classes */ nullptr,
168 /* compiled_methods */ nullptr,
169 /* thread_count */ 1,
170 /* dump_stats */ false,
171 /* dump_passes */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100172 cumulative_logger_.get(),
173 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200174 /* dex to oat map */ nullptr,
175 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800176 // Disable dedupe so we can remove compiled methods.
177 compiler_driver_->SetDedupeEnabled(false);
178 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000179
180 if (compiler_options_->GetGenerateDebugInfo()) {
181#ifdef __ANDROID__
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000182 const char* prefix = "/data/misc/trace";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000183#else
184 const char* prefix = "/tmp";
185#endif
186 DCHECK_EQ(compiler_driver_->GetThreadCount(), 1u)
187 << "Generating debug info only works with one compiler thread";
188 std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map";
189 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
190 if (perf_file_ == nullptr) {
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000191 LOG(ERROR) << "Could not create perf file at " << perf_filename <<
192 " Are you on a user build? Perf only works on userdebug/eng builds";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000193 }
194 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195}
196
197JitCompiler::~JitCompiler() {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000198 if (perf_file_ != nullptr) {
199 UNUSED(perf_file_->Flush());
200 UNUSED(perf_file_->Close());
201 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800202}
203
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700205 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700206 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800207 StackHandleScope<2> hs(self);
208 self->AssertNoPendingException();
209 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100210
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100211 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100213 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
214 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
215 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800216 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100217
218 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000219 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700220 {
221 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000222 // If we get a request to compile a proxy method, we pass the actual Java method
223 // of that proxy method, as the compiler does not expect a proxy method.
224 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100225 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000226 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile);
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000227 if (success && perf_file_ != nullptr) {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000228 const void* ptr = method_to_compile->GetEntryPointFromQuickCompiledCode();
229 std::ostringstream stream;
230 stream << std::hex
231 << reinterpret_cast<uintptr_t>(ptr)
232 << " "
233 << code_cache->GetMemorySizeOfCodePointer(ptr)
234 << " "
235 << PrettyMethod(method_to_compile)
236 << std::endl;
237 std::string str = stream.str();
238 bool res = perf_file_->WriteFully(str.c_str(), str.size());
239 CHECK(res);
240 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700241 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100242
243 // Trim maps to reduce memory usage.
244 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700245 {
246 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700247 runtime->GetArenaPool()->TrimMaps();
248 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100249
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800250 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700251 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000252 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800253}
254
255CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
256 return callbacks_.get();
257}
258
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800259} // namespace jit
260} // namespace art