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/stringpiece.h"
26 #include "base/systrace.h"
27 #include "base/time_utils.h"
28 #include "base/timing_logger.h"
29 #include "base/unix_file/fd_file.h"
30 #include "debug/elf_debug_writer.h"
31 #include "driver/compiler_driver.h"
32 #include "driver/compiler_options.h"
33 #include "jit/debugger_interface.h"
34 #include "jit/jit.h"
35 #include "jit/jit_code_cache.h"
36 #include "oat_file-inl.h"
37 #include "oat_quick_method_header.h"
38 #include "object_lock.h"
39 #include "optimizing/register_allocator.h"
40 #include "thread_list.h"
41
42 namespace art {
43 namespace jit {
44
Create()45 JitCompiler* JitCompiler::Create() {
46 return new JitCompiler();
47 }
48
jit_load(bool * generate_debug_info)49 extern "C" void* jit_load(bool* generate_debug_info) {
50 VLOG(jit) << "loading jit compiler";
51 auto* const jit_compiler = JitCompiler::Create();
52 CHECK(jit_compiler != nullptr);
53 *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo();
54 VLOG(jit) << "Done loading jit compiler";
55 return jit_compiler;
56 }
57
jit_unload(void * handle)58 extern "C" void jit_unload(void* handle) {
59 DCHECK(handle != nullptr);
60 delete reinterpret_cast<JitCompiler*>(handle);
61 }
62
jit_compile_method(void * handle,ArtMethod * method,Thread * self,bool osr)63 extern "C" bool jit_compile_method(
64 void* handle, ArtMethod* method, Thread* self, bool osr)
65 REQUIRES_SHARED(Locks::mutator_lock_) {
66 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
67 DCHECK(jit_compiler != nullptr);
68 return jit_compiler->CompileMethod(self, method, osr);
69 }
70
jit_types_loaded(void * handle,mirror::Class ** types,size_t count)71 extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
72 REQUIRES_SHARED(Locks::mutator_lock_) {
73 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
74 DCHECK(jit_compiler != nullptr);
75 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
76 const ArrayRef<mirror::Class*> types_array(types, count);
77 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
78 kRuntimeISA, jit_compiler->GetCompilerDriver()->GetInstructionSetFeatures(), types_array);
79 MutexLock mu(Thread::Current(), *Locks::native_debug_interface_lock_);
80 // 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);
83 }
84 }
85
JitCompiler()86 JitCompiler::JitCompiler() {
87 compiler_options_.reset(new CompilerOptions());
88 // 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);
91 {
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 }
99 }
100 // JIT is never PIC, no matter what the runtime compiler options specify.
101 compiler_options_->SetNonPic();
102
103 // Set debuggability based on the runtime value.
104 compiler_options_->SetDebuggable(Runtime::Current()->IsJavaDebuggable());
105
106 const InstructionSet instruction_set = kRuntimeISA;
107 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;
113 instruction_set_features_ = InstructionSetFeatures::FromVariant(
114 instruction_set, str.as_string(), &error_msg);
115 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;
121 if (instruction_set_features_ == nullptr) {
122 instruction_set_features_ = InstructionSetFeatures::FromVariant(
123 instruction_set, "default", &error_msg);
124 if (instruction_set_features_ == nullptr) {
125 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
126 }
127 }
128 instruction_set_features_ =
129 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg);
130 if (instruction_set_features_ == nullptr) {
131 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
132 }
133 }
134 }
135 if (instruction_set_features_ == nullptr) {
136 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
137 }
138 compiler_driver_.reset(new CompilerDriver(
139 compiler_options_.get(),
140 /* verification_results */ nullptr,
141 Compiler::kOptimizing,
142 instruction_set,
143 instruction_set_features_.get(),
144 /* image_classes */ nullptr,
145 /* compiled_classes */ nullptr,
146 /* compiled_methods */ nullptr,
147 /* thread_count */ 1,
148 /* swap_fd */ -1,
149 /* profile_compilation_info */ nullptr));
150 // Disable dedupe so we can remove compiled methods.
151 compiler_driver_->SetDedupeEnabled(false);
152 compiler_driver_->SetSupportBootImageFixup(false);
153
154 size_t thread_count = compiler_driver_->GetThreadCount();
155 if (compiler_options_->GetGenerateDebugInfo()) {
156 DCHECK_EQ(thread_count, 1u)
157 << "Generating debug info only works with one compiler thread";
158 jit_logger_.reset(new JitLogger());
159 jit_logger_->OpenLog();
160 }
161 }
162
~JitCompiler()163 JitCompiler::~JitCompiler() {
164 if (compiler_options_->GetGenerateDebugInfo()) {
165 jit_logger_->CloseLog();
166 }
167 }
168
CompileMethod(Thread * self,ArtMethod * method,bool osr)169 bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
170 SCOPED_TRACE << "JIT compiling " << method->PrettyMethod();
171
172 DCHECK(!method->IsProxyMethod());
173 DCHECK(method->GetDeclaringClass()->IsResolved());
174
175 TimingLogger logger(
176 "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
177 self->AssertNoPendingException();
178 Runtime* runtime = Runtime::Current();
179
180 // Do the compilation.
181 bool success = false;
182 {
183 TimingLogger::ScopedTiming t2("Compiling", &logger);
184 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
185 success = compiler_driver_->GetCompiler()->JitCompile(
186 self, code_cache, method, osr, jit_logger_.get());
187 }
188
189 // Trim maps to reduce memory usage.
190 // TODO: move this to an idle phase.
191 {
192 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
193 runtime->GetJitArenaPool()->TrimMaps();
194 }
195
196 runtime->GetJit()->AddTimingLogger(logger);
197 return success;
198 }
199
200 } // namespace jit
201 } // namespace art
202