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 "art_method-inl.h"
20 #include "arch/instruction_set.h"
21 #include "arch/instruction_set_features.h"
22 #include "base/stringpiece.h"
23 #include "base/time_utils.h"
24 #include "base/timing_logger.h"
25 #include "compiler_callbacks.h"
26 #include "dex/pass_manager.h"
27 #include "dex/quick_compiler_callbacks.h"
28 #include "driver/compiler_driver.h"
29 #include "driver/compiler_options.h"
30 #include "jit/jit.h"
31 #include "jit/jit_code_cache.h"
32 #include "oat_file-inl.h"
33 #include "object_lock.h"
34 #include "thread_list.h"
35 #include "verifier/method_verifier-inl.h"
36
37 namespace art {
38 namespace jit {
39
Create()40 JitCompiler* JitCompiler::Create() {
41 return new JitCompiler();
42 }
43
jit_load(CompilerCallbacks ** callbacks)44 extern "C" void* jit_load(CompilerCallbacks** callbacks) {
45 VLOG(jit) << "loading jit compiler";
46 auto* const jit_compiler = JitCompiler::Create();
47 CHECK(jit_compiler != nullptr);
48 *callbacks = jit_compiler->GetCompilerCallbacks();
49 VLOG(jit) << "Done loading jit compiler";
50 return jit_compiler;
51 }
52
jit_unload(void * handle)53 extern "C" void jit_unload(void* handle) {
54 DCHECK(handle != nullptr);
55 delete reinterpret_cast<JitCompiler*>(handle);
56 }
57
jit_compile_method(void * handle,ArtMethod * method,Thread * self)58 extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
59 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
60 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
61 DCHECK(jit_compiler != nullptr);
62 return jit_compiler->CompileMethod(self, method);
63 }
64
JitCompiler()65 JitCompiler::JitCompiler() : total_time_(0) {
66 auto* pass_manager_options = new PassManagerOptions;
67 pass_manager_options->SetDisablePassList("GVN,DCE,GVNCleanup");
68 compiler_options_.reset(new CompilerOptions(
69 CompilerOptions::kDefaultCompilerFilter,
70 CompilerOptions::kDefaultHugeMethodThreshold,
71 CompilerOptions::kDefaultLargeMethodThreshold,
72 CompilerOptions::kDefaultSmallMethodThreshold,
73 CompilerOptions::kDefaultTinyMethodThreshold,
74 CompilerOptions::kDefaultNumDexMethodsThreshold,
75 CompilerOptions::kDefaultInlineDepthLimit,
76 CompilerOptions::kDefaultInlineMaxCodeUnits,
77 false,
78 CompilerOptions::kDefaultTopKProfileThreshold,
79 false, // TODO: Think about debuggability of JIT-compiled code.
80 CompilerOptions::kDefaultGenerateDebugInfo,
81 false,
82 false,
83 false,
84 false, // pic
85 nullptr,
86 pass_manager_options,
87 nullptr,
88 false));
89 const InstructionSet instruction_set = kRuntimeISA;
90 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
91 VLOG(compiler) << "JIT compiler option " << option;
92 std::string error_msg;
93 if (option.starts_with("--instruction-set-variant=")) {
94 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
95 VLOG(compiler) << "JIT instruction set variant " << str;
96 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
97 instruction_set, str.as_string(), &error_msg));
98 if (instruction_set_features_ == nullptr) {
99 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
100 }
101 } else if (option.starts_with("--instruction-set-features=")) {
102 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
103 VLOG(compiler) << "JIT instruction set features " << str;
104 if (instruction_set_features_.get() == nullptr) {
105 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
106 instruction_set, "default", &error_msg));
107 if (instruction_set_features_ == nullptr) {
108 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
109 }
110 }
111 instruction_set_features_.reset(
112 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
113 if (instruction_set_features_ == nullptr) {
114 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
115 }
116 }
117 }
118 if (instruction_set_features_ == nullptr) {
119 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
120 }
121 cumulative_logger_.reset(new CumulativeLogger("jit times"));
122 verification_results_.reset(new VerificationResults(compiler_options_.get()));
123 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
124 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
125 method_inliner_map_.get(),
126 CompilerCallbacks::CallbackMode::kCompileApp));
127 compiler_driver_.reset(new CompilerDriver(
128 compiler_options_.get(), verification_results_.get(), method_inliner_map_.get(),
129 Compiler::kQuick, instruction_set, instruction_set_features_.get(), false,
130 nullptr, nullptr, nullptr, 1, false, true,
131 std::string(), cumulative_logger_.get(), -1, std::string()));
132 // Disable dedupe so we can remove compiled methods.
133 compiler_driver_->SetDedupeEnabled(false);
134 compiler_driver_->SetSupportBootImageFixup(false);
135 }
136
~JitCompiler()137 JitCompiler::~JitCompiler() {
138 }
139
CompileMethod(Thread * self,ArtMethod * method)140 bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
141 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
142 const uint64_t start_time = NanoTime();
143 StackHandleScope<2> hs(self);
144 self->AssertNoPendingException();
145 Runtime* runtime = Runtime::Current();
146 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
147 VLOG(jit) << "Already compiled " << PrettyMethod(method);
148 return true; // Already compiled
149 }
150 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
151 {
152 TimingLogger::ScopedTiming t2("Initializing", &logger);
153 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
154 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
155 return false;
156 }
157 }
158 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
159 MethodReference method_ref(dex_file, method->GetDexMethodIndex());
160 // Only verify if we don't already have verification results.
161 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
162 TimingLogger::ScopedTiming t2("Verifying", &logger);
163 std::string error;
164 if (verifier::MethodVerifier::VerifyMethod(method, true, &error) ==
165 verifier::MethodVerifier::kHardFailure) {
166 VLOG(jit) << "Not compile method " << PrettyMethod(method)
167 << " due to verification failure " << error;
168 return false;
169 }
170 }
171 CompiledMethod* compiled_method = nullptr;
172 {
173 TimingLogger::ScopedTiming t2("Compiling", &logger);
174 compiled_method = compiler_driver_->CompileMethod(self, method);
175 }
176 {
177 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
178 // Trim maps to reduce memory usage, TODO: measure how much this increases compile time.
179 runtime->GetArenaPool()->TrimMaps();
180 }
181 if (compiled_method == nullptr) {
182 return false;
183 }
184 total_time_ += NanoTime() - start_time;
185 // Don't add the method if we are supposed to be deoptimized.
186 bool result = false;
187 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
188 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method);
189 if (code != nullptr) {
190 // Already have some compiled code, just use this instead of linking.
191 // TODO: Fix recompilation.
192 method->SetEntryPointFromQuickCompiledCode(code);
193 result = true;
194 } else {
195 TimingLogger::ScopedTiming t2("MakeExecutable", &logger);
196 result = MakeExecutable(compiled_method, method);
197 }
198 }
199 // Remove the compiled method to save memory.
200 compiler_driver_->RemoveCompiledMethod(method_ref);
201 runtime->GetJit()->AddTimingLogger(logger);
202 return result;
203 }
204
GetCompilerCallbacks() const205 CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
206 return callbacks_.get();
207 }
208
WriteMethodHeaderAndCode(const CompiledMethod * compiled_method,uint8_t * reserve_begin,uint8_t * reserve_end,const uint8_t * mapping_table,const uint8_t * vmap_table,const uint8_t * gc_map)209 uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
210 uint8_t* reserve_begin, uint8_t* reserve_end,
211 const uint8_t* mapping_table,
212 const uint8_t* vmap_table,
213 const uint8_t* gc_map) {
214 reserve_begin += sizeof(OatQuickMethodHeader);
215 reserve_begin = reinterpret_cast<uint8_t*>(
216 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
217 const auto* quick_code = compiled_method->GetQuickCode();
218 CHECK_LE(reserve_begin, reserve_end);
219 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
220 auto* code_ptr = reserve_begin;
221 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
222 // Construct the header last.
223 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
224 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
225 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
226 const auto code_size = quick_code->size();
227 CHECK_NE(code_size, 0U);
228 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
229 // After we are done writing we need to update the method header.
230 // Write out the method header last.
231 method_header = new(method_header)OatQuickMethodHeader(
232 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
233 core_spill_mask, fp_spill_mask, code_size);
234 // Return the code ptr.
235 return code_ptr;
236 }
237
AddToCodeCache(ArtMethod * method,const CompiledMethod * compiled_method,OatFile::OatMethod * out_method)238 bool JitCompiler::AddToCodeCache(ArtMethod* method, const CompiledMethod* compiled_method,
239 OatFile::OatMethod* out_method) {
240 Runtime* runtime = Runtime::Current();
241 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
242 const auto* quick_code = compiled_method->GetQuickCode();
243 if (quick_code == nullptr) {
244 return false;
245 }
246 const auto code_size = quick_code->size();
247 Thread* const self = Thread::Current();
248 const uint8_t* base = code_cache->CodeCachePtr();
249 auto* const mapping_table = compiled_method->GetMappingTable();
250 auto* const vmap_table = compiled_method->GetVmapTable();
251 auto* const gc_map = compiled_method->GetGcMap();
252 CHECK(gc_map != nullptr) << PrettyMethod(method);
253 // Write out pre-header stuff.
254 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
255 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
256 if (mapping_table_ptr == nullptr) {
257 return false; // Out of data cache.
258 }
259 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
260 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
261 if (vmap_table_ptr == nullptr) {
262 return false; // Out of data cache.
263 }
264 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
265 self, gc_map->data(), gc_map->data() + gc_map->size());
266 if (gc_map_ptr == nullptr) {
267 return false; // Out of data cache.
268 }
269 // Don't touch this until you protect / unprotect the code.
270 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
271 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
272 if (code_reserve == nullptr) {
273 return false;
274 }
275 auto* code_ptr = WriteMethodHeaderAndCode(
276 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
277 vmap_table_ptr, gc_map_ptr);
278
279 __builtin___clear_cache(reinterpret_cast<char*>(code_ptr),
280 reinterpret_cast<char*>(code_ptr + quick_code->size()));
281
282 const size_t thumb_offset = compiled_method->CodeDelta();
283 const uint32_t code_offset = code_ptr - base + thumb_offset;
284 *out_method = OatFile::OatMethod(base, code_offset);
285 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
286 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
287 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
288 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
289 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
290 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
291 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
292 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
293 << "," << reinterpret_cast<void*>(code_ptr + code_size);
294 return true;
295 }
296
MakeExecutable(CompiledMethod * compiled_method,ArtMethod * method)297 bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, ArtMethod* method) {
298 CHECK(method != nullptr);
299 CHECK(compiled_method != nullptr);
300 OatFile::OatMethod oat_method(nullptr, 0);
301 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
302 return false;
303 }
304 // TODO: Flush instruction cache.
305 oat_method.LinkMethod(method);
306 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
307 << PrettyMethod(method);
308 return true;
309 }
310
311 } // namespace jit
312 } // namespace art
313