Skip to content

Commit

Permalink
[XLA:CPU] Decouple object loading from JIT compiler.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 707069250
  • Loading branch information
Google-ML-Automation committed Jan 9, 2025
1 parent 2f6eabb commit 7d7b291
Show file tree
Hide file tree
Showing 7 changed files with 480 additions and 7 deletions.
66 changes: 62 additions & 4 deletions xla/backends/cpu/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ cc_library(
srcs = ["contiguous_section_memory_manager.cc"],
hdrs = ["contiguous_section_memory_manager.h"],
deps = [
"//xla:util",
"@llvm-project//llvm:Core",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@llvm-project//llvm:ExecutionEngine",
"@llvm-project//llvm:OrcJIT",
"@llvm-project//llvm:Support",
"@tsl//tsl/platform:logging",
# TODO(basioli): This dependency increases the binary size significantly.
# Consider reducing the dependency size, or use something alternative.
"//xla:util",
],
)

Expand Down Expand Up @@ -93,6 +94,7 @@ cc_library(
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/log",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down Expand Up @@ -353,3 +355,59 @@ cc_library(
"@llvm-project//llvm:OrcJIT",
],
)

cc_library(
name = "object_loader",
srcs = ["object_loader.cc"],
hdrs = ["object_loader.h"],
deps = [
":compiled_function_library",
":contiguous_section_memory_manager",
"//xla/backends/cpu/runtime:function_library",
"//xla/service/cpu:orc_jit_memory_mapper",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:span",
"@llvm-project//llvm:JITLink",
"@llvm-project//llvm:OrcShared",
"@llvm-project//llvm:Support",
"@llvm-project//llvm:ir_headers",
],
)

