blob: 6ff1e2e95e9e653a1a8baa2f5d2a85b2db3017a0 [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"
35#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036
37namespace art {
38namespace jit {
39
40JitCompiler* JitCompiler::Create() {
41 return new JitCompiler();
42}
43
Nicolas Geoffray5b82d332016-02-18 14:22:32 +000044extern "C" void* jit_load(bool* generate_debug_info) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045 VLOG(jit) << "loading jit compiler";
46 auto* const jit_compiler = JitCompiler::Create();
47 CHECK(jit_compiler != nullptr);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000048 *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080049 VLOG(jit) << "Done loading jit compiler";
50 return jit_compiler;
51}
52
53extern "C" void jit_unload(void* handle) {
54 DCHECK(handle != nullptr);
55 delete reinterpret_cast<JitCompiler*>(handle);
56}
57
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000058extern "C" bool jit_compile_method(
59 void* handle, ArtMethod* method, Thread* self, bool osr)
Mathieu Chartier90443472015-07-16 20:32:27 -070060 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
62 DCHECK(jit_compiler != nullptr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000063 return jit_compiler->CompileMethod(self, method, osr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080064}
65
Tamas Berghammerfffbee42016-01-15 13:09:34 +000066extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
Tamas Berghammer160e6df2016-01-05 14:29:02 +000067 SHARED_REQUIRES(Locks::mutator_lock_) {
68 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
69 DCHECK(jit_compiler != nullptr);
70 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +000071 const ArrayRef<mirror::Class*> types_array(types, count);
David Srbeckyc5bfa972016-02-05 15:49:10 +000072 ArrayRef<const uint8_t> elf_file = debug::WriteDebugElfFileForClasses(kRuntimeISA, types_array);
Tamas Berghammer160e6df2016-01-05 14:29:02 +000073 CreateJITCodeEntry(std::unique_ptr<const uint8_t[]>(elf_file.data()), elf_file.size());
74 }
75}
76
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000077// Callers of this method assume it has NO_RETURN.
78NO_RETURN static void Usage(const char* fmt, ...) {
79 va_list ap;
80 va_start(ap, fmt);
81 std::string error;
82 StringAppendV(&error, fmt, ap);
83 LOG(FATAL) << error;
84 va_end(ap);
85 exit(EXIT_FAILURE);
86}
87
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000088JitCompiler::JitCompiler() {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080089 compiler_options_.reset(new CompilerOptions(
90 CompilerOptions::kDefaultCompilerFilter,
91 CompilerOptions::kDefaultHugeMethodThreshold,
92 CompilerOptions::kDefaultLargeMethodThreshold,
93 CompilerOptions::kDefaultSmallMethodThreshold,
94 CompilerOptions::kDefaultTinyMethodThreshold,
95 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010096 CompilerOptions::kDefaultInlineDepthLimit,
97 CompilerOptions::kDefaultInlineMaxCodeUnits,
Jeff Haodcdc85b2015-12-04 14:06:18 -080098 /* no_inline_from */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010099 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800100 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100101 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +0100102 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100103 /* implicit_null_checks */ true,
104 /* implicit_so_checks */ true,
105 /* implicit_suspend_checks */ false,
106 /* pic */ true, // TODO: Support non-PIC in optimizing.
107 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100108 /* init_failure_output */ nullptr,
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000109 /* abort_on_hard_verifier_failure */ false,
110 /* dump_cfg_file_name */ "",
Andreas Gampeace0dc12016-01-20 13:33:13 -0800111 /* dump_cfg_append */ false,
112 /* force_determinism */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000113 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
114 compiler_options_->ParseCompilerOption(argument, Usage);
115 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800116 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700117 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
118 VLOG(compiler) << "JIT compiler option " << option;
119 std::string error_msg;
120 if (option.starts_with("--instruction-set-variant=")) {
121 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
122 VLOG(compiler) << "JIT instruction set variant " << str;
123 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
124 instruction_set, str.as_string(), &error_msg));
125 if (instruction_set_features_ == nullptr) {
126 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
127 }
128 } else if (option.starts_with("--instruction-set-features=")) {
129 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
130 VLOG(compiler) << "JIT instruction set features " << str;
131 if (instruction_set_features_.get() == nullptr) {
132 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
133 instruction_set, "default", &error_msg));
134 if (instruction_set_features_ == nullptr) {
135 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
136 }
137 }
138 instruction_set_features_.reset(
139 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
140 if (instruction_set_features_ == nullptr) {
141 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
142 }
143 }
144 }
145 if (instruction_set_features_ == nullptr) {
146 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
147 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800148 cumulative_logger_.reset(new CumulativeLogger("jit times"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800149 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800150 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100151 compiler_options_.get(),
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000152 /* verification_results */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100153 method_inliner_map_.get(),
154 Compiler::kOptimizing,
155 instruction_set,
156 instruction_set_features_.get(),
157 /* image */ false,
158 /* image_classes */ nullptr,
159 /* compiled_classes */ nullptr,
160 /* compiled_methods */ nullptr,
161 /* thread_count */ 1,
162 /* dump_stats */ false,
163 /* dump_passes */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100164 cumulative_logger_.get(),
165 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200166 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800167 // Disable dedupe so we can remove compiled methods.
168 compiler_driver_->SetDedupeEnabled(false);
169 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000170
171 if (compiler_options_->GetGenerateDebugInfo()) {
172#ifdef __ANDROID__
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000173 const char* prefix = "/data/misc/trace";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000174#else
175 const char* prefix = "/tmp";
176#endif
177 DCHECK_EQ(compiler_driver_->GetThreadCount(), 1u)
178 << "Generating debug info only works with one compiler thread";
179 std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map";
180 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
181 if (perf_file_ == nullptr) {
Tamas Berghammerf0615a32016-01-27 16:15:56 +0000182 LOG(ERROR) << "Could not create perf file at " << perf_filename <<
183 " Are you on a user build? Perf only works on userdebug/eng builds";
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000184 }
185 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800186}
187
188JitCompiler::~JitCompiler() {
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000189 if (perf_file_ != nullptr) {
190 UNUSED(perf_file_->Flush());
191 UNUSED(perf_file_->Close());
192 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800193}
194
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000195bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000196 DCHECK(!method->IsProxyMethod());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700197 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800198 StackHandleScope<2> hs(self);
199 self->AssertNoPendingException();
200 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100201
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100202 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100204 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
205 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
206 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800207 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100208
209 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000210 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700211 {
212 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100213 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000214 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method, osr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000215 if (success && (perf_file_ != nullptr)) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000216 const void* ptr = method->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000217 std::ostringstream stream;
218 stream << std::hex
219 << reinterpret_cast<uintptr_t>(ptr)
220 << " "
221 << code_cache->GetMemorySizeOfCodePointer(ptr)
222 << " "
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000223 << PrettyMethod(method)
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000224 << std::endl;
225 std::string str = stream.str();
226 bool res = perf_file_->WriteFully(str.c_str(), str.size());
227 CHECK(res);
228 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700229 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100230
231 // Trim maps to reduce memory usage.
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000232 // TODO: move this to an idle phase.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700233 {
234 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000235 runtime->GetJitArenaPool()->TrimMaps();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700236 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100237
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700238 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000239 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800240}
241
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800242} // namespace jit
243} // namespace art