blob: bb35065921f20089f914e43e28c52de628f65066 [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"
Andreas Gampe170331f2017-12-07 18:41:03 -080024#include "base/logging.h" // For VLOG
Mathieu Chartier085fc872015-10-15 18:19:01 -070025#include "base/stringpiece.h"
Andreas Gampec6548162017-12-08 12:15:22 -080026#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/time_utils.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070028#include "base/timing_logger.h"
Andreas Gampe763cd982018-11-26 18:21:45 +000029#include "base/unix_file/fd_file.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000030#include "debug/elf_debug_writer.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "driver/compiler_driver.h"
32#include "driver/compiler_options.h"
Tamas Berghammer160e6df2016-01-05 14:29:02 +000033#include "jit/debugger_interface.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "jit/jit.h"
35#include "jit/jit_code_cache.h"
Vladimir Markoa0431112018-06-25 09:32:54 +010036#include "jit/jit_logger.h"
Andreas Gampe763cd982018-11-26 18:21:45 +000037#include "oat_file-inl.h"
38#include "oat_quick_method_header.h"
39#include "object_lock.h"
40#include "optimizing/register_allocator.h"
41#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042
43namespace art {
44namespace jit {
45
46JitCompiler* JitCompiler::Create() {
47 return new JitCompiler();
48}
49
Andreas Gampe763cd982018-11-26 18:21:45 +000050extern "C" void* jit_load(bool* generate_debug_info) {
51 VLOG(jit) << "loading jit compiler";
52 auto* const jit_compiler = JitCompiler::Create();
53 CHECK(jit_compiler != nullptr);
54 *generate_debug_info = jit_compiler->GetCompilerOptions().GetGenerateDebugInfo();
55 VLOG(jit) << "Done loading jit compiler";
56 return jit_compiler;
57}
58
59extern "C" void jit_unload(void* handle) {
60 DCHECK(handle != nullptr);
61 delete reinterpret_cast<JitCompiler*>(handle);
62}
63
64extern "C" bool jit_compile_method(
65 void* handle, ArtMethod* method, Thread* self, bool osr)
66 REQUIRES_SHARED(Locks::mutator_lock_) {
67 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
68 DCHECK(jit_compiler != nullptr);
69 return jit_compiler->CompileMethod(self, method, osr);
70}
71
72extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
73 REQUIRES_SHARED(Locks::mutator_lock_) {
74 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
75 DCHECK(jit_compiler != nullptr);
76 const CompilerOptions& compiler_options = jit_compiler->GetCompilerOptions();
77 if (compiler_options.GetGenerateDebugInfo()) {
78 const ArrayRef<mirror::Class*> types_array(types, count);
79 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
80 kRuntimeISA, compiler_options.GetInstructionSetFeatures(), types_array);
81 MutexLock mu(Thread::Current(), *Locks::native_debug_interface_lock_);
82 // We never free debug info for types, so we don't need to provide a handle
83 // (which would have been otherwise used as identifier to remove it later).
84 AddNativeDebugInfoForJit(nullptr /* handle */, elf_file);
85 }
86}
87
88JitCompiler::JitCompiler() {
89 compiler_options_.reset(new CompilerOptions());
Nicolas Geoffray62a2f272017-11-10 16:46:43 +000090 // Special case max code units for inlining, whose default is "unset" (implictly
Roland Levillainef071322018-04-17 14:16:49 +010091 // meaning no limit). Do this before parsing the actual passed options.
Nicolas Geoffray62a2f272017-11-10 16:46:43 +000092 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
Vladimir Marko6be1dbd2018-11-13 13:09:51 +000093 Runtime* runtime = Runtime::Current();
Andreas Gampe097f34c2017-08-23 08:57:51 -070094 {
95 std::string error_msg;
Vladimir Marko6be1dbd2018-11-13 13:09:51 +000096 if (!compiler_options_->ParseCompilerOptions(runtime->GetCompilerOptions(),
Andreas Gampe763cd982018-11-26 18:21:45 +000097 /*ignore_unrecognized=*/ true,
98 &error_msg)) {
Andreas Gampe097f34c2017-08-23 08:57:51 -070099 LOG(FATAL) << error_msg;
100 UNREACHABLE();
101 }
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000102 }
Vladimir Marko2fad5272017-05-17 12:57:18 +0100103 // JIT is never PIC, no matter what the runtime compiler options specify.
104 compiler_options_->SetNonPic();
105
Andreas Gampe763cd982018-11-26 18:21:45 +0000106 // Set debuggability based on the runtime value.
107 compiler_options_->SetDebuggable(runtime->IsJavaDebuggable());
Nicolas Geoffray57c47042017-06-29 11:31:39 +0100108
Vladimir Markoa0431112018-06-25 09:32:54 +0100109 const InstructionSet instruction_set = compiler_options_->GetInstructionSet();
110 if (kRuntimeISA == InstructionSet::kArm) {
111 DCHECK_EQ(instruction_set, InstructionSet::kThumb2);
112 } else {
113 DCHECK_EQ(instruction_set, kRuntimeISA);
114 }
115 std::unique_ptr<const InstructionSetFeatures> instruction_set_features;
Vladimir Marko6be1dbd2018-11-13 13:09:51 +0000116 for (const StringPiece option : runtime->GetCompilerOptions()) {
Mathieu Chartier085fc872015-10-15 18:19:01 -0700117 VLOG(compiler) << "JIT compiler option " << option;
118 std::string error_msg;
119 if (option.starts_with("--instruction-set-variant=")) {
120 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
121 VLOG(compiler) << "JIT instruction set variant " << str;
Vladimir Markoa0431112018-06-25 09:32:54 +0100122 instruction_set_features = InstructionSetFeatures::FromVariant(
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800123 instruction_set, str.as_string(), &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +0100124 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -0700125 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
126 }
127 } else if (option.starts_with("--instruction-set-features=")) {
128 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
129 VLOG(compiler) << "JIT instruction set features " << str;
Vladimir Markoa0431112018-06-25 09:32:54 +0100130 if (instruction_set_features == nullptr) {
131 instruction_set_features = InstructionSetFeatures::FromVariant(
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800132 instruction_set, "default", &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +0100133 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -0700134 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
135 }
136 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100137 instruction_set_features =
138 instruction_set_features->AddFeaturesFromString(str.as_string(), &error_msg);
139 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -0700140 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
141 }
142 }
143 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100144 if (instruction_set_features == nullptr) {
145 instruction_set_features = InstructionSetFeatures::FromCppDefines();
Mathieu Chartier085fc872015-10-15 18:19:01 -0700146 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100147 compiler_options_->instruction_set_features_ = std::move(instruction_set_features);
Vladimir Marko6be1dbd2018-11-13 13:09:51 +0000148 compiler_options_->compiling_with_core_image_ =
149 CompilerDriver::IsCoreImageFilename(runtime->GetImageLocation());
Vladimir Markoa0431112018-06-25 09:32:54 +0100150
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800151 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100152 compiler_options_.get(),
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000153 /* verification_results */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100154 Compiler::kOptimizing,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100155 /* image_classes */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100156 /* thread_count */ 1,
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +0000157 /* swap_fd */ -1));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 // Disable dedupe so we can remove compiled methods.
159 compiler_driver_->SetDedupeEnabled(false);
Andreas Gampe763cd982018-11-26 18:21:45 +0000160
161 size_t thread_count = compiler_driver_->GetThreadCount();
162 if (compiler_options_->GetGenerateDebugInfo()) {
163 DCHECK_EQ(thread_count, 1u)
164 << "Generating debug info only works with one compiler thread";
165 jit_logger_.reset(new JitLogger());
166 jit_logger_->OpenLog();
167 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800168}
169
170JitCompiler::~JitCompiler() {
xueliang.zhong383b57d2016-10-04 11:19:17 +0100171 if (compiler_options_->GetGenerateDebugInfo()) {
172 jit_logger_->CloseLog();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000173 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800174}
175
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000176bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
Andreas Gampec6548162017-12-08 12:15:22 -0800177 SCOPED_TRACE << "JIT compiling " << method->PrettyMethod();
178
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000179 DCHECK(!method->IsProxyMethod());
Nicolas Geoffray23ddfe82017-06-07 14:09:43 +0100180 DCHECK(method->GetDeclaringClass()->IsResolved());
181
Nicolas Geoffray3d699222017-09-20 15:15:20 +0100182 TimingLogger logger(
183 "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800184 self->AssertNoPendingException();
185 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100186
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100187 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000188 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700189 {
190 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100191 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffray01db5f72017-07-19 15:05:49 +0100192 success = compiler_driver_->GetCompiler()->JitCompile(
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +0100193 self, code_cache, method, /* baseline= */ false, osr, jit_logger_.get());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700194 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100195
196 // Trim maps to reduce memory usage.
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000197 // TODO: move this to an idle phase.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700198 {
199 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000200 runtime->GetJitArenaPool()->TrimMaps();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700201 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100202
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700203 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000204 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205}
206
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800207} // namespace jit
208} // namespace art