compiler_driver.h revision ae7e83817e546848ef6b2949dd9065b153e14316
1/* 2 * Copyright (C) 2011 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#ifndef ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 18#define ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 19 20#include <atomic> 21#include <set> 22#include <string> 23#include <unordered_set> 24#include <vector> 25 26#include "android-base/strings.h" 27 28#include "arch/instruction_set.h" 29#include "base/array_ref.h" 30#include "base/bit_utils.h" 31#include "base/mutex.h" 32#include "base/timing_logger.h" 33#include "class_reference.h" 34#include "compiler.h" 35#include "dex_file.h" 36#include "dex_file_types.h" 37#include "driver/compiled_method_storage.h" 38#include "jit/profile_compilation_info.h" 39#include "method_reference.h" 40#include "mirror/class.h" // For mirror::Class::Status. 41#include "os.h" 42#include "safe_map.h" 43#include "thread_pool.h" 44#include "utils/atomic_dex_ref_map.h" 45#include "utils/dex_cache_arrays_layout.h" 46 47namespace art { 48 49namespace mirror { 50class DexCache; 51} // namespace mirror 52 53namespace verifier { 54class MethodVerifier; 55class VerifierDepsTest; 56} // namespace verifier 57 58class BitVector; 59class CompiledMethod; 60class CompilerOptions; 61class DexCompilationUnit; 62struct InlineIGetIPutData; 63class InstructionSetFeatures; 64class InternTable; 65enum InvokeType : uint32_t; 66class ParallelCompilationManager; 67class ScopedObjectAccess; 68template <class Allocator> class SrcMap; 69template<class T> class Handle; 70class TimingLogger; 71class VdexFile; 72class VerificationResults; 73class VerifiedMethod; 74 75enum EntryPointCallingConvention { 76 // ABI of invocations to a method's interpreter entry point. 77 kInterpreterAbi, 78 // ABI of calls to a method's native code, only used for native methods. 79 kJniAbi, 80 // ABI of calls to a method's quick code entry point. 81 kQuickAbi 82}; 83 84class CompilerDriver { 85 public: 86 // Create a compiler targeting the requested "instruction_set". 87 // "image" should be true if image specific optimizations should be 88 // enabled. "image_classes" lets the compiler know what classes it 89 // can assume will be in the image, with null implying all available 90 // classes. 91 CompilerDriver(const CompilerOptions* compiler_options, 92 VerificationResults* verification_results, 93 Compiler::Kind compiler_kind, 94 InstructionSet instruction_set, 95 const InstructionSetFeatures* instruction_set_features, 96 std::unordered_set<std::string>* image_classes, 97 std::unordered_set<std::string>* compiled_classes, 98 std::unordered_set<std::string>* compiled_methods, 99 size_t thread_count, 100 int swap_fd, 101 const ProfileCompilationInfo* profile_compilation_info); 102 103 ~CompilerDriver(); 104 105 // Set dex files associated with the oat file being compiled. 106 void SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files); 107 108 // Set dex files classpath. 109 void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files); 110 111 // Get dex files associated with the the oat file being compiled. 112 ArrayRef<const DexFile* const> GetDexFilesForOatFile() const { 113 return ArrayRef<const DexFile* const>(dex_files_for_oat_file_); 114 } 115 116 void CompileAll(jobject class_loader, 117 const std::vector<const DexFile*>& dex_files, 118 TimingLogger* timings) 119 REQUIRES(!Locks::mutator_lock_, !dex_to_dex_references_lock_); 120 121 // Compile a single Method. 122 void CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings) 123 REQUIRES_SHARED(Locks::mutator_lock_) 124 REQUIRES(!dex_to_dex_references_lock_); 125 126 VerificationResults* GetVerificationResults() const; 127 128 InstructionSet GetInstructionSet() const { 129 return instruction_set_; 130 } 131 132 const InstructionSetFeatures* GetInstructionSetFeatures() const { 133 return instruction_set_features_; 134 } 135 136 const CompilerOptions& GetCompilerOptions() const { 137 return *compiler_options_; 138 } 139 140 Compiler* GetCompiler() const { 141 return compiler_.get(); 142 } 143 144 const std::unordered_set<std::string>* GetImageClasses() const { 145 return image_classes_.get(); 146 } 147 148 // Generate the trampolines that are invoked by unresolved direct methods. 149 std::unique_ptr<const std::vector<uint8_t>> CreateJniDlsymLookup() const; 150 std::unique_ptr<const std::vector<uint8_t>> CreateQuickGenericJniTrampoline() const; 151 std::unique_ptr<const std::vector<uint8_t>> CreateQuickImtConflictTrampoline() const; 152 std::unique_ptr<const std::vector<uint8_t>> CreateQuickResolutionTrampoline() const; 153 std::unique_ptr<const std::vector<uint8_t>> CreateQuickToInterpreterBridge() const; 154 155 mirror::Class::Status GetClassStatus(const ClassReference& ref) const; 156 bool GetCompiledClass(const ClassReference& ref, mirror::Class::Status* status) const; 157 158 CompiledMethod* GetCompiledMethod(MethodReference ref) const; 159 size_t GetNonRelativeLinkerPatchCount() const; 160 // Add a compiled method. 161 void AddCompiledMethod(const MethodReference& method_ref, 162 CompiledMethod* const compiled_method, 163 size_t non_relative_linker_patch_count); 164 165 void SetRequiresConstructorBarrier(Thread* self, 166 const DexFile* dex_file, 167 uint16_t class_def_index, 168 bool requires) 169 REQUIRES(!requires_constructor_barrier_lock_); 170 171 // Do the <init> methods for this class require a constructor barrier (prior to the return)? 172 // The answer is "yes", if and only if this class has any instance final fields. 173 // (This must not be called for any non-<init> methods; the answer would be "no"). 174 // 175 // --- 176 // 177 // JLS 17.5.1 "Semantics of final fields" mandates that all final fields are frozen at the end 178 // of the invoked constructor. The constructor barrier is a conservative implementation means of 179 // enforcing the freezes happen-before the object being constructed is observable by another 180 // thread. 181 // 182 // Note: This question only makes sense for instance constructors; 183 // static constructors (despite possibly having finals) never need 184 // a barrier. 185 // 186 // JLS 12.4.2 "Detailed Initialization Procedure" approximately describes 187 // class initialization as: 188 // 189 // lock(class.lock) 190 // class.state = initializing 191 // unlock(class.lock) 192 // 193 // invoke <clinit> 194 // 195 // lock(class.lock) 196 // class.state = initialized 197 // unlock(class.lock) <-- acts as a release 198 // 199 // The last operation in the above example acts as an atomic release 200 // for any stores in <clinit>, which ends up being stricter 201 // than what a constructor barrier needs. 202 // 203 // See also QuasiAtomic::ThreadFenceForConstructor(). 204 bool RequiresConstructorBarrier(Thread* self, 205 const DexFile* dex_file, 206 uint16_t class_def_index) 207 REQUIRES(!requires_constructor_barrier_lock_); 208 209 // Are runtime access checks necessary in the compiled code? 210 bool CanAccessTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class, 211 ObjPtr<mirror::Class> resolved_class) 212 REQUIRES_SHARED(Locks::mutator_lock_); 213 214 // Are runtime access and instantiable checks necessary in the code? 215 // out_is_finalizable is set to whether the type is finalizable. 216 bool CanAccessInstantiableTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class, 217 ObjPtr<mirror::Class> resolved_class, 218 bool* out_is_finalizable) 219 REQUIRES_SHARED(Locks::mutator_lock_); 220 221 // Resolve compiling method's class. Returns null on failure. 222 mirror::Class* ResolveCompilingMethodsClass( 223 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 224 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) 225 REQUIRES_SHARED(Locks::mutator_lock_); 226 227 mirror::Class* ResolveClass( 228 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 229 Handle<mirror::ClassLoader> class_loader, dex::TypeIndex type_index, 230 const DexCompilationUnit* mUnit) 231 REQUIRES_SHARED(Locks::mutator_lock_); 232 233 // Resolve a field. Returns null on failure, including incompatible class change. 234 // NOTE: Unlike ClassLinker's ResolveField(), this method enforces is_static. 235 ArtField* ResolveField( 236 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 237 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, 238 uint32_t field_idx, bool is_static) 239 REQUIRES_SHARED(Locks::mutator_lock_); 240 241 // Resolve a field with a given dex file. 242 ArtField* ResolveFieldWithDexFile( 243 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, 244 Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file, 245 uint32_t field_idx, bool is_static) 246 REQUIRES_SHARED(Locks::mutator_lock_); 247 248 // Can we fast-path an IGET/IPUT access to an instance field? If yes, compute the field offset. 249 std::pair<bool, bool> IsFastInstanceField( 250 mirror::DexCache* dex_cache, mirror::Class* referrer_class, 251 ArtField* resolved_field, uint16_t field_idx) 252 REQUIRES_SHARED(Locks::mutator_lock_); 253 254 // Resolve a method. Returns null on failure, including incompatible class change. 255 ArtMethod* ResolveMethod( 256 ScopedObjectAccess& soa, 257 Handle<mirror::DexCache> dex_cache, 258 Handle<mirror::ClassLoader> class_loader, 259 const DexCompilationUnit* mUnit, 260 uint32_t method_idx, 261 InvokeType invoke_type) 262 REQUIRES_SHARED(Locks::mutator_lock_); 263 264 void ProcessedInstanceField(bool resolved); 265 void ProcessedStaticField(bool resolved, bool local); 266 267 // Can we fast path instance field access? Computes field's offset and volatility. 268 bool ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, bool is_put, 269 MemberOffset* field_offset, bool* is_volatile) 270 REQUIRES(!Locks::mutator_lock_); 271 272 ArtField* ComputeInstanceFieldInfo(uint32_t field_idx, 273 const DexCompilationUnit* mUnit, 274 bool is_put, 275 const ScopedObjectAccess& soa) 276 REQUIRES_SHARED(Locks::mutator_lock_); 277 278 279 const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const; 280 bool IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc); 281 282 bool GetSupportBootImageFixup() const { 283 return support_boot_image_fixup_; 284 } 285 286 void SetSupportBootImageFixup(bool support_boot_image_fixup) { 287 support_boot_image_fixup_ = support_boot_image_fixup; 288 } 289 290 void SetCompilerContext(void* compiler_context) { 291 compiler_context_ = compiler_context; 292 } 293 294 void* GetCompilerContext() const { 295 return compiler_context_; 296 } 297 298 size_t GetThreadCount() const { 299 return parallel_thread_count_; 300 } 301 302 void SetDedupeEnabled(bool dedupe_enabled) { 303 compiled_method_storage_.SetDedupeEnabled(dedupe_enabled); 304 } 305 306 bool DedupeEnabled() const { 307 return compiled_method_storage_.DedupeEnabled(); 308 } 309 310 // Checks if class specified by type_idx is one of the image_classes_ 311 bool IsImageClass(const char* descriptor) const; 312 313 // Checks whether the provided class should be compiled, i.e., is in classes_to_compile_. 314 bool IsClassToCompile(const char* descriptor) const; 315 316 // Checks whether the provided method should be compiled, i.e., is in method_to_compile_. 317 bool IsMethodToCompile(const MethodReference& method_ref) const; 318 319 // Checks whether profile guided compilation is enabled and if the method should be compiled 320 // according to the profile file. 321 bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const; 322 323 // Checks whether profile guided verification is enabled and if the method should be verified 324 // according to the profile file. 325 bool ShouldVerifyClassBasedOnProfile(const DexFile& dex_file, uint16_t class_idx) const; 326 327 void RecordClassStatus(const ClassReference& ref, mirror::Class::Status status); 328 329 // Checks if the specified method has been verified without failures. Returns 330 // false if the method is not in the verification results (GetVerificationResults). 331 bool IsMethodVerifiedWithoutFailures(uint32_t method_idx, 332 uint16_t class_def_idx, 333 const DexFile& dex_file) const; 334 335 // Get memory usage during compilation. 336 std::string GetMemoryUsageString(bool extended) const; 337 338 void SetHadHardVerifierFailure() { 339 had_hard_verifier_failure_ = true; 340 } 341 void AddSoftVerifierFailure() { 342 number_of_soft_verifier_failures_++; 343 } 344 345 Compiler::Kind GetCompilerKind() { 346 return compiler_kind_; 347 } 348 349 CompiledMethodStorage* GetCompiledMethodStorage() { 350 return &compiled_method_storage_; 351 } 352 353 // Can we assume that the klass is loaded? 354 bool CanAssumeClassIsLoaded(mirror::Class* klass) 355 REQUIRES_SHARED(Locks::mutator_lock_); 356 357 bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const { 358 if (!kIsTargetBuild) { 359 return MayInlineInternal(inlined_from, inlined_into); 360 } 361 return true; 362 } 363 364 void MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref) 365 REQUIRES(!dex_to_dex_references_lock_); 366 367 const BitVector* GetCurrentDexToDexMethods() const { 368 return current_dex_to_dex_methods_; 369 } 370 371 const ProfileCompilationInfo* GetProfileCompilationInfo() const { 372 return profile_compilation_info_; 373 } 374 375 // Is `boot_image_filename` the name of a core image (small boot 376 // image used for ART testing only)? 377 static bool IsCoreImageFilename(const std::string& boot_image_filename) { 378 // TODO: This is under-approximating... 379 return android::base::EndsWith(boot_image_filename, "core.art") 380 || android::base::EndsWith(boot_image_filename, "core-optimizing.art"); 381 } 382 383 private: 384 void PreCompile(jobject class_loader, 385 const std::vector<const DexFile*>& dex_files, 386 TimingLogger* timings) 387 REQUIRES(!Locks::mutator_lock_); 388 389 void LoadImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_); 390 391 // Attempt to resolve all type, methods, fields, and strings 392 // referenced from code in the dex file following PathClassLoader 393 // ordering semantics. 394 void Resolve(jobject class_loader, 395 const std::vector<const DexFile*>& dex_files, 396 TimingLogger* timings) 397 REQUIRES(!Locks::mutator_lock_); 398 void ResolveDexFile(jobject class_loader, 399 const DexFile& dex_file, 400 const std::vector<const DexFile*>& dex_files, 401 ThreadPool* thread_pool, 402 size_t thread_count, 403 TimingLogger* timings) 404 REQUIRES(!Locks::mutator_lock_); 405 406 // Do fast verification through VerifierDeps if possible. Return whether 407 // verification was successful. 408 bool FastVerify(jobject class_loader, 409 const std::vector<const DexFile*>& dex_files, 410 TimingLogger* timings); 411 412 void Verify(jobject class_loader, 413 const std::vector<const DexFile*>& dex_files, 414 TimingLogger* timings); 415 416 void VerifyDexFile(jobject class_loader, 417 const DexFile& dex_file, 418 const std::vector<const DexFile*>& dex_files, 419 ThreadPool* thread_pool, 420 size_t thread_count, 421 TimingLogger* timings) 422 REQUIRES(!Locks::mutator_lock_); 423 424 void SetVerified(jobject class_loader, 425 const std::vector<const DexFile*>& dex_files, 426 TimingLogger* timings); 427 void SetVerifiedDexFile(jobject class_loader, 428 const DexFile& dex_file, 429 const std::vector<const DexFile*>& dex_files, 430 ThreadPool* thread_pool, 431 size_t thread_count, 432 TimingLogger* timings) 433 REQUIRES(!Locks::mutator_lock_); 434 435 void InitializeClasses(jobject class_loader, 436 const std::vector<const DexFile*>& dex_files, 437 TimingLogger* timings) 438 REQUIRES(!Locks::mutator_lock_); 439 void InitializeClasses(jobject class_loader, 440 const DexFile& dex_file, 441 const std::vector<const DexFile*>& dex_files, 442 TimingLogger* timings) 443 REQUIRES(!Locks::mutator_lock_); 444 445 void UpdateImageClasses(TimingLogger* timings) REQUIRES(!Locks::mutator_lock_); 446 447 void Compile(jobject class_loader, 448 const std::vector<const DexFile*>& dex_files, 449 TimingLogger* timings) REQUIRES(!dex_to_dex_references_lock_); 450 void CompileDexFile(jobject class_loader, 451 const DexFile& dex_file, 452 const std::vector<const DexFile*>& dex_files, 453 ThreadPool* thread_pool, 454 size_t thread_count, 455 TimingLogger* timings) 456 REQUIRES(!Locks::mutator_lock_); 457 458 bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const; 459 460 void InitializeThreadPools(); 461 void FreeThreadPools(); 462 void CheckThreadPools(); 463 464 bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const; 465 466 const CompilerOptions* const compiler_options_; 467 VerificationResults* const verification_results_; 468 469 std::unique_ptr<Compiler> compiler_; 470 Compiler::Kind compiler_kind_; 471 472 const InstructionSet instruction_set_; 473 const InstructionSetFeatures* const instruction_set_features_; 474 475 // All class references that require constructor barriers. If the class reference is not in the 476 // set then the result has not yet been computed. 477 mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 478 std::map<ClassReference, bool> requires_constructor_barrier_ 479 GUARDED_BY(requires_constructor_barrier_lock_); 480 481 // All class references that this compiler has compiled. Indexed by class defs. 482 using ClassStateTable = AtomicDexRefMap<ClassReference, mirror::Class::Status>; 483 ClassStateTable compiled_classes_; 484 // All class references that are in the classpath. Indexed by class defs. 485 ClassStateTable classpath_classes_; 486 487 typedef AtomicDexRefMap<MethodReference, CompiledMethod*> MethodTable; 488 489 private: 490 // All method references that this compiler has compiled. 491 MethodTable compiled_methods_; 492 493 // Number of non-relative patches in all compiled methods. These patches need space 494 // in the .oat_patches ELF section if requested in the compiler options. 495 Atomic<size_t> non_relative_linker_patch_count_; 496 497 // If image_ is true, specifies the classes that will be included in the image. 498 // Note if image_classes_ is null, all classes are included in the image. 499 std::unique_ptr<std::unordered_set<std::string>> image_classes_; 500 501 // Specifies the classes that will be compiled. Note that if classes_to_compile_ is null, 502 // all classes are eligible for compilation (duplication filters etc. will still apply). 503 // This option may be restricted to the boot image, depending on a flag in the implementation. 504 std::unique_ptr<std::unordered_set<std::string>> classes_to_compile_; 505 506 // Specifies the methods that will be compiled. Note that if methods_to_compile_ is null, 507 // all methods are eligible for compilation (compilation filters etc. will still apply). 508 // This option may be restricted to the boot image, depending on a flag in the implementation. 509 std::unique_ptr<std::unordered_set<std::string>> methods_to_compile_; 510 511 std::atomic<uint32_t> number_of_soft_verifier_failures_; 512 bool had_hard_verifier_failure_; 513 514 // A thread pool that can (potentially) run tasks in parallel. 515 std::unique_ptr<ThreadPool> parallel_thread_pool_; 516 size_t parallel_thread_count_; 517 518 // A thread pool that guarantees running single-threaded on the main thread. 519 std::unique_ptr<ThreadPool> single_thread_pool_; 520 521 class AOTCompilationStats; 522 std::unique_ptr<AOTCompilationStats> stats_; 523 524 typedef void (*CompilerCallbackFn)(CompilerDriver& driver); 525 typedef MutexLock* (*CompilerMutexLockFn)(CompilerDriver& driver); 526 527 void* compiler_context_; 528 529 bool support_boot_image_fixup_; 530 531 // List of dex files associates with the oat file. 532 std::vector<const DexFile*> dex_files_for_oat_file_; 533 534 CompiledMethodStorage compiled_method_storage_; 535 536 // Info for profile guided compilation. 537 const ProfileCompilationInfo* const profile_compilation_info_; 538 539 size_t max_arena_alloc_; 540 541 // Data for delaying dex-to-dex compilation. 542 Mutex dex_to_dex_references_lock_; 543 // In the first phase, dex_to_dex_references_ collects methods for dex-to-dex compilation. 544 class DexFileMethodSet; 545 std::vector<DexFileMethodSet> dex_to_dex_references_ GUARDED_BY(dex_to_dex_references_lock_); 546 // In the second phase, current_dex_to_dex_methods_ points to the BitVector with method 547 // indexes for dex-to-dex compilation in the current dex file. 548 const BitVector* current_dex_to_dex_methods_; 549 550 friend class CompileClassVisitor; 551 friend class DexToDexDecompilerTest; 552 friend class verifier::VerifierDepsTest; 553 DISALLOW_COPY_AND_ASSIGN(CompilerDriver); 554}; 555 556} // namespace art 557 558#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_H_ 559