xla_cc_test(
name = "object_loader_test",
srcs = ["object_loader_test.cc"],
deps = [
":ir_compiler",
":jit_compiler",
":object_loader",
"//xla:util",
"//xla:xla_data_proto_cc",
"//xla/backends/cpu/runtime:function_library",
"//xla/service:cpu_plugin",
"//xla/service/cpu:executable_proto_cc",
"//xla/service/llvm_ir:llvm_util",
"//xla/tsl/lib/core:status_test_util",
"//xla/tsl/platform:errors",
"//xla/tsl/platform:statusor",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest_main",
"@llvm-project//llvm:AsmParser",
"@llvm-project//llvm:JITLink",
"@llvm-project//llvm:Object",
"@llvm-project//llvm:Support",
"@llvm-project//llvm:Target",
"@llvm-project//llvm:ir_headers",
"@tsl//tsl/platform:statusor",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ limitations under the License.
#include <string>
#include <system_error> // NOLINT

#include "absl/log/check.h"
#include "absl/log/log.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/Process.h"
#include "xla/util.h"
#include "tsl/platform/logging.h"

namespace xla::cpu {
namespace {
Expand Down
2 changes: 1 addition & 1 deletion xla/backends/cpu/codegen/jit_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
#include "absl/base/call_once.h"
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -57,7 +58,6 @@ limitations under the License.
#include "xla/service/cpu/orc_jit_memory_mapper.h"
#include "xla/util.h"
#include "tsl/platform/cpu_info.h"
#include "tsl/platform/logging.h"
#include "tsl/platform/statusor.h"
#include "tsl/profiler/lib/traceme.h"
#include "tsl/profiler/lib/traceme_encode.h"
Expand Down
174 changes: 174 additions & 0 deletions xla/backends/cpu/codegen/object_loader.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/* Copyright 2024 The OpenXLA Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "xla/backends/cpu/codegen/object_loader.h"

#include <algorithm>
#include <cstddef>
#include <memory>
#include <string>
#include <utility>

#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "xla/backends/cpu/codegen/compiled_function_library.h"
#include "xla/backends/cpu/codegen/contiguous_section_memory_manager.h"
#include "xla/backends/cpu/runtime/function_library.h"
#include "xla/service/cpu/orc_jit_memory_mapper.h"

namespace xla::cpu {

static std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayer>
CreateObjectLinkingLayer(llvm::orc::ExecutionSession& execution_session) {
return std::make_unique<llvm::orc::RTDyldObjectLinkingLayer>(
execution_session, [] {
return std::make_unique<ContiguousSectionMemoryManager>(
orc_jit_memory_mapper::GetInstance());
});
}

ObjectLoader::ObjectLoader(size_t num_dylibs)
/*: target_machine_(std::move(target_machine))*/ {
// LLVM execution session that holds jit-compiled functions.
execution_session_ = std::make_unique<llvm::orc::ExecutionSession>(
std::make_unique<llvm::orc::UnsupportedExecutorProcessControl>(
/*SSP=*/nullptr, /*D=*/nullptr));

execution_session_->setErrorReporter([](llvm::Error err) {
LOG(ERROR) << "LLVM compilation error: " << llvm::toString(std::move(err));
});

// Create at least one dynamic library for the given jit compiler.
dylibs_.resize(std::max<size_t>(1, num_dylibs));
for (size_t i = 0; i < dylibs_.size(); ++i) {
dylibs_[i] = &execution_session_->createBareJITDylib(
absl::StrCat("<xla_jit_dylib_", i, ">"));
// TODO using target machine might bring some deps we don't need.
// as a first attempt fully remove it, consider pruning the reqs
// if (definition_generator) {
// dylibs_[i]->addGenerator(definition_generator(target_machine_.get()));
// }
}

object_layer_ = CreateObjectLinkingLayer(*execution_session_);
}

absl::Status ObjectLoader::AddObjFile(const std::string& obj_file,
const std::string& memory_buffer_name,
size_t dylib_index) {
if (dylib_index >= dylibs_.size()) {
return absl::Status(
absl::StatusCode::kInvalidArgument,
absl::StrFormat("Invalid dylib index %d (num dylibs: %d))", dylib_index,
dylibs_.size()));
}

llvm::StringRef data(obj_file.data(), obj_file.size());

auto obj_file_mem_buffer =
llvm::MemoryBuffer::getMemBuffer(data, memory_buffer_name);

if (!obj_file_mem_buffer) {
return absl::Status(absl::StatusCode::kInvalidArgument,
"Failed to create memory buffer");
}

llvm::orc::JITDylib* dylib = dylibs_[dylib_index];
if (auto err = object_layer_->add(*dylib, std::move(obj_file_mem_buffer))) {
return absl::Status(
absl::StatusCode::kInvalidArgument,
absl::StrFormat("Failed to add object file to dylib %d: %s",
dylib_index, llvm::toString(std::move(err))));
}

return absl::OkStatus();
}

absl::StatusOr<std::unique_ptr<FunctionLibrary>> ObjectLoader::Load(
absl::Span<const Symbol> symbols, const llvm::DataLayout& data_layout) && {
// Mangle symbol names for the target machine data layout.
auto mangle = [&](absl::string_view name) {
llvm::SmallVector<char, 40> mangled;
llvm::Mangler::getNameWithPrefix(mangled, name, data_layout);
return std::string(mangled.begin(), mangled.end());
};

// Build a symbol lookup set.
llvm::orc::SymbolLookupSet lookup_set;
for (const auto& symbol : symbols) {
VLOG(5) << absl::StreamFormat(" - look up symbol: %s", symbol.name);
lookup_set.add(execution_session_->intern(mangle(symbol.name)));
}

// Build a search order for the dynamic libraries.
llvm::orc::JITDylibSearchOrder search_order(dylibs_.size());
for (size_t i = 0; i < dylibs_.size(); ++i) {
search_order[i] = std::make_pair(
dylibs_[i], llvm::orc::JITDylibLookupFlags::MatchExportedSymbolsOnly);
}

// Look up all requested symbols in the execution session.
auto symbol_map = execution_session_->lookup(std::move(search_order),
std::move(lookup_set));

if (auto err = symbol_map.takeError()) {
return absl::Status(absl::StatusCode::kInternal,
absl::StrFormat("%s", llvm::toString(std::move(err))));
}

// Resolve type-erased symbol pointers from the symbol map.
using ResolvedSymbol = CompiledFunctionLibrary::ResolvedSymbol;
absl::flat_hash_map<std::string, ResolvedSymbol> resolved_map;

for (const auto& symbol : symbols) {
auto symbol_name = execution_session_->intern(mangle(symbol.name));
llvm::orc::ExecutorSymbolDef symbol_def = symbol_map->at(symbol_name);
llvm::orc::ExecutorAddr symbol_addr = symbol_def.getAddress();
void* ptr = reinterpret_cast<void*>(symbol_addr.getValue());
resolved_map[symbol.name] = ResolvedSymbol{symbol.type_id, ptr};
}

return std::make_unique<CompiledFunctionLibrary>(
std::move(execution_session_), std::move(object_layer_),
std::move(resolved_map));
}

ObjectLoader::~ObjectLoader() {
if (execution_session_) {
if (auto err = execution_session_->endSession()) {
execution_session_->reportError(std::move(err));
}
}
}

} // namespace xla::cpu
79 changes: 79 additions & 0 deletions xla/backends/cpu/codegen/object_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright 2024 The OpenXLA Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef XLA_BACKENDS_CPU_CODEGEN_OBJECT_LOADER_H_
#define XLA_BACKENDS_CPU_CODEGEN_OBJECT_LOADER_H_

#include <cstddef>
#include <memory>
#include <string>
#include <vector>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/IR/DataLayout.h"
#include "xla/backends/cpu/runtime/function_library.h"

namespace xla::cpu {

class ObjectLoader {
public:
using Symbol = FunctionLibrary::Symbol;

explicit ObjectLoader(size_t num_dylibs);

absl::Status AddObjFile(const std::string& obj_file,
const std::string& memory_buffer_name,
size_t dylib_index = 0);

absl::StatusOr<std::unique_ptr<FunctionLibrary>> Load(
absl::Span<const Symbol> symbols, const llvm::DataLayout& data_layout) &&;

llvm::orc::RTDyldObjectLinkingLayer* object_layer() {
return object_layer_.get();
}

llvm::orc::ExecutionSession* execution_session() {
return execution_session_.get();
}

absl::StatusOr<llvm::orc::JITDylib*> dylib(size_t dylib_index) {
if (dylib_index >= dylibs_.size()) {
return absl::Status(
absl::StatusCode::kInvalidArgument,
absl::StrFormat("Invalid dylib index %d (num dylibs: %d))",
dylib_index, dylibs_.size()));
}
return dylibs_[dylib_index];
}

~ObjectLoader();

private:
std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayer> object_layer_;
std::unique_ptr<llvm::orc::ExecutionSession> execution_session_;

// Non-owning pointers to dynamic libraries created for the execution session.
std::vector<llvm::orc::JITDylib*> dylibs_;

// std::shared_ptr<llvm::TargetMachine> target_machine_;
};

} // namespace xla::cpu

#endif // XLA_BACKENDS_CPU_CODEGEN_OBJECT_LOADER_H_
Loading

0 comments on commit 7d7b291

Please sign in to comment.