1 /*
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
19 #include "android-base/stringprintf.h"
20
21 #include "arch/instruction_set.h"
22 #include "arch/instruction_set_features.h"
23 #include "art_method-inl.h"
24 #include "base/logging.h" // For VLOG
25 #include "base/string_view_cpp20.h"
26 #include "base/systrace.h"
27 #include "base/time_utils.h"
28 #include "base/timing_logger.h"
29 #include "compiler.h"
30 #include "debug/elf_debug_writer.h"
31 #include "driver/compiler_options.h"
32 #include "jit/debugger_interface.h"
33 #include "jit/jit.h"
34 #include "jit/jit_code_cache.h"
35 #include "jit/jit_logger.h"
36
37 namespace art {
38 namespace jit {
39
Create()40 JitCompiler* JitCompiler::Create() {
41 return new JitCompiler();
42 }
43
ParseCompilerOptions()44 void JitCompiler::ParseCompilerOptions() {
45 // Special case max code units for inlining, whose default is "unset" (implictly
46 // meaning no limit). Do this before parsing the actual passed options.
47 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
48 Runtime* runtime = Runtime::Current();
49 {
50 std::string error_msg;
51 if (!compiler_options_->ParseCompilerOptions(runtime->GetCompilerOptions(),
52 /*ignore_unrecognized=*/ true,
53 &error_msg)) {
54 LOG(FATAL) << error_msg;
55 UNREACHABLE();
56 }
57 }
58 // Set to appropriate JIT compiler type.
59 compiler_options_->compiler_type_ = runtime->IsZygote()
60 ? CompilerOptions::CompilerType::kSharedCodeJitCompiler
61 : CompilerOptions::CompilerType::kJitCompiler;
62 // JIT is never PIC, no matter what the runtime compiler options specify.
63 compiler_options_->SetNonPic();
64
65 // If the options don't provide whether we generate debuggable code, set
66 // debuggability based on the runtime value.
67 if (!compiler_options_->GetDebuggable()) {
68 compiler_options_->SetDebuggable(runtime->IsJavaDebuggable());
69 }
70
71 compiler_options_->implicit_null_checks_ = runtime->GetImplicitNullChecks();
72 compiler_options_->implicit_so_checks_ = runtime->GetImplicitStackOverflowChecks();
73 compiler_options_->implicit_suspend_checks_ = runtime->GetImplicitSuspendChecks();
74
75 const InstructionSet instruction_set = compiler_options_->GetInstructionSet();
76 if (kRuntimeISA == InstructionSet::kArm) {
77 DCHECK_EQ(instruction_set, InstructionSet::kThumb2);
78 } else {
79 DCHECK_EQ(instruction_set, kRuntimeISA);
80 }
81 std::unique_ptr<const InstructionSetFeatures> instruction_set_features;
82 for (const std::string& option : runtime->GetCompilerOptions()) {
83 VLOG(compiler) << "JIT compiler option " << option;
84 std::string error_msg;
85 if (StartsWith(option, "--instruction-set-variant=")) {
86 const char* str = option.c_str() + strlen("--instruction-set-variant=");
87 VLOG(compiler) << "JIT instruction set variant " << str;
88 instruction_set_features = InstructionSetFeatures::FromVariant(
89 instruction_set, str, &error_msg);
90 if (instruction_set_features == nullptr) {
91 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
92 }
93 } else if (StartsWith(option, "--instruction-set-features=")) {
94 const char* str = option.c_str() + strlen("--instruction-set-features=");
95 VLOG(compiler) << "JIT instruction set features " << str;
96 if (instruction_set_features == nullptr) {
97 instruction_set_features = InstructionSetFeatures::FromVariant(
98 instruction_set, "default", &error_msg);
99 if (instruction_set_features == nullptr) {
100 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
101 }
102 }
103 instruction_set_features =
104 instruction_set_features->AddFeaturesFromString(str, &error_msg);
105 if (instruction_set_features == nullptr) {
106 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
107 }
108 }
109 }
110
111 if (instruction_set_features == nullptr) {
112 // '--instruction-set-features/--instruction-set-variant' were not used.
113 // Use build-time defined features.
114 instruction_set_features = InstructionSetFeatures::FromCppDefines();
115 }
116 compiler_options_->instruction_set_features_ = std::move(instruction_set_features);
117
118 if (compiler_options_->GetGenerateDebugInfo()) {
119 jit_logger_.reset(new JitLogger());
120 jit_logger_->OpenLog();
121 }
122 }
123
jit_load()124 extern "C" JitCompilerInterface* jit_load() {
125 VLOG(jit) << "Create jit compiler";
126 auto* const jit_compiler = JitCompiler::Create();
127 CHECK(jit_compiler != nullptr);
128 VLOG(jit) << "Done creating jit compiler";
129 return jit_compiler;
130 }
131
TypesLoaded(mirror::Class ** types,size_t count)132 void JitCompiler::TypesLoaded(mirror::Class** types, size_t count) {
133 const CompilerOptions& compiler_options = GetCompilerOptions();
134 if (compiler_options.GetGenerateDebugInfo()) {
135 InstructionSet isa = compiler_options.GetInstructionSet();
136 const InstructionSetFeatures* features = compiler_options.GetInstructionSetFeatures();
137 const ArrayRef<mirror::Class*> types_array(types, count);
138 std::vector<uint8_t> elf_file =
139 debug::WriteDebugElfFileForClasses(isa, features, types_array);
140
141 // NB: Don't allow packing since it would remove non-backtrace data.
142 MutexLock mu(Thread::Current(), *Locks::jit_lock_);
143 AddNativeDebugInfoForJit(/*code_ptr=*/ nullptr, elf_file, /*allow_packing=*/ false);
144 }
145 }
146
GenerateDebugInfo()147 bool JitCompiler::GenerateDebugInfo() {
148 return GetCompilerOptions().GetGenerateDebugInfo();
149 }
150
PackElfFileForJIT(ArrayRef<const JITCodeEntry * > elf_files,ArrayRef<const void * > removed_symbols,bool compress,size_t * num_symbols)151 std::vector<uint8_t> JitCompiler::PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files,
152 ArrayRef<const void*> removed_symbols,
153 bool compress,
154 /*out*/ size_t* num_symbols) {
155 return debug::PackElfFileForJIT(elf_files, removed_symbols, compress, num_symbols);
156 }
157
JitCompiler()158 JitCompiler::JitCompiler() {
159 compiler_options_.reset(new CompilerOptions());
160 ParseCompilerOptions();
161 compiler_.reset(
162 Compiler::Create(*compiler_options_, /*storage=*/ nullptr, Compiler::kOptimizing));
163 }
164
~JitCompiler()165 JitCompiler::~JitCompiler() {
166 if (compiler_options_->GetGenerateDebugInfo()) {
167 jit_logger_->CloseLog();
168 }
169 }
170
CompileMethod(Thread * self,JitMemoryRegion * region,ArtMethod * method,CompilationKind compilation_kind)171 bool JitCompiler::CompileMethod(
172 Thread* self, JitMemoryRegion* region, ArtMethod* method, CompilationKind compilation_kind) {
173 SCOPED_TRACE << "JIT compiling "
174 << method->PrettyMethod()
175 << " (kind=" << compilation_kind << ")";
176
177 DCHECK(!method->IsProxyMethod());
178 DCHECK(method->GetDeclaringClass()->IsResolved());
179
180 TimingLogger logger(
181 "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
182 self->AssertNoPendingException();
183 Runtime* runtime = Runtime::Current();
184
185 // Do the compilation.
186 bool success = false;
187 {
188 TimingLogger::ScopedTiming t2(compilation_kind == CompilationKind::kOsr
189 ? "Compiling OSR"
190 : compilation_kind == CompilationKind::kOptimized
191 ? "Compiling optimized"
192 : "Compiling baseline",
193 &logger);
194 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
195 metrics::AutoTimer timer{runtime->GetMetrics()->JitMethodCompileTotalTime()};
196 success = compiler_->JitCompile(
197 self, code_cache, region, method, compilation_kind, jit_logger_.get());
198 uint64_t duration_us = timer.Stop();
199 VLOG(jit) << "Compilation of " << method->PrettyMethod() << " took "
200 << PrettyDuration(UsToNs(duration_us));
201 runtime->GetMetrics()->JitMethodCompileCount()->AddOne();
202 }
203
204 // Trim maps to reduce memory usage.
205 // TODO: move this to an idle phase.
206 {
207 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
208 runtime->GetJitArenaPool()->TrimMaps();
209 }
210
211 runtime->GetJit()->AddTimingLogger(logger);
212 return success;
213 }
214
IsBaselineCompiler() const215 bool JitCompiler::IsBaselineCompiler() const {
216 return compiler_options_->IsBaseline();
217 }
218
219 } // namespace jit
220 } // namespace art
221