llvm jit windows 8.1

I'm trying run fibonacci demo on windows 8.1 compiled by VS 2013. I solved several issues so far: 1. Incompatible object file format - I changed Triple::COFF to Triple::ELF in function getDefaultFormat(const Triple &T) for windows 2. Missing asm printer - I added InitializeNativeTargetAsmPrinter() 3. Memory Protection - I manually set VirtualProtect to PAGE_EXECUTE (it's nowhere set, I don't know how this was supposed to work)

Now I'm able to run generated code, but it crashes, here is disassembled code: 000000EC4B890000 push rsi
000000EC4B890001 push rdi
000000EC4B890002 push rbx
000000EC4B890003 sub rsp,20h
000000EC4B890007 mov esi,ecx
000000EC4B890009 cmp esi,2
000000EC4B89000C jg 000000EC4B890015
000000EC4B89000E mov eax,1
000000EC4B890013 jmp 000000EC4B89002F
000000EC4B890015 lea ecx,[rsi-1]
000000EC4B890018 mov rbx,0
000000EC4B890022 call rbx
000000EC4B890024 mov edi,eax
000000EC4B890026 add esi,0FFFFFFFEh
000000EC4B890029 mov ecx,esi
000000EC4B89002B call rbx
000000EC4B89002D add eax,edi
000000EC4B89002F add rsp,20h
000000EC4B890033 pop rbx
000000EC4B890034 pop rdi
000000EC4B890035 pop rsi
000000EC4B890036 ret

on address 000000EC4B890018 is instruction mov rbx,0 and immediately after that call rbx and that leads to crash. And also there's another strange thing, the call stack seems to be corrupted, after prologue there are functions in debugger call stack which wasn't called.

Here is IR:

define i32 @fib(i32 %AnArg) {
EntryBlock:
  %cond = icmp sle i32 %AnArg, 2
  br i1 %cond, label %return, label %recurse

return:                                           ; preds = %EntryBlock
  ret i32 1

recurse:                                          ; preds = %EntryBlock
  %arg = sub i32 %AnArg, 1
  %fibx1 = tail call i32 @fib(i32 %arg)
  %arg1 = sub i32 %AnArg, 2
  %fibx2 = tail call i32 @fib(i32 %arg1)
  %addresult = add i32 %fibx1, %fibx2
  ret i32 %addresult
}

I'm running windows 8.1 professional LLVM was compiled in x64 configuration with VS 2013 community edition I'm using LLVM 3.6.0

Note: x86 configuration works (except interpreter in release, it crashes)

Is there something I missed? Should I use ELF for x64 or COFF(I think it's not implemented in JIT)?

I'm starting a big project. The performance is critical and I would like use a script in it, but it has to be jitted. Most important platforms are Windows, Android and iOS. I'm concerned about llvm's JIT support for these platforms. I know llvm and clang is widely used on all these platforms, but I'm not so sure about JIT because of the problems I faced so far.


I had the exactly same issue with Windows 8.1, Visual Studio 2013, LLVM 3.6.1 (-elf hack). The JIT'ed code wasn't resolved (mov reg, 0x0) and memory pages weren't executable (immediate crash).

The problem is: the JIT module is not actually finalized, I think it might come from the -elf hack. To solve this issue, I have to call explicitly ExecutionEngine::finalizeObject between ExecutionEngine::getFunctionAddress (which is supposed to make sure the module is finalized) and the call to the JIT'ed code.

// Call the JIT'ed code
auto pCode = (BasicBlockCode)sm_pExecutionEngine->getFunctionAddress(pExecFunc->getName());
// NOTE: We must call this method explicitly since it seems getFunctionAddress won't call it
// -elf windows hack?
sm_pExecutionEngine->finalizeObject();
pCode(reinterpret_cast<u8*>(m_pCpuCtxt), reinterpret_cast<u8*>(m_pMemCtxt));

MC JIT does not yet (though coming soon: http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20150216/261096.html) support dynamically loadable COFF objects.

Therefore, if you want cross-platform LLVM JIT, you need to check if you're running on Windows, and then change your target triple to append "-elf".

I'm not familiar with the C++ API as much, so I've an example that I've put (using LLVM .NET C# Bindings):

var platform = Environment.OSVersion.Platform;
if (platform == PlatformID.Win32NT) // On Windows, LLVM currently (3.6) does not support PE/COFF
{
   LLVM.SetTarget(mod, Marshal.PtrToStringAnsi(LLVM.GetDefaultTargetTriple()) + "-elf");
}
链接地址: http://www.djcxy.com/p/86836.html

上一篇: snprintf和Visual Studio 2010

下一篇: llvm jit windows 8.1