blob: f83d37cdf214618c622f072a1af7ca15570739f1 [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"
David Srbeckyc5bfa972016-02-05 15:49:10 +000026#include "debug/elf_debug_writer.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "driver/compiler_driver.h"
28#include "driver/compiler_options.h"
Tamas Berghammer160e6df2016-01-05 14:29:02 +000029#include "jit/debugger_interface.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "jit/jit.h"
31#include "jit/jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "object_lock.h"
Matthew Gharrity2cd05b72016-08-03 16:57:37 -070035#include "optimizing/register_allocator.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037
38namespace art {
39namespace jit {
40
41JitCompiler* JitCompiler::Create() {
42 return new JitCompiler();
43}
44
Nicolas Geoffray5b82d332016-02-18 14:22:32 +000045extern "C" void* jit_load(bool* generate_debug_info) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046 VLOG(jit) << "loading jit compiler";
47 auto* const jit_compiler = JitCompiler::Create();
48 CHECK(jit_compiler != nullptr);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000049 *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080050 VLOG(jit) << "Done loading jit compiler";
51 return jit_compiler;
52}
53
54extern "C" void jit_unload(void* handle) {
55 DCHECK(handle != nullptr);
56 delete reinterpret_cast<JitCompiler*>(handle);
57}
58
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000059extern "C" bool jit_compile_method(
60 void* handle, ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
63 DCHECK(jit_compiler != nullptr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000064 return jit_compiler->CompileMethod(self, method, osr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080065}
66
Tamas Berghammerfffbee42016-01-15 13:09:34 +000067extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 REQUIRES_SHARED(Locks::mutator_lock_) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +000069 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
70 DCHECK(jit_compiler != nullptr);
71 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +000072 const ArrayRef<mirror::Class*> types_array(types, count);
Vladimir Marko93205e32016-04-13 11:59:46 +010073 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
David Srbecky5d811202016-03-08 13:21:22 +000074 kRuntimeISA, jit_compiler->GetCompilerDriver()->GetInstructionSetFeatures(), types_array);
Vladimir Marko93205e32016-04-13 11:59:46 +010075 CreateJITCodeEntry(std::move(elf_file));
Tamas Berghammer160e6df2016-01-05 14:29:02 +000076 }
77}
78
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000079// Callers of this method assume it has NO_RETURN.
80NO_RETURN static void Usage(const char* fmt, ...) {
81 va_list ap;
82 va_start(ap, fmt);
83 std::string error;
84 StringAppendV(&error, fmt, ap);
85 LOG(FATAL) << error;
86 va_end(ap);
87 exit(EXIT_FAILURE);
88}
89
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000090JitCompiler::JitCompiler() {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091 compiler_options_.reset(new CompilerOptions(
Richard Uhlerf4b34872016-04-13 11:03:46 -070092 CompilerFilter::kDefaultCompilerFilter,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080093 CompilerOptions::kDefaultHugeMethodThreshold,
94 CompilerOptions::kDefaultLargeMethodThreshold,
95 CompilerOptions::kDefaultSmallMethodThreshold,
96 CompilerOptions::kDefaultTinyMethodThreshold,
97 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010098 CompilerOptions::kDefaultInlineDepthLimit,
99 CompilerOptions::kDefaultInlineMaxCodeUnits,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800100 /* no_inline_from */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100101 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800102 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100103 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +0100104 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100105 /* implicit_null_checks */ true,
106 /* implicit_so_checks */ true,
107 /* implicit_suspend_checks */ false,
108 /* pic */ true, // TODO: Support non-PIC in optimizing.
109 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100110 /* init_failure_output */ nullptr,
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000111 /* abort_on_hard_verifier_failure */ false,
112 /* dump_cfg_file_name */ "",
Andreas Gampeace0dc12016-01-20 13:33:13 -0800113 /* dump_cfg_append */ false,
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700114 /* force_determinism */ false,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700115 RegisterAllocator::kRegisterAllocatorDefault,
116 /* passes_to_run */ nullptr));
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;
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800127 instruction_set_features_ = InstructionSetFeatures::FromVariant(
128 instruction_set, str.as_string(), &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700129 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;
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800135 if (instruction_set_features_ == nullptr) {
136 instruction_set_features_ = InstructionSetFeatures::FromVariant(
137 instruction_set, "default", &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700138 if (instruction_set_features_ == nullptr) {
139 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
140 }
141 }
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800142 instruction_set_features_ =
143 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700144 if (instruction_set_features_ == nullptr) {
145 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
146 }
147 }
148 }
149 if (instruction_set_features_ == nullptr) {
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800150 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
Mathieu Chartier085fc872015-10-15 18:19:01 -0700151 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800152 cumulative_logger_.reset(new CumulativeLogger("jit times"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100154 compiler_options_.get(),
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000155 /* verification_results */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100156 Compiler::kOptimizing,
157 instruction_set,
158 instruction_set_features_.get(),
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100159 /* image_classes */ nullptr,
160 /* compiled_classes */ nullptr,
161 /* compiled_methods */ nullptr,
162 /* thread_count */ 1,
163 /* dump_stats */ false,
164 /* dump_passes */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100165 cumulative_logger_.get(),
166 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200167 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800168 // Disable dedupe so we can remove compiled methods.
169 compiler_driver_->SetDedupeEnabled(false);
170 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000171
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000172 size_t thread_count = compiler_driver_->GetThreadCount();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000173 if (compiler_options_->GetGenerateDebugInfo()) {
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100174#ifdef ART_TARGET_ANDROID
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000175 const char* prefix = "/data/misc/trace";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000176#else
177 const char* prefix = "/tmp";
178#endif
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000179 DCHECK_EQ(thread_count, 1u)
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000180 << "Generating debug info only works with one compiler thread";
181 std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map";
182 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
183 if (perf_file_ == nullptr) {
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000184 LOG(ERROR) << "Could not create perf file at " << perf_filename <<
185 " Are you on a user build? Perf only works on userdebug/eng builds";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000186 }
187 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000188
189 size_t inline_depth_limit = compiler_driver_->GetCompilerOptions().GetInlineDepthLimit();
190 DCHECK_LT(thread_count * inline_depth_limit, std::numeric_limits<uint16_t>::max())
191 << "ProfilingInfo's inline counter can potentially overflow";
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192}
193
194JitCompiler::~JitCompiler() {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000195 if (perf_file_ != nullptr) {
196 UNUSED(perf_file_->Flush());
197 UNUSED(perf_file_->Close());
198 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800199}
200
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000201bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000202 DCHECK(!method->IsProxyMethod());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700203 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800204 StackHandleScope<2> hs(self);
205 self->AssertNoPendingException();
206 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100207
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100208 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100210 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700211 VLOG(jit) << "JIT failed to initialize " << method->PrettyMethod();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100212 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800213 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100214
215 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000216 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700217 {
218 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100219 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000220 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method, osr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000221 if (success && (perf_file_ != nullptr)) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000222 const void* ptr = method->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000223 std::ostringstream stream;
224 stream << std::hex
225 << reinterpret_cast<uintptr_t>(ptr)
226 << " "
227 << code_cache->GetMemorySizeOfCodePointer(ptr)
228 << " "
David Sehr709b0702016-10-13 09:12:37 -0700229 << method->PrettyMethod()
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000230 << std::endl;
231 std::string str = stream.str();
232 bool res = perf_file_->WriteFully(str.c_str(), str.size());
233 CHECK(res);
234 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700235 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100236
237 // Trim maps to reduce memory usage.
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000238 // TODO: move this to an idle phase.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700239 {
240 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000241 runtime->GetJitArenaPool()->TrimMaps();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700242 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100243
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700244 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000245 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800246}
247
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800248} // namespace jit
249} // namespace art