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 const InstructionSet instruction_set = compiler_options_->GetInstructionSet();
72 if (kRuntimeISA == InstructionSet::kArm) {
73 DCHECK_EQ(instruction_set, InstructionSet::kThumb2);
74 } else {
75 DCHECK_EQ(instruction_set, kRuntimeISA);
76 }
77 std::unique_ptr<const InstructionSetFeatures> instruction_set_features;
78 for (const std::string& option : runtime->GetCompilerOptions()) {
79 VLOG(compiler) << "JIT compiler option " << option;
80 std::string error_msg;
81 if (StartsWith(option, "--instruction-set-variant=")) {
82 const char* str = option.c_str() + strlen("--instruction-set-variant=");
83 VLOG(compiler) << "JIT instruction set variant " << str;
84 instruction_set_features = InstructionSetFeatures::FromVariant(
85 instruction_set, str, &error_msg);
86 if (instruction_set_features == nullptr) {
87 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
88 }
89 } else if (StartsWith(option, "--instruction-set-features=")) {
90 const char* str = option.c_str() + strlen("--instruction-set-features=");
91 VLOG(compiler) << "JIT instruction set features " << str;
92 if (instruction_set_features == nullptr) {
93 instruction_set_features = InstructionSetFeatures::FromVariant(
94 instruction_set, "default", &error_msg);
95 if (instruction_set_features == nullptr) {
96 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
97 }
98 }
99 instruction_set_features =
100 instruction_set_features->AddFeaturesFromString(str, &error_msg);
101 if (instruction_set_features == nullptr) {
102 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
103 }
104 }
105 }
106
107 if (instruction_set_features == nullptr) {
108 // '--instruction-set-features/--instruction-set-variant' were not used.
109 // Use build-time defined features.
110 instruction_set_features = InstructionSetFeatures::FromCppDefines();
111 }
112 compiler_options_->instruction_set_features_ = std::move(instruction_set_features);
113
114 if (compiler_options_->GetGenerateDebugInfo()) {
115 jit_logger_.reset(new JitLogger());
116 jit_logger_->OpenLog();
117 }
118 }
119
jit_load()120 extern "C" JitCompilerInterface* jit_load() {
121 VLOG(jit) << "Create jit compiler";
122 auto* const jit_compiler = JitCompiler::Create();
123 CHECK(jit_compiler != nullptr);
124 VLOG(jit) << "Done creating jit compiler";
125 return jit_compiler;
126 }
127
TypesLoaded(mirror::Class ** types,size_t count)128 void JitCompiler::TypesLoaded(mirror::Class** types, size_t count) {
129 const CompilerOptions& compiler_options = GetCompilerOptions();
130 if (compiler_options.GetGenerateDebugInfo()) {
131 InstructionSet isa = compiler_options.GetInstructionSet();
132 const InstructionSetFeatures* features = compiler_options.GetInstructionSetFeatures();
133 const ArrayRef<mirror::Class*> types_array(types, count);
134 std::vector<uint8_t> elf_file =
135 debug::WriteDebugElfFileForClasses(isa, features, types_array);
136
137 // NB: Don't allow packing since it would remove non-backtrace data.
138 MutexLock mu(Thread::Current(), *Locks::jit_lock_);
139 AddNativeDebugInfoForJit(/*code_ptr=*/ nullptr, elf_file, /*allow_packing=*/ false);
140 }
141 }
142
GenerateDebugInfo()143 bool JitCompiler::GenerateDebugInfo() {
144 return GetCompilerOptions().GetGenerateDebugInfo();
145 }
146
PackElfFileForJIT(ArrayRef<const JITCodeEntry * > elf_files,ArrayRef<const void * > removed_symbols,bool compress,size_t * num_symbols)147 std::vector<uint8_t> JitCompiler::PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files,
148 ArrayRef<const void*> removed_symbols,
149 bool compress,
150 /*out*/ size_t* num_symbols) {
151 return debug::PackElfFileForJIT(elf_files, removed_symbols, compress, num_symbols);
152 }
153
JitCompiler()154 JitCompiler::JitCompiler() {
155 compiler_options_.reset(new CompilerOptions());
156 ParseCompilerOptions();
157 compiler_.reset(
158 Compiler::Create(*compiler_options_, /*storage=*/ nullptr, Compiler::kOptimizing));
159 }
160
~JitCompiler()161 JitCompiler::~JitCompiler() {
162 if (compiler_options_->GetGenerateDebugInfo()) {
163 jit_logger_->CloseLog();
164 }
165 }
166
CompileMethod(Thread * self,JitMemoryRegion * region,ArtMethod * method,CompilationKind compilation_kind)167 bool JitCompiler::CompileMethod(
168 Thread* self, JitMemoryRegion* region, ArtMethod* method, CompilationKind compilation_kind) {
169 SCOPED_TRACE << "JIT compiling "
170 << method->PrettyMethod()
171 << " (kind=" << compilation_kind << ")";
172
173 DCHECK(!method->IsProxyMethod());
174 DCHECK(method->GetDeclaringClass()->IsResolved());
175
176 TimingLogger logger(
177 "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
178 self->AssertNoPendingException();
179 Runtime* runtime = Runtime::Current();
180
181 // Do the compilation.
182 bool success = false;
183 {
184 TimingLogger::ScopedTiming t2(compilation_kind == CompilationKind::kOsr
185 ? "Compiling OSR"
186 : compilation_kind == CompilationKind::kOptimized
187 ? "Compiling optimized"
188 : "Compiling baseline",
189 &logger);
190 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
191 metrics::AutoTimer timer{runtime->GetMetrics()->JitMethodCompileTotalTime()};
192 success = compiler_->JitCompile(
193 self, code_cache, region, method, compilation_kind, jit_logger_.get());
194 uint64_t duration_us = timer.Stop();
195 VLOG(jit) << "Compilation of " << method->PrettyMethod() << " took "
196 << PrettyDuration(UsToNs(duration_us));
197 runtime->GetMetrics()->JitMethodCompileCount()->AddOne();
198 }
199
200 // Trim maps to reduce memory usage.
201 // TODO: move this to an idle phase.
202 {
203 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
204 runtime->GetJitArenaPool()->TrimMaps();
205 }
206
207 runtime->GetJit()->AddTimingLogger(logger);
208 return success;
209 }
210
211 } // namespace jit
212 } // namespace art
213