Fix the LLVM build. Separate out libart-compiler-llvm.so.
After this refactoring:
If (!USE_LLVM_COMPILER), I pass the test-art and boot the phone alright.
The behavior is the same for non-LLVM builds as before. Multi-target
art-compiler shared libraries are generated as before.
If (USE_LLVM_COMPILER), I generate libart-compiler-llvm.so successfully.
Note that the ideal refactoring for the next step will be to always build
art-compiler and LLVM-[arm|mips|x86] shared libraries. Currently, we
haven't removed all the ifdefs for libart.so yet, so this is not ideal yet.
Reason for not-done-yet is that the next step requires discussions.
I can remove all the ifdefs today, but then the (!USE_LLVM_COMPILER)
build will incur extra fields and overhead.
This refactoring allows us to restore the LLVM build situation to be before
the breakage yesterday, so we can continue making forward progress again.
One difficulty in the refactoring is that LLVM is not a method compiler
in default, unlike (!USE_LLVM_COMPILER). LLVM actually compiles an LLVM module
at a time normally. So we need to do more call backs and tell
libart-compiler-llvm.so in each invocation where we are in terms of
OatCompilationUnit.
Another difficulty is that currently our LLVM compiler is not
multithreaded and requires locking, unlike (!USE_LLVM_COMPILER). So more
callbacks are needed. This will be fixed when we fix the multithreading
issue.
Change-Id: I93bce21b6d673254188f2a60b1a7f91b508e497f
diff --git a/src/common_test.h b/src/common_test.h
index 7da5c77..29ed57c 100644
--- a/src/common_test.h
+++ b/src/common_test.h
@@ -352,7 +352,7 @@
class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
compiler_.reset(new Compiler(instruction_set, false, 2, false, NULL));
#if defined(ART_USE_LLVM_COMPILER)
- compiler_->GetCompilerLLVM()->SetElfFileName("gtest");
+ compiler_->SetElfFileName("gtest");
#endif
Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption before the test
diff --git a/src/compiled_method.cc b/src/compiled_method.cc
index 259ee44..e122816 100644
--- a/src/compiled_method.cc
+++ b/src/compiled_method.cc
@@ -24,7 +24,7 @@
: instruction_set_(instruction_set), func_(func), frame_size_in_bytes_(0),
core_spill_mask_(0), fp_spill_mask_(0) {
}
-#else
+#endif
CompiledMethod::CompiledMethod(InstructionSet instruction_set,
const std::vector<uint8_t>& code,
const size_t frame_size_in_bytes,
@@ -68,7 +68,6 @@
DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask) + __builtin_popcount(fp_spill_mask)));
}
-#endif
void CompiledMethod::SetGcMap(const std::vector<uint8_t>& gc_map) {
CHECK_NE(gc_map.size(), 0U);
@@ -180,12 +179,11 @@
CompiledInvokeStub::CompiledInvokeStub(llvm::Function* func) : func_(func) {
CHECK_NE(func, static_cast<llvm::Function*>(NULL));
}
-#else
+#endif
CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
CHECK_NE(code.size(), 0U);
code_ = code;
}
-#endif
CompiledInvokeStub::~CompiledInvokeStub() {}
diff --git a/src/compiled_method.h b/src/compiled_method.h
index 715b5d2..e6fdbaa 100644
--- a/src/compiled_method.h
+++ b/src/compiled_method.h
@@ -34,7 +34,7 @@
// Create a CompiledMethod from the oatCompileMethod
CompiledMethod(InstructionSet instruction_set,
llvm::Function* func);
-#else
+#endif
// Create a CompiledMethod from the oatCompileMethod
CompiledMethod(InstructionSet instruction_set,
const std::vector<uint8_t>& code,
@@ -43,7 +43,6 @@
const uint32_t fp_spill_mask,
const std::vector<uint32_t>& mapping_table,
const std::vector<uint16_t>& vmap_table);
-#endif
// Add a GC map to a CompiledMethod created by oatCompileMethod
void SetGcMap(const std::vector<uint8_t>& gc_map);
@@ -100,9 +99,8 @@
public:
#if defined(ART_USE_LLVM_COMPILER)
explicit CompiledInvokeStub(llvm::Function* func);
-#else
- explicit CompiledInvokeStub(std::vector<uint8_t>& code);
#endif
+ explicit CompiledInvokeStub(std::vector<uint8_t>& code);
~CompiledInvokeStub();
const std::vector<uint8_t>& GetCode() const;
private:
diff --git a/src/compiler.cc b/src/compiler.cc
index 3f7f637..447d6f5 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -40,10 +40,6 @@
#include <mach-o/dyld.h>
#endif
-#if defined(ART_USE_LLVM_COMPILER)
-#include "compiler_llvm/compiler_llvm.h"
-#endif
-
namespace art {
namespace arm {
@@ -230,7 +226,11 @@
#endif
// Work out the filename for the compiler library.
+#if !defined(ART_USE_LLVM_COMPILER)
std::string library_name(StringPrintf("art%s-compiler-%s", suffix, instruction_set_name.c_str()));
+#else
+ std::string library_name(StringPrintf("art%s-compiler-llvm", suffix));
+#endif
std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str()));
#if defined(__APPLE__)
@@ -282,16 +282,15 @@
support_debugging_(support_debugging),
stats_(new AOTCompilationStats),
image_classes_(image_classes),
-#if !defined(ART_USE_LLVM_COMPILER)
+#if defined(ART_USE_LLVM_COMPILER)
+ compiler_llvm_(NULL),
+#endif
compiler_library_(NULL),
compiler_(NULL),
jni_compiler_(NULL),
create_invoke_stub_(NULL)
-#else
- compiler_llvm_(new compiler_llvm::CompilerLLVM(this, instruction_set))
-#endif
{
- std::string compiler_so_name(MakeCompilerSoName(instruction_set));
+ std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY);
if (compiler_library_ == NULL) {
LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror();
@@ -321,6 +320,12 @@
MutexLock mu(compiled_invoke_stubs_lock_);
STLDeleteValues(&compiled_invoke_stubs_);
}
+#if defined(ART_USE_LLVM_COMPILER)
+ CompilerCallbackFn f = FindFunction<CompilerCallbackFn>(MakeCompilerSoName(instruction_set_),
+ compiler_library_,
+ "compilerLLVMDispose");
+ (*f)(*this);
+#endif
if (compiler_library_ != NULL) {
VLOG(compiler) << "dlclose(" << compiler_library_ << ")";
dlclose(compiler_library_);
@@ -427,7 +432,10 @@
const std::vector<const DexFile*>& dex_files) {
SetGcMaps(class_loader, dex_files);
#if defined(ART_USE_LLVM_COMPILER)
- compiler_llvm_->MaterializeRemainder();
+ CompilerCallbackFn f = FindFunction<CompilerCallbackFn>(MakeCompilerSoName(instruction_set_),
+ compiler_library_,
+ "compilerLLVMMaterializeRemainder");
+ (*f)(*this);
#endif
}
@@ -1057,13 +1065,16 @@
const ClassLoader* class_loader = context->GetClassLoader();
const DexFile& dex_file = *context->GetDexFile();
const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
-
#if defined(ART_USE_LLVM_COMPILER)
- compiler_llvm::CompilerLLVM* compiler_llvm = context->GetCompiler()->GetCompilerLLVM();
-
- MutexLock GUARD(compiler_llvm->compiler_lock_);
// TODO: Remove this. We should not lock the compiler_lock_ in CompileClass()
- // However, without this mutex lock, we will get segmentation fault.
+ // However, without this mutex lock, we will get segmentation fault before
+ // LLVM becomes multithreaded.
+ Compiler* cmplr = context->GetCompiler();
+ CompilerMutexLockFn f =
+ FindFunction<CompilerMutexLockFn>(MakeCompilerSoName(cmplr->GetInstructionSet()),
+ cmplr->compiler_library_,
+ "compilerLLVMMutexLock");
+ UniquePtr<MutexLock> GUARD((*f)(*cmplr));
#endif
if (SkipClass(class_loader, dex_file, class_def)) {
@@ -1102,7 +1113,11 @@
DCHECK(!it.HasNext());
#if defined(ART_USE_LLVM_COMPILER)
- compiler_llvm->MaterializeIfThresholdReached();
+ CompilerCallbackFn fn =
+ FindFunction<CompilerCallbackFn>(MakeCompilerSoName(cmplr->GetInstructionSet()),
+ cmplr->compiler_library_,
+ "compilerLLVMMaterializeIfThresholdReached");
+ (*fn)(*cmplr);
#endif
}
@@ -1117,30 +1132,13 @@
CompiledMethod* compiled_method = NULL;
uint64_t start_ns = NanoTime();
-#if defined(ART_USE_LLVM_COMPILER)
- ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
- DexCache *dex_cache = class_linker->FindDexCache(dex_file);
-
- OatCompilationUnit oat_compilation_unit(
- class_loader, class_linker, dex_file, *dex_cache, code_item,
- method_idx, access_flags);
-#endif
-
if ((access_flags & kAccNative) != 0) {
-#if defined(ART_USE_LLVM_COMPILER)
- compiled_method = compiler_llvm_->CompileNativeMethod(&oat_compilation_unit);
-#else
compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, class_loader, dex_file);
-#endif
CHECK(compiled_method != NULL);
} else if ((access_flags & kAccAbstract) != 0) {
} else {
-#if defined(ART_USE_LLVM_COMPILER)
- compiled_method = compiler_llvm_->CompileDexMethod(&oat_compilation_unit);
-#else
compiled_method = (*compiler_)(*this, code_item, access_flags, method_idx, class_loader,
dex_file);
-#endif
CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
}
uint64_t duration_ns = NanoTime() - start_ns;
@@ -1163,7 +1161,7 @@
const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
if (compiled_invoke_stub == NULL) {
#if defined(ART_USE_LLVM_COMPILER)
- compiled_invoke_stub = compiler_llvm_->CreateInvokeStub(is_static, shorty);
+ compiled_invoke_stub = (*create_invoke_stub_)(*this, is_static, shorty, shorty_len);
#else
compiled_invoke_stub = (*create_invoke_stub_)(is_static, shorty, shorty_len);
#endif
@@ -1287,11 +1285,17 @@
#if defined(ART_USE_LLVM_COMPILER)
void Compiler::SetElfFileName(std::string const& filename) {
- compiler_llvm_->SetElfFileName(filename);
+ elf_filename_ = filename;
}
void Compiler::SetBitcodeFileName(std::string const& filename) {
- compiler_llvm_->SetBitcodeFileName(filename);
+ bitcode_filename_ = filename;
+}
+std::string const& Compiler::GetElfFileName() {
+ return elf_filename_;
+}
+std::string const& Compiler::GetBitcodeFileName() {
+ return bitcode_filename_;
}
#endif
diff --git a/src/compiler.h b/src/compiler.h
index e53cc46..134e71b 100644
--- a/src/compiler.h
+++ b/src/compiler.h
@@ -30,12 +30,14 @@
#include "object.h"
#include "runtime.h"
-#if defined(ART_USE_LLVM_COMPILER)
-#include "compiler_llvm/compiler_llvm.h"
-#endif
-
namespace art {
+#if defined(ART_USE_LLVM_COMPILER)
+namespace compiler_llvm {
+class CompilerLLVM;
+}
+#endif
+
class AOTCompilationStats;
class Context;
class OatCompilationUnit;
@@ -125,9 +127,14 @@
#if defined(ART_USE_LLVM_COMPILER)
void SetElfFileName(std::string const& filename);
void SetBitcodeFileName(std::string const& filename);
+ std::string const& GetElfFileName();
+ std::string const& GetBitcodeFileName();
+ void SetCompilerLLVM(compiler_llvm::CompilerLLVM* compiler_llvm) {
+ compiler_llvm_ = compiler_llvm;
+ }
compiler_llvm::CompilerLLVM* GetCompilerLLVM() const {
- return compiler_llvm_.get();
+ return compiler_llvm_;
}
#endif
@@ -195,8 +202,12 @@
const std::set<std::string>* image_classes_;
#if defined(ART_USE_LLVM_COMPILER)
- UniquePtr<compiler_llvm::CompilerLLVM> compiler_llvm_;
-#else
+ compiler_llvm::CompilerLLVM* compiler_llvm_;
+ std::string elf_filename_;
+ std::string bitcode_filename_;
+ typedef void (*CompilerCallbackFn)(Compiler& compiler);
+ typedef MutexLock* (*CompilerMutexLockFn)(Compiler& compiler);
+#endif
void* compiler_library_;
typedef CompiledMethod* (*CompilerFn)(Compiler& compiler,
@@ -211,11 +222,14 @@
const ClassLoader* class_loader,
const DexFile& dex_file);
JniCompilerFn jni_compiler_;
-
+#if !defined(ART_USE_LLVM_COMPILER)
typedef CompiledInvokeStub* (*CreateInvokeStubFn)(bool is_static,
const char* shorty, uint32_t shorty_len);
- CreateInvokeStubFn create_invoke_stub_;
+#else
+ typedef CompiledInvokeStub* (*CreateInvokeStubFn)(Compiler& compiler, bool is_static,
+ const char* shorty, uint32_t shorty_len);
#endif
+ CreateInvokeStubFn create_invoke_stub_;
DISALLOW_COPY_AND_ASSIGN(Compiler);
};
diff --git a/src/compiler_llvm/compiler_llvm.cc b/src/compiler_llvm/compiler_llvm.cc
index 67b3f4d..5a3725e 100644
--- a/src/compiler_llvm/compiler_llvm.cc
+++ b/src/compiler_llvm/compiler_llvm.cc
@@ -16,8 +16,10 @@
#include "compiler_llvm.h"
+#include "class_linker.h"
#include "compilation_unit.h"
#include "compiler.h"
+#include "dex_cache.h"
#include "ir_builder.h"
#include "jni_compiler.h"
#include "method_compiler.h"
@@ -207,3 +209,78 @@
} // namespace compiler_llvm
} // namespace art
+
+namespace {
+
+void ensureCompilerLLVM(art::Compiler& compiler) {
+ if (compiler.GetCompilerLLVM() == NULL) {
+ compiler.SetCompilerLLVM(new art::compiler_llvm::CompilerLLVM(&compiler,
+ compiler.GetInstructionSet()));
+ }
+ art::compiler_llvm::CompilerLLVM* compiler_llvm = compiler.GetCompilerLLVM();
+ compiler_llvm->SetElfFileName(compiler.GetElfFileName());
+ compiler_llvm->SetBitcodeFileName(compiler.GetBitcodeFileName());
+}
+
+} // anonymous namespace
+
+extern "C" art::CompiledMethod* oatCompileMethod(art::Compiler& compiler,
+ const art::DexFile::CodeItem* code_item,
+ uint32_t access_flags, uint32_t method_idx,
+ const art::ClassLoader* class_loader,
+ const art::DexFile& dex_file)
+{
+ ensureCompilerLLVM(compiler);
+
+ art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
+ art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
+
+ art::OatCompilationUnit oat_compilation_unit(
+ class_loader, class_linker, dex_file, *dex_cache, code_item,
+ method_idx, access_flags);
+
+ return compiler.GetCompilerLLVM()->CompileDexMethod(&oat_compilation_unit);
+}
+
+extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
+ uint32_t access_flags, uint32_t method_idx,
+ const art::ClassLoader* class_loader,
+ const art::DexFile& dex_file) {
+ ensureCompilerLLVM(compiler);
+
+ art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker();
+ art::DexCache *dex_cache = class_linker->FindDexCache(dex_file);
+
+ art::OatCompilationUnit oat_compilation_unit(
+ class_loader, class_linker, dex_file, *dex_cache, NULL,
+ method_idx, access_flags);
+
+ return compiler.GetCompilerLLVM()->CompileNativeMethod(&oat_compilation_unit);
+}
+
+extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static,
+ const char* shorty, uint32_t shorty_len) {
+ ensureCompilerLLVM(compiler);
+ //shorty_len = 0; // To make the compiler happy
+ return compiler.GetCompilerLLVM()->CreateInvokeStub(is_static, shorty);
+}
+
+extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) {
+ ensureCompilerLLVM(compiler);
+ compiler.GetCompilerLLVM()->MaterializeRemainder();
+}
+
+extern "C" void compilerLLVMMaterializeIfThresholdReached(art::Compiler& compiler) {
+ ensureCompilerLLVM(compiler);
+ compiler.GetCompilerLLVM()->MaterializeIfThresholdReached();
+}
+
+// Note: Using this function carefully!!! This is temporary solution, we will remove it.
+extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) {
+ ensureCompilerLLVM(compiler);
+ return new art::MutexLock(compiler.GetCompilerLLVM()->compiler_lock_);
+}
+
+extern "C" void compilerLLVMDispose(art::Compiler& compiler) {
+ delete compiler.GetCompilerLLVM();
+}