llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.8k stars 11.91k forks source link

[RISC-V][ORCJIT] PostgreSql JIT fails with "error during JITing: In graph pg-jitted-objectbuffer, section .text: relocation target "CurrentMemoryContext" at address 0x555555e84b00 is out of range of R_RISCV_PCREL_HI20 fixup at 0x7f221c4bb1e4 (evalexpr_0_0, 0x7f221c4bb000 + 0x1e4)" #106203

Open andreas-schwab opened 2 months ago

andreas-schwab commented 2 months ago

When postgresql is configured with --with-llvm the regression testsuite causes a lot of crashed that all look like this:

0 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::_M_set_node (__new_node=0x19db8a08, this=)

at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:266

1 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::operator+= (__n=, this=)

at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:241

2 std::operator+ (x=..., n=)

at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:400

3 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::operator[] (this=, __n=)

at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:255

4 std::deque<llvm::SectionEntry, std::allocator >::operator[] (this=, __n=)

at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:1386

5 llvm::RuntimeDyldELF::computePlaceholderAddress (this=,

SectionID=<optimized out>, Offset=848)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp:1086

6 0x0000003f83fc33ca in llvm::RuntimeDyldImpl::resolveRelocationList (

this=0xb117b80, Relocs=..., Value=0)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1106

7 llvm::RuntimeDyldImpl::applyExternalSymbolRelocations (this=0xb117b80,

ExternalSymbolMap=<error reading variable: Cannot access memory at address 0x18>)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1119

8 0x000000000a287310 in ?? ()

IIUC RuntimeDyld is part of MC JIT and should not be used by ORC JIT.

llvmbot commented 2 months ago

@llvm/issue-subscribers-backend-risc-v

Author: Andreas Schwab (andreas-schwab)

When postgresql is configured with --with-llvm the regression testsuite causes a lot of crashed that all look like this: #0 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::_M_set_node (__new_node=0x19db8a08, this=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:266 #1 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::operator+= (__n=<optimized out>, this=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:241 #2 std::operator+ (__x=..., __n=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:400 #3 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::operator[] (this=<optimized out>, __n=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:255 #4 std::deque<llvm::SectionEntry, std::allocator<llvm::SectionEntry> >::operator[] (this=<optimized out>, __n=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:1386 #5 llvm::RuntimeDyldELF::computePlaceholderAddress (this=<optimized out>, SectionID=<optimized out>, Offset=848) at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp:1086 #6 0x0000003f83fc33ca in llvm::RuntimeDyldImpl::resolveRelocationList ( this=0xb117b80, Relocs=..., Value=0) at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1106 #7 llvm::RuntimeDyldImpl::applyExternalSymbolRelocations (this=0xb117b80, ExternalSymbolMap=<error reading variable: Cannot access memory at address 0x18>) at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1119 #8 0x000000000a287310 in ?? () IIUC RuntimeDyld is part of MC JIT and should not be used by ORC JIT.
llvmbot commented 2 months ago

@llvm/issue-subscribers-orcjit

Author: Andreas Schwab (andreas-schwab)

When postgresql is configured with --with-llvm the regression testsuite causes a lot of crashed that all look like this: #0 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::_M_set_node (__new_node=0x19db8a08, this=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:266 #1 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::operator+= (__n=<optimized out>, this=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:241 #2 std::operator+ (__x=..., __n=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:400 #3 std::_Deque_iterator<llvm::SectionEntry, llvm::SectionEntry&, llvm::SectionEntry*>::operator[] (this=<optimized out>, __n=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:255 #4 std::deque<llvm::SectionEntry, std::allocator<llvm::SectionEntry> >::operator[] (this=<optimized out>, __n=<optimized out>) at /usr/lib64/gcc/riscv64-suse-linux/13/../../../../include/c++/13/bits/stl_deque.h:1386 #5 llvm::RuntimeDyldELF::computePlaceholderAddress (this=<optimized out>, SectionID=<optimized out>, Offset=848) at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp:1086 #6 0x0000003f83fc33ca in llvm::RuntimeDyldImpl::resolveRelocationList ( this=0xb117b80, Relocs=..., Value=0) at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1106 #7 llvm::RuntimeDyldImpl::applyExternalSymbolRelocations (this=0xb117b80, ExternalSymbolMap=<error reading variable: Cannot access memory at address 0x18>) at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1119 #8 0x000000000a287310 in ?? () IIUC RuntimeDyld is part of MC JIT and should not be used by ORC JIT.
andreas-schwab commented 2 months ago

