blob: 88bdb0d010987e92a5bdeffafbfb35a4bdc621ec [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080021#include "arch/instruction_set.h"
22#include "arch/instruction_set_features.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000023#include "art_method-inl.h"
Mathieu Chartier085fc872015-10-15 18:19:01 -070024#include "base/stringpiece.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070026#include "base/timing_logger.h"
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000027#include "base/unix_file/fd_file.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000028#include "debug/elf_debug_writer.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "driver/compiler_driver.h"
30#include "driver/compiler_options.h"
Tamas Berghammer160e6df2016-01-05 14:29:02 +000031#include "jit/debugger_interface.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036#include "object_lock.h"
Matthew Gharrity2cd05b72016-08-03 16:57:37 -070037#include "optimizing/register_allocator.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039
40namespace art {
41namespace jit {
42
43JitCompiler* JitCompiler::Create() {
44 return new JitCompiler();
45}
46
Nicolas Geoffray5b82d332016-02-18 14:22:32 +000047extern "C" void* jit_load(bool* generate_debug_info) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080048 VLOG(jit) << "loading jit compiler";
49 auto* const jit_compiler = JitCompiler::Create();
50 CHECK(jit_compiler != nullptr);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000051 *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052 VLOG(jit) << "Done loading jit compiler";
53 return jit_compiler;
54}
55
56extern "C" void jit_unload(void* handle) {
57 DCHECK(handle != nullptr);
58 delete reinterpret_cast<JitCompiler*>(handle);
59}
60
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000061extern "C" bool jit_compile_method(
62 void* handle, ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080064 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
65 DCHECK(jit_compiler != nullptr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000066 return jit_compiler->CompileMethod(self, method, osr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080067}
68
Tamas Berghammerfffbee42016-01-15 13:09:34 +000069extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 REQUIRES_SHARED(Locks::mutator_lock_) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +000071 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
72 DCHECK(jit_compiler != nullptr);
73 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +000074 const ArrayRef<mirror::Class*> types_array(types, count);
Vladimir Marko93205e32016-04-13 11:59:46 +010075 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
David Srbecky5d811202016-03-08 13:21:22 +000076 kRuntimeISA, jit_compiler->GetCompilerDriver()->GetInstructionSetFeatures(), types_array);
Vladimir Marko93205e32016-04-13 11:59:46 +010077 CreateJITCodeEntry(std::move(elf_file));
Tamas Berghammer160e6df2016-01-05 14:29:02 +000078 }
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;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080086 android::base::StringAppendV(&error, fmt, ap);
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000087 LOG(FATAL) << error;
88 va_end(ap);
89 exit(EXIT_FAILURE);
90}
91
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000092JitCompiler::JitCompiler() {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080093 compiler_options_.reset(new CompilerOptions(
Richard Uhlerf4b34872016-04-13 11:03:46 -070094 CompilerFilter::kDefaultCompilerFilter,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095 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,
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700116 /* force_determinism */ false,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700117 RegisterAllocator::kRegisterAllocatorDefault,
118 /* passes_to_run */ nullptr));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000119 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
120 compiler_options_->ParseCompilerOption(argument, Usage);
121 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800122 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700123 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
124 VLOG(compiler) << "JIT compiler option " << option;
125 std::string error_msg;
126 if (option.starts_with("--instruction-set-variant=")) {
127 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
128 VLOG(compiler) << "JIT instruction set variant " << str;
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800129 instruction_set_features_ = InstructionSetFeatures::FromVariant(
130 instruction_set, str.as_string(), &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700131 if (instruction_set_features_ == nullptr) {
132 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
133 }
134 } else if (option.starts_with("--instruction-set-features=")) {
135 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
136 VLOG(compiler) << "JIT instruction set features " << str;
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800137 if (instruction_set_features_ == nullptr) {
138 instruction_set_features_ = InstructionSetFeatures::FromVariant(
139 instruction_set, "default", &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700140 if (instruction_set_features_ == nullptr) {
141 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
142 }
143 }
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800144 instruction_set_features_ =
145 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700146 if (instruction_set_features_ == nullptr) {
147 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
148 }
149 }
150 }
151 if (instruction_set_features_ == nullptr) {
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800152 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
Mathieu Chartier085fc872015-10-15 18:19:01 -0700153 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 cumulative_logger_.reset(new CumulativeLogger("jit times"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100156 compiler_options_.get(),
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000157 /* verification_results */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100158 Compiler::kOptimizing,
159 instruction_set,
160 instruction_set_features_.get(),
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100161 /* image_classes */ nullptr,
162 /* compiled_classes */ nullptr,
163 /* compiled_methods */ nullptr,
164 /* thread_count */ 1,
165 /* dump_stats */ false,
166 /* dump_passes */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100167 cumulative_logger_.get(),
168 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200169 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 // Disable dedupe so we can remove compiled methods.
171 compiler_driver_->SetDedupeEnabled(false);
172 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000173
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000174 size_t thread_count = compiler_driver_->GetThreadCount();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000175 if (compiler_options_->GetGenerateDebugInfo()) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000176 DCHECK_EQ(thread_count, 1u)
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000177 << "Generating debug info only works with one compiler thread";
xueliang.zhong383b57d2016-10-04 11:19:17 +0100178 jit_logger_.reset(new JitLogger());
179 jit_logger_->OpenLog();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000180 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000181
182 size_t inline_depth_limit = compiler_driver_->GetCompilerOptions().GetInlineDepthLimit();
183 DCHECK_LT(thread_count * inline_depth_limit, std::numeric_limits<uint16_t>::max())
184 << "ProfilingInfo's inline counter can potentially overflow";
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800185}
186
187JitCompiler::~JitCompiler() {
xueliang.zhong383b57d2016-10-04 11:19:17 +0100188 if (compiler_options_->GetGenerateDebugInfo()) {
189 jit_logger_->CloseLog();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000190 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800191}
192
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000193bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000194 DCHECK(!method->IsProxyMethod());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700195 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196 StackHandleScope<2> hs(self);
197 self->AssertNoPendingException();
198 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100199
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100200 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100202 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700203 VLOG(jit) << "JIT failed to initialize " << method->PrettyMethod();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100204 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100206
207 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000208 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700209 {
210 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100211 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000212 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method, osr);
xueliang.zhong383b57d2016-10-04 11:19:17 +0100213 if (success && (jit_logger_ != nullptr)) {
xueliang.zhong59001492017-01-11 10:36:41 +0000214 jit_logger_->WriteLog(code_cache, method, osr);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000215 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700216 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100217
218 // Trim maps to reduce memory usage.
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000219 // TODO: move this to an idle phase.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700220 {
221 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000222 runtime->GetJitArenaPool()->TrimMaps();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700223 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100224
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700225 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000226 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800227}
228
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800229} // namespace jit
230} // namespace art