Re: JIT compiling with LLVM v12.2

Thomas Munro <thomas.munro@enterprisedb.com>

From: Thomas Munro <thomas.munro@enterprisedb.com>
To: Andres Freund <andres@anarazel.de>
Cc: Stephen Frost <sfrost@snowman.net>, Pg Hackers <pgsql-hackers@postgresql.org>
Date: 2018-03-21T21:09:23Z
Lists: pgsql-hackers
On Thu, Mar 22, 2018 at 9:59 AM, Andres Freund <andres@anarazel.de> wrote:
> On 2018-03-22 09:51:01 +1300, Thomas Munro wrote:
>> Hah, that makes sense.  I tried setting cpu to "x86", and now it fails
>> differently:
>
> Did you change the variable, or replace the value that's passed to the
> LLVMCreateTargetMachine() calls?  If you did the former, the error
> wouldn't be surprising, because
>         LLVMDisposeMessage(cpu);
>         cpu = NULL;
> will attempt to free the return value of LLVMGetHostCPUName(), which'll
> obviously not work if you just set to a constant.

Duh.  Right.

> FWIW, a 32bit chroot, on a 64bit kernel works:
>
> 2018-03-21 20:57:56.576 UTC [3708] DEBUG:  successfully loaded LLVM in current session
> 2018-03-21 20:57:56.577 UTC [3708] DEBUG:  JIT detected CPU "skylake", with features "+sse2,+cx16,-tbm,-avx512ifma,-avx512dq,-fma4,+prfchw,+bmi2,+xsavec,+fsgsbase,+popcnt,+aes,-pcommit,+xsaves,-avx512er,-clwb,-avx512f,-pku,+smap,+mmx,-xop,+rdseed,+hle,-sse4a,-avx512bw,+clflushopt,+xsave,-avx512vl,+invpcid,-avx512cd,+avx,+rtm,+fma,+bmi,-mwaitx,+rdrnd,+sse4.1,+sse4.2,+avx2,+sse,+lzcnt,+pclmul,-prefetchwt1,+f16c,+ssse3,+sgx,+cmov,-avx512vbmi,+movbe,+xsaveopt,-sha,+adx,-avx512pf,+sse3"
> 2018-03-21 20:57:56.579 UTC [3708] DEBUG:  time to inline: 0.000s, opt: 0.000s, emit: 0.002s
>
> that's debian testing though.

Hmm.  So now I'm doing this:

        llvm_opt0_targetmachine =
-               LLVMCreateTargetMachine(llvm_targetref, llvm_triple,
cpu, features,
+               LLVMCreateTargetMachine(llvm_targetref, llvm_triple,
"x86" /*cpu*/, "" /*features*/,

LLVMCodeGenLevelNone,

LLVMRelocDefault,

LLVMCodeModelJITDefault);
        llvm_opt3_targetmachine =
-               LLVMCreateTargetMachine(llvm_targetref, llvm_triple,
cpu, features,
+               LLVMCreateTargetMachine(llvm_targetref, llvm_triple,
"x86" /*cpu*/, "" /*features*/,

LLVMCodeGenLevelAggressive,

LLVMRelocDefault,

LLVMCodeModelJITDefault);

And I'm still getting a segfault:

(gdb)
#0  0xac22c453 in llvm::SelectionDAG::getNode(unsigned int,
llvm::SDLoc const&, llvm::EVT, llvm::SDValue) () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/SelectionDAG.cpp:2898
#1  0xac269c29 in llvm::TargetLowering::SimplifySetCC(llvm::EVT,
llvm::SDValue, llvm::SDValue, llvm::ISD::CondCode, bool,
llvm::TargetLowering::DAGCombinerInfo&, llvm::SDLoc const&) const ()
    at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/TargetLowering.cpp:1480
#2  0xac1163a8 in (anonymous
namespace)::DAGCombiner::visit(llvm::SDNode*) () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:14438
#3  0xac117f0b in (anonymous
namespace)::DAGCombiner::combine(llvm::SDNode*) () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1449
#4  0xac11930e in llvm::SelectionDAG::Combine(llvm::CombineLevel,
llvm::AAResults&, llvm::CodeGenOpt::Level) () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1303
#5  0xac245cec in llvm::SelectionDAGISel::CodeGenAndEmitDAG() () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:755
#6  0xac246239 in
llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::Instruction
const>, llvm::ilist_iterator<llvm::Instruction const>, bool&) ()
    at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:679
