blob: 339ed7375fc89992709edac15eabde678d293ce3 [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
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000063extern "C" bool jit_compile_method(
64 void* handle, ArtMethod* method, Thread* self, bool osr)
Mathieu Chartier90443472015-07-16 20:32:27 -070065 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
67 DCHECK(jit_compiler != nullptr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000068 return jit_compiler->CompileMethod(self, method, osr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069}
70
Tamas Berghammerfffbee42016-01-15 13:09:34 +000071extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
Tamas Berghammer160e6df2016-01-05 14:29:02 +000072 SHARED_REQUIRES(Locks::mutator_lock_) {
73 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
74 DCHECK(jit_compiler != nullptr);
75 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +000076 const ArrayRef<mirror::Class*> types_array(types, count);
77 ArrayRef<const uint8_t> elf_file = dwarf::WriteDebugElfFileForClasses(kRuntimeISA, types_array);
Tamas Berghammer160e6df2016-01-05 14:29:02 +000078 CreateJITCodeEntry(std::unique_ptr<const uint8_t[]>(elf_file.data()), elf_file.size());
79 }
80}
81
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000082// Callers of this method assume it has NO_RETURN.
83NO_RETURN static void Usage(const char* fmt, ...) {
84 va_list ap;
85 va_start(ap, fmt);
86 std::string error;
87 StringAppendV(&error, fmt, ap);
88 LOG(FATAL) << error;
89 va_end(ap);
90 exit(EXIT_FAILURE);
91}
92
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080093JitCompiler::JitCompiler() : total_time_(0) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080094 compiler_options_.reset(new CompilerOptions(
95 CompilerOptions::kDefaultCompilerFilter,
96 CompilerOptions::kDefaultHugeMethodThreshold,
97 CompilerOptions::kDefaultLargeMethodThreshold,
98 CompilerOptions::kDefaultSmallMethodThreshold,
99 CompilerOptions::kDefaultTinyMethodThreshold,
100 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +0100101 CompilerOptions::kDefaultInlineDepthLimit,
102 CompilerOptions::kDefaultInlineMaxCodeUnits,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800103 /* no_inline_from */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100104 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800105 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100106 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +0100107 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100108 /* implicit_null_checks */ true,
109 /* implicit_so_checks */ true,
110 /* implicit_suspend_checks */ false,
111 /* pic */ true, // TODO: Support non-PIC in optimizing.
112 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100113 /* init_failure_output */ nullptr,
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000114 /* abort_on_hard_verifier_failure */ false,
115 /* dump_cfg_file_name */ "",
Andreas Gampeace0dc12016-01-20 13:33:13 -0800116 /* dump_cfg_append */ false,
117 /* force_determinism */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000118 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
119 compiler_options_->ParseCompilerOption(argument, Usage);
120 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800121 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700122 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
123 VLOG(compiler) << "JIT compiler option " << option;
124 std::string error_msg;
125 if (option.starts_with("--instruction-set-variant=")) {
126 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
127 VLOG(compiler) << "JIT instruction set variant " << str;
128 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
129 instruction_set, str.as_string(), &error_msg));
130 if (instruction_set_features_ == nullptr) {
131 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
132 }
133 } else if (option.starts_with("--instruction-set-features=")) {
134 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
135 VLOG(compiler) << "JIT instruction set features " << str;
136 if (instruction_set_features_.get() == nullptr) {
137 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
138 instruction_set, "default", &error_msg));
139 if (instruction_set_features_ == nullptr) {
140 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
141 }
142 }
143 instruction_set_features_.reset(
144 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
145 if (instruction_set_features_ == nullptr) {
146 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
147 }
148 }
149 }
150 if (instruction_set_features_ == nullptr) {
151 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
152 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153 cumulative_logger_.reset(new CumulativeLogger("jit times"));
154 verification_results_.reset(new VerificationResults(compiler_options_.get()));
155 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
156 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700157 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700158 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800159 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100160 compiler_options_.get(),
161 verification_results_.get(),
162 method_inliner_map_.get(),
163 Compiler::kOptimizing,
164 instruction_set,
165 instruction_set_features_.get(),
166 /* image */ false,
167 /* image_classes */ nullptr,
168 /* compiled_classes */ nullptr,
169 /* compiled_methods */ nullptr,
170 /* thread_count */ 1,
171 /* dump_stats */ false,
172 /* dump_passes */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100173 cumulative_logger_.get(),
174 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200175 /* dex to oat map */ nullptr,
176 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177 // Disable dedupe so we can remove compiled methods.
178 compiler_driver_->SetDedupeEnabled(false);
179 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000180
181 if (compiler_options_->GetGenerateDebugInfo()) {
182#ifdef __ANDROID__
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000183 const char* prefix = "/data/misc/trace";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000184#else
185 const char* prefix = "/tmp";
186#endif
187 DCHECK_EQ(compiler_driver_->GetThreadCount(), 1u)
188 << "Generating debug info only works with one compiler thread";
189 std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map";
190 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
191 if (perf_file_ == nullptr) {
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000192 LOG(ERROR) << "Could not create perf file at " << perf_filename <<
193 " Are you on a user build? Perf only works on userdebug/eng builds";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000194 }
195 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196}
197
198JitCompiler::~JitCompiler() {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000199 if (perf_file_ != nullptr) {
200 UNUSED(perf_file_->Flush());
201 UNUSED(perf_file_->Close());
202 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800203}
204
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000205bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700206 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700207 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800208 StackHandleScope<2> hs(self);
209 self->AssertNoPendingException();
210 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100211
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100212 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100214 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
215 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
216 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100218
219 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000220 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700221 {
222 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000223 // If we get a request to compile a proxy method, we pass the actual Java method
224 // of that proxy method, as the compiler does not expect a proxy method.
225 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100226 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000227 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile, osr);
228 if (success && (perf_file_ != nullptr)) {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000229 const void* ptr = method_to_compile->GetEntryPointFromQuickCompiledCode();
230 std::ostringstream stream;
231 stream << std::hex
232 << reinterpret_cast<uintptr_t>(ptr)
233 << " "
234 << code_cache->GetMemorySizeOfCodePointer(ptr)
235 << " "
236 << PrettyMethod(method_to_compile)
237 << std::endl;
238 std::string str = stream.str();
239 bool res = perf_file_->WriteFully(str.c_str(), str.size());
240 CHECK(res);
241 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700242 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100243
244 // Trim maps to reduce memory usage.
245 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700246 {
247 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700248 runtime->GetArenaPool()->TrimMaps();
249 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100250
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800251 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700252 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000253 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800254}
255
256CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
257 return callbacks_.get();
258}
259
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800260} // namespace jit
261} // namespace art