blob: b323d240385ffc1f26e2ebf3a9ebb67a3ff14b6c [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,
Jeff Haodcdc85b2015-12-04 14:06:18 -080087 /* no_inline_from */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010088 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080089 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010090 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +010091 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010092 /* implicit_null_checks */ true,
93 /* implicit_so_checks */ true,
94 /* implicit_suspend_checks */ false,
95 /* pic */ true, // TODO: Support non-PIC in optimizing.
96 /* verbose_methods */ nullptr,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010097 /* init_failure_output */ nullptr,
98 /* abort_on_hard_verifier_failure */ false));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000099 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
100 compiler_options_->ParseCompilerOption(argument, Usage);
101 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800102 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -0700103 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
104 VLOG(compiler) << "JIT compiler option " << option;
105 std::string error_msg;
106 if (option.starts_with("--instruction-set-variant=")) {
107 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
108 VLOG(compiler) << "JIT instruction set variant " << str;
109 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
110 instruction_set, str.as_string(), &error_msg));
111 if (instruction_set_features_ == nullptr) {
112 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
113 }
114 } else if (option.starts_with("--instruction-set-features=")) {
115 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
116 VLOG(compiler) << "JIT instruction set features " << str;
117 if (instruction_set_features_.get() == nullptr) {
118 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
119 instruction_set, "default", &error_msg));
120 if (instruction_set_features_ == nullptr) {
121 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
122 }
123 }
124 instruction_set_features_.reset(
125 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
126 if (instruction_set_features_ == nullptr) {
127 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
128 }
129 }
130 }
131 if (instruction_set_features_ == nullptr) {
132 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
133 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800134 cumulative_logger_.reset(new CumulativeLogger("jit times"));
135 verification_results_.reset(new VerificationResults(compiler_options_.get()));
136 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
137 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700138 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700139 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800140 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100141 compiler_options_.get(),
142 verification_results_.get(),
143 method_inliner_map_.get(),
144 Compiler::kOptimizing,
145 instruction_set,
146 instruction_set_features_.get(),
147 /* image */ false,
148 /* image_classes */ nullptr,
149 /* compiled_classes */ nullptr,
150 /* compiled_methods */ nullptr,
151 /* thread_count */ 1,
152 /* dump_stats */ false,
153 /* dump_passes */ false,
154 /* dump_cfg_file_name */ "",
Calin Juravle87000a92015-08-24 15:34:44 +0100155 /* dump_cfg_append */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100156 cumulative_logger_.get(),
157 /* swap_fd */ -1,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800158 /* profile_file */ "",
159 /* dex to oat map */ nullptr));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160 // Disable dedupe so we can remove compiled methods.
161 compiler_driver_->SetDedupeEnabled(false);
162 compiler_driver_->SetSupportBootImageFixup(false);
163}
164
165JitCompiler::~JitCompiler() {
166}
167
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700169 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700170 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171 StackHandleScope<2> hs(self);
172 self->AssertNoPendingException();
173 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100174
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100175 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100177 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
178 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
179 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100181
182 // Do the compilation.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000183 bool success = false;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700184 {
185 TimingLogger::ScopedTiming t2("Compiling", &logger);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000186 // If we get a request to compile a proxy method, we pass the actual Java method
187 // of that proxy method, as the compiler does not expect a proxy method.
188 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100189 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000190 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method_to_compile);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700191 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100192
193 // Trim maps to reduce memory usage.
194 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700195 {
196 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700197 runtime->GetArenaPool()->TrimMaps();
198 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100199
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800200 total_time_ += NanoTime() - start_time;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700201 runtime->GetJit()->AddTimingLogger(logger);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000202 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800203}
204
205CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
206 return callbacks_.get();
207}
208
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800209} // namespace jit
210} // namespace art