#7  0xac24d66f in
llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) ()
at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:1482
#8  0xac25073c in
llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&)
() at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:500
#9  0xad34f414 in (anonymous
namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&)
() at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/Target/X86/X86ISelDAGToDAG.cpp:175
#10 0xabf53019 in
llvm::MachineFunctionPass::runOnFunction(llvm::Function&) () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/CodeGen/MachineFunctionPass.cpp:60
#11 0xabde8aeb in llvm::FPPassManager::runOnFunction(llvm::Function&)
() at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/IR/LegacyPassManager.cpp:1526
#12 0xabde8e35 in llvm::FPPassManager::runOnModule(llvm::Module&) ()
at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/IR/LegacyPassManager.cpp:1547
#13 0xabde919a in llvm::legacy::PassManagerImpl::run(llvm::Module&) ()
at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/IR/LegacyPassManager.cpp:1603
#14 0xabde937f in llvm::legacy::PassManager::run(llvm::Module&) () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/IR/LegacyPassManager.cpp:1737
#15 0xacb353de in
std::_Function_handler<llvm::object::OwningBinary<llvm::object::ObjectFile>
(llvm::Module&), llvm::orc::SimpleCompiler>::_M_invoke(std::_Any_data
const&, llvm::Module&) ()
    at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/include/llvm/ExecutionEngine/Orc/CompileUtils.h:42
#16 0xacb30d00 in
std::_List_iterator<std::unique_ptr<llvm::orc::ObjectLinkingLayerBase::LinkedObjectSet,
std::default_delete<llvm::orc::ObjectLinkingLayerBase::LinkedObjectSet>
> > llvm::orc::IRCompileLayer<llvm::orc::ObjectLinkingLayer<llvm::orc::DoNothingOnNotifyLoaded>
>::addModuleSet<std::vector<llvm::Module*,
std::allocator<llvm::Module*> >,
std::unique_ptr<llvm::RuntimeDyld::MemoryManager,
std::default_delete<llvm::RuntimeDyld::MemoryManager> >,
std::unique_ptr<llvm::RuntimeDyld::SymbolResolver,
std::default_delete<llvm::RuntimeDyld::SymbolResolver> >
>(std::vector<llvm::Module*, std::allocator<llvm::Module*> >,
std::unique_ptr<llvm::RuntimeDyld::MemoryManager,
std::default_delete<llvm::RuntimeDyld::MemoryManager> >,
std::unique_ptr<llvm::RuntimeDyld::SymbolResolver,
std::default_delete<llvm::RuntimeDyld::SymbolResolver> >) () at
/usr/include/c++/6/functional:2127
#17 0xacb314f8 in unsigned int
llvm::OrcCBindingsStack::addIRModule<llvm::orc::IRCompileLayer<llvm::orc::ObjectLinkingLayer<llvm::orc::DoNothingOnNotifyLoaded>
> >(llvm::orc::IRCompileLayer<llvm::orc::ObjectLinkingLayer<llvm::orc::DoNothingOnNotifyLoaded>
>&, llvm::Module*, std::unique_ptr<llvm::RuntimeDyld::MemoryManager,
std::default_delete<llvm::RuntimeDyld::MemoryManager> >, unsigned long
long (*)(char const*, void*), void*) ()
    at /build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/ExecutionEngine/Orc/OrcCBindingsStack.h:190
