Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for libdevice relative to shared library #1176

Merged
merged 4 commits into from
Feb 11, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions lib/Target/LLVMIR/LLVMIRTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/Linker/Linker.h"
#include "llvm/Support/SourceMgr.h"
#include <filesystem>
#include <dlfcn.h>

namespace mlir {
namespace triton {
Expand Down Expand Up @@ -116,22 +117,48 @@ static std::map<std::string, std::string> getExternLibs(mlir::ModuleOp module) {
}

if (!funcs.empty()) {
// When using the Math Dialect, it is possible that some ops (e.g., log) are
// lowered to a function call. In this case, we need to link libdevice
// using its default path:
// [triton root dir]/python/triton/language/libdevice.10.bc
// TODO(Keren): handle external linkage other than libdevice?
namespace fs = std::filesystem;
static const std::string libdevice = "libdevice";
static const std::filesystem::path path = std::filesystem::path(__FILE__)
.parent_path()
.parent_path()
.parent_path()
.parent_path() /
"python" / "triton" /
"third_party" / "cuda" / "lib" /
"libdevice.10.bc";
externLibs.try_emplace(libdevice, path.string());
namespace fs = std::filesystem;
// Search for libdevice relative to its library path if used from Python
// Then native code is in `triton/_C/libtriton.so` and libdevice in
// `triton/third_party/cuda/lib/libdevice.10.bc`
static const auto this_library_path = [] {
Dl_info fileinfo;
if (dladdr(reinterpret_cast<void*>(&getExternLibs), &fileinfo) == 0) {
return std::filesystem::path();
}
return std::filesystem::path(fileinfo.dli_fname);
}();
static const auto runtime_path = this_library_path
.parent_path()
.parent_path() /
"third_party" / "cuda" / "lib" /
"libdevice.10.bc";
if (fs::exists(runtime_path)) {
externLibs.try_emplace(libdevice, runtime_path.string());
} else {
// When using the Math Dialect, it is possible that some ops (e.g., log) are
// lowered to a function call. In this case, we need to link libdevice
// using its default path:
// [triton root dir]/python/triton/language/libdevice.10.bc
// TODO(Keren): handle external linkage other than libdevice?
static const auto this_file_path = std::filesystem::path(__FILE__);
static const auto compiletime_path = this_file_path
.parent_path()
.parent_path()
.parent_path()
.parent_path() /
"python" / "triton" /
"third_party" / "cuda" / "lib" /
"libdevice.10.bc";
if (!fs::exists(compiletime_path)) {
std::string error_msg = "Can't find libdevice at neither " +
runtime_path.string() + " nor " +
compiletime_path.string();
llvm::report_fatal_error(error_msg.c_str());
}
externLibs.try_emplace(libdevice, compiletime_path.string());
}
}

return externLibs;
Expand Down