xref: /aosp_15_r20/art/compiler/jit/jit_compiler.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "jit_compiler.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
20*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
21*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set_features.h"
22*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"  // For VLOG
24*795d594fSAndroid Build Coastguard Worker #include "base/systrace.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/timing_logger.h"
27*795d594fSAndroid Build Coastguard Worker #include "compiler.h"
28*795d594fSAndroid Build Coastguard Worker #include "debug/elf_debug_writer.h"
29*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
30*795d594fSAndroid Build Coastguard Worker #include "export/jit_create.h"
31*795d594fSAndroid Build Coastguard Worker #include "jit/debugger_interface.h"
32*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
33*795d594fSAndroid Build Coastguard Worker #include "jit/jit_code_cache.h"
34*795d594fSAndroid Build Coastguard Worker #include "jit/jit_logger.h"
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
37*795d594fSAndroid Build Coastguard Worker namespace jit {
38*795d594fSAndroid Build Coastguard Worker 
Create()39*795d594fSAndroid Build Coastguard Worker JitCompiler* JitCompiler::Create() {
40*795d594fSAndroid Build Coastguard Worker   return new JitCompiler();
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker 
SetDebuggableCompilerOption(bool value)43*795d594fSAndroid Build Coastguard Worker void JitCompiler::SetDebuggableCompilerOption(bool value) {
44*795d594fSAndroid Build Coastguard Worker   compiler_options_->SetDebuggable(value);
45*795d594fSAndroid Build Coastguard Worker }
46*795d594fSAndroid Build Coastguard Worker 
ParseCompilerOptions()47*795d594fSAndroid Build Coastguard Worker void JitCompiler::ParseCompilerOptions() {
48*795d594fSAndroid Build Coastguard Worker   // Special case max code units for inlining, whose default is "unset" (implictly
49*795d594fSAndroid Build Coastguard Worker   // meaning no limit). Do this before parsing the actual passed options.
50*795d594fSAndroid Build Coastguard Worker   compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
51*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
52*795d594fSAndroid Build Coastguard Worker   {
53*795d594fSAndroid Build Coastguard Worker     std::string error_msg;
54*795d594fSAndroid Build Coastguard Worker     if (!compiler_options_->ParseCompilerOptions(runtime->GetCompilerOptions(),
55*795d594fSAndroid Build Coastguard Worker                                                 /*ignore_unrecognized=*/ true,
56*795d594fSAndroid Build Coastguard Worker                                                 &error_msg)) {
57*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << error_msg;
58*795d594fSAndroid Build Coastguard Worker       UNREACHABLE();
59*795d594fSAndroid Build Coastguard Worker     }
60*795d594fSAndroid Build Coastguard Worker   }
61*795d594fSAndroid Build Coastguard Worker   // Set to appropriate JIT compiler type.
62*795d594fSAndroid Build Coastguard Worker   compiler_options_->compiler_type_ = runtime->IsZygote()
63*795d594fSAndroid Build Coastguard Worker       ? CompilerOptions::CompilerType::kSharedCodeJitCompiler
64*795d594fSAndroid Build Coastguard Worker       : CompilerOptions::CompilerType::kJitCompiler;
65*795d594fSAndroid Build Coastguard Worker   // JIT is never PIC, no matter what the runtime compiler options specify.
66*795d594fSAndroid Build Coastguard Worker   compiler_options_->SetNonPic();
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker   // Set the appropriate read barrier option.
69*795d594fSAndroid Build Coastguard Worker   compiler_options_->emit_read_barrier_ = gUseReadBarrier;
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker   // If the options don't provide whether we generate debuggable code, set
72*795d594fSAndroid Build Coastguard Worker   // debuggability based on the runtime value.
73*795d594fSAndroid Build Coastguard Worker   if (!compiler_options_->GetDebuggable()) {
74*795d594fSAndroid Build Coastguard Worker     compiler_options_->SetDebuggable(runtime->IsJavaDebuggable());
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   compiler_options_->implicit_null_checks_ = runtime->GetImplicitNullChecks();
78*795d594fSAndroid Build Coastguard Worker   compiler_options_->implicit_so_checks_ = runtime->GetImplicitStackOverflowChecks();
79*795d594fSAndroid Build Coastguard Worker   compiler_options_->implicit_suspend_checks_ = runtime->GetImplicitSuspendChecks();
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker   const InstructionSet instruction_set = compiler_options_->GetInstructionSet();
82*795d594fSAndroid Build Coastguard Worker   if (kRuntimeISA == InstructionSet::kArm) {
83*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(instruction_set, InstructionSet::kThumb2);
84*795d594fSAndroid Build Coastguard Worker   } else {
85*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(instruction_set, kRuntimeISA);
86*795d594fSAndroid Build Coastguard Worker   }
87*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<const InstructionSetFeatures> instruction_set_features;
88*795d594fSAndroid Build Coastguard Worker   for (const std::string& option : runtime->GetCompilerOptions()) {
89*795d594fSAndroid Build Coastguard Worker     VLOG(compiler) << "JIT compiler option " << option;
90*795d594fSAndroid Build Coastguard Worker     std::string error_msg;
91*795d594fSAndroid Build Coastguard Worker     if (option.starts_with("--instruction-set-variant=")) {
92*795d594fSAndroid Build Coastguard Worker       const char* str = option.c_str() + strlen("--instruction-set-variant=");
93*795d594fSAndroid Build Coastguard Worker       VLOG(compiler) << "JIT instruction set variant " << str;
94*795d594fSAndroid Build Coastguard Worker       instruction_set_features = InstructionSetFeatures::FromVariantAndHwcap(
95*795d594fSAndroid Build Coastguard Worker           instruction_set, str, &error_msg);
96*795d594fSAndroid Build Coastguard Worker       if (instruction_set_features == nullptr) {
97*795d594fSAndroid Build Coastguard Worker         LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
98*795d594fSAndroid Build Coastguard Worker       }
99*795d594fSAndroid Build Coastguard Worker     } else if (option.starts_with("--instruction-set-features=")) {
100*795d594fSAndroid Build Coastguard Worker       const char* str = option.c_str() + strlen("--instruction-set-features=");
101*795d594fSAndroid Build Coastguard Worker       VLOG(compiler) << "JIT instruction set features " << str;
102*795d594fSAndroid Build Coastguard Worker       if (instruction_set_features == nullptr) {
103*795d594fSAndroid Build Coastguard Worker         instruction_set_features = InstructionSetFeatures::FromVariant(
104*795d594fSAndroid Build Coastguard Worker             instruction_set, "default", &error_msg);
105*795d594fSAndroid Build Coastguard Worker         if (instruction_set_features == nullptr) {
106*795d594fSAndroid Build Coastguard Worker           LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
107*795d594fSAndroid Build Coastguard Worker         }
108*795d594fSAndroid Build Coastguard Worker       }
109*795d594fSAndroid Build Coastguard Worker       instruction_set_features =
110*795d594fSAndroid Build Coastguard Worker           instruction_set_features->AddFeaturesFromString(str, &error_msg);
111*795d594fSAndroid Build Coastguard Worker       if (instruction_set_features == nullptr) {
112*795d594fSAndroid Build Coastguard Worker         LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
113*795d594fSAndroid Build Coastguard Worker       }
114*795d594fSAndroid Build Coastguard Worker     }
115*795d594fSAndroid Build Coastguard Worker   }
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker   if (instruction_set_features == nullptr) {
118*795d594fSAndroid Build Coastguard Worker     // '--instruction-set-features/--instruction-set-variant' were not used.
119*795d594fSAndroid Build Coastguard Worker     // Use build-time defined features.
120*795d594fSAndroid Build Coastguard Worker     instruction_set_features = InstructionSetFeatures::FromCppDefines();
121*795d594fSAndroid Build Coastguard Worker   }
122*795d594fSAndroid Build Coastguard Worker   compiler_options_->instruction_set_features_ = std::move(instruction_set_features);
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker   if (compiler_options_->GetGenerateDebugInfo()) {
125*795d594fSAndroid Build Coastguard Worker     jit_logger_.reset(new JitLogger());
126*795d594fSAndroid Build Coastguard Worker     jit_logger_->OpenLog();
127*795d594fSAndroid Build Coastguard Worker   }
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker 
jit_create()130*795d594fSAndroid Build Coastguard Worker JitCompilerInterface* jit_create() {
131*795d594fSAndroid Build Coastguard Worker   VLOG(jit) << "Create jit compiler";
132*795d594fSAndroid Build Coastguard Worker   auto* const jit_compiler = JitCompiler::Create();
133*795d594fSAndroid Build Coastguard Worker   CHECK(jit_compiler != nullptr);
134*795d594fSAndroid Build Coastguard Worker   VLOG(jit) << "Done creating jit compiler";
135*795d594fSAndroid Build Coastguard Worker   return jit_compiler;
136*795d594fSAndroid Build Coastguard Worker }
137*795d594fSAndroid Build Coastguard Worker 
TypesLoaded(mirror::Class ** types,size_t count)138*795d594fSAndroid Build Coastguard Worker void JitCompiler::TypesLoaded(mirror::Class** types, size_t count) {
139*795d594fSAndroid Build Coastguard Worker   const CompilerOptions& compiler_options = GetCompilerOptions();
140*795d594fSAndroid Build Coastguard Worker   if (compiler_options.GetGenerateDebugInfo()) {
141*795d594fSAndroid Build Coastguard Worker     InstructionSet isa = compiler_options.GetInstructionSet();
142*795d594fSAndroid Build Coastguard Worker     const InstructionSetFeatures* features = compiler_options.GetInstructionSetFeatures();
143*795d594fSAndroid Build Coastguard Worker     const ArrayRef<mirror::Class*> types_array(types, count);
144*795d594fSAndroid Build Coastguard Worker     std::vector<uint8_t> elf_file =
145*795d594fSAndroid Build Coastguard Worker         debug::WriteDebugElfFileForClasses(isa, features, types_array);
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker     // NB: Don't allow packing since it would remove non-backtrace data.
148*795d594fSAndroid Build Coastguard Worker     MutexLock mu(Thread::Current(), *Locks::jit_lock_);
149*795d594fSAndroid Build Coastguard Worker     AddNativeDebugInfoForJit(/*code_ptr=*/ nullptr, elf_file, /*allow_packing=*/ false);
150*795d594fSAndroid Build Coastguard Worker   }
151*795d594fSAndroid Build Coastguard Worker }
152*795d594fSAndroid Build Coastguard Worker 
GenerateDebugInfo()153*795d594fSAndroid Build Coastguard Worker bool JitCompiler::GenerateDebugInfo() {
154*795d594fSAndroid Build Coastguard Worker   return GetCompilerOptions().GetGenerateDebugInfo();
155*795d594fSAndroid Build Coastguard Worker }
156*795d594fSAndroid Build Coastguard Worker 
PackElfFileForJIT(ArrayRef<const JITCodeEntry * > elf_files,ArrayRef<const void * > removed_symbols,bool compress,size_t * num_symbols)157*795d594fSAndroid Build Coastguard Worker std::vector<uint8_t> JitCompiler::PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files,
158*795d594fSAndroid Build Coastguard Worker                                                     ArrayRef<const void*> removed_symbols,
159*795d594fSAndroid Build Coastguard Worker                                                     bool compress,
160*795d594fSAndroid Build Coastguard Worker                                                     /*out*/ size_t* num_symbols) {
161*795d594fSAndroid Build Coastguard Worker   return debug::PackElfFileForJIT(elf_files, removed_symbols, compress, num_symbols);
162*795d594fSAndroid Build Coastguard Worker }
163*795d594fSAndroid Build Coastguard Worker 
JitCompiler()164*795d594fSAndroid Build Coastguard Worker JitCompiler::JitCompiler() {
165*795d594fSAndroid Build Coastguard Worker   compiler_options_.reset(new CompilerOptions());
166*795d594fSAndroid Build Coastguard Worker   ParseCompilerOptions();
167*795d594fSAndroid Build Coastguard Worker   compiler_.reset(Compiler::Create(*compiler_options_, /*storage=*/ nullptr));
168*795d594fSAndroid Build Coastguard Worker }
169*795d594fSAndroid Build Coastguard Worker 
~JitCompiler()170*795d594fSAndroid Build Coastguard Worker JitCompiler::~JitCompiler() {
171*795d594fSAndroid Build Coastguard Worker   if (compiler_options_->GetGenerateDebugInfo()) {
172*795d594fSAndroid Build Coastguard Worker     jit_logger_->CloseLog();
173*795d594fSAndroid Build Coastguard Worker   }
174*795d594fSAndroid Build Coastguard Worker }
175*795d594fSAndroid Build Coastguard Worker 
CompileMethod(Thread * self,JitMemoryRegion * region,ArtMethod * method,CompilationKind compilation_kind)176*795d594fSAndroid Build Coastguard Worker bool JitCompiler::CompileMethod(
177*795d594fSAndroid Build Coastguard Worker     Thread* self, JitMemoryRegion* region, ArtMethod* method, CompilationKind compilation_kind) {
178*795d594fSAndroid Build Coastguard Worker   SCOPED_TRACE << "JIT compiling "
179*795d594fSAndroid Build Coastguard Worker                << method->PrettyMethod()
180*795d594fSAndroid Build Coastguard Worker                << " (kind=" << compilation_kind << ")"
181*795d594fSAndroid Build Coastguard Worker                << " from " << method->GetDexFile()->GetLocation();
182*795d594fSAndroid Build Coastguard Worker 
183*795d594fSAndroid Build Coastguard Worker   DCHECK(!method->IsProxyMethod());
184*795d594fSAndroid Build Coastguard Worker   DCHECK(method->GetDeclaringClass()->IsResolved());
185*795d594fSAndroid Build Coastguard Worker 
186*795d594fSAndroid Build Coastguard Worker   TimingLogger logger(
187*795d594fSAndroid Build Coastguard Worker       "JIT compiler timing logger", true, VLOG_IS_ON(jit), TimingLogger::TimingKind::kThreadCpu);
188*795d594fSAndroid Build Coastguard Worker   self->AssertNoPendingException();
189*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
190*795d594fSAndroid Build Coastguard Worker 
191*795d594fSAndroid Build Coastguard Worker   // Do the compilation.
192*795d594fSAndroid Build Coastguard Worker   bool success = false;
193*795d594fSAndroid Build Coastguard Worker   Jit* jit = runtime->GetJit();
194*795d594fSAndroid Build Coastguard Worker   {
195*795d594fSAndroid Build Coastguard Worker     TimingLogger::ScopedTiming t2(compilation_kind == CompilationKind::kOsr
196*795d594fSAndroid Build Coastguard Worker                                       ? "Compiling OSR"
197*795d594fSAndroid Build Coastguard Worker                                       : compilation_kind == CompilationKind::kOptimized
198*795d594fSAndroid Build Coastguard Worker                                           ? "Compiling optimized"
199*795d594fSAndroid Build Coastguard Worker                                           : "Compiling baseline",
200*795d594fSAndroid Build Coastguard Worker                                   &logger);
201*795d594fSAndroid Build Coastguard Worker     JitCodeCache* const code_cache = jit->GetCodeCache();
202*795d594fSAndroid Build Coastguard Worker     metrics::AutoTimer timer{runtime->GetMetrics()->JitMethodCompileTotalTime()};
203*795d594fSAndroid Build Coastguard Worker     success = compiler_->JitCompile(
204*795d594fSAndroid Build Coastguard Worker         self, code_cache, region, method, compilation_kind, jit_logger_.get());
205*795d594fSAndroid Build Coastguard Worker     uint64_t duration_us = timer.Stop();
206*795d594fSAndroid Build Coastguard Worker     VLOG(jit) << "Compilation of " << method->PrettyMethod() << " took "
207*795d594fSAndroid Build Coastguard Worker               << PrettyDuration(UsToNs(duration_us));
208*795d594fSAndroid Build Coastguard Worker     runtime->GetMetrics()->JitMethodCompileCount()->AddOne();
209*795d594fSAndroid Build Coastguard Worker     runtime->GetMetrics()->JitMethodCompileTotalTimeDelta()->Add(duration_us);
210*795d594fSAndroid Build Coastguard Worker     runtime->GetMetrics()->JitMethodCompileCountDelta()->AddOne();
211*795d594fSAndroid Build Coastguard Worker   }
212*795d594fSAndroid Build Coastguard Worker 
213*795d594fSAndroid Build Coastguard Worker   // If we don't have a new task following this compile,
214*795d594fSAndroid Build Coastguard Worker   // trim maps to reduce memory usage.
215*795d594fSAndroid Build Coastguard Worker   if (jit->GetThreadPool() == nullptr || jit->GetThreadPool()->GetTaskCount(self) == 0) {
216*795d594fSAndroid Build Coastguard Worker     TimingLogger::ScopedTiming t2("TrimMaps", &logger);
217*795d594fSAndroid Build Coastguard Worker     runtime->GetJitArenaPool()->TrimMaps();
218*795d594fSAndroid Build Coastguard Worker   }
219*795d594fSAndroid Build Coastguard Worker 
220*795d594fSAndroid Build Coastguard Worker   jit->AddTimingLogger(logger);
221*795d594fSAndroid Build Coastguard Worker   return success;
222*795d594fSAndroid Build Coastguard Worker }
223*795d594fSAndroid Build Coastguard Worker 
IsBaselineCompiler() const224*795d594fSAndroid Build Coastguard Worker bool JitCompiler::IsBaselineCompiler() const {
225*795d594fSAndroid Build Coastguard Worker   return compiler_options_->IsBaseline();
226*795d594fSAndroid Build Coastguard Worker }
227*795d594fSAndroid Build Coastguard Worker 
GetInlineMaxCodeUnits() const228*795d594fSAndroid Build Coastguard Worker uint32_t JitCompiler::GetInlineMaxCodeUnits() const {
229*795d594fSAndroid Build Coastguard Worker   return compiler_options_->GetInlineMaxCodeUnits();
230*795d594fSAndroid Build Coastguard Worker }
231*795d594fSAndroid Build Coastguard Worker 
232*795d594fSAndroid Build Coastguard Worker }  // namespace jit
233*795d594fSAndroid Build Coastguard Worker }  // namespace art
234