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

[orcjit] Using the same name for function name and LLVM module id in LLVM 10 #74903

Open
niyue opened this issue Dec 9, 2023 · 3 comments
Open
Labels

Comments

@niyue
Copy link

niyue commented Dec 9, 2023

When switching Gandiva from MCJIT to LLJIT [1], I find some test case failed on Ubuntu 20.04 + LLVM 10, after some investigation, I find if LLVM module's ModuleID is the same as the function name created in the module, the LLJIT->lookup API returns an symbol successfully, however the symbol's function address is 0.

Could anyone help to confirm if this is a known issue for LLVM 10 (on Linux)? I did some searching on LLVM bug tracker and github but could not find it.

The minimum test case looks roughly like this:

auto module_ = std::make_unique<llvm::Module>("codegen", *context_); // <== pay special attention to the module id
lljit_ = BuildJIT();
auto buffer = LoadBitcodeFile();
auto src_ir_module = getOwningLazyBitcodeModule(std::move(buffer), *context_);
llvm::Linker::linkModules(*module_, std::move(src_ir_module));

std::string fn_name = "codegen"; // <== use the same name as the llvm::Module above
llvm::Function* fn = llvm::Function::Create(
      prototype, llvm::GlobalValue::ExternalLinkage, fn_name, module_.get());
llvm::orc::ThreadSafeModule tsm(std::move(module_), std::move(context_));
auto error = lljit_->addIRModule(std::move(tsm));

auto sym = lljit_->lookup(fn_name);
if (!sym) { // <== the returned sym has no error
    return Status::CodeGenError("Failed to look up function: " + function +
                            " error: " + llvm::toString(sym.takeError()));
}
auto fn_addr = sym->getAddress(); // <== the fn_addr is 0
auto fn_ptr = reinterpret_cast<FnPtr>(fn_addr); // <== this becomes a nullptr
  • the code doesn't work for LLJIT under LLVM 10 + Ubuntu 20.04, but if I change the function name from "codegen" to some different value, the code works
  • the similar code (using the same name for LLVM module and function name) works for MCJIT under LLVM 10 + Ubuntu 20.04
  • the same code works for LLJIT under LLVM 14 + macOS/Ubuntu 22.04

[1] apache/arrow#39098

@EugeneZelenko
Copy link
Contributor

Could you please try 17 or main branch? Only most recent release is supported.

@niyue niyue changed the title Using the same name for function name and LLVM module id in LLVM 10 [orcjit] Using the same name for function name and LLVM module id in LLVM 10 Dec 9, 2023
@dtcxzyw dtcxzyw added orcjit and removed new issue labels Dec 9, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 9, 2023

@llvm/issue-subscribers-orcjit

Author: Yue (niyue)

When switching Gandiva from MCJIT to LLJIT [1], I find some test case failed on Ubuntu 20.04 + LLVM 10, after some investigation, I find if LLVM module's ModuleID is the same as the function name created in the module, the `LLJIT->lookup` API returns an symbol successfully, however the symbol's function address is 0.

Could anyone help to confirm if this is a known issue for LLVM 10 (on Linux)? I did some searching on LLVM bug tracker and github but could not find it.

The minimum test case looks roughly like this:

auto module_ = std::make_unique&lt;llvm::Module&gt;("codegen", *context_); // &lt;== pay special attention to the module id
lljit_ = BuildJIT();
auto buffer = LoadBitcodeFile();
auto src_ir_module = getOwningLazyBitcodeModule(std::move(buffer), *context_);
llvm::Linker::linkModules(*module_, std::move(src_ir_module));

std::string fn_name = "codegen"; // &lt;== use the same name as the llvm::Module above
llvm::Function* fn = llvm::Function::Create(
      prototype, llvm::GlobalValue::ExternalLinkage, fn_name, module_.get());
llvm::orc::ThreadSafeModule tsm(std::move(module_), std::move(context_));
auto error = lljit_-&gt;addIRModule(std::move(tsm));

auto sym = lljit_-&gt;lookup(fn_name);
if (!sym) { // &lt;== the returned sym has no error
    return Status::CodeGenError("Failed to look up function: " + function +
                            " error: " + llvm::toString(sym.takeError()));
}
auto fn_addr = sym-&gt;getAddress(); // &lt;== the fn_addr is 0
auto fn_ptr = reinterpret_cast&lt;FnPtr&gt;(fn_addr); // &lt;== this becomes a nullptr
  • the code doesn't work for LLJIT under LLVM 10 + Ubuntu 20.04, but if I change the function name from "codegen" to some different value, the code works
  • the similar code (using the same name for LLVM module and function name) works for MCJIT under LLVM 10 + Ubuntu 20.04
  • the same code works for LLJIT under LLVM 14 + macOS/Ubuntu 22.04

[1] apache/arrow#39098

@niyue
Copy link
Author

niyue commented Dec 9, 2023

As I said above, LLVM 14 doesn't run into such an issue, so I assume LLVM 17 is good. I just want to confirm if this is a known issue for LLVM 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants