The Advanced Compilation Topics talk in the WTC 2018 shows that one can easily link C++ code through the new compiler technology. This feature seems very useful as one can easily load existing C++ code into Mathematica without extensive modifications.
I followed the instruction but unable to make it work:
Needs["Compile`"]
Create the C++ source file
code="extern \"C\"
double square(double a)
{return a * a;}
";
WriteString["/tmp/e.cpp",code]
Compile the C++ code to llvm
Run["clang -S -Wno-everything -O3 -o /tmp/e.ll -emit-llvm -march=skylake /tmp/e.cpp"]
Import the llvm
llvmStr = Import["/tmp/e.ll", "Text"];
prog = Program[{
LLVMString[llvmStr],
Function[{Typed[arg1, "Real64"]},
Native`PrimitiveFunction["square"][arg1]]
}];
Compile to external program
fun = CompileToExternalFunction[prog]
I'm getting an error
Compile: CompilerException. Type kind is not handled
I'm using Mathematica V12 on ubuntu 16.04.
Any ideas about the error and the correct way to compile?
Program
andLLVMString
. Does one need someNeeds[]
? $\endgroup$int square(int a)
$\endgroup$FunctionCompile
with LLVM is finally here in 12.3 andFunctionCompileExportLibrary
. But there is not aFunctionCompileImport...
yet. I've had a go at hacking the associations returned byFunctionCompile
and substituting my ownByteArray
of externally compiled llvm bitcode, but it blocks that by overwriting the array on evaluation. AlsoProgram
andLLVMString
stay unevaluated in 12.3 so something must have changed there too. $\endgroup$