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

General symbol map #304

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 22 additions & 5 deletions src/enzyme_ad/jax/Passes/LowerKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,9 @@ struct CallInfo {
llvm::StringMap<CallInfo> kernels;
llvm::sys::SmartRWMutex<true> kernel_mutex;
std::unique_ptr<llvm::orc::LLJIT> JIT = nullptr;
llvm::orc::SymbolMap MappedSymbols;

CallInfo CompileHostModule(std::string &key, mlir::ModuleOp modOp,
bool run_init, size_t *cuLaunchPtr,
bool compileInit = true) {
bool initJIT() {
if (!JIT) {
auto tJIT =
llvm::orc::LLJITBuilder()
Expand All @@ -215,7 +214,7 @@ CallInfo CompileHostModule(std::string &key, mlir::ModuleOp modOp,
.create();
if (!tJIT) {
llvm::errs() << " jit creating error: " << tJIT.takeError() << "\n";
return {};
return false;
}
JIT = std::move(tJIT.get());
assert(JIT);
Expand All @@ -230,19 +229,33 @@ CallInfo CompileHostModule(std::string &key, mlir::ModuleOp modOp,
if (!ProcessSymsGenerator) {
llvm::errs() << " failure creating symbol generator: "
<< ProcessSymsGenerator.takeError() << "\n";
return {};
return false;
}

JIT->getMainJITDylib().addGenerator(std::move(ProcessSymsGenerator.get()));
}
return true;
}

extern "C" void EnzymeJaXMapSymbol(const char *name, void *symbol) {
initJIT();
MappedSymbols[JIT->mangleAndIntern(name)] = llvm::orc::ExecutorSymbolDef(
llvm::orc::ExecutorAddr::fromPtr(symbol), llvm::JITSymbolFlags());
}

CallInfo CompileHostModule(std::string &key, mlir::ModuleOp modOp,
bool run_init, size_t *cuLaunchPtr,
bool compileInit = true) {
std::unique_ptr<llvm::LLVMContext> ctx(new llvm::LLVMContext);
auto llvmModule = translateModuleToLLVMIR(modOp, *ctx);
if (!llvmModule) {
llvm::errs() << "modOp: " << *modOp << "\n";
llvm::errs() << "could not convert to LLVM IR\n";
return {};
}
if (!initJIT())
return {};

llvmModule->setDataLayout(JIT->getDataLayout());
llvmModule->setTargetTriple(JIT->getTargetTriple().getTriple());

Expand All @@ -254,6 +267,10 @@ CallInfo CompileHostModule(std::string &key, mlir::ModuleOp modOp,
llvm::errs() << " addIRModuleError " << Err << "\n";
return {};
}
if (auto Err = LibA->define(llvm::orc::absoluteSymbols(MappedSymbols))) {
llvm::errs() << " Symbol define Error " << Err << "\n";
return {};
}

if (cuLaunchPtr && cuLaunchPtr[0] == 0) {
// Look up the JIT'd code entry point.
Expand Down
Loading