blob: a4b9c6c0b9042c15068020f0625a6133ed4458fc [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 Berghammer160e6df2016-01-05 14:29:02 +000070extern "C" void jit_type_loaded(void* handle, mirror::Class* type)
71 SHARED_REQUIRES(Locks::mutator_lock_) {
72 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
73 DCHECK(jit_compiler != nullptr);
74 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
75 ArrayRef<const uint8_t> elf_file = dwarf::WriteDebugElfFileForClass(kRuntimeISA, type);
76 CreateJITCodeEntry(std::unique_ptr<const uint8_t[]>(elf_file.data()), elf_file.size());
77 }
78}
79
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000080// Callers of this method assume it has NO_RETURN.
81NO_RETURN static void Usage(const char* fmt, ...) {
82 va_list ap;
83 va_start(ap, fmt);
84 std::string error;
85 StringAppendV(&error, fmt, ap);
86 LOG(FATAL) << error;
87 va_end(ap);
88 exit(EXIT_FAILURE);
89}
90
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091JitCompiler::JitCompiler() : total_time_(0) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080092 compiler_options_.reset(new CompilerOptions(
93 CompilerOptions::kDefaultCompilerFilter,
94 CompilerOptions::kDefaultHugeMethodThreshold,
95 CompilerOptions::kDefaultLargeMethodThreshold,
96 CompilerOptions::kDefaultSmallMethodThreshold,
97 CompilerOptions::kDefaultTinyMethodThreshold,
98 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010099 CompilerOptions::kDefaultInlineDepthLimit,
100 CompilerOptions::kDefaultInlineMaxCodeUnits,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800101 /* no_inline_from */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100102 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800103 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100104 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +0100105 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100106 /* implicit_null_checks */ true,
107 /* implicit_so_checks */ true,
108 /* implicit_suspend_checks */ false,
109 /* pic */ true, // TODO: Support non-PIC in optimizing.
110 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100111 /* init_failure_output */ nullptr,
112 /* abort_on_hard_verifier_failure */ 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"));
149 verification_results_.reset(new VerificationResults(compiler_options_.get()));
150 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
151 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700152 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700153 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100155 compiler_options_.get(),
156 verification_results_.get(),
157 method_inliner_map_.get(),
158 Compiler::kOptimizing,
159 instruction_set,
160 instruction_set_features_.get(),
161 /* image */ false,
162 /* image_classes */ nullptr,
163 /* compiled_classes */ nullptr,
164 /* compiled_methods */ nullptr,
165 /* thread_count */ 1,
166 /* dump_stats */ false,
167 /* dump_passes */ false,
168 /* dump_cfg_file_name */ "",
Calin Juravle87000a92015-08-24 15:34:44 +0100169 /* dump_cfg_append */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100170 cumulative_logger_.get(),
171 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200172 /* dex to oat map */ nullptr,
173 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800174 // Disable dedupe so we can remove compiled methods.
175 compiler_driver_->SetDedupeEnabled(false);
176 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000177
178 if (compiler_options_->GetGenerateDebugInfo()) {
179#ifdef __ANDROID__
180 const char* prefix = GetAndroidData();
181#else
182 const char* prefix = "/tmp";
183#endif
184 DCHECK_EQ(compiler_driver_->GetThreadCount(), 1u)
185 << "Generating debug info only works with one compiler thread";
186 std::string perf_filename = std::string(prefix) + "/perf-" + std::to_string(getpid()) + ".map";
187 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
188 if (perf_file_ == nullptr) {
189 LOG(FATAL) << "Could not create perf file at " << perf_filename;
190 }
191 }
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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700202 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700203 const uint64_t start_time = NanoTime();
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)) {
211 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
212 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 Geoffray22cf3d32015-11-02 11:57:11 +0000219 // If we get a request to compile a proxy method, we pass the actual Java method
220 // of that proxy method, as the compiler does not expect a proxy method.
221 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100222 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000223 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000224 if (success && compiler_options_->GetGenerateDebugInfo()) {
225 const void* ptr = method_to_compile->GetEntryPointFromQuickCompiledCode();
226 std::ostringstream stream;
227 stream << std::hex
228 << reinterpret_cast<uintptr_t>(ptr)
229 << " "
230 << code_cache->GetMemorySizeOfCodePointer(ptr)
231 << " "
232 << PrettyMethod(method_to_compile)
233 << std::endl;
234 std::string str = stream.str();
235 bool res = perf_file_->WriteFully(str.c_str(), str.size());
236 CHECK(res);
237 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700238 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100239
240 // Trim maps to reduce memory usage.
241 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700242 {
243 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700244 runtime->GetArenaPool()->TrimMaps();
245 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100246
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800247 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700248 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000249 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800250}
251
252CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
253 return callbacks_.get();
254}
255
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800256} // namespace jit
257} // namespace art