0 llvm::RuntimeDyldImpl::applyExternalSymbolRelocations (this=0x243a91b0,

ExternalSymbolMap=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1112

1 0x0000003f985c3f36 in llvm::RuntimeDyldImpl::finalizeAsync(std::unique_ptr<llvm::RuntimeDyldImpl, std::default_delete >, llvm::unique_function<void (llvm::object::OwningBinary, std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo, std::default_delete >, llvm::Error)>, llvm::object::OwningBinary, std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo, std::default_delete >)::$_0::operator()(llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > >) (

this=0x2443f6c0, Result=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1240

2 0x0000003f985c5cfa in llvm::detail::UniqueFunctionBase<void, llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > > >::CallImpl<llvm::RuntimeDyldImpl::finalizeAsync(std::unique_ptr<llvm::RuntimeDyldImpl, std::default_delete >, llvm::unique_function<void (llvm::object::OwningBinary, std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo, std::default_delete >, llvm::Error)>, llvm::object::OwningBinary, std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo, std::default_delete >)::$_0>(void*, llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > >&) (

CallableAddr=0x243a91b0, Params=...)
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:221

3 0x0000003f9859f4ee in llvm::unique_function<void (llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > >)>::operator()(llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > >) (this=, Params=...)

at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:385

4 (anonymous namespace)::JITDylibSearchOrderResolver::lookup(std::set<llvm::StringRef, std::less, std::allocator > const&, llvm::unique_function<void (llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > >)>)::{lambda(llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >)#1}::operator()(llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >) (this=,

InternedResult=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp:43

5 llvm::detail::UniqueFunctionBase<void, llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > > >::CallImpl<(anonymous namespace)::JITDylibSearchOrderResolver::lookup(std::set<llvm::StringRef, std::less, std::allocator > const&, llvm::unique_function<void (llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > >)>)::{lambda(llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >)#1}>(void*, llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >&) (CallableAddr=, Params=...)

at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:221

6 0x0000003f9852f49e in llvm::unique_function<void (llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >)>::operator()(llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >) (this=,

Params=...)
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:385

7 llvm::orc::AsynchronousSymbolQuery::handleComplete(llvm::orc::ExecutionSession&)::RunQueryCompleteTask::run() (this=)

at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:204

8 0x0000003f98531f36 in llvm::detail::UniqueFunctionBase<void, std::unique_ptr<llvm::orc::Task, std::default_delete > >::CallImpl<void (*)(std::unique_ptr<llvm::orc::Task, std::default_delete >)> (

CallableAddr=<optimized out>, Params=...)
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:221

9 0x0000003f9851a91e in llvm::unique_function<void (std::unique_ptr<llvm::orc::Task, std::default_delete >)>::operator()(std::unique_ptr<llvm::orc::Task, std::default_delete >) (this=,

Params=std::unique_ptr<llvm::orc::Task> = {...})
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:385

10 llvm::orc::ExecutionSession::dispatchTask (this=0x240e7110, T=...)

at /usr/src/debug/llvm-18.1.8.src/include/llvm/ExecutionEngine/Orc/Core.h:1603

11 llvm::orc::AsynchronousSymbolQuery::handleComplete (this=0x243845c8,

ES=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:214

12 0x0000003f98520d0e in llvm::orc::JITDylib::resolve (this=0x24348870,

MR=..., Resolved=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:1047

13 0x0000003f9851b4c6 in llvm::orc::ExecutionSession::OL_notifyResolved (

MR=..., this=<optimized out>, Symbols=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:3034

14 llvm::orc::MaterializationResponsibility::notifyResolved (

this=0x7e29cdc87607ee00, Symbols=...)
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ExecutionEngine/Orc/Core.h:1965

15 llvm::orc::AbsoluteSymbolsMaterializationUnit::materialize (

this=<optimized out>, 
R=std::unique_ptr<llvm::orc::MaterializationResponsibility> = {...})
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:274

16 0x0000003f98526ce0 in llvm::orc::MaterializationTask::run (

this=<optimized out>)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:1921

17 0x0000003f98531f36 in llvm::detail::UniqueFunctionBase<void, std::unique_ptr<llvm::orc::Task, std::default_delete > >::CallImpl<void (*)(std::unique_ptr<llvm::orc::Task, std::default_delete >)> (

CallableAddr=<optimized out>, Params=...)
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:221

18 0x0000003f985283c2 in llvm::unique_function<void (std::unique_ptr<llvm::orc::Task, std::default_delete >)>::operator()(std::unique_ptr<llvm::orc::Task, std::default_delete >) (this=0x240e7170,

Params=std::unique_ptr<llvm::orc::Task> = {...})
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:385

19 llvm::orc::ExecutionSession::dispatchTask (this=0x240e7110, T=...)

at /usr/src/debug/llvm-18.1.8.src/include/llvm/ExecutionEngine/Orc/Core.h:1603

20 llvm::orc::ExecutionSession::dispatchOutstandingMUs (this=0x240e7110)

at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2309

21 0x0000003f9852acac in llvm::orc::ExecutionSession::OL_completeLookup(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete >, std::shared_ptr, std::function<void (llvm::DenseMap<llvm::orc::JITDylib, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > const&)>) (this=, IPLS=...,

Q=std::shared_ptr<llvm::orc::AsynchronousSymbolQuery> (use count 2, weak count 0) = {...}, RegisterDependencies=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2919

22 0x0000003f98532a6a in llvm::orc::InProgressFullLookupState::complete (

this=<optimized out>, IPLS=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:580

23 0x0000003f9851e19e in llvm::orc::ExecutionSession::OL_applyQueryPhase1 (

this=<optimized out>, 
IPLS=std::unique_ptr<llvm::orc::InProgressLookupState> = {...}, Err=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2675

24 0x0000003f9851c760 in llvm::orc::ExecutionSession::lookup(llvm::orc::LookupKind, std::vector<std::pair<llvm::orc::JITDylib, llvm::orc::JITDylibLookupFlags>, std::allocator<std::pair<llvm::orc::JITDylib, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet, llvm::orc::SymbolState, llvm::unique_function<void (llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >)>, std::function<void (llvm::DenseMap<llvm::orc::JITDylib, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > const&)>) (this=0x240e7110,

K=llvm::orc::LookupKind::Static, SearchOrder=..., Symbols=..., 
RequiredState=llvm::orc::SymbolState::Resolved, NotifyComplete=..., 
RegisterDependencies=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2147

25 0x0000003f9859f028 in (anonymous namespace)::JITDylibSearchOrderResolver::lookup(std::set<llvm::StringRef, std::less, std::allocator > const&, llvm::unique_function<void (llvm::Expected<std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > > >)>) (

this=0x3fcb1f6750, Symbols=..., OnResolved=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp:54

26 0x0000003f985c3c04 in llvm::RuntimeDyldImpl::finalizeAsync(std::unique_ptr<llvm::RuntimeDyldImpl, std::default_delete >, llvm::unique_function<void (llvm::object::OwningBinary, std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo, std::default_delete >, llvm::Error)>, llvm::object::OwningBinary, std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo, std::default_delete >) (This=..., OnEmitted=..., O=...,

Info=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1265

27 0x0000003f985c4f78 in llvm::jitLinkForORC(llvm::object::OwningBinary, llvm::RuntimeDyld::MemoryManager&, llvm::JITSymbolResolver&, bool, llvm::unique_function<llvm::Error (llvm::object::ObjectFile const&, llvm::RuntimeDyld::LoadedObjectInfo&, std::map<llvm::StringRef, llvm::JITEvaluatedSymbol, std::less, std::allocator<std::pair<llvm::StringRef const, llvm::JITEvaluatedSymbol> > >)>, llvm::unique_function<void (llvm::object::OwningBinary, std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo, std::default_delete >, llvm::Error)>) (O=..., MemMgr=..., Resolver=..., ProcessAllSections=false, OnLoaded=...,

OnEmitted=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1472

28 0x0000003f9859d694 in llvm::orc::RTDyldObjectLinkingLayer::emit (

this=0x2434bea0, R=..., O=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp:189

29 0x0000003f98596ed4 in llvm::orc::ObjectTransformLayer::emit (

this=0x24350b00, 
R=std::unique_ptr<llvm::orc::MaterializationResponsibility> = {...}, 
O=std::unique_ptr<llvm::MemoryBuffer> = {...})
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/ObjectTransformLayer.cpp:40

30 0x0000003f9855ef24 in llvm::orc::IRCompileLayer::emit (this=0x2432f180,

R=std::unique_ptr<llvm::orc::MaterializationResponsibility> = {...}, 
TSM=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/IRCompileLayer.cpp:40

31 0x0000003f9855f29e in llvm::orc::IRTransformLayer::emit (this=0x24345b50,

R=std::unique_ptr<llvm::orc::MaterializationResponsibility> = {...}, 
TSM=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/IRTransformLayer.cpp:25

32 0x0000003f9855f29e in llvm::orc::IRTransformLayer::emit (this=0x24358910,

R=std::unique_ptr<llvm::orc::MaterializationResponsibility> = {...}, 
TSM=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/IRTransformLayer.cpp:25

33 0x0000003f98563d66 in llvm::orc::BasicIRLayerMaterializationUnit::materialize (this=0x24396070,

R=std::unique_ptr<llvm::orc::MaterializationResponsibility> = {...})
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Layer.cpp:158

34 0x0000003f98526ce0 in llvm::orc::MaterializationTask::run (

this=<optimized out>)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:1921

35 0x0000003f98531f36 in llvm::detail::UniqueFunctionBase<void, std::unique_ptr<llvm::orc::Task, std::default_delete > >::CallImpl<void (*)(std::unique_ptr<llvm::orc::Task, std::default_delete >)> (

CallableAddr=<optimized out>, Params=...)
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:221

36 0x0000003f985283c2 in llvm::unique_function<void (std::unique_ptr<llvm::orc::Task, std::default_delete >)>::operator()(std::unique_ptr<llvm::orc::Task, std::default_delete >) (this=0x240e7170,

Params=std::unique_ptr<llvm::orc::Task> = {...})
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ADT/FunctionExtras.h:385

37 llvm::orc::ExecutionSession::dispatchTask (this=0x240e7110, T=...)

at /usr/src/debug/llvm-18.1.8.src/include/llvm/ExecutionEngine/Orc/Core.h:1603

38 llvm::orc::ExecutionSession::dispatchOutstandingMUs (this=0x240e7110)

at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2309

39 0x0000003f9852acac in llvm::orc::ExecutionSession::OL_completeLookup(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete >, std::shared_ptr, std::function<void (llvm::DenseMap<llvm::orc::JITDylib, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > const&)>) (this=, IPLS=...,

Q=std::shared_ptr<llvm::orc::AsynchronousSymbolQuery> (use count 2, weak count 0) = {...}, RegisterDependencies=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2919

40 0x0000003f98532a6a in llvm::orc::InProgressFullLookupState::complete (

this=<optimized out>, IPLS=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:580

41 0x0000003f9851e19e in llvm::orc::ExecutionSession::OL_applyQueryPhase1 (

this=<optimized out>, 
IPLS=std::unique_ptr<llvm::orc::InProgressLookupState> = {...}, Err=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2675

42 0x0000003f9851c760 in llvm::orc::ExecutionSession::lookup(llvm::orc::LookupKind, std::vector<std::pair<llvm::orc::JITDylib, llvm::orc::JITDylibLookupFlags>, std::allocator<std::pair<llvm::orc::JITDylib, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet, llvm::orc::SymbolState, llvm::unique_function<void (llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >)>, std::function<void (llvm::DenseMap<llvm::orc::JITDylib, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > const&)>) (this=0x240e7110,

K=llvm::orc::LookupKind::Static, SearchOrder=..., Symbols=..., 
RequiredState=llvm::orc::SymbolState::Ready, NotifyComplete=..., 
RegisterDependencies=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2147

43 0x0000003f98528536 in llvm::orc::ExecutionSession::lookup(std::vector<std::pair<llvm::orc::JITDylib, llvm::orc::JITDylibLookupFlags>, std::allocator<std::pair<llvm::orc::JITDylib, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet, llvm::orc::LookupKind, llvm::orc::SymbolState, std::function<void (llvm::DenseMap<llvm::orc::JITDylib, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > const&)>) (this=0x240e7110,

SearchOrder=std::vector of length 1, capacity 1 = {...}, Symbols=..., 
K=llvm::orc::LookupKind::Static, 
RequiredState=llvm::orc::SymbolState::Ready, RegisterDependencies=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2184

44 0x0000003f98528962 in llvm::orc::ExecutionSession::lookup (

this=0x240e7110, SearchOrder=std::vector of length 1, capacity 1 = {...}, 
Name=..., RequiredState=llvm::orc::SymbolState::Ready)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/Core.cpp:2209

45 0x0000003f98566ee0 in llvm::orc::LLJIT::lookupLinkerMangled (

this=<optimized out>, JD=..., Name=...)
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/LLJIT.cpp:874

46 0x0000003f9859b8ce in llvm::orc::LLJIT::lookupLinkerMangled (

this=0x24345830, JD=..., Name=...)
at /usr/src/debug/llvm-18.1.8.src/include/llvm/ExecutionEngine/Orc/LLJIT.h:164

47 llvm::orc::LLJIT::lookup (this=0x24345830, JD=..., UnmangledName=...)

at /usr/src/debug/llvm-18.1.8.src/include/llvm/ExecutionEngine/Orc/LLJIT.h:176

48 llvm::orc::LLJIT::lookup (this=0x24345830, UnmangledName=...)

at /usr/src/debug/llvm-18.1.8.src/include/llvm/ExecutionEngine/Orc/LLJIT.h:181

49 LLVMOrcLLJITLookup (J=0x24345830, Result=Result@entry=0x3fcb1f7288,

Name=<optimized out>, Name@entry=0x242e3a90 "evalexpr_0_3")
at /usr/src/debug/llvm-18.1.8.src/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp:1001
lhames commented 2 months ago

IIUC RuntimeDyld is part of MC JIT and should not be used by ORC JIT.

ORC can use RuntimeDyld or JITLink as the underlying JIT linker.

RuntimeDyld does not support RISC-V, at least not in llvm.org/main -- Is your JIT'd code actually targeting RISC-V?

andreas-schwab commented 2 months ago

2024-08-28 18:08:52.635 CEST client backend[48551] pg_regress/boolean DEBUG: LLVMJIT detected CPU "sifive-u74", with features ""

andreas-schwab commented 2 months ago

ORC can use RuntimeDyld or JITLink as the underlying JIT linker.

How does it decide?

andreas-schwab commented 2 months ago

What is the JITLink equivalent of LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager?

alexfanqi commented 2 months ago

I had this issue on Gentoo and have a old patch that I haven't got time to update. It is probably needed to be rebased/modified. PS: pls ignore the comments I made on that thread about riscv ABIs. It is probably no longer valid or wrong.

https://www.postgresql.org/message-id/flat/20220829074622.2474104-1-alex.fan.q%40gmail.com

lhames commented 2 months ago

ORC can use RuntimeDyld or JITLink as the underlying JIT linker.

How does it decide?

LLJIT uses the following test(s): https://github.com/llvm/llvm-project/blob/2eeeff842f993a694159183a2834b4d305549cad/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp#L776

If you're constructing your JIT manually then it just depends on whether you use an RTDyldObjectLinkingLayer (RuntimeDyld), or ObjectLinkingLayer (JITLink).

In LLVM 20 we'll be aiming to switch the default to JITLink, with fallbacks to RuntimeDyld for architectures where JITLink is unavailable / under-development.

What is the JITLink equivalent of LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager?

There isn't one yet, but we could easily add an LLVMOrcCreateObjectLinkingLayerWithInProcessMemoryManager API if you're able to try it out?

andreas-schwab commented 2 months ago

WARNING: error during JITing: In graph pg-jitted-objectbuffer, section .text: relocation target "CurrentMemoryContext" at address 0x802398 is out of range of R_RISCV_PCREL_HI20 fixup at 0x3f8c165162 (evalexpr_0_0, 0x3f8c165000 + 0x162) ERROR: failed to look up symbol "evalexpr_0_0": Failed to materialize symbols: { (main, { evalexpr_0_11, evalexpr_0_13, evalexpr_0_5, evalexpr_0_10, evalexpr_0_4, evalexpr_0_7, evalexpr_0_0, evalexpr_0_9, evalexpr_0_3, evalexpr_0_1 }) }

WARNING: error during JITing: In graph pg-jitted-objectbuffer, section .text: relocation target "CurrentMemoryContext" at address 0x802398 is out of range of R_RISCV_PCREL_HI20 fixup at 0x3f8c166182 (evalexpr_0_0, 0x3f8c166000 + 0x182) ERROR: failed to look up symbol "evalexpr_0_1": Failed to materialize symbols: { (main, { evalexpr_0_1, evalexpr_0_0 }) }

lhames commented 2 months ago

How is CurrentMemoryContext declared within the JIT'd module? And what code / relocation model are you using for compilation?

jrtc27 commented 2 months ago

The source is using the default model. The problem is that RISC-V's default isn't large, which is perfectly fine for normal compiled code but generally too restrictive for a JIT. We'll want to do something similar to what getEffectiveAArch64CodeModel does (the AArch64 and RISC-V code models are similar in their restrictions) when it sees a JIT is in use and pick a different code model, likely by changing RISCVTargetMachine to pass JIT ? CodeModel::Large : CodeModel::Small to getEffectiveCodeModel, though we don't actually support a large code model currently (it's in review). Until then maybe forcing PIC will work, as that will add GOT indirection.

lhames commented 2 months ago

How are small/static references to external symbols handled by AOT compilation? Presumably these could be at any place in the address range too?

Or is the default Small / PIC? In which case we should have the JIT default to that too.

In general we assume that the JIT is subject to the same constraints as regular compilation: code within a JITDylib must be within the range specified by the code model, and in general external references may be located anywhere in memory.

jrtc27 commented 2 months ago

Helped by a combination of copy relocations and PLTs. You can do PLTs for a JIT, but you can't do copy relocations for existing symbols.

lhames commented 2 months ago

but you can't do copy relocations for existing symbols.

Where are copy relocations documented? The RISCV ABI doc that I found wasn't much help.

I'm assuming CurrentMemoryContext is an external data reference -- how are they handled in small / static? E.g.

extern int x;

int getX() { return x; }

just produces R_RISCV_GOT_HI20 / R_RISCV_PCREL_LO12_I when I compile it with clang --target=riscv64-linux -static -mcmodel=small -c.

lhames commented 2 months ago

@andreas-schwab Sounds like it might be worth specifying PIC relocations.

(thought eventually I hope the JIT will support the default code / relocation model for RISCV, since that makes it easy for users to import precompiled libraries).

andreas-schwab commented 2 months ago

That's what the patch does?

+#ifdef __riscv

lhames commented 2 months ago

@alexfanqi's patch? Looks like iit -- but I think the default code model (probably small?) should be fine, as long as the reloc model is PIC.

lhames commented 1 month ago

Where are copy relocations documented? The RISCV ABI doc that I found wasn't much help.

Finally got around to looking for this again: https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter4-84604/index.html

You can do PLTs for a JIT, but you can't do copy relocations for existing symbols.

I think the full answer to this is: JIT'd code is always shared library code (conceptually), since it's necessarily loaded after the main executable, and is distinct from it. I'll make PIC the default relocation model for ELF in LLJIT.

At a stretch we could support copy relocations in JIT'd code under two conditions:

  1. The definition of the global and all users of the global to be copied are in JIT'd code.
  2. We get support from the system dynamic loader.

Interesting cases to consider, but I don't think they're a priority right now.

andreas-schwab commented 1 month ago

WARNING: error during JITing: In graph pg-jitted-objectbuffer, section .text: relocation target "CurrentMemoryContext" at address 0x555555e84b00 is out of range of R_RISCV_PCREL_HI20 fixup at 0x7f221c4bb1e4 (evalexpr_0_0, 0x7f221c4bb000 + 0x1e4)

lhames commented 1 month ago

WARNING: error during JITing: In graph pg-jitted-objectbuffer, section .text: relocation target "CurrentMemoryContext" at address 0x555555e84b00 is out of range of R_RISCV_PCREL_HI20 fixup at 0x7f221c4bb1e4 (evalexpr_0_0, 0x7f221c4bb000 + 0x1e4)

What code / relocation model did this occur under?

andreas-schwab commented 1 month ago

+#ifdef __riscv

andreas-schwab commented 1 month ago

With LLVMCodeModelLarge nothing changes, still the same JIT error.

jrtc27 commented 1 month ago

With what version? RISC-V only gained large code model support recently in main, with older versions falling back I assume to medium aka medany.

andreas-schwab commented 1 month ago

I'm testing with LLVM 18.