blob: f12a3ad52b9a49eccfe27cbf250ad518339f0de4 [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
Mythri Alle322ef182022-08-16 06:40:02 +000044void JitCompiler::SetDebuggableCompilerOption(bool value) {
45 compiler_options_->SetDebuggable(value);
46}
47
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000048void JitCompiler::ParseCompilerOptions() {
Nicolas Geoffray62a2f272017-11-10 16:46:43 +000049 // Special case max code units for inlining, whose default is "unset" (implictly
Roland Levillainef071322018-04-17 14:16:49 +010050 // meaning no limit). Do this before parsing the actual passed options.
Nicolas Geoffray62a2f272017-11-10 16:46:43 +000051 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
Vladimir Marko6be1dbd2018-11-13 13:09:51 +000052 Runtime* runtime = Runtime::Current();
Andreas Gampe097f34c2017-08-23 08:57:51 -070053 {
54 std::string error_msg;
Vladimir Marko6be1dbd2018-11-13 13:09:51 +000055 if (!compiler_options_->ParseCompilerOptions(runtime->GetCompilerOptions(),
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000056 /*ignore_unrecognized=*/ true,
57 &error_msg)) {
Andreas Gampe097f34c2017-08-23 08:57:51 -070058 LOG(FATAL) << error_msg;
59 UNREACHABLE();
60 }
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000061 }
Vladimir Marko695348f2020-05-19 14:42:02 +010062 // Set to appropriate JIT compiler type.
63 compiler_options_->compiler_type_ = runtime->IsZygote()
64 ? CompilerOptions::CompilerType::kSharedCodeJitCompiler
65 : CompilerOptions::CompilerType::kJitCompiler;
Vladimir Marko2fad5272017-05-17 12:57:18 +010066 // JIT is never PIC, no matter what the runtime compiler options specify.
67 compiler_options_->SetNonPic();
68
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000069 // If the options don't provide whether we generate debuggable code, set
70 // debuggability based on the runtime value.
71 if (!compiler_options_->GetDebuggable()) {
72 compiler_options_->SetDebuggable(runtime->IsJavaDebuggable());
73 }
Nicolas Geoffray57c47042017-06-29 11:31:39 +010074
Nicolas Geoffray541ca322022-01-20 09:03:04 +000075 compiler_options_->implicit_null_checks_ = runtime->GetImplicitNullChecks();
76 compiler_options_->implicit_so_checks_ = runtime->GetImplicitStackOverflowChecks();
77 compiler_options_->implicit_suspend_checks_ = runtime->GetImplicitSuspendChecks();
78
Vladimir Markoa0431112018-06-25 09:32:54 +010079 const InstructionSet instruction_set = compiler_options_->GetInstructionSet();
80 if (kRuntimeISA == InstructionSet::kArm) {
81 DCHECK_EQ(instruction_set, InstructionSet::kThumb2);
82 } else {
83 DCHECK_EQ(instruction_set, kRuntimeISA);
84 }
85 std::unique_ptr<const InstructionSetFeatures> instruction_set_features;
Vladimir Marko8581e2a2019-02-06 15:54:55 +000086 for (const std::string& option : runtime->GetCompilerOptions()) {
Mathieu Chartier085fc872015-10-15 18:19:01 -070087 VLOG(compiler) << "JIT compiler option " << option;
88 std::string error_msg;
Vladimir Marko8581e2a2019-02-06 15:54:55 +000089 if (StartsWith(option, "--instruction-set-variant=")) {
90 const char* str = option.c_str() + strlen("--instruction-set-variant=");
Mathieu Chartier085fc872015-10-15 18:19:01 -070091 VLOG(compiler) << "JIT instruction set variant " << str;
Nicolas Geoffray1e9cf892022-08-03 22:53:45 +010092 instruction_set_features = InstructionSetFeatures::FromVariantAndHwcap(
Vladimir Marko8581e2a2019-02-06 15:54:55 +000093 instruction_set, str, &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +010094 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -070095 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
96 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +000097 } else if (StartsWith(option, "--instruction-set-features=")) {
98 const char* str = option.c_str() + strlen("--instruction-set-features=");
Mathieu Chartier085fc872015-10-15 18:19:01 -070099 VLOG(compiler) << "JIT instruction set features " << str;
Vladimir Markoa0431112018-06-25 09:32:54 +0100100 if (instruction_set_features == nullptr) {
101 instruction_set_features = InstructionSetFeatures::FromVariant(
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800102 instruction_set, "default", &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +0100103 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -0700104 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
105 }
106 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100107 instruction_set_features =
Vladimir Marko8581e2a2019-02-06 15:54:55 +0000108 instruction_set_features->AddFeaturesFromString(str, &error_msg);
Vladimir Markoa0431112018-06-25 09:32:54 +0100109 if (instruction_set_features == nullptr) {
Mathieu Chartier085fc872015-10-15 18:19:01 -0700110 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
111 }
112 }
113 }
xueliang.zhong7f88c1a2018-11-06 11:42:41 +0000114
Vladimir Markoa0431112018-06-25 09:32:54 +0100115 if (instruction_set_features == nullptr) {
xueliang.zhong7f88c1a2018-11-06 11:42:41 +0000116 // '--instruction-set-features/--instruction-set-variant' were not used.
117 // Use build-time defined features.
Vladimir Markoa0431112018-06-25 09:32:54 +0100118 instruction_set_features = InstructionSetFeatures::FromCppDefines();
Mathieu Chartier085fc872015-10-15 18:19:01 -0700119 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100120 compiler_options_->instruction_set_features_ = std::move(instruction_set_features);
121
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000122 if (compiler_options_->GetGenerateDebugInfo()) {
123 jit_logger_.reset(new JitLogger());
124 jit_logger_->OpenLog();
125 }
126}
127
David Srbecky46b53532019-08-06 13:39:05 +0100128extern "C" JitCompilerInterface* jit_load() {
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000129 VLOG(jit) << "Create jit compiler";
130 auto* const jit_compiler = JitCompiler::Create();
131 CHECK(jit_compiler != nullptr);
132 VLOG(jit) << "Done creating jit compiler";
133 return jit_compiler;
134}
135
David Srbecky9ac8e432019-08-13 13:16:13 +0100136void JitCompiler::TypesLoaded(mirror::Class** types, size_t count) {
David Srbecky46b53532019-08-06 13:39:05 +0100137 const CompilerOptions& compiler_options = GetCompilerOptions();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000138 if (compiler_options.GetGenerateDebugInfo()) {
David Srbecky8fc2f952019-07-31 18:40:09 +0100139 InstructionSet isa = compiler_options.GetInstructionSet();
140 const InstructionSetFeatures* features = compiler_options.GetInstructionSetFeatures();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000141 const ArrayRef<mirror::Class*> types_array(types, count);
David Srbecky8fc2f952019-07-31 18:40:09 +0100142 std::vector<uint8_t> elf_file =
143 debug::WriteDebugElfFileForClasses(isa, features, types_array);
144
145 // NB: Don't allow packing since it would remove non-backtrace data.
David Srbecky9ac8e432019-08-13 13:16:13 +0100146 MutexLock mu(Thread::Current(), *Locks::jit_lock_);
David Srbecky8fc2f952019-07-31 18:40:09 +0100147 AddNativeDebugInfoForJit(/*code_ptr=*/ nullptr, elf_file, /*allow_packing=*/ false);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000148 }
149}
150
David Srbecky46b53532019-08-06 13:39:05 +0100151bool JitCompiler::GenerateDebugInfo() {
152 return GetCompilerOptions().GetGenerateDebugInfo();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000153}
154
David Srbeckye09b87e2019-08-19 21:31:31 +0100155std::vector<uint8_t> JitCompiler::PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files,
David Srbecky8fc2f952019-07-31 18:40:09 +0100156 ArrayRef<const void*> removed_symbols,
157 bool compress,
158 /*out*/ size_t* num_symbols) {
159 return debug::PackElfFileForJIT(elf_files, removed_symbols, compress, num_symbols);
160}
161
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000162JitCompiler::JitCompiler() {
163 compiler_options_.reset(new CompilerOptions());
164 ParseCompilerOptions();
Vladimir Marko038924b2019-02-19 15:09:35 +0000165 compiler_.reset(
166 Compiler::Create(*compiler_options_, /*storage=*/ nullptr, Compiler::kOptimizing));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800167}
168
169JitCompiler::~JitCompiler() {
xueliang.zhong383b57d2016-10-04 11:19:17 +0100170 if (compiler_options_->GetGenerateDebugInfo()) {
171 jit_logger_->CloseLog();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000172 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800173}
174
Nicolas Geoffray7f7539b2019-06-06 16:20:54 +0100175bool JitCompiler::CompileMethod(
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100176 Thread* self, JitMemoryRegion* region, ArtMethod* method, CompilationKind compilation_kind) {
Nicolas Geoffray76e52e82020-03-09 14:16:42 +0000177 SCOPED_TRACE << "JIT compiling "
178 << method->PrettyMethod()
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100179 << " (kind=" << compilation_kind << ")";
Andreas Gampec6548162017-12-08 12:15:22 -0800180
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000181 DCHECK(!method->IsProxyMethod());
Nicolas Geoffray23ddfe82017-06-07 14:09:43 +0100182 DCHECK(method->GetDeclaringClass()->IsResolved());
183
Nicolas Geoffray3d699222017-09-20 15:15:20 +0100184 TimingLogger logger(
185 "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800186 self->AssertNoPendingException();
187 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100188
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100189 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000190 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700191 {
Nicolas Geoffrayc473dc72020-07-03 15:04:21 +0100192 TimingLogger::ScopedTiming t2(compilation_kind == CompilationKind::kOsr
193 ? "Compiling OSR"
194 : compilation_kind == CompilationKind::kOptimized
195 ? "Compiling optimized"
196 : "Compiling baseline",
197 &logger);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100198 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Calin Juravle9b996ce2021-06-29 16:05:38 -0700199 metrics::AutoTimer timer{runtime->GetMetrics()->JitMethodCompileTotalTime()};
Nicolas Geoffray7f7539b2019-06-06 16:20:54 +0100200 success = compiler_->JitCompile(
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100201 self, code_cache, region, method, compilation_kind, jit_logger_.get());
Eric Holk4bb09002020-09-30 11:42:34 -0700202 uint64_t duration_us = timer.Stop();
203 VLOG(jit) << "Compilation of " << method->PrettyMethod() << " took "
204 << PrettyDuration(UsToNs(duration_us));
Eric Holkb2a14162021-03-30 16:54:13 -0700205 runtime->GetMetrics()->JitMethodCompileCount()->AddOne();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700206 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100207
208 // Trim maps to reduce memory usage.
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000209 // TODO: move this to an idle phase.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700210 {
211 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000212 runtime->GetJitArenaPool()->TrimMaps();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700213 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100214
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700215 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000216 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217}
218
Nicolas Geoffray8a608fa2021-11-29 14:47:21 +0000219bool JitCompiler::IsBaselineCompiler() const {
220 return compiler_options_->IsBaseline();
221}
222
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800223} // namespace jit
224} // namespace art