#18 0xacb318d5 in LLVMOrcAddEagerlyCompiledIR () at
/build/llvm-toolchain-3.9-UOOPrK/llvm-toolchain-3.9-3.9.1/lib/ExecutionEngine/Orc/OrcCBindingsStack.h:208
#19 0xae7b43f4 in llvm_compile_module (context=0x2438444) at llvmjit.c:539
#20 llvm_get_function (context=0x2438444, funcname=0x2542b00
"evalexpr_2_3") at llvmjit.c:244
#21 0xae7bc34e in ExecRunCompiledExpr (state=0x247d634,
econtext=0x247c10c, isNull=0xbfadf6ae "~") at llvmjit_expr.c:2563
#22 0x006b3e10 in ExecEvalExprSwitchContext (isNull=0xbfadf6ae "~",
econtext=<optimized out>, state=0x247d634) at
../../../src/include/executor/executor.h:305
#23 ExecQual (econtext=<optimized out>, state=0x247d634) at
../../../src/include/executor/executor.h:374
#24 ExecNestLoop (pstate=<optimized out>) at nodeNestloop.c:214
#25 0x006b6ddd in ExecProcNode (node=0x247c080) at
../../../src/include/executor/executor.h:239
#26 ExecSort (pstate=0x247bff4) at nodeSort.c:107
#27 0x0068c9d2 in ExecProcNode (node=0x247bff4) at
../../../src/include/executor/executor.h:239
#28 ExecutePlan (execute_once=<optimized out>, dest=0x0,
direction=NoMovementScanDirection, numberTuples=<optimized out>,
sendTuples=<optimized out>, operation=CMD_SELECT,
use_parallel_mode=<optimized out>, planstate=0x247bff4,
estate=0x247bee8) at execMain.c:1729
#29 standard_ExecutorRun (queryDesc=0x23e1a50,
direction=ForwardScanDirection, count=0, execute_once=1 '\001') at
execMain.c:365
#30 0x007f1e8d in PortalRunSelect (portal=portal@entry=0x240bf58,
forward=forward@entry=1 '\001', count=0, count@entry=2147483647,
dest=0x25383c0) at pquery.c:932
#31 0x007f36a0 in PortalRun (portal=0x240bf58, count=2147483647,
isTopLevel=1 '\001', run_once=1 '\001', dest=0x25383c0,
altdest=0x25383c0, completionTag=0xbfadf940 "") at pquery.c:773
#32 0x007ee8a7 in exec_simple_query
(query_string=query_string@entry=0x23be628 "SELECT '' AS tf_12_ff_4,
BOOLTBL1.*, BOOLTBL2.*\n   FROM BOOLTBL1, BOOLTBL2\n   WHERE
BOOLTBL2.f1 = BOOLTBL1.f1 or BOOLTBL1.f1 = bool 'true'\n   ORDER BY
BOOLTBL1.f1, BOOLTBL2.f1;")
    at postgres.c:1121
#33 0x007f070e in PostgresMain (argc=1, argv=0x23e7c44,
dbname=<optimized out>, username=0x23e7aa0 "munro") at postgres.c:4147
#34 0x004c0cff in BackendRun (port=0x23e1518) at postmaster.c:4409
#35 BackendStartup (port=0x23e1518) at postmaster.c:4081
#36 ServerLoop () at postmaster.c:1754
#37 0x0076a68f in PostmasterMain (argc=<optimized out>,
argv=<optimized out>) at postmaster.c:1362
#38 0x004c275a in main (argc=<optimized out>, argv=<optimized out>) at
main.c:228

I wonder what I'm doing wrong... what you're doing is very similar,
right?  It's a 32 bit user land on a 64 bit kernel whereas mine is a
32 bit user land on a 32 bit kernel (on a 64 bit CPU).

-- 
Thomas Munro
http://www.enterprisedb.com


Commits

  1. Improve JIT docs.

  2. Add documentation for the JIT feature.

  3. Add EXPLAIN support for JIT.

  4. Add inlining support to LLVM JIT provider.

  5. JIT tuple deforming in LLVM JIT provider.

  6. Add FIELDNO_* macro designating offset into structs required for JIT.

  7. Add expression compilation support to LLVM JIT provider.

  8. Expand list of synchronized types and functions in LLVM JIT provider.

  9. Add helpers for emitting LLVM IR.

  10. Basic planner and executor integration for JIT.

  11. Debugging and profiling support for LLVM JIT provider.

  12. Support for optimizing and emitting code in LLVM JIT provider.

  13. Add file containing extensions of the LLVM C API.

  14. Basic JIT provider and error handling infrastructure.

  15. Add configure infrastructure (--with-llvm) to enable LLVM support.

  16. Add C++ support to configure.

  17. Add PGAC_PROG_VARCC_VARFLAGS_OPT autoconf macro.

  18. Fix VM buffer pin management in heap_lock_updated_tuple_rec().

  19. Allow tupleslots to have a fixed tupledesc, use in executor nodes.

  20. Expression evaluation based aggregate transition invocation.

  21. Perform slot validity checks in a separate pass over expression.

  22. Rely on executor utils to build targetlist for DML RETURNING.

  23. Refer to OS X as "macOS", except for the port name which is still "darwin".