Calling Numba program in C++ compilation steps

First step: obtain LLVM IR files
I built a simple numba program, compiled it with “numba –dump-llvm numba_file.py > numba_file.ll“ to obtain an IR file1.
I also built a simple C++ program that calls the function defined in numba. I compiled the C++ program using “clang+±20 ….“ to obtain another IR
file2.
Second step: convert/combine LLVM IR to bitecode (*.bc):
I used “llvm-link-20 file1.ll file2.ll to_somefile.bc“

Problem: I have a versioning problem between file1.ll and file2.ll and can’t pass step 2. Installation information: LLVM-20; llvmlite 0.45; numba 0.62. Numba 0.45 has been installed in reference to llvmlite 0.45, which in turn refers to LLVM-20.

Questions: 1) how may I link explicitely numba compilation step to LLVM-20?
2) Are there ways to eliminate versioning problems between numba IR and C++ IR in second step?
3) Any other hint would be appreciated: calling numba program in C++. Thanks.

I have something similar here. It works on my mac, with these requirements.

1 Like

Thanks for the input. I’ll explore your solution further and keep you updated.

Quick question: how did you obtain this long chain in the *.h file? “cfunc_ZN8__main__11calculationB2v1B52c8tJTIeFIjxB2IKSgI4CrvQClUYkACQB1EiFSRRB9GgCAA_3d_3dE10float64_2a10float64_2ad(double* , double* , double )“?
Is after compiling the source.py with numba to IR file? I’ll appreciate it if you could give me more steps/hints.

What did you use to compile the C++ program? gcc, clang, etc. (any hint will be appreciated). Thanks.

Yes you’re correct, the long mangled cfunc… name in calcilation.h and main.cpp is the name that numba creates for the calculation function that it compiles in main.py.

If you run make, you it will (in particular) produce calculation.ll that will contain the LLVM IR code for that function. You can verify that the same named cfunc… function is defined there.

(I suppose a programmatic way of generating that name is accessible using the same mangler that numba is using.)

I compiled this on my mac with clang/llvm20.

My demo also includes creating a shared lib out of ‘another_calculation‘ (besides the static object file compiled from the ‘calculation‘ and linked to the main program). You can ignore it for your purposes.

You can run the demo via the make run command. (It will give the path to numba’s NRT shared lib as an argument to the main executable.)

1 Like

Is the goal simply to call numba code from c++? Or the goal is to call numba code from c++ using LLVM IR?

1 Like

The goal is to call numba code from c++ using LLVM IR. However, your proposed solution is a good biginning. I will explore that, make few changes and let you know how it goes. Thanks so much for your input. It’s a good subject.