blob: 2125c9a26af6aa7fb8529a32788f4d3bffa06762 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080020#include "arch/instruction_set.h"
21#include "arch/instruction_set_features.h"
Mathieu Chartier085fc872015-10-15 18:19:01 -070022#include "base/stringpiece.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070024#include "base/timing_logger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080025#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"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "object_lock.h"
35#include "thread_list.h"
36#include "verifier/method_verifier-inl.h"
37
38namespace art {
39namespace jit {
40
41JitCompiler* JitCompiler::Create() {
42 return new JitCompiler();
43}
44
45extern "C" void* jit_load(CompilerCallbacks** callbacks) {
46 VLOG(jit) << "loading jit compiler";
47 auto* const jit_compiler = JitCompiler::Create();
48 CHECK(jit_compiler != nullptr);
49 *callbacks = jit_compiler->GetCompilerCallbacks();
50 VLOG(jit) << "Done loading jit compiler";
51 return jit_compiler;
52}
53
54extern "C" void jit_unload(void* handle) {
55 DCHECK(handle != nullptr);
56 delete reinterpret_cast<JitCompiler*>(handle);
57}
58
Mathieu Chartiere401d142015-04-22 13:56:20 -070059extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -070060 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
62 DCHECK(jit_compiler != nullptr);
63 return jit_compiler->CompileMethod(self, method);
64}
65
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000066// Callers of this method assume it has NO_RETURN.
67NO_RETURN static void Usage(const char* fmt, ...) {
68 va_list ap;
69 va_start(ap, fmt);
70 std::string error;
71 StringAppendV(&error, fmt, ap);
72 LOG(FATAL) << error;
73 va_end(ap);
74 exit(EXIT_FAILURE);
75}
76
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077JitCompiler::JitCompiler() : total_time_(0) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080078 compiler_options_.reset(new CompilerOptions(
79 CompilerOptions::kDefaultCompilerFilter,
80 CompilerOptions::kDefaultHugeMethodThreshold,
81 CompilerOptions::kDefaultLargeMethodThreshold,
82 CompilerOptions::kDefaultSmallMethodThreshold,
83 CompilerOptions::kDefaultTinyMethodThreshold,
84 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010085 CompilerOptions::kDefaultInlineDepthLimit,
86 CompilerOptions::kDefaultInlineMaxCodeUnits,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010087 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080088 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010089 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +010090 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010091 /* implicit_null_checks */ true,
92 /* implicit_so_checks */ true,
93 /* implicit_suspend_checks */ false,
94 /* pic */ true, // TODO: Support non-PIC in optimizing.
95 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010096 /* init_failure_output */ nullptr,
97 /* abort_on_hard_verifier_failure */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000098 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
99 compiler_options_->ParseCompilerOption(argument, Usage);
100 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800101 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700102 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
103 VLOG(compiler) << "JIT compiler option " << option;
104 std::string error_msg;
105 if (option.starts_with("--instruction-set-variant=")) {
106 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
107 VLOG(compiler) << "JIT instruction set variant " << str;
108 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
109 instruction_set, str.as_string(), &error_msg));
110 if (instruction_set_features_ == nullptr) {
111 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
112 }
113 } else if (option.starts_with("--instruction-set-features=")) {
114 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
115 VLOG(compiler) << "JIT instruction set features " << str;
116 if (instruction_set_features_.get() == nullptr) {
117 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
118 instruction_set, "default", &error_msg));
119 if (instruction_set_features_ == nullptr) {
120 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
121 }
122 }
123 instruction_set_features_.reset(
124 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
125 if (instruction_set_features_ == nullptr) {
126 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
127 }
128 }
129 }
130 if (instruction_set_features_ == nullptr) {
131 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
132 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800133 cumulative_logger_.reset(new CumulativeLogger("jit times"));
134 verification_results_.reset(new VerificationResults(compiler_options_.get()));
135 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
136 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700137 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700138 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800139 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100140 compiler_options_.get(),
141 verification_results_.get(),
142 method_inliner_map_.get(),
143 Compiler::kOptimizing,
144 instruction_set,
145 instruction_set_features_.get(),
146 /* image */ false,
147 /* image_classes */ nullptr,
148 /* compiled_classes */ nullptr,
149 /* compiled_methods */ nullptr,
150 /* thread_count */ 1,
151 /* dump_stats */ false,
152 /* dump_passes */ false,
153 /* dump_cfg_file_name */ "",
Calin Juravle87000a92015-08-24 15:34:44 +0100154 /* dump_cfg_append */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100155 cumulative_logger_.get(),
156 /* swap_fd */ -1,
157 /* profile_file */ ""));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 // Disable dedupe so we can remove compiled methods.
159 compiler_driver_->SetDedupeEnabled(false);
160 compiler_driver_->SetSupportBootImageFixup(false);
161}
162
163JitCompiler::~JitCompiler() {
164}
165
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700167 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700168 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800169 StackHandleScope<2> hs(self);
170 self->AssertNoPendingException();
171 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100172
173 // Check if the method is already compiled.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100174 if (runtime->GetJit()->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175 VLOG(jit) << "Already compiled " << PrettyMethod(method);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100176 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100178
179 // Don't compile the method if we are supposed to be deoptimized.
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000180 instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation();
181 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100182 return false;
183 }
184
185 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100187 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
188 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
189 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800190 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100191
192 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000193 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
194 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700195 {
196 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000197 // If we get a request to compile a proxy method, we pass the actual Java method
198 // of that proxy method, as the compiler does not expect a proxy method.
199 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000200 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700201 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100202
203 // Trim maps to reduce memory usage.
204 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700205 {
206 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700207 runtime->GetArenaPool()->TrimMaps();
208 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100209
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800210 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700211 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000212 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800213}
214
215CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
216 return callbacks_.get();
217}
218
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800219} // namespace jit
220} // namespace art