LLVMSharp icon indicating copy to clipboard operation
LLVMSharp copied to clipboard

Implementation of "extern" functions in Kaleidoscope

Open inumanag opened this issue 7 years ago • 1 comments

Hi, I was trying to add support for "extern" functions from libc in Kaleidoscope (which come for free in C++ version of Kaleidoscope). However, for the following IR (trying to use sin from libc):

declare double @sin(double %f)
%calltmp = call double @sin(double 9.000000e+00)

I keep getting:

Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file /tmp/llvm-5.0.0.src/include/llvm/Support/Casting.h, line 255.
Stacktrace:
  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) LLVMSharp.LLVM.GetPointerToGlobal (LLVMSharp.LLVMExecutionEngineRef,LLVMSharp.LLVMValueRef) [0x00002] in <c6808a9c612144c69fe813302a5e9c29>:0

Will this require P/Invoke of any sort, or it is not doable from .NET framework as of now? Btw. I am using MCJIT as specified in the tutorial.

Thanks!

inumanag avatar Apr 12 '18 02:04 inumanag

Hey inumanag I figured out how to solve this issue.

delgate double Unary(double);
LLVMValueRef sin = LLVM.GetNamedFunction(visitor.Module, "sin");
LLVM.AddGlobalMapping(engine, sin, Marshal.GetFunctionPointerForDelegate<Unary>(Math.Sin));

You need to manually map the values, I'm unsure how to map an entire lib file (still learning myself) but that should help you out hopefully.

ChaseLewis avatar May 02 '18 23:05 ChaseLewis