blob: 0de00a82fa4e148c47f12fe8c2bf1415f66d0d30 [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"
Nicolas Geoffraya25dce92016-01-12 16:41:10 +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"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010037#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038#include "object_lock.h"
Matthew Gharrity2cd05b72016-08-03 16:57:37 -070039#include "optimizing/register_allocator.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080041
42namespace art {
43namespace jit {
44
45JitCompiler* JitCompiler::Create() {
46 return new JitCompiler();
47}
48
Nicolas Geoffray5b82d332016-02-18 14:22:32 +000049extern "C" void* jit_load(bool* generate_debug_info) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080050 VLOG(jit) << "loading jit compiler";
51 auto* const jit_compiler = JitCompiler::Create();
52 CHECK(jit_compiler != nullptr);
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
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000063extern "C" bool jit_compile_method(
64 void* handle, ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
67 DCHECK(jit_compiler != nullptr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000068 return jit_compiler->CompileMethod(self, method, osr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069}
70
Tamas Berghammerfffbee42016-01-15 13:09:34 +000071extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +000073 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
74 DCHECK(jit_compiler != nullptr);
75 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +000076 const ArrayRef<mirror::Class*> types_array(types, count);
Vladimir Marko93205e32016-04-13 11:59:46 +010077 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
David Srbecky5d811202016-03-08 13:21:22 +000078 kRuntimeISA, jit_compiler->GetCompilerDriver()->GetInstructionSetFeatures(), types_array);
David Srbeckyfb3de3d2018-01-29 16:11:49 +000079 MutexLock mu(Thread::Current(), *Locks::native_debug_interface_lock_);
David Srbecky440a9b32018-02-15 17:47:29 +000080 // We never free debug info for types, so we don't need to provide a handle
81 // (which would have been otherwise used as identifier to remove it later).
82 AddNativeDebugInfoForJit(nullptr /* handle */, elf_file);
Tamas Berghammer160e6df2016-01-05 14:29:02 +000083 }
84}
85
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000086JitCompiler::JitCompiler() {
Nicolas Geoffray57c47042017-06-29 11:31:39 +010087 compiler_options_.reset(new CompilerOptions());
Nicolas Geoffray62a2f272017-11-10 16:46:43 +000088 // Special case max code units for inlining, whose default is "unset" (implictly
89 // meaning no limit). Do this before parsing the actuall passed options.
90 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
Andreas Gampe097f34c2017-08-23 08:57:51 -070091 {
92 std::string error_msg;
93 if (!compiler_options_->ParseCompilerOptions(Runtime::Current()->GetCompilerOptions(),
94 true /* ignore_unrecognized */,
95 &error_msg)) {
96 LOG(FATAL) << error_msg;
97 UNREACHABLE();
98 }
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000099 }
Vladimir Marko2fad5272017-05-17 12:57:18 +0100100 // JIT is never PIC, no matter what the runtime compiler options specify.
101 compiler_options_->SetNonPic();
102
Nicolas Geoffray57c47042017-06-29 11:31:39 +0100103 // Set debuggability based on the runtime value.
104 compiler_options_->SetDebuggable(Runtime::Current()->IsJavaDebuggable());
105
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800106 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700107 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
108 VLOG(compiler) << "JIT compiler option " << option;
109 std::string error_msg;
110 if (option.starts_with("--instruction-set-variant=")) {
111 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
112 VLOG(compiler) << "JIT instruction set variant " << str;
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800113 instruction_set_features_ = InstructionSetFeatures::FromVariant(
114 instruction_set, str.as_string(), &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700115 if (instruction_set_features_ == nullptr) {
116 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
117 }
118 } else if (option.starts_with("--instruction-set-features=")) {
119 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
120 VLOG(compiler) << "JIT instruction set features " << str;
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800121 if (instruction_set_features_ == nullptr) {
122 instruction_set_features_ = InstructionSetFeatures::FromVariant(
123 instruction_set, "default", &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700124 if (instruction_set_features_ == nullptr) {
125 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
126 }
127 }
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800128 instruction_set_features_ =
129 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg);
Mathieu Chartier085fc872015-10-15 18:19:01 -0700130 if (instruction_set_features_ == nullptr) {
131 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
132 }
133 }
134 }
135 if (instruction_set_features_ == nullptr) {
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800136 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
Mathieu Chartier085fc872015-10-15 18:19:01 -0700137 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800138 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100139 compiler_options_.get(),
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000140 /* verification_results */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100141 Compiler::kOptimizing,
142 instruction_set,
143 instruction_set_features_.get(),
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100144 /* image_classes */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100145 /* thread_count */ 1,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100146 /* swap_fd */ -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200147 /* profile_compilation_info */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800148 // Disable dedupe so we can remove compiled methods.
149 compiler_driver_->SetDedupeEnabled(false);
150 compiler_driver_->SetSupportBootImageFixup(false);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000151
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000152 size_t thread_count = compiler_driver_->GetThreadCount();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000153 if (compiler_options_->GetGenerateDebugInfo()) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000154 DCHECK_EQ(thread_count, 1u)
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000155 << "Generating debug info only works with one compiler thread";
xueliang.zhong383b57d2016-10-04 11:19:17 +0100156 jit_logger_.reset(new JitLogger());
157 jit_logger_->OpenLog();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000158 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800159}
160
161JitCompiler::~JitCompiler() {
xueliang.zhong383b57d2016-10-04 11:19:17 +0100162 if (compiler_options_->GetGenerateDebugInfo()) {
163 jit_logger_->CloseLog();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000164 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800165}
166
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000167bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
Andreas Gampec6548162017-12-08 12:15:22 -0800168 SCOPED_TRACE << "JIT compiling " << method->PrettyMethod();
169
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000170 DCHECK(!method->IsProxyMethod());
Nicolas Geoffray23ddfe82017-06-07 14:09:43 +0100171 DCHECK(method->GetDeclaringClass()->IsResolved());
172
Nicolas Geoffray3d699222017-09-20 15:15:20 +0100173 TimingLogger logger(
174 "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175 self->AssertNoPendingException();
176 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100177
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100178 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000179 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700180 {
181 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100182 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffray01db5f72017-07-19 15:05:49 +0100183 success = compiler_driver_->GetCompiler()->JitCompile(
184 self, code_cache, method, osr, jit_logger_.get());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700185 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100186
187 // Trim maps to reduce memory usage.
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000188 // TODO: move this to an idle phase.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700189 {
190 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000191 runtime->GetJitArenaPool()->TrimMaps();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700192 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100193
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700194 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000195 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196}
197
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800198} // namespace jit
199} // namespace art