blob: 083ba8f343a10cd1dddb7c6dc0263cc917750f3f [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,
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000113 /* abort_on_hard_verifier_failure */ false,
114 /* dump_cfg_file_name */ "",
115 /* dump_cfg_append */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000116 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
117 compiler_options_->ParseCompilerOption(argument, Usage);
118 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800119 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700120 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
121 VLOG(compiler) << "JIT compiler option " << option;
122 std::string error_msg;
123 if (option.starts_with("--instruction-set-variant=")) {
124 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
125 VLOG(compiler) << "JIT instruction set variant " << str;
126 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
127 instruction_set, str.as_string(), &error_msg));
128 if (instruction_set_features_ == nullptr) {
129 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
130 }
131 } else if (option.starts_with("--instruction-set-features=")) {
132 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
133 VLOG(compiler) << "JIT instruction set features " << str;
134 if (instruction_set_features_.get() == nullptr) {
135 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
136 instruction_set, "default", &error_msg));
137 if (instruction_set_features_ == nullptr) {
138 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
139 }
140 }
141 instruction_set_features_.reset(
142 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
143 if (instruction_set_features_ == nullptr) {
144 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
145 }
146 }
147 }
148 if (instruction_set_features_ == nullptr) {
149 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
150 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800151 cumulative_logger_.reset(new CumulativeLogger("jit times"));
152 verification_results_.reset(new VerificationResults(compiler_options_.get()));
153 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
154 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700155 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700156 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800157 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100158 compiler_options_.get(),
159 verification_results_.get(),
160 method_inliner_map_.get(),
161 Compiler::kOptimizing,
162 instruction_set,
163 instruction_set_features_.get(),
164 /* image */ false,
165 /* image_classes */ nullptr,
166 /* compiled_classes */ nullptr,
167 /* compiled_methods */ nullptr,
168 /* thread_count */ 1,
169 /* dump_stats */ false,
170 /* dump_passes */ 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__
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000181 const char* prefix = "/data/misc/trace";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000182#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) {
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000190 LOG(ERROR) << "Could not create perf file at " << perf_filename <<
191 " Are you on a user build? Perf only works on userdebug/eng builds";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000192 }
193 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800194}
195
196JitCompiler::~JitCompiler() {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000197 if (perf_file_ != nullptr) {
198 UNUSED(perf_file_->Flush());
199 UNUSED(perf_file_->Close());
200 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800201}
202
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700204 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700205 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800206 StackHandleScope<2> hs(self);
207 self->AssertNoPendingException();
208 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100209
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100210 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100212 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
213 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
214 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800215 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100216
217 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000218 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700219 {
220 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000221 // If we get a request to compile a proxy method, we pass the actual Java method
222 // of that proxy method, as the compiler does not expect a proxy method.
223 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100224 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000225 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile);
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000226 if (success && perf_file_ != nullptr) {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000227 const void* ptr = method_to_compile->GetEntryPointFromQuickCompiledCode();
228 std::ostringstream stream;
229 stream << std::hex
230 << reinterpret_cast<uintptr_t>(ptr)
231 << " "
232 << code_cache->GetMemorySizeOfCodePointer(ptr)
233 << " "
234 << PrettyMethod(method_to_compile)
235 << std::endl;
236 std::string str = stream.str();
237 bool res = perf_file_->WriteFully(str.c_str(), str.size());
238 CHECK(res);
239 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700240 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100241
242 // Trim maps to reduce memory usage.
243 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700244 {
245 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700246 runtime->GetArenaPool()->TrimMaps();
247 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100248
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800249 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700250 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000251 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800252}
253
254CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
255 return callbacks_.get();
256}
257
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800258} // namespace jit
259} // namespace art