blob: bbf34585eb917783cc3bb3226629b0f0aef21bff [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
Vladimir Marko8581e2a2019-02-06 15:54:55 +000025#include "base/string_view_cpp20.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"
Vladimir Marko038924b2019-02-19 15:09:35 +000029#include "compiler.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_options.h"
Tamas Berghammer160e6df2016-01-05 14:29:02 +000032#include "jit/debugger_interface.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "jit/jit.h"
34#include "jit/jit_code_cache.h"
Vladimir Markoa0431112018-06-25 09:32:54 +010035#include "jit/jit_logger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036
37namespace art {
38namespace jit {
39
40JitCompiler* JitCompiler::Create() {
41 return new JitCompiler();
42}
43
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000044void JitCompiler::ParseCompilerOptions() {
Nicolas Geoffray62a2f272017-11-10 16:46:43 +000045 // Special case max code units for inlining, whose default is "unset" (implictly
Roland Levillainef071322018-04-17 14:16:49 +010046 // meaning no limit). Do this before parsing the actual passed options.
Nicolas Geoffray62a2f272017-11-10 16:46:43 +000047 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
Vladimir Marko6be1dbd2018-11-13 13:09:51 +000048 Runtime* runtime = Runtime::Current();
Andreas Gampe097f34c2017-08-23 08:57:51 -070049 {
50 std::string error_msg;
Vladimir Marko6be1dbd2018-11-13 13:09:51 +000051 if (!compiler_options_->ParseCompilerOptions(runtime->GetCompilerOptions(),
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000052 /*ignore_unrecognized=*/ true,
53 &error_msg)) {
Andreas Gampe097f34c2017-08-23 08:57:51 -070054 LOG(FATAL) << error_msg;
55 UNREACHABLE();
56 }
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000057 }
Vladimir Marko2fad5272017-05-17 12:57:18 +010058 // JIT is never PIC, no matter what the runtime compiler options specify.
59 compiler_options_->SetNonPic();
60
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000061 // If the options don't provide whether we generate debuggable code, set
62 // debuggability based on the runtime value.
63 if (!compiler_options_->GetDebuggable()) {
64 compiler_options_->SetDebuggable(runtime->IsJavaDebuggable());
65 }
Nicolas Geoffray57c47042017-06-29 11:31:39 +010066
Vladimir Markoa0431112018-06-25 09:32:54 +010067 const InstructionSet instruction_set = compiler_options_->GetInstructionSet();
68 if (kRuntimeISA == InstructionSet::kArm) {
69 DCHECK_EQ(instruction_set, InstructionSet::kThumb2);
70 } else {
71 DCHECK_EQ(instruction_set, kRuntimeISA);
72 }
73 std::unique_ptr<const InstructionSetFeatures> instruction_set_features;
Vladimir Marko8581e2a2019-02-06 15:54:55 +000074 for (const std::string& option : runtime->GetCompilerOptions()) {
Mathieu Chartier085fc872015-10-15 18:19:01 -070075 VLOG(compiler) << "JIT compiler option " << option;
76 std::string error_msg;
Vladimir Marko8581e2a2019-02-06 15:54:55 +000077 if (StartsWith(option, "--instruction-set-variant=")) {
78 const char* str = option.c_str() + strlen("--instruction-set-variant=");
Mathieu Chartier085fc872015-10-15 18:19:01 -070079 VLOG(compiler) << "JIT instruction set variant " << str;
Vladimir Markoa0431112018-06-25 09:32:54 +010080 instruction_set_features = InstructionSetFeatures::FromVariant(
Vladimir Marko8581e2a2019-02-06 15:54:55 +000081 instruction_set, str, &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +010082 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -070083 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
84 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +000085 } else if (StartsWith(option, "--instruction-set-features=")) {
86 const char* str = option.c_str() + strlen("--instruction-set-features=");
Mathieu Chartier085fc872015-10-15 18:19:01 -070087 VLOG(compiler) << "JIT instruction set features " << str;
Vladimir Markoa0431112018-06-25 09:32:54 +010088 if (instruction_set_features == nullptr) {
89 instruction_set_features = InstructionSetFeatures::FromVariant(
Andreas Gampe0415b4e2015-01-06 15:17:07 -080090 instruction_set, "default", &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +010091 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -070092 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
93 }
94 }
Vladimir Markoa0431112018-06-25 09:32:54 +010095 instruction_set_features =
Vladimir Marko8581e2a2019-02-06 15:54:55 +000096 instruction_set_features->AddFeaturesFromString(str, &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +010097 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -070098 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
99 }
100 }
101 }
xueliang.zhong7f88c1a2018-11-06 11:42:41 +0000102
Vladimir Markoa0431112018-06-25 09:32:54 +0100103 if (instruction_set_features == nullptr) {
xueliang.zhong7f88c1a2018-11-06 11:42:41 +0000104 // '--instruction-set-features/--instruction-set-variant' were not used.
105 // Use build-time defined features.
Vladimir Markoa0431112018-06-25 09:32:54 +0100106 instruction_set_features = InstructionSetFeatures::FromCppDefines();
Mathieu Chartier085fc872015-10-15 18:19:01 -0700107 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100108 compiler_options_->instruction_set_features_ = std::move(instruction_set_features);
Vladimir Marko6be1dbd2018-11-13 13:09:51 +0000109 compiler_options_->compiling_with_core_image_ =
Vladimir Marko038924b2019-02-19 15:09:35 +0000110 CompilerOptions::IsCoreImageFilename(runtime->GetImageLocation());
Vladimir Markoa0431112018-06-25 09:32:54 +0100111
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000112 if (compiler_options_->GetGenerateDebugInfo()) {
113 jit_logger_.reset(new JitLogger());
114 jit_logger_->OpenLog();
115 }
116}
117
118extern "C" void* jit_load() {
119 VLOG(jit) << "Create jit compiler";
120 auto* const jit_compiler = JitCompiler::Create();
121 CHECK(jit_compiler != nullptr);
122 VLOG(jit) << "Done creating jit compiler";
123 return jit_compiler;
124}
125
126extern "C" void jit_unload(void* handle) {
127 DCHECK(handle != nullptr);
128 delete reinterpret_cast<JitCompiler*>(handle);
129}
130
131extern "C" bool jit_compile_method(
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000132 void* handle, ArtMethod* method, Thread* self, bool baseline, bool osr)
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000133 REQUIRES_SHARED(Locks::mutator_lock_) {
134 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
135 DCHECK(jit_compiler != nullptr);
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000136 return jit_compiler->CompileMethod(self, method, baseline, osr);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000137}
138
139extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
140 REQUIRES_SHARED(Locks::mutator_lock_) {
141 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
142 DCHECK(jit_compiler != nullptr);
143 const CompilerOptions& compiler_options = jit_compiler->GetCompilerOptions();
144 if (compiler_options.GetGenerateDebugInfo()) {
145 const ArrayRef<mirror::Class*> types_array(types, count);
146 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
147 kRuntimeISA, compiler_options.GetInstructionSetFeatures(), types_array);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000148 // We never free debug info for types, so we don't need to provide a handle
149 // (which would have been otherwise used as identifier to remove it later).
David Srbeckyafc60cd2018-12-05 11:59:31 +0000150 AddNativeDebugInfoForJit(Thread::Current(),
151 /*code_ptr=*/ nullptr,
David Srbecky0b21e412018-12-05 13:24:06 +0000152 elf_file,
153 debug::PackElfFileForJIT,
154 compiler_options.GetInstructionSet(),
155 compiler_options.GetInstructionSetFeatures());
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000156 }
157}
158
159extern "C" void jit_update_options(void* handle) {
160 JitCompiler* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
161 DCHECK(jit_compiler != nullptr);
162 jit_compiler->ParseCompilerOptions();
163}
164
165extern "C" bool jit_generate_debug_info(void* handle) {
166 JitCompiler* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
167 DCHECK(jit_compiler != nullptr);
168 return jit_compiler->GetCompilerOptions().GetGenerateDebugInfo();
169}
170
171JitCompiler::JitCompiler() {
172 compiler_options_.reset(new CompilerOptions());
173 ParseCompilerOptions();
Vladimir Marko038924b2019-02-19 15:09:35 +0000174 compiler_.reset(
175 Compiler::Create(*compiler_options_, /*storage=*/ nullptr, Compiler::kOptimizing));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800176}
177
178JitCompiler::~JitCompiler() {
xueliang.zhong383b57d2016-10-04 11:19:17 +0100179 if (compiler_options_->GetGenerateDebugInfo()) {
180 jit_logger_->CloseLog();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000181 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182}
183
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000184bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool baseline, bool osr) {
Andreas Gampec6548162017-12-08 12:15:22 -0800185 SCOPED_TRACE << "JIT compiling " << method->PrettyMethod();
186
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000187 DCHECK(!method->IsProxyMethod());
Nicolas Geoffray23ddfe82017-06-07 14:09:43 +0100188 DCHECK(method->GetDeclaringClass()->IsResolved());
189
Nicolas Geoffray3d699222017-09-20 15:15:20 +0100190 TimingLogger logger(
191 "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192 self->AssertNoPendingException();
193 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100194
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100195 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000196 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700197 {
198 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100199 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Vladimir Marko038924b2019-02-19 15:09:35 +0000200 success = compiler_->JitCompile(self, code_cache, method, baseline, osr, jit_logger_.get());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700201 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100202
203 // Trim maps to reduce memory usage.
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000204 // TODO: move this to an idle phase.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700205 {
206 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000207 runtime->GetJitArenaPool()->TrimMaps();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700208 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100209
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700210 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000211 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800212}
213
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800214} // namespace jit
215} // namespace art